-
Notifications
You must be signed in to change notification settings - Fork 2
/
qap.rs
415 lines (358 loc) · 11.5 KB
/
qap.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
use std::ops::Mul;
use num_traits::Zero;
use crate::building_block::{
field::Field,
to_biguint::ToBigUint,
};
use crate::snarks::{
r1cs::R1CS,
polynomial::{
Polynomial,
DivResult,
},
sparse_vec::SparseVec,
sparse_matrix::SparseMatrix,
};
pub enum ApplyWitness {
Beginning,
End,
}
pub struct QAP {
pub f: Field,
pub a_polys: SparseMatrix,
pub b_polys: SparseMatrix,
pub c_polys: SparseMatrix,
}
impl QAP {
// build a polynomial that evaluates to target_val at x == index
// and zero for x != index.
// e.g.
// (x - 2) * (x - 3) * 3 / ((1 - 2) * (1 - 3))
// where x in [1, 2, 3]; evaluates to 3 if x == 1 and 0 if x != 1
fn build_polynomial_for_target_values(
f: &Field,
target_vals: &SparseVec,
) -> Polynomial {
let mut target_val_polys = vec![];
let one = f.elem(&1u8);
let mut target_x = f.elem(&1u8);
while target_x <= target_vals.size {
let target_val = target_vals.get(&(&target_x - &one));
// if target val is zero, simply add 0x^0
if target_val.n.is_zero() {
target_val_polys.push(Polynomial::new(f, vec![f.elem(&0u8)]));
target_x.inc();
continue;
}
let mut numerator_polys = vec![
Polynomial::new(f, vec![target_val.clone()]),
];
let mut denominator = f.elem(&1u8);
let mut i = f.elem(&1u8);
while i <= target_vals.size {
if i == target_x {
i.inc();
continue;
}
// (x - i) to let the polynomal evaluate to zero at x = i
let numerator_poly = Polynomial::new(f, vec![
-f.elem(&i),
f.elem(&1u8),
]);
numerator_polys.push(numerator_poly);
// (target_idx - i) to cancel out the corresponding
// numerator_poly at x = target_idx
denominator = denominator * (f.elem(&target_x) - f.elem(&i));
i.inc();
}
// merge denominator polynomial to numerator polynomial vector
let denominator_poly = Polynomial::new(f, vec![denominator.inv()]);
let mut polys = numerator_polys;
polys.push(denominator_poly);
// aggregate numerator polynomial vector
let mut acc_poly = Polynomial::new(f, vec![f.elem(&1u8)]);
for poly in polys {
acc_poly = acc_poly.mul(&poly);
}
target_val_polys.push(acc_poly);
target_x.inc();
}
// aggregate polynomials for all target values
let mut res = target_val_polys[0].clone();
for x in &target_val_polys[1..] {
res = res + x;
}
res
}
pub fn build(
f: &Field,
r1cs: &R1CS,
apply_witness: &ApplyWitness,
) -> QAP {
/*
a^t
a a1 a2
a1 [0 3 0 0] -> |0 0|
a2 [0 0 0 2] |3 0| <- need polynomial that returns
+------^ |0 0| 3 at x=1 and 0 at x=2
r1cs selector * |0 2| <- here polynomial that returns
witness x=1 x=2 0 at x=1 and 2 at x=2
x-th col corresponds to x-th constraint
*/
let r1cs = match apply_witness {
ApplyWitness::Beginning => r1cs.to_constraint_by_witness_matrices(),
ApplyWitness::End => r1cs.to_constraint_matrices(),
};
let a_t = r1cs.a.transpose();
let b_t = r1cs.b.transpose();
let c_t = r1cs.c.transpose();
let mut a_coeffs: Vec<SparseVec> = vec![];
let mut b_coeffs: Vec<SparseVec> = vec![];
let mut c_coeffs: Vec<SparseVec> = vec![];
let mut y = f.elem(&0u8);
let height = &a_t.height; // a_t, b_t and c_t are of the same dimention
let width = &a_t.width;
while &y < height {
let a_row = a_t.get_row(&y);
let b_row = b_t.get_row(&y);
let c_row = c_t.get_row(&y);
a_coeffs.push(QAP::build_polynomial_for_target_values(f, &a_row).to_sparse_vec(width));
b_coeffs.push(QAP::build_polynomial_for_target_values(f, &b_row).to_sparse_vec(width));
c_coeffs.push(QAP::build_polynomial_for_target_values(f, &c_row).to_sparse_vec(width));
y.inc();
}
let a_polys = SparseMatrix::from(&a_coeffs);
let b_polys = SparseMatrix::from(&b_coeffs);
let c_polys = SparseMatrix::from(&c_coeffs);
QAP { f: f.clone(), a_polys, b_polys, c_polys }
}
// build polynomial (x-1)(x-2)..(x-num_constraints)
pub fn build_z(f: &Field, num_constraints: &impl ToBigUint) -> Polynomial {
let num_constraints = f.elem(num_constraints);
let mut i = f.elem(&1u8);
let mut polys = vec![];
// create (x-i) polynomials
while i <= num_constraints {
let poly = Polynomial::new(f, vec![
-f.elem(&i),
f.elem(&1u8),
]);
polys.push(poly);
i.inc();
}
// aggregate (x-i) polynomial into a single polynomial
let mut acc_poly = Polynomial::new(f, vec![f.elem(&1u8)]);
for poly in polys {
acc_poly = acc_poly.mul(&poly);
}
acc_poly
}
pub fn check_constraints(
&self,
witness: &SparseVec,
num_constraints: &impl ToBigUint,
apply_witness: &ApplyWitness,
) -> bool {
// aggregate polynomials by calculating dot products with witness
let a_poly: Polynomial = match apply_witness {
ApplyWitness::Beginning => (&self.a_polys.flatten_rows()).into(),
ApplyWitness::End => (&self.a_polys.multiply_column(witness).flatten_rows()).into(),
};
let b_poly: Polynomial = match apply_witness {
ApplyWitness::Beginning => (&self.b_polys.flatten_rows()).into(),
ApplyWitness::End => (&self.b_polys.multiply_column(witness).flatten_rows()).into(),
};
let c_poly: Polynomial = match apply_witness {
ApplyWitness::Beginning => (&self.c_polys.flatten_rows()).into(),
ApplyWitness::End => (&self.c_polys.multiply_column(witness).flatten_rows()).into(),
};
let t = a_poly * &b_poly - &c_poly;
let num_constraints = self.f.elem(num_constraints);
let z = QAP::build_z(&self.f, &num_constraints);
match t.divide_by(&z) {
DivResult::Quotient(_) => true,
DivResult::QuotientRemainder(_) => false,
}
}
pub fn get_flattened_polys(&self) -> (Polynomial, Polynomial, Polynomial) {
let a_poly: Polynomial = (&self.a_polys.flatten_rows()).into();
let b_poly: Polynomial = (&self.b_polys.flatten_rows()).into();
let c_poly: Polynomial = (&self.c_polys.flatten_rows()).into();
(a_poly, b_poly, c_poly)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
building_block::field::FieldElem,
snarks::{
constraint::Constraint,
gate::Gate,
equation_parser::Parser,
r1cs_tmpl::R1CSTmpl,
sparse_vec::SparseVec,
term::Term,
}
};
use std::collections::HashMap;
#[test]
fn test_r1cs_to_polynomial() {
let f = &Field::new(&3911u16);
// x out t1 y t2
// 0 1 2 3 4 5
// [1, 3, 35, 9, 27, 30]
let witness = SparseVec::from(&vec![
f.elem(&1u8),
f.elem(&3u8),
f.elem(&35u8),
f.elem(&9u8),
f.elem(&27u8),
f.elem(&30u8),
]);
let witness_size = &witness.size;
// A
// 0 1 2 3 4 5
// [0, 1, 0, 0, 0, 0]
// [0, 0, 0, 1, 0, 0]
// [0, 1, 0, 0, 1, 0]
// [5, 0, 0, 0, 0, 1]
let mut a1 = SparseVec::new(f, witness_size);
a1.set(&1u8, &1u8);
let mut a2 = SparseVec::new(f, witness_size);
a2.set(&3u8, &1u8);
let mut a3 = SparseVec::new(f, witness_size);
a3.set(&1u8, &1u8);
a3.set(&4u8, &1u8);
let mut a4 = SparseVec::new(f, witness_size);
a4.set(&0u8, &5u8);
a4.set(&5u8, &1u8);
// B
// 0 1 2 3 4 5
// [0, 1, 0, 0, 0, 0]
// [0, 1, 0, 0, 0, 0]
// [1, 0, 0, 0, 0, 0]
// [1, 0, 0, 0, 0, 0]
let mut b1 = SparseVec::new(f, witness_size);
b1.set(&1u8, &1u8);
let mut b2 = SparseVec::new(f, witness_size);
b2.set(&1u8, &1u8);
let mut b3 = SparseVec::new(f, witness_size);
b3.set(&0u8, &1u8);
let mut b4 = SparseVec::new(f, witness_size);
b4.set(&0u8, &1u8);
// C
// 0 1 2 3 4 5
// [0, 0, 0, 1, 0, 0]
// [0, 0, 0, 0, 1, 0]
// [0, 0, 0, 0, 0, 1]
// [0, 0, 1, 0, 0, 0]
let mut c1 = SparseVec::new(f, witness_size);
c1.set(&3u8, &1u8);
let mut c2 = SparseVec::new(f, witness_size);
c2.set(&4u8, &1u8);
let mut c3 = SparseVec::new(f, witness_size);
c3.set(&5u8, &1u8);
let mut c4 = SparseVec::new(f, witness_size);
c4.set(&2u8, &1u8);
let constraints = vec![
Constraint::new(&a1, &b1, &c1),
Constraint::new(&a2, &b2, &c2),
Constraint::new(&a3, &b3, &c3),
Constraint::new(&a4, &b4, &c4),
];
let num_constraints = &constraints.len();
let r1cs = R1CS { constraints, witness: witness.clone() };
for apply_witness in vec![ApplyWitness::Beginning, ApplyWitness::End] {
let qap = QAP::build(f, &r1cs, &apply_witness);
let is_passed = qap.check_constraints(&witness, num_constraints, &apply_witness);
assert!(is_passed);
}
}
#[test]
fn test_build_z() {
let f = &Field::new(&3911u16);
let one = &f.elem(&1u8);
let two = &f.elem(&2u8);
let neg_three = &f.elem(&3u8).negate();
let z = QAP::build_z(f, two);
assert_eq!(z.len(), 3);
assert_eq!(&z[0], two);
assert_eq!(&z[1], neg_three);
assert_eq!(&z[2], one);
}
#[test]
fn blog_post_1_example_1() {
let f = &Field::new(&37u8);
let expr = "(x * x * x) + x + 5 == 35";
let eq = Parser::parse(f, expr).unwrap();
let gates = &Gate::build(f, &eq);
let r1cs_tmpl = R1CSTmpl::from_gates(f, gates);
// build witness
/*
x = 3
t1 = x(3) * x(3) = 9
t2 = t1(9) * x(3) = 27
t3 = x(3) + 5 = 8
out = t2(27) + t2(8) = 35
*/
let witness = {
use crate::snarks::term::Term::*;
HashMap::<Term, FieldElem>::from([
(Term::var("x"), f.elem(&3u8)),
(TmpVar(1), f.elem(&9u8)),
(TmpVar(2), f.elem(&27u8)),
(TmpVar(3), f.elem(&8u8)),
(Out, eq.rhs),
])
};
let r1cs = R1CS::from_tmpl(f, &r1cs_tmpl, &witness).unwrap();
let qap = QAP::build(f, &r1cs, &ApplyWitness::Beginning);
println!("a:\n{}", qap.a_polys.pretty_print());
println!("b:\n{}", qap.b_polys.pretty_print());
println!("c:\n{}", qap.c_polys.pretty_print());
}
#[test]
fn blog_post_1_example_2() {
let f = &Field::new(&37u8);
let expr = "(x * x * x) + x + 5 == 35";
let eq = Parser::parse(f, expr).unwrap();
let gates = &Gate::build(f, &eq);
let r1cs_tmpl = R1CSTmpl::from_gates(f, gates);
// build witness
let good_witness = {
use crate::snarks::term::Term::*;
HashMap::<Term, FieldElem>::from([
(Term::var("x"), f.elem(&3u8)),
(TmpVar(1), f.elem(&9u8)),
(TmpVar(2), f.elem(&27u8)),
(TmpVar(3), f.elem(&8u8)),
(Out, f.elem(&35u8)),
])
};
let bad_witness = {
use crate::snarks::term::Term::*;
HashMap::<Term, FieldElem>::from([
(Term::var("x"), f.elem(&4u8)), // replaced 3 with 4
(TmpVar(1), f.elem(&9u8)),
(TmpVar(2), f.elem(&27u8)),
(TmpVar(3), f.elem(&8u8)),
(Out, f.elem(&35u8)),
])
};
for test_case in vec![("good", good_witness), ("bad", bad_witness)] {
let (name, witness) = test_case;
let r1cs = R1CS::from_tmpl(f, &r1cs_tmpl, &witness).unwrap();
let qap = QAP::build(f, &r1cs, &ApplyWitness::Beginning);
let (a, b, c) = qap.get_flattened_polys();
let t = a * &b - &c;
let num_constraints = f.elem(&gates.len());
let z = QAP::build_z(f, &num_constraints);
let is_witness_valid = match t.divide_by(&z) {
DivResult::Quotient(_) => true,
DivResult::QuotientRemainder(_) => false,
};
println!("is {} witness valid? -> {}", name, is_witness_valid);
}
}
}