forked from lurk-lang/arecibo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprover.rs
218 lines (194 loc) · 6.55 KB
/
prover.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
use ff::Field;
use itertools::Itertools;
use rayon::prelude::*;
use crate::errors::NovaError::{ProofVerifyError, UnSatIndex};
use crate::parafold::cycle_fold::prover::ScalarMulAccumulator;
use crate::parafold::hash::HashElement;
use crate::parafold::nifs::{compute_fold_proof, RelaxedR1CSInstance};
use crate::parafold::transcript::prover::Transcript;
use crate::r1cs::R1CSShape;
use crate::supernova::error::SuperNovaError;
use crate::traits::commitment::CommitmentEngineTrait;
use crate::traits::CurveCycleEquipped;
use crate::{zip_with, Commitment, CommitmentKey};
/// A full Relaxed-R1CS accumulator for a circuit
/// # TODO:
/// It would make sense to store the [R1CSShape] here since
/// - There is only one accumulator per shape
/// - We can probably use an Arc to avoid copying
#[derive(Debug)]
pub struct RelaxedR1CS<E: CurveCycleEquipped> {
instance: RelaxedR1CSInstance<E>,
W: Vec<E::Scalar>,
E: Vec<E::Scalar>,
// TODO: store cache for Folding T
}
impl<E: CurveCycleEquipped> RelaxedR1CS<E> {
pub fn new(shape: &R1CSShape<E>) -> Self {
assert_eq!(shape.num_io, 2); // TODO HACK: IO needs to be even, it really is 1
Self {
instance: RelaxedR1CSInstance {
pp: shape.digest(),
u: E::Scalar::ZERO,
X: E::Scalar::ZERO,
W: Commitment::<E>::default(),
E: Commitment::<E>::default(),
},
W: vec![E::Scalar::ZERO; shape.num_vars],
E: vec![E::Scalar::ZERO; shape.num_cons],
}
}
pub fn instance(&self) -> &RelaxedR1CSInstance<E> {
&self.instance
}
/// Given the public IO `X_new` for a circuit with R1CS representation `shape`,
/// along with a satisfying witness vector `W_new`, and assuming `self` is a valid accumulator for the same circuit,
/// this function will fold the statement into `self` and return a [FoldProof] that will allow the verifier to perform
/// the same transformation over the corresponding [RelaxedR1CSInstance] of the input `self`.
///
/// # Warning
/// We assume the R1CS IO `X_new` has already been absorbed in some form into the transcript in order to avoid
/// unnecessary hashing. The caller is responsible for ensuring this assumption is valid.
pub fn fold(
&mut self,
ck: &CommitmentKey<E>,
shape: &R1CSShape<E>,
X_new: E::Scalar,
W_new: &[E::Scalar],
acc_sm: &mut ScalarMulAccumulator<E>,
transcript: &mut Transcript<E>,
) {
// TODO: Parallelize both of these operations
let W_comm_new = { E::CE::commit(ck, W_new) };
let (T, T_comm) = {
compute_fold_proof(
ck,
shape,
self.instance.u,
&[self.instance.X, self.instance.X], // TODO HACK: IO needs to be even
&self.W,
None,
&[X_new, X_new], // TODO HACK: IO needs to be even
W_new,
)
};
transcript.absorb(HashElement::CommitmentPrimary(W_comm_new));
transcript.absorb(HashElement::CommitmentPrimary(T_comm));
let r = transcript.squeeze();
self
.W
.par_iter_mut()
.zip_eq(W_new.par_iter())
.for_each(|(w, w_new)| *w += r * w_new);
self
.E
.par_iter_mut()
.zip_eq(T.par_iter())
.for_each(|(e, t)| *e += r * t);
self.instance.u += r;
self.instance.X += r * X_new;
// Compute scalar multiplications and resulting instances to be proved with the CycleFold circuit
// W_comm_next = W_comm_curr + r * W_comm_new
self.instance.W = acc_sm.scalar_mul(self.instance.W, W_comm_new, r, transcript);
// E_comm_next = E_comm_curr + r * T
self.instance.E = acc_sm.scalar_mul(self.instance.E, T_comm, r, transcript);
}
/// Given two lists of [RelaxedR1CS] accumulators,
pub fn merge_many(
ck: &CommitmentKey<E>,
shapes: &[R1CSShape<E>],
accs_L: Vec<Self>,
accs_R: Vec<Self>,
acc_sm: &mut ScalarMulAccumulator<E>,
transcript: &mut Transcript<E>,
) -> Vec<Self> {
// TODO: parallelize
let (Ts, T_comms): (Vec<_>, Vec<_>) = zip_with!(
(accs_L.iter(), accs_R.iter(), shapes),
|acc_L, acc_R, shape| {
compute_fold_proof(
ck,
shape,
acc_L.instance.u,
&[acc_L.instance.X, acc_L.instance.X],
&acc_L.W,
Some(acc_R.instance.u),
&[acc_R.instance.X, acc_R.instance.X],
&acc_R.W,
)
}
)
.unzip();
for T_comm in &T_comms {
transcript.absorb(HashElement::CommitmentPrimary(*T_comm));
}
let r = transcript.squeeze();
zip_with!(
(
accs_L.into_iter(),
accs_R.into_iter(),
Ts.into_iter(),
T_comms.into_iter()
),
|acc_L, acc_R, T, T_comm| {
let W = zip_with!(
(acc_L.W.into_par_iter(), acc_R.W.into_par_iter()),
|w_L, w_R| w_L + r * w_R
)
.collect();
let E = zip_with!(
(
acc_L.E.into_par_iter(),
T.into_par_iter(),
acc_R.E.into_par_iter()
),
|e_L, t, e_R| e_L + r * (t + r * e_R)
)
.collect();
let instance = {
assert_eq!(acc_L.instance.pp, acc_R.instance.pp);
let pp = acc_L.instance.pp;
let u = acc_L.instance.u + r * acc_R.instance.u;
let X = acc_L.instance.X + r * acc_R.instance.X;
// Compute scalar multiplications and resulting instances to be proved with the CycleFold circuit
// W_next = W_L + r * W_R
let W = acc_sm.scalar_mul(acc_L.instance.W, acc_R.instance.W, r, transcript);
let E_tmp = acc_sm.scalar_mul(T_comm, acc_R.instance.E, r, transcript);
// E_next = E_L + r * E1_next = E_L + r * T + r^2 * E_R
let E = acc_sm.scalar_mul(acc_L.instance.E, E_tmp, r, transcript);
RelaxedR1CSInstance { pp, u, X, W, E }
};
Self { instance, W, E }
}
)
.collect()
}
pub fn verify(&self, ck: &CommitmentKey<E>, shape: &R1CSShape<E>) -> Result<(), SuperNovaError> {
let E_expected = shape.compute_E(
&self.W,
&self.instance.u,
&[self.instance.X, self.instance.X], // TODO HACK: IO needs to be even
)?;
self
.E
.iter()
.zip_eq(E_expected.iter())
.enumerate()
.try_for_each(|(i, (e, e_expected))| {
if e != e_expected {
Err(UnSatIndex(i))
} else {
Ok(())
}
})?;
let W_comm = E::CE::commit(ck, &self.W);
if W_comm != self.instance.W {
return Err(SuperNovaError::NovaError(ProofVerifyError));
}
let E_comm = E::CE::commit(ck, &self.E);
if E_comm != self.instance.E {
return Err(SuperNovaError::NovaError(ProofVerifyError));
}
Ok(())
}
}