From ad33ac86e8a03293daf581f128669f75e50f87be Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Sun, 6 Sep 2026 10:11:03 +0800 Subject: [PATCH 01/18] perf(proof): fixed-base MSMs, shared CRS, and a proof/witness bench Restores the 2026-08 proof-optimization work that lived as uncommitted changes on main (kept alive in stash@{0}, "proof-bench work", 2026-08-25) onto main 945d5bd. Content is unchanged apart from rustfmt on the bench. - banderwagon: hand-rolled window-parallel Pippenger MSM (msm_windowed) that avoids arkworks' thread-pool traffic at SALT's MSM sizes; multi_scalar_mul skips the batch inversion when the bases are already affine; Committer::num_bases. - ipa-multipoint: create_with_precomp computes every IPA L/R point as a fixed-base MSM over the original CRS generators (byte-identical proofs); MultiPoint::open_with_committer and commit_dense route the polynomial commitments through the precomputed tables; queries share their polynomial via Arc; verification sums scalars per commitment run so the final multiexp has one point per node instead of per query; serial fast paths for small powers_of_par and per-query scalar work; equivalence tests against the generic paths. - salt: process-wide DEFAULT_CRS and shared_committer() so proof creation reuses the trie's fixed-base tables instead of rebuilding the CRS per call; shape.rs/subtrie.rs cut per-key allocations, read each bucket's metadata once, and cache default-commitment scalar conversions; new benches/proof.rs covering SaltProof::create/check and Witness::create/verify. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01CGFBjWNK8YFania1Bstst9 --- banderwagon/src/element.rs | 27 ++- banderwagon/src/msm.rs | 157 ++++++++++++- banderwagon/src/salt_committer.rs | 5 + .../benches/benchmarks/multipoint_prove.rs | 2 +- .../benches/benchmarks/multipoint_verify.rs | 2 +- ipa-multipoint/src/ipa.rs | 204 +++++++++++++++- ipa-multipoint/src/math_utils.rs | 7 + ipa-multipoint/src/multiproof.rs | 190 ++++++++++++--- salt/Cargo.toml | 4 + salt/benches/proof.rs | 216 +++++++++++++++++ salt/src/proof/prover.rs | 32 ++- salt/src/proof/shape.rs | 208 ++++++++++------- salt/src/proof/subtrie.rs | 217 ++++++++++-------- salt/src/trie/trie.rs | 15 +- 14 files changed, 1036 insertions(+), 250 deletions(-) create mode 100644 salt/benches/proof.rs diff --git a/banderwagon/src/element.rs b/banderwagon/src/element.rs index af271541..21baa12c 100644 --- a/banderwagon/src/element.rs +++ b/banderwagon/src/element.rs @@ -1,4 +1,4 @@ -use ark_ec::{twisted_edwards::TECurveConfig, CurveGroup, PrimeGroup, ScalarMul, VariableBaseMSM}; +use ark_ec::{twisted_edwards::TECurveConfig, CurveGroup, PrimeGroup, ScalarMul}; use ark_ed_on_bls12_381_bandersnatch::{BandersnatchConfig, EdwardsAffine, EdwardsProjective, Fq}; use ark_ff::{serial_batch_inversion_and_mul, Field, One, Zero}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError}; @@ -413,15 +413,26 @@ fn subgroup_check(point: &EdwardsProjective) -> bool { } pub fn multi_scalar_mul(bases: &[Element], scalars: &[Fr]) -> Element { - let bases_inner: Vec<_> = bases.iter().map(|element| element.0).collect(); - - // XXX: Converting all of these to affine hurts performance - let bases = EdwardsProjective::batch_convert_to_mul_base(&bases_inner); + // The MSM needs affine bases. Points loaded from storage or deserialized + // proofs already have `z = 1`, so skip the batch inversion entirely in + // that common case. + let bases: Vec = if bases.iter().all(|element| element.0.z.is_one()) { + bases + .iter() + .map(|element| EdwardsAffine::new_unchecked(element.0.x, element.0.y)) + .collect() + } else { + let bases_inner: Vec<_> = bases.iter().map(|element| element.0).collect(); + EdwardsProjective::batch_convert_to_mul_base(&bases_inner) + }; - let result = EdwardsProjective::msm(&bases, scalars) - .expect("number of bases should equal number of scalars"); + assert_eq!( + bases.len(), + scalars.len(), + "number of bases should equal number of scalars" + ); - Element(result) + Element(crate::msm::msm_windowed(&bases, scalars)) } /// Multiplies an `Element` by a scalar field element. diff --git a/banderwagon/src/msm.rs b/banderwagon/src/msm.rs index 0013776b..36205666 100644 --- a/banderwagon/src/msm.rs +++ b/banderwagon/src/msm.rs @@ -1,11 +1,127 @@ use crate::Element; use ark_ec::scalar_mul::wnaf::WnafContext; -use ark_ed_on_bls12_381_bandersnatch::{EdwardsProjective, Fr}; -use ark_ff::Zero; +use ark_ed_on_bls12_381_bandersnatch::{EdwardsAffine, EdwardsProjective, Fr}; +use ark_ff::{AdditiveGroup, PrimeField, Zero}; +use core::cmp::Ordering; -use salt_macros::iter; use salt_macros::prelude::*; -use std::vec::Vec; +use salt_macros::{into_iter, iter}; +use std::{vec, vec::Vec}; + +/// Window-parallel Pippenger multi-scalar multiplication over affine bases. +/// +/// Mirrors the signed-digit bucket algorithm of arkworks' `msm_bigint_wnaf`, +/// but converts scalars and builds the digit matrix serially: at the MSM +/// sizes SALT handles (tens to a few thousand points), the parallel +/// conversion collects inside arkworks cost more in thread-pool traffic than +/// the entire digit pass. Windows are processed in parallel only when each +/// carries enough work to pay for the dispatch. +pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsProjective { + let size = bases.len().min(scalars.len()); + let bases = &bases[..size]; + let scalars = &scalars[..size]; + if size == 0 { + return EdwardsProjective::zero(); + } + + let c = if size < 32 { + 3 + } else { + // ln_without_floats: log2(size) * ln(2), plus 2 + (usize::BITS - (size - 1).leading_zeros()) as usize * 69 / 100 + 2 + }; + let num_bits = ::MODULUS_BIT_SIZE as usize; + let digits_count = num_bits.div_ceil(c); + + // Digit matrix: scalar `i` owns scalar_digits[i * digits_count..][..digits_count]. + let mut scalar_digits = vec![0i64; size * digits_count]; + for (scalar, digits) in scalars + .iter() + .zip(scalar_digits.chunks_exact_mut(digits_count)) + { + make_digits(&scalar.into_bigint().0, c, digits); + } + + let window_sum = |window: usize| -> EdwardsProjective { + let mut buckets = vec![EdwardsProjective::zero(); 1 << c]; + for (digits, base) in scalar_digits.chunks_exact(digits_count).zip(bases) { + let digit = digits[window]; + match 0.cmp(&digit) { + Ordering::Less => buckets[(digit - 1) as usize] += base, + Ordering::Greater => buckets[(-digit - 1) as usize] -= base, + Ordering::Equal => (), + } + } + + let mut running_sum = EdwardsProjective::zero(); + let mut res = EdwardsProjective::zero(); + buckets.into_iter().rev().for_each(|b| { + running_sum += &b; + res += &running_sum; + }); + res + }; + + // Below this size the per-window work is too small to farm out. + const MIN_PARALLEL_SIZE: usize = 64; + let window_sums: Vec = if size >= MIN_PARALLEL_SIZE { + into_iter!((0..digits_count)).map(window_sum).collect() + } else { + (0..digits_count).map(window_sum).collect() + }; + + // Traverse windows from high to low, doubling `c` times between windows. + let lowest = *window_sums.first().expect("digits_count >= 1"); + lowest + + window_sums[1..] + .iter() + .rev() + .fold(EdwardsProjective::zero(), |mut total, sum_i| { + total += sum_i; + for _ in 0..c { + total.double_in_place(); + } + total + }) +} + +/// Decomposes a scalar into signed `w`-bit digits in `[-2^(w-1), 2^(w-1)]`, +/// written into `digits`. Identical digit semantics to arkworks' +/// `make_digits` (the final digit absorbs the outstanding carry). +fn make_digits(scalar: &[u64], w: usize, digits: &mut [i64]) { + let radix: u64 = 1 << w; + let window_mask: u64 = radix - 1; + + let mut carry = 0u64; + let digits_count = digits.len(); + for (i, digit_out) in digits.iter_mut().enumerate() { + // Construct a buffer of bits of the scalar, starting at `bit_offset`. + let bit_offset = i * w; + let u64_idx = bit_offset / 64; + let bit_idx = bit_offset % 64; + // Read the bits from the scalar + let bit_buf = if bit_idx < 64 - w || u64_idx == scalar.len() - 1 { + // This window's bits are contained in a single u64, + // or it's the last u64 anyway. + scalar[u64_idx] >> bit_idx + } else { + // Combine the current u64's bits with the bits from the next u64 + (scalar[u64_idx] >> bit_idx) | (scalar[1 + u64_idx] << (64 - bit_idx)) + }; + + // Read the actual coefficient value from the window + let coef = carry + (bit_buf & window_mask); // coef = [0, 2^w) + + // Recenter coefficients from [0,2^w) to [-2^w/2, 2^w/2) + carry = (coef + radix / 2) >> w; + let mut digit = (coef as i64) - (carry << w) as i64; + + if i == digits_count - 1 { + digit += (carry << w) as i64; + } + *digit_out = digit; + } +} #[derive(Clone, Debug)] pub struct MSMPrecompWnaf { @@ -68,6 +184,39 @@ mod tests { use crate::{multi_scalar_mul, Element}; use std::vec; + /// The hand-rolled windowed MSM must agree with arkworks' generic MSM for + /// every size class (serial windows, parallel windows, small-c) and for + /// edge-case scalars. + #[test] + fn msm_windowed_matches_arkworks() { + use ark_ec::{CurveGroup, VariableBaseMSM}; + use ark_ff::UniformRand; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::ChaCha20Rng; + + let mut rng = ChaCha20Rng::from_seed([9u8; 32]); + for size in [1usize, 2, 16, 31, 32, 33, 129, 256, 1000] { + let bases_proj: Vec = (0..size) + .map(|_| EdwardsProjective::rand(&mut rng)) + .collect(); + let bases = EdwardsProjective::normalize_batch(&bases_proj); + let mut scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); + // Exercise edge-case scalars. + scalars[0] = Fr::zero(); + if size > 2 { + scalars[1] = -Fr::from(1u64); + scalars[2] = Fr::from(1u64); + } + + let expected = EdwardsProjective::msm(&bases, &scalars).unwrap(); + let got = msm_windowed(&bases, &scalars); + assert_eq!(got, expected, "size {size}"); + } + + // Empty input yields the identity. + assert!(msm_windowed(&[], &[]).is_zero()); + } + #[test] fn correctness_smoke_test() { let mut crs = Vec::with_capacity(256); diff --git a/banderwagon/src/salt_committer.rs b/banderwagon/src/salt_committer.rs index 41bfa2ec..bafa1030 100644 --- a/banderwagon/src/salt_committer.rs +++ b/banderwagon/src/salt_committer.rs @@ -187,6 +187,11 @@ impl Committer { } } + /// Returns the number of base points covered by the precomputed tables. + pub fn num_bases(&self) -> usize { + self.tables.len() + } + /// Multiplies a precomputed base point by a scalar using windowed NAF. /// /// This x86_64-optimized version uses CPU prefetching instructions to diff --git a/ipa-multipoint/benches/benchmarks/multipoint_prove.rs b/ipa-multipoint/benches/benchmarks/multipoint_prove.rs index 5c83e643..bc5a232f 100644 --- a/ipa-multipoint/benches/benchmarks/multipoint_prove.rs +++ b/ipa-multipoint/benches/benchmarks/multipoint_prove.rs @@ -35,7 +35,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { let prover_query = ProverQuery { commitment: poly_comm, - poly, + poly: poly.into(), point, result: y_i, }; diff --git a/ipa-multipoint/benches/benchmarks/multipoint_verify.rs b/ipa-multipoint/benches/benchmarks/multipoint_verify.rs index a2a89a45..06dcc5e3 100644 --- a/ipa-multipoint/benches/benchmarks/multipoint_verify.rs +++ b/ipa-multipoint/benches/benchmarks/multipoint_verify.rs @@ -36,7 +36,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { let prover_query = ProverQuery { commitment: poly_comm, - poly, + poly: poly.into(), point, result: y_i, }; diff --git a/ipa-multipoint/src/ipa.rs b/ipa-multipoint/src/ipa.rs index bf8ed379..1027b8d1 100644 --- a/ipa-multipoint/src/ipa.rs +++ b/ipa-multipoint/src/ipa.rs @@ -4,13 +4,13 @@ use crate::math_utils::inner_product; use crate::transcript::{Transcript, TranscriptProtocol}; use crate::{IOResult, SerdeError}; -use banderwagon::{multi_scalar_mul, trait_defs::*, Element, Fr}; +use banderwagon::{multi_scalar_mul, salt_committer::Committer, trait_defs::*, Element, Fr}; use core::iter; use itertools::Itertools; use salt_macros::prelude::*; use salt_macros::{chunks, chunks_mut, join, num_threads}; -use std::vec::Vec; +use std::{vec, vec::Vec}; #[derive(Debug, Clone, PartialEq)] pub struct IPAProof { @@ -177,6 +177,148 @@ pub fn create( } } +/// Creates the same IPA proof as [`create`], but computes every `L`/`R` point +/// as a fixed-base multi-scalar multiplication over the *original* CRS +/// generators using precomputed wNAF tables, instead of folding the generator +/// vector with one variable-base scalar multiplication per element. +/// +/// # How it works +/// +/// After `k` folding rounds, each folded generator is a known linear +/// combination of the original generators: +/// +/// ```text +/// G_k[i] = Σ_m coeff[m] · G[m] over m with m mod len == i +/// ``` +/// +/// where `coeff[m]` is the product of the inverse challenges of every past +/// round in which index `m` sat in the right half. Substituting this into +/// `L_k = + z_L·Q` (and symmetrically for `R_k`) turns each round's +/// two MSMs into MSMs over the fixed generator set, where each original index +/// contributes to exactly one of `L`/`R` per round. The scalars fold exactly +/// as before, so the resulting proof is byte-identical to [`create`]'s. +/// +/// # Arguments +/// +/// * `precomp` - Fixed-base tables for the CRS `G` vector (must cover at least +/// `a_vec.len()` bases, in CRS order) +/// * `q` - The CRS blinding generator `Q` +pub fn create_with_precomp( + transcript: &mut Transcript, + precomp: &Committer, + q: Element, + mut a_vec: Vec, + a_comm: Element, + mut b_vec: Vec, + // This is the z in f(z) + input_point: Fr, +) -> IPAProof { + transcript.domain_sep(b"ipa"); + + let mut a = &mut a_vec[..]; + let mut b = &mut b_vec[..]; + + let n = a.len(); + assert_eq!(b.len(), n); + assert!(n.is_power_of_two()); + assert!(precomp.num_bases() >= n); + + let output_point = inner_product(a, b); + transcript.append_point(b"C", &a_comm); + transcript.append_scalar(b"input point", &input_point); + transcript.append_scalar(b"output point", &output_point); + + let w = transcript.challenge_scalar(b"w"); + let Q = q * w; + + let num_rounds = log2(n); + + let mut L_vec: Vec = Vec::with_capacity(num_rounds as usize); + let mut R_vec: Vec = Vec::with_capacity(num_rounds as usize); + + // coeff[m] tracks the coefficient of original generator G[m] inside the + // implicitly folded generator vector. + let mut coeff = vec![Fr::one(); n]; + let mut len = n; + + for _k in 0..num_rounds { + let half = len / 2; + let (a_L, a_R) = halve(a); + let (b_L, b_R) = halve(b); + + // Express this round's L/R in terms of the original generators. Every + // original index lands in exactly one of the two point sets. + let mut l_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); + let mut r_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); + for (m, c) in coeff.iter().enumerate() { + let i = m % len; + if i < half { + l_terms.push((m, a_R[i] * c)); + } else { + r_terms.push((m, a_L[i - half] * c)); + } + } + + let left_compute = || -> Element { + let z_L = inner_product(a_R, b_L); + fixed_base_msm(precomp, &l_terms) + Q * z_L + }; + let right_compute = || -> Element { + let z_R = inner_product(a_L, b_R); + fixed_base_msm(precomp, &r_terms) + Q * z_R + }; + + let (L, R) = join!(left_compute, right_compute); + L_vec.push(L); + R_vec.push(R); + + transcript.append_point(b"L", &L); + transcript.append_point(b"R", &R); + + let x = transcript.challenge_scalar(b"x"); + let x_inv = x.inverse().unwrap(); + + for i in 0..a_L.len() { + a_L[i] += x * a_R[i]; + b_L[i] += x_inv * b_R[i]; + } + // Indices in the right half of the current fold pick up the inverse + // challenge, mirroring `G_L += G_R * x_inv` on the original bases. + for (m, c) in coeff.iter_mut().enumerate() { + if m % len >= half { + *c *= x_inv; + } + } + + a = a_L; + b = b_L; + len = half; + } + + IPAProof { + L_vec, + R_vec, + a: a[0], + } +} + +/// Sums `scalar · G[index]` over the given terms using precomputed wNAF +/// tables, splitting the terms across threads. +pub(crate) fn fixed_base_msm(precomp: &Committer, terms: &[(usize, Fr)]) -> Element { + let chunk_size = terms.len().div_ceil(num_threads!()).max(1); + chunks!(terms, chunk_size) + .map(|chunk| { + let mut acc = Element::zero(); + for (index, scalar) in chunk { + if !scalar.is_zero() { + acc += precomp.mul_index(scalar, *index); + } + } + acc + }) + .sum() +} + // Halves the slice that is passed in // Assumes that the slice has an even length fn halve(scalars: &mut [T]) -> (&mut [T], &mut [T]) { @@ -217,9 +359,10 @@ impl IPAProof { let w = transcript.challenge_scalar(b"w"); // Generate all of the necessary challenges and their inverses + // (log n elements — invert serially) let challenges = generate_challenges(self, transcript); let mut challenges_inv = challenges.clone(); - batch_inversion(&mut challenges_inv); + serial_batch_inversion_and_mul(&mut challenges_inv, &Fr::one()); // Generate the coefficients for the `G` vector and the `b` vector // {-g_i}{-b_i} @@ -277,12 +420,10 @@ pub fn slow_vartime_multiscalar_mul<'a>( } pub fn multi_scalar_mul_par(bases: &[Element], scalars: &[Fr]) -> Element { - let chunk_size = bases.len().div_ceil(num_threads!()); - - chunks!(bases, chunk_size) - .zip(chunks!(scalars, chunk_size)) - .map(|(bases, scalars)| multi_scalar_mul(bases, scalars)) - .sum() + // `multi_scalar_mul` parallelizes internally across Pippenger windows; + // chunking the input here would multiply the window/doubling work per + // chunk and nest thread-pool dispatches. + multi_scalar_mul(bases, scalars) } fn generate_challenges(proof: &IPAProof, transcript: &mut Transcript) -> Vec { @@ -307,6 +448,51 @@ mod tests { use ark_std::{rand::SeedableRng, UniformRand}; use rand_chacha::ChaCha20Rng; + /// `create_with_precomp` must produce exactly the proof `create` does — + /// same transcript interaction, same L/R points, same final scalar. + #[test] + fn create_with_precomp_matches_create() { + let n = 256; + let crs = CRS::new(n, b"random seed"); + let committer = Committer::new(&crs.G, 6); + + let mut rng = ChaCha20Rng::from_seed([7u8; 32]); + let a: Vec = (0..n).map(|_| Fr::rand(&mut rng)).collect(); + let input_point = Fr::rand(&mut rng); + let b = powers_of(input_point, n); + let a_comm = slow_vartime_multiscalar_mul(a.iter(), crs.G.iter()); + + let folded = create( + &mut Transcript::new(b"ip_no_zk"), + crs.clone(), + a.clone(), + a_comm, + b.clone(), + input_point, + ); + let precomputed = create_with_precomp( + &mut Transcript::new(b"ip_no_zk"), + &committer, + crs.Q, + a.clone(), + a_comm, + b.clone(), + input_point, + ); + + assert_eq!(folded.to_bytes().unwrap(), precomputed.to_bytes().unwrap()); + + let output_point = inner_product(&a, &b); + assert!(precomputed.verify_multiexp( + &mut Transcript::new(b"ip_no_zk"), + &crs, + b, + a_comm, + input_point, + output_point + )); + } + #[test] fn test_create_IPAProof_proof() { let n = 8; diff --git a/ipa-multipoint/src/math_utils.rs b/ipa-multipoint/src/math_utils.rs index 69c8b82d..f56f47ef 100644 --- a/ipa-multipoint/src/math_utils.rs +++ b/ipa-multipoint/src/math_utils.rs @@ -22,6 +22,13 @@ pub fn powers_of(point: Fr, n: usize) -> Vec { #[inline(always)] pub fn powers_of_par(point: Fr, n: usize) -> Vec { + // A field multiplication is ~20ns, so even tens of thousands of powers + // compute faster serially than the cost of waking the thread pool. + const MIN_PARALLEL_LEN: usize = 1 << 16; + if n < MIN_PARALLEL_LEN { + return powers_of(point, n); + } + let mut powers = vec![Fr::zero(); n]; // Compute base powers for each chunk diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index b0d6c0aa..03f2ec19 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -2,28 +2,31 @@ #![allow(non_snake_case)] use crate::crs::CRS; -use crate::ipa::{multi_scalar_mul_par, IPAProof}; +use crate::ipa::{multi_scalar_mul_par, slow_vartime_multiscalar_mul, IPAProof}; use crate::lagrange_basis::{LagrangeBasis, PrecomputedWeights}; use crate::math_utils::powers_of_par; use crate::transcript::Transcript; use crate::transcript::TranscriptProtocol; -use banderwagon::{trait_defs::*, Element, Fr}; +use banderwagon::{salt_committer::Committer, trait_defs::*, Element, Fr}; use hashbrown::HashMap; use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; use salt_macros::prelude::*; use salt_macros::{chunks, chunks_mut, into_iter, iter, num_threads, reduce}; -use std::vec::Vec; +use std::sync::Arc; +use std::{vec, vec::Vec}; pub struct MultiPoint; #[derive(Clone, Debug)] pub struct ProverQuery { pub commitment: Element, - pub poly: LagrangeBasis, // TODO: Make this a reference so that upstream libraries do not need to clone + /// Shared so that opening one polynomial at several points does not clone + /// its 256 coefficients per point. + pub poly: Arc, // Given a function f, we use z_i to denote the input point and y_i to denote the output, ie f(z_i) = y_i pub point: usize, pub result: Fr, @@ -73,6 +76,34 @@ impl MultiPoint { precomp: &PrecomputedWeights, transcript: &mut Transcript, queries: Vec, + ) -> MultiPointProof { + Self::open_inner(crs, None, precomp, transcript, queries) + } + + /// Same proof as [`MultiPoint::open`] (byte-identical output), but every + /// multi-scalar multiplication over the CRS generators — the two + /// polynomial commitments and all IPA `L`/`R` points — is computed with + /// the given precomputed fixed-base tables. This removes both the + /// per-round generator folding and the small variable-base MSMs, which + /// dominate proving time. + /// + /// `committer` must hold tables for the CRS `G` vector in order. + pub fn open_with_committer( + crs: CRS, + committer: &Committer, + precomp: &PrecomputedWeights, + transcript: &mut Transcript, + queries: Vec, + ) -> MultiPointProof { + Self::open_inner(crs, Some(committer), precomp, transcript, queries) + } + + fn open_inner( + crs: CRS, + committer: Option<&Committer>, + precomp: &PrecomputedWeights, + transcript: &mut Transcript, + queries: Vec, ) -> MultiPointProof { transcript.domain_sep(b"multiproof"); @@ -90,19 +121,30 @@ impl MultiPoint { let chunk_size = grouped_queries.len().div_ceil(num_threads!()); - // aggregate all of the queries evaluated at the same point + // aggregate all of the queries evaluated at the same point; + // accumulate scaled polynomials in place instead of cloning each + // query's polynomial (one 8 KB allocation plus an extra add pass per + // query otherwise) let aggregated_queries: Vec<_> = chunks!(grouped_queries, chunk_size) .flat_map(|chunk| { chunk .iter() .map(|(point, queries_challenges)| { - let aggregated_polynomial = queries_challenges - .iter() - .map(|(query, challenge)| query.poly.clone() * *challenge) - .reduce(|acc, x| acc + x) - .expect("Failed to aggregate polynomial"); - - (*point, aggregated_polynomial) + let domain = queries_challenges + .first() + .expect("point group cannot be empty") + .0 + .poly + .values() + .len(); + let mut aggregated = vec![Fr::zero(); domain]; + for (query, challenge) in queries_challenges.iter() { + for (acc, coeff) in aggregated.iter_mut().zip(query.poly.values()) { + *acc += **challenge * coeff; + } + } + + (*point, LagrangeBasis::new(aggregated)) }) .collect::>() }) @@ -117,7 +159,7 @@ impl MultiPoint { |a, b| a + b ); - let g_x_comm = crs.commit_lagrange_poly(&g_x); + let g_x_comm = commit_dense(&crs, committer, g_x.values()); transcript.append_point(b"D", &g_x_comm); @@ -148,7 +190,7 @@ impl MultiPoint { |a, b| a + b ); - let g1_comm = crs.commit_lagrange_poly(&g1_x); + let g1_comm = commit_dense(&crs, committer, g1_x.values()); transcript.append_point(b"E", &g1_comm); @@ -158,7 +200,14 @@ impl MultiPoint { let g_3_x_comm = g1_comm - g_x_comm; // 4. Compute the IPA for g_3 - let g_3_ipa = open_point_outside_of_domain(crs, precomp, transcript, g_3_x, g_3_x_comm, t); + let g_3_ipa = match committer { + Some(committer) => { + let a = g_3_x.values().to_vec(); + let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, t); + crate::ipa::create_with_precomp(transcript, committer, crs.Q, a, g_3_x_comm, b, t) + } + None => open_point_outside_of_domain(crs, precomp, transcript, g_3_x, g_3_x_comm, t), + }; MultiPointProof { open_proof: g_3_ipa, @@ -167,6 +216,18 @@ impl MultiPoint { } } +/// Commits to a dense polynomial in Lagrange basis: with precomputed tables +/// when available, otherwise via the generic variable-base MSM. +fn commit_dense(crs: &CRS, committer: Option<&Committer>, values: &[Fr]) -> Element { + match committer { + Some(committer) => { + let terms: Vec<(usize, Fr)> = values.iter().copied().enumerate().collect(); + crate::ipa::fixed_base_msm(committer, &terms) + } + None => slow_vartime_multiscalar_mul(values.iter(), crs.G.iter()), + } +} + /// This trait is used to abstract over the query data that is used to record the transcript /// in the prover and verifier. /// @@ -289,24 +350,43 @@ impl MultiPointProof { // 3. Compute g_2(t) // - let mut g2_den: Vec<_> = iter!(queries).map(|query| t - query.point).collect(); + // A handful of field operations per query: serial execution beats + // waking the thread pool even for tens of thousands of queries. + let mut g2_den: Vec<_> = queries.iter().map(|query| t - query.point).collect(); - batch_inversion(&mut g2_den); + serial_batch_inversion_and_mul(&mut g2_den, &Fr::one()); - let helper_scalars: Vec<_> = into_iter!(powers_of_r) + let helper_scalars: Vec<_> = powers_of_r + .into_iter() .zip(g2_den) .map(|(r_i, den_inv)| den_inv * r_i) .collect(); - let g2_t: Fr = iter!(helper_scalars) - .zip(iter!(queries)) + let g2_t: Fr = helper_scalars + .iter() + .zip(queries.iter()) .map(|(r_i_den_inv, query)| *r_i_den_inv * query.result) .sum(); //4. Compute [g_1(X)] = E - let comms: Vec<_> = iter!(queries).map(|query| query.commitment).collect(); + // + // Queries arrive grouped per trie node, so runs of consecutive queries + // share one commitment. Summing the scalars of each run first shrinks + // the multiexp from one point per query to one point per node, which + // is where verification time is spent for large witnesses. + let mut comms: Vec = Vec::with_capacity(queries.len()); + let mut comm_scalars: Vec = Vec::with_capacity(queries.len()); + for (query, scalar) in queries.iter().zip(helper_scalars.iter()) { + match (comms.last(), comm_scalars.last_mut()) { + (Some(last), Some(acc)) if *last == query.commitment => *acc += scalar, + _ => { + comms.push(query.commitment); + comm_scalars.push(*scalar); + } + } + } - let g1_comm = multi_scalar_mul_par(&comms, &helper_scalars); + let g1_comm = multi_scalar_mul_par(&comms, &comm_scalars); transcript.append_point(b"E", &g1_comm); @@ -365,7 +445,7 @@ mod tests { let prover_query = ProverQuery { commitment: poly_comm, - poly, + poly: poly.into(), point, result: y_i, }; @@ -407,13 +487,13 @@ mod tests { let prover_query_i = ProverQuery { commitment: poly_comm, - poly: poly.clone(), + poly: poly.clone().into(), point: z_i, result: y_i, }; let prover_query_j = ProverQuery { commitment: poly_comm, - poly, + poly: poly.into(), point: x_j, result: y_j, }; @@ -535,13 +615,13 @@ mod tests { let prover_query_a = ProverQuery { commitment: poly_comm_a, - poly: polynomial_a, + poly: polynomial_a.into(), point: point_a, result: y_a, }; let prover_query_b = ProverQuery { commitment: poly_comm_b, - poly: polynomial_b, + poly: polynomial_b.into(), point: point_b, result: y_b, }; @@ -583,6 +663,60 @@ mod tests { assert_eq!(got, expected) } + /// The committer-accelerated prover must emit byte-identical proofs to + /// the generic prover: it computes the same group elements, only through + /// fixed-base precomputation instead of generator folding. + #[test] + fn open_with_committer_matches_open() { + let n = 256; + let crs = CRS::new(n, b"random seed"); + let committer = Committer::new(&crs.G, 6); + let precomp = PrecomputedWeights::new(n); + + let mut rng = test_rng(); + let queries: Vec = (0..7) + .map(|i| { + let poly = LagrangeBasis::new((0..n).map(|_| Fr::rand(&mut rng)).collect()); + let commitment = crs.commit_lagrange_poly(&poly); + let point = (i * 41) % n; + ProverQuery { + commitment, + result: poly.evaluate_in_domain(point), + poly: poly.into(), + point, + } + }) + .collect(); + + let generic = MultiPoint::open( + crs.clone(), + &precomp, + &mut Transcript::new(b"st"), + queries.clone(), + ); + let accelerated = MultiPoint::open_with_committer( + crs.clone(), + &committer, + &precomp, + &mut Transcript::new(b"st"), + queries.clone(), + ); + + assert_eq!( + generic.to_bytes().unwrap(), + accelerated.to_bytes().unwrap(), + "committer-accelerated proof must be byte-identical" + ); + + let verifier_queries: Vec = queries.into_iter().map(Into::into).collect(); + assert!(accelerated.check( + &crs, + &precomp, + &verifier_queries, + &mut Transcript::new(b"st") + )); + } + #[test] fn record_query_transcript_consistency() { let (prover_queries, verifier_queries) = generate_test_queries(100); @@ -623,7 +757,7 @@ mod tests { ProverQuery { commitment: random_element, - poly: poly.clone(), + poly: poly.clone().into(), point: i % 7, result: random_scalar, } diff --git a/salt/Cargo.toml b/salt/Cargo.toml index 1875c862..e4facb4a 100644 --- a/salt/Cargo.toml +++ b/salt/Cargo.toml @@ -46,6 +46,10 @@ enable-hugepages = ["std", "banderwagon/enable-hugepages"] name = "salt_trie" harness = false +[[bench]] +name = "proof" +harness = false + [[bench]] name = "parents_commitments_deser" harness = false diff --git a/salt/benches/proof.rs b/salt/benches/proof.rs new file mode 100644 index 00000000..4a409532 --- /dev/null +++ b/salt/benches/proof.rs @@ -0,0 +1,216 @@ +//! SALT Proof & Witness Performance Benchmarks +//! +//! Measures the four hot paths of the proof layer: +//! +//! - `SaltProof::create` — proof generation for a set of salt keys +//! - `SaltProof::check` — proof verification against the state root +//! - `Witness::create` — block witness generation (lookups + updates) +//! - `Witness::verify` — block witness verification (stateless validator) +//! +//! ```bash +//! cargo bench --package salt --bench proof +//! ``` + +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use rand::{rngs::StdRng, Rng, SeedableRng}; +use salt::{ + traits::StateReader, types::*, EphemeralSaltState, MemStore, SaltProof, StateRoot, Witness, +}; +use std::collections::BTreeMap; +use std::hint::black_box; +use std::sync::OnceLock; +use std::time::Duration; + +/// Number of key-value pairs pre-inserted into the store. +const INITIAL_KEYS: usize = 50_000; + +struct Setup { + store: MemStore, + /// Plain keys that exist in the store. + plain_keys: Vec>, + /// Salt keys of all existing entries. + salt_keys: Vec, + /// State root after setup. + root: ScalarBytes, +} + +fn mock_data(rng: &mut StdRng, len: usize) -> Vec { + (0..len).map(|_| rng.random()).collect() +} + +fn setup() -> &'static Setup { + static SETUP: OnceLock = OnceLock::new(); + SETUP.get_or_init(|| { + let mut rng = StdRng::seed_from_u64(42); + + let kvs: hashbrown::HashMap, Option>> = (0..INITIAL_KEYS) + .map(|_| (mock_data(&mut rng, 32), Some(mock_data(&mut rng, 32)))) + .collect(); + + let store = MemStore::new(); + let updates = EphemeralSaltState::new(&store).update_fin(&kvs).unwrap(); + store.update_state(updates.clone()); + + let (root, trie_updates) = StateRoot::new(&store).update_fin(&updates).unwrap(); + store.update_trie(trie_updates); + + let plain_keys: Vec> = kvs.keys().cloned().collect(); + let salt_keys: Vec = updates + .data + .keys() + .copied() + .filter(|k| !k.is_in_meta_bucket()) + .collect(); + + Setup { + store, + plain_keys, + salt_keys, + root, + } + }) +} + +/// Selects `n` existing salt keys plus a few non-existent ones (sorted, deduped). +fn proof_keys(s: &Setup, n: usize) -> Vec { + let mut keys: Vec = s + .salt_keys + .iter() + .step_by(s.salt_keys.len() / n) + .copied() + .collect(); + keys.truncate(n); + // A few keys proven absent (bucket untouched by setup with default capacity). + keys.push(SaltKey::from((16_777_215, 1))); + keys.push(SaltKey::from((16_777_215, 254))); + keys.sort_unstable(); + keys.dedup(); + keys +} + +/// Builds the kv data map the verifier is given for `keys`. +fn proof_data(s: &Setup, keys: &[SaltKey]) -> BTreeMap> { + keys.iter() + .map(|&k| { + let v = if k.is_in_meta_bucket() { + Some( + s.store + .metadata(bucket_id_from_metadata_key(k)) + .unwrap() + .into(), + ) + } else { + s.store.value(k).unwrap() + }; + (k, v) + }) + .collect() +} + +fn bench_salt_proof(c: &mut Criterion) { + let s = setup(); + + let mut group = c.benchmark_group("salt_proof/create"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(10)); + for n in [16usize, 256, 2048] { + let keys = proof_keys(s, n); + group.bench_with_input(BenchmarkId::from_parameter(n), &keys, |b, keys| { + b.iter(|| black_box(SaltProof::create(keys.iter().copied(), &s.store).unwrap())); + }); + } + group.finish(); + + let mut group = c.benchmark_group("salt_proof/check"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(10)); + for n in [16usize, 256, 2048] { + let keys = proof_keys(s, n); + let proof = SaltProof::create(keys.iter().copied(), &s.store).unwrap(); + let data = proof_data(s, &keys); + group.bench_with_input( + BenchmarkId::from_parameter(n), + &(proof, data), + |b, (proof, data)| { + b.iter(|| proof.check(black_box(data), s.root).unwrap()); + }, + ); + } + group.finish(); +} + +/// Plain-key updates: key -> new value (None = delete). +type PlainUpdates = BTreeMap, Option>>; + +/// A block-like workload: half the size is lookups (2/3 hits, 1/3 misses), +/// half is updates (1/2 overwrites of existing keys, 1/2 fresh inserts). +fn witness_workload(s: &Setup, n: usize, rng: &mut StdRng) -> (Vec>, PlainUpdates) { + let half = n / 2; + let hits = half * 2 / 3; + let step = s.plain_keys.len() / half.max(1); + + let mut lookups: Vec> = s + .plain_keys + .iter() + .step_by(step.max(1)) + .take(hits) + .cloned() + .collect(); + for _ in 0..(half - hits) { + lookups.push(mock_data(rng, 32)); // misses + } + + let mut updates = BTreeMap::new(); + for key in s + .plain_keys + .iter() + .skip(1) + .step_by(step.max(1)) + .take(half / 2) + { + updates.insert(key.clone(), Some(mock_data(rng, 32))); // overwrite + } + while updates.len() < half { + updates.insert(mock_data(rng, 32), Some(mock_data(rng, 32))); // insert + } + + (lookups, updates) +} + +fn bench_witness(c: &mut Criterion) { + let s = setup(); + let mut rng = StdRng::seed_from_u64(7); + + let mut group = c.benchmark_group("witness/create"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(15)); + for n in [256usize, 1024] { + let (lookups, updates) = witness_workload(s, n, &mut rng); + group.bench_with_input( + BenchmarkId::from_parameter(n), + &(lookups, updates), + |b, (lookups, updates)| { + b.iter(|| { + black_box(Witness::create([], lookups.iter(), updates, &s.store).unwrap()) + }); + }, + ); + } + group.finish(); + + let mut group = c.benchmark_group("witness/verify"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(15)); + for n in [256usize, 1024] { + let (lookups, updates) = witness_workload(s, n, &mut rng); + let witness = Witness::create([], lookups.iter(), &updates, &s.store).unwrap(); + assert_eq!(witness.state_root().unwrap(), s.root); + group.bench_with_input(BenchmarkId::from_parameter(n), &witness, |b, witness| { + b.iter(|| witness.verify().unwrap()); + }); + } + group.finish(); +} + +criterion_group!(benches, bench_salt_proof, bench_witness); +criterion_main!(benches); diff --git a/salt/src/proof/prover.rs b/salt/src/proof/prover.rs index 14b92db8..55bbd500 100644 --- a/salt/src/proof/prover.rs +++ b/salt/src/proof/prover.rs @@ -42,6 +42,12 @@ type FxHashMap = HashMap; pub static PRECOMPUTED_WEIGHTS: Lazy = Lazy::new(|| PrecomputedWeights::new(DOMAIN_SIZE)); +/// Shared default CRS. Constructing `CRS::default()` decompresses 257 points, +/// each costing a modular square root plus a subgroup check (several +/// milliseconds total), so it is done once per process and reused by every +/// proof creation and verification. +pub static DEFAULT_CRS: Lazy = Lazy::new(CRS::default); + /// Serde wrapper for banderwagon `Element` with validation and compression. /// /// This type ensures security by validating elements during deserialization @@ -230,11 +236,18 @@ impl SaltProof { let (prover_queries, parents_commitments, levels) = create_sub_trie(store, &keys)?; - let crs = CRS::default(); - let mut transcript = Transcript::new(b"st"); - let proof = MultiPoint::open(crs, &PRECOMPUTED_WEIGHTS, &mut transcript, prover_queries); + // Reuse the shared CRS (deriving it decompresses 257 points) and the + // trie's fixed-base tables for all MSMs over the CRS generators. + let committer = crate::trie::trie::shared_committer(); + let proof = MultiPoint::open_with_committer( + DEFAULT_CRS.clone(), + &committer, + &PRECOMPUTED_WEIGHTS, + &mut transcript, + prover_queries, + ); Ok(SaltProof { parents_commitments, @@ -267,14 +280,13 @@ impl SaltProof { let mut transcript = Transcript::new(b"st"); - let crs = CRS::default(); - // call MultiPointProof::check to verify the proof - if self - .proof - .0 - .check(&crs, &PRECOMPUTED_WEIGHTS, &queries, &mut transcript) - { + if self.proof.0.check( + &DEFAULT_CRS, + &PRECOMPUTED_WEIGHTS, + &queries, + &mut transcript, + ) { Ok(()) } else { Err(ProofError::MultiPointProofFailed) diff --git a/salt/src/proof/shape.rs b/salt/src/proof/shape.rs index e38e051b..58be3648 100644 --- a/salt/src/proof/shape.rs +++ b/salt/src/proof/shape.rs @@ -11,8 +11,9 @@ //! - **Bucket Subtrees**: Dynamic trees within buckets that can expand from 1-5 levels use salt_macros::prelude::*; -use salt_macros::{iter, reduce}; +use salt_macros::{chunks, num_threads}; use std::collections::{BTreeMap, BTreeSet}; +use std::vec::Vec; use hashbrown::HashMap; use rustc_hash::FxBuildHasher; @@ -61,101 +62,130 @@ pub(crate) fn parents_and_points( BTreeMap>, BTreeMap>, ) { - reduce!( - iter!(salt_keys).map(|salt_key| { + // One accumulator pair per thread chunk, merged once at the end. (A + // per-key map-reduce would allocate and merge two BTreeMaps per key.) + let chunk_size = salt_keys.len().div_ceil(num_threads!()).max(1); + let partials: Vec<_> = chunks!(salt_keys, chunk_size) + .map(|chunk| { let mut internal_nodes: BTreeMap> = BTreeMap::new(); let mut slot_position_nodes: BTreeMap> = BTreeMap::new(); - let bucket_id = salt_key.bucket_id(); - let level = levels[&bucket_id]; - - // ============================================================================ - // Phase 1: Main Trie Traversal - // ============================================================================ - // Walk from the bucket root up to the main trie root (node 0), recording - // each parent-child relationship. This captures the path through the fixed - // 4-level main trie structure that leads to this bucket. - let mut node = bucket_root_node_id(salt_key.bucket_id()); - while node != 0 { - let parent_node = get_parent_node(&node); - // Record that this parent needs to prove the child at this position - internal_nodes - .entry(parent_node) - .or_default() - .insert(vc_position_in_parent(&node)); - - node = parent_node; + for salt_key in chunk { + record_key_paths( + salt_key, + levels, + &mut internal_nodes, + &mut slot_position_nodes, + ); } + (internal_nodes, slot_position_nodes) + }) + .collect(); + + let mut merged = (BTreeMap::new(), BTreeMap::new()); + for (internal_map, slot_map) in partials { + for (node_id, positions) in internal_map { + merged + .0 + .entry(node_id) + .or_insert_with(BTreeSet::new) + .extend(positions); + } + for (node_id, positions) in slot_map { + merged + .1 + .entry(node_id) + .or_insert_with(BTreeSet::new) + .extend(positions); + } + } + merged +} - // ============================================================================ - // Phase 2: Bucket Tree Traversal - // ============================================================================ - // For expanded buckets (>256 slots), traverse the internal bucket tree - // structure from the key's leaf segment up toward the bucket root. - // This only applies to buckets with level > 2 (multi-level bucket trees). - let mut node = subtree_leaf_for_key(salt_key); - - let mut count = level; - while count > 2 { - let parent_node = get_parent_node(&node); - // Record parent-child relationships within the bucket subtree - internal_nodes - .entry(parent_node) - .or_default() - .insert(vc_position_in_parent(&node)); - - node = parent_node; - count -= 1; - } +/// Records the authentication-path nodes and slot positions for one key into +/// the given accumulators. See [`parents_and_points`] for the algorithm. +fn record_key_paths( + salt_key: &SaltKey, + levels: &FxHashMap, + internal_nodes: &mut BTreeMap>, + slot_position_nodes: &mut BTreeMap>, +) { + let bucket_id = salt_key.bucket_id(); + let level = levels[&bucket_id]; + + // ============================================================================ + // Phase 1: Main Trie Traversal + // ============================================================================ + // Walk from the bucket root up to the main trie root (node 0), recording + // each parent-child relationship. This captures the path through the fixed + // 4-level main trie structure that leads to this bucket. + let mut node = bucket_root_node_id(salt_key.bucket_id()); + while node != 0 { + let parent_node = get_parent_node(&node); + // Record that this parent needs to prove the child at this position + internal_nodes + .entry(parent_node) + .or_default() + .insert(vc_position_in_parent(&node)); + + node = parent_node; + } - // ============================================================================ - // Phase 3: Bridge Connection (Expanded buckets) - // ============================================================================ - // For bucket trees with exactly 2 levels, create the bridge connection - // between the bucket subtree and the main trie. The encode_parent function - // embeds level information in the node ID to distinguish different tree levels. - if count == 2 { - let main_trie_node = bucket_root_node_id(salt_key.bucket_id()); - // Use encoded parent to bridge bucket tree to main trie - internal_nodes - .entry(encode_parent(main_trie_node, level)) - .or_default() - .insert(vc_position_in_parent(&node)); - } + // ============================================================================ + // Phase 2: Bucket Tree Traversal + // ============================================================================ + // For expanded buckets (>256 slots), traverse the internal bucket tree + // structure from the key's leaf segment up toward the bucket root. + // This only applies to buckets with level > 2 (multi-level bucket trees). + let mut node = subtree_leaf_for_key(salt_key); + + let mut count = level; + while count > 2 { + let parent_node = get_parent_node(&node); + // Record parent-child relationships within the bucket subtree + internal_nodes + .entry(parent_node) + .or_default() + .insert(vc_position_in_parent(&node)); + + node = parent_node; + count -= 1; + } - // ============================================================================ - // Phase 4: Key-Value Slot Position - // ============================================================================ - // Record the direct parent of the key-value pair itself. This determines - // which node contains the actual data slot and what position within that - // node's 256-slot array the key occupies. - let node = if level == 1 { - // Level 1: Key stored directly in bucket root (single 256-slot segment) - bucket_root_node_id(salt_key.bucket_id()) - } else { - // Level 2+: Key stored in a leaf segment of the bucket tree - subtree_leaf_for_key(salt_key) - }; - - // Record which slot position within the segment contains this key - // Use lowest 8 bits of slot_id as position within 256-slot segment - slot_position_nodes - .entry(node) - .or_default() - .insert((salt_key.slot_id() & 0xFF) as usize); + // ============================================================================ + // Phase 3: Bridge Connection (Expanded buckets) + // ============================================================================ + // For bucket trees with exactly 2 levels, create the bridge connection + // between the bucket subtree and the main trie. The encode_parent function + // embeds level information in the node ID to distinguish different tree levels. + if count == 2 { + let main_trie_node = bucket_root_node_id(salt_key.bucket_id()); + // Use encoded parent to bridge bucket tree to main trie + internal_nodes + .entry(encode_parent(main_trie_node, level)) + .or_default() + .insert(vc_position_in_parent(&node)); + } - (internal_nodes, slot_position_nodes) - }), - || (BTreeMap::new(), BTreeMap::new()), - |mut acc, (internal_map, slot_map)| { - for (node_id, positions) in internal_map { - acc.0.entry(node_id).or_default().extend(positions); - } - for (node_id, positions) in slot_map { - acc.1.entry(node_id).or_default().extend(positions); - } - acc - } - ) + // ============================================================================ + // Phase 4: Key-Value Slot Position + // ============================================================================ + // Record the direct parent of the key-value pair itself. This determines + // which node contains the actual data slot and what position within that + // node's 256-slot array the key occupies. + let node = if level == 1 { + // Level 1: Key stored directly in bucket root (single 256-slot segment) + bucket_root_node_id(salt_key.bucket_id()) + } else { + // Level 2+: Key stored in a leaf segment of the bucket tree + subtree_leaf_for_key(salt_key) + }; + + // Record which slot position within the segment contains this key + // Use lowest 8 bits of slot_id as position within 256-slot segment + slot_position_nodes + .entry(node) + .or_default() + .insert((salt_key.slot_id() & 0xFF) as usize); } /// Encodes bucket tree level information into a main trie node ID. diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 608be8d6..cab8f255 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -25,16 +25,16 @@ use crate::{ }, traits::{StateReader, TrieReader}, trie::node_utils::{get_child_node, subtree_leaf_start_key, subtree_root_level}, - types::{BucketId, BucketMeta, NodeId, SaltKey}, + types::{BucketId, BucketMeta, CommitmentBytes, NodeId, SaltKey}, SlotId, }; -use banderwagon::{Element, Fr}; +use banderwagon::{Element, Fr, Zero}; use ipa_multipoint::{lagrange_basis::LagrangeBasis, multiproof::ProverQuery}; use salt_macros::prelude::*; use salt_macros::{chunks, into_iter, num_threads}; use std::collections::{BTreeMap, BTreeSet}; -use std::{format, string::ToString, vec, vec::Vec}; +use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; use hashbrown::HashMap; use rustc_hash::FxBuildHasher; @@ -86,15 +86,26 @@ fn multi_commitments_to_scalars( where Store: TrieReader, { - // Helper closures for concise element conversion + // Helper closure for concise element conversion let to_element = |bytes| Element::from_bytes_unchecked_uncompressed(bytes); - let default_element = |node_id| to_element(default_commitment(node_id)); - let total_capacity = nodes.len() * DOMAIN_SIZE; - let mut all_child_commitments = Vec::with_capacity(total_capacity); + // The trie is sparse, so most of a node's 256 children carry a default + // commitment, of which only a handful of distinct values exist (one or two + // per level). Mapping a commitment to the scalar field costs a field + // inversion, so defaults are converted once and cached by value while only + // the children that actually exist in storage are batch-converted. + let mut default_scalars: FxHashMap = FxHashMap::default(); + let mut cached_default_scalar = |bytes: CommitmentBytes| { + *default_scalars + .entry(bytes) + .or_insert_with(|| to_element(bytes).map_to_scalar_field()) + }; + + let mut scalars = vec![Fr::zero(); nodes.len() * DOMAIN_SIZE]; + // Children present in storage: (position in `scalars`, commitment) + let mut real_children: Vec<(usize, Element)> = Vec::new(); - // Load child commitments for each internal node - for (node_id, _) in nodes { + for (i, (node_id, _)) in nodes.iter().enumerate() { // Calculate starting index for this node's 256 children let child_idx = get_child_node(&logic_parent_id(*node_id), 0); @@ -111,23 +122,32 @@ where } else { child_idx // Non-root levels: all use child_idx as default }; - let mut child_commitments = vec![default_element(default_idx); DOMAIN_SIZE]; + let node_scalars = &mut scalars[i * DOMAIN_SIZE..(i + 1) * DOMAIN_SIZE]; + node_scalars.fill(cached_default_scalar(default_commitment(default_idx))); // Special case: root level first child uses different default if child_idx == ROOT_LEVEL_CHILD_START { - child_commitments[0] = default_element(child_idx); + node_scalars[0] = cached_default_scalar(default_commitment(child_idx)); } - // Replace defaults with actual commitments where they exist + // Record actual commitments to overwrite the defaults where they exist for (absolute_node_id, commitment_bytes) in children { let relative_index = absolute_node_id as usize - child_idx as usize; - child_commitments[relative_index] = to_element(commitment_bytes); + real_children.push(( + i * DOMAIN_SIZE + relative_index, + to_element(commitment_bytes), + )); } + } - all_child_commitments.extend(child_commitments); + // Batch-convert the existing children and scatter them into place. + let elements: Vec = real_children.iter().map(|(_, element)| *element).collect(); + let real_scalars = Element::batch_map_to_scalar_field(&elements); + for ((position, _), scalar) in real_children.iter().zip(real_scalars) { + scalars[*position] = scalar; } - Ok(Element::batch_map_to_scalar_field(&all_child_commitments)) + Ok(scalars) } /// Creates IPA prover queries for a given commitment and evaluation points. @@ -151,11 +171,13 @@ fn create_prover_queries( poly: LagrangeBasis, points: BTreeSet, ) -> Vec { + // One shared allocation per polynomial, however many points are opened. + let poly = Arc::new(poly); points .iter() .map(|&i| ProverQuery { commitment, - poly: poly.clone(), + poly: Arc::clone(&poly), point: i, result: poly.evaluate_in_domain(i), }) @@ -198,77 +220,92 @@ where }); } - // Validate salt keys - salt_keys.iter().try_for_each(|key| { - let capacity = if key.is_in_meta_bucket() { - META_BUCKET_SIZE as u64 - } else { - store - .metadata(key.bucket_id()) - .map_err(|e| ProofError::StateReadError { - reason: format!( - "Failed to read metadata for bucket {}: {e:?}", - key.bucket_id() - ), - })? - .capacity + // Steps 1 & 2: Validate every key against its bucket capacity and record + // the trie level of each bucket. Keys arrive sorted, so each bucket's + // metadata is read exactly once. + let mut buckets_level: FxHashMap = FxHashMap::default(); + let mut current_bucket: Option<(BucketId, u64)> = None; + for key in salt_keys { + let bucket_id = key.bucket_id(); + let capacity = match current_bucket { + Some((bucket, capacity)) if bucket == bucket_id => capacity, + _ => { + let (capacity, level) = if bucket_id < NUM_META_BUCKETS as BucketId { + // Metadata buckets are always at level 1 (never expand into subtrees) + (META_BUCKET_SIZE as u64, METADATA_BUCKET_LEVEL) + } else { + // Data buckets: read metadata to determine capacity and + // subtree structure (higher capacity = higher level root) + let meta = + store + .metadata(bucket_id) + .map_err(|e| ProofError::StateReadError { + reason: format!( + "Failed to read metadata for bucket {bucket_id}: {e:?}" + ), + })?; + let level = MAX_SUBTREE_LEVELS - subtree_root_level(meta.capacity); + (meta.capacity, level as u8) + }; + buckets_level.insert(bucket_id, level); + current_bucket = Some((bucket_id, capacity)); + capacity + } }; - (key.slot_id() < capacity) - .then_some(()) - .ok_or(ProofError::InvalidSaltKey { + if key.slot_id() >= capacity { + return Err(ProofError::InvalidSaltKey { key: *key, capacity, - }) - })?; - - // Step 1: Extract and deduplicate bucket IDs from the input keys - let mut bucket_ids = salt_keys.iter().map(|k| k.bucket_id()).collect::>(); - bucket_ids.dedup(); - - // Step 2: Determine the trie level for each bucket - let buckets_level: FxHashMap = bucket_ids - .into_iter() - .map(|bucket_id| { - if bucket_id < NUM_META_BUCKETS as BucketId { - // Metadata buckets are always at level 1 (never expand into subtrees) - Ok((bucket_id, METADATA_BUCKET_LEVEL)) - } else { - // Data buckets: read metadata to determine subtree structure - let meta = store - .metadata(bucket_id) - .map_err(|e| ProofError::StateReadError { - reason: format!("Failed to read metadata for bucket {bucket_id}: {e:?}"), - })?; - // Convert capacity to subtree root level (higher capacity = higher level root) - let level = MAX_SUBTREE_LEVELS - subtree_root_level(meta.capacity); - Ok((bucket_id, level as u8)) - } - }) - .collect::>()?; + }); + } + } // Step 3: Build the minimal node hierarchy needed for authentication let (internal_nodes, leaf_nodes) = parents_and_points(salt_keys, &buckets_level); - // Step 4: Collect cryptographic commitments for all parent nodes - let parents_commitments: BTreeMap = internal_nodes - .iter() - .chain(leaf_nodes.iter()) - .map(|(&parent, _)| { - let physical_parent = connect_parent_id(parent); - let commitment = Element::from_bytes_unchecked_uncompressed( - store - .commitment(physical_parent) - .map_err(|e| ProofError::StateReadError { - reason: format!( - "Failed to load commitment for node {physical_parent}: {e:?}" - ), - })?, - ); - - Ok((physical_parent, SerdeCommitment(commitment))) - }) - .collect::>()?; + // Step 4: Collect cryptographic commitments for all parent nodes in + // parallel; steps 5 and 6 reuse them instead of re-reading the store. + let parent_ids: Vec = internal_nodes + .keys() + .chain(leaf_nodes.keys()) + .map(|&parent| connect_parent_id(parent)) + .collect(); + let commitment_chunk = parent_ids.len().div_ceil(num_threads!()).max(1); + let parents_commitments: BTreeMap = chunks!( + parent_ids, + commitment_chunk + ) + .map(|chunk| { + chunk + .iter() + .map(|&physical_parent| { + let commitment = Element::from_bytes_unchecked_uncompressed( + store + .commitment(physical_parent) + .map_err(|e| ProofError::StateReadError { + reason: format!( + "Failed to load commitment for node {physical_parent}: {e:?}" + ), + })?, + ); + Ok((physical_parent, SerdeCommitment(commitment))) + }) + .collect::>>() + }) + .collect::>>()? + .into_iter() + .flatten() + .collect(); + + let parent_commitment = |parent: NodeId| -> ProofResult { + parents_commitments + .get(&connect_parent_id(parent)) + .map(|commitment| commitment.0) + .ok_or_else(|| ProofError::StateReadError { + reason: format!("Failed to load commitment for node {parent}"), + }) + }; // Step 5: Generate IPA prover queries for each node in internal nodes let in_nodes: Vec<_> = internal_nodes.into_iter().collect(); @@ -281,15 +318,8 @@ where .iter() .zip(children_scalars.chunks(DOMAIN_SIZE)) .map(|((parent, points), children_scalars)| { - let commitment = store - .commitment(connect_parent_id(*parent)) - .map(Element::from_bytes_unchecked_uncompressed) - .map_err(|e| ProofError::StateReadError { - reason: format!("Failed to load commitment for node {parent}: {e:?}"), - })?; - Ok(create_prover_queries( - commitment, + parent_commitment(*parent)?, LagrangeBasis::new(children_scalars.to_vec()), points.clone(), )) @@ -309,16 +339,7 @@ where // Step 6: Generate IPA prover queries for each node in leaf nodes let leaf_queries = into_iter!(leaf_nodes) .map(|(parent, points)| { - let physical_parent = connect_parent_id(parent); - let parent_commitment = store - .commitment(physical_parent) - .map(Element::from_bytes_unchecked_uncompressed) - .map_err(|e| ProofError::StateReadError { - reason: format!( - "Failed to load element commitment for node {physical_parent}: {e:?}" - ), - })?; - process_leaf_node(store, parent, parent_commitment, points) + process_leaf_node(store, parent, parent_commitment(parent)?, points) }) .collect::>>()? .into_iter() @@ -464,7 +485,7 @@ mod tests { let poly = LagrangeBasis::new(coeffs); let query = ProverQuery { commitment: crs.commit_lagrange_poly(&poly), - poly: poly.clone(), + poly: poly.clone().into(), point: 208, result: poly.evaluate_in_domain(208), }; diff --git a/salt/src/trie/trie.rs b/salt/src/trie/trie.rs index e0ed3c65..1b65a940 100644 --- a/salt/src/trie/trie.rs +++ b/salt/src/trie/trie.rs @@ -32,7 +32,6 @@ use crate::{ types::*, }; use banderwagon::{platform, salt_committer::Committer, Element, Fr, PrimeField}; -use ipa_multipoint::crs::CRS; use salt_macros::prelude::*; use salt_macros::{chunks, into_iter, iter, num_threads, sort_unstable_by, sort_unstable_by_key}; @@ -46,6 +45,13 @@ use core::{cmp::Ordering, ops::Range}; /// Global shared instance of the Committer to avoid repeated expensive initialization static SHARED_COMMITTER: Lazy> = Lazy::new(|| Arc::new(build_shared_committer())); +/// Returns the process-wide committer holding fixed-base precomputation +/// tables for the CRS generators. Forces the one-time table construction on +/// first use. +pub(crate) fn shared_committer() -> Arc { + Arc::clone(&SHARED_COMMITTER) +} + /// Builds the shared committer's precomputation tables. /// /// With `parallel` enabled, the construction is fully isolated from the @@ -59,7 +65,12 @@ static SHARED_COMMITTER: Lazy> = Lazy::new(|| Arc::new(build_shar /// touches `SHARED_COMMITTER` would re-enter this initialization on the /// same stack and deadlock. fn build_shared_committer() -> Committer { - let build = || Committer::new(&CRS::default().G, platform::DEFAULT_PRECOMP_WINDOW_SIZE); + let build = || { + Committer::new( + &crate::proof::prover::DEFAULT_CRS.G, + platform::DEFAULT_PRECOMP_WINDOW_SIZE, + ) + }; #[cfg(feature = "parallel")] { // Never run `build` inline here: under `parallel` it would par_iter From 62741fe21f0ed3bee612df75f508c2cf0884a97a Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 7 Sep 2026 17:40:43 +0800 Subject: [PATCH 02/18] perf(proof): cache node polynomials across witnesses and fix the blinding base - proof/subtrie: a process-wide node-polynomial cache keyed by physical node id and validated by commitment bytes (the commitment binds the children), so unchanged nodes skip the 256-child range read and the map-to-field pass. Query construction keeps node/point order either way: the transcript hashes it. Hit rate on mainnet ~44% internal / ~48% leaf (misses are the block's own write paths). - ipa: the shared committer carries Q as base n, so the IPA's per-round blinding terms z*(w*Q) are fixed-base multiplications instead of two variable-base ones per round. - banderwagon: ScalarWindows produces the wNAF digits on the fly, so mul_index no longer allocates a Vec per call. - rayon chunk floors in the multiproof aggregate, the shape pass, the parent reads and the fixed-base MSM (8 terms): below that the dispatch costs more than the work. Measured with mega-reth's replay-full pipeline (W32/K4/M6, 5000 mainnet blocks, 32-vCPU box): 150 -> 172 blocks/s; per witness at M=1 7.43 -> 6.00 ms (IPA 2.41 -> 1.70, internal nodes 1.88 -> 1.37). Proofs stay byte-identical (see the determinism test). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01722A9KuCsH6MnM83Mhax3Y --- banderwagon/src/salt_committer.rs | 98 ++++++----- ipa-multipoint/src/ipa.rs | 42 ++++- ipa-multipoint/src/multiproof.rs | 4 +- salt/src/proof/shape.rs | 2 +- salt/src/proof/subtrie.rs | 262 ++++++++++++++++++++++-------- salt/src/proof/witness.rs | 29 +++- salt/src/trie/trie.rs | 14 +- 7 files changed, 332 insertions(+), 119 deletions(-) diff --git a/banderwagon/src/salt_committer.rs b/banderwagon/src/salt_committer.rs index bafa1030..22d13b5e 100644 --- a/banderwagon/src/salt_committer.rs +++ b/banderwagon/src/salt_committer.rs @@ -33,7 +33,7 @@ use ark_ff::Zero; use salt_macros::iter; use salt_macros::prelude::*; -use std::{vec, vec::Vec}; +use std::vec::Vec; /// Precomputed Multi-Scalar Multiplication engine for fixed base points. /// @@ -209,7 +209,7 @@ impl Committer { pub fn mul_index(&self, scalar: &Fr, g_i: usize) -> Element { use std::arch::x86_64::{_mm_prefetch, _MM_HINT_T0}; - let chunks = calculate_prefetch_index(scalar, self.window_size); + let mut digits = ScalarWindows::new(scalar, self.window_size); let mut result = EdwardsProjective::default(); let precomp_table = &self.tables[g_i]; @@ -220,7 +220,7 @@ impl Committer { let mut c_next = 0; // prefetch first point - let data_0 = unsafe { *chunks.get_unchecked(0) } as usize; + let data_0 = digits.next().expect("a scalar has at least one window") as usize; if data_0 >= half_wnd { c_next = 1; idx_next = wnd_size - data_0; @@ -236,9 +236,9 @@ impl Committer { idx = idx_next; // calculate point - for i in 1..chunks.len() { + for (i, digit) in (1..).zip(digits) { // fetch next point - idx_next = unsafe { *chunks.get_unchecked(i) as usize } + c_next; + idx_next = digit as usize + c_next; let carry = c_next; if idx_next >= half_wnd { c_next = 1; @@ -303,14 +303,14 @@ impl Committer { /// The result of `scalar * G[g_i]` as an `Element`. #[cfg(any(not(target_arch = "x86_64"), not(feature = "std")))] pub fn mul_index(&self, scalar: &Fr, g_i: usize) -> Element { - let chunks = calculate_prefetch_index(scalar, self.window_size); + let digits = ScalarWindows::new(scalar, self.window_size); let mut carry = 0; let half_win = 1 << (self.window_size - 1); let mut result = EdwardsProjective::default(); let precom_table = &self.tables[g_i]; - let mut ponits = Vec::with_capacity(chunks.len()); - for i in 0..chunks.len() { - let mut index = (chunks[i] + carry) as usize; + let mut ponits = Vec::with_capacity(digits.len()); + for (i, digit) in digits.enumerate() { + let mut index = (digit + carry) as usize; if index == 0 { continue; } @@ -437,43 +437,61 @@ fn add_affine_point(result: &mut EdwardsProjective, p2_x: &Fq, p2_y: &Fq) { mont_mul_asm(&mut result.z.0 .0, &f.0 .0, &g.0 .0); } -/// Decomposes a scalar into windows for efficient table lookups. +/// The `w`-bit windows of a scalar, least significant first: the digits that index the +/// precomputed multiplication tables. /// -/// This function splits a scalar into w-bit windows for use with the -/// precomputed multiplication tables. Each window represents a digit -/// in the windowed representation of the scalar. -/// -/// # Arguments -/// -/// * `scalar` - The scalar to decompose -/// * `w` - Window size in bits -/// -/// # Returns -/// -/// A vector of w-bit values representing the windowed decomposition. -#[inline] -fn calculate_prefetch_index(scalar: &Fr, w: usize) -> Vec { - // Convert scalar from Montgomery form to big integer - let source_vec = scalar.into_bigint().0; - let mut index_vec = vec![]; - - // Extract w bits of data from a scalar of n bits length - // Fr's bit length is 253 + Carry, so the maximum length is 254 - for start_bit in (0..254).step_by(w) { +/// Fr's bit length is 253 plus a carry, so the windows cover 254 bits. They are produced on +/// the fly from the scalar's limbs, so a multiplication never touches the heap. +#[derive(Clone, Copy)] +struct ScalarWindows { + /// The scalar out of Montgomery form. + limbs: [u64; 4], + /// Window size in bits. + w: usize, + /// First bit of the next window. + start_bit: usize, +} + +impl ScalarWindows { + #[inline] + fn new(scalar: &Fr, w: usize) -> Self { + Self { + limbs: scalar.into_bigint().0, + w, + start_bit: 0, + } + } + + /// Number of windows a scalar has at this window size. + #[cfg(any(not(target_arch = "x86_64"), not(feature = "std")))] + #[inline] + fn len(&self) -> usize { + 254usize.div_ceil(self.w) + } +} + +impl Iterator for ScalarWindows { + type Item = u64; + + #[inline] + fn next(&mut self) -> Option { + let start_bit = self.start_bit; + if start_bit >= 254 { + return None; + } + self.start_bit += self.w; + let w = self.w; let source_i = start_bit >> 6; let offset_in_i = start_bit & 63; - let mut d = (source_vec[source_i] >> offset_in_i) & ((1 << w) - 1); - // If the data is not enough, take the remaining data from the next - if offset_in_i + w > 64 && source_i < source_vec.len() - 1 { + let mut d = (self.limbs[source_i] >> offset_in_i) & ((1 << w) - 1); + // If the data is not enough, take the remaining data from the next limb + if offset_in_i + w > 64 && source_i < self.limbs.len() - 1 { let left = w - (64 - offset_in_i); - d |= (source_vec[source_i + 1] & ((1 << left) - 1)) << (64 - offset_in_i); - }; - - index_vec.push(d) + d |= (self.limbs[source_i + 1] & ((1 << left) - 1)) << (64 - offset_in_i); + } + Some(d) } - - index_vec } #[cfg(test)] diff --git a/ipa-multipoint/src/ipa.rs b/ipa-multipoint/src/ipa.rs index 1027b8d1..20e5325d 100644 --- a/ipa-multipoint/src/ipa.rs +++ b/ipa-multipoint/src/ipa.rs @@ -201,7 +201,8 @@ pub fn create( /// # Arguments /// /// * `precomp` - Fixed-base tables for the CRS `G` vector (must cover at least -/// `a_vec.len()` bases, in CRS order) +/// `a_vec.len()` bases, in CRS order); when they cover one more base, it must +/// be `Q`, and the blinding terms become fixed-base multiplications too /// * `q` - The CRS blinding generator `Q` pub fn create_with_precomp( transcript: &mut Transcript, @@ -229,7 +230,19 @@ pub fn create_with_precomp( transcript.append_scalar(b"output point", &output_point); let w = transcript.challenge_scalar(b"w"); - let Q = q * w; + // Tables that cover one base past the vector carry `Q` there (the salt trie's shared + // committer does), which turns the blinding terms `z·(w·Q) = (w·z)·Q` into fixed-base + // multiplications as well. Otherwise `Q` is folded with one variable-base product. + let q_index = (precomp.num_bases() > n).then_some(n); + debug_assert!( + q_index.is_none_or(|i| precomp.mul_index(&Fr::one(), i) == q), + "committer base {n} is not the blinding generator Q" + ); + let Q = q_index.is_none().then(|| q * w); + let blind = |z: Fr| match q_index { + Some(i) => precomp.mul_index(&(w * z), i), + None => Q.expect("Q is folded whenever the tables lack it") * z, + }; let num_rounds = log2(n); @@ -261,11 +274,11 @@ pub fn create_with_precomp( let left_compute = || -> Element { let z_L = inner_product(a_R, b_L); - fixed_base_msm(precomp, &l_terms) + Q * z_L + fixed_base_msm(precomp, &l_terms) + blind(z_L) }; let right_compute = || -> Element { let z_R = inner_product(a_L, b_R); - fixed_base_msm(precomp, &r_terms) + Q * z_R + fixed_base_msm(precomp, &r_terms) + blind(z_R) }; let (L, R) = join!(left_compute, right_compute); @@ -304,8 +317,12 @@ pub fn create_with_precomp( /// Sums `scalar · G[index]` over the given terms using precomputed wNAF /// tables, splitting the terms across threads. +/// Smallest number of terms one parallel task takes: below this the rayon dispatch costs more +/// than the fixed-base multiplications it hands out, and concurrent proofs only contend. +const MSM_MIN_CHUNK: usize = 8; + pub(crate) fn fixed_base_msm(precomp: &Committer, terms: &[(usize, Fr)]) -> Element { - let chunk_size = terms.len().div_ceil(num_threads!()).max(1); + let chunk_size = terms.len().div_ceil(num_threads!()).max(MSM_MIN_CHUNK); chunks!(terms, chunk_size) .map(|chunk| { let mut acc = Element::zero(); @@ -482,6 +499,21 @@ mod tests { assert_eq!(folded.to_bytes().unwrap(), precomputed.to_bytes().unwrap()); + // Tables that also carry `Q` (as base `n`) take the fixed-base blinding path. + let mut bases_with_q = crs.G.clone(); + bases_with_q.push(crs.Q); + let committer_with_q = Committer::new(&bases_with_q, 6); + let fixed_base_q = create_with_precomp( + &mut Transcript::new(b"ip_no_zk"), + &committer_with_q, + crs.Q, + a.clone(), + a_comm, + b.clone(), + input_point, + ); + assert_eq!(folded.to_bytes().unwrap(), fixed_base_q.to_bytes().unwrap()); + let output_point = inner_product(&a, &b); assert!(precomputed.verify_multiexp( &mut Transcript::new(b"ip_no_zk"), diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index 03f2ec19..0aa206d9 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -119,7 +119,9 @@ impl MultiPoint { let grouped_queries: Vec<_> = into_iter!(grouped_queries).collect(); - let chunk_size = grouped_queries.len().div_ceil(num_threads!()); + // At least a few point groups per task: each is a 256-coefficient pass, too little to + // farm out one by one. + let chunk_size = grouped_queries.len().div_ceil(num_threads!()).max(8); // aggregate all of the queries evaluated at the same point; // accumulate scaled polynomials in place instead of cloning each diff --git a/salt/src/proof/shape.rs b/salt/src/proof/shape.rs index 58be3648..6ff278f0 100644 --- a/salt/src/proof/shape.rs +++ b/salt/src/proof/shape.rs @@ -64,7 +64,7 @@ pub(crate) fn parents_and_points( ) { // One accumulator pair per thread chunk, merged once at the end. (A // per-key map-reduce would allocate and merge two BTreeMaps per key.) - let chunk_size = salt_keys.len().div_ceil(num_threads!()).max(1); + let chunk_size = salt_keys.len().div_ceil(num_threads!()).max(64); let partials: Vec<_> = chunks!(salt_keys, chunk_size) .map(|chunk| { let mut internal_nodes: BTreeMap> = BTreeMap::new(); diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index cab8f255..f24b47a1 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -40,6 +40,59 @@ use hashbrown::HashMap; use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; +/// Smallest number of parent commitments one parallel task reads. +const MIN_PARENT_CHUNK: usize = 8; +/// Smallest number of internal nodes one parallel task materializes (256 child reads each). +const MIN_NODE_CHUNK: usize = 4; + +/// Process-wide cache of node polynomials, keyed by node id and validated by the node's +/// commitment. +/// +/// A commitment binds the node's children, so a node whose commitment is unchanged since its +/// polynomial was last built has the same 256 child scalars (internal node) or slot scalars +/// (leaf node), and the store reads plus scalar conversions that produce them can be skipped. +/// Consecutive blocks share most of their authentication paths, which is what makes the hit +/// rate worth the memory: at most `SHARDS * MAX_PER_SHARD` polynomials of 8 KiB each. A shard +/// that fills up is cleared whole; the working set refills within a few proofs. +mod node_poly_cache { + use super::*; + use crate::types::CommitmentBytes; + use crate::Lazy; + use spin::RwLock; + + const SHARDS: usize = 64; + const MAX_PER_SHARD: usize = 512; + + type Shard = RwLock)>>; + + static CACHE: Lazy> = Lazy::new(|| { + (0..SHARDS) + .map(|_| RwLock::new(FxHashMap::default())) + .collect() + }); + + fn shard(node: NodeId) -> &'static Shard { + &CACHE[(node % SHARDS as NodeId) as usize] + } + + /// The cached polynomial of `node`, if it was built for exactly this commitment. + pub(super) fn get(node: NodeId, commitment: &CommitmentBytes) -> Option> { + let guard = shard(node).read(); + guard + .get(&node) + .filter(|(seen, _)| seen == commitment) + .map(|(_, poly)| Arc::clone(poly)) + } + + pub(super) fn insert(node: NodeId, commitment: CommitmentBytes, poly: Arc) { + let mut guard = shard(node).write(); + if guard.len() >= MAX_PER_SHARD { + guard.clear(); + } + guard.insert(node, (commitment, poly)); + } +} + // Constants for improved code readability const METADATA_BUCKET_LEVEL: u8 = 1; const SLOT_INDEX_MASK: u64 = 0xff; @@ -166,13 +219,23 @@ where /// /// # Returns /// A vector of `ProverQuery` objects, one for each evaluation point +#[cfg(test)] fn create_prover_queries( commitment: Element, poly: LagrangeBasis, points: BTreeSet, ) -> Vec { // One shared allocation per polynomial, however many points are opened. - let poly = Arc::new(poly); + shared_prover_queries(commitment, Arc::new(poly), &points) +} + +/// [`create_prover_queries`] over an already shared polynomial (a cache hit, or one built for +/// the cache). +fn shared_prover_queries( + commitment: Element, + poly: Arc, + points: &BTreeSet, +) -> Vec { points .iter() .map(|&i| ProverQuery { @@ -271,32 +334,43 @@ where .chain(leaf_nodes.keys()) .map(|&parent| connect_parent_id(parent)) .collect(); - let commitment_chunk = parent_ids.len().div_ceil(num_threads!()).max(1); - let parents_commitments: BTreeMap = chunks!( - parent_ids, - commitment_chunk - ) - .map(|chunk| { - chunk - .iter() - .map(|&physical_parent| { - let commitment = Element::from_bytes_unchecked_uncompressed( - store - .commitment(physical_parent) - .map_err(|e| ProofError::StateReadError { - reason: format!( - "Failed to load commitment for node {physical_parent}: {e:?}" - ), - })?, - ); - Ok((physical_parent, SerdeCommitment(commitment))) + let commitment_chunk = parent_ids + .len() + .div_ceil(num_threads!()) + .max(MIN_PARENT_CHUNK); + let parents_read: Vec<(NodeId, crate::types::CommitmentBytes)> = + chunks!(parent_ids, commitment_chunk) + .map(|chunk| { + chunk + .iter() + .map(|&physical_parent| { + let bytes = store.commitment(physical_parent).map_err(|e| { + ProofError::StateReadError { + reason: format!( + "Failed to load commitment for node {physical_parent}: {e:?}" + ), + } + })?; + Ok((physical_parent, bytes)) + }) + .collect::>>() }) - .collect::>>() - }) - .collect::>>()? - .into_iter() - .flatten() - .collect(); + .collect::>>()? + .into_iter() + .flatten() + .collect(); + // The raw bytes key the polynomial cache; the decoded elements go into the proof. + let parent_bytes: FxHashMap = + parents_read.iter().copied().collect(); + let parents_commitments: BTreeMap = parents_read + .into_iter() + .map(|(id, bytes)| { + ( + id, + SerdeCommitment(Element::from_bytes_unchecked_uncompressed(bytes)), + ) + }) + .collect(); let parent_commitment = |parent: NodeId| -> ProofResult { parents_commitments @@ -307,56 +381,115 @@ where }) }; - // Step 5: Generate IPA prover queries for each node in internal nodes + // Step 5: Generate IPA prover queries for each node in internal nodes. A node whose + // polynomial the cache holds for its current commitment skips the child reads; the rest + // are materialized in parallel chunks. Query order (node order, then point order) is the + // same either way: the transcript hashes it, so a cache hit must not reorder the proof. let in_nodes: Vec<_> = internal_nodes.into_iter().collect(); - let chunk_size = in_nodes.len().div_ceil(num_threads!()); - let mut queries = chunks!(in_nodes, chunk_size) + let mut polys: Vec>> = Vec::with_capacity(in_nodes.len()); + let mut missing: Vec = Vec::new(); + for (i, (parent, _)) in in_nodes.iter().enumerate() { + let physical = connect_parent_id(*parent); + let hit = parent_bytes + .get(&physical) + .and_then(|bytes| node_poly_cache::get(physical, bytes)); + if hit.is_none() { + missing.push(i); + } + polys.push(hit); + } + let missing_nodes: Vec<(NodeId, BTreeSet)> = missing + .iter() + .map(|&i| (in_nodes[i].0, BTreeSet::new())) + .collect(); + let chunk_size = missing_nodes + .len() + .div_ceil(num_threads!()) + .max(MIN_NODE_CHUNK); + let computed: Vec>> = chunks!(missing_nodes, chunk_size) .map(|nodes| { - let children_scalars = multi_commitments_to_scalars(store, nodes)?; - - let res = nodes - .iter() - .zip(children_scalars.chunks(DOMAIN_SIZE)) - .map(|((parent, points), children_scalars)| { - Ok(create_prover_queries( - parent_commitment(*parent)?, - LagrangeBasis::new(children_scalars.to_vec()), - points.clone(), - )) - }) - .collect::>>()? - .into_iter() - .flatten() - .collect::>(); - - Ok(res) + let scalars = multi_commitments_to_scalars(store, nodes)?; + Ok(scalars + .chunks(DOMAIN_SIZE) + .map(|node_scalars| Arc::new(LagrangeBasis::new(node_scalars.to_vec()))) + .collect::>()) }) - .collect::>>()? - .into_iter() - .flatten() - .collect::>(); - - // Step 6: Generate IPA prover queries for each node in leaf nodes - let leaf_queries = into_iter!(leaf_nodes) - .map(|(parent, points)| { - process_leaf_node(store, parent, parent_commitment(parent)?, points) - }) - .collect::>>()? - .into_iter() - .flatten(); + .collect::>>()?; + for (&i, poly) in missing.iter().zip(computed.into_iter().flatten()) { + let physical = connect_parent_id(in_nodes[i].0); + if let Some(bytes) = parent_bytes.get(&physical) { + node_poly_cache::insert(physical, *bytes, Arc::clone(&poly)); + } + polys[i] = Some(poly); + } + let mut queries: Vec = Vec::new(); + for ((parent, points), poly) in in_nodes.into_iter().zip(polys) { + let poly = poly.expect("every internal node polynomial is resolved above"); + queries.extend(shared_prover_queries( + parent_commitment(parent)?, + poly, + &points, + )); + } - queries.extend(leaf_queries); + // Step 6: Generate IPA prover queries for each node in leaf nodes, with the same cache + // and the same order discipline. + let leaf_nodes: Vec<_> = leaf_nodes.into_iter().collect(); + let mut leaf_polys: Vec>> = Vec::with_capacity(leaf_nodes.len()); + let mut leaf_missing: Vec = Vec::new(); + for (i, (parent, _)) in leaf_nodes.iter().enumerate() { + let physical = connect_parent_id(*parent); + let hit = parent_bytes + .get(&physical) + .and_then(|bytes| node_poly_cache::get(physical, bytes)); + if hit.is_none() { + leaf_missing.push(i); + } + leaf_polys.push(hit); + } + let computed: Vec> = into_iter!(leaf_missing.clone()) + .map(|i| leaf_polynomial(store, leaf_nodes[i].0).map(Arc::new)) + .collect::>>()?; + for (&i, poly) in leaf_missing.iter().zip(computed) { + let physical = connect_parent_id(leaf_nodes[i].0); + if let Some(bytes) = parent_bytes.get(&physical) { + node_poly_cache::insert(physical, *bytes, Arc::clone(&poly)); + } + leaf_polys[i] = Some(poly); + } + for ((parent, points), poly) in leaf_nodes.into_iter().zip(leaf_polys) { + let poly = poly.expect("every leaf node polynomial is resolved above"); + queries.extend(shared_prover_queries( + parent_commitment(parent)?, + poly, + &points, + )); + } Ok((queries, parents_commitments, buckets_level)) } /// Processes a leaf node to create prover queries. +#[cfg(test)] fn process_leaf_node( store: &Store, parent: NodeId, parent_commitment: Element, points: BTreeSet, ) -> ProofResult> +where + Store: StateReader, +{ + Ok(create_prover_queries( + parent_commitment, + leaf_polynomial(store, parent)?, + points, + )) +} + +/// The 256-slot polynomial of a leaf node: every slot's value hashed to the field, with the +/// bucket kind's default for empty slots. +fn leaf_polynomial(store: &Store, parent: NodeId) -> ProofResult where Store: StateReader, { @@ -401,12 +534,7 @@ where default_coefficients[index] = slot_to_field(&Some(value)); } - // Create IPA prover queries for the specified evaluation points - Ok(create_prover_queries( - parent_commitment, - LagrangeBasis::new(default_coefficients), - points, - )) + Ok(LagrangeBasis::new(default_coefficients)) } #[cfg(test)] diff --git a/salt/src/proof/witness.rs b/salt/src/proof/witness.rs index 8ded614d..f7738574 100644 --- a/salt/src/proof/witness.rs +++ b/salt/src/proof/witness.rs @@ -195,9 +195,11 @@ impl Witness { reason: format!("{e:?}"), })?; + let salt_witness = SaltWitness::create(&witnessed_keys, store)?; + Ok(Witness { direct_lookup_tbl, - salt_witness: SaltWitness::create(&witnessed_keys, store)?, + salt_witness, }) } @@ -1173,4 +1175,29 @@ mod tests { .iter() .all(|&id| witness.metadata(id).unwrap() == new_metadata)); } + + /// A node-polynomial cache hit must reproduce the miss byte for byte: the same inputs + /// witnessed twice serialize identically (the second pass is served from the cache), and + /// the witness still verifies. + #[test] + fn cached_node_polynomials_reproduce_the_witness_byte_for_byte() { + let mut rng = StdRng::seed_from_u64(7); + let kvs: HashMap<_, _> = (0..64) + .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) + .collect(); + let store = MemStore::new(); + let updates = EphemeralSaltState::new(&store).update_fin(&kvs).unwrap(); + store.update_state(updates.clone()); + let (_root, trie_updates) = StateRoot::new(&store).update_fin(&updates).unwrap(); + store.update_trie(trie_updates); + + let build = || Witness::create([], kvs.keys(), &BTreeMap::new(), &store).unwrap(); + let encode = |witness: &Witness| { + bincode::serde::encode_to_vec(&witness.salt_witness, bincode::config::legacy()).unwrap() + }; + let first = encode(&build()); + let second = encode(&build()); + assert_eq!(first, second, "a cache hit must not change the proof"); + build().verify().unwrap(); + } } diff --git a/salt/src/trie/trie.rs b/salt/src/trie/trie.rs index 1b65a940..f2852ce1 100644 --- a/salt/src/trie/trie.rs +++ b/salt/src/trie/trie.rs @@ -66,10 +66,16 @@ pub(crate) fn shared_committer() -> Arc { /// same stack and deadlock. fn build_shared_committer() -> Committer { let build = || { - Committer::new( - &crate::proof::prover::DEFAULT_CRS.G, - platform::DEFAULT_PRECOMP_WINDOW_SIZE, - ) + let crs = &*crate::proof::prover::DEFAULT_CRS; + // `Q` rides along as base `crs.n`, so the IPA prover's blinding terms are fixed-base + // multiplications too (see `ipa::create_with_precomp`). + let bases: Vec = crs + .G + .iter() + .copied() + .chain(core::iter::once(crs.Q)) + .collect(); + Committer::new(&bases, platform::DEFAULT_PRECOMP_WINDOW_SIZE) }; #[cfg(feature = "parallel")] { From 25d387adb9b50e24f6a37c81d54440058ec055ed Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 7 Sep 2026 17:41:56 +0800 Subject: [PATCH 03/18] perf(proof): refresh cached node polynomials from a block's trie updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The node-polynomial cache only helps for nodes whose commitment did not change since they were cached; every node on a block's write paths is a miss (~55% of the internal nodes of a mainnet witness) that costs a 256-child range read and 256 map-to-field conversions. A witness proves the pre-state, so the polynomials it resolves are exactly the bases the block's transition transforms. `NodePolyRefresh` carries that transition — the (old, new) commitment of every changed node and the (old, new) value of every changed slot, the pair `StateRoot::update_fin` produced — and `Witness::create_with_refresh` (also `SaltWitness`/`SaltProof`) applies it after the witness's own lookups: each changed parent's polynomial is copied, its changed positions overwritten (hash-to-field of the new child commitment, or the slot's new value), and the result inserted under the parent's new commitment, so the next block's witness hits. Buckets whose capacity changes are frozen for the block; subtree tops are keyed the way the witness reads them; a parent read at any commitment other than the transition's old one is skipped. Debug builds check every refreshed entry: old + sum G_i*(s_i - base_i) == new, which commitment validation alone cannot see. The existing `create` signatures are unchanged. Measured on the replay-full pipeline: hit rate at one worker 44% -> 98%, per-witness internal-node phase 1.39 -> 0.42 ms; at six workers the witness build 19.6 -> 15.3 ms and the pipeline 265 -> 305 blocks/s. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01722A9KuCsH6MnM83Mhax3Y --- salt/src/lib.rs | 2 +- salt/src/proof/mod.rs | 1 + salt/src/proof/prover.rs | 19 +- salt/src/proof/salt_witness.rs | 18 +- salt/src/proof/subtrie.rs | 797 ++++++++++++++++++++++++++++++++- salt/src/proof/witness.rs | 81 +++- 6 files changed, 897 insertions(+), 21 deletions(-) diff --git a/salt/src/lib.rs b/salt/src/lib.rs index 0ef061f6..11b831fd 100644 --- a/salt/src/lib.rs +++ b/salt/src/lib.rs @@ -18,7 +18,7 @@ pub(crate) type Lazy = spin::Lazy; pub mod constant; pub mod empty_salt; pub mod proof; -pub use proof::{fx_hashmap_serde, ProofError, SaltProof, SaltWitness, Witness}; +pub use proof::{fx_hashmap_serde, NodePolyRefresh, ProofError, SaltProof, SaltWitness, Witness}; pub mod state; pub use state::{ hasher, state::EphemeralSaltState, state::PlainStateProvider, updates::StateUpdates, diff --git a/salt/src/proof/mod.rs b/salt/src/proof/mod.rs index bff27f0c..297f102c 100644 --- a/salt/src/proof/mod.rs +++ b/salt/src/proof/mod.rs @@ -78,6 +78,7 @@ mod test_utils; pub use prover::{fx_hashmap_serde, SaltProof, SerdeCommitment, SerdeMultiPointProof}; pub use salt_witness::SaltWitness; +pub use subtrie::NodePolyRefresh; pub use witness::Witness; /// Error type for proof operations diff --git a/salt/src/proof/prover.rs b/salt/src/proof/prover.rs index 55bbd500..667e8d5d 100644 --- a/salt/src/proof/prover.rs +++ b/salt/src/proof/prover.rs @@ -3,7 +3,7 @@ use crate::{ constant::{BUCKET_SLOT_ID_MASK, DOMAIN_SIZE, STARTING_NODE_ID}, proof::{ shape::{connect_parent_id, logic_parent_id, parents_and_points}, - subtrie::create_sub_trie, + subtrie::{create_sub_trie, NodePolyRefresh}, ProofError, ProofResult, }, traits::{StateReader, TrieReader}, @@ -220,6 +220,21 @@ pub(crate) fn slot_to_field(entry: &Option) -> Fr { impl SaltProof { /// Create a new proof. pub fn create(keys: I, store: &Store) -> Result + where + I: IntoIterator, + Store: StateReader + TrieReader, + { + Self::create_with_refresh(keys, store, None) + } + + /// [`Self::create`], additionally advancing the node-polynomial cache with `refresh`, the + /// witnessed block's own transition, once this proof's polynomials are resolved (see + /// [`NodePolyRefresh`]). The proof itself is the same with or without a refresh. + pub fn create_with_refresh( + keys: I, + store: &Store, + refresh: Option<&NodePolyRefresh<'_>>, + ) -> Result where I: IntoIterator, Store: StateReader + TrieReader, @@ -234,7 +249,7 @@ impl SaltProof { } keys.dedup(); - let (prover_queries, parents_commitments, levels) = create_sub_trie(store, &keys)?; + let (prover_queries, parents_commitments, levels) = create_sub_trie(store, &keys, refresh)?; let mut transcript = Transcript::new(b"st"); diff --git a/salt/src/proof/salt_witness.rs b/salt/src/proof/salt_witness.rs index bc332c21..61d851fa 100644 --- a/salt/src/proof/salt_witness.rs +++ b/salt/src/proof/salt_witness.rs @@ -5,7 +5,7 @@ //! enforces critical security properties to prevent state manipulation attacks. use crate::{ constant::ROOT_NODE_ID, - proof::{ProofError, SaltProof}, + proof::{subtrie::NodePolyRefresh, ProofError, SaltProof}, traits::{StateReader, TrieReader}, types::*, }; @@ -80,6 +80,20 @@ impl SaltWitness { /// - For regular keys, retrieves their values (or None if non-existent) /// - The proof is generated for all requested keys to ensure completeness pub fn create(keys: &[SaltKey], store: &Store) -> Result + where + Store: StateReader + TrieReader, + { + Self::create_with_refresh(keys, store, None) + } + + /// [`Self::create`], additionally advancing the node-polynomial cache with `refresh`, the + /// witnessed block's own transition, once the proof's polynomials are resolved (see + /// [`NodePolyRefresh`]). The witness itself is the same with or without a refresh. + pub fn create_with_refresh( + keys: &[SaltKey], + store: &Store, + refresh: Option<&NodePolyRefresh<'_>>, + ) -> Result where Store: StateReader + TrieReader, { @@ -100,7 +114,7 @@ impl SaltWitness { }) .collect::, _>>()?; - let proof = SaltProof::create(kvs.keys().copied(), store)?; + let proof = SaltProof::create_with_refresh(kvs.keys().copied(), store, refresh)?; Ok(SaltWitness { kvs, proof }) } diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index f24b47a1..968b8c1c 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -13,19 +13,28 @@ //! 3. Build minimal node hierarchy using [`parents_and_points`] //! 4. Collect cryptographic commitments for all parent nodes //! 5. Generate IPA prover queries for leaf nodes (bucket contents) and internal nodes (child commitments) +//! 6. Optionally advance the process-wide node-polynomial cache to the witnessed block's +//! post-state from the block's own transition ([`NodePolyRefresh`]) use crate::{ constant::{ default_commitment, BUCKET_SLOT_BITS, BUCKET_SLOT_ID_MASK, DOMAIN_SIZE, MAX_SUBTREE_LEVELS, - META_BUCKET_SIZE, NUM_META_BUCKETS, STARTING_NODE_ID, + META_BUCKET_SIZE, NUM_META_BUCKETS, ROOT_NODE_ID, STARTING_NODE_ID, }, proof::{ prover::slot_to_field, shape::{connect_parent_id, logic_parent_id, parents_and_points}, ProofError, ProofResult, SerdeCommitment, }, + state::updates::StateUpdates, traits::{StateReader, TrieReader}, - trie::node_utils::{get_child_node, subtree_leaf_start_key, subtree_root_level}, - types::{BucketId, BucketMeta, CommitmentBytes, NodeId, SaltKey}, + trie::node_utils::{ + bucket_root_node_id, get_child_node, get_parent_node, subtree_leaf_for_key, + subtree_leaf_start_key, subtree_root_level, vc_position_in_parent, + }, + types::{ + bucket_id_from_metadata_key, get_bfs_level, get_local_number, BucketId, BucketMeta, + CommitmentBytes, NodeId, SaltKey, SaltValue, METADATA_KEYS_RANGE, + }, SlotId, }; use banderwagon::{Element, Fr, Zero}; @@ -36,9 +45,10 @@ use salt_macros::{chunks, into_iter, num_threads}; use std::collections::{BTreeMap, BTreeSet}; use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; -use hashbrown::HashMap; +use hashbrown::{HashMap, HashSet}; use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; +type FxHashSet = HashSet; /// Smallest number of parent commitments one parallel task reads. const MIN_PARENT_CHUNK: usize = 8; @@ -93,6 +103,254 @@ mod node_poly_cache { } } +/// One block's header-verified trie transition, used to advance the node-polynomial cache to +/// that block's post-state while the block's own witness is built. +/// +/// The two fields are the pair [`StateRoot::update_fin`](crate::trie::trie::StateRoot::update_fin) +/// produced for the block: every node whose commitment changed with its pre- and post-state +/// commitment, and every slot that changed with its old and new value. The witness proves the +/// pre-state, so the polynomials it resolves are exactly the bases this transition transforms; +/// the refresh runs after the witness's own lookups, patches each changed node's polynomial at +/// its changed positions and inserts the result under the node's new commitment, so the next +/// block's witness hits instead of rebuilding from storage. Entries stay validated by +/// commitment, so a stale or foreign entry is only ever a miss, never a wrong witness. +/// +/// The store the witness reads must be the transition's pre-state, the view `update_fin` ran +/// on; a parent the witness read at any other commitment is skipped. Applying one transition +/// again (a retried witness) derives the same polynomial from a base validated at the same +/// old commitment and inserts the same entry, so a refresh is idempotent (I8). +pub struct NodePolyRefresh<'a> { + /// The block's changed nodes as `(node, (old commitment, new commitment))`. + pub trie_updates: &'a [(NodeId, (CommitmentBytes, CommitmentBytes))], + /// The block's changed slots as `(key, (old value, new value))`. + pub state_updates: &'a StateUpdates, +} + +/// The changed positions of one parent's polynomial, with the parent's pre- and post-state +/// commitment. +struct ParentPatch { + old: CommitmentBytes, + new: CommitmentBytes, + positions: Vec<(usize, Fr)>, +} + +/// Derives, from the block's outputs alone, which position of which cached polynomial takes +/// which new scalar. Keys are physical node ids as the cache uses them +/// ([`connect_parent_id`]): a bucket whose subtree top changed is keyed by its main-trie +/// bucket root, like the witness keys it. +/// +/// The plan is complete for every parent it emits (I2, I3): every node whose commitment +/// changed is in `trie_updates`, so a parent's unchanged positions keep the base's values and +/// its changed positions are exactly its changed children (internal node) or changed slots +/// (leaf). Shapes where a per-position patch is wrong under a valid commitment are excluded +/// whole (I5): a bucket whose capacity changes is frozen and every patch inside it is dropped, +/// while the bucket root's position in its main-trie parent is still patched, because that +/// `(old, new)` pair is a true transition of the main-trie node. +fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap { + // Fold duplicates to the first old and the last new commitment. + let mut changed: FxHashMap = + FxHashMap::with_capacity_and_hasher(refresh.trie_updates.len(), FxBuildHasher); + for &(node, (old, new)) in refresh.trie_updates { + changed + .entry(node) + .and_modify(|pair| pair.1 = new) + .or_insert((old, new)); + } + + // Buckets whose capacity changes: the trie emits their old top with a default old + // commitment and resets contracted nodes without their children, so nothing inside them + // is patched. + let capacity = |value: &Option| { + value + .as_ref() + .and_then(|value| BucketMeta::try_from(value).ok()) + .map(|meta| meta.capacity) + }; + let frozen: FxHashSet = refresh + .state_updates + .data + .range(METADATA_KEYS_RANGE) + .filter(|(_, (old, new))| match (capacity(old), capacity(new)) { + (Some(old), Some(new)) => old != new, + _ => true, + }) + .map(|(key, _)| bucket_id_from_metadata_key(*key)) + .collect(); + + // A bucket is expanded when any of its subtree nodes changed; its slots then live in + // subtree leaves rather than in the bucket root. + let expanded: FxHashSet = changed + .keys() + .filter(|node| *node >> BUCKET_SLOT_BITS != 0) + .map(|node| (node >> BUCKET_SLOT_BITS) as BucketId) + .collect(); + + // Changed children, keyed by the physical id of the parent whose polynomial holds them + // (I4). A changed subtree node is recorded under its own id unless it is the top, which + // the trie records under the bucket root; so a subtree child's parent is the top exactly + // when the parent is absent from the transition. + let mut child_patches: Vec<(NodeId, usize, CommitmentBytes)> = + Vec::with_capacity(changed.len()); + for (&child, &(_, new)) in &changed { + if child == ROOT_NODE_ID { + continue; + } + let physical = if child >> BUCKET_SLOT_BITS != 0 { + let bucket = (child >> BUCKET_SLOT_BITS) as BucketId; + if frozen.contains(&bucket) { + continue; + } + // A subtree node at local level 0 is always a top, never a child. + if get_bfs_level(get_local_number(child)) == 0 { + continue; + } + let logical = get_parent_node(&child); + if changed.contains_key(&logical) { + logical + } else { + debug_assert!( + changed.contains_key(&bucket_root_node_id(bucket)), + "subtree node {child} changed but neither its parent nor its bucket root did" + ); + bucket_root_node_id(bucket) + } + } else { + get_parent_node(&child) + }; + child_patches.push((physical, vc_position_in_parent(&child), new)); + } + + // Changed slots, keyed by their leaf. A key whose leaf is absent from the transition + // (a slot beyond the bucket's capacity, which the trie drops) is skipped, never + // re-attributed to the bucket root. + let mut leaf_patches: Vec<(NodeId, usize, Fr)> = + Vec::with_capacity(refresh.state_updates.data.len()); + for (key, (_, new)) in &refresh.state_updates.data { + let leaf = if key.is_in_meta_bucket() { + bucket_root_node_id(key.bucket_id()) + } else { + let bucket = key.bucket_id(); + if frozen.contains(&bucket) { + continue; + } + if expanded.contains(&bucket) { + let leaf = subtree_leaf_for_key(key); + if !changed.contains_key(&leaf) { + continue; + } + leaf + } else { + bucket_root_node_id(bucket) + } + }; + // An emptied slot takes the bucket kind's default, as `leaf_polynomial` fills it. + let scalar = match new { + Some(_) => slot_to_field(new), + None if key.is_in_meta_bucket() => slot_to_field(&Some(BucketMeta::default().into())), + None => slot_to_field(&None), + }; + leaf_patches.push((leaf, (key.slot_id() & SLOT_INDEX_MASK) as usize, scalar)); + } + + // One batch maps every changed child commitment to its scalar; it is the same map the + // rebuild applies to a child read from storage. + let child_commitments: Vec = child_patches + .iter() + .map(|(_, _, commitment)| *commitment) + .collect(); + let child_scalars = Element::hash_commitments(&child_commitments); + + // Group by parent; a parent without an (old, new) pair of its own is not patched. + let mut plan: FxHashMap = FxHashMap::default(); + let patches = child_patches + .iter() + .zip(child_scalars) + .map(|(&(parent, position, _), scalar)| (parent, position, scalar)) + .chain(leaf_patches); + for (parent, position, scalar) in patches { + let Some(&(old, new)) = changed.get(&parent) else { + continue; + }; + plan.entry(parent) + .or_insert_with(|| ParentPatch { + old, + new, + positions: Vec::new(), + }) + .positions + .push((position, scalar)); + } + plan +} + +/// Applies a [`refresh_plan`]: for every planned parent, copies a base polynomial that is +/// validated at the parent's old commitment, overwrites the changed positions and inserts the +/// result under the new commitment. Returns the number of entries inserted. +/// +/// The base is the witness's in-hand polynomial when the witness read the parent at exactly +/// the old commitment, else the cached entry for the old commitment, else the parent is +/// skipped (I1: an entry is only ever derived from a base validated at `old`). Every +/// insert goes through the shard write lock as a whole `(commitment, polynomial)` pair, so +/// concurrent witnesses can only ever replace an entry with another valid one (I6). +fn apply_refresh( + plan: FxHashMap, + in_hand: &FxHashMap>, + parent_bytes: &FxHashMap, +) -> usize { + let mut refreshed = 0; + for (physical, patch) in plan { + let base = match in_hand.get(&physical) { + Some(poly) => { + // The witness resolved this polynomial against the commitment it read, so + // it is the pre-state base only if that commitment is the transition's old. + let read_at_old = parent_bytes.get(&physical) == Some(&patch.old); + debug_assert!( + read_at_old, + "node {physical}: the witness read a commitment other than the refresh's pre-state" + ); + if !read_at_old { + continue; + } + Arc::clone(poly) + } + None => match node_poly_cache::get(physical, &patch.old) { + Some(poly) => poly, + None => continue, + }, + }; + let mut values: Vec = (0..DOMAIN_SIZE) + .map(|i| base.evaluate_in_domain(i)) + .collect(); + #[cfg(debug_assertions)] + assert_patch_commits(physical, &patch, &values); + for &(position, scalar) in &patch.positions { + values[position] = scalar; + } + node_poly_cache::insert(physical, patch.new, Arc::new(LagrangeBasis::new(values))); + refreshed += 1; + } + refreshed +} + +/// Debug-build self-check of one refreshed entry (I10): the patched positions must carry the +/// parent's commitment from `old` to exactly `new`, `old + Σ G_i·(s_i − base_i) == new`, +/// which is how the trie produced `new`. The check reads no store and fails on a wrong +/// position, a wrong scalar, and a missing or extra position, which commitment validation +/// alone cannot see. +#[cfg(debug_assertions)] +fn assert_patch_commits(physical: NodeId, patch: &ParentPatch, base: &[Fr]) { + let committer = crate::trie::trie::shared_committer(); + let mut acc = Element::from_bytes_unchecked_uncompressed(patch.old); + for &(position, scalar) in &patch.positions { + acc += committer.mul_index(&(scalar - base[position]), position); + } + let got = Element::batch_to_commitments(&[acc])[0]; + assert_eq!( + got, patch.new, + "refreshed polynomial of node {physical} does not commit to its new commitment" + ); +} + // Constants for improved code readability const METADATA_BUCKET_LEVEL: u8 = 1; const SLOT_INDEX_MASK: u64 = 0xff; @@ -258,6 +516,9 @@ fn shared_prover_queries( /// /// * `store` - Storage backend providing access to both trie commitments and bucket data /// * `salt_keys` - Pre-sorted and deduplicated keys to generate proofs for +/// * `refresh` - The witnessed block's own transition, applied to the node-polynomial cache +/// once this proof's polynomials are resolved (see [`NodePolyRefresh`]); `None` leaves the +/// cache to the lookups and rebuild inserts alone /// /// # Returns /// @@ -273,6 +534,7 @@ fn shared_prover_queries( pub(crate) fn create_sub_trie( store: &Store, salt_keys: &[SaltKey], + refresh: Option<&NodePolyRefresh<'_>>, ) -> ProofResult where Store: StateReader + TrieReader, @@ -381,6 +643,11 @@ where }) }; + // Every resolved polynomial by physical parent id: the bases a refresh patches (I7: the + // witness reads the pre-state, so these are the polynomials the block's transition + // transforms). + let mut in_hand: FxHashMap> = FxHashMap::default(); + // Step 5: Generate IPA prover queries for each node in internal nodes. A node whose // polynomial the cache holds for its current commitment skips the child reads; the rest // are materialized in parallel chunks. Query order (node order, then point order) is the @@ -425,6 +692,7 @@ where let mut queries: Vec = Vec::new(); for ((parent, points), poly) in in_nodes.into_iter().zip(polys) { let poly = poly.expect("every internal node polynomial is resolved above"); + in_hand.insert(connect_parent_id(parent), Arc::clone(&poly)); queries.extend(shared_prover_queries( parent_commitment(parent)?, poly, @@ -459,6 +727,7 @@ where } for ((parent, points), poly) in leaf_nodes.into_iter().zip(leaf_polys) { let poly = poly.expect("every leaf node polynomial is resolved above"); + in_hand.insert(connect_parent_id(parent), Arc::clone(&poly)); queries.extend(shared_prover_queries( parent_commitment(parent)?, poly, @@ -466,6 +735,14 @@ where )); } + // Step 7: Advance the cache to the witnessed block's post-state. The queries above are + // already built from the pre-state polynomials, so this proof is unaffected (I9); it runs + // here rather than after the proof so the entries land before the next block's witness + // looks them up. + if let Some(refresh) = refresh { + apply_refresh(refresh_plan(refresh), &in_hand, &parent_bytes); + } + Ok((queries, parents_commitments, buckets_level)) } @@ -540,11 +817,17 @@ where #[cfg(test)] mod tests { use super::*; - use crate::constant::{META_BUCKET_SIZE, ROOT_NODE_ID}; + use crate::constant::{MAIN_TRIE_LEVELS, META_BUCKET_SIZE, MIN_BUCKET_SIZE, ROOT_NODE_ID}; use crate::proof::test_utils::*; use crate::{ - mem_store::MemStore, proof::prover::PRECOMPUTED_WEIGHTS, state::state::EphemeralSaltState, - trie::trie::StateRoot, + mem_store::MemStore, + proof::{ + prover::{SaltProof, PRECOMPUTED_WEIGHTS}, + shape::{encode_parent, is_encoded_node}, + }, + state::state::EphemeralSaltState, + trie::trie::{StateRoot, TrieUpdates}, + types::bucket_metadata_key, }; use ark_ff::BigInt; use banderwagon::{Fr, Zero}; @@ -589,7 +872,7 @@ mod tests { #[test] fn create_sub_trie_generates_valid_proofs() { let (store, salt_key) = setup_test_store(); - let (prover_queries, _, _) = create_sub_trie(&store, &[salt_key]).unwrap(); + let (prover_queries, _, _) = create_sub_trie(&store, &[salt_key], None).unwrap(); assert!(verify_ipa_proof(prover_queries)); } @@ -637,7 +920,7 @@ mod tests { let (store, salt_key) = setup_test_store(); // Single key - let (q1, _, _) = create_sub_trie(&store, &[salt_key]).unwrap(); + let (q1, _, _) = create_sub_trie(&store, &[salt_key], None).unwrap(); assert!(verify_ipa_proof(q1.clone())); // Multiple keys @@ -647,21 +930,22 @@ mod tests { salt_key, SaltKey::from((salt_key.bucket_id(), salt_key.slot_id() + 1)), ], + None, ) .unwrap(); assert!(verify_ipa_proof(q2)); // Duplicate keys - let (q3, _, _) = create_sub_trie(&store, &[salt_key, salt_key]).unwrap(); + let (q3, _, _) = create_sub_trie(&store, &[salt_key, salt_key], None).unwrap(); assert_eq!(q1.len(), q3.len()); // Metadata bucket - let (q4, _, b4) = create_sub_trie(&store, &[SaltKey::from((0u32, 0u64))]).unwrap(); + let (q4, _, b4) = create_sub_trie(&store, &[SaltKey::from((0u32, 0u64))], None).unwrap(); assert!(verify_ipa_proof(q4)); assert_eq!(b4[&0], 1); // Empty input - let res = create_sub_trie(&store, &[]); + let res = create_sub_trie(&store, &[], None); assert!(res.is_err()); } @@ -749,18 +1033,501 @@ mod tests { // Invalid meta bucket key (slot_id >= META_BUCKET_SIZE) let invalid_meta_key = SaltKey::from((0u32, META_BUCKET_SIZE as u64)); - let result = create_sub_trie(&store, &[invalid_meta_key]); + let result = create_sub_trie(&store, &[invalid_meta_key], None); assert!(matches!(result, Err(ProofError::InvalidSaltKey { .. }))); // Invalid data bucket key (slot_id >= capacity) let data_bucket_id = NUM_META_BUCKETS as u32; let invalid_data_key = SaltKey::from((data_bucket_id, 1000u64)); - let result = create_sub_trie(&store, &[invalid_data_key]); + let result = create_sub_trie(&store, &[invalid_data_key], None); assert!(matches!(result, Err(ProofError::InvalidSaltKey { .. }))); // Valid keys should work let valid_meta_key = SaltKey::from((0u32, 0u64)); - let result = create_sub_trie(&store, &[valid_meta_key]); + let result = create_sub_trie(&store, &[valid_meta_key], None); assert!(result.is_ok()); } + + /// Applies one block's transition to `store` and returns its trie updates. + fn apply_block(store: &MemStore, updates: StateUpdates) -> TrieUpdates { + let (_, trie_updates) = StateRoot::new(store).update_fin(&updates).unwrap(); + store.update_state(updates); + store.update_trie(trie_updates.clone()); + trie_updates + } + + /// A store holding block A (64 random plain kvs), and block B's transition over it (16 + /// value updates, 8 deletes and 16 inserts) computed but not applied. + fn two_block_fixture(seed: u64) -> (MemStore, StateUpdates, TrieUpdates) { + let mut rng = StdRng::seed_from_u64(seed); + let kvs_a: HashMap, Option>> = (0..64) + .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) + .collect(); + let store = MemStore::new(); + let updates_a = EphemeralSaltState::new(&store).update_fin(&kvs_a).unwrap(); + apply_block(&store, updates_a); + + let mut keys_a: Vec> = kvs_a.into_keys().collect(); + keys_a.sort(); + let mut kvs_b: HashMap, Option>> = HashMap::new(); + for key in &keys_a[..16] { + kvs_b.insert(key.clone(), Some(mock_data(&mut rng, 40))); + } + for key in &keys_a[16..24] { + kvs_b.insert(key.clone(), None); + } + for _ in 0..16 { + kvs_b.insert(mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40))); + } + let updates_b = EphemeralSaltState::new(&store).update_fin(&kvs_b).unwrap(); + let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); + (store, updates_b, trie_b) + } + + /// Whether `node` is a leaf of its trie: a main-trie bucket root or a subtree leaf. + fn is_leaf_node(node: NodeId) -> bool { + let leaf_level = if node >> BUCKET_SLOT_BITS == 0 { + MAIN_TRIE_LEVELS - 1 + } else { + MAX_SUBTREE_LEVELS - 1 + }; + !is_encoded_node(node) && get_bfs_level(get_local_number(node)) == leaf_level + } + + /// The polynomial a witness rebuilds from storage for `node`; the root of an expanded + /// bucket is passed encoded, as `parents_and_points` keys it. + fn rebuilt_polynomial(store: &MemStore, node: NodeId) -> LagrangeBasis { + if is_leaf_node(node) { + leaf_polynomial(store, node).unwrap() + } else { + LagrangeBasis::new( + multi_commitments_to_scalars(store, &[(node, BTreeSet::new())]).unwrap(), + ) + } + } + + /// The `(old, new)` pair `trie_updates` records for `node`. + fn transition(trie_updates: &TrieUpdates, node: NodeId) -> (CommitmentBytes, CommitmentBytes) { + trie_updates + .iter() + .find(|(id, _)| *id == node) + .unwrap_or_else(|| panic!("node {node} is not in the transition")) + .1 + } + + /// The nodes of a transition, and the parents of a plan. + fn node_set<'a>(nodes: impl IntoIterator) -> BTreeSet { + nodes.into_iter().copied().collect() + } + + /// The patched positions the plan holds for `node`. + fn planned_positions(plan: &FxHashMap, node: NodeId) -> BTreeSet { + plan[&node].positions.iter().map(|(i, _)| *i).collect() + } + + /// The cache entry of `node` under its post-block commitment. + /// + /// A main-trie bucket root or a subtree node belongs to this fixture's bucket alone, so + /// its entry must be there. The upper main-trie levels are shared by every test in the + /// process, and another fixture's witness may have replaced such an entry with its own + /// commitment in the meantime (a miss, never a wrong entry): those are checked when + /// present. + fn refreshed(node: NodeId, new: &CommitmentBytes) -> Option> { + let entry = node_poly_cache::get(node, new); + let bucket_local = node >> BUCKET_SLOT_BITS != 0 + || get_bfs_level(get_local_number(node)) == MAIN_TRIE_LEVELS - 1; + assert!( + entry.is_some() || !bucket_local, + "node {node} was not refreshed" + ); + entry + } + + /// Every changed node is planned with its own transition, and every entry the refresh + /// inserts equals the polynomial a rebuild from the post-block store produces, for + /// main-trie leaves and internal nodes alike. + #[test] + fn refreshed_polynomials_equal_a_storage_rebuild() { + let (store, updates_b, trie_b) = two_block_fixture(1); + let keys: Vec = updates_b.data.keys().copied().collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let plan = refresh_plan(&refresh); + assert_eq!( + node_set(plan.keys()), + node_set(trie_b.iter().map(|(node, _)| node)) + ); + for &(node, (old, new)) in &trie_b { + assert_eq!( + (plan[&node].old, plan[&node].new), + (old, new), + "node {node}" + ); + } + let (queries, _, _) = create_sub_trie(&store, &keys, Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + + store.update_state(updates_b); + store.update_trie(trie_b.clone()); + let (mut leaves, mut internal) = (0, 0); + for &(node, (_, new)) in &trie_b { + let Some(entry) = refreshed(node, &new) else { + continue; + }; + assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); + if is_leaf_node(node) { + leaves += 1; + } else { + internal += 1; + } + } + assert!( + leaves >= 1 && internal >= 1, + "{leaves} leaves, {internal} internal nodes" + ); + } + + /// In an expanded bucket the refresh keys the subtree top under the bucket root, every + /// other subtree node under its own id and every slot under its subtree leaf. + #[test] + fn refresh_covers_expanded_bucket_subtrees() { + let store = MemStore::new(); + let bucket = NUM_META_BUCKETS as BucketId + 3 * MIN_BUCKET_SIZE as BucketId + 7; + let capacity = 131072u64; + let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); + let meta = |capacity| { + Some(SaltValue::from(BucketMeta { + capacity, + ..BucketMeta::default() + })) + }; + let key = |slot| SaltKey::from((bucket, slot)); + + // Block A: two slots, then an expansion to a three-level subtree (top at level 2). + apply_block( + &store, + StateUpdates { + data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] + .into_iter() + .collect(), + }, + ); + apply_block( + &store, + StateUpdates { + data: [ + ( + bucket_metadata_key(bucket), + (meta(MIN_BUCKET_SIZE as u64), meta(capacity)), + ), + (key(3), (value(1), None)), + (key(2049), (None, value(3))), + (key(capacity - 259), (None, value(4))), + (key(capacity - 1), (None, value(5))), + ] + .into_iter() + .collect(), + }, + ); + + // Block B: two segments under two different subtree children, capacity unchanged. + let updates_b = StateUpdates { + data: [ + (key(2049), (value(3), value(13))), + (key(2050), (None, value(14))), + (key(capacity - 259), (value(4), value(15))), + ] + .into_iter() + .collect(), + }; + let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); + let keys: Vec = updates_b.data.keys().copied().collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + + // The plan keys the top under the bucket root and patches exactly the changed + // children and slots at their positions. + let root = bucket_root_node_id(bucket); + let parent = get_parent_node(&root); + let leaf = |slot| subtree_leaf_for_key(&key(slot)); + let plan = refresh_plan(&refresh); + assert_eq!( + node_set(plan.keys()), + node_set(trie_b.iter().map(|(node, _)| node)) + ); + assert_eq!(planned_positions(&plan, root), [0, 1].into()); + assert_eq!( + planned_positions(&plan, get_parent_node(&leaf(2049))), + [8].into() + ); + assert_eq!( + planned_positions(&plan, get_parent_node(&leaf(capacity - 259))), + [254].into() + ); + assert_eq!(planned_positions(&plan, leaf(2049)), [1, 2].into()); + assert_eq!(planned_positions(&plan, leaf(capacity - 259)), [253].into()); + assert_eq!( + planned_positions(&plan, parent), + [vc_position_in_parent(&root)].into() + ); + + let (queries, _, _) = create_sub_trie(&store, &keys, Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + + store.update_state(updates_b); + store.update_trie(trie_b.clone()); + let (_, new_root) = transition(&trie_b, root); + assert_eq!( + *refreshed(root, &new_root).unwrap(), + rebuilt_polynomial(&store, encode_parent(root, 3)), + "subtree top under the bucket root" + ); + let (mut internal, mut leaves) = (0, 0); + for &(node, (_, new)) in &trie_b { + if node >> BUCKET_SLOT_BITS == 0 { + continue; + } + if is_leaf_node(node) { + leaves += 1; + } else { + internal += 1; + } + assert_eq!( + *refreshed(node, &new).unwrap(), + rebuilt_polynomial(&store, node), + "subtree node {node}" + ); + } + assert_eq!((internal, leaves), (2, 2)); + let (_, new_parent) = transition(&trie_b, parent); + if let Some(entry) = refreshed(parent, &new_parent) { + assert_eq!( + *entry, + rebuilt_polynomial(&store, parent), + "main-trie parent of the bucket root" + ); + } + } + + /// A bucket whose capacity changes is frozen for the block: nothing inside it is + /// refreshed, while its root's position in the main trie and the metadata leaf are; the + /// next witness over the bucket rebuilds from storage. + #[test] + fn refresh_skips_buckets_whose_capacity_changed() { + let store = MemStore::new(); + let bucket = NUM_META_BUCKETS as BucketId + 5 * MIN_BUCKET_SIZE as BucketId + 9; + let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); + let meta = |capacity| { + Some(SaltValue::from(BucketMeta { + capacity, + ..BucketMeta::default() + })) + }; + let key = |slot| SaltKey::from((bucket, slot)); + let meta_key = bucket_metadata_key(bucket); + let root = bucket_root_node_id(bucket); + let parent = get_parent_node(&root); + let meta_root = bucket_root_node_id(meta_key.bucket_id()); + + apply_block( + &store, + StateUpdates { + data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] + .into_iter() + .collect(), + }, + ); + + // Witnesses `witnessed` against the current store with the block's refresh, applies + // the block and checks what was and was not refreshed. + let run = |updates: StateUpdates, witnessed: &[SaltKey]| { + let (_, trie_updates) = StateRoot::new(&store).update_fin(&updates).unwrap(); + let refresh = NodePolyRefresh { + trie_updates: &trie_updates, + state_updates: &updates, + }; + let plan = refresh_plan(&refresh); + assert!( + plan.keys() + .all(|&node| node != root && node >> BUCKET_SLOT_BITS == 0), + "the frozen bucket was planned" + ); + assert_eq!( + planned_positions(&plan, parent), + [vc_position_in_parent(&root)].into() + ); + assert_eq!( + planned_positions(&plan, meta_root), + [(meta_key.slot_id() & SLOT_INDEX_MASK) as usize].into() + ); + + let (queries, _, _) = create_sub_trie(&store, witnessed, Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + store.update_state(updates); + store.update_trie(trie_updates.clone()); + + let (_, new_root) = transition(&trie_updates, root); + assert!( + node_poly_cache::get(root, &new_root).is_none(), + "the frozen bucket's root was refreshed" + ); + for &(node, (_, new)) in &trie_updates { + if node >> BUCKET_SLOT_BITS != 0 { + assert!( + node_poly_cache::get(node, &new).is_none(), + "subtree node {node} of the frozen bucket was refreshed" + ); + } + } + let (_, new_meta_root) = transition(&trie_updates, meta_root); + assert_eq!( + *refreshed(meta_root, &new_meta_root).unwrap(), + rebuilt_polynomial(&store, meta_root), + "metadata leaf" + ); + let (_, new_parent) = transition(&trie_updates, parent); + if let Some(entry) = refreshed(parent, &new_parent) { + assert_eq!( + *entry, + rebuilt_polynomial(&store, parent), + "main-trie parent of the bucket root" + ); + } + }; + + // Block B doubles the capacity, deleting one slot and inserting one beyond the old + // capacity; the witness covers only slots below the old capacity. + run( + StateUpdates { + data: [ + ( + meta_key, + ( + meta(MIN_BUCKET_SIZE as u64), + meta(2 * MIN_BUCKET_SIZE as u64), + ), + ), + (key(3), (value(1), None)), + (key(300), (None, value(3))), + ] + .into_iter() + .collect(), + }, + &[meta_key, key(3), key(5)], + ); + let (queries, _, _) = create_sub_trie(&store, &[key(5), key(300)], None).unwrap(); + assert!(verify_ipa_proof(queries)); + + // Block C contracts it back, removing the slot beyond the new capacity. + run( + StateUpdates { + data: [ + ( + meta_key, + ( + meta(2 * MIN_BUCKET_SIZE as u64), + meta(MIN_BUCKET_SIZE as u64), + ), + ), + (key(300), (value(3), None)), + ] + .into_iter() + .collect(), + }, + &[meta_key, key(5)], + ); + let (queries, _, _) = create_sub_trie(&store, &[key(5)], None).unwrap(); + assert!(verify_ipa_proof(queries)); + } + + /// A retried witness re-applies the block's refresh: its lookups miss on the re-keyed + /// entries, rebuild from the pre-block store and refresh again to the same entries, and + /// the proof bytes do not change. + #[test] + fn refresh_is_idempotent_across_a_retried_witness() { + let (store, updates_b, trie_b) = two_block_fixture(2); + let keys: Vec = updates_b.data.keys().copied().collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let build = || { + SaltProof::create_with_refresh(keys.iter().copied(), &store, Some(&refresh)).unwrap() + }; + let encode = |proof: &SaltProof| { + bincode::serde::encode_to_vec(proof, bincode::config::legacy()).unwrap() + }; + + let first = encode(&build()); + let entries: Vec>> = trie_b + .iter() + .map(|&(node, (_, new))| refreshed(node, &new)) + .collect(); + let second = encode(&build()); + assert_eq!( + first, second, + "a re-applied refresh must not change the proof" + ); + let mut compared = 0; + for (&(node, (_, new)), before) in trie_b.iter().zip(entries) { + if let (Some(after), Some(before)) = (refreshed(node, &new), before) { + assert_eq!(*after, *before, "node {node}"); + compared += 1; + } + } + assert!(compared >= 1); + } + + /// The debug self-check rejects a transition whose new commitment is not what the patched + /// positions commit to. + #[cfg(debug_assertions)] + #[test] + #[should_panic(expected = "does not commit")] + fn refresh_debug_check_rejects_an_inconsistent_transition() { + let (store, updates_b, mut trie_b) = two_block_fixture(3); + let foreign = trie_b + .iter() + .find(|(node, _)| *node != ROOT_NODE_ID) + .unwrap() + .1 + .1; + trie_b + .iter_mut() + .find(|(node, _)| *node == ROOT_NODE_ID) + .unwrap() + .1 + .1 = foreign; + let keys: Vec = updates_b.data.keys().copied().collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let _ = create_sub_trie(&store, &keys, Some(&refresh)); + } + + /// The debug self-check rejects a slot whose new value is not what its leaf's new + /// commitment was computed from. + #[cfg(debug_assertions)] + #[test] + #[should_panic(expected = "does not commit")] + fn refresh_debug_check_rejects_a_wrong_slot_value() { + let (store, mut updates_b, trie_b) = two_block_fixture(4); + let key = *updates_b + .data + .iter() + .find(|(key, (_, new))| !key.is_in_meta_bucket() && new.is_some()) + .unwrap() + .0; + updates_b.data.get_mut(&key).unwrap().1 = Some(SaltValue::new(&[9; 20], &[9; 40])); + let keys: Vec = updates_b.data.keys().copied().collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let _ = create_sub_trie(&store, &keys, Some(&refresh)); + } } diff --git a/salt/src/proof/witness.rs b/salt/src/proof/witness.rs index f7738574..70196e5a 100644 --- a/salt/src/proof/witness.rs +++ b/salt/src/proof/witness.rs @@ -8,6 +8,7 @@ use crate::types::{bucket_id_from_metadata_key, METADATA_KEYS_RANGE}; use crate::{ proof::salt_witness::SaltWitness, + proof::subtrie::NodePolyRefresh, proof::ProofError, state::{hasher, state::EphemeralSaltState}, traits::{StateReader, TrieReader}, @@ -109,6 +110,29 @@ impl Witness { updates: &BTreeMap, Option>>, store: &Store, ) -> Result + where + Store: StateReader + TrieReader, + { + Self::create_with_refresh(bucket_ids, lookups, updates, store, None) + } + + /// [`Self::create`], additionally advancing the process-wide node-polynomial cache to the + /// witnessed block's post-state. + /// + /// The witness proves the parent state, and `refresh` is the block's own transition (its + /// `TrieUpdates` and `StateUpdates`, as `StateRoot::update_fin` produced them from that + /// parent state). After this witness's own lookups, the cache entries of every node the + /// block changed are advanced to the post-state, so the next block's witness hits instead + /// of rebuilding them from storage. Entries stay validated by commitment, so a stale or + /// foreign entry is only ever a miss; and the witness returned here is the same with or + /// without a refresh. + pub fn create_with_refresh<'b, Store>( + bucket_ids: impl IntoIterator, + lookups: impl IntoIterator>, + updates: &BTreeMap, Option>>, + store: &Store, + refresh: Option<&NodePolyRefresh<'_>>, + ) -> Result where Store: StateReader + TrieReader, { @@ -195,7 +219,7 @@ impl Witness { reason: format!("{e:?}"), })?; - let salt_witness = SaltWitness::create(&witnessed_keys, store)?; + let salt_witness = SaltWitness::create_with_refresh(&witnessed_keys, store, refresh)?; Ok(Witness { direct_lookup_tbl, @@ -1200,4 +1224,59 @@ mod tests { assert_eq!(first, second, "a cache hit must not change the proof"); build().verify().unwrap(); } + + /// A witness built with its block's refresh verifies, and the next block's witness over + /// the refreshed entries serializes identically when built twice and still verifies: a + /// wrong refreshed entry on those paths would fail its verification. + #[test] + fn witness_over_refreshed_polynomials_verifies_and_is_stable() { + let mut rng = StdRng::seed_from_u64(11); + let kvs_a: HashMap<_, _> = (0..64) + .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) + .collect(); + let store = MemStore::new(); + let updates_a = EphemeralSaltState::new(&store).update_fin(&kvs_a).unwrap(); + store.update_state(updates_a.clone()); + let (_, trie_a) = StateRoot::new(&store).update_fin(&updates_a).unwrap(); + store.update_trie(trie_a); + + // Block B: 16 value updates, 8 deletes and 16 inserts over block A. + let mut keys_a: Vec> = kvs_a.into_keys().collect(); + keys_a.sort(); + let mut kvs_b: HashMap, Option>> = HashMap::new(); + for key in &keys_a[..16] { + kvs_b.insert(key.clone(), Some(mock_data(&mut rng, 40))); + } + for key in &keys_a[16..24] { + kvs_b.insert(key.clone(), None); + } + for _ in 0..16 { + kvs_b.insert(mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40))); + } + let updates_b = EphemeralSaltState::new(&store).update_fin(&kvs_b).unwrap(); + let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); + let plain_b: BTreeMap<_, _> = kvs_b + .iter() + .map(|(key, value)| (key.clone(), value.clone())) + .collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let witness_b = + Witness::create_with_refresh([], core::iter::empty(), &plain_b, &store, Some(&refresh)) + .unwrap(); + witness_b.verify().unwrap(); + + store.update_state(updates_b); + store.update_trie(trie_b); + let build = || Witness::create([], kvs_b.keys(), &BTreeMap::new(), &store).unwrap(); + let encode = |witness: &Witness| { + bincode::serde::encode_to_vec(&witness.salt_witness, bincode::config::legacy()).unwrap() + }; + let first = encode(&build()); + let second = encode(&build()); + assert_eq!(first, second, "a refreshed entry must not change the proof"); + build().verify().unwrap(); + } } From f4e960ec58a57d38964e2c7d58c28565749a90b1 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 7 Sep 2026 08:31:36 +0800 Subject: [PATCH 04/18] test(proof): pin the cache refresh's fallback base, coverage, and pre-state guard Six tests over `NodePolyRefresh` in subtrie.rs, each pinning a path that a mutation of the refresh left the suite blind to: - `refresh_uses_cached_bases_for_parents_the_witness_did_not_read`: a planned parent out of the witness's hand is skipped while the cache holds no entry for its old commitment, and refreshed from that entry once a pre-state witness has cached it. - `refresh_applies_every_planned_parent`: with every base in hand, `apply_refresh` inserts exactly one entry per planned parent, each equal to the post-block rebuild. - `witness_refresh_reports_every_planned_parent` (`perf`): a witness over every changed key reports `refreshed_nodes == plan.len()` on the calling thread, race-free against the shared cache. - `refresh_rejects_a_witness_over_the_post_state` (debug): the `read_at_old` guard fires with its own message on a witness over the post-state. - `refresh_skips_a_parent_read_at_a_foreign_commitment` (release): a witness over the pre-B view carrying block C's transition patches nothing, so the bucket root stays absent under C's commitment. - `refresh_patches_a_rehashed_bucket_with_unchanged_capacity`: a nonce-only rehash of a two-level, 512-slot bucket is not frozen; its leaves, top and metadata leaf are planned at the moved slots and refreshed. Each test fails under the mutation it targets (dropping the cache fallback, skipping level-1/2 parents, freezing on any metadata change, accepting any read commitment) and passes on the code as it is. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01722A9KuCsH6MnM83Mhax3Y --- salt/src/proof/subtrie.rs | 286 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 286 insertions(+) diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 968b8c1c..bd40f1d0 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -1143,6 +1143,15 @@ mod tests { entry } + /// Whether `node` is the main-trie root of a data bucket. Fixtures do not share one: random + /// keys spread over sixteen million buckets, and hand-picked buckets differ between tests. + /// Its cache entry is therefore the current test's own, while every other class of node + /// (upper main-trie levels, metadata leaves) is shared with the rest of the process and + /// checked only when present. + fn is_data_bucket_root(node: NodeId) -> bool { + node >> BUCKET_SLOT_BITS == 0 && node >= bucket_root_node_id(NUM_META_BUCKETS as BucketId) + } + /// Every changed node is planned with its own transition, and every entry the refresh /// inserts equals the polynomial a rebuild from the post-block store produces, for /// main-trie leaves and internal nodes alike. @@ -1530,4 +1539,281 @@ mod tests { }; let _ = create_sub_trie(&store, &keys, Some(&refresh)); } + + /// A planned parent the witness did not read takes its base from the cache's entry for its + /// old commitment, and is skipped while the cache holds none: after a refresh whose witness + /// covered one bucket, the other changed buckets' roots are absent under their new + /// commitment, and present, equal to the post-block rebuild, once a witness over the + /// pre-state has cached their bases. + #[test] + fn refresh_uses_cached_bases_for_parents_the_witness_did_not_read() { + let (store, updates_b, trie_b) = two_block_fixture(5); + let keys: Vec = updates_b.data.keys().copied().collect(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let bucket = keys + .iter() + .find(|key| !key.is_in_meta_bucket()) + .unwrap() + .bucket_id(); + let witnessed: Vec = keys + .iter() + .copied() + .filter(|key| key.bucket_id() == bucket) + .collect(); + let unread_roots: Vec<(NodeId, CommitmentBytes)> = trie_b + .iter() + .filter(|&&(node, _)| is_data_bucket_root(node) && node != bucket_root_node_id(bucket)) + .map(|&(node, (_, new))| (node, new)) + .collect(); + assert!( + unread_roots.len() >= 8, + "{} unread roots", + unread_roots.len() + ); + + // Nothing holds those roots' pre-state polynomials yet, so they are skipped. + let (queries, _, _) = create_sub_trie(&store, &witnessed, Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + for &(node, new) in &unread_roots { + assert!( + node_poly_cache::get(node, &new).is_none(), + "node {node} was refreshed without a base" + ); + } + + // A witness over the pre-state caches every parent under its old commitment. + create_sub_trie(&store, &keys, None).unwrap(); + let (queries, _, _) = create_sub_trie(&store, &witnessed, Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + + store.update_state(updates_b); + store.update_trie(trie_b.clone()); + for &(node, new) in &unread_roots { + let entry = node_poly_cache::get(node, &new) + .unwrap_or_else(|| panic!("node {node} was not refreshed from its cached base")); + assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); + } + for &(node, (_, new)) in &trie_b { + if let Some(entry) = node_poly_cache::get(node, &new) { + assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); + } + } + } + + /// Every planned parent whose base is in hand at its old commitment is applied: the + /// refresh inserts one entry per planned parent, skipping none, and each entry equals the + /// polynomial a rebuild from the post-block store produces. + #[test] + fn refresh_applies_every_planned_parent() { + let (store, updates_b, trie_b) = two_block_fixture(9); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let plan = refresh_plan(&refresh); + let planned = plan.len(); + let mut in_hand = FxHashMap::default(); + let mut parent_bytes = FxHashMap::default(); + for &node in plan.keys() { + in_hand.insert(node, Arc::new(rebuilt_polynomial(&store, node))); + parent_bytes.insert(node, store.commitment(node).unwrap()); + } + assert_eq!(apply_refresh(plan, &in_hand, &parent_bytes), planned); + + store.update_state(updates_b); + store.update_trie(trie_b.clone()); + for &(node, (_, new)) in &trie_b { + let entry = node_poly_cache::get(node, &new); + assert!( + entry.is_some() || !is_data_bucket_root(node), + "node {node} was not refreshed" + ); + if let Some(entry) = entry { + assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); + } + } + } + + /// The witness must read the transition's pre-state: over the post-state store every + /// parent is read at its new commitment, which the debug guard rejects before any entry + /// is derived from it. + #[cfg(debug_assertions)] + #[test] + #[should_panic(expected = "other than the refresh's pre-state")] + fn refresh_rejects_a_witness_over_the_post_state() { + let (store, updates_b, trie_b) = two_block_fixture(6); + let keys: Vec = updates_b.data.keys().copied().collect(); + store.update_state(updates_b.clone()); + store.update_trie(trie_b.clone()); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + let _ = create_sub_trie(&store, &keys, Some(&refresh)); + } + + /// A parent the witness read at a commitment other than the refresh's old one is skipped, + /// never patched: a witness over the pre-B view carrying block C's transition (whose old + /// commitments are B's) leaves the bucket root absent under C's commitment, and inserts + /// nothing that differs from the post-C rebuild. Debug builds reject such a witness + /// outright instead (`refresh_rejects_a_witness_over_the_post_state`). + #[cfg(not(debug_assertions))] + #[test] + fn refresh_skips_a_parent_read_at_a_foreign_commitment() { + let store = MemStore::new(); + let bucket = NUM_META_BUCKETS as BucketId + 7 * MIN_BUCKET_SIZE as BucketId + 11; + let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); + let key = |slot| SaltKey::from((bucket, slot)); + + // Blocks A and B change slot 3; block C, computed over B, changes slot 5. + apply_block( + &store, + StateUpdates { + data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] + .into_iter() + .collect(), + }, + ); + let pre_b = store.clone(); + apply_block( + &store, + StateUpdates { + data: [(key(3), (value(1), value(3)))].into_iter().collect(), + }, + ); + let updates_c = StateUpdates { + data: [(key(5), (value(2), value(4)))].into_iter().collect(), + }; + let (_, trie_c) = StateRoot::new(&store).update_fin(&updates_c).unwrap(); + let refresh = NodePolyRefresh { + trie_updates: &trie_c, + state_updates: &updates_c, + }; + + let (queries, _, _) = create_sub_trie(&pre_b, &[key(3), key(5)], Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + + store.update_state(updates_c); + store.update_trie(trie_c.clone()); + let root = bucket_root_node_id(bucket); + let (_, new_root) = transition(&trie_c, root); + assert!( + node_poly_cache::get(root, &new_root).is_none(), + "a parent read at a foreign commitment was patched" + ); + for &(node, (_, new)) in &trie_c { + if let Some(entry) = node_poly_cache::get(node, &new) { + assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); + } + } + } + + /// A metadata change that keeps the capacity (a rehash under a new nonce) does not freeze + /// the bucket: over a two-level, 512-slot subtree the moved slots are patched in their + /// leaves, the changed leaves in the top under the bucket root, and the metadata leaf at + /// the bucket's slot. + #[test] + fn refresh_patches_a_rehashed_bucket_with_unchanged_capacity() { + let store = MemStore::new(); + let bucket = NUM_META_BUCKETS as BucketId + 9 * MIN_BUCKET_SIZE as BucketId + 13; + let capacity = 2 * MIN_BUCKET_SIZE as u64; + let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); + let meta = |nonce| { + Some(SaltValue::from(BucketMeta { + nonce, + capacity, + ..BucketMeta::default() + })) + }; + let key = |slot| SaltKey::from((bucket, slot)); + let meta_key = bucket_metadata_key(bucket); + let root = bucket_root_node_id(bucket); + let meta_root = bucket_root_node_id(meta_key.bucket_id()); + + // Block A: a 512-slot bucket holding two values in its first leaf and one in its + // second. + apply_block( + &store, + StateUpdates { + data: [ + (meta_key, (Some(BucketMeta::default().into()), meta(0))), + (key(3), (None, value(1))), + (key(5), (None, value(2))), + (key(300), (None, value(3))), + ] + .into_iter() + .collect(), + }, + ); + + // Block B: a rehash under a new nonce moves the values, capacity unchanged. + let updates_b = EphemeralSaltState::new(&store) + .set_nonce(bucket, 1) + .unwrap(); + assert_eq!(updates_b.data[&meta_key], (meta(0), meta(1))); + let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); + let refresh = NodePolyRefresh { + trie_updates: &trie_b, + state_updates: &updates_b, + }; + + // Every changed node is planned; the leaves at their moved slots, the top at the + // changed leaves, the metadata leaf at the bucket's slot. + let mut moved: BTreeMap> = BTreeMap::new(); + for key in updates_b.data.keys().filter(|key| !key.is_in_meta_bucket()) { + moved + .entry(subtree_leaf_for_key(key)) + .or_default() + .insert((key.slot_id() & SLOT_INDEX_MASK) as usize); + } + assert_eq!(moved.len(), 2, "both leaves change"); + let plan = refresh_plan(&refresh); + assert_eq!( + node_set(plan.keys()), + node_set(trie_b.iter().map(|(node, _)| node)) + ); + for (leaf, slots) in &moved { + assert_eq!(planned_positions(&plan, *leaf), *slots, "leaf {leaf}"); + } + assert_eq!( + planned_positions(&plan, root), + moved.keys().map(vc_position_in_parent).collect() + ); + assert_eq!( + planned_positions(&plan, meta_root), + [(meta_key.slot_id() & SLOT_INDEX_MASK) as usize].into() + ); + + let keys: Vec = updates_b.data.keys().copied().collect(); + let (queries, _, _) = create_sub_trie(&store, &keys, Some(&refresh)).unwrap(); + assert!(verify_ipa_proof(queries)); + + store.update_state(updates_b); + store.update_trie(trie_b.clone()); + let (_, new_root) = transition(&trie_b, root); + assert_eq!( + *refreshed(root, &new_root).unwrap(), + rebuilt_polynomial(&store, encode_parent(root, 2)), + "subtree top under the bucket root" + ); + for leaf in moved.keys() { + let (_, new) = transition(&trie_b, *leaf); + assert_eq!( + *refreshed(*leaf, &new).unwrap(), + rebuilt_polynomial(&store, *leaf), + "leaf {leaf}" + ); + } + let (_, new_meta_root) = transition(&trie_b, meta_root); + if let Some(entry) = node_poly_cache::get(meta_root, &new_meta_root) { + assert_eq!( + *entry, + rebuilt_polynomial(&store, meta_root), + "metadata leaf" + ); + } + } } From 6cfc1cd9fad0b3ea2551899b6eeabe25b0b355a1 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 7 Sep 2026 08:33:13 +0800 Subject: [PATCH 05/18] docs(proof): describe the refresh's invariants in place The refresh doc comments referred to numbered invariants of a design note outside the repository; the sentences now stand on their own. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01722A9KuCsH6MnM83Mhax3Y --- salt/src/proof/subtrie.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index bd40f1d0..4e2831dc 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -118,7 +118,7 @@ mod node_poly_cache { /// The store the witness reads must be the transition's pre-state, the view `update_fin` ran /// on; a parent the witness read at any other commitment is skipped. Applying one transition /// again (a retried witness) derives the same polynomial from a base validated at the same -/// old commitment and inserts the same entry, so a refresh is idempotent (I8). +/// old commitment and inserts the same entry, so a refresh is idempotent. pub struct NodePolyRefresh<'a> { /// The block's changed nodes as `(node, (old commitment, new commitment))`. pub trie_updates: &'a [(NodeId, (CommitmentBytes, CommitmentBytes))], @@ -139,11 +139,11 @@ struct ParentPatch { /// ([`connect_parent_id`]): a bucket whose subtree top changed is keyed by its main-trie /// bucket root, like the witness keys it. /// -/// The plan is complete for every parent it emits (I2, I3): every node whose commitment +/// The plan is complete for every parent it emits: every node whose commitment /// changed is in `trie_updates`, so a parent's unchanged positions keep the base's values and /// its changed positions are exactly its changed children (internal node) or changed slots /// (leaf). Shapes where a per-position patch is wrong under a valid commitment are excluded -/// whole (I5): a bucket whose capacity changes is frozen and every patch inside it is dropped, +/// whole: a bucket whose capacity changes is frozen and every patch inside it is dropped, /// while the bucket root's position in its main-trie parent is still patched, because that /// `(old, new)` pair is a true transition of the main-trie node. fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap { @@ -186,7 +186,7 @@ fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap .collect(); // Changed children, keyed by the physical id of the parent whose polynomial holds them - // (I4). A changed subtree node is recorded under its own id unless it is the top, which + //. A changed subtree node is recorded under its own id unless it is the top, which // the trie records under the bucket root; so a subtree child's parent is the top exactly // when the parent is absent from the transition. let mut child_patches: Vec<(NodeId, usize, CommitmentBytes)> = @@ -289,9 +289,9 @@ fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap /// /// The base is the witness's in-hand polynomial when the witness read the parent at exactly /// the old commitment, else the cached entry for the old commitment, else the parent is -/// skipped (I1: an entry is only ever derived from a base validated at `old`). Every +/// skipped (an entry is only ever derived from a base validated at `old`). Every /// insert goes through the shard write lock as a whole `(commitment, polynomial)` pair, so -/// concurrent witnesses can only ever replace an entry with another valid one (I6). +/// concurrent witnesses can only ever replace an entry with another valid one. fn apply_refresh( plan: FxHashMap, in_hand: &FxHashMap>, @@ -332,7 +332,7 @@ fn apply_refresh( refreshed } -/// Debug-build self-check of one refreshed entry (I10): the patched positions must carry the +/// Debug-build self-check of one refreshed entry: the patched positions must carry the /// parent's commitment from `old` to exactly `new`, `old + Σ G_i·(s_i − base_i) == new`, /// which is how the trie produced `new`. The check reads no store and fails on a wrong /// position, a wrong scalar, and a missing or extra position, which commitment validation @@ -643,7 +643,7 @@ where }) }; - // Every resolved polynomial by physical parent id: the bases a refresh patches (I7: the + // Every resolved polynomial by physical parent id: the bases a refresh patches (the // witness reads the pre-state, so these are the polynomials the block's transition // transforms). let mut in_hand: FxHashMap> = FxHashMap::default(); @@ -736,7 +736,7 @@ where } // Step 7: Advance the cache to the witnessed block's post-state. The queries above are - // already built from the pre-state polynomials, so this proof is unaffected (I9); it runs + // already built from the pre-state polynomials, so this proof is unaffected; it runs // here rather than after the proof so the entries land before the next block's witness // looks them up. if let Some(refresh) = refresh { From d357608e784612ed78e30eb79b9a520b20ca3112 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 14 Sep 2026 07:44:12 +0800 Subject: [PATCH 06/18] refactor(proof): simplify the node-polynomial cache paths and share test fixtures Quality cleanup of the proof-opt changes; proof and witness bytes are unchanged (blake3 digests of three serializations match before/after). - subtrie: one resolve_polys helper replaces the duplicated internal/leaf cache blocks; in_hand carries the commitment each base was read at and is only filled with a refresh, so apply_refresh drops its parent_bytes parameter; multi_commitments_to_scalars takes node ids; empty_slot_scalar and slot_position are shared by refresh_plan and leaf_polynomial; the test-only query shims are gone; node_poly_cache::insert evicts only for a new node and frees evicted or replaced entries outside the shard lock. - ipa-multipoint: open_with_committer takes &CRS (no CRS clone per proof); create_with_precomp uses a plain flag for Q in the tables and reuses its per-round term buffers; commit_dense falls back to commit_lagrange_poly; check calls multi_scalar_mul directly and multi_scalar_mul_par is deprecated; LagrangeBasis::values is public. - DEFAULT_CRS lives in trie.rs next to the committer built from it, so the trie no longer depends on proof::prover. - tests/bench: apply_block and two_block_fixture move to proof::test_utils and back the witness.rs refresh test; shared value/meta/store_with_two_slots helpers; the check bench builds its data with SaltWitness::create. Co-Authored-By: Claude Opus 5 (1M context) --- ipa-multipoint/src/ipa.rs | 28 +- ipa-multipoint/src/lagrange_basis.rs | 2 +- ipa-multipoint/src/multiproof.rs | 40 +- salt/benches/proof.rs | 32 +- salt/src/proof/prover.rs | 15 +- salt/src/proof/shape.rs | 25 +- salt/src/proof/subtrie.rs | 551 ++++++++++++--------------- salt/src/proof/test_utils.rs | 56 ++- salt/src/proof/witness.rs | 58 +-- salt/src/trie/trie.rs | 9 +- 10 files changed, 385 insertions(+), 431 deletions(-) diff --git a/ipa-multipoint/src/ipa.rs b/ipa-multipoint/src/ipa.rs index 20e5325d..2032f812 100644 --- a/ipa-multipoint/src/ipa.rs +++ b/ipa-multipoint/src/ipa.rs @@ -232,16 +232,18 @@ pub fn create_with_precomp( let w = transcript.challenge_scalar(b"w"); // Tables that cover one base past the vector carry `Q` there (the salt trie's shared // committer does), which turns the blinding terms `z·(w·Q) = (w·z)·Q` into fixed-base - // multiplications as well. Otherwise `Q` is folded with one variable-base product. - let q_index = (precomp.num_bases() > n).then_some(n); + // multiplications as well. Otherwise `Q` takes one variable-base product per term. + let q_in_tables = precomp.num_bases() > n; debug_assert!( - q_index.is_none_or(|i| precomp.mul_index(&Fr::one(), i) == q), + !q_in_tables || precomp.mul_index(&Fr::one(), n) == q, "committer base {n} is not the blinding generator Q" ); - let Q = q_index.is_none().then(|| q * w); - let blind = |z: Fr| match q_index { - Some(i) => precomp.mul_index(&(w * z), i), - None => Q.expect("Q is folded whenever the tables lack it") * z, + let blind = |z: Fr| { + if q_in_tables { + precomp.mul_index(&(w * z), n) + } else { + q * (w * z) + } }; let num_rounds = log2(n); @@ -253,6 +255,9 @@ pub fn create_with_precomp( // implicitly folded generator vector. let mut coeff = vec![Fr::one(); n]; let mut len = n; + // This round's L/R terms over the original generators; each index lands in one of them. + let mut l_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); + let mut r_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); for _k in 0..num_rounds { let half = len / 2; @@ -261,8 +266,8 @@ pub fn create_with_precomp( // Express this round's L/R in terms of the original generators. Every // original index lands in exactly one of the two point sets. - let mut l_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); - let mut r_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); + l_terms.clear(); + r_terms.clear(); for (m, c) in coeff.iter().enumerate() { let i = m % len; if i < half { @@ -315,12 +320,12 @@ pub fn create_with_precomp( } } -/// Sums `scalar · G[index]` over the given terms using precomputed wNAF -/// tables, splitting the terms across threads. /// Smallest number of terms one parallel task takes: below this the rayon dispatch costs more /// than the fixed-base multiplications it hands out, and concurrent proofs only contend. const MSM_MIN_CHUNK: usize = 8; +/// Sums `scalar · G[index]` over the given terms using precomputed wNAF +/// tables, splitting the terms across threads. pub(crate) fn fixed_base_msm(precomp: &Committer, terms: &[(usize, Fr)]) -> Element { let chunk_size = terms.len().div_ceil(num_threads!()).max(MSM_MIN_CHUNK); chunks!(terms, chunk_size) @@ -436,6 +441,7 @@ pub fn slow_vartime_multiscalar_mul<'a>( multi_scalar_mul(&points, &scalars) } +#[deprecated(note = "no longer chunks its input; call `banderwagon::multi_scalar_mul`")] pub fn multi_scalar_mul_par(bases: &[Element], scalars: &[Fr]) -> Element { // `multi_scalar_mul` parallelizes internally across Pippenger windows; // chunking the input here would multiply the window/doubling work per diff --git a/ipa-multipoint/src/lagrange_basis.rs b/ipa-multipoint/src/lagrange_basis.rs index 6ace3c7f..27df8cf6 100644 --- a/ipa-multipoint/src/lagrange_basis.rs +++ b/ipa-multipoint/src/lagrange_basis.rs @@ -236,7 +236,7 @@ impl LagrangeBasis { self.values[index] } - pub(crate) fn values(&self) -> &[Fr] { + pub fn values(&self) -> &[Fr] { &self.values } diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index 0aa206d9..d32eb04c 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -2,14 +2,14 @@ #![allow(non_snake_case)] use crate::crs::CRS; -use crate::ipa::{multi_scalar_mul_par, slow_vartime_multiscalar_mul, IPAProof}; +use crate::ipa::IPAProof; use crate::lagrange_basis::{LagrangeBasis, PrecomputedWeights}; use crate::math_utils::powers_of_par; use crate::transcript::Transcript; use crate::transcript::TranscriptProtocol; -use banderwagon::{salt_committer::Committer, trait_defs::*, Element, Fr}; +use banderwagon::{multi_scalar_mul, salt_committer::Committer, trait_defs::*, Element, Fr}; use hashbrown::HashMap; use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; @@ -77,7 +77,7 @@ impl MultiPoint { transcript: &mut Transcript, queries: Vec, ) -> MultiPointProof { - Self::open_inner(crs, None, precomp, transcript, queries) + Self::open_inner(&crs, None, precomp, transcript, queries) } /// Same proof as [`MultiPoint::open`] (byte-identical output), but every @@ -87,9 +87,10 @@ impl MultiPoint { /// per-round generator folding and the small variable-base MSMs, which /// dominate proving time. /// - /// `committer` must hold tables for the CRS `G` vector in order. + /// `committer` must hold tables for the CRS `G` vector in order; tables that also hold + /// `Q` as base `crs.n` make the IPA's blinding terms fixed-base too. pub fn open_with_committer( - crs: CRS, + crs: &CRS, committer: &Committer, precomp: &PrecomputedWeights, transcript: &mut Transcript, @@ -99,7 +100,7 @@ impl MultiPoint { } fn open_inner( - crs: CRS, + crs: &CRS, committer: Option<&Committer>, precomp: &PrecomputedWeights, transcript: &mut Transcript, @@ -132,14 +133,7 @@ impl MultiPoint { chunk .iter() .map(|(point, queries_challenges)| { - let domain = queries_challenges - .first() - .expect("point group cannot be empty") - .0 - .poly - .values() - .len(); - let mut aggregated = vec![Fr::zero(); domain]; + let mut aggregated = vec![Fr::zero(); crs.n]; for (query, challenge) in queries_challenges.iter() { for (acc, coeff) in aggregated.iter_mut().zip(query.poly.values()) { *acc += **challenge * coeff; @@ -161,7 +155,7 @@ impl MultiPoint { |a, b| a + b ); - let g_x_comm = commit_dense(&crs, committer, g_x.values()); + let g_x_comm = commit_dense(crs, committer, &g_x); transcript.append_point(b"D", &g_x_comm); @@ -192,7 +186,7 @@ impl MultiPoint { |a, b| a + b ); - let g1_comm = commit_dense(&crs, committer, g1_x.values()); + let g1_comm = commit_dense(crs, committer, &g1_x); transcript.append_point(b"E", &g1_comm); @@ -208,7 +202,9 @@ impl MultiPoint { let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, t); crate::ipa::create_with_precomp(transcript, committer, crs.Q, a, g_3_x_comm, b, t) } - None => open_point_outside_of_domain(crs, precomp, transcript, g_3_x, g_3_x_comm, t), + None => { + open_point_outside_of_domain(crs.clone(), precomp, transcript, g_3_x, g_3_x_comm, t) + } }; MultiPointProof { @@ -220,13 +216,13 @@ impl MultiPoint { /// Commits to a dense polynomial in Lagrange basis: with precomputed tables /// when available, otherwise via the generic variable-base MSM. -fn commit_dense(crs: &CRS, committer: Option<&Committer>, values: &[Fr]) -> Element { +fn commit_dense(crs: &CRS, committer: Option<&Committer>, poly: &LagrangeBasis) -> Element { match committer { Some(committer) => { - let terms: Vec<(usize, Fr)> = values.iter().copied().enumerate().collect(); + let terms: Vec<(usize, Fr)> = poly.values().iter().copied().enumerate().collect(); crate::ipa::fixed_base_msm(committer, &terms) } - None => slow_vartime_multiscalar_mul(values.iter(), crs.G.iter()), + None => crs.commit_lagrange_poly(poly), } } @@ -388,7 +384,7 @@ impl MultiPointProof { } } - let g1_comm = multi_scalar_mul_par(&comms, &comm_scalars); + let g1_comm = multi_scalar_mul(&comms, &comm_scalars); transcript.append_point(b"E", &g1_comm); @@ -697,7 +693,7 @@ mod tests { queries.clone(), ); let accelerated = MultiPoint::open_with_committer( - crs.clone(), + &crs, &committer, &precomp, &mut Transcript::new(b"st"), diff --git a/salt/benches/proof.rs b/salt/benches/proof.rs index 4a409532..25a9ee99 100644 --- a/salt/benches/proof.rs +++ b/salt/benches/proof.rs @@ -13,9 +13,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use rand::{rngs::StdRng, Rng, SeedableRng}; -use salt::{ - traits::StateReader, types::*, EphemeralSaltState, MemStore, SaltProof, StateRoot, Witness, -}; +use salt::{types::*, EphemeralSaltState, MemStore, SaltProof, SaltWitness, StateRoot, Witness}; use std::collections::BTreeMap; use std::hint::black_box; use std::sync::OnceLock; @@ -88,25 +86,6 @@ fn proof_keys(s: &Setup, n: usize) -> Vec { keys } -/// Builds the kv data map the verifier is given for `keys`. -fn proof_data(s: &Setup, keys: &[SaltKey]) -> BTreeMap> { - keys.iter() - .map(|&k| { - let v = if k.is_in_meta_bucket() { - Some( - s.store - .metadata(bucket_id_from_metadata_key(k)) - .unwrap() - .into(), - ) - } else { - s.store.value(k).unwrap() - }; - (k, v) - }) - .collect() -} - fn bench_salt_proof(c: &mut Criterion) { let s = setup(); @@ -126,13 +105,12 @@ fn bench_salt_proof(c: &mut Criterion) { group.measurement_time(Duration::from_secs(10)); for n in [16usize, 256, 2048] { let keys = proof_keys(s, n); - let proof = SaltProof::create(keys.iter().copied(), &s.store).unwrap(); - let data = proof_data(s, &keys); + let SaltWitness { kvs, proof } = SaltWitness::create(&keys, &s.store).unwrap(); group.bench_with_input( BenchmarkId::from_parameter(n), - &(proof, data), - |b, (proof, data)| { - b.iter(|| proof.check(black_box(data), s.root).unwrap()); + &(proof, kvs), + |b, (proof, kvs)| { + b.iter(|| proof.check(black_box(kvs), s.root).unwrap()); }, ); } diff --git a/salt/src/proof/prover.rs b/salt/src/proof/prover.rs index 667e8d5d..2bd97477 100644 --- a/salt/src/proof/prover.rs +++ b/salt/src/proof/prover.rs @@ -9,14 +9,13 @@ use crate::{ traits::{StateReader, TrieReader}, trie::{ node_utils::{get_child_node, subtree_leaf_start_key}, - trie::kv_hash, + trie::{kv_hash, shared_committer, DEFAULT_CRS}, }, types::{hash_commitment, CommitmentBytes, NodeId, SaltKey, SaltValue}, BucketId, ScalarBytes, }; use banderwagon::{Element, Fr}; use ipa_multipoint::{ - crs::CRS, lagrange_basis::PrecomputedWeights, multiproof::{MultiPoint, MultiPointProof, VerifierQuery}, transcript::Transcript, @@ -42,12 +41,6 @@ type FxHashMap = HashMap; pub static PRECOMPUTED_WEIGHTS: Lazy = Lazy::new(|| PrecomputedWeights::new(DOMAIN_SIZE)); -/// Shared default CRS. Constructing `CRS::default()` decompresses 257 points, -/// each costing a modular square root plus a subgroup check (several -/// milliseconds total), so it is done once per process and reused by every -/// proof creation and verification. -pub static DEFAULT_CRS: Lazy = Lazy::new(CRS::default); - /// Serde wrapper for banderwagon `Element` with validation and compression. /// /// This type ensures security by validating elements during deserialization @@ -255,9 +248,9 @@ impl SaltProof { // Reuse the shared CRS (deriving it decompresses 257 points) and the // trie's fixed-base tables for all MSMs over the CRS generators. - let committer = crate::trie::trie::shared_committer(); + let committer = shared_committer(); let proof = MultiPoint::open_with_committer( - DEFAULT_CRS.clone(), + &DEFAULT_CRS, &committer, &PRECOMPUTED_WEIGHTS, &mut transcript, @@ -616,7 +609,7 @@ mod tests { BucketMeta, }; use banderwagon::{CanonicalSerialize, PrimeField}; - use ipa_multipoint::lagrange_basis::LagrangeBasis; + use ipa_multipoint::{crs::CRS, lagrange_basis::LagrangeBasis}; use rand::{rngs::StdRng, SeedableRng}; use std::vec; diff --git a/salt/src/proof/shape.rs b/salt/src/proof/shape.rs index 6ff278f0..103bc15a 100644 --- a/salt/src/proof/shape.rs +++ b/salt/src/proof/shape.rs @@ -81,24 +81,17 @@ pub(crate) fn parents_and_points( }) .collect(); - let mut merged = (BTreeMap::new(), BTreeMap::new()); - for (internal_map, slot_map) in partials { - for (node_id, positions) in internal_map { - merged - .0 - .entry(node_id) - .or_insert_with(BTreeSet::new) - .extend(positions); - } - for (node_id, positions) in slot_map { - merged - .1 - .entry(node_id) - .or_insert_with(BTreeSet::new) - .extend(positions); + let merge = |into: &mut BTreeMap>, from| { + for (node_id, positions) in from { + into.entry(node_id).or_default().extend(positions); } + }; + let (mut internal_nodes, mut slot_position_nodes) = (BTreeMap::new(), BTreeMap::new()); + for (internal_map, slot_map) in partials { + merge(&mut internal_nodes, internal_map); + merge(&mut slot_position_nodes, slot_map); } - merged + (internal_nodes, slot_position_nodes) } /// Records the authentication-path nodes and slot positions for one key into diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 4e2831dc..e42e0a41 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -41,7 +41,7 @@ use banderwagon::{Element, Fr, Zero}; use ipa_multipoint::{lagrange_basis::LagrangeBasis, multiproof::ProverQuery}; use salt_macros::prelude::*; -use salt_macros::{chunks, into_iter, num_threads}; +use salt_macros::{chunks, iter, num_threads}; use std::collections::{BTreeMap, BTreeSet}; use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; @@ -66,7 +66,6 @@ const MIN_NODE_CHUNK: usize = 4; /// that fills up is cleared whole; the working set refills within a few proofs. mod node_poly_cache { use super::*; - use crate::types::CommitmentBytes; use crate::Lazy; use spin::RwLock; @@ -96,10 +95,17 @@ mod node_poly_cache { pub(super) fn insert(node: NodeId, commitment: CommitmentBytes, poly: Arc) { let mut guard = shard(node).write(); - if guard.len() >= MAX_PER_SHARD { - guard.clear(); - } - guard.insert(node, (commitment, poly)); + // Only a new node grows the shard. Whatever the insert evicts or replaces is dropped + // after the lock is released, so readers do not wait on the frees. + let evicted = (guard.len() >= MAX_PER_SHARD && !guard.contains_key(&node)).then(|| { + core::mem::replace( + &mut *guard, + FxHashMap::with_capacity_and_hasher(MAX_PER_SHARD, FxBuildHasher), + ) + }); + let replaced = guard.insert(node, (commitment, poly)); + drop(guard); + drop((evicted, replaced)); } } @@ -185,8 +191,8 @@ fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap .map(|node| (node >> BUCKET_SLOT_BITS) as BucketId) .collect(); - // Changed children, keyed by the physical id of the parent whose polynomial holds them - //. A changed subtree node is recorded under its own id unless it is the top, which + // Changed children, keyed by the physical id of the parent whose polynomial holds them. + // A changed subtree node is recorded under its own id unless it is the top, which // the trie records under the bucket root; so a subtree child's parent is the top exactly // when the parent is absent from the transition. let mut child_patches: Vec<(NodeId, usize, CommitmentBytes)> = @@ -246,10 +252,9 @@ fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap // An emptied slot takes the bucket kind's default, as `leaf_polynomial` fills it. let scalar = match new { Some(_) => slot_to_field(new), - None if key.is_in_meta_bucket() => slot_to_field(&Some(BucketMeta::default().into())), - None => slot_to_field(&None), + None => empty_slot_scalar(key.bucket_id()), }; - leaf_patches.push((leaf, (key.slot_id() & SLOT_INDEX_MASK) as usize, scalar)); + leaf_patches.push((leaf, slot_position(key), scalar)); } // One batch maps every changed child commitment to its scalar; it is the same map the @@ -294,16 +299,15 @@ fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap /// concurrent witnesses can only ever replace an entry with another valid one. fn apply_refresh( plan: FxHashMap, - in_hand: &FxHashMap>, - parent_bytes: &FxHashMap, + in_hand: &FxHashMap)>, ) -> usize { let mut refreshed = 0; for (physical, patch) in plan { let base = match in_hand.get(&physical) { - Some(poly) => { + Some((read, poly)) => { // The witness resolved this polynomial against the commitment it read, so // it is the pre-state base only if that commitment is the transition's old. - let read_at_old = parent_bytes.get(&physical) == Some(&patch.old); + let read_at_old = *read == patch.old; debug_assert!( read_at_old, "node {physical}: the witness read a commitment other than the refresh's pre-state" @@ -318,9 +322,7 @@ fn apply_refresh( None => continue, }, }; - let mut values: Vec = (0..DOMAIN_SIZE) - .map(|i| base.evaluate_in_domain(i)) - .collect(); + let mut values = base.values().to_vec(); #[cfg(debug_assertions)] assert_patch_commits(physical, &patch, &values); for &(position, scalar) in &patch.positions { @@ -373,7 +375,7 @@ type SubTrieInfo = ( /// # Parameters /// /// * `store` - Storage backend providing access to trie node commitments -/// * `nodes` - Internal nodes with their evaluation points (child indices to prove) +/// * `nodes` - Internal nodes (logical ids) whose children to convert /// /// # Returns /// @@ -390,10 +392,7 @@ type SubTrieInfo = ( /// # Errors /// /// Returns `ProofError::StateReadError` if unable to read child node commitments from storage. -fn multi_commitments_to_scalars( - store: &Store, - nodes: &[(NodeId, BTreeSet)], -) -> ProofResult> +fn multi_commitments_to_scalars(store: &Store, nodes: &[NodeId]) -> ProofResult> where Store: TrieReader, { @@ -413,10 +412,11 @@ where }; let mut scalars = vec![Fr::zero(); nodes.len() * DOMAIN_SIZE]; - // Children present in storage: (position in `scalars`, commitment) - let mut real_children: Vec<(usize, Element)> = Vec::new(); + // Children present in storage: their positions in `scalars`, and their commitments + let mut real_positions: Vec = Vec::new(); + let mut real_children: Vec = Vec::new(); - for (i, (node_id, _)) in nodes.iter().enumerate() { + for (i, node_id) in nodes.iter().enumerate() { // Calculate starting index for this node's 256 children let child_idx = get_child_node(&logic_parent_id(*node_id), 0); @@ -444,18 +444,15 @@ where // Record actual commitments to overwrite the defaults where they exist for (absolute_node_id, commitment_bytes) in children { let relative_index = absolute_node_id as usize - child_idx as usize; - real_children.push(( - i * DOMAIN_SIZE + relative_index, - to_element(commitment_bytes), - )); + real_positions.push(i * DOMAIN_SIZE + relative_index); + real_children.push(to_element(commitment_bytes)); } } // Batch-convert the existing children and scatter them into place. - let elements: Vec = real_children.iter().map(|(_, element)| *element).collect(); - let real_scalars = Element::batch_map_to_scalar_field(&elements); - for ((position, _), scalar) in real_children.iter().zip(real_scalars) { - scalars[*position] = scalar; + let real_scalars = Element::batch_map_to_scalar_field(&real_children); + for (position, scalar) in real_positions.into_iter().zip(real_scalars) { + scalars[position] = scalar; } Ok(scalars) @@ -472,24 +469,12 @@ where /// /// # Parameters /// * `commitment` - The cryptographic commitment to the polynomial -/// * `poly` - The polynomial in Lagrange basis form (256 coefficients) +/// * `poly` - The polynomial in Lagrange basis form (256 coefficients), shared by every query /// * `points` - Set of evaluation points (child indices) to create queries for /// /// # Returns /// A vector of `ProverQuery` objects, one for each evaluation point -#[cfg(test)] fn create_prover_queries( - commitment: Element, - poly: LagrangeBasis, - points: BTreeSet, -) -> Vec { - // One shared allocation per polynomial, however many points are opened. - shared_prover_queries(commitment, Arc::new(poly), &points) -} - -/// [`create_prover_queries`] over an already shared polynomial (a cache hit, or one built for -/// the cache). -fn shared_prover_queries( commitment: Element, poly: Arc, points: &BTreeSet, @@ -600,30 +585,28 @@ where .len() .div_ceil(num_threads!()) .max(MIN_PARENT_CHUNK); - let parents_read: Vec<(NodeId, crate::types::CommitmentBytes)> = - chunks!(parent_ids, commitment_chunk) - .map(|chunk| { - chunk - .iter() - .map(|&physical_parent| { - let bytes = store.commitment(physical_parent).map_err(|e| { - ProofError::StateReadError { - reason: format!( - "Failed to load commitment for node {physical_parent}: {e:?}" - ), - } - })?; - Ok((physical_parent, bytes)) - }) - .collect::>>() - }) - .collect::>>()? - .into_iter() - .flatten() - .collect(); + let parents_read: Vec<(NodeId, CommitmentBytes)> = chunks!(parent_ids, commitment_chunk) + .map(|chunk| { + chunk + .iter() + .map(|&physical_parent| { + let bytes = store.commitment(physical_parent).map_err(|e| { + ProofError::StateReadError { + reason: format!( + "Failed to load commitment for node {physical_parent}: {e:?}" + ), + } + })?; + Ok((physical_parent, bytes)) + }) + .collect::>>() + }) + .collect::>>()? + .into_iter() + .flatten() + .collect(); // The raw bytes key the polynomial cache; the decoded elements go into the proof. - let parent_bytes: FxHashMap = - parents_read.iter().copied().collect(); + let parent_bytes: FxHashMap = parents_read.iter().copied().collect(); let parents_commitments: BTreeMap = parents_read .into_iter() .map(|(id, bytes)| { @@ -643,125 +626,95 @@ where }) }; - // Every resolved polynomial by physical parent id: the bases a refresh patches (the - // witness reads the pre-state, so these are the polynomials the block's transition - // transforms). - let mut in_hand: FxHashMap> = FxHashMap::default(); - - // Step 5: Generate IPA prover queries for each node in internal nodes. A node whose - // polynomial the cache holds for its current commitment skips the child reads; the rest - // are materialized in parallel chunks. Query order (node order, then point order) is the - // same either way: the transcript hashes it, so a cache hit must not reorder the proof. - let in_nodes: Vec<_> = internal_nodes.into_iter().collect(); - let mut polys: Vec>> = Vec::with_capacity(in_nodes.len()); - let mut missing: Vec = Vec::new(); - for (i, (parent, _)) in in_nodes.iter().enumerate() { - let physical = connect_parent_id(*parent); - let hit = parent_bytes - .get(&physical) - .and_then(|bytes| node_poly_cache::get(physical, bytes)); - if hit.is_none() { - missing.push(i); - } - polys.push(hit); - } - let missing_nodes: Vec<(NodeId, BTreeSet)> = missing - .iter() - .map(|&i| (in_nodes[i].0, BTreeSet::new())) - .collect(); - let chunk_size = missing_nodes - .len() - .div_ceil(num_threads!()) - .max(MIN_NODE_CHUNK); - let computed: Vec>> = chunks!(missing_nodes, chunk_size) - .map(|nodes| { - let scalars = multi_commitments_to_scalars(store, nodes)?; - Ok(scalars - .chunks(DOMAIN_SIZE) - .map(|node_scalars| Arc::new(LagrangeBasis::new(node_scalars.to_vec()))) - .collect::>()) - }) - .collect::>>()?; - for (&i, poly) in missing.iter().zip(computed.into_iter().flatten()) { - let physical = connect_parent_id(in_nodes[i].0); - if let Some(bytes) = parent_bytes.get(&physical) { - node_poly_cache::insert(physical, *bytes, Arc::clone(&poly)); - } - polys[i] = Some(poly); - } - let mut queries: Vec = Vec::new(); - for ((parent, points), poly) in in_nodes.into_iter().zip(polys) { - let poly = poly.expect("every internal node polynomial is resolved above"); - in_hand.insert(connect_parent_id(parent), Arc::clone(&poly)); - queries.extend(shared_prover_queries( - parent_commitment(parent)?, - poly, - &points, - )); - } - - // Step 6: Generate IPA prover queries for each node in leaf nodes, with the same cache - // and the same order discipline. + // Step 5: Generate IPA prover queries for the internal nodes, then the leaf nodes. Internal + // nodes the cache misses are materialized in parallel chunks, leaf nodes one per task. + let internal_nodes: Vec<_> = internal_nodes.into_iter().collect(); let leaf_nodes: Vec<_> = leaf_nodes.into_iter().collect(); - let mut leaf_polys: Vec>> = Vec::with_capacity(leaf_nodes.len()); - let mut leaf_missing: Vec = Vec::new(); - for (i, (parent, _)) in leaf_nodes.iter().enumerate() { - let physical = connect_parent_id(*parent); - let hit = parent_bytes - .get(&physical) - .and_then(|bytes| node_poly_cache::get(physical, bytes)); - if hit.is_none() { - leaf_missing.push(i); - } - leaf_polys.push(hit); - } - let computed: Vec> = into_iter!(leaf_missing.clone()) - .map(|i| leaf_polynomial(store, leaf_nodes[i].0).map(Arc::new)) - .collect::>>()?; - for (&i, poly) in leaf_missing.iter().zip(computed) { - let physical = connect_parent_id(leaf_nodes[i].0); - if let Some(bytes) = parent_bytes.get(&physical) { - node_poly_cache::insert(physical, *bytes, Arc::clone(&poly)); + let internal_polys = resolve_polys(&internal_nodes, &parent_bytes, |missing| { + let chunk_size = missing.len().div_ceil(num_threads!()).max(MIN_NODE_CHUNK); + let chunks = chunks!(missing, chunk_size) + .map(|nodes| { + let scalars = multi_commitments_to_scalars(store, nodes)?; + Ok(scalars + .chunks(DOMAIN_SIZE) + .map(|node_scalars| LagrangeBasis::new(node_scalars.to_vec())) + .collect::>()) + }) + .collect::>>()?; + Ok(chunks.into_iter().flatten().collect()) + })?; + let leaf_polys = resolve_polys(&leaf_nodes, &parent_bytes, |missing| { + iter!(missing) + .map(|&node| leaf_polynomial(store, node)) + .collect() + })?; + + // Every resolved polynomial by physical parent id, with the commitment it was read at: + // the bases a refresh patches (the witness reads the pre-state, so these are the + // polynomials the block's transition transforms). + let mut in_hand: FxHashMap)> = + FxHashMap::default(); + let mut queries: Vec = Vec::new(); + let nodes = internal_nodes.into_iter().chain(leaf_nodes); + for ((parent, points), poly) in nodes.zip(internal_polys.into_iter().chain(leaf_polys)) { + if refresh.is_some() { + let physical = connect_parent_id(parent); + in_hand.insert(physical, (parent_bytes[&physical], Arc::clone(&poly))); } - leaf_polys[i] = Some(poly); - } - for ((parent, points), poly) in leaf_nodes.into_iter().zip(leaf_polys) { - let poly = poly.expect("every leaf node polynomial is resolved above"); - in_hand.insert(connect_parent_id(parent), Arc::clone(&poly)); - queries.extend(shared_prover_queries( + queries.extend(create_prover_queries( parent_commitment(parent)?, poly, &points, )); } - // Step 7: Advance the cache to the witnessed block's post-state. The queries above are + // Step 6: Advance the cache to the witnessed block's post-state. The queries above are // already built from the pre-state polynomials, so this proof is unaffected; it runs // here rather than after the proof so the entries land before the next block's witness // looks them up. if let Some(refresh) = refresh { - apply_refresh(refresh_plan(refresh), &in_hand, &parent_bytes); + apply_refresh(refresh_plan(refresh), &in_hand); } Ok((queries, parents_commitments, buckets_level)) } -/// Processes a leaf node to create prover queries. -#[cfg(test)] -fn process_leaf_node( - store: &Store, - parent: NodeId, - parent_commitment: Element, - points: BTreeSet, -) -> ProofResult> -where - Store: StateReader, -{ - Ok(create_prover_queries( - parent_commitment, - leaf_polynomial(store, parent)?, - points, - )) +/// The polynomial of every node in `nodes` (logical parent ids), in order: query order is +/// node order, which the transcript hashes, so a cache hit must not reorder the proof. A node +/// whose polynomial the cache holds for the commitment it was read at skips `rebuild`; the +/// misses are rebuilt in one call, one polynomial per missing node, and cached under that +/// commitment. +fn resolve_polys( + nodes: &[(NodeId, BTreeSet)], + parent_bytes: &FxHashMap, + rebuild: impl FnOnce(&[NodeId]) -> ProofResult>, +) -> ProofResult>> { + let hits: Vec>> = nodes + .iter() + .map(|(parent, _)| { + let physical = connect_parent_id(*parent); + node_poly_cache::get(physical, &parent_bytes[&physical]) + }) + .collect(); + let missing: Vec = nodes + .iter() + .zip(&hits) + .filter(|(_, hit)| hit.is_none()) + .map(|((parent, _), _)| *parent) + .collect(); + let mut rebuilt = rebuild(&missing)?.into_iter(); + Ok(nodes + .iter() + .zip(hits) + .map(|((parent, _), hit)| { + hit.unwrap_or_else(|| { + let physical = connect_parent_id(*parent); + let poly = Arc::new(rebuilt.next().expect("one rebuilt polynomial per miss")); + node_poly_cache::insert(physical, parent_bytes[&physical], Arc::clone(&poly)); + poly + }) + }) + .collect()) } /// The 256-slot polynomial of a leaf node: every slot's value hashed to the field, with the @@ -796,24 +749,31 @@ where })?; // Initialize polynomial coefficients with appropriate default values - let mut default_coefficients = if bucket_id < NUM_META_BUCKETS as BucketId { - // Metadata buckets: initialize with default metadata hash - vec![slot_to_field(&Some(BucketMeta::default().into())); DOMAIN_SIZE] - } else { - // Data buckets: initialize with empty slot hash - vec![slot_to_field(&None); DOMAIN_SIZE] - }; + let mut default_coefficients = vec![empty_slot_scalar(bucket_id); DOMAIN_SIZE]; // Replace default values with actual key-value hashes where data exists for (key, value) in entries { - // Map slot ID to polynomial coefficient index (last 8 bits) - let index = (key.slot_id() & SLOT_INDEX_MASK) as usize; - default_coefficients[index] = slot_to_field(&Some(value)); + default_coefficients[slot_position(&key)] = slot_to_field(&Some(value)); } Ok(LagrangeBasis::new(default_coefficients)) } +/// The scalar of an empty slot in `bucket_id`: the default metadata's hash for a metadata +/// bucket, the empty slot's hash for a data bucket. +fn empty_slot_scalar(bucket_id: BucketId) -> Fr { + if bucket_id < NUM_META_BUCKETS as BucketId { + slot_to_field(&Some(BucketMeta::default().into())) + } else { + slot_to_field(&None) + } +} + +/// The position of `key`'s slot in its leaf's 256-slot polynomial (the slot id's low 8 bits). +fn slot_position(key: &SaltKey) -> usize { + (key.slot_id() & SLOT_INDEX_MASK) as usize +} + #[cfg(test)] mod tests { use super::*; @@ -950,25 +910,27 @@ mod tests { } #[test] - fn process_leaf_node_with_real_commitment() { + fn leaf_polynomial_with_real_commitment() { let (store, salt_key) = setup_test_store(); let parent_node = STARTING_NODE_ID[3] as NodeId + salt_key.bucket_id() as u64; let commitment = Element::from_bytes_unchecked_uncompressed(store.commitment(parent_node).unwrap()); - let points = [0, salt_key.slot_id() as usize & 0xff].into(); + let points = [0, slot_position(&salt_key)].into(); - let queries = process_leaf_node(&store, parent_node, commitment, points).unwrap(); + let poly = Arc::new(leaf_polynomial(&store, parent_node).unwrap()); + let queries = create_prover_queries(commitment, poly, &points); assert_eq!(queries.len(), 2); assert!(verify_ipa_proof(queries)); } #[test] - fn process_leaf_node_uses_metadata_default_for_meta_bucket() { + fn leaf_polynomial_uses_metadata_default_for_meta_bucket() { let store = MemStore::new(); let parent_node = STARTING_NODE_ID[3] as NodeId; let commitment = Element::from_bytes_unchecked_uncompressed(default_commitment(parent_node)); - let queries = process_leaf_node(&store, parent_node, commitment, [0, 255].into()).unwrap(); + let poly = Arc::new(leaf_polynomial(&store, parent_node).unwrap()); + let queries = create_prover_queries(commitment, poly, &[0, 255].into()); let metadata_default = slot_to_field(&Some(BucketMeta::default().into())); assert_eq!( @@ -979,12 +941,13 @@ mod tests { } #[test] - fn process_leaf_node_uses_empty_default_for_data_bucket() { + fn leaf_polynomial_uses_empty_default_for_data_bucket() { let store = MemStore::new(); let parent_node = STARTING_NODE_ID[3] as NodeId + NUM_META_BUCKETS as NodeId; let commitment = Element::from_bytes_unchecked_uncompressed(default_commitment(parent_node)); - let queries = process_leaf_node(&store, parent_node, commitment, [0, 255].into()).unwrap(); + let poly = Arc::new(leaf_polynomial(&store, parent_node).unwrap()); + let queries = create_prover_queries(commitment, poly, &[0, 255].into()); let empty_default = slot_to_field(&None); assert_eq!( @@ -1002,16 +965,15 @@ mod tests { assert_eq!(multi_commitments_to_scalars(&store, &[]).unwrap().len(), 0); // Single internal node - let node_points = vec![(STARTING_NODE_ID[2] as NodeId, [0, 1].into())]; - let scalars = multi_commitments_to_scalars(&store, &node_points).unwrap(); + let scalars = + multi_commitments_to_scalars(&store, &[STARTING_NODE_ID[2] as NodeId]).unwrap(); assert_eq!(scalars.len(), DOMAIN_SIZE); } #[test] fn multi_commitments_to_scalars_root_defaults_are_pinned() { let store = MemStore::new(); - let scalars = - multi_commitments_to_scalars(&store, &[(ROOT_NODE_ID, [0, 1, 255].into())]).unwrap(); + let scalars = multi_commitments_to_scalars(&store, &[ROOT_NODE_ID]).unwrap(); let expected = Element::batch_map_to_scalar_field(&[ Element::from_bytes_unchecked_uncompressed(default_commitment( STARTING_NODE_ID[1] as NodeId, @@ -1048,40 +1010,33 @@ mod tests { assert!(result.is_ok()); } - /// Applies one block's transition to `store` and returns its trie updates. - fn apply_block(store: &MemStore, updates: StateUpdates) -> TrieUpdates { - let (_, trie_updates) = StateRoot::new(store).update_fin(&updates).unwrap(); - store.update_state(updates); - store.update_trie(trie_updates.clone()); - trie_updates + /// A slot value filled with `byte`. + fn value(byte: u8) -> Option { + Some(SaltValue::new(&[byte; 32], &[byte; 32])) } - /// A store holding block A (64 random plain kvs), and block B's transition over it (16 - /// value updates, 8 deletes and 16 inserts) computed but not applied. - fn two_block_fixture(seed: u64) -> (MemStore, StateUpdates, TrieUpdates) { - let mut rng = StdRng::seed_from_u64(seed); - let kvs_a: HashMap, Option>> = (0..64) - .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) - .collect(); + /// Bucket metadata with `nonce` and `capacity`. + fn meta(nonce: u32, capacity: u64) -> Option { + Some(SaltValue::from(BucketMeta { + nonce, + capacity, + ..BucketMeta::default() + })) + } + + /// A store holding `bucket` with slots 3 and 5 set to `value(1)` and `value(2)`. + fn store_with_two_slots(bucket: BucketId) -> MemStore { let store = MemStore::new(); - let updates_a = EphemeralSaltState::new(&store).update_fin(&kvs_a).unwrap(); - apply_block(&store, updates_a); - - let mut keys_a: Vec> = kvs_a.into_keys().collect(); - keys_a.sort(); - let mut kvs_b: HashMap, Option>> = HashMap::new(); - for key in &keys_a[..16] { - kvs_b.insert(key.clone(), Some(mock_data(&mut rng, 40))); - } - for key in &keys_a[16..24] { - kvs_b.insert(key.clone(), None); - } - for _ in 0..16 { - kvs_b.insert(mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40))); - } - let updates_b = EphemeralSaltState::new(&store).update_fin(&kvs_b).unwrap(); - let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); - (store, updates_b, trie_b) + let key = |slot| SaltKey::from((bucket, slot)); + apply_block( + &store, + StateUpdates { + data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] + .into_iter() + .collect(), + }, + ); + store } /// Whether `node` is a leaf of its trie: a main-trie bucket root or a subtree leaf. @@ -1100,9 +1055,7 @@ mod tests { if is_leaf_node(node) { leaf_polynomial(store, node).unwrap() } else { - LagrangeBasis::new( - multi_commitments_to_scalars(store, &[(node, BTreeSet::new())]).unwrap(), - ) + LagrangeBasis::new(multi_commitments_to_scalars(store, &[node]).unwrap()) } } @@ -1157,7 +1110,12 @@ mod tests { /// main-trie leaves and internal nodes alike. #[test] fn refreshed_polynomials_equal_a_storage_rebuild() { - let (store, updates_b, trie_b) = two_block_fixture(1); + let TwoBlocks { + store, + updates_b, + trie_b, + .. + } = two_block_fixture(1); let keys: Vec = updates_b.data.keys().copied().collect(); let refresh = NodePolyRefresh { trie_updates: &trie_b, @@ -1202,34 +1160,19 @@ mod tests { /// other subtree node under its own id and every slot under its subtree leaf. #[test] fn refresh_covers_expanded_bucket_subtrees() { - let store = MemStore::new(); let bucket = NUM_META_BUCKETS as BucketId + 3 * MIN_BUCKET_SIZE as BucketId + 7; let capacity = 131072u64; - let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); - let meta = |capacity| { - Some(SaltValue::from(BucketMeta { - capacity, - ..BucketMeta::default() - })) - }; let key = |slot| SaltKey::from((bucket, slot)); // Block A: two slots, then an expansion to a three-level subtree (top at level 2). - apply_block( - &store, - StateUpdates { - data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] - .into_iter() - .collect(), - }, - ); + let store = store_with_two_slots(bucket); apply_block( &store, StateUpdates { data: [ ( bucket_metadata_key(bucket), - (meta(MIN_BUCKET_SIZE as u64), meta(capacity)), + (meta(0, MIN_BUCKET_SIZE as u64), meta(0, capacity)), ), (key(3), (value(1), None)), (key(2049), (None, value(3))), @@ -1327,30 +1270,14 @@ mod tests { /// next witness over the bucket rebuilds from storage. #[test] fn refresh_skips_buckets_whose_capacity_changed() { - let store = MemStore::new(); let bucket = NUM_META_BUCKETS as BucketId + 5 * MIN_BUCKET_SIZE as BucketId + 9; - let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); - let meta = |capacity| { - Some(SaltValue::from(BucketMeta { - capacity, - ..BucketMeta::default() - })) - }; + let store = store_with_two_slots(bucket); let key = |slot| SaltKey::from((bucket, slot)); let meta_key = bucket_metadata_key(bucket); let root = bucket_root_node_id(bucket); let parent = get_parent_node(&root); let meta_root = bucket_root_node_id(meta_key.bucket_id()); - apply_block( - &store, - StateUpdates { - data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] - .into_iter() - .collect(), - }, - ); - // Witnesses `witnessed` against the current store with the block's refresh, applies // the block and checks what was and was not refreshed. let run = |updates: StateUpdates, witnessed: &[SaltKey]| { @@ -1371,7 +1298,7 @@ mod tests { ); assert_eq!( planned_positions(&plan, meta_root), - [(meta_key.slot_id() & SLOT_INDEX_MASK) as usize].into() + [slot_position(&meta_key)].into() ); let (queries, _, _) = create_sub_trie(&store, witnessed, Some(&refresh)).unwrap(); @@ -1416,8 +1343,8 @@ mod tests { ( meta_key, ( - meta(MIN_BUCKET_SIZE as u64), - meta(2 * MIN_BUCKET_SIZE as u64), + meta(0, MIN_BUCKET_SIZE as u64), + meta(0, 2 * MIN_BUCKET_SIZE as u64), ), ), (key(3), (value(1), None)), @@ -1438,8 +1365,8 @@ mod tests { ( meta_key, ( - meta(2 * MIN_BUCKET_SIZE as u64), - meta(MIN_BUCKET_SIZE as u64), + meta(0, 2 * MIN_BUCKET_SIZE as u64), + meta(0, MIN_BUCKET_SIZE as u64), ), ), (key(300), (value(3), None)), @@ -1458,7 +1385,12 @@ mod tests { /// the proof bytes do not change. #[test] fn refresh_is_idempotent_across_a_retried_witness() { - let (store, updates_b, trie_b) = two_block_fixture(2); + let TwoBlocks { + store, + updates_b, + trie_b, + .. + } = two_block_fixture(2); let keys: Vec = updates_b.data.keys().copied().collect(); let refresh = NodePolyRefresh { trie_updates: &trie_b, @@ -1497,7 +1429,12 @@ mod tests { #[test] #[should_panic(expected = "does not commit")] fn refresh_debug_check_rejects_an_inconsistent_transition() { - let (store, updates_b, mut trie_b) = two_block_fixture(3); + let TwoBlocks { + store, + updates_b, + mut trie_b, + .. + } = two_block_fixture(3); let foreign = trie_b .iter() .find(|(node, _)| *node != ROOT_NODE_ID) @@ -1524,7 +1461,12 @@ mod tests { #[test] #[should_panic(expected = "does not commit")] fn refresh_debug_check_rejects_a_wrong_slot_value() { - let (store, mut updates_b, trie_b) = two_block_fixture(4); + let TwoBlocks { + store, + mut updates_b, + trie_b, + .. + } = two_block_fixture(4); let key = *updates_b .data .iter() @@ -1547,7 +1489,12 @@ mod tests { /// pre-state has cached their bases. #[test] fn refresh_uses_cached_bases_for_parents_the_witness_did_not_read() { - let (store, updates_b, trie_b) = two_block_fixture(5); + let TwoBlocks { + store, + updates_b, + trie_b, + .. + } = two_block_fixture(5); let keys: Vec = updates_b.data.keys().copied().collect(); let refresh = NodePolyRefresh { trie_updates: &trie_b, @@ -1608,20 +1555,26 @@ mod tests { /// polynomial a rebuild from the post-block store produces. #[test] fn refresh_applies_every_planned_parent() { - let (store, updates_b, trie_b) = two_block_fixture(9); + let TwoBlocks { + store, + updates_b, + trie_b, + .. + } = two_block_fixture(9); let refresh = NodePolyRefresh { trie_updates: &trie_b, state_updates: &updates_b, }; let plan = refresh_plan(&refresh); let planned = plan.len(); - let mut in_hand = FxHashMap::default(); - let mut parent_bytes = FxHashMap::default(); - for &node in plan.keys() { - in_hand.insert(node, Arc::new(rebuilt_polynomial(&store, node))); - parent_bytes.insert(node, store.commitment(node).unwrap()); - } - assert_eq!(apply_refresh(plan, &in_hand, &parent_bytes), planned); + let in_hand = plan + .keys() + .map(|&node| { + let base = Arc::new(rebuilt_polynomial(&store, node)); + (node, (store.commitment(node).unwrap(), base)) + }) + .collect(); + assert_eq!(apply_refresh(plan, &in_hand), planned); store.update_state(updates_b); store.update_trie(trie_b.clone()); @@ -1644,7 +1597,12 @@ mod tests { #[test] #[should_panic(expected = "other than the refresh's pre-state")] fn refresh_rejects_a_witness_over_the_post_state() { - let (store, updates_b, trie_b) = two_block_fixture(6); + let TwoBlocks { + store, + updates_b, + trie_b, + .. + } = two_block_fixture(6); let keys: Vec = updates_b.data.keys().copied().collect(); store.update_state(updates_b.clone()); store.update_trie(trie_b.clone()); @@ -1663,20 +1621,11 @@ mod tests { #[cfg(not(debug_assertions))] #[test] fn refresh_skips_a_parent_read_at_a_foreign_commitment() { - let store = MemStore::new(); let bucket = NUM_META_BUCKETS as BucketId + 7 * MIN_BUCKET_SIZE as BucketId + 11; - let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); let key = |slot| SaltKey::from((bucket, slot)); // Blocks A and B change slot 3; block C, computed over B, changes slot 5. - apply_block( - &store, - StateUpdates { - data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] - .into_iter() - .collect(), - }, - ); + let store = store_with_two_slots(bucket); let pre_b = store.clone(); apply_block( &store, @@ -1720,14 +1669,6 @@ mod tests { let store = MemStore::new(); let bucket = NUM_META_BUCKETS as BucketId + 9 * MIN_BUCKET_SIZE as BucketId + 13; let capacity = 2 * MIN_BUCKET_SIZE as u64; - let value = |byte: u8| Some(SaltValue::new(&[byte; 32], &[byte; 32])); - let meta = |nonce| { - Some(SaltValue::from(BucketMeta { - nonce, - capacity, - ..BucketMeta::default() - })) - }; let key = |slot| SaltKey::from((bucket, slot)); let meta_key = bucket_metadata_key(bucket); let root = bucket_root_node_id(bucket); @@ -1739,7 +1680,10 @@ mod tests { &store, StateUpdates { data: [ - (meta_key, (Some(BucketMeta::default().into()), meta(0))), + ( + meta_key, + (Some(BucketMeta::default().into()), meta(0, capacity)), + ), (key(3), (None, value(1))), (key(5), (None, value(2))), (key(300), (None, value(3))), @@ -1753,7 +1697,10 @@ mod tests { let updates_b = EphemeralSaltState::new(&store) .set_nonce(bucket, 1) .unwrap(); - assert_eq!(updates_b.data[&meta_key], (meta(0), meta(1))); + assert_eq!( + updates_b.data[&meta_key], + (meta(0, capacity), meta(1, capacity)) + ); let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); let refresh = NodePolyRefresh { trie_updates: &trie_b, @@ -1767,7 +1714,7 @@ mod tests { moved .entry(subtree_leaf_for_key(key)) .or_default() - .insert((key.slot_id() & SLOT_INDEX_MASK) as usize); + .insert(slot_position(key)); } assert_eq!(moved.len(), 2, "both leaves change"); let plan = refresh_plan(&refresh); @@ -1784,7 +1731,7 @@ mod tests { ); assert_eq!( planned_positions(&plan, meta_root), - [(meta_key.slot_id() & SLOT_INDEX_MASK) as usize].into() + [slot_position(&meta_key)].into() ); let keys: Vec = updates_b.data.keys().copied().collect(); diff --git a/salt/src/proof/test_utils.rs b/salt/src/proof/test_utils.rs index 38eb35a8..826ac08c 100644 --- a/salt/src/proof/test_utils.rs +++ b/salt/src/proof/test_utils.rs @@ -2,10 +2,14 @@ //! //! This module provides common mock data generation functions used across proof tests. +use crate::mem_store::MemStore; use crate::proof::SerdeCommitment; +use crate::state::{state::EphemeralSaltState, updates::StateUpdates}; +use crate::trie::trie::{StateRoot, TrieUpdates}; use crate::types::SaltValue; use banderwagon::{Element, Fr}; -use rand::{rngs::StdRng, Rng}; +use hashbrown::HashMap; +use rand::{rngs::StdRng, Rng, SeedableRng}; use std::vec::Vec; /// Generates random test data of specified length. @@ -38,3 +42,53 @@ pub(crate) fn mock_commitment() -> SerdeCommitment { pub(crate) fn mock_salt_value() -> SaltValue { SaltValue::new(&[1u8; 32], &[2u8; 32]) } + +/// Applies one block's transition to `store` and returns its trie updates. +pub(crate) fn apply_block(store: &MemStore, updates: StateUpdates) -> TrieUpdates { + let (_, trie_updates) = StateRoot::new(store).update_fin(&updates).unwrap(); + store.update_state(updates); + store.update_trie(trie_updates.clone()); + trie_updates +} + +/// A store holding block A (64 random plain kvs), and block B's transition over it (16 value +/// updates, 8 deletes and 16 inserts) computed but not applied. +pub(crate) struct TwoBlocks { + pub store: MemStore, + /// Block B's plain writes. + pub kvs_b: HashMap, Option>>, + pub updates_b: StateUpdates, + pub trie_b: TrieUpdates, +} + +/// Builds [`TwoBlocks`] from `seed`. +pub(crate) fn two_block_fixture(seed: u64) -> TwoBlocks { + let mut rng = StdRng::seed_from_u64(seed); + let kvs_a: HashMap, Option>> = (0..64) + .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) + .collect(); + let store = MemStore::new(); + let updates_a = EphemeralSaltState::new(&store).update_fin(&kvs_a).unwrap(); + apply_block(&store, updates_a); + + let mut keys_a: Vec> = kvs_a.into_keys().collect(); + keys_a.sort(); + let mut kvs_b: HashMap, Option>> = HashMap::new(); + for key in &keys_a[..16] { + kvs_b.insert(key.clone(), Some(mock_data(&mut rng, 40))); + } + for key in &keys_a[16..24] { + kvs_b.insert(key.clone(), None); + } + for _ in 0..16 { + kvs_b.insert(mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40))); + } + let updates_b = EphemeralSaltState::new(&store).update_fin(&kvs_b).unwrap(); + let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); + TwoBlocks { + store, + kvs_b, + updates_b, + trie_b, + } +} diff --git a/salt/src/proof/witness.rs b/salt/src/proof/witness.rs index 70196e5a..48347f89 100644 --- a/salt/src/proof/witness.rs +++ b/salt/src/proof/witness.rs @@ -1200,6 +1200,11 @@ mod tests { .all(|&id| witness.metadata(id).unwrap() == new_metadata)); } + /// The bytes of `witness`'s SALT witness, as the proof byte-identity tests compare them. + fn encode_salt_witness(witness: &Witness) -> Vec { + bincode::serde::encode_to_vec(&witness.salt_witness, bincode::config::legacy()).unwrap() + } + /// A node-polynomial cache hit must reproduce the miss byte for byte: the same inputs /// witnessed twice serialize identically (the second pass is served from the cache), and /// the witness still verifies. @@ -1210,17 +1215,14 @@ mod tests { .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) .collect(); let store = MemStore::new(); - let updates = EphemeralSaltState::new(&store).update_fin(&kvs).unwrap(); - store.update_state(updates.clone()); - let (_root, trie_updates) = StateRoot::new(&store).update_fin(&updates).unwrap(); - store.update_trie(trie_updates); + apply_block( + &store, + EphemeralSaltState::new(&store).update_fin(&kvs).unwrap(), + ); let build = || Witness::create([], kvs.keys(), &BTreeMap::new(), &store).unwrap(); - let encode = |witness: &Witness| { - bincode::serde::encode_to_vec(&witness.salt_witness, bincode::config::legacy()).unwrap() - }; - let first = encode(&build()); - let second = encode(&build()); + let first = encode_salt_witness(&build()); + let second = encode_salt_witness(&build()); assert_eq!(first, second, "a cache hit must not change the proof"); build().verify().unwrap(); } @@ -1230,31 +1232,12 @@ mod tests { /// wrong refreshed entry on those paths would fail its verification. #[test] fn witness_over_refreshed_polynomials_verifies_and_is_stable() { - let mut rng = StdRng::seed_from_u64(11); - let kvs_a: HashMap<_, _> = (0..64) - .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) - .collect(); - let store = MemStore::new(); - let updates_a = EphemeralSaltState::new(&store).update_fin(&kvs_a).unwrap(); - store.update_state(updates_a.clone()); - let (_, trie_a) = StateRoot::new(&store).update_fin(&updates_a).unwrap(); - store.update_trie(trie_a); - - // Block B: 16 value updates, 8 deletes and 16 inserts over block A. - let mut keys_a: Vec> = kvs_a.into_keys().collect(); - keys_a.sort(); - let mut kvs_b: HashMap, Option>> = HashMap::new(); - for key in &keys_a[..16] { - kvs_b.insert(key.clone(), Some(mock_data(&mut rng, 40))); - } - for key in &keys_a[16..24] { - kvs_b.insert(key.clone(), None); - } - for _ in 0..16 { - kvs_b.insert(mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40))); - } - let updates_b = EphemeralSaltState::new(&store).update_fin(&kvs_b).unwrap(); - let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); + let TwoBlocks { + store, + kvs_b, + updates_b, + trie_b, + } = two_block_fixture(11); let plain_b: BTreeMap<_, _> = kvs_b .iter() .map(|(key, value)| (key.clone(), value.clone())) @@ -1271,11 +1254,8 @@ mod tests { store.update_state(updates_b); store.update_trie(trie_b); let build = || Witness::create([], kvs_b.keys(), &BTreeMap::new(), &store).unwrap(); - let encode = |witness: &Witness| { - bincode::serde::encode_to_vec(&witness.salt_witness, bincode::config::legacy()).unwrap() - }; - let first = encode(&build()); - let second = encode(&build()); + let first = encode_salt_witness(&build()); + let second = encode_salt_witness(&build()); assert_eq!(first, second, "a refreshed entry must not change the proof"); build().verify().unwrap(); } diff --git a/salt/src/trie/trie.rs b/salt/src/trie/trie.rs index f2852ce1..23909b94 100644 --- a/salt/src/trie/trie.rs +++ b/salt/src/trie/trie.rs @@ -32,6 +32,7 @@ use crate::{ types::*, }; use banderwagon::{platform, salt_committer::Committer, Element, Fr, PrimeField}; +use ipa_multipoint::crs::CRS; use salt_macros::prelude::*; use salt_macros::{chunks, into_iter, iter, num_threads, sort_unstable_by, sort_unstable_by_key}; @@ -42,6 +43,12 @@ use std::{sync::Arc, vec}; use core::{cmp::Ordering, ops::Range}; +/// Shared default CRS. Constructing `CRS::default()` decompresses 257 points, +/// each costing a modular square root plus a subgroup check (several +/// milliseconds total), so it is done once per process and reused by the +/// shared committer and by every proof creation and verification. +pub static DEFAULT_CRS: Lazy = Lazy::new(CRS::default); + /// Global shared instance of the Committer to avoid repeated expensive initialization static SHARED_COMMITTER: Lazy> = Lazy::new(|| Arc::new(build_shared_committer())); @@ -66,7 +73,7 @@ pub(crate) fn shared_committer() -> Arc { /// same stack and deadlock. fn build_shared_committer() -> Committer { let build = || { - let crs = &*crate::proof::prover::DEFAULT_CRS; + let crs = &*DEFAULT_CRS; // `Q` rides along as base `crs.n`, so the IPA prover's blinding terms are fixed-base // multiplications too (see `ipa::create_with_precomp`). let bases: Vec = crs From 911cc37b88241cc7330b574ffd5e9272eecab74f Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 21 Sep 2026 01:28:15 +0800 Subject: [PATCH 07/18] perf(msm): size each Pippenger window's buckets to the digits it can hold MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `make_digits` recenters every window's digit to [-2^(c-1), 2^(c-1) - 1], so a bucket index (|digit| - 1) never reaches the upper half of the 2^c array the windowed MSM allocated: half of every window's bucket array was untouched, and the reduction still walked it, paying one point addition per empty bucket (arkworks skips the zero operand, so the cost was the `res += running_sum` of each empty slot rather than both additions). Each window now takes 2^(c-1) buckets. The last window is the exception: it absorbs the outstanding carry instead of being recentered, so its digits run 0..=2^last_bits and it takes 2^last_bits buckets — the full 2^c when c divides the 253-bit modulus width, which is the width the size heuristic picks for 8,193 to 16,384 points. Results are unchanged (exact group arithmetic), so proofs stay byte-identical. Measured on an M4 Pro, release, medians of two runs: 274 points (the IPA's closing MSM, present in every proof check) 525 -> 418 us, 400 points 580 -> 484, 1,000 points 880 -> 798, 2,335 points 2,046 -> 1,718, 8,192 points 4,884 -> 4,564. The band that gains most is exactly the one a mainnet head witness lands in: its commitments coalesce to a few hundred to a couple of thousand points. None of this depends on AVX-512, so every platform gets it. `msm_windowed` now picks `c` and delegates to `msm_windowed_with_c`, which lets the new equivalence test cover every window width (and with it every bucket-count branch) at a size that keeps a debug run cheap; the existing size-driven test still covers the parallel path. A second test pins the digit bounds the bucket counts rest on, and an ignored timing test reports the sizes above. Co-Authored-By: Claude Opus 5 (1M context) --- banderwagon/src/msm.rs | 153 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 146 insertions(+), 7 deletions(-) diff --git a/banderwagon/src/msm.rs b/banderwagon/src/msm.rs index 36205666..c18dd670 100644 --- a/banderwagon/src/msm.rs +++ b/banderwagon/src/msm.rs @@ -18,18 +18,26 @@ use std::{vec, vec::Vec}; /// carries enough work to pay for the dispatch. pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsProjective { let size = bases.len().min(scalars.len()); - let bases = &bases[..size]; - let scalars = &scalars[..size]; - if size == 0 { - return EdwardsProjective::zero(); - } - let c = if size < 32 { 3 } else { // ln_without_floats: log2(size) * ln(2), plus 2 (usize::BITS - (size - 1).leading_zeros()) as usize * 69 / 100 + 2 }; + msm_windowed_with_c(bases, scalars, c) +} + +/// [`msm_windowed`] with the window width given, so the tests can exercise every +/// width — and with it every bucket-count branch of [`bucket_len`] — at a size +/// that keeps a debug-mode run cheap. +fn msm_windowed_with_c(bases: &[EdwardsAffine], scalars: &[Fr], c: usize) -> EdwardsProjective { + let size = bases.len().min(scalars.len()); + let bases = &bases[..size]; + let scalars = &scalars[..size]; + if size == 0 { + return EdwardsProjective::zero(); + } + let num_bits = ::MODULUS_BIT_SIZE as usize; let digits_count = num_bits.div_ceil(c); @@ -43,7 +51,8 @@ pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsPr } let window_sum = |window: usize| -> EdwardsProjective { - let mut buckets = vec![EdwardsProjective::zero(); 1 << c]; + let mut buckets = + vec![EdwardsProjective::zero(); bucket_len(c, num_bits, digits_count, window)]; for (digits, base) in scalar_digits.chunks_exact(digits_count).zip(bases) { let digit = digits[window]; match 0.cmp(&digit) { @@ -85,6 +94,25 @@ pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsPr }) } +/// Buckets one window of [`msm_windowed_with_c`] needs, which is half of the +/// window's `2^c` digit values for every window but the last. +/// +/// [`make_digits`] recenters each digit to `[-2^(c-1), 2^(c-1) - 1]`, so a +/// bucket index `|digit| - 1` stays below `2^(c-1)` — the upper half of a +/// `2^c` array is never written, and reducing over it costs one full point +/// addition per empty bucket. The last window is the exception: it absorbs the +/// outstanding carry instead of being recentered, so its digits run +/// `0..=2^last_bits`, where `last_bits` is what the modulus leaves for it. +/// `last_bits == c` when `c` divides the modulus width (c = 11 and c = 23 at +/// 253 bits), and then the last window does need all `2^c` buckets. +fn bucket_len(c: usize, num_bits: usize, digits_count: usize, window: usize) -> usize { + if window == digits_count - 1 { + 1 << (num_bits - (digits_count - 1) * c) + } else { + 1 << (c - 1) + } +} + /// Decomposes a scalar into signed `w`-bit digits in `[-2^(w-1), 2^(w-1)]`, /// written into `digits`. Identical digit semantics to arkworks' /// `make_digits` (the final digit absorbs the outstanding carry). @@ -217,6 +245,117 @@ mod tests { assert!(msm_windowed(&[], &[]).is_zero()); } + /// Every window width must agree with arkworks, not only the ones the size + /// heuristic happens to pick: [`bucket_len`] sizes the last window from the + /// bits the modulus leaves it, and `c = 11` — the width chosen for 8,193 to + /// 16,384 points — is the one where that is the full `2^c`. + #[test] + fn msm_windowed_matches_arkworks_for_every_window_width() { + use ark_ec::{CurveGroup, VariableBaseMSM}; + use ark_ff::UniformRand; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::ChaCha20Rng; + + let mut rng = ChaCha20Rng::from_seed([21u8; 32]); + let size = 200; + let bases_proj: Vec = (0..size) + .map(|_| EdwardsProjective::rand(&mut rng)) + .collect(); + let bases = EdwardsProjective::normalize_batch(&bases_proj); + let mut scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); + scalars[0] = Fr::zero(); + // `r - 1` maximizes the last window's digit, which is the one the last + // bucket count has to cover. + scalars[1] = -Fr::from(1u64); + scalars[2] = Fr::from(1u64); + + let expected = EdwardsProjective::msm(&bases, &scalars).unwrap(); + for c in 3..=13 { + assert_eq!( + msm_windowed_with_c(&bases, &scalars, c), + expected, + "c = {c}" + ); + } + } + + /// Timing of [`msm_windowed`] at the sizes SALT verification actually hits: + /// the IPA's closing MSM is a fixed 274 points, an ordinary head witness + /// coalesces to a few hundred to a couple of thousand commitments, and a + /// dense one to tens of thousands. Run with + /// `cargo test -p banderwagon --release msm_windowed_timing -- --ignored --nocapture`. + #[test] + #[ignore = "timing, not a correctness check"] + fn msm_windowed_timing() { + use ark_ec::CurveGroup; + use ark_ff::UniformRand; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::ChaCha20Rng; + use std::{println, time::Instant}; + + let mut rng = ChaCha20Rng::from_seed([5u8; 32]); + // Spin the thread pool up before the first timed size. + { + let warm: Vec = (0..512) + .map(|_| EdwardsProjective::rand(&mut rng)) + .collect(); + let warm = EdwardsProjective::normalize_batch(&warm); + let scalars: Vec = (0..warm.len()).map(|_| Fr::rand(&mut rng)).collect(); + for _ in 0..20 { + let _ = core::hint::black_box(msm_windowed(&warm, &scalars)); + } + } + for size in [274usize, 400, 1000, 2335, 8192] { + let bases_proj: Vec = (0..size) + .map(|_| EdwardsProjective::rand(&mut rng)) + .collect(); + let bases = EdwardsProjective::normalize_batch(&bases_proj); + let scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); + + let reps = if size <= 1000 { 50 } else { 10 }; + let _ = msm_windowed(&bases, &scalars); + let start = Instant::now(); + for _ in 0..reps { + let _ = core::hint::black_box(msm_windowed(&bases, &scalars)); + } + let us = start.elapsed().as_secs_f64() * 1e6 / reps as f64; + println!("msm_windowed n = {size:>5}: {us:>9.1} us"); + } + } + + /// The bucket counts [`bucket_len`] hands out must cover every digit + /// [`make_digits`] can produce: a bucket index is `|digit| - 1`, so every + /// window needs `|digit| <= bucket_len`. + #[test] + fn make_digits_stays_within_the_bucket_counts() { + use ark_ff::UniformRand; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::ChaCha20Rng; + + let mut rng = ChaCha20Rng::from_seed([7u8; 32]); + let num_bits = ::MODULUS_BIT_SIZE as usize; + let mut scalars: Vec = (0..64).map(|_| Fr::rand(&mut rng)).collect(); + scalars.extend([Fr::zero(), Fr::from(1u64), -Fr::from(1u64)]); + + for c in 3..=16 { + let digits_count = num_bits.div_ceil(c); + let mut digits = vec![0i64; digits_count]; + for scalar in &scalars { + make_digits(&scalar.into_bigint().0, c, &mut digits); + for (window, &digit) in digits.iter().enumerate() { + let len = bucket_len(c, num_bits, digits_count, window) as i64; + assert!( + digit.abs() <= len, + "c = {c}, window {window}: digit {digit} needs more than {len} buckets" + ); + if window == digits_count - 1 { + assert!(digit >= 0, "c = {c}: the last digit absorbs the carry"); + } + } + } + } + } + #[test] fn correctness_smoke_test() { let mut crs = Vec::with_capacity(256); From 4e613ced9e07385b7ac8b629387c7396e0b36375 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 21 Sep 2026 10:24:14 +0800 Subject: [PATCH 08/18] bench(proof): time a cold witness build, not a saturated cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `benches/proof.rs` creates every proof over one store and one key set, so from the second sample on, the process-wide node-polynomial cache serves every node and the benchmark reports a 100% hit rate — a number no caller sees, and one that hides the cost of the stages that feed the cache (store reads, map-to-field passes, their rayon chunking) from anything measured against it. `salt_proof/create-cold` and `witness/create-cold` clear the cache before every iteration, so each sample pays for the nodes on its own paths. The warm groups stay: production sits between the two, at 44% on mainnet without the refresh and 98% with it, so the pair brackets it. `clear_node_poly_cache` is the hook the benches call. It is `#[doc(hidden)]` — nothing in production wants to drop the cache — and clearing a shard frees its map after the lock is released, like the eviction path does. Co-Authored-By: Claude Opus 5 (1M context) --- salt/benches/proof.rs | 45 +++++++++++++++++++++++++++++++++++++-- salt/src/lib.rs | 5 ++++- salt/src/proof/mod.rs | 2 +- salt/src/proof/subtrie.rs | 21 ++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/salt/benches/proof.rs b/salt/benches/proof.rs index 25a9ee99..49672670 100644 --- a/salt/benches/proof.rs +++ b/salt/benches/proof.rs @@ -11,9 +11,12 @@ //! cargo bench --package salt --bench proof //! ``` -use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; use rand::{rngs::StdRng, Rng, SeedableRng}; -use salt::{types::*, EphemeralSaltState, MemStore, SaltProof, SaltWitness, StateRoot, Witness}; +use salt::{ + clear_node_poly_cache, types::*, EphemeralSaltState, MemStore, SaltProof, SaltWitness, + StateRoot, Witness, +}; use std::collections::BTreeMap; use std::hint::black_box; use std::sync::OnceLock; @@ -100,6 +103,25 @@ fn bench_salt_proof(c: &mut Criterion) { } group.finish(); + // Cold: the node-polynomial cache is cleared before every iteration, so each proof pays + // the store reads and map-to-field passes for every node on its paths. A repeated warm + // benchmark measures a 100% hit rate, which no caller sees; production sits between the + // two (44% on mainnet without the refresh, 98% with it). + let mut group = c.benchmark_group("salt_proof/create-cold"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(10)); + for n in [16usize, 256, 2048] { + let keys = proof_keys(s, n); + group.bench_with_input(BenchmarkId::from_parameter(n), &keys, |b, keys| { + b.iter_batched( + clear_node_poly_cache, + |()| black_box(SaltProof::create(keys.iter().copied(), &s.store).unwrap()), + BatchSize::PerIteration, + ); + }); + } + group.finish(); + let mut group = c.benchmark_group("salt_proof/check"); group.sample_size(10); group.measurement_time(Duration::from_secs(10)); @@ -176,6 +198,25 @@ fn bench_witness(c: &mut Criterion) { } group.finish(); + let mut group = c.benchmark_group("witness/create-cold"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(15)); + for n in [256usize, 1024] { + let (lookups, updates) = witness_workload(s, n, &mut rng); + group.bench_with_input( + BenchmarkId::from_parameter(n), + &(lookups, updates), + |b, (lookups, updates)| { + b.iter_batched( + clear_node_poly_cache, + |()| black_box(Witness::create([], lookups.iter(), updates, &s.store).unwrap()), + BatchSize::PerIteration, + ); + }, + ); + } + group.finish(); + let mut group = c.benchmark_group("witness/verify"); group.sample_size(10); group.measurement_time(Duration::from_secs(15)); diff --git a/salt/src/lib.rs b/salt/src/lib.rs index 11b831fd..ff63bdcf 100644 --- a/salt/src/lib.rs +++ b/salt/src/lib.rs @@ -18,7 +18,10 @@ pub(crate) type Lazy = spin::Lazy; pub mod constant; pub mod empty_salt; pub mod proof; -pub use proof::{fx_hashmap_serde, NodePolyRefresh, ProofError, SaltProof, SaltWitness, Witness}; +pub use proof::{ + clear_node_poly_cache, fx_hashmap_serde, NodePolyRefresh, ProofError, SaltProof, SaltWitness, + Witness, +}; pub mod state; pub use state::{ hasher, state::EphemeralSaltState, state::PlainStateProvider, updates::StateUpdates, diff --git a/salt/src/proof/mod.rs b/salt/src/proof/mod.rs index 297f102c..9bcc4a6b 100644 --- a/salt/src/proof/mod.rs +++ b/salt/src/proof/mod.rs @@ -78,7 +78,7 @@ mod test_utils; pub use prover::{fx_hashmap_serde, SaltProof, SerdeCommitment, SerdeMultiPointProof}; pub use salt_witness::SaltWitness; -pub use subtrie::NodePolyRefresh; +pub use subtrie::{clear_node_poly_cache, NodePolyRefresh}; pub use witness::Witness; /// Error type for proof operations diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index e42e0a41..bf441239 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -93,6 +93,16 @@ mod node_poly_cache { .map(|(_, poly)| Arc::clone(poly)) } + /// Drops every entry, freeing each shard's map after its lock is released. + pub(super) fn clear() { + for shard in CACHE.iter() { + let mut guard = shard.write(); + let dropped = core::mem::take(&mut *guard); + drop(guard); + drop(dropped); + } + } + pub(super) fn insert(node: NodeId, commitment: CommitmentBytes, poly: Arc) { let mut guard = shard(node).write(); // Only a new node grows the shard. Whatever the insert evicts or replaces is dropped @@ -109,6 +119,17 @@ mod node_poly_cache { } } +/// Drops every entry of the process-wide node-polynomial cache. +/// +/// Nothing in production wants this — carrying entries across witnesses is the point of the +/// cache. It exists so a benchmark can time a *cold* witness build, the shape a witness takes +/// over nodes no earlier witness has touched: without it a benchmark that repeats one witness +/// measures a 100% hit rate, which no validator sees. +#[doc(hidden)] +pub fn clear_node_poly_cache() { + node_poly_cache::clear(); +} + /// One block's header-verified trie transition, used to advance the node-polynomial cache to /// that block's post-state while the block's own witness is built. /// From 784d695c86ea5d43476defff86adc09784e6bfe0 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 21 Sep 2026 15:57:00 +0800 Subject: [PATCH 09/18] refactor(proof): drop the tuning that an ablation could not find an effect for MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An ablation of every mechanism this branch adds (one arm per mechanism switched off, salt's own criterion benches, 32-thread EPYC) found three that are either inert at mainnet witness shapes or too small to carry their cost: - `ScalarWindows` (allocation-free wNAF digits in `mul_index`): nothing on proof creation, +1.3% / +2.4% on `update 10000 KVs` at 1 / 8 threads, the first of which sits on the benchmark's noise floor. It also removed `calculate_prefetch_index`, which the batched committer this branch is about to be merged with builds on. `salt_committer.rs` goes back to what `ad33ac8` left. - `MIN_PARENT_CHUNK = 8`: a floor binds only below `floor x threads` items, and a mainnet head witness reads ~560 parent commitments, so it never bound under 70 threads. The parent list is never empty, so no floor is needed at all. - The `max(8)` on the multiproof aggregate and the `max(64)` on `parents_and_points`: no measurable effect in isolation. Both become `max(1)`, which is what keeps an empty input away from `chunks(0)` (a panic) — the verifier reaches `parents_and_points` with whatever key set it was handed, and `MultiPoint::open` is a library entry point. `MIN_NODE_CHUNK` and `MSM_MIN_CHUNK` stay: the first also guards the all-hits case (an empty rebuild list, the norm once the refresh lifts the hit rate to 98%), the second binds on every proof once the pool has more than 16 threads, and what both are for — several proofs sharing one pool — is not something a one-proof-at-a-time benchmark can see. Outputs are unchanged; proofs stay byte-identical. Co-Authored-By: Claude Fable 5.1 --- banderwagon/src/salt_committer.rs | 98 +++++++++++++------------------ ipa-multipoint/src/multiproof.rs | 5 +- salt/src/proof/shape.rs | 4 +- salt/src/proof/subtrie.rs | 7 +-- 4 files changed, 46 insertions(+), 68 deletions(-) diff --git a/banderwagon/src/salt_committer.rs b/banderwagon/src/salt_committer.rs index 22d13b5e..bafa1030 100644 --- a/banderwagon/src/salt_committer.rs +++ b/banderwagon/src/salt_committer.rs @@ -33,7 +33,7 @@ use ark_ff::Zero; use salt_macros::iter; use salt_macros::prelude::*; -use std::vec::Vec; +use std::{vec, vec::Vec}; /// Precomputed Multi-Scalar Multiplication engine for fixed base points. /// @@ -209,7 +209,7 @@ impl Committer { pub fn mul_index(&self, scalar: &Fr, g_i: usize) -> Element { use std::arch::x86_64::{_mm_prefetch, _MM_HINT_T0}; - let mut digits = ScalarWindows::new(scalar, self.window_size); + let chunks = calculate_prefetch_index(scalar, self.window_size); let mut result = EdwardsProjective::default(); let precomp_table = &self.tables[g_i]; @@ -220,7 +220,7 @@ impl Committer { let mut c_next = 0; // prefetch first point - let data_0 = digits.next().expect("a scalar has at least one window") as usize; + let data_0 = unsafe { *chunks.get_unchecked(0) } as usize; if data_0 >= half_wnd { c_next = 1; idx_next = wnd_size - data_0; @@ -236,9 +236,9 @@ impl Committer { idx = idx_next; // calculate point - for (i, digit) in (1..).zip(digits) { + for i in 1..chunks.len() { // fetch next point - idx_next = digit as usize + c_next; + idx_next = unsafe { *chunks.get_unchecked(i) as usize } + c_next; let carry = c_next; if idx_next >= half_wnd { c_next = 1; @@ -303,14 +303,14 @@ impl Committer { /// The result of `scalar * G[g_i]` as an `Element`. #[cfg(any(not(target_arch = "x86_64"), not(feature = "std")))] pub fn mul_index(&self, scalar: &Fr, g_i: usize) -> Element { - let digits = ScalarWindows::new(scalar, self.window_size); + let chunks = calculate_prefetch_index(scalar, self.window_size); let mut carry = 0; let half_win = 1 << (self.window_size - 1); let mut result = EdwardsProjective::default(); let precom_table = &self.tables[g_i]; - let mut ponits = Vec::with_capacity(digits.len()); - for (i, digit) in digits.enumerate() { - let mut index = (digit + carry) as usize; + let mut ponits = Vec::with_capacity(chunks.len()); + for i in 0..chunks.len() { + let mut index = (chunks[i] + carry) as usize; if index == 0 { continue; } @@ -437,61 +437,43 @@ fn add_affine_point(result: &mut EdwardsProjective, p2_x: &Fq, p2_y: &Fq) { mont_mul_asm(&mut result.z.0 .0, &f.0 .0, &g.0 .0); } -/// The `w`-bit windows of a scalar, least significant first: the digits that index the -/// precomputed multiplication tables. +/// Decomposes a scalar into windows for efficient table lookups. /// -/// Fr's bit length is 253 plus a carry, so the windows cover 254 bits. They are produced on -/// the fly from the scalar's limbs, so a multiplication never touches the heap. -#[derive(Clone, Copy)] -struct ScalarWindows { - /// The scalar out of Montgomery form. - limbs: [u64; 4], - /// Window size in bits. - w: usize, - /// First bit of the next window. - start_bit: usize, -} - -impl ScalarWindows { - #[inline] - fn new(scalar: &Fr, w: usize) -> Self { - Self { - limbs: scalar.into_bigint().0, - w, - start_bit: 0, - } - } - - /// Number of windows a scalar has at this window size. - #[cfg(any(not(target_arch = "x86_64"), not(feature = "std")))] - #[inline] - fn len(&self) -> usize { - 254usize.div_ceil(self.w) - } -} - -impl Iterator for ScalarWindows { - type Item = u64; - - #[inline] - fn next(&mut self) -> Option { - let start_bit = self.start_bit; - if start_bit >= 254 { - return None; - } - self.start_bit += self.w; - let w = self.w; +/// This function splits a scalar into w-bit windows for use with the +/// precomputed multiplication tables. Each window represents a digit +/// in the windowed representation of the scalar. +/// +/// # Arguments +/// +/// * `scalar` - The scalar to decompose +/// * `w` - Window size in bits +/// +/// # Returns +/// +/// A vector of w-bit values representing the windowed decomposition. +#[inline] +fn calculate_prefetch_index(scalar: &Fr, w: usize) -> Vec { + // Convert scalar from Montgomery form to big integer + let source_vec = scalar.into_bigint().0; + let mut index_vec = vec![]; + + // Extract w bits of data from a scalar of n bits length + // Fr's bit length is 253 + Carry, so the maximum length is 254 + for start_bit in (0..254).step_by(w) { let source_i = start_bit >> 6; let offset_in_i = start_bit & 63; - let mut d = (self.limbs[source_i] >> offset_in_i) & ((1 << w) - 1); - // If the data is not enough, take the remaining data from the next limb - if offset_in_i + w > 64 && source_i < self.limbs.len() - 1 { + let mut d = (source_vec[source_i] >> offset_in_i) & ((1 << w) - 1); + // If the data is not enough, take the remaining data from the next + if offset_in_i + w > 64 && source_i < source_vec.len() - 1 { let left = w - (64 - offset_in_i); - d |= (self.limbs[source_i + 1] & ((1 << left) - 1)) << (64 - offset_in_i); - } - Some(d) + d |= (source_vec[source_i + 1] & ((1 << left) - 1)) << (64 - offset_in_i); + }; + + index_vec.push(d) } + + index_vec } #[cfg(test)] diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index d32eb04c..3b8b1aa9 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -120,9 +120,8 @@ impl MultiPoint { let grouped_queries: Vec<_> = into_iter!(grouped_queries).collect(); - // At least a few point groups per task: each is a 256-coefficient pass, too little to - // farm out one by one. - let chunk_size = grouped_queries.len().div_ceil(num_threads!()).max(8); + // `max(1)`: an empty query set must not reach `chunks(0)`, which panics. + let chunk_size = grouped_queries.len().div_ceil(num_threads!()).max(1); // aggregate all of the queries evaluated at the same point; // accumulate scaled polynomials in place instead of cloning each diff --git a/salt/src/proof/shape.rs b/salt/src/proof/shape.rs index 103bc15a..37771e46 100644 --- a/salt/src/proof/shape.rs +++ b/salt/src/proof/shape.rs @@ -64,7 +64,9 @@ pub(crate) fn parents_and_points( ) { // One accumulator pair per thread chunk, merged once at the end. (A // per-key map-reduce would allocate and merge two BTreeMaps per key.) - let chunk_size = salt_keys.len().div_ceil(num_threads!()).max(64); + // `max(1)`: the verifier calls this with whatever key set it was handed, including none, + // and `chunks(0)` panics. + let chunk_size = salt_keys.len().div_ceil(num_threads!()).max(1); let partials: Vec<_> = chunks!(salt_keys, chunk_size) .map(|chunk| { let mut internal_nodes: BTreeMap> = BTreeMap::new(); diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index bf441239..3c672076 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -50,8 +50,6 @@ use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; type FxHashSet = HashSet; -/// Smallest number of parent commitments one parallel task reads. -const MIN_PARENT_CHUNK: usize = 8; /// Smallest number of internal nodes one parallel task materializes (256 child reads each). const MIN_NODE_CHUNK: usize = 4; @@ -602,10 +600,7 @@ where .chain(leaf_nodes.keys()) .map(|&parent| connect_parent_id(parent)) .collect(); - let commitment_chunk = parent_ids - .len() - .div_ceil(num_threads!()) - .max(MIN_PARENT_CHUNK); + let commitment_chunk = parent_ids.len().div_ceil(num_threads!()); let parents_read: Vec<(NodeId, CommitmentBytes)> = chunks!(parent_ids, commitment_chunk) .map(|chunk| { chunk From 6d5298f52cadfa3f14f0a6b34fa116a72618fe05 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 21 Sep 2026 18:17:58 +0800 Subject: [PATCH 10/18] refactor(proof): drop the last chunk floors and the cold-cache bench hook An end-to-end replay-full run (5000 mainnet blocks, 5 alternating rounds per arm) put numbers on what the isolated ablation could not: - `MIN_NODE_CHUNK` and `MSM_MIN_CHUNK` are worth 1.3% of pipeline throughput with 8 concurrent witness builds and cost 1.4% with one. Both become `.max(1)`, which only keeps an empty input off `chunks(0)`. - The cold-cache bench groups and the public `clear_node_poly_cache` hook they needed (reverts 4e613ce): the pipeline run now answers the question they were added for, so the crate does not need the extra public symbol. - `msm_windowed_timing`, an ignored timing harness. Co-Authored-By: Claude Fable 5.1 --- banderwagon/src/msm.rs | 44 -------------------------------------- ipa-multipoint/src/ipa.rs | 7 ++---- salt/benches/proof.rs | 45 ++------------------------------------- salt/src/lib.rs | 5 +---- salt/src/proof/mod.rs | 2 +- salt/src/proof/subtrie.rs | 27 ++--------------------- 6 files changed, 8 insertions(+), 122 deletions(-) diff --git a/banderwagon/src/msm.rs b/banderwagon/src/msm.rs index c18dd670..196d4273 100644 --- a/banderwagon/src/msm.rs +++ b/banderwagon/src/msm.rs @@ -279,50 +279,6 @@ mod tests { } } - /// Timing of [`msm_windowed`] at the sizes SALT verification actually hits: - /// the IPA's closing MSM is a fixed 274 points, an ordinary head witness - /// coalesces to a few hundred to a couple of thousand commitments, and a - /// dense one to tens of thousands. Run with - /// `cargo test -p banderwagon --release msm_windowed_timing -- --ignored --nocapture`. - #[test] - #[ignore = "timing, not a correctness check"] - fn msm_windowed_timing() { - use ark_ec::CurveGroup; - use ark_ff::UniformRand; - use rand_chacha::rand_core::SeedableRng; - use rand_chacha::ChaCha20Rng; - use std::{println, time::Instant}; - - let mut rng = ChaCha20Rng::from_seed([5u8; 32]); - // Spin the thread pool up before the first timed size. - { - let warm: Vec = (0..512) - .map(|_| EdwardsProjective::rand(&mut rng)) - .collect(); - let warm = EdwardsProjective::normalize_batch(&warm); - let scalars: Vec = (0..warm.len()).map(|_| Fr::rand(&mut rng)).collect(); - for _ in 0..20 { - let _ = core::hint::black_box(msm_windowed(&warm, &scalars)); - } - } - for size in [274usize, 400, 1000, 2335, 8192] { - let bases_proj: Vec = (0..size) - .map(|_| EdwardsProjective::rand(&mut rng)) - .collect(); - let bases = EdwardsProjective::normalize_batch(&bases_proj); - let scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); - - let reps = if size <= 1000 { 50 } else { 10 }; - let _ = msm_windowed(&bases, &scalars); - let start = Instant::now(); - for _ in 0..reps { - let _ = core::hint::black_box(msm_windowed(&bases, &scalars)); - } - let us = start.elapsed().as_secs_f64() * 1e6 / reps as f64; - println!("msm_windowed n = {size:>5}: {us:>9.1} us"); - } - } - /// The bucket counts [`bucket_len`] hands out must cover every digit /// [`make_digits`] can produce: a bucket index is `|digit| - 1`, so every /// window needs `|digit| <= bucket_len`. diff --git a/ipa-multipoint/src/ipa.rs b/ipa-multipoint/src/ipa.rs index 2032f812..5bd2a1c6 100644 --- a/ipa-multipoint/src/ipa.rs +++ b/ipa-multipoint/src/ipa.rs @@ -320,14 +320,11 @@ pub fn create_with_precomp( } } -/// Smallest number of terms one parallel task takes: below this the rayon dispatch costs more -/// than the fixed-base multiplications it hands out, and concurrent proofs only contend. -const MSM_MIN_CHUNK: usize = 8; - /// Sums `scalar · G[index]` over the given terms using precomputed wNAF /// tables, splitting the terms across threads. pub(crate) fn fixed_base_msm(precomp: &Committer, terms: &[(usize, Fr)]) -> Element { - let chunk_size = terms.len().div_ceil(num_threads!()).max(MSM_MIN_CHUNK); + // `max(1)`: an empty term list must not reach `chunks(0)`, which panics. + let chunk_size = terms.len().div_ceil(num_threads!()).max(1); chunks!(terms, chunk_size) .map(|chunk| { let mut acc = Element::zero(); diff --git a/salt/benches/proof.rs b/salt/benches/proof.rs index 49672670..25a9ee99 100644 --- a/salt/benches/proof.rs +++ b/salt/benches/proof.rs @@ -11,12 +11,9 @@ //! cargo bench --package salt --bench proof //! ``` -use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use rand::{rngs::StdRng, Rng, SeedableRng}; -use salt::{ - clear_node_poly_cache, types::*, EphemeralSaltState, MemStore, SaltProof, SaltWitness, - StateRoot, Witness, -}; +use salt::{types::*, EphemeralSaltState, MemStore, SaltProof, SaltWitness, StateRoot, Witness}; use std::collections::BTreeMap; use std::hint::black_box; use std::sync::OnceLock; @@ -103,25 +100,6 @@ fn bench_salt_proof(c: &mut Criterion) { } group.finish(); - // Cold: the node-polynomial cache is cleared before every iteration, so each proof pays - // the store reads and map-to-field passes for every node on its paths. A repeated warm - // benchmark measures a 100% hit rate, which no caller sees; production sits between the - // two (44% on mainnet without the refresh, 98% with it). - let mut group = c.benchmark_group("salt_proof/create-cold"); - group.sample_size(10); - group.measurement_time(Duration::from_secs(10)); - for n in [16usize, 256, 2048] { - let keys = proof_keys(s, n); - group.bench_with_input(BenchmarkId::from_parameter(n), &keys, |b, keys| { - b.iter_batched( - clear_node_poly_cache, - |()| black_box(SaltProof::create(keys.iter().copied(), &s.store).unwrap()), - BatchSize::PerIteration, - ); - }); - } - group.finish(); - let mut group = c.benchmark_group("salt_proof/check"); group.sample_size(10); group.measurement_time(Duration::from_secs(10)); @@ -198,25 +176,6 @@ fn bench_witness(c: &mut Criterion) { } group.finish(); - let mut group = c.benchmark_group("witness/create-cold"); - group.sample_size(10); - group.measurement_time(Duration::from_secs(15)); - for n in [256usize, 1024] { - let (lookups, updates) = witness_workload(s, n, &mut rng); - group.bench_with_input( - BenchmarkId::from_parameter(n), - &(lookups, updates), - |b, (lookups, updates)| { - b.iter_batched( - clear_node_poly_cache, - |()| black_box(Witness::create([], lookups.iter(), updates, &s.store).unwrap()), - BatchSize::PerIteration, - ); - }, - ); - } - group.finish(); - let mut group = c.benchmark_group("witness/verify"); group.sample_size(10); group.measurement_time(Duration::from_secs(15)); diff --git a/salt/src/lib.rs b/salt/src/lib.rs index ff63bdcf..11b831fd 100644 --- a/salt/src/lib.rs +++ b/salt/src/lib.rs @@ -18,10 +18,7 @@ pub(crate) type Lazy = spin::Lazy; pub mod constant; pub mod empty_salt; pub mod proof; -pub use proof::{ - clear_node_poly_cache, fx_hashmap_serde, NodePolyRefresh, ProofError, SaltProof, SaltWitness, - Witness, -}; +pub use proof::{fx_hashmap_serde, NodePolyRefresh, ProofError, SaltProof, SaltWitness, Witness}; pub mod state; pub use state::{ hasher, state::EphemeralSaltState, state::PlainStateProvider, updates::StateUpdates, diff --git a/salt/src/proof/mod.rs b/salt/src/proof/mod.rs index 9bcc4a6b..297f102c 100644 --- a/salt/src/proof/mod.rs +++ b/salt/src/proof/mod.rs @@ -78,7 +78,7 @@ mod test_utils; pub use prover::{fx_hashmap_serde, SaltProof, SerdeCommitment, SerdeMultiPointProof}; pub use salt_witness::SaltWitness; -pub use subtrie::{clear_node_poly_cache, NodePolyRefresh}; +pub use subtrie::NodePolyRefresh; pub use witness::Witness; /// Error type for proof operations diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 3c672076..effa405d 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -50,9 +50,6 @@ use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; type FxHashSet = HashSet; -/// Smallest number of internal nodes one parallel task materializes (256 child reads each). -const MIN_NODE_CHUNK: usize = 4; - /// Process-wide cache of node polynomials, keyed by node id and validated by the node's /// commitment. /// @@ -91,16 +88,6 @@ mod node_poly_cache { .map(|(_, poly)| Arc::clone(poly)) } - /// Drops every entry, freeing each shard's map after its lock is released. - pub(super) fn clear() { - for shard in CACHE.iter() { - let mut guard = shard.write(); - let dropped = core::mem::take(&mut *guard); - drop(guard); - drop(dropped); - } - } - pub(super) fn insert(node: NodeId, commitment: CommitmentBytes, poly: Arc) { let mut guard = shard(node).write(); // Only a new node grows the shard. Whatever the insert evicts or replaces is dropped @@ -117,17 +104,6 @@ mod node_poly_cache { } } -/// Drops every entry of the process-wide node-polynomial cache. -/// -/// Nothing in production wants this — carrying entries across witnesses is the point of the -/// cache. It exists so a benchmark can time a *cold* witness build, the shape a witness takes -/// over nodes no earlier witness has touched: without it a benchmark that repeats one witness -/// measures a 100% hit rate, which no validator sees. -#[doc(hidden)] -pub fn clear_node_poly_cache() { - node_poly_cache::clear(); -} - /// One block's header-verified trie transition, used to advance the node-polynomial cache to /// that block's post-state while the block's own witness is built. /// @@ -647,7 +623,8 @@ where let internal_nodes: Vec<_> = internal_nodes.into_iter().collect(); let leaf_nodes: Vec<_> = leaf_nodes.into_iter().collect(); let internal_polys = resolve_polys(&internal_nodes, &parent_bytes, |missing| { - let chunk_size = missing.len().div_ceil(num_threads!()).max(MIN_NODE_CHUNK); + // `max(1)`: `missing` is empty when every node hits the cache, and `chunks(0)` panics. + let chunk_size = missing.len().div_ceil(num_threads!()).max(1); let chunks = chunks!(missing, chunk_size) .map(|nodes| { let scalars = multi_commitments_to_scalars(store, nodes)?; From 94f1c4ad9f1278d8ee717a37481f1b772dbf13d3 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Mon, 21 Sep 2026 18:20:54 +0800 Subject: [PATCH 11/18] refactor(proof): move the node-polynomial cache refresh to its own branch `NodePolyRefresh` and the three `create_with_refresh` entry points leave this branch for `liquan/perf/proof-refresh`, which carries them on top of it unchanged. The refresh is 336 lines of product code and about 850 of tests, close to half of this PR, and the part that most deserves a review of its own; end to end it is worth 12.5% of replay-full throughput on top of the cache, which stays here and is worth 16% by itself. Without a refresh the cache is filled by rebuilds alone, so a node the previous block wrote is rebuilt once under its new commitment. Co-Authored-By: Claude Fable 5.1 --- salt/src/lib.rs | 2 +- salt/src/proof/mod.rs | 1 - salt/src/proof/prover.rs | 19 +- salt/src/proof/salt_witness.rs | 18 +- salt/src/proof/subtrie.rs | 1066 +------------------------------- salt/src/proof/test_utils.rs | 47 +- salt/src/proof/witness.rs | 59 +- 7 files changed, 23 insertions(+), 1189 deletions(-) diff --git a/salt/src/lib.rs b/salt/src/lib.rs index 11b831fd..0ef061f6 100644 --- a/salt/src/lib.rs +++ b/salt/src/lib.rs @@ -18,7 +18,7 @@ pub(crate) type Lazy = spin::Lazy; pub mod constant; pub mod empty_salt; pub mod proof; -pub use proof::{fx_hashmap_serde, NodePolyRefresh, ProofError, SaltProof, SaltWitness, Witness}; +pub use proof::{fx_hashmap_serde, ProofError, SaltProof, SaltWitness, Witness}; pub mod state; pub use state::{ hasher, state::EphemeralSaltState, state::PlainStateProvider, updates::StateUpdates, diff --git a/salt/src/proof/mod.rs b/salt/src/proof/mod.rs index 297f102c..bff27f0c 100644 --- a/salt/src/proof/mod.rs +++ b/salt/src/proof/mod.rs @@ -78,7 +78,6 @@ mod test_utils; pub use prover::{fx_hashmap_serde, SaltProof, SerdeCommitment, SerdeMultiPointProof}; pub use salt_witness::SaltWitness; -pub use subtrie::NodePolyRefresh; pub use witness::Witness; /// Error type for proof operations diff --git a/salt/src/proof/prover.rs b/salt/src/proof/prover.rs index 2bd97477..c4c6c7a4 100644 --- a/salt/src/proof/prover.rs +++ b/salt/src/proof/prover.rs @@ -3,7 +3,7 @@ use crate::{ constant::{BUCKET_SLOT_ID_MASK, DOMAIN_SIZE, STARTING_NODE_ID}, proof::{ shape::{connect_parent_id, logic_parent_id, parents_and_points}, - subtrie::{create_sub_trie, NodePolyRefresh}, + subtrie::create_sub_trie, ProofError, ProofResult, }, traits::{StateReader, TrieReader}, @@ -213,21 +213,6 @@ pub(crate) fn slot_to_field(entry: &Option) -> Fr { impl SaltProof { /// Create a new proof. pub fn create(keys: I, store: &Store) -> Result - where - I: IntoIterator, - Store: StateReader + TrieReader, - { - Self::create_with_refresh(keys, store, None) - } - - /// [`Self::create`], additionally advancing the node-polynomial cache with `refresh`, the - /// witnessed block's own transition, once this proof's polynomials are resolved (see - /// [`NodePolyRefresh`]). The proof itself is the same with or without a refresh. - pub fn create_with_refresh( - keys: I, - store: &Store, - refresh: Option<&NodePolyRefresh<'_>>, - ) -> Result where I: IntoIterator, Store: StateReader + TrieReader, @@ -242,7 +227,7 @@ impl SaltProof { } keys.dedup(); - let (prover_queries, parents_commitments, levels) = create_sub_trie(store, &keys, refresh)?; + let (prover_queries, parents_commitments, levels) = create_sub_trie(store, &keys)?; let mut transcript = Transcript::new(b"st"); diff --git a/salt/src/proof/salt_witness.rs b/salt/src/proof/salt_witness.rs index 61d851fa..bc332c21 100644 --- a/salt/src/proof/salt_witness.rs +++ b/salt/src/proof/salt_witness.rs @@ -5,7 +5,7 @@ //! enforces critical security properties to prevent state manipulation attacks. use crate::{ constant::ROOT_NODE_ID, - proof::{subtrie::NodePolyRefresh, ProofError, SaltProof}, + proof::{ProofError, SaltProof}, traits::{StateReader, TrieReader}, types::*, }; @@ -80,20 +80,6 @@ impl SaltWitness { /// - For regular keys, retrieves their values (or None if non-existent) /// - The proof is generated for all requested keys to ensure completeness pub fn create(keys: &[SaltKey], store: &Store) -> Result - where - Store: StateReader + TrieReader, - { - Self::create_with_refresh(keys, store, None) - } - - /// [`Self::create`], additionally advancing the node-polynomial cache with `refresh`, the - /// witnessed block's own transition, once the proof's polynomials are resolved (see - /// [`NodePolyRefresh`]). The witness itself is the same with or without a refresh. - pub fn create_with_refresh( - keys: &[SaltKey], - store: &Store, - refresh: Option<&NodePolyRefresh<'_>>, - ) -> Result where Store: StateReader + TrieReader, { @@ -114,7 +100,7 @@ impl SaltWitness { }) .collect::, _>>()?; - let proof = SaltProof::create_with_refresh(kvs.keys().copied(), store, refresh)?; + let proof = SaltProof::create(kvs.keys().copied(), store)?; Ok(SaltWitness { kvs, proof }) } diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index effa405d..2079b134 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -13,28 +13,19 @@ //! 3. Build minimal node hierarchy using [`parents_and_points`] //! 4. Collect cryptographic commitments for all parent nodes //! 5. Generate IPA prover queries for leaf nodes (bucket contents) and internal nodes (child commitments) -//! 6. Optionally advance the process-wide node-polynomial cache to the witnessed block's -//! post-state from the block's own transition ([`NodePolyRefresh`]) use crate::{ constant::{ default_commitment, BUCKET_SLOT_BITS, BUCKET_SLOT_ID_MASK, DOMAIN_SIZE, MAX_SUBTREE_LEVELS, - META_BUCKET_SIZE, NUM_META_BUCKETS, ROOT_NODE_ID, STARTING_NODE_ID, + META_BUCKET_SIZE, NUM_META_BUCKETS, STARTING_NODE_ID, }, proof::{ prover::slot_to_field, shape::{connect_parent_id, logic_parent_id, parents_and_points}, ProofError, ProofResult, SerdeCommitment, }, - state::updates::StateUpdates, traits::{StateReader, TrieReader}, - trie::node_utils::{ - bucket_root_node_id, get_child_node, get_parent_node, subtree_leaf_for_key, - subtree_leaf_start_key, subtree_root_level, vc_position_in_parent, - }, - types::{ - bucket_id_from_metadata_key, get_bfs_level, get_local_number, BucketId, BucketMeta, - CommitmentBytes, NodeId, SaltKey, SaltValue, METADATA_KEYS_RANGE, - }, + trie::node_utils::{get_child_node, subtree_leaf_start_key, subtree_root_level}, + types::{BucketId, BucketMeta, CommitmentBytes, NodeId, SaltKey}, SlotId, }; use banderwagon::{Element, Fr, Zero}; @@ -45,10 +36,9 @@ use salt_macros::{chunks, iter, num_threads}; use std::collections::{BTreeMap, BTreeSet}; use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; -use hashbrown::{HashMap, HashSet}; +use hashbrown::HashMap; use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; -type FxHashSet = HashSet; /// Process-wide cache of node polynomials, keyed by node id and validated by the node's /// commitment. @@ -104,250 +94,6 @@ mod node_poly_cache { } } -/// One block's header-verified trie transition, used to advance the node-polynomial cache to -/// that block's post-state while the block's own witness is built. -/// -/// The two fields are the pair [`StateRoot::update_fin`](crate::trie::trie::StateRoot::update_fin) -/// produced for the block: every node whose commitment changed with its pre- and post-state -/// commitment, and every slot that changed with its old and new value. The witness proves the -/// pre-state, so the polynomials it resolves are exactly the bases this transition transforms; -/// the refresh runs after the witness's own lookups, patches each changed node's polynomial at -/// its changed positions and inserts the result under the node's new commitment, so the next -/// block's witness hits instead of rebuilding from storage. Entries stay validated by -/// commitment, so a stale or foreign entry is only ever a miss, never a wrong witness. -/// -/// The store the witness reads must be the transition's pre-state, the view `update_fin` ran -/// on; a parent the witness read at any other commitment is skipped. Applying one transition -/// again (a retried witness) derives the same polynomial from a base validated at the same -/// old commitment and inserts the same entry, so a refresh is idempotent. -pub struct NodePolyRefresh<'a> { - /// The block's changed nodes as `(node, (old commitment, new commitment))`. - pub trie_updates: &'a [(NodeId, (CommitmentBytes, CommitmentBytes))], - /// The block's changed slots as `(key, (old value, new value))`. - pub state_updates: &'a StateUpdates, -} - -/// The changed positions of one parent's polynomial, with the parent's pre- and post-state -/// commitment. -struct ParentPatch { - old: CommitmentBytes, - new: CommitmentBytes, - positions: Vec<(usize, Fr)>, -} - -/// Derives, from the block's outputs alone, which position of which cached polynomial takes -/// which new scalar. Keys are physical node ids as the cache uses them -/// ([`connect_parent_id`]): a bucket whose subtree top changed is keyed by its main-trie -/// bucket root, like the witness keys it. -/// -/// The plan is complete for every parent it emits: every node whose commitment -/// changed is in `trie_updates`, so a parent's unchanged positions keep the base's values and -/// its changed positions are exactly its changed children (internal node) or changed slots -/// (leaf). Shapes where a per-position patch is wrong under a valid commitment are excluded -/// whole: a bucket whose capacity changes is frozen and every patch inside it is dropped, -/// while the bucket root's position in its main-trie parent is still patched, because that -/// `(old, new)` pair is a true transition of the main-trie node. -fn refresh_plan(refresh: &NodePolyRefresh<'_>) -> FxHashMap { - // Fold duplicates to the first old and the last new commitment. - let mut changed: FxHashMap = - FxHashMap::with_capacity_and_hasher(refresh.trie_updates.len(), FxBuildHasher); - for &(node, (old, new)) in refresh.trie_updates { - changed - .entry(node) - .and_modify(|pair| pair.1 = new) - .or_insert((old, new)); - } - - // Buckets whose capacity changes: the trie emits their old top with a default old - // commitment and resets contracted nodes without their children, so nothing inside them - // is patched. - let capacity = |value: &Option| { - value - .as_ref() - .and_then(|value| BucketMeta::try_from(value).ok()) - .map(|meta| meta.capacity) - }; - let frozen: FxHashSet = refresh - .state_updates - .data - .range(METADATA_KEYS_RANGE) - .filter(|(_, (old, new))| match (capacity(old), capacity(new)) { - (Some(old), Some(new)) => old != new, - _ => true, - }) - .map(|(key, _)| bucket_id_from_metadata_key(*key)) - .collect(); - - // A bucket is expanded when any of its subtree nodes changed; its slots then live in - // subtree leaves rather than in the bucket root. - let expanded: FxHashSet = changed - .keys() - .filter(|node| *node >> BUCKET_SLOT_BITS != 0) - .map(|node| (node >> BUCKET_SLOT_BITS) as BucketId) - .collect(); - - // Changed children, keyed by the physical id of the parent whose polynomial holds them. - // A changed subtree node is recorded under its own id unless it is the top, which - // the trie records under the bucket root; so a subtree child's parent is the top exactly - // when the parent is absent from the transition. - let mut child_patches: Vec<(NodeId, usize, CommitmentBytes)> = - Vec::with_capacity(changed.len()); - for (&child, &(_, new)) in &changed { - if child == ROOT_NODE_ID { - continue; - } - let physical = if child >> BUCKET_SLOT_BITS != 0 { - let bucket = (child >> BUCKET_SLOT_BITS) as BucketId; - if frozen.contains(&bucket) { - continue; - } - // A subtree node at local level 0 is always a top, never a child. - if get_bfs_level(get_local_number(child)) == 0 { - continue; - } - let logical = get_parent_node(&child); - if changed.contains_key(&logical) { - logical - } else { - debug_assert!( - changed.contains_key(&bucket_root_node_id(bucket)), - "subtree node {child} changed but neither its parent nor its bucket root did" - ); - bucket_root_node_id(bucket) - } - } else { - get_parent_node(&child) - }; - child_patches.push((physical, vc_position_in_parent(&child), new)); - } - - // Changed slots, keyed by their leaf. A key whose leaf is absent from the transition - // (a slot beyond the bucket's capacity, which the trie drops) is skipped, never - // re-attributed to the bucket root. - let mut leaf_patches: Vec<(NodeId, usize, Fr)> = - Vec::with_capacity(refresh.state_updates.data.len()); - for (key, (_, new)) in &refresh.state_updates.data { - let leaf = if key.is_in_meta_bucket() { - bucket_root_node_id(key.bucket_id()) - } else { - let bucket = key.bucket_id(); - if frozen.contains(&bucket) { - continue; - } - if expanded.contains(&bucket) { - let leaf = subtree_leaf_for_key(key); - if !changed.contains_key(&leaf) { - continue; - } - leaf - } else { - bucket_root_node_id(bucket) - } - }; - // An emptied slot takes the bucket kind's default, as `leaf_polynomial` fills it. - let scalar = match new { - Some(_) => slot_to_field(new), - None => empty_slot_scalar(key.bucket_id()), - }; - leaf_patches.push((leaf, slot_position(key), scalar)); - } - - // One batch maps every changed child commitment to its scalar; it is the same map the - // rebuild applies to a child read from storage. - let child_commitments: Vec = child_patches - .iter() - .map(|(_, _, commitment)| *commitment) - .collect(); - let child_scalars = Element::hash_commitments(&child_commitments); - - // Group by parent; a parent without an (old, new) pair of its own is not patched. - let mut plan: FxHashMap = FxHashMap::default(); - let patches = child_patches - .iter() - .zip(child_scalars) - .map(|(&(parent, position, _), scalar)| (parent, position, scalar)) - .chain(leaf_patches); - for (parent, position, scalar) in patches { - let Some(&(old, new)) = changed.get(&parent) else { - continue; - }; - plan.entry(parent) - .or_insert_with(|| ParentPatch { - old, - new, - positions: Vec::new(), - }) - .positions - .push((position, scalar)); - } - plan -} - -/// Applies a [`refresh_plan`]: for every planned parent, copies a base polynomial that is -/// validated at the parent's old commitment, overwrites the changed positions and inserts the -/// result under the new commitment. Returns the number of entries inserted. -/// -/// The base is the witness's in-hand polynomial when the witness read the parent at exactly -/// the old commitment, else the cached entry for the old commitment, else the parent is -/// skipped (an entry is only ever derived from a base validated at `old`). Every -/// insert goes through the shard write lock as a whole `(commitment, polynomial)` pair, so -/// concurrent witnesses can only ever replace an entry with another valid one. -fn apply_refresh( - plan: FxHashMap, - in_hand: &FxHashMap)>, -) -> usize { - let mut refreshed = 0; - for (physical, patch) in plan { - let base = match in_hand.get(&physical) { - Some((read, poly)) => { - // The witness resolved this polynomial against the commitment it read, so - // it is the pre-state base only if that commitment is the transition's old. - let read_at_old = *read == patch.old; - debug_assert!( - read_at_old, - "node {physical}: the witness read a commitment other than the refresh's pre-state" - ); - if !read_at_old { - continue; - } - Arc::clone(poly) - } - None => match node_poly_cache::get(physical, &patch.old) { - Some(poly) => poly, - None => continue, - }, - }; - let mut values = base.values().to_vec(); - #[cfg(debug_assertions)] - assert_patch_commits(physical, &patch, &values); - for &(position, scalar) in &patch.positions { - values[position] = scalar; - } - node_poly_cache::insert(physical, patch.new, Arc::new(LagrangeBasis::new(values))); - refreshed += 1; - } - refreshed -} - -/// Debug-build self-check of one refreshed entry: the patched positions must carry the -/// parent's commitment from `old` to exactly `new`, `old + Σ G_i·(s_i − base_i) == new`, -/// which is how the trie produced `new`. The check reads no store and fails on a wrong -/// position, a wrong scalar, and a missing or extra position, which commitment validation -/// alone cannot see. -#[cfg(debug_assertions)] -fn assert_patch_commits(physical: NodeId, patch: &ParentPatch, base: &[Fr]) { - let committer = crate::trie::trie::shared_committer(); - let mut acc = Element::from_bytes_unchecked_uncompressed(patch.old); - for &(position, scalar) in &patch.positions { - acc += committer.mul_index(&(scalar - base[position]), position); - } - let got = Element::batch_to_commitments(&[acc])[0]; - assert_eq!( - got, patch.new, - "refreshed polynomial of node {physical} does not commit to its new commitment" - ); -} - // Constants for improved code readability const METADATA_BUCKET_LEVEL: u8 = 1; const SLOT_INDEX_MASK: u64 = 0xff; @@ -496,9 +242,6 @@ fn create_prover_queries( /// /// * `store` - Storage backend providing access to both trie commitments and bucket data /// * `salt_keys` - Pre-sorted and deduplicated keys to generate proofs for -/// * `refresh` - The witnessed block's own transition, applied to the node-polynomial cache -/// once this proof's polynomials are resolved (see [`NodePolyRefresh`]); `None` leaves the -/// cache to the lookups and rebuild inserts alone /// /// # Returns /// @@ -514,7 +257,6 @@ fn create_prover_queries( pub(crate) fn create_sub_trie( store: &Store, salt_keys: &[SaltKey], - refresh: Option<&NodePolyRefresh<'_>>, ) -> ProofResult where Store: StateReader + TrieReader, @@ -642,18 +384,9 @@ where .collect() })?; - // Every resolved polynomial by physical parent id, with the commitment it was read at: - // the bases a refresh patches (the witness reads the pre-state, so these are the - // polynomials the block's transition transforms). - let mut in_hand: FxHashMap)> = - FxHashMap::default(); let mut queries: Vec = Vec::new(); let nodes = internal_nodes.into_iter().chain(leaf_nodes); for ((parent, points), poly) in nodes.zip(internal_polys.into_iter().chain(leaf_polys)) { - if refresh.is_some() { - let physical = connect_parent_id(parent); - in_hand.insert(physical, (parent_bytes[&physical], Arc::clone(&poly))); - } queries.extend(create_prover_queries( parent_commitment(parent)?, poly, @@ -661,14 +394,6 @@ where )); } - // Step 6: Advance the cache to the witnessed block's post-state. The queries above are - // already built from the pre-state polynomials, so this proof is unaffected; it runs - // here rather than after the proof so the entries land before the next block's witness - // looks them up. - if let Some(refresh) = refresh { - apply_refresh(refresh_plan(refresh), &in_hand); - } - Ok((queries, parents_commitments, buckets_level)) } @@ -770,17 +495,11 @@ fn slot_position(key: &SaltKey) -> usize { #[cfg(test)] mod tests { use super::*; - use crate::constant::{MAIN_TRIE_LEVELS, META_BUCKET_SIZE, MIN_BUCKET_SIZE, ROOT_NODE_ID}; + use crate::constant::{META_BUCKET_SIZE, ROOT_NODE_ID}; use crate::proof::test_utils::*; use crate::{ - mem_store::MemStore, - proof::{ - prover::{SaltProof, PRECOMPUTED_WEIGHTS}, - shape::{encode_parent, is_encoded_node}, - }, - state::state::EphemeralSaltState, - trie::trie::{StateRoot, TrieUpdates}, - types::bucket_metadata_key, + mem_store::MemStore, proof::prover::PRECOMPUTED_WEIGHTS, state::state::EphemeralSaltState, + trie::trie::StateRoot, }; use ark_ff::BigInt; use banderwagon::{Fr, Zero}; @@ -825,7 +544,7 @@ mod tests { #[test] fn create_sub_trie_generates_valid_proofs() { let (store, salt_key) = setup_test_store(); - let (prover_queries, _, _) = create_sub_trie(&store, &[salt_key], None).unwrap(); + let (prover_queries, _, _) = create_sub_trie(&store, &[salt_key]).unwrap(); assert!(verify_ipa_proof(prover_queries)); } @@ -873,7 +592,7 @@ mod tests { let (store, salt_key) = setup_test_store(); // Single key - let (q1, _, _) = create_sub_trie(&store, &[salt_key], None).unwrap(); + let (q1, _, _) = create_sub_trie(&store, &[salt_key]).unwrap(); assert!(verify_ipa_proof(q1.clone())); // Multiple keys @@ -883,22 +602,21 @@ mod tests { salt_key, SaltKey::from((salt_key.bucket_id(), salt_key.slot_id() + 1)), ], - None, ) .unwrap(); assert!(verify_ipa_proof(q2)); // Duplicate keys - let (q3, _, _) = create_sub_trie(&store, &[salt_key, salt_key], None).unwrap(); + let (q3, _, _) = create_sub_trie(&store, &[salt_key, salt_key]).unwrap(); assert_eq!(q1.len(), q3.len()); // Metadata bucket - let (q4, _, b4) = create_sub_trie(&store, &[SaltKey::from((0u32, 0u64))], None).unwrap(); + let (q4, _, b4) = create_sub_trie(&store, &[SaltKey::from((0u32, 0u64))]).unwrap(); assert!(verify_ipa_proof(q4)); assert_eq!(b4[&0], 1); // Empty input - let res = create_sub_trie(&store, &[], None); + let res = create_sub_trie(&store, &[]); assert!(res.is_err()); } @@ -988,772 +706,18 @@ mod tests { // Invalid meta bucket key (slot_id >= META_BUCKET_SIZE) let invalid_meta_key = SaltKey::from((0u32, META_BUCKET_SIZE as u64)); - let result = create_sub_trie(&store, &[invalid_meta_key], None); + let result = create_sub_trie(&store, &[invalid_meta_key]); assert!(matches!(result, Err(ProofError::InvalidSaltKey { .. }))); // Invalid data bucket key (slot_id >= capacity) let data_bucket_id = NUM_META_BUCKETS as u32; let invalid_data_key = SaltKey::from((data_bucket_id, 1000u64)); - let result = create_sub_trie(&store, &[invalid_data_key], None); + let result = create_sub_trie(&store, &[invalid_data_key]); assert!(matches!(result, Err(ProofError::InvalidSaltKey { .. }))); // Valid keys should work let valid_meta_key = SaltKey::from((0u32, 0u64)); - let result = create_sub_trie(&store, &[valid_meta_key], None); + let result = create_sub_trie(&store, &[valid_meta_key]); assert!(result.is_ok()); } - - /// A slot value filled with `byte`. - fn value(byte: u8) -> Option { - Some(SaltValue::new(&[byte; 32], &[byte; 32])) - } - - /// Bucket metadata with `nonce` and `capacity`. - fn meta(nonce: u32, capacity: u64) -> Option { - Some(SaltValue::from(BucketMeta { - nonce, - capacity, - ..BucketMeta::default() - })) - } - - /// A store holding `bucket` with slots 3 and 5 set to `value(1)` and `value(2)`. - fn store_with_two_slots(bucket: BucketId) -> MemStore { - let store = MemStore::new(); - let key = |slot| SaltKey::from((bucket, slot)); - apply_block( - &store, - StateUpdates { - data: [(key(3), (None, value(1))), (key(5), (None, value(2)))] - .into_iter() - .collect(), - }, - ); - store - } - - /// Whether `node` is a leaf of its trie: a main-trie bucket root or a subtree leaf. - fn is_leaf_node(node: NodeId) -> bool { - let leaf_level = if node >> BUCKET_SLOT_BITS == 0 { - MAIN_TRIE_LEVELS - 1 - } else { - MAX_SUBTREE_LEVELS - 1 - }; - !is_encoded_node(node) && get_bfs_level(get_local_number(node)) == leaf_level - } - - /// The polynomial a witness rebuilds from storage for `node`; the root of an expanded - /// bucket is passed encoded, as `parents_and_points` keys it. - fn rebuilt_polynomial(store: &MemStore, node: NodeId) -> LagrangeBasis { - if is_leaf_node(node) { - leaf_polynomial(store, node).unwrap() - } else { - LagrangeBasis::new(multi_commitments_to_scalars(store, &[node]).unwrap()) - } - } - - /// The `(old, new)` pair `trie_updates` records for `node`. - fn transition(trie_updates: &TrieUpdates, node: NodeId) -> (CommitmentBytes, CommitmentBytes) { - trie_updates - .iter() - .find(|(id, _)| *id == node) - .unwrap_or_else(|| panic!("node {node} is not in the transition")) - .1 - } - - /// The nodes of a transition, and the parents of a plan. - fn node_set<'a>(nodes: impl IntoIterator) -> BTreeSet { - nodes.into_iter().copied().collect() - } - - /// The patched positions the plan holds for `node`. - fn planned_positions(plan: &FxHashMap, node: NodeId) -> BTreeSet { - plan[&node].positions.iter().map(|(i, _)| *i).collect() - } - - /// The cache entry of `node` under its post-block commitment. - /// - /// A main-trie bucket root or a subtree node belongs to this fixture's bucket alone, so - /// its entry must be there. The upper main-trie levels are shared by every test in the - /// process, and another fixture's witness may have replaced such an entry with its own - /// commitment in the meantime (a miss, never a wrong entry): those are checked when - /// present. - fn refreshed(node: NodeId, new: &CommitmentBytes) -> Option> { - let entry = node_poly_cache::get(node, new); - let bucket_local = node >> BUCKET_SLOT_BITS != 0 - || get_bfs_level(get_local_number(node)) == MAIN_TRIE_LEVELS - 1; - assert!( - entry.is_some() || !bucket_local, - "node {node} was not refreshed" - ); - entry - } - - /// Whether `node` is the main-trie root of a data bucket. Fixtures do not share one: random - /// keys spread over sixteen million buckets, and hand-picked buckets differ between tests. - /// Its cache entry is therefore the current test's own, while every other class of node - /// (upper main-trie levels, metadata leaves) is shared with the rest of the process and - /// checked only when present. - fn is_data_bucket_root(node: NodeId) -> bool { - node >> BUCKET_SLOT_BITS == 0 && node >= bucket_root_node_id(NUM_META_BUCKETS as BucketId) - } - - /// Every changed node is planned with its own transition, and every entry the refresh - /// inserts equals the polynomial a rebuild from the post-block store produces, for - /// main-trie leaves and internal nodes alike. - #[test] - fn refreshed_polynomials_equal_a_storage_rebuild() { - let TwoBlocks { - store, - updates_b, - trie_b, - .. - } = two_block_fixture(1); - let keys: Vec = updates_b.data.keys().copied().collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let plan = refresh_plan(&refresh); - assert_eq!( - node_set(plan.keys()), - node_set(trie_b.iter().map(|(node, _)| node)) - ); - for &(node, (old, new)) in &trie_b { - assert_eq!( - (plan[&node].old, plan[&node].new), - (old, new), - "node {node}" - ); - } - let (queries, _, _) = create_sub_trie(&store, &keys, Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - - store.update_state(updates_b); - store.update_trie(trie_b.clone()); - let (mut leaves, mut internal) = (0, 0); - for &(node, (_, new)) in &trie_b { - let Some(entry) = refreshed(node, &new) else { - continue; - }; - assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); - if is_leaf_node(node) { - leaves += 1; - } else { - internal += 1; - } - } - assert!( - leaves >= 1 && internal >= 1, - "{leaves} leaves, {internal} internal nodes" - ); - } - - /// In an expanded bucket the refresh keys the subtree top under the bucket root, every - /// other subtree node under its own id and every slot under its subtree leaf. - #[test] - fn refresh_covers_expanded_bucket_subtrees() { - let bucket = NUM_META_BUCKETS as BucketId + 3 * MIN_BUCKET_SIZE as BucketId + 7; - let capacity = 131072u64; - let key = |slot| SaltKey::from((bucket, slot)); - - // Block A: two slots, then an expansion to a three-level subtree (top at level 2). - let store = store_with_two_slots(bucket); - apply_block( - &store, - StateUpdates { - data: [ - ( - bucket_metadata_key(bucket), - (meta(0, MIN_BUCKET_SIZE as u64), meta(0, capacity)), - ), - (key(3), (value(1), None)), - (key(2049), (None, value(3))), - (key(capacity - 259), (None, value(4))), - (key(capacity - 1), (None, value(5))), - ] - .into_iter() - .collect(), - }, - ); - - // Block B: two segments under two different subtree children, capacity unchanged. - let updates_b = StateUpdates { - data: [ - (key(2049), (value(3), value(13))), - (key(2050), (None, value(14))), - (key(capacity - 259), (value(4), value(15))), - ] - .into_iter() - .collect(), - }; - let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); - let keys: Vec = updates_b.data.keys().copied().collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - - // The plan keys the top under the bucket root and patches exactly the changed - // children and slots at their positions. - let root = bucket_root_node_id(bucket); - let parent = get_parent_node(&root); - let leaf = |slot| subtree_leaf_for_key(&key(slot)); - let plan = refresh_plan(&refresh); - assert_eq!( - node_set(plan.keys()), - node_set(trie_b.iter().map(|(node, _)| node)) - ); - assert_eq!(planned_positions(&plan, root), [0, 1].into()); - assert_eq!( - planned_positions(&plan, get_parent_node(&leaf(2049))), - [8].into() - ); - assert_eq!( - planned_positions(&plan, get_parent_node(&leaf(capacity - 259))), - [254].into() - ); - assert_eq!(planned_positions(&plan, leaf(2049)), [1, 2].into()); - assert_eq!(planned_positions(&plan, leaf(capacity - 259)), [253].into()); - assert_eq!( - planned_positions(&plan, parent), - [vc_position_in_parent(&root)].into() - ); - - let (queries, _, _) = create_sub_trie(&store, &keys, Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - - store.update_state(updates_b); - store.update_trie(trie_b.clone()); - let (_, new_root) = transition(&trie_b, root); - assert_eq!( - *refreshed(root, &new_root).unwrap(), - rebuilt_polynomial(&store, encode_parent(root, 3)), - "subtree top under the bucket root" - ); - let (mut internal, mut leaves) = (0, 0); - for &(node, (_, new)) in &trie_b { - if node >> BUCKET_SLOT_BITS == 0 { - continue; - } - if is_leaf_node(node) { - leaves += 1; - } else { - internal += 1; - } - assert_eq!( - *refreshed(node, &new).unwrap(), - rebuilt_polynomial(&store, node), - "subtree node {node}" - ); - } - assert_eq!((internal, leaves), (2, 2)); - let (_, new_parent) = transition(&trie_b, parent); - if let Some(entry) = refreshed(parent, &new_parent) { - assert_eq!( - *entry, - rebuilt_polynomial(&store, parent), - "main-trie parent of the bucket root" - ); - } - } - - /// A bucket whose capacity changes is frozen for the block: nothing inside it is - /// refreshed, while its root's position in the main trie and the metadata leaf are; the - /// next witness over the bucket rebuilds from storage. - #[test] - fn refresh_skips_buckets_whose_capacity_changed() { - let bucket = NUM_META_BUCKETS as BucketId + 5 * MIN_BUCKET_SIZE as BucketId + 9; - let store = store_with_two_slots(bucket); - let key = |slot| SaltKey::from((bucket, slot)); - let meta_key = bucket_metadata_key(bucket); - let root = bucket_root_node_id(bucket); - let parent = get_parent_node(&root); - let meta_root = bucket_root_node_id(meta_key.bucket_id()); - - // Witnesses `witnessed` against the current store with the block's refresh, applies - // the block and checks what was and was not refreshed. - let run = |updates: StateUpdates, witnessed: &[SaltKey]| { - let (_, trie_updates) = StateRoot::new(&store).update_fin(&updates).unwrap(); - let refresh = NodePolyRefresh { - trie_updates: &trie_updates, - state_updates: &updates, - }; - let plan = refresh_plan(&refresh); - assert!( - plan.keys() - .all(|&node| node != root && node >> BUCKET_SLOT_BITS == 0), - "the frozen bucket was planned" - ); - assert_eq!( - planned_positions(&plan, parent), - [vc_position_in_parent(&root)].into() - ); - assert_eq!( - planned_positions(&plan, meta_root), - [slot_position(&meta_key)].into() - ); - - let (queries, _, _) = create_sub_trie(&store, witnessed, Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - store.update_state(updates); - store.update_trie(trie_updates.clone()); - - let (_, new_root) = transition(&trie_updates, root); - assert!( - node_poly_cache::get(root, &new_root).is_none(), - "the frozen bucket's root was refreshed" - ); - for &(node, (_, new)) in &trie_updates { - if node >> BUCKET_SLOT_BITS != 0 { - assert!( - node_poly_cache::get(node, &new).is_none(), - "subtree node {node} of the frozen bucket was refreshed" - ); - } - } - let (_, new_meta_root) = transition(&trie_updates, meta_root); - assert_eq!( - *refreshed(meta_root, &new_meta_root).unwrap(), - rebuilt_polynomial(&store, meta_root), - "metadata leaf" - ); - let (_, new_parent) = transition(&trie_updates, parent); - if let Some(entry) = refreshed(parent, &new_parent) { - assert_eq!( - *entry, - rebuilt_polynomial(&store, parent), - "main-trie parent of the bucket root" - ); - } - }; - - // Block B doubles the capacity, deleting one slot and inserting one beyond the old - // capacity; the witness covers only slots below the old capacity. - run( - StateUpdates { - data: [ - ( - meta_key, - ( - meta(0, MIN_BUCKET_SIZE as u64), - meta(0, 2 * MIN_BUCKET_SIZE as u64), - ), - ), - (key(3), (value(1), None)), - (key(300), (None, value(3))), - ] - .into_iter() - .collect(), - }, - &[meta_key, key(3), key(5)], - ); - let (queries, _, _) = create_sub_trie(&store, &[key(5), key(300)], None).unwrap(); - assert!(verify_ipa_proof(queries)); - - // Block C contracts it back, removing the slot beyond the new capacity. - run( - StateUpdates { - data: [ - ( - meta_key, - ( - meta(0, 2 * MIN_BUCKET_SIZE as u64), - meta(0, MIN_BUCKET_SIZE as u64), - ), - ), - (key(300), (value(3), None)), - ] - .into_iter() - .collect(), - }, - &[meta_key, key(5)], - ); - let (queries, _, _) = create_sub_trie(&store, &[key(5)], None).unwrap(); - assert!(verify_ipa_proof(queries)); - } - - /// A retried witness re-applies the block's refresh: its lookups miss on the re-keyed - /// entries, rebuild from the pre-block store and refresh again to the same entries, and - /// the proof bytes do not change. - #[test] - fn refresh_is_idempotent_across_a_retried_witness() { - let TwoBlocks { - store, - updates_b, - trie_b, - .. - } = two_block_fixture(2); - let keys: Vec = updates_b.data.keys().copied().collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let build = || { - SaltProof::create_with_refresh(keys.iter().copied(), &store, Some(&refresh)).unwrap() - }; - let encode = |proof: &SaltProof| { - bincode::serde::encode_to_vec(proof, bincode::config::legacy()).unwrap() - }; - - let first = encode(&build()); - let entries: Vec>> = trie_b - .iter() - .map(|&(node, (_, new))| refreshed(node, &new)) - .collect(); - let second = encode(&build()); - assert_eq!( - first, second, - "a re-applied refresh must not change the proof" - ); - let mut compared = 0; - for (&(node, (_, new)), before) in trie_b.iter().zip(entries) { - if let (Some(after), Some(before)) = (refreshed(node, &new), before) { - assert_eq!(*after, *before, "node {node}"); - compared += 1; - } - } - assert!(compared >= 1); - } - - /// The debug self-check rejects a transition whose new commitment is not what the patched - /// positions commit to. - #[cfg(debug_assertions)] - #[test] - #[should_panic(expected = "does not commit")] - fn refresh_debug_check_rejects_an_inconsistent_transition() { - let TwoBlocks { - store, - updates_b, - mut trie_b, - .. - } = two_block_fixture(3); - let foreign = trie_b - .iter() - .find(|(node, _)| *node != ROOT_NODE_ID) - .unwrap() - .1 - .1; - trie_b - .iter_mut() - .find(|(node, _)| *node == ROOT_NODE_ID) - .unwrap() - .1 - .1 = foreign; - let keys: Vec = updates_b.data.keys().copied().collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let _ = create_sub_trie(&store, &keys, Some(&refresh)); - } - - /// The debug self-check rejects a slot whose new value is not what its leaf's new - /// commitment was computed from. - #[cfg(debug_assertions)] - #[test] - #[should_panic(expected = "does not commit")] - fn refresh_debug_check_rejects_a_wrong_slot_value() { - let TwoBlocks { - store, - mut updates_b, - trie_b, - .. - } = two_block_fixture(4); - let key = *updates_b - .data - .iter() - .find(|(key, (_, new))| !key.is_in_meta_bucket() && new.is_some()) - .unwrap() - .0; - updates_b.data.get_mut(&key).unwrap().1 = Some(SaltValue::new(&[9; 20], &[9; 40])); - let keys: Vec = updates_b.data.keys().copied().collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let _ = create_sub_trie(&store, &keys, Some(&refresh)); - } - - /// A planned parent the witness did not read takes its base from the cache's entry for its - /// old commitment, and is skipped while the cache holds none: after a refresh whose witness - /// covered one bucket, the other changed buckets' roots are absent under their new - /// commitment, and present, equal to the post-block rebuild, once a witness over the - /// pre-state has cached their bases. - #[test] - fn refresh_uses_cached_bases_for_parents_the_witness_did_not_read() { - let TwoBlocks { - store, - updates_b, - trie_b, - .. - } = two_block_fixture(5); - let keys: Vec = updates_b.data.keys().copied().collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let bucket = keys - .iter() - .find(|key| !key.is_in_meta_bucket()) - .unwrap() - .bucket_id(); - let witnessed: Vec = keys - .iter() - .copied() - .filter(|key| key.bucket_id() == bucket) - .collect(); - let unread_roots: Vec<(NodeId, CommitmentBytes)> = trie_b - .iter() - .filter(|&&(node, _)| is_data_bucket_root(node) && node != bucket_root_node_id(bucket)) - .map(|&(node, (_, new))| (node, new)) - .collect(); - assert!( - unread_roots.len() >= 8, - "{} unread roots", - unread_roots.len() - ); - - // Nothing holds those roots' pre-state polynomials yet, so they are skipped. - let (queries, _, _) = create_sub_trie(&store, &witnessed, Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - for &(node, new) in &unread_roots { - assert!( - node_poly_cache::get(node, &new).is_none(), - "node {node} was refreshed without a base" - ); - } - - // A witness over the pre-state caches every parent under its old commitment. - create_sub_trie(&store, &keys, None).unwrap(); - let (queries, _, _) = create_sub_trie(&store, &witnessed, Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - - store.update_state(updates_b); - store.update_trie(trie_b.clone()); - for &(node, new) in &unread_roots { - let entry = node_poly_cache::get(node, &new) - .unwrap_or_else(|| panic!("node {node} was not refreshed from its cached base")); - assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); - } - for &(node, (_, new)) in &trie_b { - if let Some(entry) = node_poly_cache::get(node, &new) { - assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); - } - } - } - - /// Every planned parent whose base is in hand at its old commitment is applied: the - /// refresh inserts one entry per planned parent, skipping none, and each entry equals the - /// polynomial a rebuild from the post-block store produces. - #[test] - fn refresh_applies_every_planned_parent() { - let TwoBlocks { - store, - updates_b, - trie_b, - .. - } = two_block_fixture(9); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let plan = refresh_plan(&refresh); - let planned = plan.len(); - let in_hand = plan - .keys() - .map(|&node| { - let base = Arc::new(rebuilt_polynomial(&store, node)); - (node, (store.commitment(node).unwrap(), base)) - }) - .collect(); - assert_eq!(apply_refresh(plan, &in_hand), planned); - - store.update_state(updates_b); - store.update_trie(trie_b.clone()); - for &(node, (_, new)) in &trie_b { - let entry = node_poly_cache::get(node, &new); - assert!( - entry.is_some() || !is_data_bucket_root(node), - "node {node} was not refreshed" - ); - if let Some(entry) = entry { - assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); - } - } - } - - /// The witness must read the transition's pre-state: over the post-state store every - /// parent is read at its new commitment, which the debug guard rejects before any entry - /// is derived from it. - #[cfg(debug_assertions)] - #[test] - #[should_panic(expected = "other than the refresh's pre-state")] - fn refresh_rejects_a_witness_over_the_post_state() { - let TwoBlocks { - store, - updates_b, - trie_b, - .. - } = two_block_fixture(6); - let keys: Vec = updates_b.data.keys().copied().collect(); - store.update_state(updates_b.clone()); - store.update_trie(trie_b.clone()); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let _ = create_sub_trie(&store, &keys, Some(&refresh)); - } - - /// A parent the witness read at a commitment other than the refresh's old one is skipped, - /// never patched: a witness over the pre-B view carrying block C's transition (whose old - /// commitments are B's) leaves the bucket root absent under C's commitment, and inserts - /// nothing that differs from the post-C rebuild. Debug builds reject such a witness - /// outright instead (`refresh_rejects_a_witness_over_the_post_state`). - #[cfg(not(debug_assertions))] - #[test] - fn refresh_skips_a_parent_read_at_a_foreign_commitment() { - let bucket = NUM_META_BUCKETS as BucketId + 7 * MIN_BUCKET_SIZE as BucketId + 11; - let key = |slot| SaltKey::from((bucket, slot)); - - // Blocks A and B change slot 3; block C, computed over B, changes slot 5. - let store = store_with_two_slots(bucket); - let pre_b = store.clone(); - apply_block( - &store, - StateUpdates { - data: [(key(3), (value(1), value(3)))].into_iter().collect(), - }, - ); - let updates_c = StateUpdates { - data: [(key(5), (value(2), value(4)))].into_iter().collect(), - }; - let (_, trie_c) = StateRoot::new(&store).update_fin(&updates_c).unwrap(); - let refresh = NodePolyRefresh { - trie_updates: &trie_c, - state_updates: &updates_c, - }; - - let (queries, _, _) = create_sub_trie(&pre_b, &[key(3), key(5)], Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - - store.update_state(updates_c); - store.update_trie(trie_c.clone()); - let root = bucket_root_node_id(bucket); - let (_, new_root) = transition(&trie_c, root); - assert!( - node_poly_cache::get(root, &new_root).is_none(), - "a parent read at a foreign commitment was patched" - ); - for &(node, (_, new)) in &trie_c { - if let Some(entry) = node_poly_cache::get(node, &new) { - assert_eq!(*entry, rebuilt_polynomial(&store, node), "node {node}"); - } - } - } - - /// A metadata change that keeps the capacity (a rehash under a new nonce) does not freeze - /// the bucket: over a two-level, 512-slot subtree the moved slots are patched in their - /// leaves, the changed leaves in the top under the bucket root, and the metadata leaf at - /// the bucket's slot. - #[test] - fn refresh_patches_a_rehashed_bucket_with_unchanged_capacity() { - let store = MemStore::new(); - let bucket = NUM_META_BUCKETS as BucketId + 9 * MIN_BUCKET_SIZE as BucketId + 13; - let capacity = 2 * MIN_BUCKET_SIZE as u64; - let key = |slot| SaltKey::from((bucket, slot)); - let meta_key = bucket_metadata_key(bucket); - let root = bucket_root_node_id(bucket); - let meta_root = bucket_root_node_id(meta_key.bucket_id()); - - // Block A: a 512-slot bucket holding two values in its first leaf and one in its - // second. - apply_block( - &store, - StateUpdates { - data: [ - ( - meta_key, - (Some(BucketMeta::default().into()), meta(0, capacity)), - ), - (key(3), (None, value(1))), - (key(5), (None, value(2))), - (key(300), (None, value(3))), - ] - .into_iter() - .collect(), - }, - ); - - // Block B: a rehash under a new nonce moves the values, capacity unchanged. - let updates_b = EphemeralSaltState::new(&store) - .set_nonce(bucket, 1) - .unwrap(); - assert_eq!( - updates_b.data[&meta_key], - (meta(0, capacity), meta(1, capacity)) - ); - let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - - // Every changed node is planned; the leaves at their moved slots, the top at the - // changed leaves, the metadata leaf at the bucket's slot. - let mut moved: BTreeMap> = BTreeMap::new(); - for key in updates_b.data.keys().filter(|key| !key.is_in_meta_bucket()) { - moved - .entry(subtree_leaf_for_key(key)) - .or_default() - .insert(slot_position(key)); - } - assert_eq!(moved.len(), 2, "both leaves change"); - let plan = refresh_plan(&refresh); - assert_eq!( - node_set(plan.keys()), - node_set(trie_b.iter().map(|(node, _)| node)) - ); - for (leaf, slots) in &moved { - assert_eq!(planned_positions(&plan, *leaf), *slots, "leaf {leaf}"); - } - assert_eq!( - planned_positions(&plan, root), - moved.keys().map(vc_position_in_parent).collect() - ); - assert_eq!( - planned_positions(&plan, meta_root), - [slot_position(&meta_key)].into() - ); - - let keys: Vec = updates_b.data.keys().copied().collect(); - let (queries, _, _) = create_sub_trie(&store, &keys, Some(&refresh)).unwrap(); - assert!(verify_ipa_proof(queries)); - - store.update_state(updates_b); - store.update_trie(trie_b.clone()); - let (_, new_root) = transition(&trie_b, root); - assert_eq!( - *refreshed(root, &new_root).unwrap(), - rebuilt_polynomial(&store, encode_parent(root, 2)), - "subtree top under the bucket root" - ); - for leaf in moved.keys() { - let (_, new) = transition(&trie_b, *leaf); - assert_eq!( - *refreshed(*leaf, &new).unwrap(), - rebuilt_polynomial(&store, *leaf), - "leaf {leaf}" - ); - } - let (_, new_meta_root) = transition(&trie_b, meta_root); - if let Some(entry) = node_poly_cache::get(meta_root, &new_meta_root) { - assert_eq!( - *entry, - rebuilt_polynomial(&store, meta_root), - "metadata leaf" - ); - } - } } diff --git a/salt/src/proof/test_utils.rs b/salt/src/proof/test_utils.rs index 826ac08c..5be045d3 100644 --- a/salt/src/proof/test_utils.rs +++ b/salt/src/proof/test_utils.rs @@ -4,12 +4,11 @@ use crate::mem_store::MemStore; use crate::proof::SerdeCommitment; -use crate::state::{state::EphemeralSaltState, updates::StateUpdates}; +use crate::state::updates::StateUpdates; use crate::trie::trie::{StateRoot, TrieUpdates}; use crate::types::SaltValue; use banderwagon::{Element, Fr}; -use hashbrown::HashMap; -use rand::{rngs::StdRng, Rng, SeedableRng}; +use rand::{rngs::StdRng, Rng}; use std::vec::Vec; /// Generates random test data of specified length. @@ -50,45 +49,3 @@ pub(crate) fn apply_block(store: &MemStore, updates: StateUpdates) -> TrieUpdate store.update_trie(trie_updates.clone()); trie_updates } - -/// A store holding block A (64 random plain kvs), and block B's transition over it (16 value -/// updates, 8 deletes and 16 inserts) computed but not applied. -pub(crate) struct TwoBlocks { - pub store: MemStore, - /// Block B's plain writes. - pub kvs_b: HashMap, Option>>, - pub updates_b: StateUpdates, - pub trie_b: TrieUpdates, -} - -/// Builds [`TwoBlocks`] from `seed`. -pub(crate) fn two_block_fixture(seed: u64) -> TwoBlocks { - let mut rng = StdRng::seed_from_u64(seed); - let kvs_a: HashMap, Option>> = (0..64) - .map(|_| (mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40)))) - .collect(); - let store = MemStore::new(); - let updates_a = EphemeralSaltState::new(&store).update_fin(&kvs_a).unwrap(); - apply_block(&store, updates_a); - - let mut keys_a: Vec> = kvs_a.into_keys().collect(); - keys_a.sort(); - let mut kvs_b: HashMap, Option>> = HashMap::new(); - for key in &keys_a[..16] { - kvs_b.insert(key.clone(), Some(mock_data(&mut rng, 40))); - } - for key in &keys_a[16..24] { - kvs_b.insert(key.clone(), None); - } - for _ in 0..16 { - kvs_b.insert(mock_data(&mut rng, 20), Some(mock_data(&mut rng, 40))); - } - let updates_b = EphemeralSaltState::new(&store).update_fin(&kvs_b).unwrap(); - let (_, trie_b) = StateRoot::new(&store).update_fin(&updates_b).unwrap(); - TwoBlocks { - store, - kvs_b, - updates_b, - trie_b, - } -} diff --git a/salt/src/proof/witness.rs b/salt/src/proof/witness.rs index 48347f89..bfccf51a 100644 --- a/salt/src/proof/witness.rs +++ b/salt/src/proof/witness.rs @@ -8,7 +8,6 @@ use crate::types::{bucket_id_from_metadata_key, METADATA_KEYS_RANGE}; use crate::{ proof::salt_witness::SaltWitness, - proof::subtrie::NodePolyRefresh, proof::ProofError, state::{hasher, state::EphemeralSaltState}, traits::{StateReader, TrieReader}, @@ -110,29 +109,6 @@ impl Witness { updates: &BTreeMap, Option>>, store: &Store, ) -> Result - where - Store: StateReader + TrieReader, - { - Self::create_with_refresh(bucket_ids, lookups, updates, store, None) - } - - /// [`Self::create`], additionally advancing the process-wide node-polynomial cache to the - /// witnessed block's post-state. - /// - /// The witness proves the parent state, and `refresh` is the block's own transition (its - /// `TrieUpdates` and `StateUpdates`, as `StateRoot::update_fin` produced them from that - /// parent state). After this witness's own lookups, the cache entries of every node the - /// block changed are advanced to the post-state, so the next block's witness hits instead - /// of rebuilding them from storage. Entries stay validated by commitment, so a stale or - /// foreign entry is only ever a miss; and the witness returned here is the same with or - /// without a refresh. - pub fn create_with_refresh<'b, Store>( - bucket_ids: impl IntoIterator, - lookups: impl IntoIterator>, - updates: &BTreeMap, Option>>, - store: &Store, - refresh: Option<&NodePolyRefresh<'_>>, - ) -> Result where Store: StateReader + TrieReader, { @@ -219,7 +195,7 @@ impl Witness { reason: format!("{e:?}"), })?; - let salt_witness = SaltWitness::create_with_refresh(&witnessed_keys, store, refresh)?; + let salt_witness = SaltWitness::create(&witnessed_keys, store)?; Ok(Witness { direct_lookup_tbl, @@ -1226,37 +1202,4 @@ mod tests { assert_eq!(first, second, "a cache hit must not change the proof"); build().verify().unwrap(); } - - /// A witness built with its block's refresh verifies, and the next block's witness over - /// the refreshed entries serializes identically when built twice and still verifies: a - /// wrong refreshed entry on those paths would fail its verification. - #[test] - fn witness_over_refreshed_polynomials_verifies_and_is_stable() { - let TwoBlocks { - store, - kvs_b, - updates_b, - trie_b, - } = two_block_fixture(11); - let plain_b: BTreeMap<_, _> = kvs_b - .iter() - .map(|(key, value)| (key.clone(), value.clone())) - .collect(); - let refresh = NodePolyRefresh { - trie_updates: &trie_b, - state_updates: &updates_b, - }; - let witness_b = - Witness::create_with_refresh([], core::iter::empty(), &plain_b, &store, Some(&refresh)) - .unwrap(); - witness_b.verify().unwrap(); - - store.update_state(updates_b); - store.update_trie(trie_b); - let build = || Witness::create([], kvs_b.keys(), &BTreeMap::new(), &store).unwrap(); - let first = encode_salt_witness(&build()); - let second = encode_salt_witness(&build()); - assert_eq!(first, second, "a refreshed entry must not change the proof"); - build().verify().unwrap(); - } } From 1f97191011bdece6f39064db120f97322a5e817e Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Tue, 22 Sep 2026 10:47:43 +0800 Subject: [PATCH 12/18] refactor: fold the cleanups a four-angle review of this branch found Reuse, simplification, efficiency and altitude passes over the diff against main; outputs are unchanged and the proof byte-identity tests pin that. - `thread_chunk_size!` (salt-macros): the `div_ceil(num_threads).max(1)` idiom and its zero-chunk guard live once, not at four sites with four comments; every chunking site uses it. - `CRS::committer` builds the `G ++ [Q]` tables the provers require, and `create_with_precomp` asserts that shape: its `q` parameter, the table-shape sniffing, the debug assert and the variable-base blinding fallback no caller reached are gone. The trie's shared committer is built through it. - `Committer::msm` (banderwagon) replaces `ipa::fixed_base_msm`, which multiproof reached across modules for a committer operation. - `create_sub_trie`: one `ParentNode` (logical id, physical id, bytes, decoded commitment) per parent, read in parallel in node order; this drops the second `parent_bytes` map, the lookup closure with an unreachable error arm, the repeated `connect_parent_id` calls and the hash lookups per node, and `resolve_polys` takes the slice. `internal_polynomials` returns one polynomial per node instead of a flat buffer the caller copied out of. Key validation groups the sorted keys with `chunk_by`. - `open_inner` derives `a`/`b` once for both IPA paths; `open_point_outside_of_domain` is inlined into its only (test) caller. - Deleted or narrowed: `multi_scalar_mul_par` (deprecated, no callers), `LagrangeBasis::values` and `DEFAULT_CRS` back to `pub(crate)`, `shared_committer` returns `&'static`, `apply_block` returns nothing, `msm_windowed` stops truncating to the shorter slice (its caller asserts equal lengths), `slot_position` is shared with `shape.rs`, the arkworks agreement tests share one input builder. Bench (M4 Pro, warm cache) against the tree before this commit: `salt_proof/create/{16,256,2048}` and `witness/create/{256,1024}` within noise. An intermediate version read the parents with an unbounded `par_iter`, which cost 2% on `witness/create/256`; the read is bounded to one chunk per thread again via `with_min_len`. Co-Authored-By: Claude Fable 5.1 --- banderwagon/src/element.rs | 12 +- banderwagon/src/msm.rs | 71 +++---- banderwagon/src/salt_committer.rs | 19 +- ipa-multipoint/src/crs.rs | 15 +- ipa-multipoint/src/ipa.rs | 84 ++------ ipa-multipoint/src/lagrange_basis.rs | 2 +- ipa-multipoint/src/math_utils.rs | 4 +- ipa-multipoint/src/multiproof.rs | 54 ++--- salt-macros/src/lib.rs | 9 + salt/benches/proof.rs | 12 +- salt/src/proof/prover.rs | 7 +- salt/src/proof/shape.rs | 15 +- salt/src/proof/subtrie.rs | 281 +++++++++++---------------- salt/src/proof/test_utils.rs | 9 +- salt/src/proof/witness.rs | 8 +- salt/src/trie/trie.rs | 23 +-- 16 files changed, 248 insertions(+), 377 deletions(-) diff --git a/banderwagon/src/element.rs b/banderwagon/src/element.rs index 21baa12c..7263498e 100644 --- a/banderwagon/src/element.rs +++ b/banderwagon/src/element.rs @@ -413,6 +413,12 @@ fn subgroup_check(point: &EdwardsProjective) -> bool { } pub fn multi_scalar_mul(bases: &[Element], scalars: &[Fr]) -> Element { + assert_eq!( + bases.len(), + scalars.len(), + "number of bases should equal number of scalars" + ); + // The MSM needs affine bases. Points loaded from storage or deserialized // proofs already have `z = 1`, so skip the batch inversion entirely in // that common case. @@ -426,12 +432,6 @@ pub fn multi_scalar_mul(bases: &[Element], scalars: &[Fr]) -> Element { EdwardsProjective::batch_convert_to_mul_base(&bases_inner) }; - assert_eq!( - bases.len(), - scalars.len(), - "number of bases should equal number of scalars" - ); - Element(crate::msm::msm_windowed(&bases, scalars)) } diff --git a/banderwagon/src/msm.rs b/banderwagon/src/msm.rs index 196d4273..ecaac227 100644 --- a/banderwagon/src/msm.rs +++ b/banderwagon/src/msm.rs @@ -17,7 +17,7 @@ use std::{vec, vec::Vec}; /// the entire digit pass. Windows are processed in parallel only when each /// carries enough work to pay for the dispatch. pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsProjective { - let size = bases.len().min(scalars.len()); + let size = bases.len(); let c = if size < 32 { 3 } else { @@ -31,9 +31,8 @@ pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsPr /// width — and with it every bucket-count branch of [`bucket_len`] — at a size /// that keeps a debug-mode run cheap. fn msm_windowed_with_c(bases: &[EdwardsAffine], scalars: &[Fr], c: usize) -> EdwardsProjective { - let size = bases.len().min(scalars.len()); - let bases = &bases[..size]; - let scalars = &scalars[..size]; + debug_assert_eq!(bases.len(), scalars.len()); + let size = bases.len(); if size == 0 { return EdwardsProjective::zero(); } @@ -74,7 +73,7 @@ fn msm_windowed_with_c(bases: &[EdwardsAffine], scalars: &[Fr], c: usize) -> Edw // Below this size the per-window work is too small to farm out. const MIN_PARALLEL_SIZE: usize = 64; let window_sums: Vec = if size >= MIN_PARALLEL_SIZE { - into_iter!((0..digits_count)).map(window_sum).collect() + into_iter!(0..digits_count).map(window_sum).collect() } else { (0..digits_count).map(window_sum).collect() }; @@ -212,30 +211,39 @@ mod tests { use crate::{multi_scalar_mul, Element}; use std::vec; + /// `size` random affine points and scalars, with the edge cases `0`, `r - 1` (which + /// maximizes the last window's digit, the one the last bucket count has to cover) and `1` + /// patched into the first scalars. + fn msm_input(seed: u8, size: usize) -> (Vec, Vec) { + use ark_ec::CurveGroup; + use ark_ff::UniformRand; + use rand_chacha::rand_core::SeedableRng; + use rand_chacha::ChaCha20Rng; + + let mut rng = ChaCha20Rng::from_seed([seed; 32]); + let bases_proj: Vec = (0..size) + .map(|_| EdwardsProjective::rand(&mut rng)) + .collect(); + let bases = EdwardsProjective::normalize_batch(&bases_proj); + let mut scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); + for (scalar, edge) in scalars + .iter_mut() + .zip([Fr::zero(), -Fr::from(1u64), Fr::from(1u64)]) + { + *scalar = edge; + } + (bases, scalars) + } + /// The hand-rolled windowed MSM must agree with arkworks' generic MSM for /// every size class (serial windows, parallel windows, small-c) and for /// edge-case scalars. #[test] fn msm_windowed_matches_arkworks() { - use ark_ec::{CurveGroup, VariableBaseMSM}; - use ark_ff::UniformRand; - use rand_chacha::rand_core::SeedableRng; - use rand_chacha::ChaCha20Rng; + use ark_ec::VariableBaseMSM; - let mut rng = ChaCha20Rng::from_seed([9u8; 32]); for size in [1usize, 2, 16, 31, 32, 33, 129, 256, 1000] { - let bases_proj: Vec = (0..size) - .map(|_| EdwardsProjective::rand(&mut rng)) - .collect(); - let bases = EdwardsProjective::normalize_batch(&bases_proj); - let mut scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); - // Exercise edge-case scalars. - scalars[0] = Fr::zero(); - if size > 2 { - scalars[1] = -Fr::from(1u64); - scalars[2] = Fr::from(1u64); - } - + let (bases, scalars) = msm_input(9, size); let expected = EdwardsProjective::msm(&bases, &scalars).unwrap(); let got = msm_windowed(&bases, &scalars); assert_eq!(got, expected, "size {size}"); @@ -251,24 +259,9 @@ mod tests { /// 16,384 points — is the one where that is the full `2^c`. #[test] fn msm_windowed_matches_arkworks_for_every_window_width() { - use ark_ec::{CurveGroup, VariableBaseMSM}; - use ark_ff::UniformRand; - use rand_chacha::rand_core::SeedableRng; - use rand_chacha::ChaCha20Rng; - - let mut rng = ChaCha20Rng::from_seed([21u8; 32]); - let size = 200; - let bases_proj: Vec = (0..size) - .map(|_| EdwardsProjective::rand(&mut rng)) - .collect(); - let bases = EdwardsProjective::normalize_batch(&bases_proj); - let mut scalars: Vec = (0..size).map(|_| Fr::rand(&mut rng)).collect(); - scalars[0] = Fr::zero(); - // `r - 1` maximizes the last window's digit, which is the one the last - // bucket count has to cover. - scalars[1] = -Fr::from(1u64); - scalars[2] = Fr::from(1u64); + use ark_ec::VariableBaseMSM; + let (bases, scalars) = msm_input(21, 200); let expected = EdwardsProjective::msm(&bases, &scalars).unwrap(); for c in 3..=13 { assert_eq!( diff --git a/banderwagon/src/salt_committer.rs b/banderwagon/src/salt_committer.rs index bafa1030..dc89e61a 100644 --- a/banderwagon/src/salt_committer.rs +++ b/banderwagon/src/salt_committer.rs @@ -31,8 +31,8 @@ use ark_ed_on_bls12_381_bandersnatch::{EdwardsAffine, EdwardsProjective, Fq, Fr} use ark_ff::PrimeField; use ark_ff::Zero; -use salt_macros::iter; use salt_macros::prelude::*; +use salt_macros::{chunks, iter, thread_chunk_size}; use std::{vec, vec::Vec}; /// Precomputed Multi-Scalar Multiplication engine for fixed base points. @@ -350,6 +350,23 @@ impl Committer { /// * `result` - The projective point to update (in-place) /// * `p2_x` - X-coordinate of the affine point to add /// * `p2_y` - Y-coordinate of the affine point to add +impl Committer { + /// Sums `scalar · G[index]` over `terms`, splitting them across threads. + pub fn msm(&self, terms: &[(usize, Fr)]) -> Element { + chunks!(terms, thread_chunk_size!(terms.len())) + .map(|chunk| { + let mut acc = Element::zero(); + for (index, scalar) in chunk { + if !scalar.is_zero() { + acc += self.mul_index(scalar, *index); + } + } + acc + }) + .sum() + } +} + #[cfg(not(target_arch = "x86_64"))] fn add_affine_point(result: &mut EdwardsProjective, p2_x: &Fq, p2_y: &Fq) { use ark_ff::biginteger::BigInt; diff --git a/ipa-multipoint/src/crs.rs b/ipa-multipoint/src/crs.rs index 6971a663..ee491a57 100644 --- a/ipa-multipoint/src/crs.rs +++ b/ipa-multipoint/src/crs.rs @@ -10,7 +10,7 @@ //! approach, ensuring reproducibility and verifiability of the setup. use crate::{default_crs, ipa::slow_vartime_multiscalar_mul, lagrange_basis::LagrangeBasis}; -use banderwagon::{try_reduce_to_element, Element, SerializationError}; +use banderwagon::{salt_committer::Committer, try_reduce_to_element, Element, SerializationError}; use std::{string::String, vec::Vec}; use thiserror::Error; @@ -177,6 +177,19 @@ impl CRS { /// /// # Returns /// The elliptic curve point representing the polynomial commitment + /// Fixed-base tables over this CRS: `G` in order, then `Q` as base `n`. This is the shape + /// [`crate::multiproof::MultiPoint::open_with_committer`] and [`crate::ipa::create_with_precomp`] + /// take, so every MSM they compute — the IPA's blinding terms included — is fixed-base. + pub fn committer(&self, window_size: usize) -> Committer { + let bases: Vec = self + .G + .iter() + .copied() + .chain(core::iter::once(self.Q)) + .collect(); + Committer::new(&bases, window_size) + } + pub fn commit_lagrange_poly(&self, polynomial: &LagrangeBasis) -> Element { slow_vartime_multiscalar_mul(polynomial.values().iter(), self.G.iter()) } diff --git a/ipa-multipoint/src/ipa.rs b/ipa-multipoint/src/ipa.rs index 5bd2a1c6..ef655db7 100644 --- a/ipa-multipoint/src/ipa.rs +++ b/ipa-multipoint/src/ipa.rs @@ -9,7 +9,7 @@ use core::iter; use itertools::Itertools; use salt_macros::prelude::*; -use salt_macros::{chunks, chunks_mut, join, num_threads}; +use salt_macros::{chunks, chunks_mut, join, thread_chunk_size}; use std::{vec, vec::Vec}; #[derive(Debug, Clone, PartialEq)] @@ -152,7 +152,7 @@ pub fn create( let x = transcript.challenge_scalar(b"x"); let x_inv = x.inverse().unwrap(); - let chunk_size = G_L.len().div_ceil(num_threads!()); + let chunk_size = thread_chunk_size!(G_L.len()); chunks_mut!(G_L, chunk_size) .zip(chunks!(G_R, chunk_size)) .for_each(|(g_l_chunk, g_r_chunk)| { @@ -200,14 +200,11 @@ pub fn create( /// /// # Arguments /// -/// * `precomp` - Fixed-base tables for the CRS `G` vector (must cover at least -/// `a_vec.len()` bases, in CRS order); when they cover one more base, it must -/// be `Q`, and the blinding terms become fixed-base multiplications too -/// * `q` - The CRS blinding generator `Q` +/// * `precomp` - The CRS's fixed-base tables ([`CRS::committer`](crate::crs::CRS::committer)): +/// `G` in order, then `Q` as base `a_vec.len()`, so the blinding terms are fixed-base too pub fn create_with_precomp( transcript: &mut Transcript, precomp: &Committer, - q: Element, mut a_vec: Vec, a_comm: Element, mut b_vec: Vec, @@ -222,7 +219,11 @@ pub fn create_with_precomp( let n = a.len(); assert_eq!(b.len(), n); assert!(n.is_power_of_two()); - assert!(precomp.num_bases() >= n); + assert_eq!( + precomp.num_bases(), + n + 1, + "the committer must hold the CRS `G` vector followed by `Q`" + ); let output_point = inner_product(a, b); transcript.append_point(b"C", &a_comm); @@ -230,21 +231,8 @@ pub fn create_with_precomp( transcript.append_scalar(b"output point", &output_point); let w = transcript.challenge_scalar(b"w"); - // Tables that cover one base past the vector carry `Q` there (the salt trie's shared - // committer does), which turns the blinding terms `z·(w·Q) = (w·z)·Q` into fixed-base - // multiplications as well. Otherwise `Q` takes one variable-base product per term. - let q_in_tables = precomp.num_bases() > n; - debug_assert!( - !q_in_tables || precomp.mul_index(&Fr::one(), n) == q, - "committer base {n} is not the blinding generator Q" - ); - let blind = |z: Fr| { - if q_in_tables { - precomp.mul_index(&(w * z), n) - } else { - q * (w * z) - } - }; + // `Q` is base `n` of the tables, so the blinding term `z·(w·Q) = (w·z)·Q` is fixed-base too. + let blind = |z: Fr| precomp.mul_index(&(w * z), n); let num_rounds = log2(n); @@ -259,7 +247,7 @@ pub fn create_with_precomp( let mut l_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); let mut r_terms: Vec<(usize, Fr)> = Vec::with_capacity(n / 2); - for _k in 0..num_rounds { + for _ in 0..num_rounds { let half = len / 2; let (a_L, a_R) = halve(a); let (b_L, b_R) = halve(b); @@ -279,11 +267,11 @@ pub fn create_with_precomp( let left_compute = || -> Element { let z_L = inner_product(a_R, b_L); - fixed_base_msm(precomp, &l_terms) + blind(z_L) + precomp.msm(&l_terms) + blind(z_L) }; let right_compute = || -> Element { let z_R = inner_product(a_L, b_R); - fixed_base_msm(precomp, &r_terms) + blind(z_R) + precomp.msm(&r_terms) + blind(z_R) }; let (L, R) = join!(left_compute, right_compute); @@ -320,24 +308,6 @@ pub fn create_with_precomp( } } -/// Sums `scalar · G[index]` over the given terms using precomputed wNAF -/// tables, splitting the terms across threads. -pub(crate) fn fixed_base_msm(precomp: &Committer, terms: &[(usize, Fr)]) -> Element { - // `max(1)`: an empty term list must not reach `chunks(0)`, which panics. - let chunk_size = terms.len().div_ceil(num_threads!()).max(1); - chunks!(terms, chunk_size) - .map(|chunk| { - let mut acc = Element::zero(); - for (index, scalar) in chunk { - if !scalar.is_zero() { - acc += precomp.mul_index(scalar, *index); - } - } - acc - }) - .sum() -} - // Halves the slice that is passed in // Assumes that the slice has an even length fn halve(scalars: &mut [T]) -> (&mut [T], &mut [T]) { @@ -438,14 +408,6 @@ pub fn slow_vartime_multiscalar_mul<'a>( multi_scalar_mul(&points, &scalars) } -#[deprecated(note = "no longer chunks its input; call `banderwagon::multi_scalar_mul`")] -pub fn multi_scalar_mul_par(bases: &[Element], scalars: &[Fr]) -> Element { - // `multi_scalar_mul` parallelizes internally across Pippenger windows; - // chunking the input here would multiply the window/doubling work per - // chunk and nest thread-pool dispatches. - multi_scalar_mul(bases, scalars) -} - fn generate_challenges(proof: &IPAProof, transcript: &mut Transcript) -> Vec { let mut challenges: Vec = Vec::with_capacity(proof.L_vec.len()); @@ -474,7 +436,7 @@ mod tests { fn create_with_precomp_matches_create() { let n = 256; let crs = CRS::new(n, b"random seed"); - let committer = Committer::new(&crs.G, 6); + let committer = crs.committer(6); let mut rng = ChaCha20Rng::from_seed([7u8; 32]); let a: Vec = (0..n).map(|_| Fr::rand(&mut rng)).collect(); @@ -493,7 +455,6 @@ mod tests { let precomputed = create_with_precomp( &mut Transcript::new(b"ip_no_zk"), &committer, - crs.Q, a.clone(), a_comm, b.clone(), @@ -502,21 +463,6 @@ mod tests { assert_eq!(folded.to_bytes().unwrap(), precomputed.to_bytes().unwrap()); - // Tables that also carry `Q` (as base `n`) take the fixed-base blinding path. - let mut bases_with_q = crs.G.clone(); - bases_with_q.push(crs.Q); - let committer_with_q = Committer::new(&bases_with_q, 6); - let fixed_base_q = create_with_precomp( - &mut Transcript::new(b"ip_no_zk"), - &committer_with_q, - crs.Q, - a.clone(), - a_comm, - b.clone(), - input_point, - ); - assert_eq!(folded.to_bytes().unwrap(), fixed_base_q.to_bytes().unwrap()); - let output_point = inner_product(&a, &b); assert!(precomputed.verify_multiexp( &mut Transcript::new(b"ip_no_zk"), diff --git a/ipa-multipoint/src/lagrange_basis.rs b/ipa-multipoint/src/lagrange_basis.rs index 27df8cf6..6ace3c7f 100644 --- a/ipa-multipoint/src/lagrange_basis.rs +++ b/ipa-multipoint/src/lagrange_basis.rs @@ -236,7 +236,7 @@ impl LagrangeBasis { self.values[index] } - pub fn values(&self) -> &[Fr] { + pub(crate) fn values(&self) -> &[Fr] { &self.values } diff --git a/ipa-multipoint/src/math_utils.rs b/ipa-multipoint/src/math_utils.rs index f56f47ef..da3d76c0 100644 --- a/ipa-multipoint/src/math_utils.rs +++ b/ipa-multipoint/src/math_utils.rs @@ -1,6 +1,6 @@ use banderwagon::{trait_defs::*, Fr}; use salt_macros::prelude::*; -use salt_macros::{chunks_mut, num_threads}; +use salt_macros::{chunks_mut, thread_chunk_size}; use std::{vec, vec::Vec}; /// Computes the inner product between two scalar vectors @@ -32,7 +32,7 @@ pub fn powers_of_par(point: Fr, n: usize) -> Vec { let mut powers = vec![Fr::zero(); n]; // Compute base powers for each chunk - let chunk_size = n.div_ceil(num_threads!()); + let chunk_size = thread_chunk_size!(n); // to handle the case where n is not a multiple of chunk_size let len = n.div_ceil(chunk_size); diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index 3b8b1aa9..0a53aed8 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -15,9 +15,8 @@ use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; use salt_macros::prelude::*; -use salt_macros::{chunks, chunks_mut, into_iter, iter, num_threads, reduce}; -use std::sync::Arc; -use std::{vec, vec::Vec}; +use salt_macros::{chunks, chunks_mut, into_iter, iter, reduce, thread_chunk_size}; +use std::{sync::Arc, vec, vec::Vec}; pub struct MultiPoint; @@ -87,8 +86,7 @@ impl MultiPoint { /// per-round generator folding and the small variable-base MSMs, which /// dominate proving time. /// - /// `committer` must hold tables for the CRS `G` vector in order; tables that also hold - /// `Q` as base `crs.n` make the IPA's blinding terms fixed-base too. + /// `committer` is `crs`'s fixed-base tables, [`CRS::committer`]: `G` in order, then `Q`. pub fn open_with_committer( crs: &CRS, committer: &Committer, @@ -120,8 +118,7 @@ impl MultiPoint { let grouped_queries: Vec<_> = into_iter!(grouped_queries).collect(); - // `max(1)`: an empty query set must not reach `chunks(0)`, which panics. - let chunk_size = grouped_queries.len().div_ceil(num_threads!()).max(1); + let chunk_size = thread_chunk_size!(grouped_queries.len()); // aggregate all of the queries evaluated at the same point; // accumulate scaled polynomials in place instead of cloning each @@ -195,15 +192,13 @@ impl MultiPoint { let g_3_x_comm = g1_comm - g_x_comm; // 4. Compute the IPA for g_3 + let a = g_3_x.values().to_vec(); + let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, t); let g_3_ipa = match committer { Some(committer) => { - let a = g_3_x.values().to_vec(); - let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, t); - crate::ipa::create_with_precomp(transcript, committer, crs.Q, a, g_3_x_comm, b, t) - } - None => { - open_point_outside_of_domain(crs.clone(), precomp, transcript, g_3_x, g_3_x_comm, t) + crate::ipa::create_with_precomp(transcript, committer, a, g_3_x_comm, b, t) } + None => crate::ipa::create(transcript, crs.clone(), a, g_3_x_comm, b, t), }; MultiPointProof { @@ -219,7 +214,7 @@ fn commit_dense(crs: &CRS, committer: Option<&Committer>, poly: &LagrangeBasis) match committer { Some(committer) => { let terms: Vec<(usize, Fr)> = poly.values().iter().copied().enumerate().collect(); - crate::ipa::fixed_base_msm(committer, &terms) + committer.msm(&terms) } None => crs.commit_lagrange_poly(poly), } @@ -398,24 +393,6 @@ impl MultiPointProof { } } -// TODO: we could probably get rid of this method altogether and just do this in the multiproof -// TODO method -// TODO: check that the point is actually not in the domain -pub(crate) fn open_point_outside_of_domain( - crs: CRS, - precomp: &PrecomputedWeights, - transcript: &mut Transcript, - polynomial: LagrangeBasis, - commitment: Element, - z_i: Fr, -) -> IPAProof { - let a = polynomial.values().to_vec(); - - let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, z_i); - - crate::ipa::create(transcript, crs, a, commitment, b, z_i) -} - #[cfg(test)] mod tests { use super::*; @@ -533,13 +510,13 @@ mod tests { ); let mut prover_transcript = Transcript::new(b"test"); - - let proof = open_point_outside_of_domain( - crs.clone(), - &precomp, + let b = LagrangeBasis::evaluate_lagrange_coefficients(&precomp, crs.n, input_point); + let proof = crate::ipa::create( &mut prover_transcript, - polynomial, + crs.clone(), + poly.clone(), commitment, + b.clone(), input_point, ); @@ -552,7 +529,6 @@ mod tests { ); let mut verifier_transcript = Transcript::new(b"test"); - let b = LagrangeBasis::evaluate_lagrange_coefficients(&precomp, crs.n, input_point); let output_point = inner_product(&poly, &b); let mut bytes = [0u8; 32]; output_point.serialize_compressed(&mut bytes[..]).unwrap(); @@ -667,7 +643,7 @@ mod tests { fn open_with_committer_matches_open() { let n = 256; let crs = CRS::new(n, b"random seed"); - let committer = Committer::new(&crs.G, 6); + let committer = crs.committer(6); let precomp = PrecomputedWeights::new(n); let mut rng = test_rng(); diff --git a/salt-macros/src/lib.rs b/salt-macros/src/lib.rs index 5e0466ac..40901ef2 100644 --- a/salt-macros/src/lib.rs +++ b/salt-macros/src/lib.rs @@ -26,6 +26,15 @@ macro_rules! num_threads { }}; } +/// The chunk size that spreads `len` items over the threads [`num_threads!`] reports, never +/// zero: `chunks!`/`chunks_mut!` panic on a zero size, and an empty slice still has to chunk. +#[macro_export] +macro_rules! thread_chunk_size { + ($len: expr) => { + ($len).div_ceil($crate::num_threads!()).max(1) + }; +} + /// Chooses between parallel and sequential `into_iter`. /// Uses `into_par_iter()` if the "parallel" feature is enabled, otherwise uses `into_iter()`. #[macro_export] diff --git a/salt/benches/proof.rs b/salt/benches/proof.rs index 25a9ee99..25799bd2 100644 --- a/salt/benches/proof.rs +++ b/salt/benches/proof.rs @@ -125,12 +125,12 @@ type PlainUpdates = BTreeMap, Option>>; fn witness_workload(s: &Setup, n: usize, rng: &mut StdRng) -> (Vec>, PlainUpdates) { let half = n / 2; let hits = half * 2 / 3; - let step = s.plain_keys.len() / half.max(1); + let step = (s.plain_keys.len() / half.max(1)).max(1); let mut lookups: Vec> = s .plain_keys .iter() - .step_by(step.max(1)) + .step_by(step) .take(hits) .cloned() .collect(); @@ -139,13 +139,7 @@ fn witness_workload(s: &Setup, n: usize, rng: &mut StdRng) -> (Vec>, Pla } let mut updates = BTreeMap::new(); - for key in s - .plain_keys - .iter() - .skip(1) - .step_by(step.max(1)) - .take(half / 2) - { + for key in s.plain_keys.iter().skip(1).step_by(step).take(half / 2) { updates.insert(key.clone(), Some(mock_data(rng, 32))); // overwrite } while updates.len() < half { diff --git a/salt/src/proof/prover.rs b/salt/src/proof/prover.rs index c4c6c7a4..230c3c08 100644 --- a/salt/src/proof/prover.rs +++ b/salt/src/proof/prover.rs @@ -23,7 +23,7 @@ use ipa_multipoint::{ use crate::Lazy; use salt_macros::prelude::*; -use salt_macros::{chunks, into_iter, iter, num_threads, sort_unstable}; +use salt_macros::{chunks, into_iter, iter, sort_unstable, thread_chunk_size}; use serde::{ de::{Error as _, MapAccess, Visitor}, ser::SerializeMap, @@ -233,10 +233,9 @@ impl SaltProof { // Reuse the shared CRS (deriving it decompresses 257 points) and the // trie's fixed-base tables for all MSMs over the CRS generators. - let committer = shared_committer(); let proof = MultiPoint::open_with_committer( &DEFAULT_CRS, - &committer, + shared_committer(), &PRECOMPUTED_WEIGHTS, &mut transcript, prover_queries, @@ -396,7 +395,7 @@ fn create_internal_node_queries( ) -> ProofResult> { // Distribute internal nodes across CPU threads for parallel processing let in_nodes: Vec<_> = internal_nodes.iter().collect(); - let chunk_size = in_nodes.len().div_ceil(num_threads!()); + let chunk_size = thread_chunk_size!(in_nodes.len()); let queries = chunks!(in_nodes, chunk_size) .map(|nodes| { // Step 1: Collect all child commitments needed by this thread's nodes diff --git a/salt/src/proof/shape.rs b/salt/src/proof/shape.rs index 37771e46..9c246d46 100644 --- a/salt/src/proof/shape.rs +++ b/salt/src/proof/shape.rs @@ -11,7 +11,7 @@ //! - **Bucket Subtrees**: Dynamic trees within buckets that can expand from 1-5 levels use salt_macros::prelude::*; -use salt_macros::{chunks, num_threads}; +use salt_macros::{chunks, thread_chunk_size}; use std::collections::{BTreeMap, BTreeSet}; use std::vec::Vec; @@ -64,9 +64,7 @@ pub(crate) fn parents_and_points( ) { // One accumulator pair per thread chunk, merged once at the end. (A // per-key map-reduce would allocate and merge two BTreeMaps per key.) - // `max(1)`: the verifier calls this with whatever key set it was handed, including none, - // and `chunks(0)` panics. - let chunk_size = salt_keys.len().div_ceil(num_threads!()).max(1); + let chunk_size = thread_chunk_size!(salt_keys.len()); let partials: Vec<_> = chunks!(salt_keys, chunk_size) .map(|chunk| { let mut internal_nodes: BTreeMap> = BTreeMap::new(); @@ -175,12 +173,15 @@ fn record_key_paths( subtree_leaf_for_key(salt_key) }; - // Record which slot position within the segment contains this key - // Use lowest 8 bits of slot_id as position within 256-slot segment slot_position_nodes .entry(node) .or_default() - .insert((salt_key.slot_id() & 0xFF) as usize); + .insert(slot_position(salt_key)); +} + +/// The position of `key`'s slot in its leaf's 256-slot polynomial (the slot id's low 8 bits). +pub(super) fn slot_position(key: &SaltKey) -> usize { + (key.slot_id() & 0xFF) as usize } /// Encodes bucket tree level information into a main trie node ID. diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 2079b134..a49772df 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -11,8 +11,9 @@ //! 1. Extract and deduplicate bucket IDs from input keys //! 2. Determine trie levels for each bucket (metadata vs dynamic data buckets) //! 3. Build minimal node hierarchy using [`parents_and_points`] -//! 4. Collect cryptographic commitments for all parent nodes -//! 5. Generate IPA prover queries for leaf nodes (bucket contents) and internal nodes (child commitments) +//! 4. Read the commitment of every parent node +//! 5. Resolve every parent's polynomial, from the process-wide node-polynomial cache or storage +//! 6. Generate IPA prover queries for leaf nodes (bucket contents) and internal nodes (child commitments) use crate::{ constant::{ default_commitment, BUCKET_SLOT_BITS, BUCKET_SLOT_ID_MASK, DOMAIN_SIZE, MAX_SUBTREE_LEVELS, @@ -20,7 +21,7 @@ use crate::{ }, proof::{ prover::slot_to_field, - shape::{connect_parent_id, logic_parent_id, parents_and_points}, + shape::{connect_parent_id, logic_parent_id, parents_and_points, slot_position}, ProofError, ProofResult, SerdeCommitment, }, traits::{StateReader, TrieReader}, @@ -28,11 +29,11 @@ use crate::{ types::{BucketId, BucketMeta, CommitmentBytes, NodeId, SaltKey}, SlotId, }; -use banderwagon::{Element, Fr, Zero}; +use banderwagon::{Element, Fr}; use ipa_multipoint::{lagrange_basis::LagrangeBasis, multiproof::ProverQuery}; use salt_macros::prelude::*; -use salt_macros::{chunks, iter, num_threads}; +use salt_macros::{chunks, iter, thread_chunk_size}; use std::collections::{BTreeMap, BTreeSet}; use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; @@ -47,8 +48,8 @@ type FxHashMap = HashMap; /// polynomial was last built has the same 256 child scalars (internal node) or slot scalars /// (leaf node), and the store reads plus scalar conversions that produce them can be skipped. /// Consecutive blocks share most of their authentication paths, which is what makes the hit -/// rate worth the memory: at most `SHARDS * MAX_PER_SHARD` polynomials of 8 KiB each. A shard -/// that fills up is cleared whole; the working set refills within a few proofs. +/// rate worth the memory: at most `SHARDS * MAX_PER_SHARD` polynomials of 8 KiB each, 256 MiB. +/// A shard that fills up is cleared whole; the working set refills within a few proofs. mod node_poly_cache { use super::*; use crate::Lazy; @@ -96,7 +97,6 @@ mod node_poly_cache { // Constants for improved code readability const METADATA_BUCKET_LEVEL: u8 = 1; -const SLOT_INDEX_MASK: u64 = 0xff; const ROOT_LEVEL_CHILD_START: NodeId = 1; /// Information returned by the subtrie creation process @@ -106,34 +106,14 @@ type SubTrieInfo = ( FxHashMap, ); -/// Converts cryptographic commitments from multiple internal trie nodes into scalar field elements. -/// -/// This function processes internal nodes in the trie hierarchy and converts their child node -/// commitments into scalar field elements suitable for IPA (Inner Product Argument) proof generation. -/// For each internal node, it loads commitments for all 256 possible child positions, using default -/// commitments where actual child nodes don't exist (sparse trie optimization). -/// -/// # Parameters -/// -/// * `store` - Storage backend providing access to trie node commitments -/// * `nodes` - Internal nodes (logical ids) whose children to convert -/// -/// # Returns -/// -/// A vector of scalar field elements (`Fr`) representing all child commitments for the given nodes. -/// The elements are ordered by node, then by child index (0-255 per node). -/// -/// # Implementation Details -/// -/// - Handles sparse child nodes by filling missing positions with appropriate default commitments -/// - Special handling for root level nodes (different default commitment for first child) -/// - Uses parallel processing for performance when dealing with multiple nodes -/// - Converts Element commitments to scalar field using `serial_batch_map_to_scalar_field` +/// The polynomial of every internal node in `nodes` (logical ids), in order: its 256 child +/// commitments mapped to the scalar field, with the level's default commitment where a child +/// does not exist in storage (the root level's first child has its own default). /// /// # Errors /// /// Returns `ProofError::StateReadError` if unable to read child node commitments from storage. -fn multi_commitments_to_scalars(store: &Store, nodes: &[NodeId]) -> ProofResult> +fn internal_polynomials(store: &Store, nodes: &[NodeId]) -> ProofResult> where Store: TrieReader, { @@ -152,9 +132,9 @@ where .or_insert_with(|| to_element(bytes).map_to_scalar_field()) }; - let mut scalars = vec![Fr::zero(); nodes.len() * DOMAIN_SIZE]; - // Children present in storage: their positions in `scalars`, and their commitments - let mut real_positions: Vec = Vec::new(); + let mut polys: Vec> = Vec::with_capacity(nodes.len()); + // Children present in storage: `(node index, child index)`, and their commitments + let mut real_positions: Vec<(usize, usize)> = Vec::new(); let mut real_children: Vec = Vec::new(); for (i, node_id) in nodes.iter().enumerate() { @@ -174,8 +154,8 @@ where } else { child_idx // Non-root levels: all use child_idx as default }; - let node_scalars = &mut scalars[i * DOMAIN_SIZE..(i + 1) * DOMAIN_SIZE]; - node_scalars.fill(cached_default_scalar(default_commitment(default_idx))); + let mut node_scalars = + vec![cached_default_scalar(default_commitment(default_idx)); DOMAIN_SIZE]; // Special case: root level first child uses different default if child_idx == ROOT_LEVEL_CHILD_START { @@ -184,19 +164,29 @@ where // Record actual commitments to overwrite the defaults where they exist for (absolute_node_id, commitment_bytes) in children { - let relative_index = absolute_node_id as usize - child_idx as usize; - real_positions.push(i * DOMAIN_SIZE + relative_index); + real_positions.push((i, absolute_node_id as usize - child_idx as usize)); real_children.push(to_element(commitment_bytes)); } + polys.push(node_scalars); } // Batch-convert the existing children and scatter them into place. let real_scalars = Element::batch_map_to_scalar_field(&real_children); - for (position, scalar) in real_positions.into_iter().zip(real_scalars) { - scalars[position] = scalar; + for ((node, position), scalar) in real_positions.into_iter().zip(real_scalars) { + polys[node][position] = scalar; } - Ok(scalars) + Ok(polys.into_iter().map(LagrangeBasis::new).collect()) +} + +/// One parent node of the subtrie: the logical id [`parents_and_points`] reports (which +/// encodes a bucket root's level), the physical id the store and the polynomial cache key by, +/// and its commitment as read for this proof. +struct ParentNode { + logical: NodeId, + physical: NodeId, + bytes: CommitmentBytes, + commitment: Element, } /// Creates IPA prover queries for a given commitment and evaluation points. @@ -267,40 +257,28 @@ where }); } - // Steps 1 & 2: Validate every key against its bucket capacity and record - // the trie level of each bucket. Keys arrive sorted, so each bucket's - // metadata is read exactly once. + // Steps 1 & 2: Validate every key against its bucket capacity and record the trie level + // of each bucket. Keys arrive sorted, so each bucket's keys form one run and its metadata + // is read once. let mut buckets_level: FxHashMap = FxHashMap::default(); - let mut current_bucket: Option<(BucketId, u64)> = None; - for key in salt_keys { - let bucket_id = key.bucket_id(); - let capacity = match current_bucket { - Some((bucket, capacity)) if bucket == bucket_id => capacity, - _ => { - let (capacity, level) = if bucket_id < NUM_META_BUCKETS as BucketId { - // Metadata buckets are always at level 1 (never expand into subtrees) - (META_BUCKET_SIZE as u64, METADATA_BUCKET_LEVEL) - } else { - // Data buckets: read metadata to determine capacity and - // subtree structure (higher capacity = higher level root) - let meta = - store - .metadata(bucket_id) - .map_err(|e| ProofError::StateReadError { - reason: format!( - "Failed to read metadata for bucket {bucket_id}: {e:?}" - ), - })?; - let level = MAX_SUBTREE_LEVELS - subtree_root_level(meta.capacity); - (meta.capacity, level as u8) - }; - buckets_level.insert(bucket_id, level); - current_bucket = Some((bucket_id, capacity)); - capacity - } + for bucket_keys in salt_keys.chunk_by(|a, b| a.bucket_id() == b.bucket_id()) { + let bucket_id = bucket_keys[0].bucket_id(); + let (capacity, level) = if bucket_id < NUM_META_BUCKETS as BucketId { + // Metadata buckets are always at level 1 (never expand into subtrees) + (META_BUCKET_SIZE as u64, METADATA_BUCKET_LEVEL) + } else { + // Data buckets: read metadata to determine capacity and + // subtree structure (higher capacity = higher level root) + let meta = store + .metadata(bucket_id) + .map_err(|e| ProofError::StateReadError { + reason: format!("Failed to read metadata for bucket {bucket_id}: {e:?}"), + })?; + let level = MAX_SUBTREE_LEVELS - subtree_root_level(meta.capacity); + (meta.capacity, level as u8) }; - - if key.slot_id() >= capacity { + buckets_level.insert(bucket_id, level); + if let Some(key) = bucket_keys.iter().find(|key| key.slot_id() >= capacity) { return Err(ProofError::InvalidSaltKey { key: *key, capacity, @@ -311,124 +289,90 @@ where // Step 3: Build the minimal node hierarchy needed for authentication let (internal_nodes, leaf_nodes) = parents_and_points(salt_keys, &buckets_level); - // Step 4: Collect cryptographic commitments for all parent nodes in - // parallel; steps 5 and 6 reuse them instead of re-reading the store. - let parent_ids: Vec = internal_nodes - .keys() - .chain(leaf_nodes.keys()) - .map(|&parent| connect_parent_id(parent)) - .collect(); - let commitment_chunk = parent_ids.len().div_ceil(num_threads!()); - let parents_read: Vec<(NodeId, CommitmentBytes)> = chunks!(parent_ids, commitment_chunk) - .map(|chunk| { - chunk - .iter() - .map(|&physical_parent| { - let bytes = store.commitment(physical_parent).map_err(|e| { - ProofError::StateReadError { - reason: format!( - "Failed to load commitment for node {physical_parent}: {e:?}" - ), - } + // Step 4: Read every parent's commitment in parallel, internal nodes first, then leaves; + // steps 5 and 6 index this list instead of re-reading the store. + let parents: Vec = { + let logical_ids: Vec = internal_nodes + .keys() + .chain(leaf_nodes.keys()) + .copied() + .collect(); + // A store read is too short a task to split below one chunk per thread. + iter!(logical_ids, thread_chunk_size!(logical_ids.len())) + .map(|&logical| { + let physical = connect_parent_id(logical); + let bytes = store + .commitment(physical) + .map_err(|e| ProofError::StateReadError { + reason: format!("Failed to load commitment for node {physical}: {e:?}"), })?; - Ok((physical_parent, bytes)) + Ok(ParentNode { + logical, + physical, + bytes, + commitment: Element::from_bytes_unchecked_uncompressed(bytes), }) - .collect::>>() - }) - .collect::>>()? - .into_iter() - .flatten() - .collect(); - // The raw bytes key the polynomial cache; the decoded elements go into the proof. - let parent_bytes: FxHashMap = parents_read.iter().copied().collect(); - let parents_commitments: BTreeMap = parents_read - .into_iter() - .map(|(id, bytes)| { - ( - id, - SerdeCommitment(Element::from_bytes_unchecked_uncompressed(bytes)), - ) - }) - .collect(); - - let parent_commitment = |parent: NodeId| -> ProofResult { - parents_commitments - .get(&connect_parent_id(parent)) - .map(|commitment| commitment.0) - .ok_or_else(|| ProofError::StateReadError { - reason: format!("Failed to load commitment for node {parent}"), }) + .collect::>()? }; + let parents_commitments: BTreeMap = parents + .iter() + .map(|parent| (parent.physical, SerdeCommitment(parent.commitment))) + .collect(); - // Step 5: Generate IPA prover queries for the internal nodes, then the leaf nodes. Internal - // nodes the cache misses are materialized in parallel chunks, leaf nodes one per task. - let internal_nodes: Vec<_> = internal_nodes.into_iter().collect(); - let leaf_nodes: Vec<_> = leaf_nodes.into_iter().collect(); - let internal_polys = resolve_polys(&internal_nodes, &parent_bytes, |missing| { - // `max(1)`: `missing` is empty when every node hits the cache, and `chunks(0)` panics. - let chunk_size = missing.len().div_ceil(num_threads!()).max(1); + // Step 5: Resolve the polynomial of every parent, internal nodes in parallel chunks and + // leaves one per task. + let (internal_parents, leaf_parents) = parents.split_at(internal_nodes.len()); + let internal_polys = resolve_polys(internal_parents, |missing| { + let chunk_size = thread_chunk_size!(missing.len()); let chunks = chunks!(missing, chunk_size) - .map(|nodes| { - let scalars = multi_commitments_to_scalars(store, nodes)?; - Ok(scalars - .chunks(DOMAIN_SIZE) - .map(|node_scalars| LagrangeBasis::new(node_scalars.to_vec())) - .collect::>()) - }) + .map(|nodes| internal_polynomials(store, nodes)) .collect::>>()?; Ok(chunks.into_iter().flatten().collect()) })?; - let leaf_polys = resolve_polys(&leaf_nodes, &parent_bytes, |missing| { + let leaf_polys = resolve_polys(leaf_parents, |missing| { iter!(missing) .map(|&node| leaf_polynomial(store, node)) .collect() })?; + // Step 6: One IPA prover query per proven point, in node order. + let points = internal_nodes.into_values().chain(leaf_nodes.into_values()); + let polys = internal_polys.into_iter().chain(leaf_polys); let mut queries: Vec = Vec::new(); - let nodes = internal_nodes.into_iter().chain(leaf_nodes); - for ((parent, points), poly) in nodes.zip(internal_polys.into_iter().chain(leaf_polys)) { - queries.extend(create_prover_queries( - parent_commitment(parent)?, - poly, - &points, - )); + for ((parent, points), poly) in parents.iter().zip(points).zip(polys) { + queries.extend(create_prover_queries(parent.commitment, poly, &points)); } Ok((queries, parents_commitments, buckets_level)) } -/// The polynomial of every node in `nodes` (logical parent ids), in order: query order is -/// node order, which the transcript hashes, so a cache hit must not reorder the proof. A node -/// whose polynomial the cache holds for the commitment it was read at skips `rebuild`; the -/// misses are rebuilt in one call, one polynomial per missing node, and cached under that -/// commitment. +/// The polynomial of every parent in `parents`, in order: query order is node order, which +/// the transcript hashes, so a cache hit must not reorder the proof. A parent whose polynomial +/// the cache holds for the commitment it was read at skips `rebuild`; the misses are rebuilt +/// in one call (by logical id, one polynomial per miss) and cached under that commitment. fn resolve_polys( - nodes: &[(NodeId, BTreeSet)], - parent_bytes: &FxHashMap, + parents: &[ParentNode], rebuild: impl FnOnce(&[NodeId]) -> ProofResult>, ) -> ProofResult>> { - let hits: Vec>> = nodes + let hits: Vec>> = parents .iter() - .map(|(parent, _)| { - let physical = connect_parent_id(*parent); - node_poly_cache::get(physical, &parent_bytes[&physical]) - }) + .map(|parent| node_poly_cache::get(parent.physical, &parent.bytes)) .collect(); - let missing: Vec = nodes + let missing: Vec = parents .iter() .zip(&hits) .filter(|(_, hit)| hit.is_none()) - .map(|((parent, _), _)| *parent) + .map(|(parent, _)| parent.logical) .collect(); let mut rebuilt = rebuild(&missing)?.into_iter(); - Ok(nodes + Ok(parents .iter() .zip(hits) - .map(|((parent, _), hit)| { + .map(|(parent, hit)| { hit.unwrap_or_else(|| { - let physical = connect_parent_id(*parent); let poly = Arc::new(rebuilt.next().expect("one rebuilt polynomial per miss")); - node_poly_cache::insert(physical, parent_bytes[&physical], Arc::clone(&poly)); + node_poly_cache::insert(parent.physical, parent.bytes, Arc::clone(&poly)); poly }) }) @@ -487,11 +431,6 @@ fn empty_slot_scalar(bucket_id: BucketId) -> Fr { } } -/// The position of `key`'s slot in its leaf's 256-slot polynomial (the slot id's low 8 bits). -fn slot_position(key: &SaltKey) -> usize { - (key.slot_id() & SLOT_INDEX_MASK) as usize -} - #[cfg(test)] mod tests { use super::*; @@ -669,22 +608,24 @@ mod tests { } #[test] - fn multi_commitments_to_scalars_empty_and_single_node() { + fn internal_polynomials_empty_and_single_node() { let store = MemStore::new(); // Empty input - assert_eq!(multi_commitments_to_scalars(&store, &[]).unwrap().len(), 0); + assert!(internal_polynomials(&store, &[]).unwrap().is_empty()); // Single internal node - let scalars = - multi_commitments_to_scalars(&store, &[STARTING_NODE_ID[2] as NodeId]).unwrap(); - assert_eq!(scalars.len(), DOMAIN_SIZE); + let polys = internal_polynomials(&store, &[STARTING_NODE_ID[2] as NodeId]).unwrap(); + assert_eq!(polys.len(), 1); } #[test] - fn multi_commitments_to_scalars_root_defaults_are_pinned() { + fn internal_polynomials_root_defaults_are_pinned() { let store = MemStore::new(); - let scalars = multi_commitments_to_scalars(&store, &[ROOT_NODE_ID]).unwrap(); + let polys = internal_polynomials(&store, &[ROOT_NODE_ID]).unwrap(); + let scalars: Vec = (0..DOMAIN_SIZE) + .map(|i| polys[0].evaluate_in_domain(i)) + .collect(); let expected = Element::batch_map_to_scalar_field(&[ Element::from_bytes_unchecked_uncompressed(default_commitment( STARTING_NODE_ID[1] as NodeId, diff --git a/salt/src/proof/test_utils.rs b/salt/src/proof/test_utils.rs index 5be045d3..19207ebb 100644 --- a/salt/src/proof/test_utils.rs +++ b/salt/src/proof/test_utils.rs @@ -5,7 +5,7 @@ use crate::mem_store::MemStore; use crate::proof::SerdeCommitment; use crate::state::updates::StateUpdates; -use crate::trie::trie::{StateRoot, TrieUpdates}; +use crate::trie::trie::StateRoot; use crate::types::SaltValue; use banderwagon::{Element, Fr}; use rand::{rngs::StdRng, Rng}; @@ -42,10 +42,9 @@ pub(crate) fn mock_salt_value() -> SaltValue { SaltValue::new(&[1u8; 32], &[2u8; 32]) } -/// Applies one block's transition to `store` and returns its trie updates. -pub(crate) fn apply_block(store: &MemStore, updates: StateUpdates) -> TrieUpdates { +/// Applies one block's transition to `store`. +pub(crate) fn apply_block(store: &MemStore, updates: StateUpdates) { let (_, trie_updates) = StateRoot::new(store).update_fin(&updates).unwrap(); store.update_state(updates); - store.update_trie(trie_updates.clone()); - trie_updates + store.update_trie(trie_updates); } diff --git a/salt/src/proof/witness.rs b/salt/src/proof/witness.rs index bfccf51a..d7b0aafc 100644 --- a/salt/src/proof/witness.rs +++ b/salt/src/proof/witness.rs @@ -195,11 +195,9 @@ impl Witness { reason: format!("{e:?}"), })?; - let salt_witness = SaltWitness::create(&witnessed_keys, store)?; - Ok(Witness { direct_lookup_tbl, - salt_witness, + salt_witness: SaltWitness::create(&witnessed_keys, store)?, }) } @@ -462,9 +460,7 @@ mod tests { let witness = Witness::create([], kvs.keys(), &BTreeMap::new(), &store).unwrap(); // Test serialization round-trip of the underlying SaltWitness - let serialized = - bincode::serde::encode_to_vec(&witness.salt_witness, bincode::config::legacy()) - .unwrap(); + let serialized = encode_salt_witness(&witness); let (deserialized, _): (SaltWitness, _) = bincode::serde::decode_from_slice(&serialized, bincode::config::legacy()).unwrap(); diff --git a/salt/src/trie/trie.rs b/salt/src/trie/trie.rs index 23909b94..400ddd85 100644 --- a/salt/src/trie/trie.rs +++ b/salt/src/trie/trie.rs @@ -47,16 +47,14 @@ use core::{cmp::Ordering, ops::Range}; /// each costing a modular square root plus a subgroup check (several /// milliseconds total), so it is done once per process and reused by the /// shared committer and by every proof creation and verification. -pub static DEFAULT_CRS: Lazy = Lazy::new(CRS::default); +pub(crate) static DEFAULT_CRS: Lazy = Lazy::new(CRS::default); /// Global shared instance of the Committer to avoid repeated expensive initialization static SHARED_COMMITTER: Lazy> = Lazy::new(|| Arc::new(build_shared_committer())); -/// Returns the process-wide committer holding fixed-base precomputation -/// tables for the CRS generators. Forces the one-time table construction on -/// first use. -pub(crate) fn shared_committer() -> Arc { - Arc::clone(&SHARED_COMMITTER) +/// The process-wide committer: fixed-base tables over the shared CRS. +pub(crate) fn shared_committer() -> &'static Committer { + SHARED_COMMITTER.as_ref() } /// Builds the shared committer's precomputation tables. @@ -72,18 +70,7 @@ pub(crate) fn shared_committer() -> Arc { /// touches `SHARED_COMMITTER` would re-enter this initialization on the /// same stack and deadlock. fn build_shared_committer() -> Committer { - let build = || { - let crs = &*DEFAULT_CRS; - // `Q` rides along as base `crs.n`, so the IPA prover's blinding terms are fixed-base - // multiplications too (see `ipa::create_with_precomp`). - let bases: Vec = crs - .G - .iter() - .copied() - .chain(core::iter::once(crs.Q)) - .collect(); - Committer::new(&bases, platform::DEFAULT_PRECOMP_WINDOW_SIZE) - }; + let build = || DEFAULT_CRS.committer(platform::DEFAULT_PRECOMP_WINDOW_SIZE); #[cfg(feature = "parallel")] { // Never run `build` inline here: under `parallel` it would par_iter From 7acdb55323d2137ffc2dd810d1820001aebc7290 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Tue, 22 Sep 2026 11:47:44 +0800 Subject: [PATCH 13/18] test(proof): pin the node-polynomial cache's eviction rule and refresh the suppressions The mutation gate's three survivors sat in the cache's eviction condition (`len >= MAX_PER_SHARD && !contains_key`): nothing filled a shard. That cannot be tested deterministically against a process-wide cache that every proof test writes to, so the shards become `NodePolyCache`, with `CACHE` as the one global instance, and a test fills a private instance: a full shard keeps its entries when a node it holds is re-inserted (at a new commitment, which retires the old one) and is cleared only by a node it does not hold. The suppression hygiene check has failed on this branch since 9/7: three entries named `multi_commitments_to_scalars` and `process_leaf_node`, which this branch rewrote (the `<` guard is now in `leaf_polynomial`; the `*` sizing arithmetic no longer exists), and four `create_node_aligned_chunks` entries were pinned to lines that `DEFAULT_CRS` and `shared_committer` shifted by eleven. Re-pointed and re-pinned; the orphan check passes. Co-Authored-By: Claude Fable 5.1 --- mutants/suppressions.toml | 30 +++------- salt/src/proof/subtrie.rs | 112 +++++++++++++++++++++++++++----------- 2 files changed, 86 insertions(+), 56 deletions(-) diff --git a/mutants/suppressions.toml b/mutants/suppressions.toml index e64c3b58..aacbb9d3 100644 --- a/mutants/suppressions.toml +++ b/mutants/suppressions.toml @@ -110,25 +110,9 @@ reviewer = "full-run triage 2026-07-08 (issue #144)" kind = "line" category = "equivalent" file = "salt/src/proof/subtrie.rs" -mutant = "replace * with + in multi_commitments_to_scalars" -justification = "total_capacity only sizes Vec::with_capacity, a performance hint with no behavioral effect." -reviewer = "full-run triage 2026-07-08 (issue #144)" - -[[suppress]] -kind = "line" -category = "equivalent" -file = "salt/src/proof/subtrie.rs" -mutant = "replace * with / in multi_commitments_to_scalars" -justification = "total_capacity only sizes Vec::with_capacity, a performance hint with no behavioral effect." -reviewer = "full-run triage 2026-07-08 (issue #144)" - -[[suppress]] -kind = "line" -category = "equivalent" -file = "salt/src/proof/subtrie.rs" -mutant = "replace < with <= in process_leaf_node" -justification = "parent == BUCKET_SLOT_ID_MASK is unreachable for the same reason as in create_leaf_node_queries: neither addressing scheme can produce that value." -reviewer = "full-run triage 2026-07-08 (issue #144)" +mutant = "replace < with <= in leaf_polynomial" +justification = "parent == BUCKET_SLOT_ID_MASK is unreachable for the same reason as in create_leaf_node_queries: neither addressing scheme can produce that value. (process_leaf_node became leaf_polynomial in #152; the two multi_commitments_to_scalars entries went with the total_capacity arithmetic that PR removed.)" +reviewer = "full-run triage 2026-07-08 (issue #144); re-pointed in #152" [[suppress]] kind = "line" @@ -269,7 +253,7 @@ reviewer = "full-run triage 2026-07-08 (issue #144)" kind = "line" category = "timeout" file = "salt/src/trie/trie.rs" -line = 895 +line = 906 mutant = "replace < with <= in StateRoot<'a, Store>::create_node_aligned_chunks" justification = "The outer-loop variant re-enters with next_boundary == len and never advances (observed hang). Pinned to the outer-loop site; the boundary-check twin terminates and is killed by the exact-chunks test." reviewer = "full-run triage 2026-07-08 (issue #144)" @@ -286,7 +270,7 @@ reviewer = "full-run triage 2026-07-08 (issue #144)" kind = "line" category = "timeout" file = "salt/src/trie/trie.rs" -line = 904 +line = 915 mutant = "replace < with == in StateRoot<'a, Store>::create_node_aligned_chunks" justification = "Observed non-terminating: the boundary check never fires, so next_boundary stops advancing inside the outer loop. Pinned to the boundary-check site." reviewer = "full-run triage 2026-07-08 (issue #144)" @@ -295,7 +279,7 @@ reviewer = "full-run triage 2026-07-08 (issue #144)" kind = "line" category = "timeout" file = "salt/src/trie/trie.rs" -line = 901 +line = 912 mutant = "replace += with *= in StateRoot<'a, Store>::create_node_aligned_chunks" justification = "The inner-scan variant multiplies by one and never advances past an equal-node run (observed hang). Pinned to the inner-scan site; the stride twin terminates and is killed by the exact-chunks test." reviewer = "full-run triage 2026-07-08 (issue #144)" @@ -304,7 +288,7 @@ reviewer = "full-run triage 2026-07-08 (issue #144)" kind = "line" category = "timeout" file = "salt/src/trie/trie.rs" -line = 906 +line = 917 mutant = "replace += with -= in StateRoot<'a, Store>::create_node_aligned_chunks" justification = "Observed non-terminating: the boundary marches backwards and the loop never completes. Pinned to the stride site." reviewer = "full-run triage 2026-07-08 (issue #144)" diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index a49772df..433d6367 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -55,43 +55,61 @@ mod node_poly_cache { use crate::Lazy; use spin::RwLock; - const SHARDS: usize = 64; - const MAX_PER_SHARD: usize = 512; + pub(super) const SHARDS: usize = 64; + pub(super) const MAX_PER_SHARD: usize = 512; type Shard = RwLock)>>; - static CACHE: Lazy> = Lazy::new(|| { - (0..SHARDS) - .map(|_| RwLock::new(FxHashMap::default())) - .collect() - }); - - fn shard(node: NodeId) -> &'static Shard { - &CACHE[(node % SHARDS as NodeId) as usize] - } + /// The shards of one cache: [`CACHE`] is the process-wide instance, and a test holds its own. + pub(super) struct NodePolyCache(Vec); - /// The cached polynomial of `node`, if it was built for exactly this commitment. - pub(super) fn get(node: NodeId, commitment: &CommitmentBytes) -> Option> { - let guard = shard(node).read(); - guard - .get(&node) - .filter(|(seen, _)| seen == commitment) - .map(|(_, poly)| Arc::clone(poly)) - } + pub(super) static CACHE: Lazy = Lazy::new(NodePolyCache::new); - pub(super) fn insert(node: NodeId, commitment: CommitmentBytes, poly: Arc) { - let mut guard = shard(node).write(); - // Only a new node grows the shard. Whatever the insert evicts or replaces is dropped - // after the lock is released, so readers do not wait on the frees. - let evicted = (guard.len() >= MAX_PER_SHARD && !guard.contains_key(&node)).then(|| { - core::mem::replace( - &mut *guard, - FxHashMap::with_capacity_and_hasher(MAX_PER_SHARD, FxBuildHasher), + impl NodePolyCache { + pub(super) fn new() -> Self { + Self( + (0..SHARDS) + .map(|_| RwLock::new(FxHashMap::default())) + .collect(), ) - }); - let replaced = guard.insert(node, (commitment, poly)); - drop(guard); - drop((evicted, replaced)); + } + + fn shard(&self, node: NodeId) -> &Shard { + &self.0[(node % SHARDS as NodeId) as usize] + } + + /// The cached polynomial of `node`, if it was built for exactly this commitment. + pub(super) fn get( + &self, + node: NodeId, + commitment: &CommitmentBytes, + ) -> Option> { + let guard = self.shard(node).read(); + guard + .get(&node) + .filter(|(seen, _)| seen == commitment) + .map(|(_, poly)| Arc::clone(poly)) + } + + pub(super) fn insert( + &self, + node: NodeId, + commitment: CommitmentBytes, + poly: Arc, + ) { + let mut guard = self.shard(node).write(); + // Only a new node grows the shard. Whatever the insert evicts or replaces is dropped + // after the lock is released, so readers do not wait on the frees. + let evicted = (guard.len() >= MAX_PER_SHARD && !guard.contains_key(&node)).then(|| { + core::mem::replace( + &mut *guard, + FxHashMap::with_capacity_and_hasher(MAX_PER_SHARD, FxBuildHasher), + ) + }); + let replaced = guard.insert(node, (commitment, poly)); + drop(guard); + drop((evicted, replaced)); + } } } @@ -357,7 +375,7 @@ fn resolve_polys( ) -> ProofResult>> { let hits: Vec>> = parents .iter() - .map(|parent| node_poly_cache::get(parent.physical, &parent.bytes)) + .map(|parent| node_poly_cache::CACHE.get(parent.physical, &parent.bytes)) .collect(); let missing: Vec = parents .iter() @@ -372,7 +390,7 @@ fn resolve_polys( .map(|(parent, hit)| { hit.unwrap_or_else(|| { let poly = Arc::new(rebuilt.next().expect("one rebuilt polynomial per miss")); - node_poly_cache::insert(parent.physical, parent.bytes, Arc::clone(&poly)); + node_poly_cache::CACHE.insert(parent.physical, parent.bytes, Arc::clone(&poly)); poly }) }) @@ -607,6 +625,34 @@ mod tests { assert!(queries.iter().all(|q| q.result == empty_default)); } + /// A full shard is cleared only by a node it does not hold: re-inserting a node it holds + /// replaces that entry and keeps the rest, and a lookup at another commitment misses. + #[test] + fn node_poly_cache_clears_a_full_shard_only_for_a_new_node() { + use node_poly_cache::{NodePolyCache, MAX_PER_SHARD, SHARDS}; + + let cache = NodePolyCache::new(); + let poly = Arc::new(LagrangeBasis::new(vec![Fr::zero(); DOMAIN_SIZE])); + let commitment = |byte: u8| -> CommitmentBytes { [byte; 64] }; + // Nodes `7 + i * SHARDS` share one shard. + let node = |i: usize| (7 + i * SHARDS) as NodeId; + + for i in 0..MAX_PER_SHARD { + cache.insert(node(i), commitment(1), Arc::clone(&poly)); + } + assert!((0..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_some())); + assert!(cache.get(node(0), &commitment(2)).is_none()); + + cache.insert(node(0), commitment(2), Arc::clone(&poly)); + assert!(cache.get(node(0), &commitment(2)).is_some()); + assert!(cache.get(node(0), &commitment(1)).is_none()); + assert!((1..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_some())); + + cache.insert(node(MAX_PER_SHARD), commitment(1), Arc::clone(&poly)); + assert!(cache.get(node(MAX_PER_SHARD), &commitment(1)).is_some()); + assert!((0..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_none())); + } + #[test] fn internal_polynomials_empty_and_single_node() { let store = MemStore::new(); From 75cdab8c28750d9bd9c5d4f2c485dcc64a52e3fa Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Wed, 14 Oct 2026 22:48:22 +0900 Subject: [PATCH 14/18] fix: restore misplaced docs and add guards a review of this branch found - crs.rs, salt_committer.rs: put `CRS::committer` and `Committer::msm` where they no longer split `commit_lagrange_poly` and `add_affine_point` from their docs - multiproof.rs: assert each query polynomial has one coefficient per CRS generator, as the old `LagrangeBasis` addition did, and check the committer's base count before any proving work in `open_with_committer` - subtrie.rs: document that the node-polynomial cache relies on callers keeping the store unchanged while a proof is created Co-Authored-By: Claude Sonnet 5.5 --- banderwagon/src/salt_committer.rs | 22 ++++++++++------------ ipa-multipoint/src/crs.rs | 8 ++++---- ipa-multipoint/src/multiproof.rs | 10 ++++++++++ salt/src/proof/subtrie.rs | 7 +++++++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/banderwagon/src/salt_committer.rs b/banderwagon/src/salt_committer.rs index dc89e61a..18500f30 100644 --- a/banderwagon/src/salt_committer.rs +++ b/banderwagon/src/salt_committer.rs @@ -338,19 +338,7 @@ impl Committer { .for_each(|p| add_affine_point(&mut result, &p.x, &p.y)); Element(result) } -} -/// Adds an affine point to a projective point using extended Edwards coordinates. -/// -/// This is the generic implementation using the extended twisted Edwards addition formula. -/// The formula is optimized for adding an affine point (Z=1) to a projective point. -/// -/// # Arguments -/// -/// * `result` - The projective point to update (in-place) -/// * `p2_x` - X-coordinate of the affine point to add -/// * `p2_y` - Y-coordinate of the affine point to add -impl Committer { /// Sums `scalar · G[index]` over `terms`, splitting them across threads. pub fn msm(&self, terms: &[(usize, Fr)]) -> Element { chunks!(terms, thread_chunk_size!(terms.len())) @@ -367,6 +355,16 @@ impl Committer { } } +/// Adds an affine point to a projective point using extended Edwards coordinates. +/// +/// This is the generic implementation using the extended twisted Edwards addition formula. +/// The formula is optimized for adding an affine point (Z=1) to a projective point. +/// +/// # Arguments +/// +/// * `result` - The projective point to update (in-place) +/// * `p2_x` - X-coordinate of the affine point to add +/// * `p2_y` - Y-coordinate of the affine point to add #[cfg(not(target_arch = "x86_64"))] fn add_affine_point(result: &mut EdwardsProjective, p2_x: &Fq, p2_y: &Fq) { use ark_ff::biginteger::BigInt; diff --git a/ipa-multipoint/src/crs.rs b/ipa-multipoint/src/crs.rs index ee491a57..23350e11 100644 --- a/ipa-multipoint/src/crs.rs +++ b/ipa-multipoint/src/crs.rs @@ -177,6 +177,10 @@ impl CRS { /// /// # Returns /// The elliptic curve point representing the polynomial commitment + pub fn commit_lagrange_poly(&self, polynomial: &LagrangeBasis) -> Element { + slow_vartime_multiscalar_mul(polynomial.values().iter(), self.G.iter()) + } + /// Fixed-base tables over this CRS: `G` in order, then `Q` as base `n`. This is the shape /// [`crate::multiproof::MultiPoint::open_with_committer`] and [`crate::ipa::create_with_precomp`] /// take, so every MSM they compute — the IPA's blinding terms included — is fixed-base. @@ -189,10 +193,6 @@ impl CRS { .collect(); Committer::new(&bases, window_size) } - - pub fn commit_lagrange_poly(&self, polynomial: &LagrangeBasis) -> Element { - slow_vartime_multiscalar_mul(polynomial.values().iter(), self.G.iter()) - } } impl core::ops::Index for CRS { diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index 0a53aed8..d1fd6036 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -94,6 +94,11 @@ impl MultiPoint { transcript: &mut Transcript, queries: Vec, ) -> MultiPointProof { + assert_eq!( + committer.num_bases(), + crs.n + 1, + "the committer must hold the CRS `G` vector followed by `Q`" + ); Self::open_inner(crs, Some(committer), precomp, transcript, queries) } @@ -131,6 +136,11 @@ impl MultiPoint { .map(|(point, queries_challenges)| { let mut aggregated = vec![Fr::zero(); crs.n]; for (query, challenge) in queries_challenges.iter() { + assert_eq!( + query.poly.values().len(), + crs.n, + "a query polynomial must have one coefficient per CRS generator" + ); for (acc, coeff) in aggregated.iter_mut().zip(query.poly.values()) { *acc += **challenge * coeff; } diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 433d6367..56b16f89 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -50,6 +50,13 @@ type FxHashMap = HashMap; /// Consecutive blocks share most of their authentication paths, which is what makes the hit /// rate worth the memory: at most `SHARDS * MAX_PER_SHARD` polynomials of 8 KiB each, 256 MiB. /// A shard that fills up is cleared whole; the working set refills within a few proofs. +/// +/// The commitment key only holds if the store is a consistent snapshot: the commitment and the +/// children or slots of a node must come from the same state. Callers must keep the store +/// unchanged while a proof is created; a polynomial rebuilt from a changed store would be cached +/// under a commitment it does not open to, and every later proof that reads that commitment +/// would reuse it. The rebuilt polynomial is deliberately not checked against the commitment, +/// which would cost an MSM per miss. mod node_poly_cache { use super::*; use crate::Lazy; From 7c709ef2a18c4c2a10c0d34eb35705e428001ff5 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Thu, 15 Oct 2026 23:28:49 +0900 Subject: [PATCH 15/18] fix(proof): cache a rebuilt node polynomial only if it opens to its commitment The node-polynomial cache is process-wide and keyed by (node id, commitment), so a polynomial rebuilt from a store whose trie and state disagree and filed under the commitment it was read at poisoned every later proof that read that commitment, including proofs from consistent stores. On a miss, check the rebuilt polynomial with one fixed-base MSM against the commitment and insert it only if they match; a mismatching polynomial is still used for the current proof. Hits stay free. Add a regression test: a witness from a store with block 2's state but block 1's trie is created first, then a witness from a consistent block-1 store must still verify. Co-Authored-By: Claude Sonnet 5.5 --- salt/src/proof/subtrie.rs | 123 +++++++++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 21 deletions(-) diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 56b16f89..563c9105 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -25,7 +25,10 @@ use crate::{ ProofError, ProofResult, SerdeCommitment, }, traits::{StateReader, TrieReader}, - trie::node_utils::{get_child_node, subtree_leaf_start_key, subtree_root_level}, + trie::{ + node_utils::{get_child_node, subtree_leaf_start_key, subtree_root_level}, + trie::shared_committer, + }, types::{BucketId, BucketMeta, CommitmentBytes, NodeId, SaltKey}, SlotId, }; @@ -51,12 +54,13 @@ type FxHashMap = HashMap; /// rate worth the memory: at most `SHARDS * MAX_PER_SHARD` polynomials of 8 KiB each, 256 MiB. /// A shard that fills up is cleared whole; the working set refills within a few proofs. /// -/// The commitment key only holds if the store is a consistent snapshot: the commitment and the -/// children or slots of a node must come from the same state. Callers must keep the store -/// unchanged while a proof is created; a polynomial rebuilt from a changed store would be cached -/// under a commitment it does not open to, and every later proof that reads that commitment -/// would reuse it. The rebuilt polynomial is deliberately not checked against the commitment, -/// which would cost an MSM per miss. +/// The commitment key only holds if the polynomial really opens to that commitment, which a +/// store that is not a consistent snapshot (commitments and children or slots from different +/// states) does not guarantee. Callers should still keep the store unchanged while a proof is +/// created, since such a proof will not verify; but an inconsistent store must not poison the +/// cache for consistent ones, so a rebuilt polynomial is cached only after one fixed-base MSM +/// shows it opens to the commitment it was read at. One that does not is used for this proof +/// and dropped. mod node_poly_cache { use super::*; use crate::Lazy; @@ -375,7 +379,8 @@ where /// The polynomial of every parent in `parents`, in order: query order is node order, which /// the transcript hashes, so a cache hit must not reorder the proof. A parent whose polynomial /// the cache holds for the commitment it was read at skips `rebuild`; the misses are rebuilt -/// in one call (by logical id, one polynomial per miss) and cached under that commitment. +/// in one call (by logical id, one polynomial per miss) and cached under that commitment, but +/// only if the polynomial opens to it. fn resolve_polys( parents: &[ParentNode], rebuild: impl FnOnce(&[NodeId]) -> ProofResult>, @@ -384,26 +389,43 @@ fn resolve_polys( .iter() .map(|parent| node_poly_cache::CACHE.get(parent.physical, &parent.bytes)) .collect(); - let missing: Vec = parents + let misses: Vec<&ParentNode> = parents .iter() .zip(&hits) .filter(|(_, hit)| hit.is_none()) - .map(|(parent, _)| parent.logical) + .map(|(parent, _)| parent) .collect(); - let mut rebuilt = rebuild(&missing)?.into_iter(); - Ok(parents - .iter() - .zip(hits) - .map(|(parent, hit)| { - hit.unwrap_or_else(|| { - let poly = Arc::new(rebuilt.next().expect("one rebuilt polynomial per miss")); - node_poly_cache::CACHE.insert(parent.physical, parent.bytes, Arc::clone(&poly)); - poly - }) - }) + let missing: Vec = misses.iter().map(|parent| parent.logical).collect(); + let rebuilt: Vec> = rebuild(&missing)?.into_iter().map(Arc::new).collect(); + + // A polynomial rebuilt from a store that does not match the commitment must not be cached + // under it: the cache is process-wide, so every later proof reading that commitment would + // reuse it. + let consistent: Vec = iter!(misses) + .zip(iter!(rebuilt)) + .map(|(parent, poly)| opens_to(poly, &parent.commitment)) + .collect(); + for ((parent, poly), consistent) in misses.iter().zip(&rebuilt).zip(consistent) { + if consistent { + node_poly_cache::CACHE.insert(parent.physical, parent.bytes, Arc::clone(poly)); + } + } + + let mut rebuilt = rebuilt.into_iter(); + Ok(hits + .into_iter() + .map(|hit| hit.unwrap_or_else(|| rebuilt.next().expect("one rebuilt polynomial per miss"))) .collect()) } +/// Whether `poly` commits to `commitment` under the shared CRS. +fn opens_to(poly: &LagrangeBasis, commitment: &Element) -> bool { + let terms: Vec<(usize, Fr)> = (0..DOMAIN_SIZE) + .map(|i| (i, poly.evaluate_in_domain(i))) + .collect(); + shared_committer().msm(&terms) == *commitment +} + /// The 256-slot polynomial of a leaf node: every slot's value hashed to the field, with the /// bucket kind's default for empty slots. fn leaf_polynomial(store: &Store, parent: NodeId) -> ProofResult @@ -660,6 +682,65 @@ mod tests { assert!((0..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_none())); } + /// Regression test for PR #152 review comment r4080228453: a store whose trie and state + /// disagree must not poison the process-wide node-polynomial cache for consistent stores. + /// + /// The cache is keyed by (node id, commitment). On a miss, `resolve_polys` reads the + /// node's commitment and rebuilds its polynomial from the store's slots or children; it + /// files the result only if the polynomial opens to that commitment. + /// + /// Here `inconsistent` has block 2's state but block 1's trie (`update_state` without + /// `update_trie`), so the leaf holding `key` reports block 1's commitment while its slots + /// hold block 2's value. Without the check, creating a witness on it files block 2's leaf + /// polynomial under block 1's commitment, `consistent` (a correct store of block 1) is + /// then served that polynomial, and its witness no longer verifies. + /// + /// The `inconsistent` witness is created first on purpose: a cache entry for block 1's + /// commitment that `consistent` had already filed would hide the problem. The key and + /// values come from a seed no other test uses, so the only entries this test could poison + /// are ones no other test in the process reads. + #[test] + fn inconsistent_store_does_not_poison_witnesses_of_a_consistent_store() { + use crate::proof::salt_witness::SaltWitness; + + let mut rng = StdRng::seed_from_u64(0x5A17_0152); + let key = mock_data(&mut rng, 52); + let (block_1_value, block_2_value) = (mock_data(&mut rng, 32), mock_data(&mut rng, 32)); + let write = |value: &Vec| -> HashMap, Option>> { + [(key.clone(), Some(value.clone()))].into_iter().collect() + }; + + // Block 1, fully applied: state and trie agree. + let consistent = MemStore::new(); + let block_1 = EphemeralSaltState::new(&consistent) + .update_fin(&write(&block_1_value)) + .unwrap(); + consistent.update_state(block_1.clone()); + let (_, block_1_trie) = StateRoot::new(&consistent).update_fin(&block_1).unwrap(); + consistent.update_trie(block_1_trie); + let salt_key = *block_1.data.keys().next().unwrap(); + + // Block 2 on top of it, with only its state applied: the commitments are block 1's. + let inconsistent = consistent.clone(); + let block_2 = EphemeralSaltState::new(&inconsistent) + .update_fin(&write(&block_2_value)) + .unwrap(); + assert_eq!(*block_2.data.keys().next().unwrap(), salt_key); + inconsistent.update_state(block_2); + + // A witness from the inconsistent store cannot verify: its value is block 2's, its + // commitments are block 1's. That is the expected cost of the bad store itself. + let bad = SaltWitness::create(&[salt_key], &inconsistent).unwrap(); + assert!(bad.verify_proof().is_err()); + + // The consistent store must be unaffected by what the other store did. + let good = SaltWitness::create(&[salt_key], &consistent).unwrap(); + good.verify_proof().expect( + "a witness from a consistent store must verify, \ + whatever other stores the process has proven before", + ); + } + #[test] fn internal_polynomials_empty_and_single_node() { let store = MemStore::new(); From 44e30943926ffd3d5dbda152c36a63f7a614b4a9 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Thu, 12 Nov 2026 23:01:20 +0900 Subject: [PATCH 16/18] perf(ipa): commit a dense polynomial without a term list, and open without cloning the CRS `commit_dense` built a `Vec<(usize, Fr)>` of 256 terms for every commitment to feed `Committer::msm`, and the node-polynomial check in salt did the same. `Committer::msm_dense` takes the scalars as a slice, base `i` for scalar `i`, and both callers use it. `LagrangeBasis::values` becomes public so salt can pass a polynomial's evaluations without copying them. `MultiPoint::open` takes the CRS by value, but since `open_inner` borrowed it, the path without a committer cloned all 257 points for the IPA, which folds the generators in place. `open_inner` now takes a `Cow`, so `open` moves its CRS as it did before this branch. Co-Authored-By: Claude Fable 5.2 --- banderwagon/src/salt_committer.rs | 50 ++++++++++++++++++++++++++++ ipa-multipoint/src/lagrange_basis.rs | 3 +- ipa-multipoint/src/multiproof.rs | 30 ++++++++++------- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/banderwagon/src/salt_committer.rs b/banderwagon/src/salt_committer.rs index 18500f30..0409d519 100644 --- a/banderwagon/src/salt_committer.rs +++ b/banderwagon/src/salt_committer.rs @@ -353,6 +353,28 @@ impl Committer { }) .sum() } + + /// Sums `scalars[i] · G[i]`, splitting the scalars across threads: [`Committer::msm`] for + /// a dense vector over the first `scalars.len()` bases, without a list of terms to build. + /// + /// # Panics + /// + /// If there are more scalars than bases. + pub fn msm_dense(&self, scalars: &[Fr]) -> Element { + let chunk_size = thread_chunk_size!(scalars.len()); + chunks!(scalars, chunk_size) + .enumerate() + .map(|(chunk, scalars)| { + let mut acc = Element::zero(); + for (offset, scalar) in scalars.iter().enumerate() { + if !scalar.is_zero() { + acc += self.mul_index(scalar, chunk * chunk_size + offset); + } + } + acc + }) + .sum() + } } /// Adds an affine point to a projective point using extended Edwards coordinates. @@ -503,6 +525,34 @@ mod tests { use std::str::FromStr; use std::string::ToString; + /// `msm_dense` is `msm` over the scalars' own positions: for a full vector, a shorter + /// one, a single scalar and none, it gives what `msm` gives for the same terms and what + /// the reference MSM gives, zero scalars included. + #[test] + fn msm_dense_matches_msm_and_the_reference() { + let crs: Vec = (0..256) + .map(|i| Element::prime_subgroup_generator() * Fr::from((i + 1) as u64)) + .collect(); + let committer = Committer::new(&crs, platform::DEFAULT_PRECOMP_WINDOW_SIZE); + + let mut rng = ChaCha20Rng::seed_from_u64(7); + let mut scalars: Vec = (0..256).map(|_| Fr::rand(&mut rng)).collect(); + scalars[3] = Fr::zero(); + scalars[255] = Fr::zero(); + + for len in [256, 100, 1, 0] { + let scalars = &scalars[..len]; + let terms: Vec<(usize, Fr)> = scalars.iter().copied().enumerate().collect(); + let dense = committer.msm_dense(scalars); + assert_eq!(dense, committer.msm(&terms), "{len} scalars"); + assert_eq!( + dense, + multi_scalar_mul(&crs[..len], scalars), + "{len} scalars" + ); + } + } + /// Tests the correctness of precomputed MSM against the reference implementation. /// /// This comprehensive test verifies that the `Committer`'s optimized scalar multiplication diff --git a/ipa-multipoint/src/lagrange_basis.rs b/ipa-multipoint/src/lagrange_basis.rs index 6ace3c7f..6edd4a6d 100644 --- a/ipa-multipoint/src/lagrange_basis.rs +++ b/ipa-multipoint/src/lagrange_basis.rs @@ -236,7 +236,8 @@ impl LagrangeBasis { self.values[index] } - pub(crate) fn values(&self) -> &[Fr] { + /// The polynomial's evaluations over the domain, in order. + pub fn values(&self) -> &[Fr] { &self.values } diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index d1fd6036..51f59d65 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -16,7 +16,7 @@ type FxHashMap = HashMap; use salt_macros::prelude::*; use salt_macros::{chunks, chunks_mut, into_iter, iter, reduce, thread_chunk_size}; -use std::{sync::Arc, vec, vec::Vec}; +use std::{borrow::Cow, sync::Arc, vec, vec::Vec}; pub struct MultiPoint; @@ -24,7 +24,8 @@ pub struct MultiPoint; pub struct ProverQuery { pub commitment: Element, /// Shared so that opening one polynomial at several points does not clone - /// its 256 coefficients per point. + /// its 256 coefficients per point. A caller that owns a `LagrangeBasis` + /// passes `poly.into()`. pub poly: Arc, // Given a function f, we use z_i to denote the input point and y_i to denote the output, ie f(z_i) = y_i pub point: usize, @@ -76,7 +77,7 @@ impl MultiPoint { transcript: &mut Transcript, queries: Vec, ) -> MultiPointProof { - Self::open_inner(&crs, None, precomp, transcript, queries) + Self::open_inner(Cow::Owned(crs), None, precomp, transcript, queries) } /// Same proof as [`MultiPoint::open`] (byte-identical output), but every @@ -99,11 +100,19 @@ impl MultiPoint { crs.n + 1, "the committer must hold the CRS `G` vector followed by `Q`" ); - Self::open_inner(crs, Some(committer), precomp, transcript, queries) + Self::open_inner( + Cow::Borrowed(crs), + Some(committer), + precomp, + transcript, + queries, + ) } + /// `crs` is owned on the path without a committer, whose IPA folds the generators in + /// place and so takes the CRS by value; borrowed, that path would have to clone it. fn open_inner( - crs: &CRS, + crs: Cow<'_, CRS>, committer: Option<&Committer>, precomp: &PrecomputedWeights, transcript: &mut Transcript, @@ -161,7 +170,7 @@ impl MultiPoint { |a, b| a + b ); - let g_x_comm = commit_dense(crs, committer, &g_x); + let g_x_comm = commit_dense(&crs, committer, &g_x); transcript.append_point(b"D", &g_x_comm); @@ -192,7 +201,7 @@ impl MultiPoint { |a, b| a + b ); - let g1_comm = commit_dense(crs, committer, &g1_x); + let g1_comm = commit_dense(&crs, committer, &g1_x); transcript.append_point(b"E", &g1_comm); @@ -208,7 +217,7 @@ impl MultiPoint { Some(committer) => { crate::ipa::create_with_precomp(transcript, committer, a, g_3_x_comm, b, t) } - None => crate::ipa::create(transcript, crs.clone(), a, g_3_x_comm, b, t), + None => crate::ipa::create(transcript, crs.into_owned(), a, g_3_x_comm, b, t), }; MultiPointProof { @@ -222,10 +231,7 @@ impl MultiPoint { /// when available, otherwise via the generic variable-base MSM. fn commit_dense(crs: &CRS, committer: Option<&Committer>, poly: &LagrangeBasis) -> Element { match committer { - Some(committer) => { - let terms: Vec<(usize, Fr)> = poly.values().iter().copied().enumerate().collect(); - committer.msm(&terms) - } + Some(committer) => committer.msm_dense(poly.values()), None => crs.commit_lagrange_poly(poly), } } From 3bb70866b3e4863196af05f17dd5d56e62f33e9c Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Thu, 12 Nov 2026 23:01:21 +0900 Subject: [PATCH 17/18] perf(proof): check rebuilt node polynomials in one batch and rework the cache around it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Checking each rebuilt polynomial with its own 256-term fixed-base MSM (7c709ef) cost about 0.85 ms of CPU per cache miss, which made a cold proof ten times slower: 320 keys over about 550 nodes took 57 ms instead of 5 ms on 14 cores, and 485 ms instead of 18 ms on one thread. A call's rebuilt polynomials are now checked together: with one 128-bit challenge per node, `commit(Σ r_i·poly_i)` is compared with `Σ r_i·C_i`. The challenges come from a blake3 hash of every commitment and polynomial in the batch, so the check needs no entropy source and works under `no_std`. Only a batch that fails is checked node by node, which caches exactly what the per-node check cached. The same proof now takes 7.7 ms cold (29.5 ms on one thread); the warm path is unchanged. The cache itself, following the review of this branch: - A full shard evicted everything it held, so the leaves one proof touched took the paths every proof walks with them. It now evicts one polynomial per new node and passes over those used since it last considered them. - `NodePolyCache` is public: `global()`, `capacity()`, `set_capacity()` and `clear()`. A capacity of zero turns the cache off, and proof creation then skips the check as well. - The shards use a lock whose waiters park under `std`, as `Lazy` already does; `no_std` keeps `spin`. - `resolve_polys` takes the cache as a parameter, so tests check on a cache of their own that a consistent rebuild is cached and served, and that an inconsistent one is not. Co-Authored-By: Claude Fable 5.2 --- salt/src/lib.rs | 32 +- salt/src/proof/mod.rs | 1 + salt/src/proof/subtrie.rs | 624 +++++++++++++++++++++++++++++++++----- 3 files changed, 580 insertions(+), 77 deletions(-) diff --git a/salt/src/lib.rs b/salt/src/lib.rs index 0ef061f6..312b3fb0 100644 --- a/salt/src/lib.rs +++ b/salt/src/lib.rs @@ -15,10 +15,40 @@ pub(crate) type Lazy = std::sync::LazyLock; #[cfg(not(feature = "std"))] pub(crate) type Lazy = spin::Lazy; +/// Reader-writer lock for process-wide state, under the same policy as [`Lazy`]: waiters +/// park under `std`, and spin only on `no_std` targets, which have nothing to park on. +/// +/// The `std` lock's poisoning is ignored: what these locks guard is written one whole +/// entry at a time, so the data a panicking holder leaves behind is still valid. +#[cfg(feature = "std")] +pub(crate) struct RwLock(std::sync::RwLock); + +#[cfg(feature = "std")] +impl RwLock { + pub(crate) fn new(value: T) -> Self { + Self(std::sync::RwLock::new(value)) + } + + pub(crate) fn read(&self) -> std::sync::RwLockReadGuard<'_, T> { + self.0 + .read() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } + + pub(crate) fn write(&self) -> std::sync::RwLockWriteGuard<'_, T> { + self.0 + .write() + .unwrap_or_else(std::sync::PoisonError::into_inner) + } +} + +#[cfg(not(feature = "std"))] +pub(crate) type RwLock = spin::RwLock; + pub mod constant; pub mod empty_salt; pub mod proof; -pub use proof::{fx_hashmap_serde, ProofError, SaltProof, SaltWitness, Witness}; +pub use proof::{fx_hashmap_serde, NodePolyCache, ProofError, SaltProof, SaltWitness, Witness}; pub mod state; pub use state::{ hasher, state::EphemeralSaltState, state::PlainStateProvider, updates::StateUpdates, diff --git a/salt/src/proof/mod.rs b/salt/src/proof/mod.rs index bff27f0c..6e143f1b 100644 --- a/salt/src/proof/mod.rs +++ b/salt/src/proof/mod.rs @@ -78,6 +78,7 @@ mod test_utils; pub use prover::{fx_hashmap_serde, SaltProof, SerdeCommitment, SerdeMultiPointProof}; pub use salt_witness::SaltWitness; +pub use subtrie::NodePolyCache; pub use witness::Witness; /// Error type for proof operations diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index 447eba0d..a150e1bb 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -32,11 +32,11 @@ use crate::{ types::{BucketId, BucketMeta, CommitmentBytes, NodeId, SaltKey}, SlotId, }; -use banderwagon::{Element, Fr}; +use banderwagon::{multi_scalar_mul, Element, Fr, PrimeField, Zero}; use ipa_multipoint::{lagrange_basis::LagrangeBasis, multiproof::ProverQuery}; use salt_macros::prelude::*; -use salt_macros::{chunks, iter, thread_chunk_size}; +use salt_macros::{chunks, iter, reduce, thread_chunk_size}; use std::collections::{BTreeMap, BTreeSet}; use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; @@ -51,42 +51,125 @@ type FxHashMap = HashMap; /// polynomial was last built has the same 256 child scalars (internal node) or slot scalars /// (leaf node), and the store reads plus scalar conversions that produce them can be skipped. /// Consecutive blocks share most of their authentication paths, which is what makes the hit -/// rate worth the memory: at most `SHARDS * MAX_PER_SHARD` polynomials of 8 KiB each, 256 MiB. -/// A shard that fills up is cleared whole; the working set refills within a few proofs. +/// rate worth the memory: by default at most 32768 polynomials of 8 KiB each, 256 MiB +/// ([`NodePolyCache::set_capacity`]). A full shard evicts one polynomial per new node and +/// passes over those used since it last considered them, so the paths that proof after +/// proof walks outlive the leaves a single proof touched. /// /// The commitment key only holds if the polynomial really opens to that commitment, which a /// store that is not a consistent snapshot (commitments and children or slots from different /// states) does not guarantee. Callers should still keep the store unchanged while a proof is /// created, since such a proof will not verify; but an inconsistent store must not poison the -/// cache for consistent ones, so a rebuilt polynomial is cached only after one fixed-base MSM -/// shows it opens to the commitment it was read at. One that does not is used for this proof -/// and dropped. +/// cache for consistent ones, so a rebuilt polynomial is cached only once it is shown to open +/// to the commitment it was read at: one check for all the polynomials a call rebuilds +/// ([`batch_opens`]), and one per polynomial only when that fails. One that does not open is +/// used for this proof and dropped. mod node_poly_cache { use super::*; - use crate::Lazy; - use spin::RwLock; + use crate::{Lazy, RwLock}; + use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; pub(super) const SHARDS: usize = 64; - pub(super) const MAX_PER_SHARD: usize = 512; - type Shard = RwLock)>>; + /// One cached polynomial, with the node it belongs to and the commitment it was built + /// for. + struct Slot { + node: NodeId, + commitment: CommitmentBytes, + poly: Arc, + /// Set when the polynomial is served or the node is cached again, cleared when the + /// eviction hand passes: a polynomial is evicted only when the hand comes round and + /// finds it unused since. + used: AtomicBool, + } + + /// One shard: its polynomials, which slot holds which node, and the slot the next + /// eviction considers first (taken modulo the number of slots). + #[derive(Default)] + struct Shard { + slots: Vec, + index: FxHashMap, + hand: usize, + } - /// The shards of one cache: [`CACHE`] is the process-wide instance, and a test holds its own. - pub(super) struct NodePolyCache(Vec); + /// The cache of node polynomials that proof and witness creation share across calls: see + /// [`NodePolyCache::global`] for the one instance a process has. + /// + /// A cached polynomial saves the store reads and scalar conversions that rebuild it, and + /// costs 8 KiB while it is kept. The default bound suits a process that proves block + /// after block; one that creates a single proof, or cannot spare the memory, can lower it + /// or turn the cache off with [`set_capacity`](Self::set_capacity). + pub struct NodePolyCache { + shards: Vec>, + max_per_shard: AtomicUsize, + } - pub(super) static CACHE: Lazy = Lazy::new(NodePolyCache::new); + static CACHE: Lazy = + Lazy::new(|| NodePolyCache::with_capacity(NodePolyCache::DEFAULT_CAPACITY)); impl NodePolyCache { - pub(super) fn new() -> Self { - Self( - (0..SHARDS) - .map(|_| RwLock::new(FxHashMap::default())) - .collect(), - ) + /// The bound a cache starts with: 32768 polynomials, 256 MiB. + pub const DEFAULT_CAPACITY: usize = SHARDS * 512; + + /// The cache every proof and witness creation in this process reads and fills. + pub fn global() -> &'static Self { + &CACHE + } + + /// The most polynomials the cache holds, at 8 KiB each. + pub fn capacity(&self) -> usize { + self.max_per_shard.load(Ordering::Relaxed) * SHARDS + } + + /// Bounds the cache to `polynomials` entries, rounded up to a multiple of its 64 + /// shards. Zero turns it off: nothing is cached, and proof creation skips the check + /// that caching a polynomial needs. A shard that holds more than its new share is + /// emptied. + pub fn set_capacity(&self, polynomials: usize) { + let max = polynomials.div_ceil(SHARDS); + // Stored before the shards are trimmed: `insert` reads it under the shard lock, + // so an insert that follows a shard's trim sees the new bound. + self.max_per_shard.store(max, Ordering::Relaxed); + for shard in &self.shards { + let mut guard = shard.write(); + let trimmed = (guard.slots.len() > max).then(|| core::mem::take(&mut *guard)); + drop(guard); + drop(trimmed); + } + } + + /// Drops every cached polynomial, each shard's after its lock is released. + pub fn clear(&self) { + for shard in &self.shards { + let mut guard = shard.write(); + let dropped = core::mem::take(&mut *guard); + drop(guard); + drop(dropped); + } } - fn shard(&self, node: NodeId) -> &Shard { - &self.0[(node % SHARDS as NodeId) as usize] + pub(super) fn with_capacity(polynomials: usize) -> Self { + Self { + shards: (0..SHARDS).map(|_| RwLock::new(Shard::default())).collect(), + max_per_shard: AtomicUsize::new(polynomials.div_ceil(SHARDS)), + } + } + + fn shard(&self, node: NodeId) -> &RwLock { + &self.shards[(node % SHARDS as NodeId) as usize] + } + + /// The nodes the cache holds, in ascending order. Unlike `get`, looking does not + /// count as a use. + #[cfg(test)] + pub(super) fn nodes(&self) -> Vec { + let mut nodes: Vec = self + .shards + .iter() + .flat_map(|shard| shard.read().index.keys().copied().collect::>()) + .collect(); + nodes.sort_unstable(); + nodes } /// The cached polynomial of `node`, if it was built for exactly this commitment. @@ -95,11 +178,17 @@ mod node_poly_cache { node: NodeId, commitment: &CommitmentBytes, ) -> Option> { - let guard = self.shard(node).read(); - guard - .get(&node) - .filter(|(seen, _)| seen == commitment) - .map(|(_, poly)| Arc::clone(poly)) + let shard = self.shard(node).read(); + let slot = &shard.slots[*shard.index.get(&node)?]; + if slot.commitment != *commitment { + return None; + } + // Written only when it changes, so the readers of a polynomial every proof uses + // do not pass its cache line back and forth. + if !slot.used.load(Ordering::Relaxed) { + slot.used.store(true, Ordering::Relaxed); + } + Some(Arc::clone(&slot.poly)) } pub(super) fn insert( @@ -109,20 +198,47 @@ mod node_poly_cache { poly: Arc, ) { let mut guard = self.shard(node).write(); - // Only a new node grows the shard. Whatever the insert evicts or replaces is dropped - // after the lock is released, so readers do not wait on the frees. - let evicted = (guard.len() >= MAX_PER_SHARD && !guard.contains_key(&node)).then(|| { - core::mem::replace( - &mut *guard, - FxHashMap::with_capacity_and_hasher(MAX_PER_SHARD, FxBuildHasher), - ) - }); - let replaced = guard.insert(node, (commitment, poly)); + let max = self.max_per_shard.load(Ordering::Relaxed); + if max == 0 { + return; + } + let shard = &mut *guard; + let slot = |used| Slot { + node, + commitment, + poly, + used: AtomicBool::new(used), + }; + // Whatever the insert replaces or evicts is dropped after the lock is released, + // so readers do not wait on the free. + let dropped = if let Some(&at) = shard.index.get(&node) { + // A node cached again was resolved again, which is a use. + Some(core::mem::replace(&mut shard.slots[at], slot(true))) + } else if shard.slots.len() < max { + shard.index.insert(node, shard.slots.len()); + shard.slots.push(slot(false)); + None + } else { + // The hand passes over the polynomials used since its last visit, clearing + // their mark, and takes the first one it finds unused: the one it started + // at, after a full turn, if every mark was set. + let len = shard.slots.len(); + let at = (0..=len) + .map(|step| (shard.hand + step) % len) + .find(|&at| !core::mem::take(shard.slots[at].used.get_mut())) + .expect("a full turn clears every mark"); + shard.hand = at + 1; + let evicted = core::mem::replace(&mut shard.slots[at], slot(false)); + shard.index.remove(&evicted.node); + shard.index.insert(node, at); + Some(evicted) + }; drop(guard); - drop((evicted, replaced)); + drop(dropped); } } } +pub use node_poly_cache::NodePolyCache; // Constants for improved code readability pub(crate) const METADATA_BUCKET_LEVEL: u8 = 1; @@ -355,14 +471,15 @@ where // Step 5: Resolve the polynomial of every parent, internal nodes in parallel chunks and // leaves one per task. let (internal_parents, leaf_parents) = parents.split_at(internal_nodes.len()); - let internal_polys = resolve_polys(internal_parents, |missing| { + let cache = NodePolyCache::global(); + let internal_polys = resolve_polys(cache, internal_parents, |missing| { let chunk_size = thread_chunk_size!(missing.len()); let chunks = chunks!(missing, chunk_size) .map(|nodes| internal_polynomials(store, nodes)) .collect::>>()?; Ok(chunks.into_iter().flatten().collect()) })?; - let leaf_polys = resolve_polys(leaf_parents, |missing| { + let leaf_polys = resolve_polys(cache, leaf_parents, |missing| { iter!(missing) .map(|&node| leaf_polynomial(store, node)) .collect() @@ -381,16 +498,17 @@ where /// The polynomial of every parent in `parents`, in order: query order is node order, which /// the transcript hashes, so a cache hit must not reorder the proof. A parent whose polynomial -/// the cache holds for the commitment it was read at skips `rebuild`; the misses are rebuilt +/// `cache` holds for the commitment it was read at skips `rebuild`; the misses are rebuilt /// in one call (by logical id, one polynomial per miss) and cached under that commitment, but /// only if the polynomial opens to it. fn resolve_polys( + cache: &NodePolyCache, parents: &[ParentNode], rebuild: impl FnOnce(&[NodeId]) -> ProofResult>, ) -> ProofResult>> { let hits: Vec>> = parents .iter() - .map(|parent| node_poly_cache::CACHE.get(parent.physical, &parent.bytes)) + .map(|parent| cache.get(parent.physical, &parent.bytes)) .collect(); let misses: Vec<&ParentNode> = parents .iter() @@ -403,14 +521,22 @@ fn resolve_polys( // A polynomial rebuilt from a store that does not match the commitment must not be cached // under it: the cache is process-wide, so every later proof reading that commitment would - // reuse it. - let consistent: Vec = iter!(misses) - .zip(iter!(rebuilt)) - .map(|(parent, poly)| opens_to(poly, &parent.commitment)) - .collect(); - for ((parent, poly), consistent) in misses.iter().zip(&rebuilt).zip(consistent) { - if consistent { - node_poly_cache::CACHE.insert(parent.physical, parent.bytes, Arc::clone(poly)); + // reuse it. A consistent store passes the one check for the whole call; only a call that + // fails it is gone through node by node, to cache the polynomials that do open. A cache + // that is turned off takes nothing, so there is nothing to check. + if cache.capacity() != 0 { + let consistent: Vec = if batch_opens(&misses, &rebuilt) { + vec![true; misses.len()] + } else { + iter!(misses) + .zip(iter!(rebuilt)) + .map(|(parent, poly)| opens_to(poly, &parent.commitment)) + .collect() + }; + for ((parent, poly), consistent) in misses.iter().zip(&rebuilt).zip(consistent) { + if consistent { + cache.insert(parent.physical, parent.bytes, Arc::clone(poly)); + } } } @@ -421,12 +547,85 @@ fn resolve_polys( .collect()) } +/// Whether every polynomial in `rebuilt` opens to the commitment its node was read at, +/// decided for all of them at once. +/// +/// Checking a node on its own costs a 256-term fixed-base MSM ([`opens_to`]), and one per +/// miss is most of the work of a cold proof. Here every node gets a challenge `r_i`, and +/// `Σ r_i·poly_i` is committed once and compared with `Σ r_i·C_i`. The two are equal when +/// every polynomial opens to its commitment. When one does not, they are equal only if the +/// challenges cancel the differences, and the challenges are 128 bits drawn from a hash of +/// the very commitments and polynomials being checked ([`batch_challenges`]), so that happens +/// with probability about 2^-128. +fn batch_opens(misses: &[&ParentNode], rebuilt: &[Arc]) -> bool { + let challenges = batch_challenges(misses, rebuilt); + + // Σ r_i·poly_i, one partial sum per thread. + let chunk_size = thread_chunk_size!(rebuilt.len()); + let combined = reduce!( + chunks!(rebuilt, chunk_size) + .zip(chunks!(challenges, chunk_size)) + .map(|(polys, challenges)| { + let mut sum = [Fr::zero(); DOMAIN_SIZE]; + for (poly, challenge) in polys.iter().zip(challenges) { + for (term, value) in sum.iter_mut().zip(poly.values()) { + *term += *value * challenge; + } + } + sum + }), + || [Fr::zero(); DOMAIN_SIZE], + |mut sum, part| { + for (term, part) in sum.iter_mut().zip(part) { + *term += part; + } + sum + } + ); + + let commitments: Vec = misses.iter().map(|parent| parent.commitment).collect(); + shared_committer().msm_dense(&combined) == multi_scalar_mul(&commitments, &challenges) +} + +/// One 128-bit challenge per miss, drawn from a hash of the whole batch: each node's +/// commitment bytes and the 256 evaluations of its polynomial, in node order. +fn batch_challenges(misses: &[&ParentNode], rebuilt: &[Arc]) -> Vec { + const SCALAR_BYTES: usize = 32; + + // Each node is hashed on its own, so the polynomials (8 KiB apiece) are absorbed in + // parallel and the batch hash below only reads a digest per node. + let digests: Vec<[u8; blake3::OUT_LEN]> = iter!(misses) + .zip(iter!(rebuilt)) + .map(|(parent, poly)| { + let mut evaluations = [0u8; DOMAIN_SIZE * SCALAR_BYTES]; + let limbs = poly.values().iter().flat_map(|value| value.into_bigint().0); + for (bytes, limb) in evaluations.chunks_exact_mut(8).zip(limbs) { + bytes.copy_from_slice(&limb.to_le_bytes()); + } + let mut hasher = blake3::Hasher::new(); + hasher.update(&parent.bytes).update(&evaluations); + *hasher.finalize().as_bytes() + }) + .collect(); + + let mut hasher = blake3::Hasher::new(); + for digest in &digests { + hasher.update(digest); + } + let mut output = hasher.finalize_xof(); + digests + .iter() + .map(|_| { + let mut challenge = [0u8; 16]; + output.fill(&mut challenge); + Fr::from(u128::from_le_bytes(challenge)) + }) + .collect() +} + /// Whether `poly` commits to `commitment` under the shared CRS. fn opens_to(poly: &LagrangeBasis, commitment: &Element) -> bool { - let terms: Vec<(usize, Fr)> = (0..DOMAIN_SIZE) - .map(|i| (i, poly.evaluate_in_domain(i))) - .collect(); - shared_committer().msm(&terms) == *commitment + shared_committer().msm_dense(poly.values()) == *commitment } /// The 256-slot polynomial of a leaf node: every slot's value hashed to the field, with the @@ -487,8 +686,10 @@ mod tests { use crate::constant::{META_BUCKET_SIZE, ROOT_NODE_ID}; use crate::proof::test_utils::*; use crate::{ - mem_store::MemStore, proof::prover::PRECOMPUTED_WEIGHTS, state::state::EphemeralSaltState, - trie::trie::StateRoot, + mem_store::MemStore, + proof::prover::PRECOMPUTED_WEIGHTS, + state::state::EphemeralSaltState, + trie::trie::{StateRoot, DEFAULT_CRS}, }; use ark_ff::BigInt; use banderwagon::{Fr, Zero}; @@ -657,32 +858,122 @@ mod tests { assert!(queries.iter().all(|q| q.result == empty_default)); } - /// A full shard is cleared only by a node it does not hold: re-inserting a node it holds - /// replaces that entry and keeps the rest, and a lookup at another commitment misses. + /// A cache bounded to four polynomials per shard, with the pieces the cache tests share: + /// node `i` of one shard, a commitment made of one byte, and a polynomial to store. + fn small_cache() -> ( + NodePolyCache, + impl Fn(usize) -> NodeId, + impl Fn(u8) -> CommitmentBytes, + Arc, + ) { + use node_poly_cache::SHARDS; + ( + NodePolyCache::with_capacity(4 * SHARDS), + // Nodes `7 + i * SHARDS` share one shard. + |i| (7 + i * SHARDS) as NodeId, + |byte| [byte; 64], + Arc::new(LagrangeBasis::new(vec![Fr::zero(); DOMAIN_SIZE])), + ) + } + + /// A full shard evicts one polynomial per new node: the first the hand finds unused since + /// its last visit. A polynomial served, or a node cached again, counts as used; a lookup + /// at another commitment does not, and neither is a use that the hand has already passed. #[test] - fn node_poly_cache_clears_a_full_shard_only_for_a_new_node() { - use node_poly_cache::{NodePolyCache, MAX_PER_SHARD, SHARDS}; + fn node_poly_cache_evicts_what_was_not_used_since_the_hand_passed() { + let (cache, node, commitment, poly) = small_cache(); + let nodes = |held: &[usize]| held.iter().map(|&i| node(i)).collect::>(); + let insert = |i: usize, byte: u8| cache.insert(node(i), commitment(byte), poly.clone()); + let hit = |i: usize, byte: u8| cache.get(node(i), &commitment(byte)).is_some(); + + // A node of another shard, which nothing below disturbs. + cache.insert(8, commitment(1), poly.clone()); + let with_other_shard = |held: &[usize]| { + let mut all = nodes(held); + all.push(8); + all.sort_unstable(); + all + }; - let cache = NodePolyCache::new(); - let poly = Arc::new(LagrangeBasis::new(vec![Fr::zero(); DOMAIN_SIZE])); - let commitment = |byte: u8| -> CommitmentBytes { [byte; 64] }; - // Nodes `7 + i * SHARDS` share one shard. - let node = |i: usize| (7 + i * SHARDS) as NodeId; + (0..4).for_each(|i| insert(i, 1)); + assert_eq!(cache.nodes(), with_other_shard(&[0, 1, 2, 3])); + assert!(hit(0, 1) && hit(2, 1)); + assert!(!hit(1, 2), "another commitment is a miss"); + assert!(!hit(9, 1), "a node the cache does not hold is a miss"); + + // The hand passes node 0 (used) and evicts node 1, then passes node 2 and evicts 3. + insert(4, 1); + assert_eq!(cache.nodes(), with_other_shard(&[0, 2, 3, 4])); + insert(5, 1); + assert_eq!(cache.nodes(), with_other_shard(&[0, 2, 4, 5])); + // Back at node 0, whose use it has already passed over once. + insert(6, 1); + assert_eq!(cache.nodes(), with_other_shard(&[2, 4, 5, 6])); + + // Caching node 4 again replaces its polynomial in place and counts as a use, so the + // hand passes it and evicts node 2. + insert(4, 2); + assert_eq!(cache.nodes(), with_other_shard(&[2, 4, 5, 6])); + insert(7, 1); + assert_eq!(cache.nodes(), with_other_shard(&[4, 5, 6, 7])); + assert!(hit(4, 2) && !hit(4, 1)); + + // With every polynomial used, the hand makes a full turn and evicts where it began. + assert!(hit(5, 1) && hit(6, 1) && hit(7, 1)); + insert(9, 1); + assert_eq!(cache.nodes(), with_other_shard(&[4, 6, 7, 9])); + } - for i in 0..MAX_PER_SHARD { - cache.insert(node(i), commitment(1), Arc::clone(&poly)); - } - assert!((0..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_some())); - assert!(cache.get(node(0), &commitment(2)).is_none()); + /// The capacity is a bound per shard, rounded up. Lowering it empties the shards that hold + /// more than their new share and leaves the rest; zero turns the cache off, and raising + /// it again turns it back on. Clearing drops everything and changes no bound. + #[test] + fn node_poly_cache_capacity_bounds_trims_and_turns_the_cache_off() { + use node_poly_cache::SHARDS; + + let (cache, node, commitment, poly) = small_cache(); + assert_eq!(cache.capacity(), 4 * SHARDS); + assert_eq!(NodePolyCache::DEFAULT_CAPACITY, 32768); + assert_eq!( + NodePolyCache::global().capacity(), + NodePolyCache::DEFAULT_CAPACITY + ); - cache.insert(node(0), commitment(2), Arc::clone(&poly)); - assert!(cache.get(node(0), &commitment(2)).is_some()); - assert!(cache.get(node(0), &commitment(1)).is_none()); - assert!((1..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_some())); + // Three nodes in one shard, one in another. + (0..3).for_each(|i| cache.insert(node(i), commitment(1), poly.clone())); + cache.insert(8, commitment(1), poly.clone()); + let everything = vec![node(0), 8, node(1), node(2)]; + assert_eq!(cache.nodes(), everything); + + cache.set_capacity(2 * SHARDS + 1); + assert_eq!(cache.capacity(), 3 * SHARDS); + assert_eq!(cache.nodes(), everything, "no shard is over three"); + cache.set_capacity(2 * SHARDS); + assert_eq!(cache.nodes(), [8], "the shard holding three is emptied"); + + cache.set_capacity(0); + assert_eq!(cache.capacity(), 0); + assert!(cache.nodes().is_empty()); + cache.insert(node(0), commitment(1), poly.clone()); + assert!( + cache.nodes().is_empty(), + "a cache that is off takes nothing" + ); - cache.insert(node(MAX_PER_SHARD), commitment(1), Arc::clone(&poly)); - assert!(cache.get(node(MAX_PER_SHARD), &commitment(1)).is_some()); - assert!((0..MAX_PER_SHARD).all(|i| cache.get(node(i), &commitment(1)).is_none())); + cache.set_capacity(1); + assert_eq!(cache.capacity(), SHARDS); + cache.insert(node(0), commitment(1), poly.clone()); + cache.insert(8, commitment(1), poly.clone()); + assert_eq!(cache.nodes(), [node(0), 8]); + // One polynomial per shard: a second node of a shard takes the first one's place. + cache.insert(node(1), commitment(1), poly.clone()); + assert_eq!(cache.nodes(), [8, node(1)]); + + cache.clear(); + assert!(cache.nodes().is_empty()); + assert_eq!(cache.capacity(), SHARDS); + cache.insert(node(0), commitment(1), poly); + assert_eq!(cache.nodes(), [node(0)]); } /// Regression test for PR #152 review comment r4080228453: a store whose trie and state @@ -744,6 +1035,187 @@ mod tests { ); } + /// `count` nodes with random polynomials, each read at the commitment its polynomial + /// opens to. The commitments come from the CRS directly, not from the committer the + /// checks under test use, and a node's physical id differs from its logical one. + fn committed_nodes(seed: u64, count: usize) -> (Vec, Vec>) { + let mut rng = StdRng::seed_from_u64(seed); + (0..count as NodeId) + .map(|logical| { + let poly = LagrangeBasis::new( + (0..DOMAIN_SIZE) + .map(|_| Fr::from_le_bytes_mod_order(&mock_data(&mut rng, 32))) + .collect(), + ); + let bytes = DEFAULT_CRS + .commit_lagrange_poly(&poly) + .to_bytes_uncompressed(); + let node = ParentNode { + logical, + physical: logical + 1000, + bytes, + commitment: Element::from_bytes_unchecked_uncompressed(bytes), + }; + (node, Arc::new(poly)) + }) + .unzip() + } + + /// `poly` with the evaluation at `index` zeroed, which no longer opens to `poly`'s + /// commitment: a random evaluation is not zero. + fn zeroed_at(poly: &LagrangeBasis, index: usize) -> LagrangeBasis { + let mut evaluations: Vec = (0..DOMAIN_SIZE) + .map(|i| poly.evaluate_in_domain(i)) + .collect(); + assert!(!evaluations[index].is_zero()); + evaluations[index] = Fr::zero(); + LagrangeBasis::new(evaluations) + } + + /// The batch check passes when every polynomial opens to its node's commitment, and fails + /// when any does not, however small the difference and wherever it sits in the batch. + #[test] + fn batch_check_passes_only_when_every_polynomial_opens_to_its_commitment() { + let (nodes, polys) = committed_nodes(0xBA7C, 5); + let misses: Vec<&ParentNode> = nodes.iter().collect(); + + assert!(batch_opens(&misses, &polys)); + assert!(batch_opens(&misses[..1], &polys[..1])); + assert!(batch_opens(&[], &[])); + + // One evaluation of one polynomial differs. + for (node, index) in [(0, 0), (3, DOMAIN_SIZE - 1), (4, 17)] { + let mut off = polys.clone(); + off[node] = Arc::new(zeroed_at(&polys[node], index)); + assert!(!batch_opens(&misses, &off), "node {node}, index {index}"); + assert!(!batch_opens(&misses[node..=node], &off[node..=node])); + } + + // Two polynomials trade places. Each still opens to a commitment of the batch, so + // the plain sums of the two sides agree; it is the per-node challenges that tell. + let mut swapped = polys.clone(); + swapped.swap(1, 2); + assert!(!batch_opens(&misses, &swapped)); + } + + /// The challenges are drawn from the batch itself: one per node, 128 bits each, and any + /// change to a commitment, a polynomial or the node order moves every one of them. + #[test] + fn batch_challenges_depend_on_every_commitment_and_polynomial() { + let (nodes, polys) = committed_nodes(0xC4A1, 3); + let misses: Vec<&ParentNode> = nodes.iter().collect(); + let challenges = batch_challenges(&misses, &polys); + + assert_eq!(challenges.len(), 3); + assert_eq!(challenges, batch_challenges(&misses, &polys)); + for (i, challenge) in challenges.iter().enumerate() { + assert!(!challenge.is_zero()); + assert_eq!(challenge.into_bigint().0[2..], [0, 0], "128 bits"); + assert!(!challenges[..i].contains(challenge)); + } + let all_moved = + |other: Vec| other.len() == 3 && other.iter().zip(&challenges).all(|(a, b)| a != b); + + // The first and the last evaluation of a polynomial. + for (node, index) in [(0, 0), (2, DOMAIN_SIZE - 1)] { + let mut changed = polys.clone(); + changed[node] = Arc::new(zeroed_at(&polys[node], index)); + assert!(all_moved(batch_challenges(&misses, &changed))); + } + + // One bit of a commitment's bytes. + let mut bytes = nodes[1].bytes; + bytes[63] ^= 1; + let rewritten = ParentNode { + logical: nodes[1].logical, + physical: nodes[1].physical, + bytes, + commitment: nodes[1].commitment, + }; + assert!(all_moved(batch_challenges( + &[&nodes[0], &rewritten, &nodes[2]], + &polys + ))); + + // The order of the nodes. + let reversed: Vec> = polys.iter().rev().cloned().collect(); + assert!(all_moved(batch_challenges( + &[&nodes[2], &nodes[1], &nodes[0]], + &reversed + ))); + } + + /// `resolve_polys` hands the proof every polynomial it rebuilt, but caches only those that + /// open to the commitment their node was read at: one that does not is rebuilt again by + /// the next call, and the ones that do are served from the cache from then on. + #[test] + fn resolve_polys_caches_only_the_rebuilds_that_open_to_their_commitment() { + let cache = NodePolyCache::with_capacity(NodePolyCache::DEFAULT_CAPACITY); + let (nodes, polys) = committed_nodes(0x0152, 4); + let rebuild = |poly: &Arc| LagrangeBasis::clone(poly); + // What an inconsistent store rebuilds for node 2. + let stale = zeroed_at(&polys[2], 0); + + let logical_ids: Vec = nodes.iter().map(|node| node.logical).collect(); + let first = resolve_polys(&cache, &nodes, |missing| { + assert_eq!(missing, logical_ids); + Ok(vec![ + rebuild(&polys[0]), + rebuild(&polys[1]), + stale.clone(), + rebuild(&polys[3]), + ]) + }) + .unwrap(); + assert_eq!(*first[2], stale); + for (i, node) in nodes.iter().enumerate() { + let cached = cache.get(node.physical, &node.bytes); + if i == 2 { + assert!(cached.is_none()); + } else { + assert_eq!(first[i], polys[i]); + assert_eq!(cached.as_ref(), Some(&polys[i])); + } + assert!(cache.get(node.logical, &node.bytes).is_none()); + } + + // Node 2 alone is rebuilt again, this time from a consistent store. + let second = resolve_polys(&cache, &nodes, |missing| { + assert_eq!(missing, [nodes[2].logical]); + Ok(vec![rebuild(&polys[2])]) + }) + .unwrap(); + assert_eq!(second, polys); + + let third = resolve_polys(&cache, &nodes, |missing| { + assert!(missing.is_empty()); + Ok(Vec::new()) + }) + .unwrap(); + assert_eq!(third, polys); + } + + /// A cache that is turned off serves and takes nothing: every call rebuilds every node. + #[test] + fn resolve_polys_rebuilds_everything_when_the_cache_is_off() { + let cache = NodePolyCache::with_capacity(0); + let (nodes, polys) = committed_nodes(0x0FF, 3); + let logical_ids: Vec = nodes.iter().map(|node| node.logical).collect(); + + for _ in 0..2 { + let resolved = resolve_polys(&cache, &nodes, |missing| { + assert_eq!(missing, logical_ids); + Ok(polys + .iter() + .map(|poly| LagrangeBasis::clone(poly)) + .collect()) + }) + .unwrap(); + assert_eq!(resolved, polys); + assert!(cache.nodes().is_empty()); + } + } + #[test] fn internal_polynomials_empty_and_single_node() { let store = MemStore::new(); From a015ed15366dd7a7b4cf580edc4c03e77b649492 Mon Sep 17 00:00:00 2001 From: "liquan.eth" Date: Thu, 12 Nov 2026 23:01:21 +0900 Subject: [PATCH 18/18] bench(proof): time cold creation again The proof benches repeat one input, so after the first iteration every node polynomial comes from the cache and the miss path is never timed, which is how a tenfold slowdown on it went unnoticed. `salt_proof/create-cold` and `witness/create-cold` clear the cache before each iteration, through `NodePolyCache::clear` now that the cache has a public handle (6d5298f dropped these groups together with the hook they needed). Co-Authored-By: Claude Fable 5.2 --- salt/benches/proof.rs | 46 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/salt/benches/proof.rs b/salt/benches/proof.rs index 25799bd2..dcd0af9b 100644 --- a/salt/benches/proof.rs +++ b/salt/benches/proof.rs @@ -7,13 +7,21 @@ //! - `Witness::create` — block witness generation (lookups + updates) //! - `Witness::verify` — block witness verification (stateless validator) //! +//! Creation is timed twice. The plain groups repeat one input, so after the first iteration +//! every node polynomial is served from the cache; the `-cold` groups clear the cache before +//! each iteration, so every node is rebuilt from the store and checked before it is cached. +//! A caller sees something in between. +//! //! ```bash //! cargo bench --package salt --bench proof //! ``` -use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; +use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; use rand::{rngs::StdRng, Rng, SeedableRng}; -use salt::{types::*, EphemeralSaltState, MemStore, SaltProof, SaltWitness, StateRoot, Witness}; +use salt::{ + types::*, EphemeralSaltState, MemStore, NodePolyCache, SaltProof, SaltWitness, StateRoot, + Witness, +}; use std::collections::BTreeMap; use std::hint::black_box; use std::sync::OnceLock; @@ -100,6 +108,21 @@ fn bench_salt_proof(c: &mut Criterion) { } group.finish(); + let mut group = c.benchmark_group("salt_proof/create-cold"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(10)); + for n in [16usize, 256, 2048] { + let keys = proof_keys(s, n); + group.bench_with_input(BenchmarkId::from_parameter(n), &keys, |b, keys| { + b.iter_batched( + || NodePolyCache::global().clear(), + |()| black_box(SaltProof::create(keys.iter().copied(), &s.store).unwrap()), + BatchSize::PerIteration, + ); + }); + } + group.finish(); + let mut group = c.benchmark_group("salt_proof/check"); group.sample_size(10); group.measurement_time(Duration::from_secs(10)); @@ -170,6 +193,25 @@ fn bench_witness(c: &mut Criterion) { } group.finish(); + let mut group = c.benchmark_group("witness/create-cold"); + group.sample_size(10); + group.measurement_time(Duration::from_secs(15)); + for n in [256usize, 1024] { + let (lookups, updates) = witness_workload(s, n, &mut rng); + group.bench_with_input( + BenchmarkId::from_parameter(n), + &(lookups, updates), + |b, (lookups, updates)| { + b.iter_batched( + || NodePolyCache::global().clear(), + |()| black_box(Witness::create([], lookups.iter(), updates, &s.store).unwrap()), + BatchSize::PerIteration, + ); + }, + ); + } + group.finish(); + let mut group = c.benchmark_group("witness/verify"); group.sample_size(10); group.measurement_time(Duration::from_secs(15));