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
use crate::ml_sumcheck::data_structures::PolynomialInfo;
use crate::ml_sumcheck::protocol::prover::ProverMsg;
use crate::ml_sumcheck::protocol::IPForMLSumcheck;
use ark_ff::Field;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, SerializationError, Write};
use ark_std::rand::RngCore;
use ark_std::vec::Vec;
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct VerifierMsg<F: Field> {
pub randomness: F,
}
pub struct VerifierState<F: Field> {
round: usize,
nv: usize,
max_multiplicands: usize,
finished: bool,
polynomials_received: Vec<Vec<F>>,
randomness: Vec<F>,
}
pub struct SubClaim<F: Field> {
pub point: Vec<F>,
pub expected_evaluation: F,
}
impl<F: Field> IPForMLSumcheck<F> {
pub fn verifier_init(index_info: &PolynomialInfo) -> VerifierState<F> {
VerifierState {
round: 1,
nv: index_info.num_variables,
max_multiplicands: index_info.max_multiplicands,
finished: false,
polynomials_received: Vec::with_capacity(index_info.num_variables),
randomness: Vec::with_capacity(index_info.num_variables),
}
}
pub fn verify_round<R: RngCore>(
prover_msg: ProverMsg<F>,
mut verifier_state: VerifierState<F>,
rng: &mut R,
) -> (Option<VerifierMsg<F>>, VerifierState<F>) {
if verifier_state.finished {
panic!("Incorrect verifier state: Verifier is already finished.");
}
let msg = Self::sample_round(rng);
verifier_state.randomness.push(msg.randomness);
verifier_state
.polynomials_received
.push(prover_msg.evaluations);
if verifier_state.round == verifier_state.nv {
verifier_state.finished = true;
} else {
verifier_state.round += 1;
}
(Some(msg), verifier_state)
}
pub fn check_and_generate_subclaim(
verifier_state: VerifierState<F>,
asserted_sum: F,
) -> Result<SubClaim<F>, crate::Error> {
if !verifier_state.finished {
panic!("Verifier has not finished.");
}
let mut expected = asserted_sum;
if verifier_state.polynomials_received.len() != verifier_state.nv {
panic!("insufficient rounds");
}
for i in 0..verifier_state.nv {
let evaluations = &verifier_state.polynomials_received[i];
if evaluations.len() != verifier_state.max_multiplicands + 1 {
panic!("incorrect number of evaluations");
}
let p0 = evaluations[0];
let p1 = evaluations[1];
if p0 + p1 != expected {
return Err(crate::Error::Reject(Some(
"Prover message is not consistent with the claim.".into(),
)));
}
expected = interpolate_uni_poly(evaluations, verifier_state.randomness[i]);
}
return Ok(SubClaim {
point: verifier_state.randomness,
expected_evaluation: expected,
});
}
#[inline]
pub fn sample_round<R: RngCore>(rng: &mut R) -> VerifierMsg<F> {
VerifierMsg {
randomness: F::rand(rng),
}
}
}
pub(crate) fn interpolate_uni_poly<F: Field>(p_i: &[F], eval_at: F) -> F {
let mut result = F::zero();
let mut i = F::zero();
for term in p_i.iter() {
let mut term = *term;
let mut j = F::zero();
for _ in 0..p_i.len() {
if j != i {
term = term * (eval_at - j) / (i - j)
}
j += F::one();
}
i += F::one();
result += term;
}
result
}