Skip to main content

poly_commitment/
pbt_srs.rs

1//! Property-based tests for the SRS trait.
2//!
3//! It includes tests regarding methods the SRS trait should implement.
4//! It aims to verify the implementation respects the properties described in
5//! the documentation of the methods.
6
7use ark_ff::Zero;
8use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial};
9use rand::Rng;
10
11use crate::{commitment::CommitmentCurve, SRS};
12
13/// Test how many chunks are generated with different polynomial sizes.
14///
15/// Verifies that `commit_non_hiding` produces the expected number of
16/// chunks for various polynomial sizes and requested chunk counts.
17///
18/// # Panics
19///
20/// Panics if the number of generated chunks does not match expectations.
21pub fn test_regression_commit_non_hiding_expected_number_of_chunks<
22    G: CommitmentCurve,
23    Srs: SRS<G>,
24>() {
25    let mut rng = &mut o1_utils::tests::make_test_rng(None);
26    // maximum random srs size is 64
27    let log2_srs_size = rng.gen_range(1..6);
28    let srs_size = 1 << log2_srs_size;
29    let srs = Srs::create(srs_size);
30
31    // If we have a polynomial of the size of the SRS (i.e. of degree `srs_size
32    // - 1`), and we request 1 chunk, we should get 1 chunk.
33    {
34        // srs_size is the number of evaluation degree
35        let poly_degree = srs_size - 1;
36        let poly = DensePolynomial::<G::ScalarField>::rand(poly_degree, &mut rng);
37        let commitment = srs.commit_non_hiding(&poly, 1);
38        assert_eq!(commitment.len(), 1);
39    }
40
41    // If we have a polynomial of the size of the SRS (i.e. of degree `srs_size
42    // - 1`), and we request k chunks (k > 1), we should get k chunk.
43    {
44        // srs_size is the number of evaluation degree
45        let poly_degree = srs_size - 1;
46        // maximum 10 chunks for the test
47        let k = rng.gen_range(2..10);
48        let poly = DensePolynomial::<G::ScalarField>::rand(poly_degree, &mut rng);
49        let commitment = srs.commit_non_hiding(&poly, k);
50        assert_eq!(commitment.len(), k);
51    }
52
53    // Same than the two previous cases, but with the special polynomial equals
54    // to zero.
55    {
56        let k = rng.gen_range(1..10);
57        let poly = DensePolynomial::<G::ScalarField>::zero();
58        let commitment = srs.commit_non_hiding(&poly, k);
59        assert_eq!(commitment.len(), k);
60    }
61
62    // Polynomial of exactly a multiple of the SRS size, i.e degree is k *
63    // srs_size - 1.
64    {
65        let k = rng.gen_range(2..5);
66        let poly_degree = k * srs_size - 1;
67        let poly = DensePolynomial::<G::ScalarField>::rand(poly_degree, &mut rng);
68        // if we request a number of chunks smaller than the multiple, we will
69        // still get a number of chunks equals to the multiple.
70        let requested_num_chunks = rng.gen_range(1..k);
71        let commitment = srs.commit_non_hiding(&poly, requested_num_chunks);
72        assert_eq!(commitment.len(), k);
73
74        // if we request a number of chunks equals to the multiple, we will get
75        // the exact number of chunks.
76        let commitment = srs.commit_non_hiding(&poly, k);
77        assert_eq!(commitment.len(), k);
78
79        // if we request a number of chunks greater than the multiple, we will
80        // get the exact number of chunks requested.
81        let requested_num_chunks = rng.gen_range(k + 1..10);
82        let commitment = srs.commit_non_hiding(&poly, requested_num_chunks);
83        assert_eq!(commitment.len(), requested_num_chunks);
84    }
85}