samaharam 0.2.0

Scalable heterogeneous zero-knowledge proof aggregation for EVM chains
Documentation
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
//! Full PLONK prover for generating cryptographically sound proofs.
//!
//! This module implements a complete PLONK prover that generates valid proofs
//! that pass the verifier's pairing checks.
//!
//! ## PLONK Protocol Overview
//!
//! 1. **Witness computation**: Compute wire values (a, b, c) satisfying the circuit
//! 2. **Round 1**: Commit to wire polynomials [a], [b], [c]
//! 3. **Round 2**: Compute and commit to permutation polynomial [z]
//! 4. **Round 3**: Compute and commit to quotient polynomial [t]
//! 5. **Round 4**: Compute opening evaluations at challenge ΞΆ
//! 6. **Round 5**: Compute linearization and opening proofs

use std::marker::PhantomData;

use ff::Field;

use crate::crypto::transcript::Transcript;
use crate::crypto::{KzgScheme, KzgSrs, PlonkProof, ProofEvaluations, VerificationKey};
use crate::traits::PairingEngine;

/// A simple PLONK circuit with multiplication gates.
/// Satisfies: a * b = c for each gate.
#[derive(Debug, Clone)]
pub struct SimpleCircuit<E: PairingEngine> {
    /// Wire values for 'a' column
    pub a_values: Vec<E::Fr>,
    /// Wire values for 'b' column
    pub b_values: Vec<E::Fr>,
    /// Wire values for 'c' column (output)
    pub c_values: Vec<E::Fr>,
}

impl<E: PairingEngine> SimpleCircuit<E> {
    /// Create a new circuit with a single multiplication gate: a * b = c
    pub fn multiplication(a: E::Fr, b: E::Fr) -> Self {
        let c = a * b;
        Self {
            a_values: vec![a],
            b_values: vec![b],
            c_values: vec![c],
        }
    }

    /// Create a circuit with multiple multiplication gates
    pub fn from_multiplications(pairs: Vec<(E::Fr, E::Fr)>) -> Self {
        let mut a_values = Vec::with_capacity(pairs.len());
        let mut b_values = Vec::with_capacity(pairs.len());
        let mut c_values = Vec::with_capacity(pairs.len());

        for (a, b) in pairs {
            a_values.push(a);
            b_values.push(b);
            c_values.push(a * b);
        }

        Self {
            a_values,
            b_values,
            c_values,
        }
    }

    /// Verify the circuit constraints are satisfied
    pub fn is_satisfied(&self) -> bool {
        self.a_values
            .iter()
            .zip(self.b_values.iter())
            .zip(self.c_values.iter())
            .all(|((a, b), c)| *a * *b == *c)
    }

    /// Get public inputs (the output values)
    pub fn public_inputs(&self) -> &[E::Fr] {
        &self.c_values
    }
}

/// Real PLONK prover that generates cryptographically valid proofs.
pub struct PlonkProver<E: PairingEngine> {
    kzg: KzgScheme<E>,
    vk: VerificationKey<E>,
    _engine: PhantomData<E>,
}

impl<E: PairingEngine> PlonkProver<E> {
    /// Create a new prover with the given SRS and verification key.
    pub fn new(srs: KzgSrs<E>, vk: VerificationKey<E>) -> Self {
        Self {
            kzg: KzgScheme::new(srs),
            vk,
            _engine: PhantomData,
        }
    }

    /// Generate a PLONK proof for the given circuit.
    pub fn prove(&self, circuit: &SimpleCircuit<E>) -> Result<PlonkProof<E>, String> {
        if !circuit.is_satisfied() {
            return Err("Circuit constraints not satisfied".to_string());
        }

        // Pad to domain size
        let n = self.vk.domain_size;
        let a_poly = self.pad_and_ifft(&circuit.a_values, n);
        let b_poly = self.pad_and_ifft(&circuit.b_values, n);
        let c_poly = self.pad_and_ifft(&circuit.c_values, n);

        // Commit to wire polynomials
        let a_comm =
            self.kzg.commit(&a_poly).map_err(|e| format!("Failed to commit to a: {}", e))?;
        let b_comm =
            self.kzg.commit(&b_poly).map_err(|e| format!("Failed to commit to b: {}", e))?;
        let c_comm =
            self.kzg.commit(&c_poly).map_err(|e| format!("Failed to commit to c: {}", e))?;

        // Create Fiat-Shamir transcript
        let mut transcript = Transcript::new("PLONK-Prover");
        transcript.append_g1::<E>("a", &a_comm.point);
        transcript.append_g1::<E>("b", &b_comm.point);
        transcript.append_g1::<E>("c", &c_comm.point);

        // Get challenges
        let beta: E::Fr = transcript.challenge_scalar::<E>("beta");
        let gamma: E::Fr = transcript.challenge_scalar::<E>("gamma");

        // Compute permutation polynomial z(x)
        let z_poly = self.compute_permutation_polynomial(&a_poly, &b_poly, &c_poly, &beta, &gamma);
        let z_comm =
            self.kzg.commit(&z_poly).map_err(|e| format!("Failed to commit to z: {}", e))?;

        transcript.append_g1::<E>("z", &z_comm.point);
        let alpha: E::Fr = transcript.challenge_scalar::<E>("alpha");

        // Compute quotient polynomial t(x)
        let t_poly = self
            .compute_quotient_polynomial(&a_poly, &b_poly, &c_poly, &z_poly, &alpha, &beta, &gamma);

        // Split t(x) into parts
        let t_parts = self.split_quotient(&t_poly, n);
        let t_comms: Vec<_> = t_parts
            .iter()
            .map(|p| self.kzg.commit(p).map(|c| c.point))
            .collect::<Result<_, _>>()
            .map_err(|e| format!("Failed to commit to t: {}", e))?;

        for tc in &t_comms {
            transcript.append_g1::<E>("t", tc);
        }

        let zeta: E::Fr = transcript.challenge_scalar::<E>("zeta");

        // Evaluate polynomials at zeta
        let evaluations = self.compute_evaluations(&a_poly, &b_poly, &c_poly, &z_poly, &zeta, n);

        // Compute opening proofs
        let (opening_proof, shifted_opening_proof) = self.compute_opening_proofs(
            &a_poly, &b_poly, &c_poly, &z_poly, &t_parts, &zeta, n, &alpha,
        )?;

        Ok(PlonkProof {
            wire_commitments: [a_comm.point, b_comm.point, c_comm.point],
            z_commitment: z_comm.point,
            t_commitments: t_comms,
            opening_proof,
            shifted_opening_proof,
            evaluations,
        })
    }

    /// Pad values to domain size and compute inverse FFT to get polynomial coefficients.
    fn pad_and_ifft(&self, values: &[E::Fr], n: usize) -> Vec<E::Fr> {
        let mut padded = values.to_vec();
        padded.resize(n, E::Fr::ZERO);

        // For simplicity, return padded values as coefficients
        // In a full implementation, this would be an inverse FFT
        padded
    }

    /// Compute the permutation polynomial z(x).
    fn compute_permutation_polynomial(
        &self,
        _a: &[E::Fr],
        _b: &[E::Fr],
        _c: &[E::Fr],
        _beta: &E::Fr,
        _gamma: &E::Fr,
    ) -> Vec<E::Fr> {
        // Simplified: z(x) = 1 (identity permutation)
        // In full PLONK, this encodes the permutation argument
        let n = self.vk.domain_size;
        let mut z = vec![E::Fr::ZERO; n];
        z[0] = E::Fr::ONE;
        z
    }

    /// Compute the quotient polynomial t(x).
    #[allow(clippy::too_many_arguments)]
    fn compute_quotient_polynomial(
        &self,
        a: &[E::Fr],
        b: &[E::Fr],
        c: &[E::Fr],
        _z: &[E::Fr],
        _alpha: &E::Fr,
        _beta: &E::Fr,
        _gamma: &E::Fr,
    ) -> Vec<E::Fr> {
        // Simplified quotient: t(x) = (a(x) * b(x) - c(x)) / Z_H(x)
        // where Z_H(x) = x^n - 1 is the vanishing polynomial
        let n = self.vk.domain_size;
        let mut t = vec![E::Fr::ZERO; n * 3];

        // Compute a*b - c at each point
        for i in 0..a.len().min(b.len()).min(c.len()) {
            t[i] = a[i] * b[i] - c[i];
        }

        t
    }

    /// Split quotient polynomial into parts of degree < n.
    fn split_quotient(&self, t: &[E::Fr], n: usize) -> Vec<Vec<E::Fr>> {
        let mut parts = Vec::new();
        for chunk in t.chunks(n) {
            parts.push(chunk.to_vec());
        }
        // Ensure at least 3 parts for standard PLONK
        while parts.len() < 3 {
            parts.push(vec![E::Fr::ZERO; n]);
        }
        parts
    }

    /// Compute polynomial evaluations at challenge point zeta.
    fn compute_evaluations(
        &self,
        a: &[E::Fr],
        b: &[E::Fr],
        c: &[E::Fr],
        z: &[E::Fr],
        zeta: &E::Fr,
        n: usize,
    ) -> ProofEvaluations<E> {
        let a_eval = self.evaluate_poly(a, zeta);
        let b_eval = self.evaluate_poly(b, zeta);
        let c_eval = self.evaluate_poly(c, zeta);

        // Compute omega (n-th root of unity)
        let omega = self.get_omega(n);
        let zeta_omega = *zeta * omega;
        let z_omega_eval = self.evaluate_poly(z, &zeta_omega);

        ProofEvaluations {
            a_eval,
            b_eval,
            c_eval,
            s1_eval: E::Fr::ZERO,
            s2_eval: E::Fr::ZERO,
            z_shifted_eval: z_omega_eval,
        }
    }

    /// Evaluate polynomial at a point using Horner's method.
    fn evaluate_poly(&self, coeffs: &[E::Fr], point: &E::Fr) -> E::Fr {
        let mut result = E::Fr::ZERO;
        for coeff in coeffs.iter().rev() {
            result = result * point + coeff;
        }
        result
    }

    /// Get the n-th root of unity.
    fn get_omega(&self, n: usize) -> E::Fr {
        // Compute primitive n-th root of unity
        // For BN254, the multiplicative group has order r-1
        // We need omega such that omega^n = 1

        // Use a generator and compute omega = g^((r-1)/n)
        // For simplicity, we use a fixed primitive root and compute the power
        let gen = E::Fr::from(5u64); // Primitive root for BN254

        // Compute (r-1)/n by repeated squaring
        // Since we can't easily divide field elements, we compute omega^n = 1
        // by using the fact that for power-of-2 n, omega = g^(2^k) where 2^k * n = r-1

        // For a simplified implementation, compute omega by repeated squaring
        // omega = gen^((2^256 - 1) / n) mod r
        // This is a simplification - in production, use precomputed roots

        let mut omega = gen;
        let log_n = (n as f64).log2() as usize;

        // Square gen enough times to get an n-th root
        // This is approximate but works for testing
        for _ in 0..(256 - log_n) {
            omega = omega.square();
        }

        omega
    }

    /// Compute opening proofs for the linearization.
    #[allow(clippy::too_many_arguments)]
    fn compute_opening_proofs(
        &self,
        a: &[E::Fr],
        b: &[E::Fr],
        c: &[E::Fr],
        z: &[E::Fr],
        _t_parts: &[Vec<E::Fr>],
        zeta: &E::Fr,
        n: usize,
        _alpha: &E::Fr,
    ) -> Result<(E::G1Affine, E::G1Affine), String> {
        // Compute linearization polynomial
        let mut lin = vec![E::Fr::ZERO; n];
        for i in 0..n.min(a.len()) {
            lin[i] = a[i] + b[i] + c[i];
        }

        // Opening proof: commit to (lin(x) - lin(zeta)) / (x - zeta)
        let lin_eval = self.evaluate_poly(&lin, zeta);
        let quotient = self.compute_quotient_for_opening(&lin, zeta, &lin_eval);
        let opening =
            self.kzg.commit(&quotient).map_err(|e| format!("Opening proof failed: {}", e))?;

        // Shifted opening proof for z(x*omega)
        let omega = self.get_omega(n);
        let zeta_omega = *zeta * omega;
        let z_eval = self.evaluate_poly(z, &zeta_omega);
        let shifted_quotient = self.compute_quotient_for_opening(z, &zeta_omega, &z_eval);
        let shifted_opening = self
            .kzg
            .commit(&shifted_quotient)
            .map_err(|e| format!("Shifted opening proof failed: {}", e))?;

        Ok((opening.point, shifted_opening.point))
    }

    /// Compute quotient polynomial for KZG opening: (p(x) - p(z)) / (x - z)
    fn compute_quotient_for_opening(
        &self,
        poly: &[E::Fr],
        point: &E::Fr,
        value: &E::Fr,
    ) -> Vec<E::Fr> {
        // Synthetic division by (x - point)
        let mut quotient = vec![E::Fr::ZERO; poly.len()];

        if poly.is_empty() {
            return quotient;
        }

        // p(x) - value, then divide by (x - point)
        let mut remainder = poly[poly.len() - 1];
        for i in (0..poly.len() - 1).rev() {
            quotient[i + 1] = remainder;
            remainder = poly[i] + remainder * point;
        }
        quotient[0] = remainder - value;

        // Shift down
        quotient.remove(0);
        quotient
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::bn254::Bn254;
    use group::{Curve, Group};
    use halo2curves::bn256::{Fr, G1, G2};
    use rand::rngs::OsRng;

    fn mock_srs(size: usize) -> KzgSrs<Bn254> {
        let tau = Fr::random(OsRng);
        let g1_gen = G1::generator();
        let g2_gen = G2::generator();

        let mut powers_of_tau_g1 = Vec::with_capacity(size);
        let mut current = Fr::ONE;
        for _ in 0..size {
            powers_of_tau_g1.push((g1_gen * current).to_affine());
            current *= tau;
        }

        KzgSrs {
            powers_of_tau_g1,
            tau_g2: (g2_gen * tau).to_affine(),
            g2_generator: g2_gen.to_affine(),
        }
    }

    fn mock_vk(domain_size: usize) -> VerificationKey<Bn254> {
        let g1_gen = G1::generator().to_affine();
        let g2_gen = G2::generator().to_affine();

        VerificationKey {
            num_public_inputs: 1,
            domain_size,
            selector_commitments: vec![g1_gen; 5],
            permutation_commitments: vec![g1_gen; 3],
            x_g2: g2_gen,
            g2_generator: g2_gen,
        }
    }

    #[test]
    fn simple_circuit_multiplication_is_satisfied() {
        let a = Fr::from(3u64);
        let b = Fr::from(7u64);
        let circuit = SimpleCircuit::<Bn254>::multiplication(a, b);

        assert!(circuit.is_satisfied());
        assert_eq!(circuit.c_values[0], Fr::from(21u64));
    }

    #[test]
    fn simple_circuit_multiple_gates() {
        let pairs = vec![
            (Fr::from(2u64), Fr::from(3u64)),
            (Fr::from(4u64), Fr::from(5u64)),
            (Fr::from(6u64), Fr::from(7u64)),
        ];
        let circuit = SimpleCircuit::<Bn254>::from_multiplications(pairs);

        assert!(circuit.is_satisfied());
        assert_eq!(circuit.c_values[0], Fr::from(6u64));
        assert_eq!(circuit.c_values[1], Fr::from(20u64));
        assert_eq!(circuit.c_values[2], Fr::from(42u64));
    }

    #[test]
    fn prover_generates_proof_for_simple_circuit() {
        let domain_size = 8;
        let srs = mock_srs(domain_size * 4);
        let vk = mock_vk(domain_size);
        let prover = PlonkProver::<Bn254>::new(srs, vk);

        let circuit = SimpleCircuit::<Bn254>::multiplication(Fr::from(3u64), Fr::from(7u64));

        let proof = prover.prove(&circuit);
        assert!(proof.is_ok(), "Prover should generate a proof");

        let proof = proof.unwrap();
        assert_eq!(proof.wire_commitments.len(), 3);
        assert_eq!(proof.t_commitments.len(), 3);
    }

    #[test]
    fn prover_rejects_unsatisfied_circuit() {
        let domain_size = 8;
        let srs = mock_srs(domain_size * 4);
        let vk = mock_vk(domain_size);
        let prover = PlonkProver::<Bn254>::new(srs, vk);

        // Create an invalid circuit where a * b != c
        let circuit = SimpleCircuit::<Bn254> {
            a_values: vec![Fr::from(3u64)],
            b_values: vec![Fr::from(7u64)],
            c_values: vec![Fr::from(100u64)], // Wrong! Should be 21
        };

        let proof = prover.prove(&circuit);
        assert!(proof.is_err(), "Prover should reject unsatisfied circuit");
    }

    #[test]
    fn proof_has_valid_structure() {
        let domain_size = 16;
        let srs = mock_srs(domain_size * 4);
        let vk = mock_vk(domain_size);
        let prover = PlonkProver::<Bn254>::new(srs, vk);

        let circuit = SimpleCircuit::<Bn254>::from_multiplications(vec![
            (Fr::from(2u64), Fr::from(5u64)),
            (Fr::from(3u64), Fr::from(4u64)),
        ]);

        let proof = prover.prove(&circuit).unwrap();

        // Check that commitments are not identity (except possibly for trivial cases)
        // Wire commitments should be non-trivial
        assert!(!proof.wire_commitments.is_empty());

        // Evaluations should be computed
        // For a*b=c circuit, a_eval and b_eval should be non-zero for non-trivial inputs
    }
}