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
#![allow(non_snake_case)]
#![allow(clippy::type_complexity)]
#![deny(missing_docs)]
mod commitments;
pub mod bellperson;
mod circuit;
pub mod errors;
mod gadgets;
pub mod pasta;
mod poseidon;
pub mod r1cs;
pub mod traits;
use std::marker::PhantomData;
use commitments::{AppendToTranscriptTrait, CompressedCommitment};
use errors::NovaError;
use merlin::Transcript;
use r1cs::{
R1CSGens, R1CSInstance, R1CSShape, R1CSWitness, RelaxedR1CSInstance, RelaxedR1CSWitness,
};
use traits::{ChallengeTrait, Group};
pub struct StepSNARK<G: Group> {
comm_T: CompressedCommitment<G::CompressedGroupElement>,
_p: PhantomData<G>,
}
impl<G: Group> StepSNARK<G> {
fn protocol_name() -> &'static [u8] {
b"NovaStepSNARK"
}
pub fn prove(
gens: &R1CSGens<G>,
S: &R1CSShape<G>,
U1: &RelaxedR1CSInstance<G>,
W1: &RelaxedR1CSWitness<G>,
U2: &R1CSInstance<G>,
W2: &R1CSWitness<G>,
transcript: &mut Transcript,
) -> Result<
(
StepSNARK<G>,
(RelaxedR1CSInstance<G>, RelaxedR1CSWitness<G>),
),
NovaError,
> {
transcript.append_message(b"protocol-name", StepSNARK::<G>::protocol_name());
let (T, comm_T) = S.commit_T(gens, U1, W1, U2, W2)?;
comm_T.append_to_transcript(b"comm_T", transcript);
let r = G::Scalar::challenge(b"r", transcript);
let U = U1.fold(U2, &comm_T, &r)?;
let W = W1.fold(W2, &T, &r)?;
Ok((
StepSNARK {
comm_T,
_p: Default::default(),
},
(U, W),
))
}
pub fn verify(
&self,
U1: &RelaxedR1CSInstance<G>,
U2: &R1CSInstance<G>,
transcript: &mut Transcript,
) -> Result<RelaxedR1CSInstance<G>, NovaError> {
transcript.append_message(b"protocol-name", StepSNARK::<G>::protocol_name());
self.comm_T.append_to_transcript(b"comm_T", transcript);
let r = G::Scalar::challenge(b"r", transcript);
let U = U1.fold(U2, &self.comm_T, &r)?;
Ok(U)
}
}
pub struct FinalSNARK<G: Group> {
W: RelaxedR1CSWitness<G>,
}
impl<G: Group> FinalSNARK<G> {
pub fn prove(W: &RelaxedR1CSWitness<G>) -> Result<FinalSNARK<G>, NovaError> {
Ok(FinalSNARK { W: W.clone() })
}
pub fn verify(
&self,
gens: &R1CSGens<G>,
S: &R1CSShape<G>,
U: &RelaxedR1CSInstance<G>,
) -> Result<(), NovaError> {
S.is_sat_relaxed(gens, U, &self.W)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::PrimeField;
use rand::rngs::OsRng;
type S = pasta_curves::pallas::Scalar;
type G = pasta_curves::pallas::Point;
#[test]
fn test_tiny_r1cs() {
let one = S::one();
let (num_cons, num_vars, num_io, A, B, C) = {
let num_cons = 4;
let num_vars = 4;
let num_io = 2;
let mut A: Vec<(usize, usize, S)> = Vec::new();
let mut B: Vec<(usize, usize, S)> = Vec::new();
let mut C: Vec<(usize, usize, S)> = Vec::new();
A.push((0, num_vars + 1, one));
B.push((0, num_vars + 1, one));
C.push((0, 0, one));
A.push((1, 0, one));
B.push((1, num_vars + 1, one));
C.push((1, 1, one));
A.push((2, 1, one));
A.push((2, num_vars + 1, one));
B.push((2, num_vars, one));
C.push((2, 2, one));
A.push((3, 2, one));
A.push((3, num_vars, one + one + one + one + one));
B.push((3, num_vars, one));
C.push((3, num_vars + 2, one));
(num_cons, num_vars, num_io, A, B, C)
};
let S = {
let res = R1CSShape::new(num_cons, num_vars, num_io, &A, &B, &C);
assert!(res.is_ok());
res.unwrap()
};
let gens = R1CSGens::new(num_cons, num_vars);
let rand_inst_witness_generator =
|gens: &R1CSGens<G>, I: &S| -> (S, R1CSInstance<G>, R1CSWitness<G>) {
let i0 = *I;
let (O, vars, X) = {
let z0 = i0 * i0;
let z1 = i0 * z0;
let z2 = z1 + i0;
let i1 = z2 + one + one + one + one + one;
let W = vec![z0, z1, z2, S::zero()];
let X = vec![i0, i1];
(i1, W, X)
};
let W = {
let res = R1CSWitness::new(&S, &vars);
assert!(res.is_ok());
res.unwrap()
};
let U = {
let comm_W = W.commit(gens);
let res = R1CSInstance::new(&S, &comm_W, &X);
assert!(res.is_ok());
res.unwrap()
};
assert!(S.is_sat(gens, &U, &W).is_ok());
(O, U, W)
};
let mut csprng: OsRng = OsRng;
let I = S::random(&mut csprng);
let (O, U1, W1) = rand_inst_witness_generator(&gens, &I);
let (_O, U2, W2) = rand_inst_witness_generator(&gens, &O);
let mut r_W = RelaxedR1CSWitness::default(&S);
let mut r_U = RelaxedR1CSInstance::default(&gens, &S);
let mut prover_transcript = Transcript::new(b"StepSNARKExample");
let res = StepSNARK::prove(&gens, &S, &r_U, &r_W, &U1, &W1, &mut prover_transcript);
assert!(res.is_ok());
let (step_snark, (_U, W)) = res.unwrap();
let mut verifier_transcript = Transcript::new(b"StepSNARKExample");
let res = step_snark.verify(&r_U, &U1, &mut verifier_transcript);
assert!(res.is_ok());
let U = res.unwrap();
assert_eq!(U, _U);
r_W = W;
r_U = U;
let res = StepSNARK::prove(&gens, &S, &r_U, &r_W, &U2, &W2, &mut prover_transcript);
assert!(res.is_ok());
let (step_snark, (_U, W)) = res.unwrap();
let res = step_snark.verify(&r_U, &U2, &mut verifier_transcript);
assert!(res.is_ok());
let U = res.unwrap();
assert_eq!(U, _U);
r_W = W;
r_U = U;
let res = FinalSNARK::prove(&r_W);
assert!(res.is_ok());
let final_snark = res.unwrap();
let res = final_snark.verify(&gens, &S, &r_U);
assert!(res.is_ok());
}
}