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
use crate::pairing::ff::{Field, PrimeField};
use crate::pairing::Engine;
use crate::SynthesisError;
use std::marker::PhantomData;
use super::cs::*;
#[derive(Debug, Clone)]
pub struct TestAssembly<E: Engine, P: PlonkConstraintSystemParams<E>> {
m: usize,
n: usize,
num_inputs: usize,
num_aux: usize,
input_assingments: Vec<E::Fr>,
aux_assingments: Vec<E::Fr>,
inputs_map: Vec<usize>,
is_finalized: bool,
next_step_leftover_from_previous_gate: Option<(E::Fr, P::NextTraceStepCoefficients)>,
_marker: std::marker::PhantomData<P>,
}
impl<E: Engine, P: PlonkConstraintSystemParams<E>> ConstraintSystem<E, P> for TestAssembly<E, P> {
// allocate a variable
fn alloc<F>(&mut self, value: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<E::Fr, SynthesisError>,
{
let value = value()?;
self.num_aux += 1;
let index = self.num_aux;
self.aux_assingments.push(value);
// println!("Allocated variable Aux({}) with value {}", index, value);
Ok(Variable(Index::Aux(index)))
}
// allocate an input variable
fn alloc_input<F>(&mut self, value: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<E::Fr, SynthesisError>,
{
let value = value()?;
self.num_inputs += 1;
let index = self.num_inputs;
self.input_assingments.push(value);
let input_var = Variable(Index::Input(index));
self.n += 1;
Ok(input_var)
}
// allocate an abstract gate
fn new_gate(&mut self, variables: P::StateVariables, this_step_coeffs: P::ThisTraceStepCoefficients, next_step_coeffs: P::NextTraceStepCoefficients) -> Result<(), SynthesisError> {
// check that leftover of this gate is satisfied
if let Some((value_leftover, coeffs)) = self.next_step_leftover_from_previous_gate.take() {
let mut leftover = value_leftover;
for (&var, coeff) in variables.as_ref().iter().rev().zip(coeffs.as_ref().iter()) {
let mut value = self.get_value(var)?;
value.mul_assign(&coeff);
leftover.add_assign(&value);
}
if leftover.is_zero() == false {
return Err(SynthesisError::Unsatisfiable);
}
}
// now check for THIS gate
let mut gate_value = E::Fr::zero();
let mut this_step_coeffs_iter = this_step_coeffs.as_ref().iter();
// first take an LC
for (&var, coeff) in variables.as_ref().iter().zip(&mut this_step_coeffs_iter) {
let mut value = self.get_value(var)?;
value.mul_assign(&coeff);
gate_value.add_assign(&value);
}
// multiplication
let mut q_m = *(this_step_coeffs_iter.next().unwrap());
q_m.mul_assign(&self.get_value(variables.as_ref()[0])?);
q_m.mul_assign(&self.get_value(variables.as_ref()[1])?);
gate_value.add_assign(&q_m);
// constant
gate_value.add_assign(this_step_coeffs_iter.next().unwrap());
assert!(next_step_coeffs.as_ref().len() <= 1);
if next_step_coeffs.as_ref().len() != 0 {
assert!(P::CAN_ACCESS_NEXT_TRACE_STEP == true);
if next_step_coeffs.as_ref()[0].is_zero() == false {
self.next_step_leftover_from_previous_gate = Some((gate_value, next_step_coeffs));
}
// assert!(self.next_step_vars.is_some());
// let next_step_vars = self.next_step_vars.take().expect("must have some next step variables")
// for (&var, coeff) in variables.as_ref().iter().rev()
// .zip(next_step_coeffs.as_ref().iter())
// {
// let mut value = self.get_value(var)?;
// value.mul_assign(&coeff);
// gate_value.add_assign(&value);
// }
} else {
if gate_value.is_zero() == false {
return Err(SynthesisError::Unsatisfiable);
}
}
self.n += 1;
Ok(())
}
fn get_value(&self, var: Variable) -> Result<E::Fr, SynthesisError> {
let value = match var {
Variable(Index::Aux(0)) => {
E::Fr::zero()
// return Err(SynthesisError::AssignmentMissing);
}
Variable(Index::Input(0)) => {
return Err(SynthesisError::AssignmentMissing);
}
Variable(Index::Input(input)) => self.input_assingments[input - 1],
Variable(Index::Aux(aux)) => self.aux_assingments[aux - 1],
};
Ok(value)
}
fn get_dummy_variable(&self) -> Variable {
self.dummy_variable()
}
}
impl<E: Engine, P: PlonkConstraintSystemParams<E>> TestAssembly<E, P> {
pub fn new() -> Self {
let tmp = Self {
n: 0,
m: 0,
num_inputs: 0,
num_aux: 0,
input_assingments: vec![],
aux_assingments: vec![],
inputs_map: vec![],
is_finalized: false,
next_step_leftover_from_previous_gate: None,
_marker: std::marker::PhantomData,
};
tmp
}
pub fn new_with_size_hints(num_inputs: usize, num_aux: usize) -> Self {
let tmp = Self {
n: 0,
m: 0,
num_inputs: 0,
num_aux: 0,
input_assingments: Vec::with_capacity(num_inputs),
aux_assingments: Vec::with_capacity(num_aux),
inputs_map: Vec::with_capacity(num_inputs),
is_finalized: false,
next_step_leftover_from_previous_gate: None,
_marker: std::marker::PhantomData,
};
tmp
}
// return variable that is not in a constraint formally, but has some value
fn dummy_variable(&self) -> Variable {
Variable(Index::Aux(0))
}
pub fn is_well_formed(&self) -> bool {
// check that last gate does not chain further!
self.next_step_leftover_from_previous_gate.is_none()
}
// pub fn is_satisfied(&self, in_a_middle: bool) -> bool {
// // expect a small number of inputs
// for (i, gate) in self.input_gates.iter().enumerate()
// {
// let Gate::<E::Fr> {
// variables: [a_var, b_var, c_var],
// coefficients: [q_l, q_r, q_o, q_m, q_c, q_c_next]
// } = *gate;
// assert!(q_c.is_zero(), "should not hardcode a constant into the input gate");
// assert!(q_c_next.is_zero(), "input gates should not link to the next gate");
// let a_value = self.get_value(a_var).expect("must get a variable value");
// let b_value = self.get_value(b_var).expect("must get a variable value");
// let c_value = self.get_value(c_var).expect("must get a variable value");
// let input_value = self.input_assingments[i];
// let mut res = input_value;
// res.negate();
// let mut tmp = q_l;
// tmp.mul_assign(&a_value);
// res.add_assign(&tmp);
// let mut tmp = q_r;
// tmp.mul_assign(&b_value);
// res.add_assign(&tmp);
// let mut tmp = q_o;
// tmp.mul_assign(&c_value);
// res.add_assign(&tmp);
// let mut tmp = q_m;
// tmp.mul_assign(&a_value);
// tmp.mul_assign(&b_value);
// res.add_assign(&tmp);
// if !res.is_zero() {
// println!("Unsatisfied at input gate {}: {:?}", i+1, gate);
// println!("A value = {}, B value = {}, C value = {}", a_value, b_value, c_value);
// return false;
// }
// }
// for (i, gate_pair) in self.aux_gates.windows(2).enumerate()
// {
// let this_gate = gate_pair[0];
// let next_gate = &gate_pair[1];
// let Gate::<E::Fr> {
// variables: [a_var, b_var, c_var],
// coefficients: [q_l, q_r, q_o, q_m, q_c, q_c_next]
// } = this_gate;
// let a_value = self.get_value(a_var).expect("must get a variable value");
// let b_value = self.get_value(b_var).expect("must get a variable value");
// let c_value = self.get_value(c_var).expect("must get a variable value");
// let next_gate_c_var = next_gate.variables[2];
// let c_next_value = self.get_value(next_gate_c_var).expect("must get a variable value");
// let mut res = q_c;
// let mut tmp = q_l;
// tmp.mul_assign(&a_value);
// res.add_assign(&tmp);
// let mut tmp = q_r;
// tmp.mul_assign(&b_value);
// res.add_assign(&tmp);
// let mut tmp = q_o;
// tmp.mul_assign(&c_value);
// res.add_assign(&tmp);
// let mut tmp = q_m;
// tmp.mul_assign(&a_value);
// tmp.mul_assign(&b_value);
// res.add_assign(&tmp);
// let mut tmp = q_c_next;
// tmp.mul_assign(&c_next_value);
// res.add_assign(&tmp);
// if !res.is_zero() {
// println!("Unsatisfied at aux gate {}", i+1);
// println!("Gate {:?}", this_gate);
// println!("A = {}, B = {}, C = {}", a_value, b_value, c_value);
// return false;
// }
// }
// if !in_a_middle {
// let i = self.aux_gates.len();
// let last_gate = *self.aux_gates.last().unwrap();
// let Gate::<E::Fr> {
// variables: [a_var, b_var, c_var],
// coefficients: [q_l, q_r, q_o, q_m, q_c, q_c_next]
// } = last_gate;
// let a_value = self.get_value(a_var).expect("must get a variable value");
// let b_value = self.get_value(b_var).expect("must get a variable value");
// let c_value = self.get_value(c_var).expect("must get a variable value");
// assert!(q_c_next.is_zero(), "last gate should not be linked to the next one");
// let mut res = q_c;
// let mut tmp = q_l;
// tmp.mul_assign(&a_value);
// res.add_assign(&tmp);
// let mut tmp = q_r;
// tmp.mul_assign(&b_value);
// res.add_assign(&tmp);
// let mut tmp = q_o;
// tmp.mul_assign(&c_value);
// res.add_assign(&tmp);
// let mut tmp = q_m;
// tmp.mul_assign(&a_value);
// tmp.mul_assign(&b_value);
// res.add_assign(&tmp);
// if !res.is_zero() {
// println!("Unsatisfied at aux gate {}", i+1);
// println!("Gate {:?}", last_gate);
// println!("A = {}, B = {}, C = {}", a_value, b_value, c_value);
// return false;
// }
// }
// true
// }
pub fn num_gates(&self) -> usize {
self.n
}
}