Repository navigation
Expand file tree
/
Copy pathmod.rs
More file actions
420 lines (397 loc) · 17.5 KB
/
Copy pathmod.rs
File metadata and controls
420 lines (397 loc) · 17.5 KB
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
416
417
418
419
420
use std::iter::zip;
use itertools::{izip, zip_eq, Itertools};
use p3_challenger::{CanObserve, FieldChallenger};
use p3_commit::{Pcs, PolynomialSpace};
use p3_field::{FieldAlgebra, FieldExtensionAlgebra};
use p3_util::log2_strict_usize;
use tracing::instrument;
use crate::{
config::{Com, Domain, StarkGenericConfig, Val},
interaction::RapPhaseSeq,
keygen::{types::MultiStarkVerifyingKey, view::MultiStarkVerifyingKeyView},
proof::{AdjacentOpenedValues, Proof},
verifier::constraints::verify_single_rap_constraints,
};
pub mod constraints;
mod error;
/// Constraint folder
pub mod folder;
pub use error::*;
pub use folder::GenericVerifierConstraintFolder;
/// Verifies a partitioned proof of multi-matrix AIRs.
pub struct MultiTraceStarkVerifier<'c, SC: StarkGenericConfig> {
config: &'c SC,
}
impl<'c, SC: StarkGenericConfig> MultiTraceStarkVerifier<'c, SC> {
pub fn new(config: &'c SC) -> Self {
Self { config }
}
/// Verify collection of InteractiveAIRs and check the permutation
/// cumulative sum is equal to zero across all AIRs.
#[instrument(name = "MultiTraceStarkVerifier::verify", level = "debug", skip_all)]
pub fn verify(
&self,
challenger: &mut SC::Challenger,
mvk: &MultiStarkVerifyingKey<SC>,
proof: &Proof<SC>,
) -> Result<(), VerificationError> {
// Note: construction of view panics if any air_id exceeds number of AIRs in provided `MultiStarkVerifyingKey`
let mvk = mvk.view(&proof.get_air_ids());
self.verify_raps(challenger, &mvk, proof)?;
Ok(())
}
/// Verify general RAPs without checking any relations (e.g., cumulative sum) between exposed values of different RAPs.
///
/// Public values is a global list shared across all AIRs.
///
/// - `num_challenges_to_sample[i]` is the number of challenges to sample in the trace challenge phase corresponding to `proof.commitments.after_challenge[i]`. This must have length equal
/// to `proof.commitments.after_challenge`.
#[instrument(level = "debug", skip_all)]
pub fn verify_raps(
&self,
challenger: &mut SC::Challenger,
mvk: &MultiStarkVerifyingKeyView<Val<SC>, Com<SC>>,
proof: &Proof<SC>,
) -> Result<(), VerificationError> {
challenger.observe(mvk.pre_hash.clone());
let air_ids = proof.get_air_ids();
let num_airs = air_ids.len();
challenger.observe(Val::<SC>::from_canonical_usize(num_airs));
for &air_id in &air_ids {
challenger.observe(Val::<SC>::from_canonical_usize(air_id));
}
// Enforce trace height linear inequalities
for constraint in mvk.trace_height_constraints {
let sum = proof
.per_air
.iter()
.map(|ap| constraint.coefficients[ap.air_id] as u64 * ap.degree as u64)
.sum::<u64>();
if sum >= constraint.threshold as u64 {
return Err(VerificationError::InvalidProofShape);
}
}
// (T01a): Check that all `air_id`s are different and contained in `MultiStarkVerifyingKey`
{
let mut air_ids = air_ids;
air_ids.sort();
for ids in air_ids.windows(2) {
if ids[0] >= ids[1] {
return Err(VerificationError::DuplicateAirs);
}
}
}
// Note: (T02) is implicit in use of BTreeMap in FRI PCS verify
let public_values = proof.get_public_values();
// (T03a): verify shape of public values
{
for (pvs_per_air, vk) in zip_eq(&public_values, &mvk.per_air) {
if pvs_per_air.len() != vk.params.num_public_values {
return Err(VerificationError::InvalidProofShape);
}
}
}
// Challenger must observe public values
for pis in &public_values {
challenger.observe_slice(pis);
}
for preprocessed_commit in mvk.flattened_preprocessed_commits() {
challenger.observe(preprocessed_commit);
}
// (T04a): validate shapes of `main_trace_commits`:
{
let num_cached_mains = mvk
.per_air
.iter()
.map(|vk| vk.params.width.cached_mains.len())
.sum::<usize>();
let num_main_commits = num_cached_mains + 1; // always 1 common main
if proof.commitments.main_trace.len() != num_main_commits {
return Err(VerificationError::InvalidProofShape);
}
}
// Observe main trace commitments
challenger.observe_slice(&proof.commitments.main_trace);
challenger.observe_slice(
&proof
.per_air
.iter()
.map(|ap| Val::<SC>::from_canonical_usize(log2_strict_usize(ap.degree)))
.collect_vec(),
);
// Verification of challenge phase (except openings, which are done next).
let rap_phase = self.config.rap_phase_seq();
let exposed_values_per_air_per_phase = proof
.per_air
.iter()
.map(|proof| proof.exposed_values_after_challenge.clone())
.collect_vec();
let permutation_opened_values = proof
.opening
.values
.after_challenge
.iter()
.map(|after_challenge_per_matrix| {
after_challenge_per_matrix
.iter()
.map(|after_challenge| {
vec![after_challenge.local.clone(), after_challenge.next.clone()]
})
.collect_vec()
})
.collect_vec();
// (T01b): `num_phases < 2`.
// Assumption: valid mvk has num_phases consistent between num_challenges_to_sample and exposed_values
let num_phases = mvk.num_phases();
if num_phases != proof.commitments.after_challenge.len() || num_phases > 1 {
return Err(VerificationError::InvalidProofShape);
}
// (T01c): validate shape of `exposed_values_after_challenge`
if !zip_eq(&exposed_values_per_air_per_phase, &mvk.per_air).all(|(ev_per_phase, vk)| {
ev_per_phase.len() == vk.params.num_exposed_values_after_challenge.len()
&& zip_eq(ev_per_phase, &vk.params.num_exposed_values_after_challenge)
.all(|(ev, &n)| ev.len() == n)
}) {
return Err(VerificationError::InvalidProofShape);
}
let (after_challenge_data, rap_phase_seq_result) = rap_phase.partially_verify(
challenger,
proof.rap_phase_seq_proof.as_ref(),
&exposed_values_per_air_per_phase,
&proof.commitments.after_challenge,
&permutation_opened_values,
);
// We don't want to bail on error yet; `OodEvaluationMismatch` should take precedence over
// `ChallengePhaseError`, but we won't know if the former happens until later.
let rap_phase_seq_result =
rap_phase_seq_result.map_err(|_| VerificationError::ChallengePhaseError);
// Draw `alpha` challenge
let alpha: SC::Challenge = challenger.sample_ext_element();
tracing::debug!("alpha: {alpha:?}");
// Observe quotient commitments
challenger.observe(proof.commitments.quotient.clone());
// Draw `zeta` challenge
let zeta: SC::Challenge = challenger.sample_ext_element();
tracing::debug!("zeta: {zeta:?}");
let pcs = self.config.pcs();
// Build domains
let (domains, quotient_chunks_domains): (Vec<_>, Vec<Vec<_>>) = mvk
.per_air
.iter()
.zip_eq(&proof.per_air)
.map(|(vk, air_proof)| {
let degree = air_proof.degree;
let quotient_degree = vk.quotient_degree;
let domain = pcs.natural_domain_for_degree(degree);
let quotient_domain =
domain.create_disjoint_domain(degree * quotient_degree as usize);
let qc_domains = quotient_domain.split_domains(quotient_degree as usize);
(domain, qc_domains)
})
.unzip();
// Verify all opening proofs
let opened_values = &proof.opening.values;
let trace_domain_and_openings =
|domain: Domain<SC>,
zeta: SC::Challenge,
values: &AdjacentOpenedValues<SC::Challenge>| {
(
domain,
vec![
(zeta, values.local.clone()),
(domain.next_point(zeta).unwrap(), values.next.clone()),
],
)
};
// Build the opening rounds
// 1. First the preprocessed trace openings
// Assumption: each AIR with preprocessed trace has its own commitment and opening values
// T05a: validate `opened_values.preprocessed` shape
let preprocessed_widths: Vec<usize> = mvk
.per_air
.iter()
.filter_map(|vk| vk.params.width.preprocessed)
.collect();
if preprocessed_widths.len() != opened_values.preprocessed.len()
|| zip_eq(preprocessed_widths, &opened_values.preprocessed)
.any(|(w, ov)| w != ov.local.len() || w != ov.next.len())
{
return Err(VerificationError::InvalidProofShape);
}
let mut rounds: Vec<_> = mvk
.preprocessed_commits()
.into_iter()
.zip_eq(&domains)
.flat_map(|(commit, domain)| commit.map(|commit| (commit, *domain)))
.zip_eq(&opened_values.preprocessed)
.map(|((commit, domain), values)| {
let domain_and_openings = trace_domain_and_openings(domain, zeta, values);
(commit, vec![domain_and_openings])
})
.collect();
// 2. Then the main trace openings
// T05b: validate `opened_values.main` shape inline below
let num_main_commits = opened_values.main.len();
if num_main_commits != proof.commitments.main_trace.len() {
return Err(VerificationError::InvalidProofShape);
}
let mut main_commit_idx = 0;
// All commits except the last one are cached main traces.
for (vk, domain) in zip_eq(&mvk.per_air, &domains) {
for &cached_main_width in &vk.params.width.cached_mains {
let commit = proof.commitments.main_trace[main_commit_idx].clone();
if opened_values.main[main_commit_idx].len() != 1 {
return Err(VerificationError::InvalidProofShape);
}
let value = &opened_values.main[main_commit_idx][0];
if cached_main_width != value.local.len() || cached_main_width != value.next.len() {
return Err(VerificationError::InvalidProofShape);
}
let domains_and_openings = vec![trace_domain_and_openings(*domain, zeta, value)];
rounds.push((commit.clone(), domains_and_openings));
main_commit_idx += 1;
}
}
// In the last commit, each matrix corresponds to an AIR with a common main trace.
{
let values_per_mat = &opened_values.main[main_commit_idx];
let commit = proof.commitments.main_trace[main_commit_idx].clone();
let domains_and_openings = zip(&mvk.per_air, &domains)
.filter(|(vk, _)| vk.has_common_main())
.zip(values_per_mat)
.map(|((vk, domain), values)| {
let width = vk.params.width.common_main;
if width != values.local.len() || width != values.next.len() {
Err(VerificationError::InvalidProofShape)
} else {
Ok(trace_domain_and_openings(*domain, zeta, values))
}
})
.collect::<Result<Vec<_>, _>>()?;
if domains_and_openings.len() != values_per_mat.len() {
return Err(VerificationError::InvalidProofShape);
}
rounds.push((commit.clone(), domains_and_openings));
}
let ext_degree = <SC::Challenge as FieldExtensionAlgebra<Val<SC>>>::D;
// 3. Then after_challenge trace openings, at most 1 phase for now.
// All AIRs with interactions should an after challenge trace.
let mut after_challenge_vk_domain_per_air = zip_eq(&mvk.per_air, &domains)
.filter(|(vk, _)| vk.has_interaction())
.peekable();
if after_challenge_vk_domain_per_air.peek().is_none() {
if !proof.commitments.after_challenge.is_empty()
|| !opened_values.after_challenge.is_empty()
{
return Err(VerificationError::InvalidProofShape);
}
assert_eq!(num_phases, 0);
} else {
if num_phases != 1 || opened_values.after_challenge.len() != 1 {
return Err(VerificationError::InvalidProofShape);
}
let after_challenge_commit = proof.commitments.after_challenge[0].clone();
let domains_and_openings = zip(
after_challenge_vk_domain_per_air,
&opened_values.after_challenge[0],
)
.map(|((vk, domain), values)| {
let width = vk.params.width.after_challenge[0] * ext_degree;
if width != values.local.len() || width != values.next.len() {
Err(VerificationError::InvalidProofShape)
} else {
Ok(trace_domain_and_openings(*domain, zeta, values))
}
})
.collect::<Result<Vec<_>, _>>()?;
if domains_and_openings.len() != opened_values.after_challenge[0].len() {
return Err(VerificationError::InvalidProofShape);
}
rounds.push((after_challenge_commit, domains_and_openings));
}
if opened_values.quotient.len() != num_airs {
return Err(VerificationError::InvalidProofShape);
}
// All opened_values.quotient should have width D
if zip_eq(&opened_values.quotient, &mvk.per_air).any(|(per_air, vk)| {
per_air.len() != vk.quotient_degree as usize || {
per_air
.iter()
.any(|per_chunk| per_chunk.len() != ext_degree)
}
}) {
return Err(VerificationError::InvalidProofShape);
}
let quotient_domains_and_openings =
zip_eq(&opened_values.quotient, "ient_chunks_domains)
.flat_map(|(chunk, quotient_chunks_domains_per_air)| {
chunk
.iter()
.zip_eq(quotient_chunks_domains_per_air)
.map(|(values, &domain)| (domain, vec![(zeta, values.clone())]))
})
.collect_vec();
rounds.push((
proof.commitments.quotient.clone(),
quotient_domains_and_openings,
));
pcs.verify(rounds, &proof.opening.proof, challenger)
.map_err(|e| VerificationError::InvalidOpeningArgument(format!("{:?}", e)))?;
let mut preprocessed_idx = 0usize; // preprocessed commit idx
let mut after_challenge_idx = vec![0usize; num_phases];
let mut cached_main_commit_idx = 0;
let mut common_main_matrix_idx = 0;
// Verify each RAP's constraints
for (domain, qc_domains, quotient_chunks, vk, air_proof) in izip!(
domains,
quotient_chunks_domains,
&opened_values.quotient,
&mvk.per_air,
&proof.per_air
) {
let preprocessed_values = vk.preprocessed_data.as_ref().map(|_| {
let values = &opened_values.preprocessed[preprocessed_idx];
preprocessed_idx += 1;
values
});
let mut partitioned_main_values = Vec::with_capacity(vk.num_cached_mains());
for _ in 0..vk.num_cached_mains() {
partitioned_main_values.push(&opened_values.main[cached_main_commit_idx][0]);
cached_main_commit_idx += 1;
}
if vk.has_common_main() {
partitioned_main_values
.push(&opened_values.main.last().unwrap()[common_main_matrix_idx]);
common_main_matrix_idx += 1;
}
// loop through challenge phases of this single RAP
let after_challenge_values = if vk.has_interaction() {
(0..num_phases)
.map(|phase_idx| {
let matrix_idx = after_challenge_idx[phase_idx];
after_challenge_idx[phase_idx] += 1;
&opened_values.after_challenge[phase_idx][matrix_idx]
})
.collect_vec()
} else {
vec![]
};
verify_single_rap_constraints::<SC>(
&vk.symbolic_constraints.constraints,
preprocessed_values,
partitioned_main_values,
after_challenge_values,
quotient_chunks,
domain,
&qc_domains,
zeta,
alpha,
&after_challenge_data.challenges_per_phase,
&air_proof.public_values,
&air_proof.exposed_values_after_challenge,
)?;
}
// If we made it this far, use the `rap_phase_result` as the final result.
rap_phase_seq_result
}
}