Skip to content

Commit 8788c77

Browse files
committed
fix verifier logic for not including last round value
1 parent 80a33d3 commit 8788c77

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

mpcs/src/basefold.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -378,26 +378,28 @@ where
378378
);
379379
}
380380

381-
let max_num_var = rounds
381+
let (min_num_var, max_num_var) = rounds
382382
.iter()
383-
.map(|(commit, openings)| {
384-
let max_num_var = openings
385-
.iter()
386-
.map(|(num_vars, _)| *num_vars)
387-
.max()
388-
.unwrap();
389-
assert_eq!(
390-
commit.log2_max_codeword_size,
391-
max_num_var + Spec::get_rate_log()
392-
);
393-
max_num_var
383+
.flat_map(|(commit, openings)| {
384+
openings.iter().map(move |(num_vars, _)| {
385+
assert_eq!(
386+
commit.log2_max_codeword_size,
387+
*num_vars + Spec::get_rate_log()
388+
);
389+
*num_vars
390+
})
394391
})
395-
.max()
396-
.unwrap();
397-
if max_num_var < Spec::get_basecode_msg_size_log() {
398-
// all the matrices are trivial, so we can skip the folding
399-
return Ok(());
400-
}
392+
.minmax()
393+
.into_option()
394+
.expect("non-empty input");
395+
396+
assert!(
397+
min_num_var >= Spec::get_basecode_msg_size_log(),
398+
"invalid input: min_num_var {} < get_basecode_msg_size_log {}",
399+
min_num_var,
400+
Spec::get_basecode_msg_size_log()
401+
);
402+
401403
let num_rounds = max_num_var - Spec::get_basecode_msg_size_log();
402404

403405
// prepare folding challenges via sumcheck round msg + FRI commitment

mpcs/src/basefold/query_phase.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::slice;
1+
use std::{mem, slice};
22

33
use crate::{
44
Point,
@@ -228,7 +228,7 @@ pub fn batch_verifier_query_phase<E: ExtensionField, S: EncodingScheme<E>>(
228228
// first folding challenge
229229
let r = fold_challenges.first().unwrap();
230230
let coeff = S::verifier_folding_coeffs(vp, log2_height, idx);
231-
let (lo, hi) = reduced_openings_by_height[log2_height].unwrap();
231+
let (lo, hi) = mem::take(&mut reduced_openings_by_height[log2_height]).unwrap();
232232
let mut folded = codeword_fold_with_challenge(&[lo, hi], *r, coeff, inv_2);
233233

234234
for (
@@ -249,8 +249,10 @@ pub fn batch_verifier_query_phase<E: ExtensionField, S: EncodingScheme<E>>(
249249
let idx_sibling = idx & 0x01;
250250
let mut leafs = vec![*sibling_value; 2];
251251
leafs[idx_sibling] = folded;
252-
if let Some((lo, hi)) = reduced_openings_by_height[log2_height].as_mut() {
253-
leafs[idx_sibling] += if idx_sibling == 1 { *hi } else { *lo };
252+
253+
if let Some((lo, hi)) = mem::take(&mut reduced_openings_by_height[log2_height])
254+
{
255+
leafs[idx_sibling] += if idx_sibling == 1 { hi } else { lo };
254256
}
255257

256258
idx >>= 1;
@@ -270,6 +272,24 @@ pub fn batch_verifier_query_phase<E: ExtensionField, S: EncodingScheme<E>>(
270272
let coeff = S::verifier_folding_coeffs(vp, log2_height, idx);
271273
folded = codeword_fold_with_challenge(&[leafs[0], leafs[1]], *r, coeff, inv_2);
272274
}
275+
276+
// in the final folding round, we add the opening value associated with
277+
// the current `log2_height` into the folded result.
278+
// The choice between `lo` or `hi` depends on the sibling index.
279+
// afterward, we check that the final folded value matches the expected
280+
// entry in the `final_codeword`, as no merkle commitment exists for last round.
281+
log2_height -= 1;
282+
let idx_sibling = idx & 0x01;
283+
if let Some((lo, hi)) = mem::take(&mut reduced_openings_by_height[log2_height]) {
284+
folded += if idx_sibling == 1 { hi } else { lo };
285+
}
286+
287+
assert!(
288+
reduced_openings_by_height.iter().all(|v| v.is_none()),
289+
"there are unused openings remain"
290+
);
291+
292+
// final value check: validate the folded result matches the final codeword entry
273293
assert!(
274294
final_codeword.values[idx] == folded,
275295
"final_codeword.values[idx] value {:?} != folded {:?}",

0 commit comments

Comments
 (0)