-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteractive_sumcheck.rs
164 lines (131 loc) · 6.56 KB
/
interactive_sumcheck.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
// Sumcheck protocol
use ark_ff::PrimeField;
use polynomials::univariate_polynomial::UnivariatePolynomial;
pub struct Channel<F: PrimeField> {
pub message: Vec<UnivariatePolynomial<F>>,
}
#[cfg(test)]
mod tests {
use super::super::prover::Prover;
use polynomials::{
multilinear_polynomial::coef_form::{MultilinearMonomial, MultilinearPolynomial},
univariate_polynomial::UnivariatePolynomial,
};
use ark_bls12_381::Fr;
type Fq = Fr;
// #[test]
// fn test_sumcheck() {
// // At the begining of round, prover sends polynomial and the sum of evaluating the said poly at the boolean hypercube to the to verifier
// // Prover
// let poly = MultilinearPolynomial::<Fq>::interpolate(
// vec![
// Fq::from(3),
// Fq::from(5),
// Fq::from(2),
// Fq::from(9),
// Fq::from(11),
// Fq::from(16),
// Fq::from(1),
// Fq::from(5),
// ]
// );
// let mut num_of_vars = poly.terms[0].vars.len();
// let mut sum = Fq::from(0);
// for i in 0..2_usize.pow(num_of_vars as u32) {
// let eval_points = get_binary_string(i, num_of_vars);
// let eval_points_as_field : Vec<(usize, Fq)>= eval_points.chars().enumerate().map(|(a, b)| {
// if b == '0' {
// (a, Fq::from(0))
// } else {
// (a, Fq::from(1))
// }
// }).collect();
// let eval_res = &poly.evaluate(eval_points_as_field);
// sum += eval_res;
// };
// assert!(sum == Fq::from(52));
// // Prover generates a univariate polynomial by evaluating the polynomial at the boolean hypercube
// let mut round_0_poly = MultilinearPolynomial::<Fq>::new(vec![]);
// num_of_vars -= 1;
// let mut round = 0_usize;
// for i in 0..2_usize.pow(num_of_vars as u32) {
// let eval_points = get_binary_string(i, num_of_vars);
// let eval_points_as_field : Vec<(usize, Fq)>= eval_points.chars().enumerate().map(|(a, b)| {
// if b == '0' {
// (a + round.clone() + 1, Fq::from(0))
// } else {
// (a + round.clone() + 1, Fq::from(1))
// }
// }).collect();
// let eval_res = poly.clone().partial_eval(eval_points_as_field).relabel();
// round_0_poly = round_0_poly.add(eval_res.clone());
// };
// let round_0_univariate_poly = Polynomial::try_from(round_0_poly.relabel()).unwrap();
// // Prover sends univariate poly to verifier who then evaluates it at the boolean hypercube and checks that the sum equals the sum at the previous round
// let verifier_check = round_0_univariate_poly.evaluate(Fq::from(0)) + round_0_univariate_poly.evaluate(Fq::from(1));
// assert!(verifier_check == sum);
// // Round 1
// round += 1;
// // The verifier samples a random point and sends to the prover
// let round_1_challenge = Fq::from(5);
// let mut challenges = vec![];
// challenges.push(round_1_challenge);
// // Prover generates a univariate polynomial by evaluating the polynomial at the random point and the boolean hypercube
// let mut round_1_poly = MultilinearPolynomial::<Fq>::new(vec![]);
// num_of_vars -= 1;
// for i in 0..2_usize.pow(num_of_vars as u32) {
// let eval_points = get_binary_string(i, num_of_vars);
// let mut eval_points_as_field : Vec<(usize, Fq)>= eval_points.chars().enumerate().map(|(a, b)| {
// if b == '0' {
// (a + round.clone() + 1, Fq::from(0))
// } else {
// (a + round.clone() + 1, Fq::from(1))
// }
// }).collect();
// for i in 0..round {
// dbg!(&i);
// eval_points_as_field.push((i, challenges[i]));
// }
// let eval_res = poly.clone().partial_eval(eval_points_as_field).relabel();
// round_1_poly = round_1_poly.add(eval_res.clone());
// };
// let round_1_univariate_poly = Polynomial::try_from(round_1_poly.relabel()).unwrap();
// dbg!(&round_1_univariate_poly);
// // Prover sends univariate poly to verifier who then evaluates it at the boolean hypercube and checks that the sum equals the sum at the previous round
// let verifier_check = round_1_univariate_poly.evaluate(Fq::from(0)) + round_1_univariate_poly.evaluate(Fq::from(1));
// assert!(verifier_check == round_0_univariate_poly.evaluate(challenges[round - 1]));
// dbg!(&verifier_check);
// dbg!(round_0_univariate_poly.evaluate(challenges[round - 1]));
// }
#[test]
fn test_sumcheck_prover() {
let poly = MultilinearPolynomial::new(vec![
MultilinearMonomial::new(Fq::from(2), vec![true, true, false]),
MultilinearMonomial::new(Fq::from(3), vec![false, true, true]),
]);
let prover = Prover::new(poly, Fq::from(10));
let round_1_poly = prover.prove(&vec![]);
assert!(round_1_poly == UnivariatePolynomial::new(vec![Fq::from(3), Fq::from(4)]));
let round_2_poly = prover.prove(&vec![Fq::from(5)]);
assert!(round_2_poly == UnivariatePolynomial::new(vec![Fq::from(0), Fq::from(23)]));
let round_3_poly = prover.prove(&vec![Fq::from(5), Fq::from(9)]);
assert!(round_3_poly == UnivariatePolynomial::new(vec![Fq::from(90), Fq::from(27)]));
}
// #[test]
// fn test_sumcheck_verifier() {
// let poly = MultilinearPolynomial::new(vec![
// MultilinearMonomial::new(Fq::from(2), vec![true, true, false]),
// MultilinearMonomial::new(Fq::from(3), vec![false, true, true]),
// ]);
// let mut verifier = Verifier::new(poly, Fq::from(10));
// verifier.challenges.push(Fq::from(5));
// let round_1_verification = verifier.verify(Polynomial::new(vec![Fq::from(3), Fq::from(4)]));
// assert!(round_1_verification);
// verifier.challenges.push(Fq::from(9));
// let round_2_verification = verifier.verify(Polynomial::new(vec![Fq::from(0), Fq::from(23)]));
// assert!(round_2_verification);
// verifier.challenges.push(Fq::from(10));
// let round_3_verification = verifier.verify(Polynomial::new(vec![Fq::from(90), Fq::from(27)]));
// assert!(round_3_verification);
// }
}