forked from privacy-scaling-explorations/halo2
-
Notifications
You must be signed in to change notification settings - Fork 39
/
verifier.rs
191 lines (169 loc) · 5.91 KB
/
verifier.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
use std::iter;
use super::super::{
circuit::Expression, ChallengeBeta, ChallengeGamma, ChallengeTheta, ChallengeX,
};
use super::Argument;
use crate::{
arithmetic::CurveAffine,
plonk::{Error, VerifyingKey},
poly::{commitment::MSM, Rotation, VerifierQuery},
transcript::{EncodedChallenge, TranscriptRead},
};
use ff::{BatchInvert, Field, PrimeField, WithSmallOrderMulGroup};
pub struct PreparedCommitments<C: CurveAffine> {
m_commitment: C,
}
pub struct Committed<C: CurveAffine> {
prepared: PreparedCommitments<C>,
phi_commitment: C,
}
pub struct Evaluated<C: CurveAffine> {
committed: Committed<C>,
phi_eval: C::Scalar,
phi_next_eval: C::Scalar,
m_eval: C::Scalar,
}
impl<F: PrimeField + WithSmallOrderMulGroup<3>> Argument<F> {
pub(in crate::plonk) fn read_prepared_commitments<
C: CurveAffine,
E: EncodedChallenge<C>,
T: TranscriptRead<C, E>,
>(
&self,
transcript: &mut T,
) -> Result<PreparedCommitments<C>, Error> {
let m_commitment = transcript.read_point()?;
Ok(PreparedCommitments { m_commitment })
}
}
impl<C: CurveAffine> PreparedCommitments<C> {
pub(in crate::plonk) fn read_grand_sum_commitment<
E: EncodedChallenge<C>,
T: TranscriptRead<C, E>,
>(
self,
transcript: &mut T,
) -> Result<Committed<C>, Error> {
let phi_commitment = transcript.read_point()?;
Ok(Committed {
prepared: self,
phi_commitment,
})
}
}
impl<C: CurveAffine> Committed<C> {
pub(crate) fn evaluate<E: EncodedChallenge<C>, T: TranscriptRead<C, E>>(
self,
transcript: &mut T,
) -> Result<Evaluated<C>, Error> {
let phi_eval = transcript.read_scalar()?;
let phi_next_eval = transcript.read_scalar()?;
let m_eval = transcript.read_scalar()?;
Ok(Evaluated {
committed: self,
phi_eval,
phi_next_eval,
m_eval,
})
}
}
impl<C: CurveAffine> Evaluated<C> {
pub(in crate::plonk) fn expressions<'a>(
&'a self,
l_0: C::Scalar,
l_last: C::Scalar,
l_blind: C::Scalar,
argument: &'a Argument<C::Scalar>,
theta: ChallengeTheta<C>,
beta: ChallengeBeta<C>,
advice_evals: &[C::Scalar],
fixed_evals: &[C::Scalar],
instance_evals: &[C::Scalar],
challenges: &[C::Scalar],
) -> impl Iterator<Item = C::Scalar> + 'a {
let active_rows = C::Scalar::ZERO - (l_last + l_blind);
/*
φ_i(X) = f_i(X) + beta
τ(X) = t(X) + beta
LHS = τ(X) * Π(φ_i(X)) * (ϕ(gX) - ϕ(X))
RHS = τ(X) * Π(φ_i(X)) * (∑ 1/(φ_i(X)) - m(X) / τ(X))))
*/
let grand_sum_expression = || {
let compress_expressions = |expressions: &[Expression<C::Scalar>]| {
expressions
.iter()
.map(|expression| {
expression.evaluate(
&|scalar| scalar,
&|_| panic!("virtual selectors are removed during optimization"),
&|query| fixed_evals[query.index],
&|query| advice_evals[query.index],
&|query| instance_evals[query.index],
&|challenge| challenges[challenge.index()],
&|a| -a,
&|a, b| a + &b,
&|a, b| a * &b,
&|a, scalar| a * &scalar,
)
})
.fold(C::Scalar::ZERO, |acc, eval| acc * &*theta + &eval)
};
// φ_i(X) = f_i(X) + beta
let mut f_evals: Vec<_> = argument
.inputs_expressions
.iter()
.map(|input_expressions| compress_expressions(input_expressions) + *beta)
.collect();
let t_eval = compress_expressions(&argument.table_expressions);
let tau = t_eval + *beta;
// Π(φ_i(X))
let prod_fi = f_evals.iter().fold(C::Scalar::ZERO, |acc, eval| acc * eval);
// ∑ 1/(φ_i(X))
let sum_inv_fi = {
f_evals.batch_invert();
f_evals.iter().fold(C::Scalar::ZERO, |acc, eval| acc + eval)
};
// LHS = τ(X) * Π(φ_i(X)) * (ϕ(gX) - ϕ(X))
let lhs = tau * prod_fi * (self.phi_next_eval - self.phi_eval);
// RHS = τ(X) * Π(φ_i(X)) * (∑ 1/(φ_i(X)) - m(X) / τ(X))))
let rhs = { tau * prod_fi * (sum_inv_fi - self.m_eval * tau.invert().unwrap()) };
(lhs - rhs) * active_rows
};
std::iter::empty()
.chain(
// phi[0] = 0
Some(l_0 * self.phi_eval),
)
.chain(
// phi[u] = 0
Some(l_last * self.phi_eval),
)
.chain(
// (1 - l_last - l_blind) * (lhs - rhs) = 0
Some(grand_sum_expression()),
)
}
pub(in crate::plonk) fn queries<'r, M: MSM<C> + 'r>(
&'r self,
vk: &'r VerifyingKey<C>,
x: ChallengeX<C>,
) -> impl Iterator<Item = VerifierQuery<'r, C, M>> + Clone {
let x_next = vk.domain.rotate_omega(*x, Rotation::next());
iter::empty()
.chain(Some(VerifierQuery::new_commitment(
&self.committed.phi_commitment,
*x,
self.phi_eval,
)))
.chain(Some(VerifierQuery::new_commitment(
&self.committed.phi_commitment,
x_next,
self.phi_next_eval,
)))
.chain(Some(VerifierQuery::new_commitment(
&self.committed.prepared.m_commitment,
*x,
self.m_eval,
)))
}
}