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
/// Generic test runners for prover/verifier.
use crate::{
    expr::E, logup::LookupTableID, lookups::LookupTableIDs, proof::ProofInputs, prover::prove,
    verifier::verify, witness::Witness, BaseSponge, Fp, OpeningProof, ScalarSponge, BN254,
};
use ark_ec::AffineRepr;
use kimchi::circuits::domains::EvaluationDomains;
use poly_commitment::kzg::PairingSRS;
use rand::{CryptoRng, RngCore};

/// No lookups, no selectors, only witness column. `N_WIT == N_REL`.
pub fn test_completeness_generic_only_relation<const N_REL: usize, RNG>(
    constraints: Vec<E<Fp>>,
    evaluations: Witness<N_REL, Vec<Fp>>,
    domain_size: usize,
    rng: &mut RNG,
) where
    RNG: RngCore + CryptoRng,
{
    test_completeness_generic_no_lookups::<N_REL, N_REL, 0, 0, _>(
        constraints,
        Box::new([]),
        evaluations,
        domain_size,
        rng,
    )
}

pub fn test_completeness_generic_no_lookups<
    const N_WIT: usize,
    const N_REL: usize,
    const N_DSEL: usize,
    const N_FSEL: usize,
    RNG,
>(
    constraints: Vec<E<Fp>>,
    fixed_selectors: Box<[Vec<Fp>; N_FSEL]>,
    evaluations: Witness<N_WIT, Vec<Fp>>,
    domain_size: usize,
    rng: &mut RNG,
) where
    RNG: RngCore + CryptoRng,
{
    let proof_inputs = ProofInputs {
        evaluations,
        logups: Default::default(),
    };
    test_completeness_generic::<N_WIT, N_REL, N_DSEL, N_FSEL, LookupTableIDs, _>(
        constraints,
        fixed_selectors,
        proof_inputs,
        domain_size,
        rng,
    )
}

// Generic function to test with different circuits with the generic prover/verifier.
// It doesn't use the interpreter to build the witness and compute the constraints.
pub fn test_completeness_generic<
    const N_WIT: usize,
    const N_REL: usize,
    const N_DSEL: usize,
    const N_FSEL: usize,
    LT: LookupTableID,
    RNG,
>(
    constraints: Vec<E<Fp>>,
    fixed_selectors: Box<[Vec<Fp>; N_FSEL]>,
    proof_inputs: ProofInputs<N_WIT, Fp, LT>,
    domain_size: usize,
    rng: &mut RNG,
) where
    RNG: RngCore + CryptoRng,
{
    let domain = EvaluationDomains::<Fp>::create(domain_size).unwrap();

    let srs: PairingSRS<BN254> = crate::precomputed_srs::get_bn254_srs(domain);

    let proof =
        prove::<_, OpeningProof, BaseSponge, ScalarSponge, _, N_WIT, N_REL, N_DSEL, N_FSEL, LT>(
            domain,
            &srs,
            &constraints,
            fixed_selectors.clone(),
            proof_inputs.clone(),
            rng,
        )
        .unwrap();

    {
        // Checking the proof size. We should have:
        // - N commitments for the columns
        // - N evaluations for the columns
        // - MAX_DEGREE - 1 commitments for the constraints (quotient polynomial)
        // TODO: add lookups

        // We check there is always only one commitment chunk
        (&proof.proof_comms.witness_comms)
            .into_iter()
            .for_each(|x| assert_eq!(x.len(), 1));
        // This equality is therefore trivial, but still doing it.
        assert!(
            (&proof.proof_comms.witness_comms)
                .into_iter()
                .fold(0, |acc, x| acc + x.len())
                == N_WIT
        );
        // Checking that none of the commitments are zero
        (&proof.proof_comms.witness_comms)
            .into_iter()
            .for_each(|v| v.chunks.iter().for_each(|x| assert!(!x.is_zero())));

        // Checking the number of chunks of the quotient polynomial
        let max_degree = {
            if proof_inputs.logups.is_empty() {
                constraints
                    .iter()
                    .map(|expr| expr.degree(1, 0))
                    .max()
                    .unwrap_or(0)
            } else {
                8
            }
        };

        if max_degree == 1 {
            assert_eq!(proof.proof_comms.t_comm.len(), 1);
        } else {
            assert_eq!(proof.proof_comms.t_comm.len(), max_degree as usize - 1);
        }
    }

    let verifies =
        verify::<_, OpeningProof, BaseSponge, ScalarSponge, N_WIT, N_REL, N_DSEL, N_FSEL, 0, LT>(
            domain,
            &srs,
            &constraints,
            fixed_selectors,
            &proof,
            Witness::zero_vec(domain_size),
        );
    assert!(verifies)
}

pub fn test_soundness_generic<
    const N_WIT: usize,
    const N_REL: usize,
    const N_DSEL: usize,
    const N_FSEL: usize,
    LT: LookupTableID,
    RNG,
>(
    constraints: Vec<E<Fp>>,
    fixed_selectors: Box<[Vec<Fp>; N_FSEL]>,
    proof_inputs: ProofInputs<N_WIT, Fp, LT>,
    proof_inputs_prime: ProofInputs<N_WIT, Fp, LT>,
    domain_size: usize,
    rng: &mut RNG,
) where
    RNG: RngCore + CryptoRng,
{
    let domain = EvaluationDomains::<Fp>::create(domain_size).unwrap();

    let srs: PairingSRS<BN254> = crate::precomputed_srs::get_bn254_srs(domain);

    // generate the proof
    let proof =
        prove::<_, OpeningProof, BaseSponge, ScalarSponge, _, N_WIT, N_REL, N_DSEL, N_FSEL, LT>(
            domain,
            &srs,
            &constraints,
            fixed_selectors.clone(),
            proof_inputs,
            rng,
        )
        .unwrap();

    // generate another (prime) proof
    let proof_prime =
        prove::<_, OpeningProof, BaseSponge, ScalarSponge, _, N_WIT, N_REL, N_DSEL, N_FSEL, LT>(
            domain,
            &srs,
            &constraints,
            fixed_selectors.clone(),
            proof_inputs_prime,
            rng,
        )
        .unwrap();

    // Swap the opening proof. The verification should fail.
    {
        let mut proof_clone = proof.clone();
        proof_clone.opening_proof = proof_prime.opening_proof;
        let verifies = verify::<
            _,
            OpeningProof,
            BaseSponge,
            ScalarSponge,
            N_WIT,
            N_REL,
            N_DSEL,
            N_FSEL,
            0,
            LT,
        >(
            domain,
            &srs,
            &constraints,
            fixed_selectors.clone(),
            &proof_clone,
            Witness::zero_vec(domain_size),
        );
        assert!(!verifies, "Proof with a swapped opening must fail");
    }

    // Changing at least one commitment in the proof should fail the verification.
    // TODO: improve me by swapping only one commitments. It should be
    // easier when an index trait is implemented.
    {
        let mut proof_clone = proof.clone();
        proof_clone.proof_comms = proof_prime.proof_comms;
        let verifies = verify::<
            _,
            OpeningProof,
            BaseSponge,
            ScalarSponge,
            N_WIT,
            N_REL,
            N_DSEL,
            N_FSEL,
            0,
            LT,
        >(
            domain,
            &srs,
            &constraints,
            fixed_selectors.clone(),
            &proof_clone,
            Witness::zero_vec(domain_size),
        );
        assert!(!verifies, "Proof with a swapped commitment must fail");
    }

    // Changing at least one evaluation at zeta in the proof should fail
    // the verification.
    // TODO: improve me by swapping only one evaluation at \zeta. It should be
    // easier when an index trait is implemented.
    {
        let mut proof_clone = proof.clone();
        proof_clone.proof_evals.witness_evals = proof_prime.proof_evals.witness_evals;
        let verifies = verify::<
            _,
            OpeningProof,
            BaseSponge,
            ScalarSponge,
            N_WIT,
            N_REL,
            N_DSEL,
            N_FSEL,
            0,
            LT,
        >(
            domain,
            &srs,
            &constraints,
            fixed_selectors,
            &proof_clone,
            Witness::zero_vec(domain_size),
        );
        assert!(!verifies, "Proof with a swapped witness eval must fail");
    }
}