1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! This module contains a list of property tests for the `MVPoly` trait.
//!
//! Any type that implements the `MVPoly` trait should pass these tests.
//!
//! For instance, one can call the `test_mul_by_one` as follows:
//!
//! ```rust
//! use mvpoly::MVPoly;
//! use mvpoly::prime::Dense;
//! use mina_curves::pasta::Fp;
//!
//! #[test]
//! fn test_mul_by_one() {
//!     mvpoly::pbt::test_mul_by_one::<Fp, 2, 2, Dense<Fp, 2, 2>>();
//!     mvpoly::pbt::test_mul_by_one::<Fp, 4, 2, Dense<Fp, 4, 2>>();
//! }
//! ```

use crate::MVPoly;
use ark_ff::PrimeField;
use rand::{seq::SliceRandom, Rng};
use std::ops::Neg;

pub fn test_mul_by_one<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let one = T::one();
    let p2 = p1.clone() * one.clone();
    assert_eq!(p1.clone(), p2);
    let p3 = one * p1.clone();
    assert_eq!(p1.clone(), p3);
}

pub fn test_mul_by_zero<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let zero = T::zero();
    let p2 = p1.clone() * zero.clone();
    assert_eq!(zero, p2);
    let p3 = zero.clone() * p1.clone();
    assert_eq!(zero.clone(), p3);
}

pub fn test_add_zero<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let zero = T::zero();
    let p2 = p1.clone() + zero.clone();
    assert_eq!(p1.clone(), p2);
    let p3 = zero.clone() + p1.clone();
    assert_eq!(p1.clone(), p3);
}

pub fn test_double_is_add_twice<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let p2 = p1.clone() + p1.clone();
    let p3 = p1.clone().double();
    assert_eq!(p2, p3);
}

pub fn test_sub_zero<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let zero = T::zero();
    let p2 = p1.clone() - zero.clone();
    assert_eq!(p1.clone(), p2);
}

pub fn test_neg<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let p2 = -p1.clone();
    // Test that p1 + (-p1) = 0
    let sum = p1.clone() + p2.clone();
    assert_eq!(sum, T::zero());
    // Test that -(-p1) = p1
    let p3 = -p2;
    assert_eq!(p1, p3);
    // Test negation of zero
    let zero = T::zero();
    let neg_zero = -zero.clone();
    assert_eq!(zero, neg_zero);
}

pub fn test_eval_pbt_add<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>()
where
    for<'a> &'a T: std::ops::Add<T, Output = T> + std::ops::Add<&'a T, Output = T>,
{
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let p1 = unsafe { T::random(&mut rng, None) };
    let p2 = unsafe { T::random(&mut rng, None) };
    let eval_p1 = p1.eval(&random_evaluation);
    let eval_p2 = p2.eval(&random_evaluation);
    {
        let p3 = p1.clone() + p2.clone();
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 + eval_p2);
    }
    {
        let p3 = p1.clone() + &p2;
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 + eval_p2);
    }
    {
        let p3 = &p1 + p2.clone();
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 + eval_p2);
    }
    {
        let p3 = &p1 + &p2;
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 + eval_p2);
    }
}

pub fn test_eval_pbt_sub<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>()
where
    for<'a> &'a T: std::ops::Sub<T, Output = T> + std::ops::Sub<&'a T, Output = T>,
{
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let p1 = unsafe { T::random(&mut rng, None) };
    let p2 = unsafe { T::random(&mut rng, None) };
    let eval_p1 = p1.eval(&random_evaluation);
    let eval_p2 = p2.eval(&random_evaluation);
    {
        let p3 = p1.clone() - p2.clone();
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 - eval_p2);
    }
    {
        let p3 = p1.clone() - &p2;
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 - eval_p2);
    }
    {
        let p3 = &p1 - p2.clone();
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 - eval_p2);
    }
    {
        let p3 = &p1 - &p2;
        let eval_p3 = p3.eval(&random_evaluation);
        assert_eq!(eval_p3, eval_p1 - eval_p2);
    }
}

pub fn test_eval_pbt_mul_by_scalar<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let p1 = unsafe { T::random(&mut rng, None) };
    let c = F::rand(&mut rng);
    let p2 = p1.clone() * T::from(c);
    let eval_p1 = p1.eval(&random_evaluation);
    let eval_p2 = p2.eval(&random_evaluation);
    assert_eq!(eval_p2, eval_p1 * c);
}

pub fn test_eval_pbt_neg<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let p1 = unsafe { T::random(&mut rng, None) };
    let p2 = -p1.clone();
    let eval_p1 = p1.eval(&random_evaluation);
    let eval_p2 = p2.eval(&random_evaluation);
    assert_eq!(eval_p2, -eval_p1);
}

pub fn test_neg_ref<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>()
where
    for<'a> &'a T: Neg<Output = T>,
{
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let p2 = -&p1;
    // Test that p1 + (-&p1) = 0
    let sum = p1.clone() + p2.clone();
    assert_eq!(sum, T::zero());
    // Test that -(-&p1) = p1
    let p3 = -&p2;
    assert_eq!(p1, p3);
}

pub fn test_mul_by_scalar<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let mut p2 = T::zero();
    let c = F::rand(&mut rng);
    p2.modify_monomial([0; N], c);
    assert_eq!(p2 * p1.clone(), p1.clone().mul_by_scalar(c));
}

pub fn test_mul_by_scalar_with_zero<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let c = F::zero();
    assert_eq!(p1.mul_by_scalar(c), T::zero());
}

pub fn test_mul_by_scalar_with_one<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    let c = F::one();
    assert_eq!(p1.mul_by_scalar(c), p1);
}

pub fn test_evaluation_zero_polynomial<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let zero = T::zero();
    let evaluation = zero.eval(&random_evaluation);
    assert_eq!(evaluation, F::zero());
}

pub fn test_evaluation_constant_polynomial<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let cst = F::rand(&mut rng);
    let poly = T::from(cst);
    let evaluation = poly.eval(&random_evaluation);
    assert_eq!(evaluation, cst);
}

pub fn test_degree_constant<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let c = F::rand(&mut rng);
    let p = T::from(c);
    let degree = unsafe { p.degree() };
    assert_eq!(degree, 0);
    let p = T::zero();
    let degree = unsafe { p.degree() };
    assert_eq!(degree, 0);
}

pub fn test_degree_random_degree<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let max_degree: usize = rng.gen_range(1..D);
    let p = unsafe { T::random(&mut rng, Some(max_degree)) };
    let degree = unsafe { p.degree() };
    assert!(degree <= max_degree);
}

pub fn test_mvpoly_add_degree_pbt<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let degree = rng.gen_range(1..D);
    let p1 = unsafe { T::random(&mut rng, Some(degree)) };
    let p2 = unsafe { T::random(&mut rng, Some(degree)) };
    let p3 = p1.clone() + p2.clone();
    let degree_p1 = unsafe { p1.degree() };
    let degree_p2 = unsafe { p2.degree() };
    let degree_p3 = unsafe { p3.degree() };
    assert!(degree_p3 <= std::cmp::max(degree_p1, degree_p2));
}

pub fn test_mvpoly_sub_degree_pbt<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let degree = rng.gen_range(1..D);
    let p1 = unsafe { T::random(&mut rng, Some(degree)) };
    let p2 = unsafe { T::random(&mut rng, Some(degree)) };
    let p3 = p1.clone() - p2.clone();
    let degree_p1 = unsafe { p1.degree() };
    let degree_p2 = unsafe { p2.degree() };
    let degree_p3 = unsafe { p3.degree() };
    assert!(degree_p3 <= std::cmp::max(degree_p1, degree_p2));
}

pub fn test_mvpoly_neg_degree_pbt<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let degree = rng.gen_range(1..D);
    let p1 = unsafe { T::random(&mut rng, Some(degree)) };
    let p2 = -p1.clone();
    let degree_p1 = unsafe { p1.degree() };
    let degree_p2 = unsafe { p2.degree() };
    assert_eq!(degree_p1, degree_p2);
}

pub fn test_mvpoly_mul_by_scalar_degree_pbt<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let max_degree = rng.gen_range(1..D / 2);
    let p1 = unsafe { T::random(&mut rng, Some(max_degree)) };
    let c = F::rand(&mut rng);
    let p2 = p1.clone() * T::from(c);
    let degree_p1 = unsafe { p1.degree() };
    let degree_p2 = unsafe { p2.degree() };
    assert!(degree_p2 <= degree_p1);
}

pub fn test_mvpoly_mul_degree_pbt<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let max_degree = rng.gen_range(1..D / 2);
    let p1 = unsafe { T::random(&mut rng, Some(max_degree)) };
    let p2 = unsafe { T::random(&mut rng, Some(max_degree)) };
    let p3 = p1.clone() * p2.clone();
    let degree_p1 = unsafe { p1.degree() };
    let degree_p2 = unsafe { p2.degree() };
    let degree_p3 = unsafe { p3.degree() };
    assert!(degree_p3 <= degree_p1 + degree_p2);
}

pub fn test_mvpoly_mul_eval_pbt<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let max_degree = rng.gen_range(1..D / 2);
    let p1 = unsafe { T::random(&mut rng, Some(max_degree)) };
    let p2 = unsafe { T::random(&mut rng, Some(max_degree)) };
    let p3 = p1.clone() * p2.clone();
    let random_evaluation: [F; N] = std::array::from_fn(|_| F::rand(&mut rng));
    let eval_p1 = p1.eval(&random_evaluation);
    let eval_p2 = p2.eval(&random_evaluation);
    let eval_p3 = p3.eval(&random_evaluation);
    assert_eq!(eval_p3, eval_p1 * eval_p2);
}

pub fn test_mvpoly_mul_pbt<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let max_degree = rng.gen_range(1..D / 2);
    let p1 = unsafe { T::random(&mut rng, Some(max_degree)) };
    let p2 = unsafe { T::random(&mut rng, Some(max_degree)) };
    assert_eq!(p1.clone() * p2.clone(), p2.clone() * p1.clone());
}

pub fn test_can_be_printed_with_debug<
    F: PrimeField,
    const N: usize,
    const D: usize,
    T: MVPoly<F, N, D>,
>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = unsafe { T::random(&mut rng, None) };
    println!("{:?}", p1);
}

pub fn test_is_zero<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let p1 = T::zero();
    assert!(p1.is_zero());
    let p2 = unsafe { T::random(&mut rng, None) };
    assert!(!p2.is_zero());
}

pub fn test_homogeneous_eval<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let random_eval = std::array::from_fn(|_| F::rand(&mut rng));
    let u = F::rand(&mut rng);

    // Homogeneous form is u^2
    let p1 = T::one();
    let homogenous_eval = p1.homogeneous_eval(&random_eval, u);
    assert_eq!(homogenous_eval, u * u);

    let mut p2 = T::zero();
    let mut exp1 = [0; N];
    exp1[0] = 1;
    p2.add_monomial(exp1, F::one());
    let homogenous_eval = p2.homogeneous_eval(&random_eval, u);
    assert_eq!(homogenous_eval, random_eval[0] * u);

    let mut p3 = T::zero();
    let mut exp2 = [0; N];
    exp2[1] = 1;
    p3.add_monomial(exp2, F::one());
    let homogenous_eval = p3.homogeneous_eval(&random_eval, u);
    assert_eq!(homogenous_eval, random_eval[1] * u);

    let mut p4 = T::zero();
    let mut exp3 = [0; N];
    exp3[0] = 1;
    exp3[1] = 1;
    p4.add_monomial(exp3, F::one());
    let homogenous_eval = p4.homogeneous_eval(&random_eval, u);
    assert_eq!(homogenous_eval, random_eval[0] * random_eval[1]);

    let mut p5 = T::zero();
    let mut exp4 = [0; N];
    exp4[0] = 2;
    p5.add_monomial(exp4, F::one());
    let homogenous_eval = p5.homogeneous_eval(&random_eval, u);
    assert_eq!(homogenous_eval, random_eval[0] * random_eval[0]);

    let mut p6 = T::zero();
    let mut exp5a = [0; N];
    let mut exp5b = [0; N];
    exp5a[1] = 2;
    exp5b[0] = 2;
    p6.add_monomial(exp5a, F::one());
    p6.add_monomial(exp5b, F::one());
    let homogenous_eval = p6.homogeneous_eval(&random_eval, u);
    assert_eq!(
        homogenous_eval,
        random_eval[1] * random_eval[1] + random_eval[0] * random_eval[0]
    );

    let mut p7 = T::zero();
    let mut exp6a = [0; N];
    let mut exp6b = [0; N];
    let mut exp6c = [0; N];
    exp6a[1] = 2;
    exp6b[0] = 2;
    exp6c[0] = 1;
    p7.add_monomial(exp6a, F::one());
    p7.add_monomial(exp6b, F::one());
    p7.add_monomial(exp6c, F::one());
    p7.add_monomial([0; N], F::from(42u32));
    let homogenous_eval = p7.homogeneous_eval(&random_eval, u);
    assert_eq!(
        homogenous_eval,
        random_eval[1] * random_eval[1]
            + random_eval[0] * random_eval[0]
            + u * random_eval[0]
            + u * u * F::from(42u32)
    );
}

pub fn test_add_monomial<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);

    // Adding constant monomial one to zero
    let mut p1 = T::zero();
    p1.add_monomial([0; N], F::one());
    assert_eq!(p1, T::one());

    // Adding random constant monomial one to zero
    let mut p2 = T::zero();
    let random_c = F::rand(&mut rng);
    p2.add_monomial([0; N], random_c);
    assert_eq!(p2, T::from(random_c));

    let mut p3 = T::zero();
    let random_c1 = F::rand(&mut rng);
    let random_c2 = F::rand(&mut rng);
    // X1 + X2
    let mut exp1 = [0; N];
    let mut exp2 = [0; N];
    exp1[0] = 1;
    exp2[1] = 1;
    p3.add_monomial(exp1, random_c1);
    p3.add_monomial(exp2, random_c2);

    let random_eval = std::array::from_fn(|_| F::rand(&mut rng));
    let eval_p3 = p3.eval(&random_eval);
    let exp_eval_p3 = random_c1 * random_eval[0] + random_c2 * random_eval[1];
    assert_eq!(eval_p3, exp_eval_p3);

    let mut p4 = T::zero();
    let random_c1 = F::rand(&mut rng);
    let random_c2 = F::rand(&mut rng);
    // X1^2 + X2^2
    let mut exp1 = [0; N];
    let mut exp2 = [0; N];
    exp1[0] = 2;
    exp2[1] = 2;
    p4.add_monomial(exp1, random_c1);
    p4.add_monomial(exp2, random_c2);
    let eval_p4 = p4.eval(&random_eval);
    let exp_eval_p4 =
        random_c1 * random_eval[0] * random_eval[0] + random_c2 * random_eval[1] * random_eval[1];
    assert_eq!(eval_p4, exp_eval_p4);
}

pub fn test_is_multilinear<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);

    // Test with zero polynomial
    let p1 = T::zero();
    assert!(p1.is_multilinear());

    // Test with a constant polynomial
    let c = F::rand(&mut rng);
    let p2 = T::from(c);
    assert!(p2.is_multilinear());

    // Test with a polynomial with one variable having a linear monomial
    {
        let mut p = T::zero();
        let c = F::rand(&mut rng);
        let idx = rng.gen_range(0..N);
        let monomials_exponents = std::array::from_fn(|i| if i == idx { 1 } else { 0 });
        p.add_monomial(monomials_exponents, c);
        assert!(p.is_multilinear());
    }

    // Test with a multilinear polynomial with random variables
    {
        let mut p = T::zero();
        let c = F::rand(&mut rng);
        let nb_var = rng.gen_range(0..D);
        let mut monomials_exponents: [usize; N] =
            std::array::from_fn(|i| if i <= nb_var { 1 } else { 0 });
        monomials_exponents.shuffle(&mut rng);
        p.add_monomial(monomials_exponents, c);
        assert!(p.is_multilinear());
    }

    // Test with a random polynomial (very unlikely to be multilinear)
    {
        let p = unsafe { T::random(&mut rng, None) };
        assert!(!p.is_multilinear());
    }
}

pub fn test_is_constant<F: PrimeField, const N: usize, const D: usize, T: MVPoly<F, N, D>>() {
    let mut rng = o1_utils::tests::make_test_rng(None);
    let c = F::rand(&mut rng);
    let p = T::from(c);
    assert!(p.is_constant());

    let p = T::zero();
    assert!(p.is_constant());

    let p = {
        let mut res = T::zero();
        let monomial: [usize; N] = std::array::from_fn(|i| if i == 0 { 1 } else { 0 });
        res.add_monomial(monomial, F::one());
        res
    };
    assert!(!p.is_constant());

    let p = {
        let mut res = T::zero();
        let monomial: [usize; N] = std::array::from_fn(|i| if i == 1 { 1 } else { 0 });
        res.add_monomial(monomial, F::one());
        res
    };
    assert!(!p.is_constant());

    // This might be flaky
    let p = unsafe { T::random(&mut rng, None) };
    assert!(!p.is_constant());
}