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
#[cfg(any(test, std))]
#[macro_use]
extern crate std;

use core::fmt::Debug;
use pairing::{
    group::{ff::Field, prime::PrimeCurveAffine, Curve},
    Engine,
};
use thiserror::Error;

pub mod polynomial;

use polynomial::{
    Polynomial,
    op_tree
};

/// parameters from tested setup
#[derive(Clone, Debug)]
pub struct KZGParams<E: Engine, const MAX_COEFFS: usize> {
    /// generator of g
    g: E::G1Affine,
    /// generator of G2
    h: E::G2Affine,
    /// g^alpha^1, g^alpha^2, ...
    gs: [E::G1Affine; MAX_COEFFS],
    /// g^alpha^1, g^alpha^2, ...
    hs: [E::G2Affine; MAX_COEFFS],
}

// the commitment - "C" in the paper. It's a single group element
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KZGCommitment<E: Engine>(E::G1Affine);
impl<E: Engine> Copy for KZGCommitment<E> {}

// A witness for a single element - "w_i" in the paper. It's a group element.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KZGWitness<E: Engine>(E::G1Affine);
impl<E: Engine> Copy for KZGWitness<E> {}

// A witness for a several elements - "w_B" in the paper. It's a single group element plus a polynomial
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KZGBatchWitness<E: Engine, const MAX_COEFFS: usize>{
    r: Polynomial<E, MAX_COEFFS>,
    w: E::G1Affine
}

#[derive(Error, Debug)]
pub enum KZGError {
    #[error("no polynomial!")]
    NoPolynomial,
    #[error("point not on polynomial!")]
    PointNotOnPolynomial,
    #[error("batch opening remainder is zero!")]
    BatchOpeningZeroRemainder,
}

#[derive(Debug, Clone)]
pub struct KZGProver<'a, E: Engine, const MAX_COEFFS: usize> {
    parameters: &'a KZGParams<E, MAX_COEFFS>,
    polynomial: Option<Polynomial<E, MAX_COEFFS>>,
    commitment: Option<KZGCommitment<E>>,
}

#[derive(Debug, Clone)]
pub struct KZGVerifier<'a, E: Engine, const MAX_COEFFS: usize> {
    parameters: &'a KZGParams<E, MAX_COEFFS>,
}

impl<'a, E: Engine, const MAX_COEFFS: usize> KZGProver<'a, E, MAX_COEFFS> {
    /// initializes `polynomial` to zero polynomial
    pub fn new(parameters: &'a KZGParams<E, MAX_COEFFS>) -> Self {
        Self {
            parameters,
            polynomial: None,
            commitment: None,
        }
    }

    pub fn commit(&mut self, polynomial: Polynomial<E, MAX_COEFFS>) -> KZGCommitment<E> {
        let commitment = op_tree(
            polynomial.num_coeffs(),
            &|i| {
                if i == 0 {
                    self.parameters.g * polynomial.coeffs[i]
                } else {
                    self.parameters.gs[i - 1] * polynomial.coeffs[i]
                }
            },
            &|a, b| a + b
        ).to_affine();

        self.polynomial = Some(polynomial);
        let commitment = KZGCommitment(commitment);
        self.commitment = Some(commitment);
        commitment
    }

    pub fn open(&self) -> Result<Polynomial<E, MAX_COEFFS>, KZGError> {
        self.polynomial.clone().ok_or(KZGError::NoPolynomial)
    }

    pub fn has_commitment(&self) -> bool {
        self.commitment.is_some()
    }

    pub fn create_witness(&mut self, (x, y): (E::Fr, E::Fr)) -> Result<KZGWitness<E>, KZGError> {
        match self.polynomial {
            None => Err(KZGError::NoPolynomial),
            Some(ref polynomial) => {
                let mut dividend = polynomial.clone();
                dividend.coeffs[0] -= y;

                let mut divisor = Polynomial::new_from_coeffs([E::Fr::zero(); MAX_COEFFS], 1);
                divisor.coeffs[0] = -x;
                divisor.coeffs[1] = E::Fr::one();
                match dividend.long_division(&divisor) {
                    // by polynomial remainder theorem, if (x - point.x) does not divide self.polynomial, then
                    // self.polynomial(point.y) != point.1
                    (_, Some(_)) => Err(KZGError::PointNotOnPolynomial),
                    (psi, None) => {
                        let witness = op_tree(
                            psi.num_coeffs(), 
                            &|i| {
                                if i == 0 {
                                    self.parameters.g * psi.coeffs[i]
                                } else {
                                    self.parameters.gs[i - 1] * psi.coeffs[i]
                                }
                            },
                            &|a, b| a + b
                        ).to_affine(); 

                        Ok(KZGWitness(witness))
                    }
                }
            }
        }
    }

    // #[cfg(any(std))]
    pub fn create_witness_batched(&mut self, points: &Vec<(E::Fr, E::Fr)>) -> Result<KZGBatchWitness<E, MAX_COEFFS>, KZGError> {
        match self.polynomial {
            None => Err(KZGError::NoPolynomial),
            Some(ref polynomial) => {
                let zeros: Polynomial<E, MAX_COEFFS> = op_tree(
                    points.len(),
                    &|i| {
                        let mut coeffs = [E::Fr::zero(); MAX_COEFFS];
                        coeffs[0] = -points[i].0;
                        coeffs[1] = E::Fr::one();
                        Polynomial::new_from_coeffs(coeffs, 1)
                    },
                    &|a, b| a * b
                );
                
                let (xs, ys): (Vec<E::Fr>, Vec<E::Fr>) = points.iter().cloned().unzip();
                let interpolation = Polynomial::<E, MAX_COEFFS>::lagrange_interpolation(xs.as_slice(), ys.as_slice());

                let numerator = polynomial - &interpolation;
                let (psi, rem) = numerator.long_division(&zeros);
                match rem {
                    Some(_) => Err(KZGError::PointNotOnPolynomial),
                    None => {
                        let w = op_tree(
                            psi.num_coeffs(),
                            &|i| {
                                if i == 0 {
                                    self.parameters.g * psi.coeffs[i]
                                } else {
                                    self.parameters.gs[i - 1] * psi.coeffs[i]
                                }
                            },
                            &|a, b| a + b
                        ).to_affine();
                        Ok(KZGBatchWitness { r: interpolation, w })
                    }
                }
            }
        }
    }
}

impl<'a, E: Engine, const MAX_COEFFS: usize> KZGVerifier<'a, E, MAX_COEFFS> {
    pub fn new(parameters: &'a KZGParams<E, MAX_COEFFS>) -> Self {
        KZGVerifier { parameters }
    }

    pub fn verify_poly(
        &self,
        commitment: &KZGCommitment<E>,
        polynomial: &Polynomial<E, MAX_COEFFS>,
    ) -> bool {
        let check = op_tree(
            polynomial.num_coeffs(), 
            &|i| {
                if i == 0 {
                    self.parameters.g * polynomial.coeffs[i]
                } else {
                    self.parameters.gs[i - 1] * polynomial.coeffs[i]
                }
            },
            &|a, b| a + b
        );

        check.to_affine() == commitment.0
    }

    pub fn verify_eval(
        &self,
        (x, y): (E::Fr, E::Fr),
        commitment: &KZGCommitment<E>,
        witness: &KZGWitness<E>,
    ) -> bool {
        let lhs = E::pairing(
            &witness.0,
            &(self.parameters.hs[0].to_curve() - self.parameters.h * x).to_affine(),
        );
        let rhs = E::pairing(
            &(commitment.0.to_curve() - self.parameters.g * y).to_affine(),
            &self.parameters.h,
        );

        lhs == rhs
    }

    pub fn verify_eval_batched(
        &self,
        points: &Vec<(E::Fr, E::Fr)>,
        commitment: &KZGCommitment<E>,
        witness: &KZGBatchWitness<E, MAX_COEFFS>
    ) -> bool {
        let z: Polynomial<E, MAX_COEFFS> = op_tree(
            points.len(),
            &|i| {
                let mut coeffs = [E::Fr::zero(); MAX_COEFFS];
                coeffs[0] = -points[i].0;
                coeffs[1] = E::Fr::one();
                Polynomial::new_from_coeffs(coeffs, 1)
            },
            &|a, b| a * b
        );

        let hz = op_tree(
            z.num_coeffs(),
            &|i| {
                if i == 0 {
                    self.parameters.h * z.coeffs[i]
                } else {
                    self.parameters.hs[i - 1] * z.coeffs[i]
                }
            },
            &|a, b| a + b
        ).to_affine();

        let gr = op_tree(
            witness.r.num_coeffs(),
            &|i| {
                if i == 0 {
                    self.parameters.g * witness.r.coeffs[i]
                } else {
                    self.parameters.gs[i - 1] * witness.r.coeffs[i]
                }
            },
            &|a, b| a + b
        );

        let lhs = E::pairing(&witness.w, &hz);
        let rhs = E::pairing(
            &(commitment.0.to_curve() - gr).to_affine(),
            &self.parameters.h
        );

        lhs == rhs
    }
}

pub fn setup<E: Engine, const MAX_COEFFS: usize>(s: E::Fr) -> KZGParams<E, MAX_COEFFS> {
    let g = E::G1Affine::generator();
    let h = E::G2Affine::generator();

    let mut gs = [g; MAX_COEFFS];
    let mut hs = [h; MAX_COEFFS];

    let mut curr = g;

    for g in gs.iter_mut() {
        *g = (curr * s).to_affine();
        curr = *g;
    }

    let mut curr = h;
    for h in hs.iter_mut() {
        *h = (curr * s).to_affine();
        curr = *h;
    }

    KZGParams { g, h, gs, hs }
}

#[cfg(any(csprng_setup, test))]
use rand::random;

#[cfg(any(csprng_setup, test))]
pub fn csprng_setup<E: Engine, const MAX_COEFFS: usize>() -> KZGParams<E, MAX_COEFFS> {
    let s: E::Fr = random::<u64>().into();
    setup(s)
}

#[cfg(test)]
mod tests {
    use super::*;
    use bls12_381::{Bls12, Scalar};
    use lazy_static::lazy_static;
    use rand::{rngs::SmallRng, Rng, SeedableRng};
    use std::sync::Mutex;

    const RNG_SEED_0: [u8; 32] = [42; 32];
    const RNG_SEED_1: [u8; 32] = [79; 32];

    lazy_static! {
        static ref RNG_0: Mutex<SmallRng> = Mutex::new(SmallRng::from_seed(RNG_SEED_0));
        static ref RNG_1: Mutex<SmallRng> = Mutex::new(SmallRng::from_seed(RNG_SEED_1));
    }

    fn test_setup<E: Engine, const MAX_COEFFS: usize>() -> KZGParams<E, MAX_COEFFS> {
        let s: E::Fr = RNG_0.lock().unwrap().gen::<u64>().into();
        setup(s)
    }

    fn test_participants<'a, E: Engine, const MAX_COEFFS: usize>(params: &'a KZGParams<E, MAX_COEFFS>) -> (
        KZGProver<'a, E, MAX_COEFFS>, KZGVerifier<'a, E, MAX_COEFFS>
    ) {
        let prover = KZGProver::new(params);
        let verifier = KZGVerifier::new(params);

        (prover, verifier)
    }

    // never returns zero polynomial
    fn random_polynomial<E: Engine, const MAX_COEFFS: usize>(
        min_coeffs: usize,
    ) -> Polynomial<E, MAX_COEFFS> {
        let num_coeffs = RNG_1.lock().unwrap().gen_range(min_coeffs..MAX_COEFFS);
        let mut coeffs = [E::Fr::zero(); MAX_COEFFS];

        for i in 0..num_coeffs {
            coeffs[i] = RNG_1.lock().unwrap().gen::<u64>().into();
        }

        Polynomial::new_from_coeffs(coeffs, num_coeffs - 1)
    }

    fn assert_verify_poly<E: Engine + Debug, const MAX_COEFFS: usize>(
        verifier: &KZGVerifier<E, MAX_COEFFS>,
        commitment: &KZGCommitment<E>,
        polynomial: &Polynomial<E, MAX_COEFFS>,
    ) {
        assert!(
            verifier.verify_poly(&commitment, &polynomial),
            "verify_poly failed for commitment {:#?} and polynomial {:#?}",
            commitment,
            polynomial
        );
    }

    fn assert_verify_poly_fails<E: Engine + Debug, const MAX_COEFFS: usize>(
        verifier: &KZGVerifier<E, MAX_COEFFS>,
        commitment: &KZGCommitment<E>,
        polynomial: &Polynomial<E, MAX_COEFFS>,
    ) {
        assert!(
            !verifier.verify_poly(&commitment, &polynomial),
            "expected verify_poly to fail for commitment {:#?} and polynomial {:#?} but it didn't",
            commitment,
            polynomial
        );
    }

    fn assert_verify_eval<E: Engine + Debug, const MAX_COEFFS: usize>(
        verifier: &KZGVerifier<E, MAX_COEFFS>,
        point: (E::Fr, E::Fr),
        commitment: &KZGCommitment<E>,
        witness: &KZGWitness<E>,
    ) {
        assert!(
            verifier.verify_eval(point, &commitment, &witness),
            "verify_eval failed for point {:#?}, commitment {:#?}, and witness {:#?}",
            point,
            commitment,
            witness
        );
    }

    fn assert_verify_eval_fails<E: Engine + Debug, const MAX_COEFFS: usize>(
        verifier: &KZGVerifier<E, MAX_COEFFS>,
        point: (E::Fr, E::Fr),
        commitment: &KZGCommitment<E>,
        witness: &KZGWitness<E>,
    ) {
        assert!(!verifier.verify_eval(point, &commitment, &witness), "expected verify_eval to fail for for point {:#?}, commitment {:#?}, and witness {:#?}, but it didn't", point, commitment, witness);
    }

    #[test]
    fn test_basic() {
        let params = test_setup::<Bls12, 10>();
        let (mut prover, verifier) = test_participants(&params);

        let polynomial = random_polynomial(1);
        let commitment = prover.commit(polynomial.clone());

        assert_verify_poly(&verifier, &commitment, &polynomial);
        assert_verify_poly_fails(&verifier, &commitment, &random_polynomial(1));
    }

    fn random_field_elem_neq<E: Engine>(val: E::Fr) -> E::Fr {
        let mut v: E::Fr = RNG_1.lock().unwrap().gen::<u64>().into();
        while v == val {
            v = RNG_1.lock().unwrap().gen::<u64>().into();
        }

        v
    }

    #[test]
    fn test_modify_single_coeff() {
        let params = test_setup::<Bls12, 8>();
        let (mut prover, verifier) = test_participants(&params);

        let polynomial = random_polynomial(4);
        let commitment = prover.commit(polynomial.clone());

        let mut modified_polynomial = polynomial.clone();
        let new_coeff = random_field_elem_neq::<Bls12>(modified_polynomial.coeffs[2]);
        modified_polynomial.coeffs[2] = new_coeff;

        assert_verify_poly(&verifier, &commitment, &polynomial);
        assert_verify_poly_fails(&verifier, &commitment, &modified_polynomial);
    }

    #[test]
    fn test_eval_basic() {
        let params = test_setup::<Bls12, 13>();
        let (mut prover, verifier) = test_participants(&params);

        let polynomial = random_polynomial(5);
        let commitment = prover.commit(polynomial.clone());

        let x: Scalar = RNG_1.lock().unwrap().gen::<u64>().into();
        let y = polynomial.eval(x);

        let witness = prover.create_witness((x, y)).unwrap();
        assert_verify_eval(&verifier, (x, y), &commitment, &witness);

        let y_prime = random_field_elem_neq::<Bls12>(y);
        assert_verify_eval_fails(&verifier, (x, y_prime), &commitment, &witness);
    }

    #[test]
    fn test_eval_batched() {
        let params = test_setup::<Bls12, 15>();
        let (mut prover, verifier) = test_participants(&params);
        let polynomial = random_polynomial(8);
        let commitment = prover.commit(polynomial.clone());

        let mut points: Vec<(Scalar, Scalar)> = Vec::with_capacity(8);
        for _ in 0..8 {
            let x: Scalar = RNG_1.lock().unwrap().gen::<u64>().into();
            points.push((x, polynomial.eval(x)));
        }

        let witness = prover.create_witness_batched(&points).unwrap();
        assert!(verifier.verify_eval_batched(&points, &commitment, &witness));

        let mut other_points: Vec<(Scalar, Scalar)> = Vec::with_capacity(8);
        for _ in 0..8 {
            let x: Scalar = RNG_1.lock().unwrap().gen::<u64>().into();
            other_points.push((x, polynomial.eval(x)));
        }

        assert!(!verifier.verify_eval_batched(&other_points, &commitment, &witness))
    }
}