-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathppsnark.rs
1073 lines (930 loc) · 32.7 KB
/
ppsnark.rs
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! This module implements `RelaxedR1CSSNARK` traits using a spark-based approach to prove evaluations of
//! sparse multilinear polynomials involved in Spartan's sum-check protocol, thereby providing a preprocessing SNARK
//! The verifier in this preprocessing SNARK maintains a commitment to R1CS matrices. This is beneficial when using a
//! polynomial commitment scheme in which the verifier's costs is succinct.
//! This code includes experimental optimizations to reduce runtimes and proof sizes.
use crate::{
digest::{DigestComputer, SimpleDigestible},
errors::NovaError,
r1cs::{R1CSShape, RelaxedR1CSInstance, RelaxedR1CSWitness},
spartan::{
math::Math,
polys::{
eq::EqPolynomial,
identity::IdentityPolynomial,
multilinear::MultilinearPolynomial,
power::PowPolynomial,
univariate::{CompressedUniPoly, UniPoly},
},
powers,
sumcheck::{
engine::{
InnerSumcheckInstance, MemorySumcheckInstance, OuterSumcheckInstance, SumcheckEngine,
WitnessBoundSumcheck,
},
SumcheckProof,
},
PolyEvalInstance, PolyEvalWitness,
},
traits::{
commitment::{CommitmentEngineTrait, CommitmentTrait, Len},
evaluation::EvaluationEngineTrait,
snark::{DigestHelperTrait, RelaxedR1CSSNARKTrait},
Engine, TranscriptEngineTrait, TranscriptReprTrait,
},
zip_with, Commitment, CommitmentKey, CompressedCommitment,
};
use core::cmp::max;
use ff::Field;
use itertools::Itertools as _;
use once_cell::sync::OnceCell;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use super::polys::{masked_eq::MaskedEqPolynomial, multilinear::SparsePolynomial};
fn padded<E: Engine>(v: &[E::Scalar], n: usize, e: &E::Scalar) -> Vec<E::Scalar> {
let mut v_padded = vec![*e; n];
v_padded[..v.len()].copy_from_slice(v);
v_padded
}
/// A type that holds `R1CSShape` in a form amenable to memory checking
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct R1CSShapeSparkRepr<E: Engine> {
pub(in crate::spartan) N: usize, // size of the vectors
// dense representation
pub(in crate::spartan) row: Vec<E::Scalar>,
pub(in crate::spartan) col: Vec<E::Scalar>,
pub(in crate::spartan) val_A: Vec<E::Scalar>,
pub(in crate::spartan) val_B: Vec<E::Scalar>,
pub(in crate::spartan) val_C: Vec<E::Scalar>,
// timestamp polynomials
pub(in crate::spartan) ts_row: Vec<E::Scalar>,
pub(in crate::spartan) ts_col: Vec<E::Scalar>,
}
/// A type that holds a commitment to a sparse polynomial
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct R1CSShapeSparkCommitment<E: Engine> {
pub(in crate::spartan) N: usize, // size of each vector
// commitments to the dense representation
pub(in crate::spartan) comm_row: Commitment<E>,
pub(in crate::spartan) comm_col: Commitment<E>,
pub(in crate::spartan) comm_val_A: Commitment<E>,
pub(in crate::spartan) comm_val_B: Commitment<E>,
pub(in crate::spartan) comm_val_C: Commitment<E>,
// commitments to the timestamp polynomials
pub(in crate::spartan) comm_ts_row: Commitment<E>,
pub(in crate::spartan) comm_ts_col: Commitment<E>,
}
impl<E: Engine> TranscriptReprTrait<E::GE> for R1CSShapeSparkCommitment<E> {
fn to_transcript_bytes(&self) -> Vec<u8> {
[
self.comm_row,
self.comm_col,
self.comm_val_A,
self.comm_val_B,
self.comm_val_C,
self.comm_ts_row,
self.comm_ts_col,
]
.as_slice()
.to_transcript_bytes()
}
}
impl<E: Engine> R1CSShapeSparkRepr<E> {
/// represents `R1CSShape` in a Spark-friendly format amenable to memory checking
pub fn new(S: &R1CSShape<E>) -> Self {
let N = {
let total_nz = S.A.len() + S.B.len() + S.C.len();
max(total_nz, max(2 * S.num_vars, S.num_cons)).next_power_of_two()
};
// we make col lookup into the last entry of z, so we commit to zeros
let (mut row, mut col, mut val_A, mut val_B, mut val_C) = (
vec![0; N],
vec![N - 1; N],
vec![E::Scalar::ZERO; N],
vec![E::Scalar::ZERO; N],
vec![E::Scalar::ZERO; N],
);
for (i, entry) in S.A.iter().enumerate() {
let (r, c, v) = entry;
row[i] = r;
col[i] = c;
val_A[i] = v;
}
let b_offset = S.A.len();
for (i, entry) in S.B.iter().enumerate() {
let (r, c, v) = entry;
row[b_offset + i] = r;
col[b_offset + i] = c;
val_B[b_offset + i] = v;
}
let c_offset = S.A.len() + S.B.len();
for (i, entry) in S.C.iter().enumerate() {
let (r, c, v) = entry;
row[c_offset + i] = r;
col[c_offset + i] = c;
val_C[c_offset + i] = v;
}
// timestamp calculation routine
let timestamp_calc = |num_ops: usize, num_cells: usize, addr_trace: &[usize]| -> Vec<usize> {
let mut ts = vec![0usize; num_cells];
assert!(num_ops >= addr_trace.len());
for addr in addr_trace {
assert!(*addr < num_cells);
ts[*addr] += 1;
}
ts
};
// timestamp polynomials for row
let (ts_row, ts_col) =
rayon::join(|| timestamp_calc(N, N, &row), || timestamp_calc(N, N, &col));
// a routine to turn a vector of usize into a vector scalars
let to_vec_scalar = |v: &[usize]| -> Vec<E::Scalar> {
v.iter()
.map(|x| E::Scalar::from(*x as u64))
.collect::<Vec<_>>()
};
Self {
N,
// dense representation
row: to_vec_scalar(&row),
col: to_vec_scalar(&col),
val_A,
val_B,
val_C,
// timestamp polynomials
ts_row: to_vec_scalar(&ts_row),
ts_col: to_vec_scalar(&ts_col),
}
}
pub(in crate::spartan) fn commit(&self, ck: &CommitmentKey<E>) -> R1CSShapeSparkCommitment<E> {
let comm_vec: Vec<Commitment<E>> = [
&self.row,
&self.col,
&self.val_A,
&self.val_B,
&self.val_C,
&self.ts_row,
&self.ts_col,
]
.par_iter()
.map(|v| E::CE::commit(ck, v))
.collect();
R1CSShapeSparkCommitment {
N: self.row.len(),
comm_row: comm_vec[0],
comm_col: comm_vec[1],
comm_val_A: comm_vec[2],
comm_val_B: comm_vec[3],
comm_val_C: comm_vec[4],
comm_ts_row: comm_vec[5],
comm_ts_col: comm_vec[6],
}
}
// computes evaluation oracles
fn evaluation_oracles(
&self,
S: &R1CSShape<E>,
r_x: &E::Scalar,
z: &[E::Scalar],
) -> (
Vec<E::Scalar>,
Vec<E::Scalar>,
Vec<E::Scalar>,
Vec<E::Scalar>,
) {
let mem_row = PowPolynomial::new(r_x, self.N.log_2()).evals();
let mem_col = padded::<E>(z, self.N, &E::Scalar::ZERO);
let (L_row, L_col) = {
let mut L_row = vec![mem_row[0]; self.N]; // we place mem_row[0] since resized row is appended with 0s
let mut L_col = vec![mem_col[self.N - 1]; self.N]; // we place mem_col[N-1] since resized col is appended with N-1
for (i, (val_r, val_c)) in S
.A
.iter()
.chain(S.B.iter())
.chain(S.C.iter())
.map(|(r, c, _)| (mem_row[r], mem_col[c]))
.enumerate()
{
L_row[i] = val_r;
L_col[i] = val_c;
}
(L_row, L_col)
};
(mem_row, mem_col, L_row, L_col)
}
}
/// A type that represents the prover's key
#[derive(Debug, Clone)]
pub struct ProverKey<E: Engine, EE: EvaluationEngineTrait<E>> {
pk_ee: EE::ProverKey,
S_repr: R1CSShapeSparkRepr<E>,
S_comm: R1CSShapeSparkCommitment<E>,
vk_digest: E::Scalar, // digest of verifier's key
}
/// A type that represents the verifier's key
#[derive(Debug, Clone, Serialize)]
#[serde(bound = "EE::VerifierKey: Serialize")]
pub struct VerifierKey<E: Engine, EE: EvaluationEngineTrait<E>> {
num_cons: usize,
num_vars: usize,
vk_ee: EE::VerifierKey,
S_comm: R1CSShapeSparkCommitment<E>,
#[serde(skip, default = "OnceCell::new")]
digest: OnceCell<E::Scalar>,
}
impl<E: Engine, EE: EvaluationEngineTrait<E>> SimpleDigestible for VerifierKey<E, EE> where
EE::VerifierKey: Serialize
{
}
/// A succinct proof of knowledge of a witness to a relaxed R1CS instance
/// The proof is produced using Spartan's combination of the sum-check and
/// the commitment to a vector viewed as a polynomial commitment
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "")]
pub struct RelaxedR1CSSNARK<E: Engine, EE: EvaluationEngineTrait<E>> {
// commitment to oracles: the first three are for Az, Bz, Cz,
// and the last two are for memory reads
comm_Az: CompressedCommitment<E>,
comm_Bz: CompressedCommitment<E>,
comm_Cz: CompressedCommitment<E>,
comm_L_row: CompressedCommitment<E>,
comm_L_col: CompressedCommitment<E>,
// commitments to aid the memory checks
comm_t_plus_r_inv_row: CompressedCommitment<E>,
comm_w_plus_r_inv_row: CompressedCommitment<E>,
comm_t_plus_r_inv_col: CompressedCommitment<E>,
comm_w_plus_r_inv_col: CompressedCommitment<E>,
// claims about Az, Bz, and Cz polynomials
eval_Az_at_tau: E::Scalar,
eval_Bz_at_tau: E::Scalar,
eval_Cz_at_tau: E::Scalar,
// sum-check
sc: SumcheckProof<E>,
// claims from the end of sum-check
eval_Az: E::Scalar,
eval_Bz: E::Scalar,
eval_Cz: E::Scalar,
eval_E: E::Scalar,
eval_L_row: E::Scalar,
eval_L_col: E::Scalar,
eval_val_A: E::Scalar,
eval_val_B: E::Scalar,
eval_val_C: E::Scalar,
eval_W: E::Scalar,
eval_t_plus_r_inv_row: E::Scalar,
eval_row: E::Scalar, // address
eval_w_plus_r_inv_row: E::Scalar,
eval_ts_row: E::Scalar,
eval_t_plus_r_inv_col: E::Scalar,
eval_col: E::Scalar, // address
eval_w_plus_r_inv_col: E::Scalar,
eval_ts_col: E::Scalar,
// a PCS evaluation argument
eval_arg: EE::EvaluationArgument,
}
impl<E: Engine, EE: EvaluationEngineTrait<E>> RelaxedR1CSSNARK<E, EE> {
fn prove_helper<T1, T2, T3, T4>(
mem: &mut T1,
outer: &mut T2,
inner: &mut T3,
witness: &mut T4,
transcript: &mut E::TE,
) -> Result<
(
SumcheckProof<E>,
Vec<E::Scalar>,
Vec<Vec<E::Scalar>>,
Vec<Vec<E::Scalar>>,
Vec<Vec<E::Scalar>>,
Vec<Vec<E::Scalar>>,
),
NovaError,
>
where
T1: SumcheckEngine<E>,
T2: SumcheckEngine<E>,
T3: SumcheckEngine<E>,
T4: SumcheckEngine<E>,
{
// sanity checks
assert_eq!(mem.size(), outer.size());
assert_eq!(mem.size(), inner.size());
assert_eq!(mem.size(), witness.size());
assert_eq!(mem.degree(), outer.degree());
assert_eq!(mem.degree(), inner.degree());
assert_eq!(mem.degree(), witness.degree());
// these claims are already added to the transcript, so we do not need to add
let claims = mem
.initial_claims()
.into_iter()
.chain(outer.initial_claims())
.chain(inner.initial_claims())
.chain(witness.initial_claims())
.collect::<Vec<E::Scalar>>();
let s = transcript.squeeze(b"r")?;
let coeffs = powers(&s, claims.len());
// compute the joint claim
let claim = zip_with!(iter, (claims, coeffs), |c_1, c_2| *c_1 * c_2).sum();
let mut e = claim;
let mut r: Vec<E::Scalar> = Vec::new();
let mut cubic_polys: Vec<CompressedUniPoly<E::Scalar>> = Vec::new();
let num_rounds = mem.size().log_2();
for _ in 0..num_rounds {
let ((evals_mem, evals_outer), (evals_inner, evals_witness)) = rayon::join(
|| rayon::join(|| mem.evaluation_points(), || outer.evaluation_points()),
|| rayon::join(|| inner.evaluation_points(), || witness.evaluation_points()),
);
let evals: Vec<Vec<E::Scalar>> = evals_mem
.into_iter()
.chain(evals_outer.into_iter())
.chain(evals_inner.into_iter())
.chain(evals_witness.into_iter())
.collect::<Vec<Vec<E::Scalar>>>();
assert_eq!(evals.len(), claims.len());
let evals_combined_0 = (0..evals.len()).map(|i| evals[i][0] * coeffs[i]).sum();
let evals_combined_2 = (0..evals.len()).map(|i| evals[i][1] * coeffs[i]).sum();
let evals_combined_3 = (0..evals.len()).map(|i| evals[i][2] * coeffs[i]).sum();
let evals = vec![
evals_combined_0,
e - evals_combined_0,
evals_combined_2,
evals_combined_3,
];
let poly = UniPoly::from_evals(&evals);
// append the prover's message to the transcript
transcript.absorb(b"p", &poly);
// derive the verifier's challenge for the next round
let r_i = transcript.squeeze(b"c")?;
r.push(r_i);
let _ = rayon::join(
|| rayon::join(|| mem.bound(&r_i), || outer.bound(&r_i)),
|| rayon::join(|| inner.bound(&r_i), || witness.bound(&r_i)),
);
e = poly.evaluate(&r_i);
cubic_polys.push(poly.compress());
}
let mem_claims = mem.final_claims();
let outer_claims = outer.final_claims();
let inner_claims = inner.final_claims();
let witness_claims = witness.final_claims();
Ok((
SumcheckProof::new(cubic_polys),
r,
mem_claims,
outer_claims,
inner_claims,
witness_claims,
))
}
}
impl<E: Engine, EE: EvaluationEngineTrait<E>> VerifierKey<E, EE> {
fn new(
num_cons: usize,
num_vars: usize,
S_comm: R1CSShapeSparkCommitment<E>,
vk_ee: EE::VerifierKey,
) -> Self {
Self {
num_cons,
num_vars,
S_comm,
vk_ee,
digest: Default::default(),
}
}
}
impl<E: Engine, EE: EvaluationEngineTrait<E>> DigestHelperTrait<E> for VerifierKey<E, EE> {
/// Returns the digest of the verifier's key
fn digest(&self) -> E::Scalar {
self
.digest
.get_or_try_init(|| {
let dc = DigestComputer::new(self);
dc.digest()
})
.cloned()
.expect("Failure to retrieve digest!")
}
}
impl<E: Engine, EE: EvaluationEngineTrait<E>> RelaxedR1CSSNARKTrait<E> for RelaxedR1CSSNARK<E, EE> {
type ProverKey = ProverKey<E, EE>;
type VerifierKey = VerifierKey<E, EE>;
fn ck_floor() -> Box<dyn for<'a> Fn(&'a R1CSShape<E>) -> usize> {
Box::new(|shape: &R1CSShape<E>| -> usize {
// the commitment key should be large enough to commit to the R1CS matrices
shape.A.len() + shape.B.len() + shape.C.len()
})
}
fn setup(
ck: Arc<CommitmentKey<E>>,
S: &R1CSShape<E>,
) -> Result<(Self::ProverKey, Self::VerifierKey), NovaError> {
// check the provided commitment key meets minimal requirements
if ck.length() < Self::ck_floor()(S) {
return Err(NovaError::InvalidCommitmentKeyLength);
}
let (pk_ee, vk_ee) = EE::setup(ck.clone());
// pad the R1CS matrices
let S = S.pad();
let S_repr = R1CSShapeSparkRepr::new(&S);
let S_comm = S_repr.commit(&*ck);
let vk = VerifierKey::new(S.num_cons, S.num_vars, S_comm.clone(), vk_ee);
let pk = ProverKey {
pk_ee,
S_repr,
S_comm,
vk_digest: vk.digest(),
};
Ok((pk, vk))
}
/// produces a succinct proof of satisfiability of a `RelaxedR1CS` instance
#[tracing::instrument(skip_all, name = "PPSNARK::prove")]
fn prove(
ck: &CommitmentKey<E>,
pk: &Self::ProverKey,
S: &R1CSShape<E>,
U: &RelaxedR1CSInstance<E>,
W: &RelaxedR1CSWitness<E>,
) -> Result<Self, NovaError> {
// pad the R1CSShape
let S = S.pad();
// sanity check that R1CSShape has all required size characteristics
assert!(S.is_regular_shape());
let W = W.pad(&S); // pad the witness
let mut transcript = E::TE::new(b"RelaxedR1CSSNARK");
// append the verifier key (which includes commitment to R1CS matrices) and the RelaxedR1CSInstance to the transcript
transcript.absorb(b"vk", &pk.vk_digest);
transcript.absorb(b"U", U);
// compute the full satisfying assignment by concatenating W.W, U.u, and U.X
let z = [W.W.clone(), vec![U.u], U.X.clone()].concat();
// compute Az, Bz, Cz
let (mut Az, mut Bz, mut Cz) = S.multiply_vec(&z)?;
// commit to Az, Bz, Cz
let (comm_Az, (comm_Bz, comm_Cz)) = rayon::join(
|| E::CE::commit(ck, &Az),
|| rayon::join(|| E::CE::commit(ck, &Bz), || E::CE::commit(ck, &Cz)),
);
transcript.absorb(b"c", &[comm_Az, comm_Bz, comm_Cz].as_slice());
// number of rounds of sum-check
let num_rounds_sc = pk.S_repr.N.log_2();
let tau = transcript.squeeze(b"t")?;
let tau_coords = PowPolynomial::new(&tau, num_rounds_sc).coordinates();
// (1) send commitments to Az, Bz, and Cz along with their evaluations at tau
let (Az, Bz, Cz, W, E) = {
Az.resize(pk.S_repr.N, E::Scalar::ZERO);
Bz.resize(pk.S_repr.N, E::Scalar::ZERO);
Cz.resize(pk.S_repr.N, E::Scalar::ZERO);
let E = padded::<E>(&W.E, pk.S_repr.N, &E::Scalar::ZERO);
let W = padded::<E>(&W.W, pk.S_repr.N, &E::Scalar::ZERO);
(Az, Bz, Cz, W, E)
};
let chis_taus = EqPolynomial::evals_from_points(&tau_coords);
let (eval_Az_at_tau, eval_Bz_at_tau, eval_Cz_at_tau) = {
let evals_at_tau = [&Az, &Bz, &Cz]
.into_par_iter()
.map(|p| MultilinearPolynomial::evaluate_with_chis(p, &chis_taus))
.collect::<Vec<E::Scalar>>();
(evals_at_tau[0], evals_at_tau[1], evals_at_tau[2])
};
// (2) send commitments to the following two oracles
// L_row(i) = eq(tau, row(i)) for all i
// L_col(i) = z(col(i)) for all i
let (mem_row, mem_col, L_row, L_col) = pk.S_repr.evaluation_oracles(&S, &tau, &z);
let (comm_L_row, comm_L_col) =
rayon::join(|| E::CE::commit(ck, &L_row), || E::CE::commit(ck, &L_col));
// since all the three polynomials are opened at tau,
// we can combine them into a single polynomial opened at tau
let eval_vec = vec![eval_Az_at_tau, eval_Bz_at_tau, eval_Cz_at_tau];
// absorb the claimed evaluations into the transcript
transcript.absorb(b"e", &eval_vec.as_slice());
// absorb commitments to L_row and L_col in the transcript
transcript.absorb(b"e", &vec![comm_L_row, comm_L_col].as_slice());
let comm_vec = vec![comm_Az, comm_Bz, comm_Cz];
let poly_vec = vec![&Az, &Bz, &Cz];
let c = transcript.squeeze(b"c")?;
let w: PolyEvalWitness<E> = PolyEvalWitness::batch(&poly_vec, &c);
let u: PolyEvalInstance<E> =
PolyEvalInstance::batch(&comm_vec, tau_coords.clone(), &eval_vec, &c);
// we now need to prove four claims
// (1) 0 = \sum_x poly_tau(x) * (poly_Az(x) * poly_Bz(x) - poly_uCz_E(x)), and eval_Az_at_tau + r * eval_Bz_at_tau + r^2 * eval_Cz_at_tau = (Az+r*Bz+r^2*Cz)(tau)
// (2) eval_Az_at_tau + c * eval_Bz_at_tau + c^2 * eval_Cz_at_tau = \sum_y L_row(y) * (val_A(y) + c * val_B(y) + c^2 * val_C(y)) * L_col(y)
// (3) L_row(i) = eq(tau, row(i)) and L_col(i) = z(col(i))
// (4) Check that the witness polynomial W is well-formed e.g., it is padded with only zeros
let gamma = transcript.squeeze(b"g")?;
let r = transcript.squeeze(b"r")?;
let ((mut outer_sc_inst, mut inner_sc_inst), mem_res) = rayon::join(
|| {
// a sum-check instance to prove the first claim
let outer_sc_inst = OuterSumcheckInstance::new(
PowPolynomial::new(&tau, num_rounds_sc).evals(),
Az.clone(),
Bz.clone(),
(0..Cz.len())
.map(|i| U.u * Cz[i] + E[i])
.collect::<Vec<E::Scalar>>(),
w.p.clone(), // Mz = Az + r * Bz + r^2 * Cz
&u.e, // eval_Az_at_tau + r * eval_Az_at_tau + r^2 * eval_Cz_at_tau
);
// a sum-check instance to prove the second claim
let val = zip_with!(
par_iter,
(pk.S_repr.val_A, pk.S_repr.val_B, pk.S_repr.val_C),
|v_a, v_b, v_c| *v_a + c * *v_b + c * c * *v_c
)
.collect::<Vec<E::Scalar>>();
let inner_sc_inst = InnerSumcheckInstance {
claim: eval_Az_at_tau + c * eval_Bz_at_tau + c * c * eval_Cz_at_tau,
poly_L_row: MultilinearPolynomial::new(L_row.clone()),
poly_L_col: MultilinearPolynomial::new(L_col.clone()),
poly_val: MultilinearPolynomial::new(val),
};
(outer_sc_inst, inner_sc_inst)
},
|| {
// a third sum-check instance to prove the read-only memory claim
// we now need to prove that L_row and L_col are well-formed
// hash the tuples of (addr,val) memory contents and read responses into a single field element using `hash_func`
let (comm_mem_oracles, mem_oracles, mem_aux) =
MemorySumcheckInstance::<E>::compute_oracles(
ck,
&r,
&gamma,
&mem_row,
&pk.S_repr.row,
&L_row,
&pk.S_repr.ts_row,
&mem_col,
&pk.S_repr.col,
&L_col,
&pk.S_repr.ts_col,
)?;
// absorb the commitments
transcript.absorb(b"l", &comm_mem_oracles.as_slice());
let rho = transcript.squeeze(b"r")?;
let poly_eq = MultilinearPolynomial::new(PowPolynomial::new(&rho, num_rounds_sc).evals());
Ok::<_, NovaError>((
MemorySumcheckInstance::new(
mem_oracles.clone(),
mem_aux,
poly_eq.Z,
pk.S_repr.ts_row.clone(),
pk.S_repr.ts_col.clone(),
),
comm_mem_oracles,
mem_oracles,
))
},
);
let (mut mem_sc_inst, comm_mem_oracles, mem_oracles) = mem_res?;
let mut witness_sc_inst = WitnessBoundSumcheck::new(tau, W.clone(), S.num_vars);
let (sc, rand_sc, claims_mem, claims_outer, claims_inner, claims_witness) = Self::prove_helper(
&mut mem_sc_inst,
&mut outer_sc_inst,
&mut inner_sc_inst,
&mut witness_sc_inst,
&mut transcript,
)?;
// claims from the end of the sum-check
let eval_Az = claims_outer[0][0];
let eval_Bz = claims_outer[0][1];
let eval_L_row = claims_inner[0][0];
let eval_L_col = claims_inner[0][1];
let eval_t_plus_r_inv_row = claims_mem[0][0];
let eval_w_plus_r_inv_row = claims_mem[0][1];
let eval_ts_row = claims_mem[0][2];
let eval_t_plus_r_inv_col = claims_mem[1][0];
let eval_w_plus_r_inv_col = claims_mem[1][1];
let eval_ts_col = claims_mem[1][2];
let eval_W = claims_witness[0][0];
// compute the remaining claims that did not come for free from the sum-check prover
let (eval_Cz, eval_E, eval_val_A, eval_val_B, eval_val_C, eval_row, eval_col) = {
let e = [
&Cz,
&E,
&pk.S_repr.val_A,
&pk.S_repr.val_B,
&pk.S_repr.val_C,
&pk.S_repr.row,
&pk.S_repr.col,
]
.into_par_iter()
.map(|p| MultilinearPolynomial::evaluate_with(p, &rand_sc))
.collect::<Vec<E::Scalar>>();
(e[0], e[1], e[2], e[3], e[4], e[5], e[6])
};
// all the evaluations are at rand_sc, we can fold them into one claim
let eval_vec = vec![
eval_W,
eval_Az,
eval_Bz,
eval_Cz,
eval_E,
eval_L_row,
eval_L_col,
eval_val_A,
eval_val_B,
eval_val_C,
eval_t_plus_r_inv_row,
eval_row,
eval_w_plus_r_inv_row,
eval_ts_row,
eval_t_plus_r_inv_col,
eval_col,
eval_w_plus_r_inv_col,
eval_ts_col,
];
let comm_vec = [
U.comm_W,
comm_Az,
comm_Bz,
comm_Cz,
U.comm_E,
comm_L_row,
comm_L_col,
pk.S_comm.comm_val_A,
pk.S_comm.comm_val_B,
pk.S_comm.comm_val_C,
comm_mem_oracles[0],
pk.S_comm.comm_row,
comm_mem_oracles[1],
pk.S_comm.comm_ts_row,
comm_mem_oracles[2],
pk.S_comm.comm_col,
comm_mem_oracles[3],
pk.S_comm.comm_ts_col,
];
let poly_vec = [
&W,
&Az,
&Bz,
&Cz,
&E,
&L_row,
&L_col,
&pk.S_repr.val_A,
&pk.S_repr.val_B,
&pk.S_repr.val_C,
mem_oracles[0].as_ref(),
&pk.S_repr.row,
mem_oracles[1].as_ref(),
&pk.S_repr.ts_row,
mem_oracles[2].as_ref(),
&pk.S_repr.col,
mem_oracles[3].as_ref(),
&pk.S_repr.ts_col,
];
transcript.absorb(b"e", &eval_vec.as_slice()); // comm_vec is already in the transcript
let c = transcript.squeeze(b"c")?;
let w: PolyEvalWitness<E> = PolyEvalWitness::batch(&poly_vec, &c);
let u: PolyEvalInstance<E> = PolyEvalInstance::batch(&comm_vec, rand_sc.clone(), &eval_vec, &c);
let eval_arg = EE::prove(ck, &pk.pk_ee, &mut transcript, &u.c, &w.p, &rand_sc, &u.e)?;
Ok(Self {
comm_Az: comm_Az.compress(),
comm_Bz: comm_Bz.compress(),
comm_Cz: comm_Cz.compress(),
comm_L_row: comm_L_row.compress(),
comm_L_col: comm_L_col.compress(),
comm_t_plus_r_inv_row: comm_mem_oracles[0].compress(),
comm_w_plus_r_inv_row: comm_mem_oracles[1].compress(),
comm_t_plus_r_inv_col: comm_mem_oracles[2].compress(),
comm_w_plus_r_inv_col: comm_mem_oracles[3].compress(),
eval_Az_at_tau,
eval_Bz_at_tau,
eval_Cz_at_tau,
sc,
eval_Az,
eval_Bz,
eval_Cz,
eval_E,
eval_L_row,
eval_L_col,
eval_val_A,
eval_val_B,
eval_val_C,
eval_W,
eval_t_plus_r_inv_row,
eval_row,
eval_w_plus_r_inv_row,
eval_ts_row,
eval_col,
eval_t_plus_r_inv_col,
eval_w_plus_r_inv_col,
eval_ts_col,
eval_arg,
})
}
/// verifies a proof of satisfiability of a `RelaxedR1CS` instance
fn verify(&self, vk: &Self::VerifierKey, U: &RelaxedR1CSInstance<E>) -> Result<(), NovaError> {
let mut transcript = E::TE::new(b"RelaxedR1CSSNARK");
// append the verifier key (including commitment to R1CS matrices) and the RelaxedR1CSInstance to the transcript
transcript.absorb(b"vk", &vk.digest());
transcript.absorb(b"U", U);
let comm_Az = Commitment::<E>::decompress(&self.comm_Az)?;
let comm_Bz = Commitment::<E>::decompress(&self.comm_Bz)?;
let comm_Cz = Commitment::<E>::decompress(&self.comm_Cz)?;
let comm_L_row = Commitment::<E>::decompress(&self.comm_L_row)?;
let comm_L_col = Commitment::<E>::decompress(&self.comm_L_col)?;
let comm_t_plus_r_inv_row = Commitment::<E>::decompress(&self.comm_t_plus_r_inv_row)?;
let comm_w_plus_r_inv_row = Commitment::<E>::decompress(&self.comm_w_plus_r_inv_row)?;
let comm_t_plus_r_inv_col = Commitment::<E>::decompress(&self.comm_t_plus_r_inv_col)?;
let comm_w_plus_r_inv_col = Commitment::<E>::decompress(&self.comm_w_plus_r_inv_col)?;
transcript.absorb(b"c", &[comm_Az, comm_Bz, comm_Cz].as_slice());
let num_rounds_sc = vk.S_comm.N.log_2();
let tau = transcript.squeeze(b"t")?;
let tau_coords = PowPolynomial::new(&tau, num_rounds_sc).coordinates();
// add claims about Az, Bz, and Cz to be checked later
// since all the three polynomials are opened at tau,
// we can combine them into a single polynomial opened at tau
let eval_vec = vec![
self.eval_Az_at_tau,
self.eval_Bz_at_tau,
self.eval_Cz_at_tau,
];
transcript.absorb(b"e", &eval_vec.as_slice());
transcript.absorb(b"e", &vec![comm_L_row, comm_L_col].as_slice());
let comm_vec = vec![comm_Az, comm_Bz, comm_Cz];
let c = transcript.squeeze(b"c")?;
let u: PolyEvalInstance<E> =
PolyEvalInstance::batch(&comm_vec, tau_coords.clone(), &eval_vec, &c);
let claim = u.e;
let gamma = transcript.squeeze(b"g")?;
let r = transcript.squeeze(b"r")?;
transcript.absorb(
b"l",
&vec![
comm_t_plus_r_inv_row,
comm_w_plus_r_inv_row,
comm_t_plus_r_inv_col,
comm_w_plus_r_inv_col,
]
.as_slice(),
);
let rho = transcript.squeeze(b"r")?;
let num_claims = 10;
let s = transcript.squeeze(b"r")?;
let coeffs = powers(&s, num_claims);
let claim = (coeffs[7] + coeffs[8]) * claim; // rest are zeros
// verify sc
let (claim_sc_final, rand_sc) = self.sc.verify(claim, num_rounds_sc, 3, &mut transcript)?;
// verify claim_sc_final
let claim_sc_final_expected = {
let rand_eq_bound_rand_sc = PowPolynomial::new(&rho, num_rounds_sc).evaluate(&rand_sc);
let eq_tau: EqPolynomial<_> = PowPolynomial::new(&tau, num_rounds_sc).into();
let taus_bound_rand_sc = eq_tau.evaluate(&rand_sc);
let taus_masked_bound_rand_sc =
MaskedEqPolynomial::new(&eq_tau, vk.num_vars.log_2()).evaluate(&rand_sc);
let eval_t_plus_r_row = {
let eval_addr_row = IdentityPolynomial::new(num_rounds_sc).evaluate(&rand_sc);
let eval_val_row = taus_bound_rand_sc;
let eval_t = eval_addr_row + gamma * eval_val_row;
eval_t + r
};
let eval_w_plus_r_row = {
let eval_addr_row = self.eval_row;
let eval_val_row = self.eval_L_row;
let eval_w = eval_addr_row + gamma * eval_val_row;
eval_w + r
};
let eval_t_plus_r_col = {
let eval_addr_col = IdentityPolynomial::new(num_rounds_sc).evaluate(&rand_sc);
// memory contents is z, so we compute eval_Z from eval_W and eval_X
let eval_val_col = {
// rand_sc was padded, so we now remove the padding
let (factor, rand_sc_unpad) = {
let l = vk.S_comm.N.log_2() - (2 * vk.num_vars).log_2();
let mut factor = E::Scalar::ONE;
for r_p in rand_sc.iter().take(l) {
factor *= E::Scalar::ONE - r_p
}
let rand_sc_unpad = rand_sc[l..].to_vec();
(factor, rand_sc_unpad)
};
let eval_X = {
// public IO is (u, X)
let X = vec![U.u]
.into_iter()
.chain(U.X.iter().cloned())
.collect::<Vec<E::Scalar>>();
// evaluate the sparse polynomial at rand_sc_unpad[1..]
let poly_X = SparsePolynomial::new(rand_sc_unpad.len() - 1, X);
poly_X.evaluate(&rand_sc_unpad[1..])
};
self.eval_W + factor * rand_sc_unpad[0] * eval_X
};
let eval_t = eval_addr_col + gamma * eval_val_col;
eval_t + r
};
let eval_w_plus_r_col = {
let eval_addr_col = self.eval_col;
let eval_val_col = self.eval_L_col;
let eval_w = eval_addr_col + gamma * eval_val_col;
eval_w + r
};
let claim_mem_final_expected: E::Scalar = coeffs[0]
* (self.eval_t_plus_r_inv_row - self.eval_w_plus_r_inv_row)
+ coeffs[1] * (self.eval_t_plus_r_inv_col - self.eval_w_plus_r_inv_col)
+ coeffs[2]
* (rand_eq_bound_rand_sc
* (self.eval_t_plus_r_inv_row * eval_t_plus_r_row - self.eval_ts_row))
+ coeffs[3]
* (rand_eq_bound_rand_sc
* (self.eval_w_plus_r_inv_row * eval_w_plus_r_row - E::Scalar::ONE))
+ coeffs[4]
* (rand_eq_bound_rand_sc
* (self.eval_t_plus_r_inv_col * eval_t_plus_r_col - self.eval_ts_col))
+ coeffs[5]
* (rand_eq_bound_rand_sc
* (self.eval_w_plus_r_inv_col * eval_w_plus_r_col - E::Scalar::ONE));
let claim_outer_final_expected = coeffs[6]
* taus_bound_rand_sc
* (self.eval_Az * self.eval_Bz - U.u * self.eval_Cz - self.eval_E)
+ coeffs[7] * taus_bound_rand_sc * (self.eval_Az + c * self.eval_Bz + c * c * self.eval_Cz);
let claim_inner_final_expected = coeffs[8]
* self.eval_L_row
* self.eval_L_col
* (self.eval_val_A + c * self.eval_val_B + c * c * self.eval_val_C);
let claim_witness_final_expected = coeffs[9] * taus_masked_bound_rand_sc * self.eval_W;
claim_mem_final_expected
+ claim_outer_final_expected
+ claim_inner_final_expected
+ claim_witness_final_expected
};
if claim_sc_final_expected != claim_sc_final {
return Err(NovaError::InvalidSumcheckProof);
}
let eval_vec = vec![
self.eval_W,
self.eval_Az,
self.eval_Bz,
self.eval_Cz,
self.eval_E,
self.eval_L_row,