Skip to main content

binius_iop/basefold/
opening.rs

1// Copyright 2025 Irreducible Inc.
2// Copyright 2026 The Binius Developers
3
4//! The core BaseFold opening protocol on the verifier side.
5
6use binius_field::BinaryField;
7use binius_ip::sumcheck::{RoundCoeffs, RoundProof};
8use binius_utils::checked_arithmetics::log2_ceil_usize;
9
10use super::error::Error;
11use crate::{
12	fri::{FRIFoldVerifier, FRIParams, verify::FRIQueryVerifier},
13	merkle_channel::MerkleIPVerifierChannel,
14};
15
16/// Verifies the single BaseFold sumcheck opening of the Batched ZK BaseFold construction
17/// (binius.xyz#128, step 4).
18///
19/// This is the verifier counterpart of `binius_iop_prover::basefold::prove_sumcheck_basefold`. The
20/// claim is `Σ_{X,Z} G(X, Z) · T(X, Z) = eval_claim`, where `G` is the multilinear the FRI opens:
21/// the γ-folded round oracles, concatenated along the oracle-index variables `Z` (`log_n_oracles`
22/// of them) with `G(X, Z) = Σ_r M^γ_r↑(X) · eq(r, Z)`. `T` is the verifier-computable multiplicand.
23///
24/// The sumcheck binds the variables in the order the FRI folder consumes its challenges: the mask
25/// fold γ (no round polynomial, and only if some round is masked), then the `Z` variables, then
26/// the `X` variables, low to high. Each round polynomial is degree 2 and is received before the
27/// FRI round is processed, so that a commitment the FRI sends in the same round lands after it.
28///
29/// ## Arguments
30///
31/// * `codeword_commitments` - one per oracle, in the same order as [`FRIParams::input_oracles`], as
32///   commitment handles previously received over `channel`.
33/// * `eval_claim` - the target `Σ_{X,Z} G · T`.
34/// * `n_vars` - the number of variables `X`, the largest round oracle's.
35/// * `batch_challenge` - the mask fold challenge `γ`, if any round is masked.
36/// * `multiplicand_eval` - evaluates `T(ρ, ζ)`. It receives `ρ` in the order sampled, which is the
37///   variable order (low to high), and `ζ` in the order sampled, which is the bit order of the
38///   oracle index.
39/// * `channel` - the Merkle channel carrying all prover interaction: round coefficients,
40///   challenges, commitments, and query openings.
41///
42/// The final consistency check `G(ρ, ζ) · T(ρ, ζ) = final sum` is asserted internally, so `Ok`
43/// means the opening verified.
44pub fn verify_sumcheck_basefold<F, Channel>(
45	fri_params: &FRIParams<F>,
46	codeword_commitments: &[Channel::Commitment],
47	eval_claim: Channel::Elem,
48	n_vars: usize,
49	batch_challenge: Option<Channel::Elem>,
50	multiplicand_eval: impl FnOnce(&[Channel::Elem], &[Channel::Elem]) -> Channel::Elem,
51	channel: &mut Channel,
52) -> Result<(), Error>
53where
54	F: BinaryField,
55	Channel: MerkleIPVerifierChannel<F>,
56	Channel::Elem: From<F>,
57{
58	// The round polynomial of a product of two multilinears is degree 2.
59	const DEGREE: usize = 2;
60
61	let log_n_oracles = log2_ceil_usize(codeword_commitments.len());
62	let n_inner = usize::from(batch_challenge.is_some());
63	let mut fri_challenges = Vec::with_capacity(n_inner + log_n_oracles + n_vars);
64	let mut fri_fold_verifier = FRIFoldVerifier::new(fri_params);
65
66	// Inner (unbatch) round: fold every interleaved (M_r ‖ ω_r) ZK codeword at the masking
67	// challenge. It carries no round polynomial.
68	if let Some(gamma) = batch_challenge {
69		fri_fold_verifier.process_round(channel)?;
70		fri_challenges.push(gamma);
71	}
72
73	// The sumcheck rounds: `log_n_oracles` rounds over `Z`, then `n_vars` over `X`.
74	let mut sum = eval_claim;
75	for _ in 0..log_n_oracles + n_vars {
76		let round_proof = RoundProof(RoundCoeffs(channel.recv_many(DEGREE)?));
77		fri_fold_verifier.process_round(channel)?;
78
79		let challenge = channel.sample();
80		sum = round_proof.recover(sum).evaluate(&challenge);
81		fri_challenges.push(challenge);
82	}
83
84	fri_fold_verifier.process_round(channel)?;
85	let round_commitments = fri_fold_verifier.finalize();
86
87	let fri_verifier = FRIQueryVerifier::new_batch(
88		fri_params,
89		codeword_commitments,
90		&round_commitments,
91		&fri_challenges,
92	);
93	let final_fri_value = fri_verifier.verify(channel)?;
94
95	// `Z` challenges come first, then `X` low to high, which is the order of `ρ`.
96	let (zeta, rho) = fri_challenges[n_inner..].split_at(log_n_oracles);
97	let multiplicand = multiplicand_eval(rho, zeta);
98
99	channel.assert_zero(sum - final_fri_value * multiplicand)?;
100
101	Ok(())
102}