diff --git a/banderwagon/src/element.rs b/banderwagon/src/element.rs index 592add3e..eedc785d 100644 --- a/banderwagon/src/element.rs +++ b/banderwagon/src/element.rs @@ -361,26 +361,46 @@ impl Element { /// # Returns /// /// Vector of scalar field elements, one for each input Element + /// + /// On an `x86_64` host with AVX-512 IFMA the inversions and products run eight elements per vector + /// register ([`crate::ifma_normalize`]) for every full group of eight, the tail through the scalar + /// path; the values are the same. pub fn batch_map_to_scalar_field(elements: &[Element]) -> Vec { - let (xs, mut ys): (Vec, Vec) = elements.iter().map(|e| (e.0.x, e.0.y)).unzip(); - - serial_batch_inversion_and_mul(&mut ys, &Fq::ONE); - - xs.into_iter() - .zip(ys) - .map(|(x, y_inv)| base_to_scalar(x * y_inv)) - .collect() + #[cfg(all(feature = "std", target_arch = "x86_64"))] + if elements.len() >= 8 && crate::ifma::available() { + let head = elements.len() / 8 * 8; + let mut out = vec![Fr::zero(); elements.len()]; + // SAFETY: `available()` was checked; `head` is a multiple of eight within both slices. + unsafe { + crate::ifma_normalize::batch_map_to_scalar_field( + &elements[..head], + &mut out[..head], + ) + }; + out.truncate(head); + out.extend(batch_map_to_scalar_field_scalar(&elements[head..])); + return out; + } + batch_map_to_scalar_field_scalar(elements) } /// Takes uncompressed element bytes (64 bytes each) and returns scalar field elements. /// See [`map_to_scalar_field()`](Element::map_to_scalar_field) for mapping semantics. + /// + /// On an `x86_64` host with AVX-512 IFMA the bytes are read straight into the lanes + /// ([`crate::ifma_normalize`]) for every full group of eight, the tail through the scalar path. pub fn hash_commitments(elements: &[[u8; 64]]) -> Vec { - let elements: Vec = elements - .iter() - .map(|&bytes| Element::from_bytes_unchecked_uncompressed(bytes)) - .collect(); - - Self::batch_map_to_scalar_field(&elements) + #[cfg(all(feature = "std", target_arch = "x86_64"))] + if elements.len() >= 8 && crate::ifma::available() { + let head = elements.len() / 8 * 8; + let mut out = vec![Fr::zero(); elements.len()]; + // SAFETY: `available()` was checked; `head` is a multiple of eight within both slices. + unsafe { crate::ifma_normalize::hash_commitments(&elements[..head], &mut out[..head]) }; + out.truncate(head); + out.extend(hash_commitments_scalar(&elements[head..])); + return out; + } + hash_commitments_scalar(elements) } /// Converts banderwagon elements to their 64-byte commitment representations. @@ -398,12 +418,24 @@ impl Element { /// Vector of 64-byte arrays, each containing canonicalized uncompressed coordinates: /// - Bytes 0-31: X-coordinate (little-endian) /// - Bytes 32-63: Y-coordinate (little-endian, always positive) + /// + /// On an `x86_64` host with AVX-512 IFMA the normalization runs eight points per vector register + /// ([`crate::ifma_normalize`]) for every full group of eight, the tail through the scalar path; the + /// bytes are the same. pub fn batch_to_commitments(elements: &[Element]) -> Vec<[u8; 64]> { - let points: Vec<_> = elements.iter().map(|e| e.0).collect(); - EdwardsProjective::normalize_batch(&points) - .into_iter() - .map(affine_to_canonical_bytes) - .collect() + #[cfg(all(feature = "std", target_arch = "x86_64"))] + if elements.len() >= 8 && crate::ifma::available() { + let head = elements.len() / 8 * 8; + let mut out = vec![[0u8; 64]; elements.len()]; + // SAFETY: `available()` was checked; `head` is a multiple of eight within both slices. + unsafe { + crate::ifma_normalize::batch_to_commitments(&elements[..head], &mut out[..head]) + }; + out.truncate(head); + out.extend(batch_to_commitments_scalar(&elements[head..])); + return out; + } + batch_to_commitments_scalar(elements) } pub fn zero() -> Element { @@ -443,6 +475,240 @@ pub(crate) fn is_positive(coordinate: Fq) -> bool { coordinate > -coordinate } +/// [`Element::batch_map_to_scalar_field`] on the scalar field arithmetic: one batch inversion of the `y` +/// coordinates, then `x · y⁻¹` reinterpreted. Single-threaded on purpose (small batches). +pub(crate) fn batch_map_to_scalar_field_scalar(elements: &[Element]) -> Vec { + let (xs, mut ys): (Vec, Vec) = elements.iter().map(|e| (e.0.x, e.0.y)).unzip(); + + serial_batch_inversion_and_mul(&mut ys, &Fq::ONE); + + xs.into_iter() + .zip(ys) + .map(|(x, y_inv)| base_to_scalar(x * y_inv)) + .collect() +} + +/// [`Element::hash_commitments`] on the scalar field arithmetic. +pub(crate) fn hash_commitments_scalar(elements: &[[u8; 64]]) -> Vec { + let elements: Vec = elements + .iter() + .map(|&bytes| Element::from_bytes_unchecked_uncompressed(bytes)) + .collect(); + + batch_map_to_scalar_field_scalar(&elements) +} + +/// [`Element::batch_to_commitments`] on the scalar field arithmetic: ark's batch normalization, then the +/// canonical bytes of each affine point. +pub(crate) fn batch_to_commitments_scalar(elements: &[Element]) -> Vec<[u8; 64]> { + let points: Vec<_> = elements.iter().map(|e| e.0).collect(); + EdwardsProjective::normalize_batch(&points) + .into_iter() + .map(affine_to_canonical_bytes) + .collect() +} + +/// A banderwagon element in affine coordinates: the 64 bytes `(x, y)` (Montgomery words) instead of the +/// 128-byte projective `(X, Y, T, Z)` an [`Element`] carries. The form a witness holds its commitments in: +/// every decoded point is affine already (`Z = 1`), the verifier's multi-scalar multiplication takes +/// affine bases, the transcript and the scalar-field map read `x` and `y` only, and the one consumer that +/// adds to a commitment (the trie update) re-enters projective form with a single multiplication +/// (`T = x · y`, [`AffineElement::to_element`]). Equality is the quotient group's, as [`Element`]'s. +#[derive(Debug, Clone, Copy)] +#[repr(transparent)] +pub struct AffineElement(pub(crate) EdwardsAffine); + +impl PartialEq for AffineElement { + /// [`Element`]'s equality: `x₁ · y₂ == x₂ · y₁`, `(0, 0)` equal to nothing. + fn eq(&self, other: &Self) -> bool { + let (x1, y1) = (self.0.x, self.0.y); + let (x2, y2) = (other.0.x, other.0.y); + if x1.is_zero() & y1.is_zero() { + return false; + } + if x2.is_zero() & y2.is_zero() { + return false; + } + (x1 * y2) == (x2 * y1) + } +} + +impl From for AffineElement { + /// `Z = 1` (every decoded or normalized point) costs nothing; otherwise one inversion. + fn from(e: Element) -> Self { + AffineElement(e.0.into()) + } +} + +impl From for Element { + fn from(a: AffineElement) -> Self { + a.to_element() + } +} + +impl AffineElement { + /// The projective element: `(x, y, x · y, 1)`. + pub fn to_element(&self) -> Element { + Element(self.0.into()) + } + + /// The identity, `(0, 1)`. + pub fn zero() -> Self { + AffineElement(EdwardsAffine::zero()) + } + + pub fn is_zero(&self) -> bool { + *self == Self::zero() + } + + /// [`Element::to_bytes`]: `sign(y) · x`, big-endian. + pub fn to_bytes(&self) -> [u8; 32] { + let x = if is_positive(self.0.y) { + self.0.x + } else { + -self.0.x + }; + let mut bytes = [0u8; 32]; + x.serialize_compressed(&mut bytes[..]) + .expect("serialization failed"); + bytes.reverse(); + bytes + } + + /// [`Element::to_bytes_uncompressed`]: the representative with positive `y`, little-endian `x` then `y`. + pub fn to_bytes_uncompressed(&self) -> [u8; 64] { + affine_to_canonical_bytes(self.0) + } + + /// [`Element::from_bytes`], affine. + pub fn from_bytes(bytes: [u8; 32]) -> Result { + Element::from_bytes(bytes).map(Self::from) + } + + /// [`Element::from_bytes_batch`], affine: the lane decode yields `(x, y)` and stores it as is. + pub fn from_bytes_batch(bytes: &[[u8; 32]]) -> Vec> { + #[cfg(all(feature = "std", target_arch = "x86_64"))] + if crate::ifma::available() { + return Self::from_bytes_batch_lanes(bytes); + } + bytes.iter().map(|b| Self::from_bytes(*b)).collect() + } + + #[cfg(all(feature = "std", target_arch = "x86_64"))] + fn from_bytes_batch_lanes(bytes: &[[u8; 32]]) -> Vec> { + use crate::ifma::{CHUNK, TASK_CHUNKS}; + let mut out = Vec::with_capacity(bytes.len()); + for task in bytes.chunks(CHUNK * TASK_CHUNKS) { + let mut chunks = task.chunks_exact(CHUNK); + let mut xs: Vec<[Fq; CHUNK]> = Vec::with_capacity(TASK_CHUNKS); + let mut lane_chunks: Vec<&[[u8; 32]]> = Vec::with_capacity(TASK_CHUNKS); + let mut scalar_chunks: Vec<(usize, &[[u8; 32]])> = Vec::new(); + for (k, chunk) in (&mut chunks).enumerate() { + let mut x = [Fq::zero(); CHUNK]; + let mut parsed = true; + for (x, b) in x.iter_mut().zip(chunk) { + let mut le = *b; + le.reverse(); + match Fq::deserialize_compressed(&le[..]) { + Ok(v) => *x = v, + Err(_) => { + parsed = false; + break; + } + } + } + if parsed { + xs.push(x); + lane_chunks.push(chunk); + } else { + scalar_chunks.push((k, chunk)); + } + } + let decoded = if xs.is_empty() { + Vec::new() + } else { + // SAFETY: `available()` was checked by the caller. + unsafe { crate::ifma::decode_chunks(&xs) } + }; + let mut lanes = lane_chunks.iter().zip(decoded); + let mut scalars = scalar_chunks.iter().peekable(); + for k in 0..task.len() / CHUNK { + if scalars.peek().is_some_and(|(sk, _)| *sk == k) { + let (_, chunk) = scalars.next().unwrap(); + out.extend(chunk.iter().map(|b| Self::from_bytes(*b))); + continue; + } + let (chunk, (points, undecided)) = lanes.next().expect("a lane chunk per index"); + for i in 0..CHUNK { + out.push(if undecided[i] { + Self::from_bytes(chunk[i]) + } else { + points[i] + .map(|(x, y)| AffineElement(EdwardsAffine::new_unchecked(x, y))) + .ok_or(SerializationError::InvalidData) + }); + } + } + out.extend(chunks.remainder().iter().map(|b| Self::from_bytes(*b))); + } + out + } + + /// [`Element::map_to_scalar_field`] on an affine point: `x / y` reinterpreted. + pub fn map_to_scalar_field(&self) -> Fr { + base_to_scalar(self.0.x / self.0.y) + } + + /// [`Element::batch_map_to_scalar_field`] on affine points: the same values, the lanes reading the + /// 64-byte records where the host has AVX-512 IFMA. + pub fn batch_map_to_scalar_field(elements: &[AffineElement]) -> Vec { + #[cfg(all(feature = "std", target_arch = "x86_64"))] + if elements.len() >= 8 && crate::ifma::available() { + let head = elements.len() / 8 * 8; + let mut out = vec![Fr::zero(); elements.len()]; + // SAFETY: `available()` was checked; `head` is a multiple of eight within both slices. + unsafe { + crate::ifma_normalize::batch_map_to_scalar_field_affine( + &elements[..head], + &mut out[..head], + ) + }; + out.truncate(head); + out.extend(batch_map_to_scalar_field_affine_scalar(&elements[head..])); + return out; + } + batch_map_to_scalar_field_affine_scalar(elements) + } +} + +/// [`AffineElement::batch_map_to_scalar_field`] on the scalar field arithmetic. +pub(crate) fn batch_map_to_scalar_field_affine_scalar(elements: &[AffineElement]) -> Vec { + let (xs, mut ys): (Vec, Vec) = elements.iter().map(|e| (e.0.x, e.0.y)).unzip(); + serial_batch_inversion_and_mul(&mut ys, &Fq::ONE); + xs.into_iter() + .zip(ys) + .map(|(x, y_inv)| base_to_scalar(x * y_inv)) + .collect() +} + +/// [`multi_scalar_mul`] on affine bases: no conversion, the bases as they are. +pub fn multi_scalar_mul_affine(bases: &[AffineElement], scalars: &[Fr]) -> Element { + // SAFETY: `AffineElement` is `repr(transparent)` over `EdwardsAffine`. + let bases: &[EdwardsAffine] = + unsafe { core::slice::from_raw_parts(bases.as_ptr() as *const EdwardsAffine, bases.len()) }; + Element(msm(bases, scalars)) +} + +/// The canonical little-endian bytes of a base field element (tests). +#[cfg(test)] +#[allow(dead_code)] // used by the std decoder path only; the test target builds this crate with -D warnings +pub(crate) fn fq_to_le_bytes(x: Fq) -> [u8; 32] { + let mut bytes = [0u8; 32]; + x.serialize_compressed(&mut bytes[..]) + .expect("32 bytes hold a field element"); + bytes +} + /// Canonicalizes an affine point and serializes it to 64 bytes. /// /// Chooses the banderwagon representative with positive y-coordinate, @@ -556,6 +822,12 @@ 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(msm(&bases, scalars)) } @@ -652,6 +924,30 @@ mod tests { extern crate std; use self::std::println; + #[test] + fn hash_commitments_rejects_noncanonical_coordinates() { + use ark_ff::{BigInteger, PrimeField}; + let valid = Element::prime_subgroup_generator().to_bytes_uncompressed(); + let modulus = Fq::MODULUS.to_bytes_le(); + for n in [1, 7, 8, 9, 16, 17] { + for index in 0..n { + for offset in [0, 32] { + for bad in [modulus.as_slice(), &[0xff; 32]] { + let mut bytes = vec![valid; n]; + bytes[index][offset..offset + 32].copy_from_slice(bad); + assert!( + std::panic::catch_unwind(|| hash_commitments_scalar(&bytes)).is_err() + ); + assert!( + std::panic::catch_unwind(|| Element::hash_commitments(&bytes)).is_err(), + "n={n} index={index} offset={offset}" + ); + } + } + } + } + } + #[cfg(feature = "parallel")] #[test] fn msm_pool_crossover_preserves_group_result() { @@ -763,6 +1059,88 @@ mod tests { } } + /// The affine element is the projective one on every path: the batch decode (accepted points, + /// canonical `x` with no square root, off-subgroup points, non-canonical bytes, a short tail), the + /// compressed and uncompressed bytes, the scalar-field map in batches of every length modulo eight, + /// the multi-scalar multiplication, and equality including the `(x, y) ~ (-x, -y)` identification. + #[test] + fn affine_element_matches_element() { + use ark_ff::UniformRand; + use rand_chacha::rand_core::{RngCore, SeedableRng}; + let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(11); + let mut inputs: Vec<[u8; 32]> = Vec::new(); + for _ in 0..300 { + inputs.push((Element::prime_subgroup_generator() * Fr::rand(&mut rng)).to_bytes()); + } + for _ in 0..100 { + let x = Fq::rand(&mut rng); + let mut b = [0u8; 32]; + x.serialize_compressed(&mut b[..]).unwrap(); + b.reverse(); + inputs.push(b); + } + for _ in 0..10 { + inputs.push([0xff; 32]); + let mut b = [0u8; 32]; + rng.fill_bytes(&mut b); + inputs.push(b); + } + inputs.push(Element::zero().to_bytes()); + let expected = Element::from_bytes_batch(&inputs); + let got = AffineElement::from_bytes_batch(&inputs); + assert_eq!(got.len(), expected.len()); + let mut points: Vec = Vec::new(); + let mut affine: Vec = Vec::new(); + for (e, a) in expected.into_iter().zip(got) { + match (e, a) { + (Ok(e), Ok(a)) => { + assert_eq!(a.to_element(), e); + assert_eq!(AffineElement::from(e), a); + assert_eq!(a.to_bytes(), e.to_bytes()); + assert_eq!(a.to_bytes_uncompressed(), e.to_bytes_uncompressed()); + assert_eq!(a.map_to_scalar_field(), e.map_to_scalar_field()); + assert_eq!(a.is_zero(), e.is_zero()); + points.push(e); + affine.push(a); + } + (Err(_), Err(_)) => {} + (e, a) => panic!("affine {a:?} vs projective {e:?}"), + } + } + assert!(points.len() >= 300); + for n in [0usize, 1, 7, 8, 9, 15, 16, 17, 100, points.len()] { + assert_eq!( + AffineElement::batch_map_to_scalar_field(&affine[..n]), + Element::batch_map_to_scalar_field(&points[..n]), + "batch of {n}" + ); + } + let scalars: Vec = (0..points.len()).map(|_| Fr::rand(&mut rng)).collect(); + assert_eq!( + multi_scalar_mul_affine(&affine, &scalars), + multi_scalar_mul(&points, &scalars) + ); + assert_eq!( + multi_scalar_mul_affine(&affine[..5], &scalars[..5]), + multi_scalar_mul(&points[..5], &scalars[..5]) + ); + // the other representative of a point is the same affine element, with the same bytes + let neg = AffineElement(EdwardsAffine::new_unchecked(-affine[0].0.x, -affine[0].0.y)); + assert_eq!(neg, affine[0]); + assert_eq!(neg.to_bytes(), affine[0].to_bytes()); + assert_eq!( + neg.to_bytes_uncompressed(), + affine[0].to_bytes_uncompressed() + ); + assert_ne!(affine[0], affine[1]); + assert_eq!(AffineElement::zero().to_element(), Element::zero()); + assert!(AffineElement::zero().is_zero()); + // a projective point with Z ≠ 1 (a sum) converts by its affine coordinates + let sum = points[0] + points[1]; + assert_eq!(AffineElement::from(sum).to_element(), sum); + assert_eq!(AffineElement::from(sum).to_bytes(), sum.to_bytes()); + } + /// Decode cost per point, lanes against the scalar path, one thread (run with `--ignored --nocapture`). #[test] #[ignore] diff --git a/banderwagon/src/ifma.rs b/banderwagon/src/ifma.rs index 007904b6..f7150b6f 100644 --- a/banderwagon/src/ifma.rs +++ b/banderwagon/src/ifma.rs @@ -217,6 +217,8 @@ pub(crate) struct Ctx { trace_minus_one_div_two: [u64; 4], modulus_minus_one_div_two: [u64; 4], pub(crate) modulus_minus_two: [u64; 4], + /// `(p − 1) / 2` as lane limbs: a canonical `y` is positive (`y > −y`) iff `y` exceeds it. + half_p: L5, /// The order-256 subgroup `⟨g^(2^24)⟩` (g the 2^32-th root of unity): the canonical Montgomery low /// limb of each element, sorted, and the exponent it belongs to. dlog_keys: [u64; 256], @@ -278,6 +280,7 @@ unsafe fn ctx() -> Ctx { trace_minus_one_div_two: Fq::TRACE_MINUS_ONE_DIV_TWO.0, modulus_minus_one_div_two: Fq::MODULUS_MINUS_ONE_DIV_TWO.0, modulus_minus_two: [0; 4], + half_p: bcast(&to_limbs52(&Fq::MODULUS_MINUS_ONE_DIV_TWO)), dlog_keys: [0; 256], dlog_vals: [0; 256], neg_pow: Vec::new(), @@ -788,6 +791,15 @@ pub(crate) unsafe fn sub(c: &Ctx, a: &L5, b: &L5) -> L5 { r } +/// The plain (non-Montgomery) canonical integer of each lane's value: `a · R⁻¹ mod p`, in `[0, p)`. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn to_plain(c: &Ctx, a: &L5) -> L5 { + let mut one_int = [_mm512_setzero_si512(); 5]; + one_int[0] = _mm512_set1_epi64(1); + canon(c, &mont_mul(c, a, &one_int)) +} + /// Lane mask: which lanes of canonical `a` are zero. #[inline] #[target_feature(enable = "avx512f,avx512ifma")] @@ -800,6 +812,74 @@ pub(crate) unsafe fn is_zero(a: &L5) -> __mmask8 { m } +/// Lane mask: which lanes of canonical `a` exceed `(p − 1) / 2`, i.e. are "positive" in the sense of +/// [`crate::element::is_positive`] (`a > −a` as canonical integers). +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn is_positive(c: &Ctx, a: &L5) -> __mmask8 { + let mut gt: __mmask8 = 0; + let mut decided: __mmask8 = 0; + for j in (0..5).rev() { + let g = _mm512_cmpgt_epu64_mask(a[j], c.half_p[j]); + let l = _mm512_cmplt_epu64_mask(a[j], c.half_p[j]); + gt |= g & !decided; + decided |= g | l; + } + gt +} + +/// `p − a` canonical for canonical `a` (zero stays zero): the field negation on plain or Montgomery lanes. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn neg_canon(c: &Ctx, a: &L5) -> L5 { + let z = is_zero(a); + let mut r = [_mm512_setzero_si512(); 5]; + let mut borrow = _mm512_setzero_si512(); + for j in 0..5 { + let d = _mm512_sub_epi64(_mm512_sub_epi64(c.p[j], a[j]), borrow); + borrow = _mm512_srli_epi64(d, 63); + r[j] = _mm512_and_si512(d, c.mask); + } + let zero = [_mm512_setzero_si512(); 5]; + sel(z, &r, &zero) +} + +/// Five 52-bit limbs (a canonical value < 2^255) as four 64-bit words, the little-endian integer. +#[inline] +#[target_feature(enable = "avx512f")] +pub(crate) unsafe fn limbs_to_words(l: &L5) -> [__m512i; 4] { + [ + _mm512_or_si512(l[0], _mm512_slli_epi64(l[1], 52)), + _mm512_or_si512(_mm512_srli_epi64(l[1], 12), _mm512_slli_epi64(l[2], 40)), + _mm512_or_si512(_mm512_srli_epi64(l[2], 24), _mm512_slli_epi64(l[3], 28)), + _mm512_or_si512(_mm512_srli_epi64(l[3], 36), _mm512_slli_epi64(l[4], 16)), + ] +} + +/// Four 64-bit words (ark's Montgomery representation, gathered one word per register) as five +/// 52-bit limbs; the inverse of [`limbs_to_words`]. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn words_to_limbs(c: &Ctx, w: &[__m512i; 4]) -> L5 { + let m = c.mask; + [ + _mm512_and_si512(w[0], m), + _mm512_and_si512( + _mm512_or_si512(_mm512_srli_epi64(w[0], 52), _mm512_slli_epi64(w[1], 12)), + m, + ), + _mm512_and_si512( + _mm512_or_si512(_mm512_srli_epi64(w[1], 40), _mm512_slli_epi64(w[2], 24)), + m, + ), + _mm512_and_si512( + _mm512_or_si512(_mm512_srli_epi64(w[2], 28), _mm512_slli_epi64(w[3], 36)), + m, + ), + _mm512_srli_epi64(w[3], 16), + ] +} + /// A field element (ark Montgomery form, R = 2^256) as the lane form's limbs (R = 2^260): the lane /// integer is x·2^260 mod p = the ark representation of 16·x. pub(crate) fn bcast_limbs(x: &Fq) -> [u64; 5] { diff --git a/banderwagon/src/ifma_normalize.rs b/banderwagon/src/ifma_normalize.rs new file mode 100644 index 00000000..6406b616 --- /dev/null +++ b/banderwagon/src/ifma_normalize.rs @@ -0,0 +1,622 @@ +//! The trie update's commitment conversions eight per vector register on AVX-512 IFMA, in the lane field +//! arithmetic of [`crate::ifma`]: projective points to canonical 64-byte commitments +//! ([`crate::Element::batch_to_commitments`]), and commitments to the scalar field by `x / y` +//! ([`crate::Element::batch_map_to_scalar_field`], [`crate::Element::hash_commitments`]). +//! +//! Each is one batch inversion (Montgomery's trick: three products per element and one exponentiation +//! per block of [`BLOCK`] elements, the chain split four ways so the products overlap) and a few products +//! per element, all on eight elements per register. Ark's Montgomery words (`R = 2^256`) are read as the +//! lane form (`R = 2^260`) of `v / 16` without conversion, as the lane committer does: `X/Z`, `X/Y` are +//! ratios, so the common factor cancels and the affine coordinates come out exact; the canonical bytes +//! are the plain integers (one product by 1) with the sign rule of the scalar path (`y` positive), and +//! the scalar-field image is the scalar path's `Fr::from_le_bytes_mod_order` of those bytes. A zero +//! denominator (which ark's batch inversion leaves at zero) is masked out of the chain and yields zero, +//! as the scalar path's does. The tests check every output byte and scalar against the scalar path on +//! random points, the identity, points with a negative `y`, and batches of every length modulo eight. +#![allow(clippy::needless_range_loop)] +use crate::ifma::*; +use crate::ifma_msm::transpose8; +use crate::{AffineElement, Element}; +use ark_ed_on_bls12_381_bandersnatch::{EdwardsAffine, EdwardsProjective, Fq, Fr}; +use ark_ff::{BigInt, Field, PrimeField}; +use core::arch::x86_64::*; +use std::sync::OnceLock; + +/// Elements per exponentiation: 256 lane groups, four interleaved product chains of 64 steps. +const BLOCK: usize = 2048; +const GROUPS: usize = BLOCK / 8; +const CHAINS: usize = 4; + +/// Byte offsets of the projective coordinates inside an [`Element`] (ark's `Projective { x, y, t, z }`, +/// 32-byte `Fq` each; the layout is read, not assumed). +const OX: usize = core::mem::offset_of!(EdwardsProjective, x); +const OY: usize = core::mem::offset_of!(EdwardsProjective, y); +const OZ: usize = core::mem::offset_of!(EdwardsProjective, z); +const _: () = assert!( + core::mem::size_of::() == 128 + && core::mem::size_of::() == 32 + && OX.is_multiple_of(8) + && OY.is_multiple_of(8) + && OZ.is_multiple_of(8) +); +/// An [`AffineElement`] is the 64-byte record `x` then `y` (ark's `Affine { x, y }`; the layout is read, +/// not assumed): the lanes load it as they load a 64-byte commitment. +const _: () = assert!( + core::mem::size_of::() == 64 + && core::mem::offset_of!(EdwardsAffine, x) == 0 + && core::mem::offset_of!(EdwardsAffine, y) == 32 +); + +/// Word `w` (0..16) of every point after the two transposes of eight 128-byte points. +#[inline] +fn word(a: &[__m512i; 8], b: &[__m512i; 8], w: usize) -> __m512i { + if w < 8 { + a[w] + } else { + b[w - 8] + } +} + +/// Loads eight consecutive points' `x`, `y`, `z` words as lane limbs of `x/16`, `y/16`, `z/16`. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn load_xyz(c: &Ctx, pts: *const Element) -> (L5, L5, L5) { + let base = pts as *const u8; + let mut lo = [_mm512_setzero_si512(); 8]; + let mut hi = [_mm512_setzero_si512(); 8]; + for i in 0..8 { + lo[i] = _mm512_loadu_si512(base.add(128 * i) as *const _); + hi[i] = _mm512_loadu_si512(base.add(128 * i + 64) as *const _); + } + let a = transpose8(&lo); // words 0..8 of every point + let b = transpose8(&hi); // words 8..16 + let coord = |o: usize| { + let w = o / 8; + [ + word(&a, &b, w), + word(&a, &b, w + 1), + word(&a, &b, w + 2), + word(&a, &b, w + 3), + ] + }; + let x = words_to_limbs(c, &coord(OX)); + let y = words_to_limbs(c, &coord(OY)); + let z = words_to_limbs(c, &coord(OZ)); + (x, y, z) +} + +/// Loads eight consecutive 64-byte commitments (`x` then `y`, little-endian canonical integers) as plain +/// lane limbs. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn load_xy_bytes(c: &Ctx, recs: *const [u8; 64]) -> (L5, L5) { + let base = recs as *const u8; + let mut r = [_mm512_setzero_si512(); 8]; + for i in 0..8 { + r[i] = _mm512_loadu_si512(base.add(64 * i) as *const _); + } + let a = transpose8(&r); + let x = words_to_limbs(c, &[a[0], a[1], a[2], a[3]]); + let y = words_to_limbs(c, &[a[4], a[5], a[6], a[7]]); + (x, y) +} + +/// Stores eight points' canonical `(x, y)` plain limbs as 64-byte commitments. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn store_xy_bytes(x: &L5, y: &L5, out: *mut [u8; 64]) { + let xw = limbs_to_words(x); + let yw = limbs_to_words(y); + let r = transpose8(&[xw[0], xw[1], xw[2], xw[3], yw[0], yw[1], yw[2], yw[3]]); + let base = out as *mut u8; + for i in 0..8 { + _mm512_storeu_si512(base.add(64 * i) as *mut _, r[i]); + } +} + +/// The scalar field `Fr` (253 bits) as a lane modulus: `mont_mul_r(a, b) = a·b·2^−260 mod r`. Used once per +/// converted point to reinterpret the plain base-field integer `u` (< 2^255) into `Fr`'s Montgomery form +/// (`R = 2^256`): `mont_mul_r(u, k) = u·2^256 mod r` with `k = 2^516 mod r`, which is `Fr::from_le_bytes_mod_order` +/// of `u`'s bytes — the same element — without the byte round trip and its five scalar products. +struct FrMod { + p: L5, + n0: __m512i, + /// `2^516 mod r`, plain (`u · k / 2^260 = u · 2^256`). + k: L5, +} + +// SAFETY: __m512i is plain data; built once, read afterwards. +unsafe impl Sync for FrMod {} +unsafe impl Send for FrMod {} + +fn fr_mod() -> &'static FrMod { + static M: OnceLock = OnceLock::new(); + // SAFETY: callers check `available()` first; this only broadcasts constants. + M.get_or_init(|| unsafe { build_fr_mod() }) +} + +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn build_fr_mod() -> FrMod { + let r = to_limbs52(&Fr::MODULUS); + let mut inv = r[0]; + for _ in 0..7 { + inv = inv.wrapping_mul(2u64.wrapping_sub(r[0].wrapping_mul(inv))); + } + let n0 = (0u64.wrapping_sub(inv)) & ((1u64 << 52) - 1); + let k = Fr::from(2u64).pow([516u64]).into_bigint(); + FrMod { + p: bcast(&r), + n0: _mm512_set1_epi64(n0 as i64), + k: bcast(&to_limbs52(&k)), + } +} + +/// `mont_mul` with `Fr`'s modulus (the same schoolbook loop as [`crate::ifma::mont_mul`]); inputs with normalized +/// limbs and `a·b < r·2^260`, output `< 2r`. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn mont_mul_r(c: &Ctx, m: &FrMod, a: &L5, b: &L5) -> L5 { + let zero = _mm512_setzero_si512(); + let mut t = [zero; 6]; + for i in 0..5 { + let bi = b[i]; + for j in 0..5 { + t[j] = _mm512_madd52lo_epu64(t[j], a[j], bi); + t[j + 1] = _mm512_madd52hi_epu64(t[j + 1], a[j], bi); + } + let q = _mm512_madd52lo_epu64(zero, t[0], m.n0); + for j in 0..5 { + t[j] = _mm512_madd52lo_epu64(t[j], q, m.p[j]); + t[j + 1] = _mm512_madd52hi_epu64(t[j + 1], q, m.p[j]); + } + let carry = _mm512_srli_epi64(t[0], 52); + t[1] = _mm512_add_epi64(t[1], carry); + t[0] = t[1]; + t[1] = t[2]; + t[2] = t[3]; + t[3] = t[4]; + t[4] = t[5]; + t[5] = zero; + } + let mut r = [zero; 5]; + let mut carry = zero; + for j in 0..5 { + let v = _mm512_add_epi64(t[j], carry); + r[j] = _mm512_and_si512(v, c.mask); + carry = _mm512_srli_epi64(v, 52); + } + r +} + +/// Canonical representative (< r) of a value < 2r. +#[inline] +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn canon_r(c: &Ctx, m: &FrMod, a: &L5) -> L5 { + let mut ge: __mmask8 = 0; + let mut decided: __mmask8 = 0; + for j in (0..5).rev() { + let gt = _mm512_cmpgt_epu64_mask(a[j], m.p[j]); + let lt = _mm512_cmplt_epu64_mask(a[j], m.p[j]); + ge |= gt & !decided; + decided |= gt | lt; + } + ge |= !decided; + let mut r = *a; + let mut borrow = _mm512_setzero_si512(); + for j in 0..5 { + let d = _mm512_sub_epi64(_mm512_sub_epi64(a[j], m.p[j]), borrow); + borrow = _mm512_srli_epi64(d, 63); + let fixed = _mm512_and_si512(d, c.mask); + r[j] = _mm512_mask_blend_epi64(ge, a[j], fixed); + } + r +} + +/// Per-call scratch: a block's denominators (zero lanes replaced by one), the prefix products, the +/// inverses, and the zero masks. +struct Scratch { + dd: Vec, + prefix: Vec, + inv: Vec, + zero: Vec, +} + +impl Scratch { + /// Sized to the call's largest block: `groups` lane groups (≤ [`GROUPS`]). + fn new(c: &Ctx, groups: usize) -> Self { + let g = groups.min(GROUPS); + Scratch { + dd: vec![c.one; g], + prefix: vec![c.one; g], + inv: vec![c.one; g], + zero: vec![0; g], + } + } +} + +/// The lane groups of a call: eight elements each, at most a block's worth live at once. +fn groups_of(n: usize) -> usize { + (n / 8).min(GROUPS) +} + +/// The plain (non-Montgomery) inverses of every group's lanes in `d` (zero lanes give zero) into +/// `s.inv`, one exponentiation for the block: `mont_mul(v, inv_plain)` of a lane value `v` is then the +/// plain quotient `v / d` directly. +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn invert_block(c: &Ctx, d: &[L5], s: &mut Scratch) { + let n = d.len(); + debug_assert!(n <= GROUPS); + for g in 0..n { + let cd = canon(c, &d[g]); + s.zero[g] = is_zero(&cd); + s.dd[g] = sel(s.zero[g], &cd, &c.one); + } + // four interleaved prefix-product chains: chain k holds the groups g ≡ k (mod 4) + for g in 0..n { + s.prefix[g] = if g < CHAINS { + s.dd[g] + } else { + mont_mul(c, &s.prefix[g - CHAINS], &s.dd[g]) + }; + } + let mut ends = [c.one; CHAINS]; + for k in 0..CHAINS.min(n) { + let last = (n - 1 - k) / CHAINS * CHAINS + k; // the largest g ≡ k (mod 4) below n + ends[k] = s.prefix[last]; + } + let mut inv = batch_inverse::(c, &ends); + let zero = [_mm512_setzero_si512(); 5]; + for g in (0..n).rev() { + let k = g % CHAINS; + let o = if g < CHAINS { + inv[k] + } else { + let o = mont_mul(c, &inv[k], &s.prefix[g - CHAINS]); + inv[k] = mont_mul(c, &inv[k], &s.dd[g]); + o + }; + s.inv[g] = sel(s.zero[g], &to_plain(c, &o), &zero); + } +} + +/// `Element::batch_to_commitments` on the lanes; `elements.len()` is a multiple of eight. +/// +/// # Safety +/// `available()` must have returned true on this host. +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn batch_to_commitments(elements: &[Element], out: &mut [[u8; 64]]) { + debug_assert!(elements.len().is_multiple_of(8) && out.len() == elements.len()); + let c = context(); + let g = groups_of(elements.len()); + let mut s = Scratch::new(c, g); + let mut xs = vec![c.one; g]; + let mut ys = vec![c.one; g]; + let mut zs = vec![c.one; g]; + for (blk, out_blk) in elements.chunks(BLOCK).zip(out.chunks_mut(BLOCK)) { + let n = blk.len() / 8; + for g in 0..n { + let (x, y, z) = load_xyz(c, blk.as_ptr().add(8 * g)); + xs[g] = x; + ys[g] = y; + zs[g] = z; + } + invert_block(c, &zs[..n], &mut s); + for g in 0..n { + // (X/16)·(16/Z) = X/Z: the affine coordinates exactly, as plain canonical integers + let x = canon(c, &mont_mul(c, &xs[g], &s.inv[g])); + let y = canon(c, &mont_mul(c, &ys[g], &s.inv[g])); + // the representative with positive y: negate both where y is not + let flip = !is_positive(c, &y); + let x = sel(flip, &x, &neg_canon(c, &x)); + let y = sel(flip, &y, &neg_canon(c, &y)); + store_xy_bytes(&x, &y, out_blk.as_mut_ptr().add(8 * g)); + } + } +} + +/// The plain integers of `num / den` for every group (zero where `den` is zero), reinterpreted into the scalar +/// field as the scalar path does (`Fr::from_le_bytes_mod_order` of the little-endian bytes: `u mod r`, in +/// Montgomery form), on the lanes. +#[target_feature(enable = "avx512f,avx512ifma")] +unsafe fn ratios_to_scalars(c: &Ctx, nums: &[L5], dens: &[L5], s: &mut Scratch, out: &mut [Fr]) { + let m = fr_mod(); + let n = nums.len(); + invert_block(c, dens, s); + for g in 0..n { + let u = canon(c, &mont_mul(c, &nums[g], &s.inv[g])); // plain, < p < 2^255 + let f = canon_r(c, m, &mont_mul_r(c, m, &u, &m.k)); // u · 2^256 mod r: Fr's representation + let w = limbs_to_words(&f); + let mut words = [[0u64; 8]; 4]; + for j in 0..4 { + _mm512_storeu_si512(words[j].as_mut_ptr() as *mut _, w[j]); + } + for l in 0..8 { + out[8 * g + l] = + Fr::new_unchecked(BigInt([words[0][l], words[1][l], words[2][l], words[3][l]])); + } + } +} + +/// `Element::batch_map_to_scalar_field` on the lanes; `elements.len()` is a multiple of eight. +/// +/// # Safety +/// `available()` must have returned true on this host. +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn batch_map_to_scalar_field(elements: &[Element], out: &mut [Fr]) { + debug_assert!(elements.len().is_multiple_of(8) && out.len() == elements.len()); + let c = context(); + let g = groups_of(elements.len()); + let mut s = Scratch::new(c, g); + let mut xs = vec![c.one; g]; + let mut ys = vec![c.one; g]; + for (blk, out_blk) in elements.chunks(BLOCK).zip(out.chunks_mut(BLOCK)) { + let n = blk.len() / 8; + for g in 0..n { + // x/y = (X/Z)/(Y/Z) = X/Y: the projective coordinates directly, Z unused + let (x, y, _) = load_xyz(c, blk.as_ptr().add(8 * g)); + xs[g] = x; + ys[g] = y; + } + ratios_to_scalars(c, &xs[..n], &ys[..n], &mut s, out_blk); + } +} + +/// `AffineElement::batch_map_to_scalar_field` on the lanes; `elements.len()` is a multiple of eight. The +/// 64-byte affine records (`x` then `y`, Montgomery words) are read as `hash_commitments` reads plain +/// bytes: `x/y` is a ratio, the common factor cancels. +/// +/// # Safety +/// `available()` must have returned true on this host. +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn batch_map_to_scalar_field_affine(elements: &[AffineElement], out: &mut [Fr]) { + debug_assert!(elements.len().is_multiple_of(8) && out.len() == elements.len()); + let c = context(); + let g = groups_of(elements.len()); + let mut s = Scratch::new(c, g); + let mut xs = vec![c.one; g]; + let mut ys = vec![c.one; g]; + for (blk, out_blk) in elements.chunks(BLOCK).zip(out.chunks_mut(BLOCK)) { + let n = blk.len() / 8; + for g in 0..n { + let (x, y) = load_xy_bytes(c, blk.as_ptr().add(8 * g) as *const [u8; 64]); + xs[g] = x; + ys[g] = y; + } + ratios_to_scalars(c, &xs[..n], &ys[..n], &mut s, out_blk); + } +} + +/// `Element::hash_commitments` on the lanes; `commitments.len()` is a multiple of eight. +/// +/// # Safety +/// `available()` must have returned true on this host. +#[target_feature(enable = "avx512f,avx512ifma")] +pub(crate) unsafe fn hash_commitments(commitments: &[[u8; 64]], out: &mut [Fr]) { + debug_assert!(commitments.len().is_multiple_of(8) && out.len() == commitments.len()); + let c = context(); + let g = groups_of(commitments.len()); + let mut s = Scratch::new(c, g); + let mut xs = vec![c.one; g]; + let mut ys = vec![c.one; g]; + for (blk, out_blk) in commitments.chunks(BLOCK).zip(out.chunks_mut(BLOCK)) { + for bytes in blk { + // Unchecked point decoding still rejects noncanonical field coordinates. + // Validate before raw lane loads, without a Montgomery conversion. + for coordinate in bytes.chunks_exact(32) { + let words = core::array::from_fn(|i| { + u64::from_le_bytes(coordinate[i * 8..i * 8 + 8].try_into().unwrap()) + }); + assert!( + BigInt::<4>(words) < Fq::MODULUS, + "noncanonical commitment coordinate" + ); + } + #[cfg(debug_assertions)] + let _ = Element::from_bytes_unchecked_uncompressed(*bytes); + } + let n = blk.len() / 8; + for g in 0..n { + // plain x, y read as the lane form of x/R, y/R: the ratio is x/y all the same + let (x, y) = load_xy_bytes(c, blk.as_ptr().add(8 * g)); + xs[g] = x; + ys[g] = y; + } + ratios_to_scalars(c, &xs[..n], &ys[..n], &mut s, out_blk); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::element::{ + batch_map_to_scalar_field_affine_scalar, batch_map_to_scalar_field_scalar, + batch_to_commitments_scalar, hash_commitments_scalar, + }; + use ark_ec::CurveGroup; + use ark_ed_on_bls12_381_bandersnatch::{EdwardsAffine, EdwardsProjective, Fq}; + use ark_ff::{One, UniformRand, Zero}; + use rand_chacha::rand_core::SeedableRng; + + fn points(n: usize, seed: u64) -> Vec { + let mut rng = rand_chacha::ChaCha20Rng::seed_from_u64(seed); + let mut v: Vec = (0..n) + .map(|_| Element(EdwardsProjective::rand(&mut rng))) + .collect(); + if n > 0 { + v[0] = Element::zero(); + } + if n > 1 { + // a point with negative y, projective with z ≠ 1 (a sum keeps z generic) + let g = Element::prime_subgroup_generator(); + let a = EdwardsAffine::from(v[1].0); + let neg_y = if crate::element::is_positive(a.y) { + EdwardsAffine::new_unchecked(-a.x, -a.y) + } else { + a + }; + v[1] = Element(EdwardsProjective::from(neg_y) + g.0 - g.0); + } + if n > 2 { + // an ark-projective with z = 0 (not a valid point; ark's batch inversion leaves its inverse 0) + v[2] = Element(EdwardsProjective::new_unchecked( + Fq::from(7u64), + Fq::from(9u64), + Fq::zero(), + Fq::zero(), + )); + } + v + } + + #[test] + fn lane_batch_to_commitments_matches_scalar() { + if !available() { + return; + } + for n in [8usize, 16, 64, 2048, 2056, 4096 + 24] { + let pts = points(n, n as u64); + let want = batch_to_commitments_scalar(&pts); + let mut got = vec![[0u8; 64]; n]; + unsafe { batch_to_commitments(&pts, &mut got) }; + assert_eq!(got, want, "n = {n}"); + } + } + + /// The affine kernel reads the 64-byte Montgomery records and gives the projective kernel's and + /// the scalar path's values: the identity, a point with negative `y`, `y = 0` (masked, zero), random + /// points, at every batch length modulo the block. + #[test] + fn lane_batch_map_to_scalar_field_affine_matches_scalar() { + if !available() { + return; + } + for n in [8usize, 16, 64, 2048, 2056, 4096 + 24] { + let mut pts = points(n, n as u64 + 1); + if n > 2 { + pts[2] = Element(EdwardsProjective::new_unchecked( + Fq::from(7u64), + Fq::zero(), + Fq::zero(), + Fq::one(), + )); + } + let affine: Vec = pts + .iter() + .map(|p| AffineElement(EdwardsAffine::new_unchecked(p.0.x / p.0.z, p.0.y / p.0.z))) + .collect(); + let want = batch_map_to_scalar_field_affine_scalar(&affine); + let mut got = vec![Fr::zero(); n]; + unsafe { batch_map_to_scalar_field_affine(&affine, &mut got) }; + assert_eq!(got, want, "n = {n}"); + // and the projective kernel on the same points + let mut proj = vec![Fr::zero(); n]; + let as_elements: Vec = affine.iter().map(|a| a.to_element()).collect(); + unsafe { batch_map_to_scalar_field(&as_elements, &mut proj) }; + assert_eq!(proj, want, "n = {n} (projective)"); + } + } + + #[test] + fn lane_batch_map_to_scalar_field_matches_scalar() { + if !available() { + return; + } + for n in [8usize, 24, 2048, 2056, 4096 + 8] { + let pts = points(n, 100 + n as u64); + let want = batch_map_to_scalar_field_scalar(&pts); + let mut got = vec![Fr::zero(); n]; + unsafe { batch_map_to_scalar_field(&pts, &mut got) }; + assert_eq!(got, want, "n = {n}"); + } + } + + #[test] + fn lane_hash_commitments_matches_scalar() { + if !available() { + return; + } + for n in [8usize, 40, 2048, 2064] { + let mut pts = points(n, 200 + n as u64); + pts[2] = pts[3]; // the z = 0 record has no affine form; the zero denominator is tested below + let bytes: Vec<[u8; 64]> = pts + .iter() + .map(|e| { + let a = e.0.into_affine(); + let mut b = [0u8; 64]; + b[..32].copy_from_slice(&crate::element::fq_to_le_bytes(a.x)); + b[32..].copy_from_slice(&crate::element::fq_to_le_bytes(a.y)); + b + }) + .collect(); + let want = hash_commitments_scalar(&bytes); + let mut got = vec![Fr::zero(); n]; + unsafe { hash_commitments(&bytes, &mut got) }; + assert_eq!(got, want, "n = {n}"); + } + // Unchecked release-mode parsing accepts canonical off-curve records; debug + // parsing rejects them, exactly as the scalar reference does. + #[cfg(not(debug_assertions))] + { + let mut recs = vec![[0u8; 64]; 8]; + let g = Element::prime_subgroup_generator().0.into_affine(); + for r in recs.iter_mut() { + r[..32].copy_from_slice(&crate::element::fq_to_le_bytes(g.x)); + r[32..].copy_from_slice(&crate::element::fq_to_le_bytes(g.y)); + } + recs[5][..32].copy_from_slice(&crate::element::fq_to_le_bytes(Fq::from(7u64))); + recs[5][32..].fill(0); + let mut got = vec![Fr::zero(); 8]; + unsafe { hash_commitments(&recs, &mut got) }; + let g_scalar = Element::prime_subgroup_generator().map_to_scalar_field(); + for (i, v) in got.iter().enumerate() { + assert_eq!(*v, if i == 5 { Fr::zero() } else { g_scalar }, "lane {i}"); + } + } + } + /// The scalar path against the lanes on the trie update's shapes (a level's changed nodes). + /// `cargo test --release -p banderwagon bench_normalize -- --ignored --nocapture` + #[test] + #[ignore] + fn bench_normalize() { + if !available() { + return; + } + for n in [64usize, 512, 4096, 32768] { + let pts = points(n, 7); + let bytes = batch_to_commitments_scalar(&pts); + let reps = (1 << 18) / n; + let time = |f: &dyn Fn()| { + f(); + let t0 = std::time::Instant::now(); + for _ in 0..reps { + f(); + } + t0.elapsed().as_nanos() as f64 / (reps * n) as f64 + }; + let s1 = time(&|| { + std::hint::black_box(batch_to_commitments_scalar(&pts)); + }); + let l1 = time(&|| { + std::hint::black_box(Element::batch_to_commitments(&pts)); + }); + let s2 = time(&|| { + std::hint::black_box(batch_map_to_scalar_field_scalar(&pts)); + }); + let l2 = time(&|| { + std::hint::black_box(Element::batch_map_to_scalar_field(&pts)); + }); + let s3 = time(&|| { + std::hint::black_box(hash_commitments_scalar(&bytes)); + }); + let l3 = time(&|| { + std::hint::black_box(Element::hash_commitments(&bytes)); + }); + println!( + "n={n}: to_commitments scalar {s1:.0} lanes {l1:.0} ns/pt x{:.1}; map_to_scalar scalar {s2:.0} lanes {l2:.0} x{:.1}; hash_commitments scalar {s3:.0} lanes {l3:.0} x{:.1}", + s1 / l1, + s2 / l2, + s3 / l3 + ); + } + } +} diff --git a/banderwagon/src/lib.rs b/banderwagon/src/lib.rs index fc52250b..0a3d78d2 100644 --- a/banderwagon/src/lib.rs +++ b/banderwagon/src/lib.rs @@ -11,9 +11,11 @@ mod element; mod ifma; #[cfg(all(feature = "std", target_arch = "x86_64"))] mod ifma_msm; +#[cfg(all(feature = "std", target_arch = "x86_64"))] +mod ifma_normalize; pub use ark_ed_on_bls12_381_bandersnatch::Fq; use ark_ff::BigInteger256; -pub use element::{multi_scalar_mul, Element, Fr}; +pub use element::{multi_scalar_mul, multi_scalar_mul_affine, AffineElement, Element, Fr}; pub mod salt_committer; mod scalar_multi_asm; diff --git a/ipa-multipoint/src/multiproof.rs b/ipa-multipoint/src/multiproof.rs index 51f59d65..d49f66d7 100644 --- a/ipa-multipoint/src/multiproof.rs +++ b/ipa-multipoint/src/multiproof.rs @@ -9,7 +9,10 @@ use crate::math_utils::powers_of_par; use crate::transcript::Transcript; use crate::transcript::TranscriptProtocol; -use banderwagon::{multi_scalar_mul, salt_committer::Committer, trait_defs::*, Element, Fr}; +use banderwagon::{ + multi_scalar_mul, multi_scalar_mul_affine, salt_committer::Committer, trait_defs::*, + AffineElement, Element, Fr, +}; use hashbrown::HashMap; use rustc_hash::FxBuildHasher; type FxHashMap = HashMap; @@ -49,6 +52,21 @@ pub struct VerifierQuery { pub result: Fr, } +/// A verifier query whose commitment is an index into a shared slice of commitments +/// (`MultiPointProof::check_indexed`), for callers whose queries open a small set of +/// polynomials at many points each: a trie witness opens each node's polynomial at every +/// child or slot the proof touches, so a `VerifierQuery` per opening carries the node's +/// 128-byte commitment once per opening — the same point copied into every query, then copied +/// again into the multiexp's point vector. Here the node's commitment is stored once, the +/// query is 72 bytes, and the multiexp runs over the commitment slice itself. +#[derive(Clone, Debug, PartialEq)] +pub struct IndexedQuery { + /// Index into the `commitments` slice handed to `check_indexed`. + pub commitment: u32, + pub point: Fr, + pub result: Fr, +} + //XXX: change to group_prover_queries_by_point #[inline(always)] fn group_prover_queries<'a>( @@ -271,37 +289,84 @@ impl QueryData for VerifierQuery { } } +const BYTES_PER_QUERY: usize = 99; // 32 + 1 + 32 + 1 + 32 + 1 + +/// Queries per transcript chunk: the chunk is serialized in parallel into one buffer and the +/// buffer fed to the transcript's hash, so the transcript never holds more than this many +/// queries' bytes (≈ 1.6 MB) however many queries a proof has. A 16K chunk halves the +/// serialization-and-hash handoffs for large proofs while keeping memory bounded. +const TRANSCRIPT_CHUNK_QUERIES: usize = 16384; + +#[inline(always)] +fn write_query_bytes(chunk_res: &mut [u8], commitment: &[u8; 32], point: &Fr, result: &Fr) { + // Commitment + chunk_res[0] = b'C'; + chunk_res[1..33].copy_from_slice(commitment); + + // Point + chunk_res[33] = b'z'; + point + .serialize_compressed(&mut chunk_res[34..66]) + .expect("Failed to serialize point"); + + // Result + chunk_res[66] = b'y'; + result + .serialize_compressed(&mut chunk_res[67..99]) + .expect("Failed to serialize result"); +} + +/// Appends `n` queries' bytes to the transcript, `write(i, bytes)` producing query `i`'s +/// 99 bytes, chunk by chunk: each chunk is written in parallel, then hashed in order. +#[inline(always)] +fn record_query_bytes( + transcript: &mut Transcript, + n: usize, + write: impl Fn(usize, &mut [u8]) + Sync, +) { + let mut buffer = vec![0u8; n.min(TRANSCRIPT_CHUNK_QUERIES) * BYTES_PER_QUERY]; + let mut base = 0; + while base < n { + let len = (n - base).min(TRANSCRIPT_CHUNK_QUERIES); + let slice = &mut buffer[..len * BYTES_PER_QUERY]; + chunks_mut!(slice, BYTES_PER_QUERY) + .enumerate() + .for_each(|(j, chunk_res)| write(base + j, chunk_res)); + transcript.append_raw(slice); + base += len; + } +} + #[inline(always)] fn record_query_transcript(transcript: &mut Transcript, queries: &[T]) { - const BYTES_PER_QUERY: usize = 99; // 32 + 1 + 32 + 1 + 32 + 1 - let total_size = queries.len() * BYTES_PER_QUERY; - - let origin = transcript.state.len(); - transcript.state.resize(origin + total_size, 0); - - let state_slice = &mut transcript.state[origin..]; - - // Process chunks in parallel - chunks_mut!(state_slice, BYTES_PER_QUERY) - .zip(iter!(queries)) - .for_each(|(chunk_res, p)| { - // Commitment - chunk_res[0] = b'C'; - chunk_res[1..33].copy_from_slice(&p.commitment().to_bytes()); - - // Point - chunk_res[33] = b'z'; - let point_scalar = p.point_as_fr(); - point_scalar - .serialize_compressed(&mut chunk_res[34..66]) - .expect("Failed to serialize point"); - - // Result - chunk_res[66] = b'y'; - p.result() - .serialize_compressed(&mut chunk_res[67..99]) - .expect("Failed to serialize result"); - }); + record_query_bytes(transcript, queries.len(), |i, chunk_res| { + let p = &queries[i]; + write_query_bytes( + chunk_res, + &p.commitment().to_bytes(), + &p.point_as_fr(), + p.result(), + ) + }); +} + +/// The same transcript bytes as [`record_query_transcript`] for indexed queries: each +/// commitment is serialized once (`commitment_bytes`, parallel to `commitments`). +#[inline(always)] +fn record_indexed_query_transcript( + transcript: &mut Transcript, + commitment_bytes: &[[u8; 32]], + queries: &[IndexedQuery], +) { + record_query_bytes(transcript, queries.len(), |i, chunk_res| { + let q = &queries[i]; + write_query_bytes( + chunk_res, + &commitment_bytes[q.commitment as usize], + &q.point, + &q.result, + ) + }); } #[derive(Debug, Clone, PartialEq)] @@ -407,6 +472,74 @@ impl MultiPointProof { self.open_proof .verify_multiexp(transcript, crs, b, g3_comm, t, g2_t) } + + /// [`Self::check`] over indexed queries: the same transcript, the same challenges and the + /// same acceptance for the queries `(commitments[q.commitment], q.point, q.result)`, with + /// the multiexp over `commitments` itself — each commitment's scalar the sum of its + /// queries' `r^i / (t − z_i)` — instead of over one copied point per query. Returns + /// `false` for an index outside `commitments`. + pub fn check_indexed( + &self, + crs: &CRS, + precomp: &PrecomputedWeights, + commitments: &[AffineElement], + queries: &[IndexedQuery], + transcript: &mut Transcript, + ) -> bool { + if queries + .iter() + .any(|q| q.commitment as usize >= commitments.len()) + { + return false; + } + + transcript.domain_sep(b"multiproof"); + // 1. Compute `r` + // + // Add points and evaluations + let commitment_bytes: Vec<[u8; 32]> = iter!(commitments).map(|c| c.to_bytes()).collect(); + record_indexed_query_transcript(transcript, &commitment_bytes, queries); + drop(commitment_bytes); + + let r = transcript.challenge_scalar(b"r"); + + // 2. Compute `t` + transcript.append_point(b"D", &self.g_x_comm); + let t = transcript.challenge_scalar(b"t"); + + // 3. Compute g_2(t), and each commitment's scalar for [g_1(X)] + // + // helper_i = r^i / (t − z_i); g_2(t) = Σ helper_i · y_i; the multiexp scalar of a + // commitment is the sum of the helpers of the queries that open it. One serial pass: + // a handful of field operations per query, and the powers of r taken as it goes. + let mut g2_den: Vec<_> = queries.iter().map(|query| t - query.point).collect(); + + serial_batch_inversion_and_mul(&mut g2_den, &Fr::one()); + + let mut comm_scalars = vec![Fr::zero(); commitments.len()]; + let mut g2_t = Fr::zero(); + let mut r_i = Fr::one(); + for (query, den_inv) in queries.iter().zip(g2_den) { + let helper = den_inv * r_i; + g2_t += helper * query.result; + comm_scalars[query.commitment as usize] += helper; + r_i *= r; + } + + //4. Compute [g_1(X)] = E + let g1_comm = multi_scalar_mul_affine(commitments, &comm_scalars); + + transcript.append_point(b"E", &g1_comm); + + // E - D + let g3_comm = g1_comm - self.g_x_comm; + + // Check IPA + let b = LagrangeBasis::evaluate_lagrange_coefficients(precomp, crs.n, t); + + self.open_proof + .verify_multiexp(transcript, crs, b, g3_comm, t, g2_t) + } } #[cfg(test)] @@ -715,15 +848,104 @@ mod tests { let mut transcript = Transcript::new(b""); record_query_transcript(&mut transcript, &prover_queries); - let prover_state2 = transcript.state; + let prover_state2 = transcript.pending_digest(); assert_eq!(prover_state, prover_state2); let mut transcript = Transcript::new(b""); record_query_transcript(&mut transcript, &verifier_queries); - let verifier_state2 = transcript.state; + let verifier_state2 = transcript.pending_digest(); assert_eq!(verifier_state, verifier_state2); + + // Indexed queries over a shared commitment slice: the same bytes again, across chunk + // boundaries (more queries than one transcript chunk, each commitment opened at + // several points). + let (_, verifier_queries) = generate_test_queries(3 * TRANSCRIPT_CHUNK_QUERIES + 17); + let mut commitments: Vec = Vec::new(); + let indexed: Vec = verifier_queries + .iter() + .map(|q| { + let idx = match commitments.iter().position(|c| *c == q.commitment) { + Some(i) => i, + None => { + commitments.push(q.commitment); + commitments.len() - 1 + } + }; + IndexedQuery { + commitment: idx as u32, + point: q.point, + result: q.result, + } + }) + .collect(); + let bytes: Vec<[u8; 32]> = commitments.iter().map(|c| c.to_bytes()).collect(); + let mut transcript = Transcript::new(b""); + record_indexed_query_transcript(&mut transcript, &bytes, &indexed); + let mut expected = Transcript::new(b""); + record_query_transcript(&mut expected, &verifier_queries); + assert_eq!(transcript.pending_digest(), expected.pending_digest()); + } + + /// `check_indexed` accepts exactly what `check` accepts, on a proof whose queries open a + /// few polynomials at many points (the trie witness shape), and refuses a tampered result. + #[test] + fn check_indexed_matches_check() { + let mut rng = test_rng(); + let crs = CRS::new(32, b"random seed"); + let precomp = PrecomputedWeights::new(32); + + let polys: Vec = (0..5) + .map(|_| LagrangeBasis::new((0..32).map(|_| Fr::rand(&mut rng)).collect())) + .collect(); + let comms: Vec = polys.iter().map(|p| crs.commit_lagrange_poly(p)).collect(); + + let mut prover_queries = Vec::new(); + let mut indexed = Vec::new(); + for (i, poly) in polys.iter().enumerate() { + for point in (0..32).step_by(1 + i) { + let result = poly.evaluate_in_domain(point); + prover_queries.push(ProverQuery { + commitment: comms[i], + poly: poly.clone().into(), + point, + result, + }); + indexed.push(IndexedQuery { + commitment: i as u32, + point: Fr::from(point as u128), + result, + }); + } + } + let verifier_queries: Vec = + prover_queries.iter().cloned().map(Into::into).collect(); + + let mut prover_transcript = Transcript::new(b"test"); + let proof = MultiPoint::open( + crs.clone(), + &precomp, + &mut prover_transcript, + prover_queries, + ); + + let mut t1 = Transcript::new(b"test"); + assert!(proof.check(&crs, &precomp, &verifier_queries, &mut t1)); + let affine: Vec = comms.iter().map(|c| AffineElement::from(*c)).collect(); + let mut t2 = Transcript::new(b"test"); + assert!(proof.check_indexed(&crs, &precomp, &affine, &indexed, &mut t2)); + assert_eq!(t1.pending_digest(), t2.pending_digest()); + + let mut tampered = indexed.clone(); + tampered[3].result += Fr::one(); + let mut t3 = Transcript::new(b"test"); + assert!(!proof.check_indexed(&crs, &precomp, &affine, &tampered, &mut t3)); + + let mut out_of_range = indexed.clone(); + out_of_range[0].commitment = comms.len() as u32; + let mut t4 = Transcript::new(b"test"); + assert!(!proof.check_indexed(&crs, &precomp, &affine, &out_of_range, &mut t4)); } fn generate_test_queries(size: usize) -> (Vec, Vec) { @@ -765,8 +987,8 @@ mod tests { (prover_queries, verifier_queries) } - fn prover_query_transcript(queries: &[ProverQuery]) -> Vec { - let mut transcript = Transcript { state: Vec::new() }; + fn prover_query_transcript(queries: &[ProverQuery]) -> [u8; 32] { + let mut transcript = Transcript::new(b""); for query in queries.iter() { transcript.append_point(b"C", &query.commitment); @@ -776,17 +998,17 @@ mod tests { // It's just an index operation on the lagrange basis transcript.append_scalar(b"y", &query.result) } - transcript.state + transcript.pending_digest() } - fn verifier_query_transcript(queries: &[VerifierQuery]) -> Vec { - let mut transcript = Transcript { state: Vec::new() }; + fn verifier_query_transcript(queries: &[VerifierQuery]) -> [u8; 32] { + let mut transcript = Transcript::new(b""); for query in queries.iter() { transcript.append_point(b"C", &query.commitment); transcript.append_scalar(b"z", &query.point); transcript.append_scalar(b"y", &query.result); } - transcript.state + transcript.pending_digest() } } diff --git a/ipa-multipoint/src/transcript.rs b/ipa-multipoint/src/transcript.rs index a3a75e6a..533bbe5a 100644 --- a/ipa-multipoint/src/transcript.rs +++ b/ipa-multipoint/src/transcript.rs @@ -1,5 +1,4 @@ use banderwagon::{trait_defs::*, Element, Fr}; -use std::vec::Vec; pub trait TranscriptProtocol { /// Compute a `label`ed challenge variable. @@ -10,32 +9,47 @@ pub trait TranscriptProtocol { } use sha2::{Digest, Sha256}; + +/// The Fiat–Shamir transcript: every appended byte goes straight into a running SHA-256, and a +/// challenge is the digest of everything appended since the previous challenge. +/// +/// This used to buffer the appended bytes in a `Vec` and hash the buffer at each challenge. +/// The digest is the same either way (SHA-256 over the same byte sequence), but a witness of +/// tens of thousands of queries appended ≈ 8 MB of commitments and evaluations before its first +/// challenge, held for the whole verification, in every concurrent verification. pub struct Transcript { - pub state: Vec, + hasher: Sha256, } impl Transcript { pub fn new(label: &'static [u8]) -> Transcript { - // TODO: add a with capacity method, so we don't reallocate alot - let mut state = Vec::new(); - state.extend(label); - Transcript { state } + let mut hasher = Sha256::new(); + hasher.update(label); + Transcript { hasher } } fn append_message(&mut self, message: &[u8], label: &'static [u8]) { - self.state.extend(label); - self.state.extend(message); + self.hasher.update(label); + self.hasher.update(message); } // TODO: Add this to the other implementations! or most likely, we just need to add // TODO sub protocol specific domain separators ipa_domain_sep(n) and under the roof // TODO it adds the ipa label and the argument size n pub fn append_u64(&mut self, label: &'static [u8], number: u64) { - self.state.extend(label); - self.state.extend(number.to_be_bytes()); + self.hasher.update(label); + self.hasher.update(number.to_be_bytes()); } pub fn append_raw(&mut self, message: &[u8]) { - self.state.extend(message); + self.hasher.update(message); + } + + /// The digest of everything appended since the last challenge, without disturbing the + /// transcript — what `challenge_scalar` would hash next, for tests that compare two + /// transcripts' states. + #[cfg(test)] + pub(crate) fn pending_digest(&self) -> [u8; 32] { + self.hasher.clone().finalize().into() } } @@ -43,13 +57,8 @@ impl TranscriptProtocol for Transcript { fn challenge_scalar(&mut self, label: &'static [u8]) -> Fr { self.domain_sep(label); - // Hash entire transcript state - let mut sha256 = Sha256::new(); - sha256.update(&self.state); - let hash: Vec = sha256.finalize_reset().to_vec(); - - // Clear the state - self.state.clear(); + // Hash entire transcript state since the last challenge, and start afresh + let hash = self.hasher.finalize_reset(); let scalar = Fr::from_le_bytes_mod_order(&hash); @@ -70,10 +79,9 @@ impl TranscriptProtocol for Transcript { } fn domain_sep(&mut self, label: &'static [u8]) { - self.state.extend(label) + self.hasher.update(label) } } - #[cfg(test)] mod tests { use super::*; diff --git a/mutants/suppressions.toml b/mutants/suppressions.toml index 17560892..c2d7259c 100644 --- a/mutants/suppressions.toml +++ b/mutants/suppressions.toml @@ -86,9 +86,33 @@ reviewer = "full-run triage 2026-07-08 (issue #144)" kind = "line" category = "equivalent" file = "salt/src/proof/prover.rs" -mutant = "replace < with <= in create_leaf_node_queries" -justification = "parent_node == BUCKET_SLOT_ID_MASK is unreachable: main-trie ids are < 2^25 and subtree ids carry a data bucket id (>= 65536) in the top 24 bits, so they exceed the mask." -reviewer = "full-run triage 2026-07-08 (issue #144)" +mutant = "replace < with <= in SaltProof::create_indexed_queries" +justification = "parent_node == BUCKET_SLOT_ID_MASK is unreachable: main-trie ids are < 2^25 and subtree ids carry a data bucket id (>= 65536) in the top 24 bits, so they exceed the mask. (The same site was reviewed in create_leaf_node_queries, now test-only.)" +reviewer = "full-run triage 2026-07-08 (issue #144); moved with the code 2026-11-14" + +[[suppress]] +kind = "line" +category = "equivalent" +file = "salt/src/proof/prover.rs" +mutant = "canon required_node_ids.dedup(); ->" +justification = "The ids pushed are already distinct, so the dedup removes nothing. A node is pushed once per run of its id in the two sorted pair lists, whose node sets are disjoint, and connect_parent_id changes only a bridge id (a bucket root tagged with the bucket's level), mapping it to that bucket root. A bucket root is otherwise pushed only as the leaf of a one-level bucket, and a bucket has one level, so no two pushed ids are equal. If one ever repeated, dropping the dedup would make the comparison with the proof's strictly ascending ids fail, which rejects the proof." +reviewer = "compact witness storage port 2026-11-14" + +[[suppress]] +kind = "line" +category = "equivalent" +file = "salt/src/proof/prover.rs" +mutant = "replace match guard node == encode_node with false in SaltProof::create_indexed_queries" +justification = "The guard only reuses the commitment position found for the previous pair of the same node. With it false every pair looks its node up again and gets the same position (or the same error), so the queries are unchanged." +reviewer = "compact witness storage port 2026-11-14" + +[[suppress]] +kind = "line" +category = "equivalent" +file = "salt/src/proof/prover.rs" +mutant = "replace match guard node == parent_node with false in SaltProof::create_indexed_queries" +justification = "The guard only reuses the commitment position and segment start key computed for the previous pair of the same leaf. With it false every pair computes both again from the same node id and gets the same values, so the queries are unchanged." +reviewer = "compact witness storage port 2026-11-14" [[suppress]] kind = "line" diff --git a/salt/benches/parents_commitments_deser.rs b/salt/benches/parents_commitments_deser.rs index 12542df7..571cf202 100644 --- a/salt/benches/parents_commitments_deser.rs +++ b/salt/benches/parents_commitments_deser.rs @@ -6,8 +6,9 @@ //! `parents_commitments` map, the witness-decode hot path this crate parallelizes. Each //! commitment costs a modular sqrt (point decompression) plus a subgroup check in //! `Element::from_bytes`, and a proof carries one per path node, so this map dominates decode. -//! `sequential` is the derived per-element path; `parallel` is the overridden -//! [`parents_commitments_serde::deserialize`]. +//! `sequential` is the per-element path (a `BTreeMap` of `SerdeCommitment`, each through its own +//! deserializer); `parallel` is [`PathCommitments`]' deserializer (the overridden +//! [`parents_commitments_serde::deserialize`], the wire map read straight into its two vectors). //! - `salt_proof_codec` — full-`SaltProof` bincode `serialize` vs `deserialize` (deserialize uses //! the parallel override), showing the encode/decode asymmetry: encoding already-normalized //! commitments is cheap, decoding pays the per-point elliptic-curve work. @@ -21,7 +22,7 @@ use std::hint::black_box; use bincode::config::legacy; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; -use salt::proof::SerdeCommitment; +use salt::proof::{PathCommitments, SerdeCommitment}; use salt::{types::NodeId, SaltProof}; /// Directory of committed `SaltProof` fixtures, resolved relative to this crate at compile time. @@ -31,8 +32,7 @@ const FIXTURES_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/benches/fixture /// single-field bincode struct adds no framing, so it decodes the same bytes as a bare map. #[derive(serde::Deserialize)] struct ParallelMap { - #[serde(deserialize_with = "salt::proof::prover::parents_commitments_serde::deserialize")] - inner: BTreeMap, + inner: PathCommitments, } /// A loaded fixture: the parsed proof, its on-wire bytes, and its `parents_commitments` map bytes. diff --git a/salt/src/proof/mod.rs b/salt/src/proof/mod.rs index 6e143f1b..58f1ce11 100644 --- a/salt/src/proof/mod.rs +++ b/salt/src/proof/mod.rs @@ -76,7 +76,9 @@ pub mod witness; #[cfg(test)] mod test_utils; -pub use prover::{fx_hashmap_serde, SaltProof, SerdeCommitment, SerdeMultiPointProof}; +pub use prover::{ + fx_hashmap_serde, PathCommitments, SaltProof, SerdeCommitment, SerdeMultiPointProof, +}; pub use salt_witness::SaltWitness; pub use subtrie::NodePolyCache; pub use witness::Witness; diff --git a/salt/src/proof/prover.rs b/salt/src/proof/prover.rs index ea8f86d9..1ca84dc1 100644 --- a/salt/src/proof/prover.rs +++ b/salt/src/proof/prover.rs @@ -4,7 +4,7 @@ use crate::{ BUCKET_SLOT_ID_MASK, DOMAIN_SIZE, MAX_SUBTREE_LEVELS, NUM_META_BUCKETS, STARTING_NODE_ID, }, proof::{ - shape::{connect_parent_id, logic_parent_id, parents_and_points}, + shape::{connect_parent_id, logic_parent_id, sorted_parents_and_points}, subtrie::{create_sub_trie, METADATA_BUCKET_LEVEL}, ProofError, ProofResult, }, @@ -16,23 +16,29 @@ use crate::{ types::{hash_commitment, CommitmentBytes, NodeId, SaltKey, SaltValue}, BucketId, ScalarBytes, }; -use banderwagon::{Element, Fr}; +use banderwagon::{AffineElement, Element, Fr}; use ipa_multipoint::{ lagrange_basis::PrecomputedWeights, - multiproof::{MultiPoint, MultiPointProof, VerifierQuery}, + multiproof::{IndexedQuery, MultiPoint, MultiPointProof}, transcript::Transcript, }; use crate::Lazy; use salt_macros::prelude::*; -use salt_macros::{chunks, iter, sort_unstable, thread_chunk_size}; +use salt_macros::{chunks, chunks_mut, reduce, sort_unstable}; use serde::{ de::{Error as _, MapAccess, Visitor}, ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer, }; -use std::collections::{BTreeMap, BTreeSet}; -use std::{format, string::ToString, vec::Vec}; +use std::collections::BTreeMap; +use std::{format, string::ToString, vec, vec::Vec}; +#[cfg(test)] +use { + crate::proof::shape::parents_and_points, + salt_macros::{iter, thread_chunk_size}, + std::collections::BTreeSet, +}; use core::fmt; use hashbrown::HashMap; @@ -43,16 +49,20 @@ type FxHashMap = HashMap; pub static PRECOMPUTED_WEIGHTS: Lazy = Lazy::new(|| PrecomputedWeights::new(DOMAIN_SIZE)); -/// Serde wrapper for banderwagon `Element` with validation and compression. +/// Serde wrapper for a banderwagon commitment with validation and compression. /// /// This type ensures security by validating elements during deserialization -/// via `Element::from_bytes()`. Once validated, the `Element` is stored directly, +/// via `Element::from_bytes()`. Once validated, the point is stored directly, in affine +/// coordinates ([`AffineElement`], 64 bytes: every decoded point has `Z = 1`, and every reader — +/// the verifier's multi-scalar multiplication, the transcript, the scalar-field map, the trie +/// update — takes affine coordinates or re-enters projective form with one multiplication), /// enabling efficient operations without repeated validation. /// /// Serialization compresses the 64-byte uncompressed format to 32 bytes for /// storage/transmission. -#[derive(Clone, Debug, PartialEq)] -pub struct SerdeCommitment(pub(crate) Element); +#[derive(Clone, Copy, Debug, PartialEq)] +#[repr(transparent)] +pub struct SerdeCommitment(pub(crate) AffineElement); impl SerdeCommitment { /// Returns the commitment as a 64-byte uncompressed representation. @@ -61,6 +71,22 @@ impl SerdeCommitment { pub fn as_bytes(&self) -> CommitmentBytes { self.0.to_bytes_uncompressed() } + + /// The commitment in projective form (`(x, y, x · y, 1)`). + pub fn to_element(&self) -> Element { + self.0.to_element() + } + + /// The affine point. + pub fn affine(&self) -> AffineElement { + self.0 + } +} + +impl From for SerdeCommitment { + fn from(e: Element) -> Self { + SerdeCommitment(AffineElement::from(e)) + } } impl Serialize for SerdeCommitment { @@ -74,12 +100,153 @@ impl<'de> Deserialize<'de> for SerdeCommitment { /// Deserializes from compressed (32-byte) format and validates the element. fn deserialize>(deserializer: D) -> Result { let bytes = <[u8; 32]>::deserialize(deserializer)?; - Element::from_bytes(bytes) + AffineElement::from_bytes(bytes) .map(Self) .map_err(|_| serde::de::Error::custom("invalid element bytes")) } } +/// The proof's node commitments: the node ids in ascending order and, beside them, each node's +/// commitment, so a commitment is found by binary search and named by its position — the position +/// the verifier's queries carry ([`SaltProof::create_indexed_queries`]) and the slice the verifier +/// hands its multi-scalar multiplication without a copy. A witness holds tens of thousands of these; +/// 72 bytes each here (a `BTreeMap` held each 128-byte projective point in a +/// map node). Wire format: a map of `NodeId` to 32-byte compressed point in key order, as before. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct PathCommitments { + ids: Vec, + commitments: Vec, +} + +impl PathCommitments { + pub fn new() -> Self { + Self::default() + } + + pub fn len(&self) -> usize { + self.ids.len() + } + + pub fn is_empty(&self) -> bool { + self.ids.is_empty() + } + + /// The position of `node_id`'s commitment. + pub fn position(&self, node_id: NodeId) -> Option { + self.ids.binary_search(&node_id).ok() + } + + pub fn get(&self, node_id: NodeId) -> Option<&SerdeCommitment> { + self.position(node_id).map(|i| &self.commitments[i]) + } + + pub fn contains_key(&self, node_id: NodeId) -> bool { + self.position(node_id).is_some() + } + + /// The node ids, ascending. + pub fn keys(&self) -> impl ExactSizeIterator + '_ { + self.ids.iter() + } + + /// The node ids, ascending. + pub fn ids(&self) -> &[NodeId] { + &self.ids + } + + /// The commitments in key order. + pub fn values(&self) -> impl ExactSizeIterator + '_ { + self.commitments.iter() + } + + /// The commitments in key order, as affine points (the verifier's bases). + pub fn points(&self) -> &[AffineElement] { + // SAFETY: `SerdeCommitment` is `repr(transparent)` over `AffineElement`. + unsafe { + core::slice::from_raw_parts( + self.commitments.as_ptr() as *const AffineElement, + self.commitments.len(), + ) + } + } + + pub fn iter(&self) -> impl ExactSizeIterator + '_ { + self.ids.iter().zip(self.commitments.iter()) + } + + /// Sets `node_id`'s commitment, returning the one it replaces. + pub fn insert( + &mut self, + node_id: NodeId, + commitment: SerdeCommitment, + ) -> Option { + match self.ids.binary_search(&node_id) { + Ok(i) => Some(core::mem::replace(&mut self.commitments[i], commitment)), + Err(i) => { + self.ids.insert(i, node_id); + self.commitments.insert(i, commitment); + None + } + } + } + + pub fn remove(&mut self, node_id: NodeId) -> Option { + let i = self.position(node_id)?; + self.ids.remove(i); + Some(self.commitments.remove(i)) + } + + /// From ids and commitments already in ascending key order without duplicates (checked in + /// debug builds); the general case is [`FromIterator`]. + fn from_sorted(ids: Vec, commitments: Vec) -> Self { + debug_assert_eq!(ids.len(), commitments.len()); + debug_assert!(ids.windows(2).all(|w| w[0] < w[1])); + Self { ids, commitments } + } +} + +impl FromIterator<(NodeId, SerdeCommitment)> for PathCommitments { + /// Any order; a later entry for the same id replaces the earlier one, as a map's `insert`. + fn from_iter>(iter: I) -> Self { + let mut entries: Vec<(NodeId, SerdeCommitment)> = iter.into_iter().collect(); + entries.sort_by_key(|(id, _)| *id); + let mut ids = Vec::with_capacity(entries.len()); + let mut commitments = Vec::with_capacity(entries.len()); + for (id, c) in entries { + if ids.last() == Some(&id) { + *commitments.last_mut().unwrap() = c; + } else { + ids.push(id); + commitments.push(c); + } + } + Self { ids, commitments } + } +} + +impl From<[(NodeId, SerdeCommitment); N]> for PathCommitments { + fn from(entries: [(NodeId, SerdeCommitment); N]) -> Self { + entries.into_iter().collect() + } +} + +impl Serialize for PathCommitments { + /// A map of `NodeId` to 32-byte compressed point, in ascending key order. + fn serialize(&self, serializer: S) -> Result { + let mut map = serializer.serialize_map(Some(self.ids.len()))?; + for (id, c) in self.iter() { + map.serialize_entry(id, c)?; + } + map.end() + } +} + +impl<'de> Deserialize<'de> for PathCommitments { + fn deserialize>(d: D) -> Result { + parents_commitments_serde::deserialize(d) + } +} + /// Serde wrapper for `MultiPointProof`. #[derive(Debug, Clone, PartialEq)] pub struct SerdeMultiPointProof(pub MultiPointProof); @@ -109,39 +276,81 @@ impl<'de> Deserialize<'de> for SerdeMultiPointProof { /// [`Element::from_bytes`] and a witness carries one per path node. Wire format is unchanged (a map /// of `NodeId` to a 32-byte compressed point); only deserialization is overridden, because /// serializing already-normalized commitments (`Z = 1`) is cheap and batch normalization measured -/// slower on real witnesses. +/// slower on real witnesses. The entries are read straight into the id and byte vectors (the wire +/// order is ascending; any other order is sorted, a repeated id keeping its last entry, as a map +/// would) and the points decoded in place, task by task, into the commitment vector. pub mod parents_commitments_serde { use super::*; - /// Points per decode task: four lane chunks of [`Element::from_bytes_batch`], so a task is + /// Points per decode task: four lane chunks of [`AffineElement::from_bytes_batch`], so a task is /// long enough (~1 ms) that scheduling it costs nothing against the exponentiations it carries. const DECODE_TASK: usize = 128; - pub fn deserialize<'de, D: Deserializer<'de>>( - d: D, - ) -> Result, D::Error> { - let raw = BTreeMap::::deserialize(d)?; - let (ids, bytes): (Vec, Vec<[u8; 32]>) = raw.into_iter().unzip(); + struct EntriesVisitor; + + impl<'de> Visitor<'de> for EntriesVisitor { + type Value = (Vec, Vec<[u8; 32]>); + + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("a map of node id to 32-byte commitment") + } + + fn visit_map>(self, mut map: A) -> Result { + // The declared length is untrusted. Cap the initial reservation; + // larger valid maps grow as their entries are read. + let n = map.size_hint().unwrap_or(0).min(65_536); + let mut ids: Vec = Vec::with_capacity(n); + let mut bytes: Vec<[u8; 32]> = Vec::with_capacity(n); + let mut sorted = true; + while let Some((id, b)) = map.next_entry::()? { + sorted &= ids.last().is_none_or(|last| *last < id); + ids.push(id); + bytes.push(b); + } + if !sorted { + let mut entries: Vec<(NodeId, [u8; 32])> = ids.into_iter().zip(bytes).collect(); + entries.sort_by_key(|(id, _)| *id); + ids = Vec::with_capacity(entries.len()); + bytes = Vec::with_capacity(entries.len()); + for (id, b) in entries { + if ids.last() == Some(&id) { + *bytes.last_mut().unwrap() = b; + } else { + ids.push(id); + bytes.push(b); + } + } + } + Ok((ids, bytes)) + } + } + + pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result { + let (ids, bytes) = d.deserialize_map(EntriesVisitor)?; // Decode in tasks of many points: on a host with AVX-512 IFMA the points of a task share // vector registers for their exponentiations; elsewhere this is `Element::from_bytes` per point. - let decoded: Result>, ()> = chunks!(bytes, DECODE_TASK) - .map(|task| { - Element::from_bytes_batch(task) - .into_iter() - .collect::, _>>() - .map_err(|_| ()) - }) - .collect(); - - decoded - .map_err(|()| serde::de::Error::custom("invalid element bytes")) - .map(|tasks| { - ids.into_iter() - .zip(tasks.into_iter().flatten()) - .map(|(id, e)| (id, SerdeCommitment(e))) - .collect() - }) + let mut commitments = vec![SerdeCommitment(AffineElement::zero()); bytes.len()]; + let invalid = reduce!( + chunks!(bytes, DECODE_TASK) + .zip(chunks_mut!(commitments, DECODE_TASK)) + .map(|(task, out): (&[[u8; 32]], &mut [SerdeCommitment])| { + let mut invalid = false; + for (o, r) in out.iter_mut().zip(AffineElement::from_bytes_batch(task)) { + match r { + Ok(p) => *o = SerdeCommitment(p), + Err(_) => invalid = true, + } + } + invalid + }), + || false, + |a, b| a | b + ); + if invalid { + return Err(serde::de::Error::custom("invalid element bytes")); + } + Ok(PathCommitments::from_sorted(ids, commitments)) } } @@ -149,8 +358,7 @@ pub mod parents_commitments_serde { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct SaltProof { /// the node id of nodes in the path => node commitment - #[serde(deserialize_with = "parents_commitments_serde::deserialize")] - pub parents_commitments: BTreeMap, + pub parents_commitments: PathCommitments, /// the IPA proof pub proof: SerdeMultiPointProof, @@ -295,11 +503,11 @@ impl SaltProof { data: &BTreeMap>, state_root: ScalarBytes, ) -> Result<(), ProofError> { - let queries = self.create_verifier_queries(data)?; + let (commitments, queries) = self.create_indexed_queries(data)?; let root = self .parents_commitments - .get(&0) + .get(0) .ok_or(ProofError::MissingRootCommitment)?; let trie_root = hash_commitment(root.as_bytes()); @@ -313,10 +521,12 @@ impl SaltProof { let mut transcript = Transcript::new(b"st"); - // call MultiPointProof::check to verify the proof - if self.proof.0.check( + // call MultiPointProof::check_indexed to verify the proof: the queries index the + // proof's commitments, held once, instead of each carrying a copy of its node's + if self.proof.0.check_indexed( &DEFAULT_CRS, &PRECOMPUTED_WEIGHTS, + commitments, &queries, &mut transcript, ) { @@ -351,66 +561,180 @@ impl SaltProof { /// # Errors /// /// * `ProofError::StateReadError` - If bucket level info or path commitments are inconsistent with the queried keys + #[cfg(test)] pub(crate) fn create_verifier_queries( &self, kvs: &BTreeMap>, - ) -> ProofResult> { + ) -> ProofResult> { + use ipa_multipoint::multiproof::VerifierQuery; + let (commitments, queries) = self.create_indexed_queries(kvs)?; + Ok(queries + .into_iter() + .map(|q| VerifierQuery { + commitment: commitments[q.commitment as usize].to_element(), + point: q.point, + result: q.result, + }) + .collect()) + } + + /// [`Self::create_verifier_queries`] in the form [`MultiPointProof::check_indexed`] takes: + /// the proof's commitments once, in `parents_commitments`' key order, and every query + /// naming its node's commitment by index. A node's polynomial is opened at every child or + /// slot the proof touches, so a query per opening that carried the node's 128-byte + /// commitment held each commitment once per opening; here it is held once. + pub(crate) fn create_indexed_queries( + &self, + kvs: &BTreeMap>, + ) -> ProofResult<(&[AffineElement], Vec)> { if kvs.is_empty() { return Err(ProofError::StateReadError { reason: "kvs is empty".to_string(), }); } - // Validates that bucket level information in the proof matches the queried keys. - let bucket_ids_from_keys: BTreeSet<_> = kvs.keys().map(|k| k.bucket_id()).collect(); - - let bucket_ids_from_proof: BTreeSet<_> = self.levels.keys().copied().collect(); - - if bucket_ids_from_proof != bucket_ids_from_keys { + // Validates that bucket level information in the proof matches the queried keys: the keys + // ascend, so their bucket ids do, and the two sets are equal when every distinct bucket + // id of the keys has a level and there are as many as the proof's levels. + let mut key_buckets = 0usize; + let mut unknown_bucket = false; + let mut previous_bucket = None; + for key in kvs.keys() { + let bucket_id = key.bucket_id(); + if previous_bucket != Some(bucket_id) { + previous_bucket = Some(bucket_id); + key_buckets += 1; + unknown_bucket |= !self.levels.contains_key(&bucket_id); + } + } + if unknown_bucket || key_buckets != self.levels.len() { return Err(ProofError::StateReadError { reason: "buckets_top_level in proof contains unknown bucket level info".to_string(), }); } // Analyze the SALT tree structure to determine which parent nodes need verification - // and what evaluation points (child indices or slot positions) are required - let keys_to_verify: Vec<_> = kvs.keys().copied().collect(); - let (internal_nodes, leaf_nodes) = parents_and_points(&keys_to_verify, &self.levels); - - // Convert logical parent IDs to their commitment storage IDs and validate - let required_node_ids: BTreeSet<_> = internal_nodes - .keys() - .chain(leaf_nodes.keys()) - .map(|node_id| connect_parent_id(*node_id)) - .collect(); - - // Validates that the proof contains and ONLY contains commitments for all required nodes, . - let proof_node_ids: BTreeSet<_> = self.parents_commitments.keys().copied().collect(); - if proof_node_ids != required_node_ids { + // and what evaluation points (child indices or slot positions) are required: sorted + // (node, position) pairs, the order the maps of `parents_and_points` are walked in. + let (keys_to_verify, values): (Vec, Vec<&Option>) = kvs.iter().unzip(); + let (internal_nodes, leaf_nodes) = sorted_parents_and_points(&keys_to_verify, &self.levels); + + // Convert logical parent IDs to their commitment storage IDs and validate that the proof + // contains and ONLY contains commitments for all required nodes. + let mut required_node_ids: Vec = Vec::with_capacity(self.parents_commitments.len()); + let mut previous_node = None; + for node_id in internal_nodes + .iter() + .map(|&(node_id, _)| node_id) + .chain(leaf_nodes.iter().map(|&(node_id, _, _)| node_id)) + { + if previous_node != Some(node_id) { + previous_node = Some(node_id); + required_node_ids.push(connect_parent_id(node_id)); + } + } + required_node_ids.sort_unstable(); + required_node_ids.dedup(); + if required_node_ids.as_slice() != self.parents_commitments.ids() { return Err(ProofError::StateReadError { reason: "path_commitments in proof contains unknown node commitment".to_string(), }); } - let internal_queries = - create_internal_node_queries(&internal_nodes, &self.parents_commitments)?; - let leaf_queries = create_leaf_node_queries(&leaf_nodes, &self.parents_commitments, kvs)?; + // The commitments once, in key order; a query names its node by position in this list. + let commitments = self.parents_commitments.points(); + let path_commitments = &self.parents_commitments; + let mut queries = Vec::with_capacity(internal_nodes.len() + leaf_nodes.len()); - let mut queries = internal_queries; - queries.extend(leaf_queries); + // Internal nodes: every child's commitment, in query order, mapped to the scalar field in + // one batch; each query then takes its child's scalar by position. + let children_commitments = internal_nodes + .iter() + .map(|&(encode_node, point)| { + let child_id = get_child_node(&logic_parent_id(encode_node), point as usize); + path_commitments.get(child_id).map(|c| c.0).ok_or_else(|| { + ProofError::StateReadError { + reason: format!("Missing commitment for node ID {child_id}"), + } + }) + }) + .collect::>>()?; + let children_frs = AffineElement::batch_map_to_scalar_field(&children_commitments); + let mut previous_parent = None; + for (&(encode_node, point), fr) in internal_nodes.iter().zip(children_frs) { + let commitment = match previous_parent { + Some((node, index)) if node == encode_node => index, + _ => { + let index = path_commitments.index_of(connect_parent_id(encode_node))?; + previous_parent = Some((encode_node, index)); + index + } + }; + queries.push(IndexedQuery { + commitment, + point: Fr::from(point as u64), + result: fr, + }); + } - Ok(queries) + // Leaf nodes: the slot's key is the one that produced the pair when the segment's start + // plus the position gives it back, and otherwise is looked up, as before. + let mut previous_leaf = None; + for &(parent_node, point, key_index) in &leaf_nodes { + let (commitment, salt_key_start) = match previous_leaf { + Some((node, index, start)) if node == parent_node => (index, start), + _ => { + let index = path_commitments.index_of(connect_parent_id(parent_node))?; + let start = if parent_node < BUCKET_SLOT_ID_MASK as NodeId { + let bucket_id = (parent_node - STARTING_NODE_ID[3] as NodeId) as BucketId; + SaltKey::from((bucket_id, 0)) + } else { + subtree_leaf_start_key(&parent_node) + }; + previous_leaf = Some((parent_node, index, start)); + (index, start) + } + }; + let salt_key = SaltKey(salt_key_start.0 + point as u64); + let salt_val = if keys_to_verify[key_index as usize] == salt_key { + values[key_index as usize] + } else { + kvs.get(&salt_key) + .ok_or_else(|| ProofError::StateReadError { + reason: format!("Missing key-value entry for salt_key: {salt_key:?}"), + })? + }; + queries.push(IndexedQuery { + commitment, + point: Fr::from(point as u64), + result: slot_to_field(salt_val), + }); + } + + Ok((commitments, queries)) + } +} + +impl PathCommitments { + /// The index of `node_id`'s commitment, or the same error a missing commitment gives. + fn index_of(&self, node_id: NodeId) -> ProofResult { + self.position(node_id) + .map(|i| i as u32) + .ok_or_else(|| ProofError::StateReadError { + reason: format!("Missing commitment for node ID {node_id}"), + }) } } /// Safely retrieves a commitment from the proof, returning an error when the commitment is missing. +#[cfg(test)] fn get_commitment_safe( - path_commitments: &BTreeMap, + path_commitments: &PathCommitments, node_id: NodeId, ) -> ProofResult { path_commitments - .get(&node_id) - .map(|c| c.0) + .get(node_id) + .map(|c| c.to_element()) .ok_or_else(|| ProofError::StateReadError { reason: format!("Missing commitment for node ID {node_id}"), }) @@ -430,10 +754,13 @@ fn get_commitment_safe( /// # Returns /// /// Vector of `VerifierQuery` objects for polynomial verification of internal node relationships +/// +/// The reference form of [`SaltProof::create_indexed_queries`]'s internal half, kept for its tests. +#[cfg(test)] fn create_internal_node_queries( internal_nodes: &BTreeMap>, - path_commitments: &BTreeMap, -) -> ProofResult> { + path_commitments: &PathCommitments, +) -> ProofResult> { // Distribute internal nodes across CPU threads for parallel processing let in_nodes: Vec<_> = internal_nodes.iter().collect(); let chunk_size = thread_chunk_size!(in_nodes.len()); @@ -445,7 +772,7 @@ fn create_internal_node_queries( children_commitments_to_scalars(nodes, path_commitments)?; // Step 2: PERFORMANCE CRITICAL - Batch convert commitments to field elements - let children_frs = Element::batch_map_to_scalar_field(&children_commitments); + let children_frs = AffineElement::batch_map_to_scalar_field(&children_commitments); let child_map: FxHashMap = children_ids.into_iter().zip(children_frs).collect(); @@ -454,9 +781,8 @@ fn create_internal_node_queries( .iter() .map(|(&encode_node, points)| { let mut queries = Vec::new(); - // Get parent node's polynomial commitment - let commitment = - get_commitment_safe(path_commitments, connect_parent_id(encode_node))?; + // Get parent node's polynomial commitment (by position in the list) + let commitment = path_commitments.index_of(connect_parent_id(encode_node))?; // Create one query per child for &point in points.iter() { @@ -468,7 +794,7 @@ fn create_internal_node_queries( reason: format!("Missing commitment for node ID {child_id}"), })?; - queries.push(VerifierQuery { + queries.push(IndexedQuery { commitment, // Parent polynomial commitment point: Fr::from(point as u64), // Child index (0-255) result: *fr, // Expected result: child's commitment as field element @@ -501,10 +827,11 @@ fn create_internal_node_queries( /// # Returns /// /// Tuple of (child_node_ids, child_commitments) ready for batch field conversion +#[cfg(test)] fn children_commitments_to_scalars( nodes: &[(&NodeId, &BTreeSet)], - path_commitments: &BTreeMap, -) -> ProofResult<(Vec, Vec)> { + path_commitments: &PathCommitments, +) -> ProofResult<(Vec, Vec)> { // Flatten nested structure: parent_nodes → child_indices → (child_id, commitment) let (children_ids, children_commitments): (Vec<_>, Vec<_>) = nodes .iter() @@ -516,9 +843,9 @@ fn children_commitments_to_scalars( let child_id = get_child_node(&logic_parent_id(encode_node), point); Ok(( child_id, - // Extract Element from SerdeCommitment + // Extract the affine point from SerdeCommitment path_commitments - .get(&child_id) + .get(child_id) .ok_or_else(|| ProofError::StateReadError { reason: format!("Missing commitment for node ID {child_id}"), })? @@ -562,17 +889,19 @@ fn children_commitments_to_scalars( /// # Returns /// /// Lazy iterator of `VerifierQuery` objects for bucket data verification +/// +/// The reference form of [`SaltProof::create_indexed_queries`]'s leaf half, kept for its tests. +#[cfg(test)] fn create_leaf_node_queries( leaf_nodes: &BTreeMap>, - path_commitments: &BTreeMap, + path_commitments: &PathCommitments, kvs: &BTreeMap>, -) -> ProofResult> { +) -> ProofResult> { // Process leaf nodes in parallel - each represents a data bucket let queries = iter!(leaf_nodes) .map(|(parent_node, evaluation_points)| { - // Get the polynomial commitment for this bucket - let commitment = - get_commitment_safe(path_commitments, connect_parent_id(*parent_node))?; + // Get the polynomial commitment for this bucket (by position in the list) + let commitment = path_commitments.index_of(connect_parent_id(*parent_node))?; // Calculate the starting SaltKey address for this bucket/segment let salt_key_start = if *parent_node < BUCKET_SLOT_ID_MASK as NodeId { @@ -601,7 +930,7 @@ fn create_leaf_node_queries( })?; // Create query: verify bucket_polynomial(slot_position) == stored_value - Ok(VerifierQuery { + Ok(IndexedQuery { commitment, // Bucket polynomial commitment point: Fr::from(point as u64), // Slot position within bucket (0-255) result: slot_to_field(salt_val), // Stored value converted to field element @@ -1203,6 +1532,200 @@ mod tests { let proof = SaltProof::create(state_updates.data.keys().copied(), &store).unwrap(); assert!(proof.check(&data, root_hash).is_ok()); + assert_indexed_queries_match_reference(&proof, &data); + } + + /// The queries of the maps-and-sets form, in its order: internal nodes, then leaves. + fn reference_indexed_queries( + proof: &SaltProof, + kvs: &BTreeMap>, + ) -> ProofResult> { + let keys: Vec<_> = kvs.keys().copied().collect(); + let (internal_nodes, leaf_nodes) = parents_and_points(&keys, &proof.levels); + let mut queries = + create_internal_node_queries(&internal_nodes, &proof.parents_commitments)?; + queries.extend(create_leaf_node_queries( + &leaf_nodes, + &proof.parents_commitments, + kvs, + )?); + Ok(queries) + } + + fn assert_indexed_queries_match_reference( + proof: &SaltProof, + kvs: &BTreeMap>, + ) { + let (commitments, queries) = proof.create_indexed_queries(kvs).unwrap(); + assert_eq!(commitments.len(), proof.parents_commitments.len()); + let reference = reference_indexed_queries(proof, kvs).unwrap(); + assert_eq!(queries.len(), reference.len()); + assert!(queries == reference); + } + + /// The sorted-vector query builder equals the maps-and-sets form, query for query and in + /// order (the transcript hashes them in order), on witnesses over many buckets, over an + /// expanded bucket (subtree levels and the bridge), and with absent keys; and it refuses what + /// the reference refuses. + #[test] + fn test_indexed_queries_match_reference_form() { + let mut rng = StdRng::seed_from_u64(9); + for n in [1usize, 2, 7, 64, 1000] { + let initial_kvs = (0..n) + .map(|_| (mock_data(&mut rng, 52), Some(mock_data(&mut rng, 32)))) + .collect::>(); + let mem_store = MemStore::new(); + let mut state = EphemeralSaltState::new(&mem_store); + let updates = state.update_fin(&initial_kvs).unwrap(); + mem_store.update_state(updates.clone()); + let (trie_root, trie_updates) = + StateRoot::new(&mem_store).update_fin(&updates).unwrap(); + mem_store.update_trie(trie_updates); + + let mut data: BTreeMap<_, _> = updates + .data + .iter() + .map(|(key, (_, new))| (*key, new.clone())) + .collect(); + for slot in [1, 2, 255] { + data.insert((16777215, slot).into(), None); + } + let proof = SaltProof::create(data.keys().copied(), &mem_store).unwrap(); + assert!(proof.check(&data, trie_root).is_ok()); + assert_indexed_queries_match_reference(&proof, &data); + + // A key the proof was not built for: both forms refuse, with the same error kind. + if let Some((&key, _)) = data.iter().next() { + let mut other = data.clone(); + let moved = SaltKey(key.0 ^ 1); + if !other.contains_key(&moved) { + let value = other.remove(&key).unwrap(); + other.insert(moved, value); + let new = proof.create_indexed_queries(&other).map(|(_, q)| q); + let reference = reference_indexed_queries(&proof, &other); + match (new, reference) { + (Ok(a), Ok(b)) => assert!(a == b), + (Err(a), Err(b)) => assert_eq!(format!("{a:?}"), format!("{b:?}")), + (a, b) => panic!("forms disagree: {:?} vs {:?}", a.is_ok(), b.is_ok()), + } + } + } + } + } + + /// A proof may declare one level for a bucket and be checked against a key beyond that + /// bucket's first segment. The pair such a key yields names the slot at the key's low byte + /// in the bucket root, which is another key's slot: the queries take that key's entry from + /// `kvs` and refuse when there is none, as the reference form does. They never fall back + /// to the value of the key that yielded the pair. + #[test] + fn indexed_queries_read_the_slot_a_pair_names() { + let bucket_id: BucketId = 70_000; + let far_key = SaltKey::from((bucket_id, 300)); + let near_key = SaltKey::from((bucket_id, 300 & 0xFF)); + let levels: FxHashMap = [(bucket_id, 1)].into_iter().collect(); + + let (internal_nodes, leaf_nodes) = parents_and_points(&[far_key], &levels); + let mut proof = crate::proof::salt_witness::create_mock_proof(); + proof.parents_commitments = internal_nodes + .keys() + .chain(leaf_nodes.keys()) + .map(|node| (connect_parent_id(*node), mock_commitment())) + .collect(); + proof.levels = levels; + + // Only the far key: its pair names the near key's slot, which `kvs` does not hold. + let mut kvs = BTreeMap::from([(far_key, Some(mock_salt_value()))]); + let refused = proof + .create_indexed_queries(&kvs) + .map(|(_, queries)| queries); + assert!( + matches!( + &refused, + Err(ProofError::StateReadError { reason }) + if reason.contains("Missing key-value entry") + ), + "{refused:?}" + ); + let reference = reference_indexed_queries(&proof, &kvs); + assert_eq!(format!("{refused:?}"), format!("{reference:?}")); + + // With the near key held too, the pair's query carries the near key's value. + let near_value = Some(SaltValue::new(&[3u8; 32], &[4u8; 32])); + kvs.insert(near_key, near_value.clone()); + let (_, queries) = proof.create_indexed_queries(&kvs).unwrap(); + assert!(queries == reference_indexed_queries(&proof, &kvs).unwrap()); + assert_eq!(queries.last().unwrap().result, slot_to_field(&near_value)); + } + + /// `PathCommitments` answers as the map it replaced: empty until an entry is inserted, + /// holding the ids inserted and no others, an insert under a held id replacing its entry. + #[test] + fn path_commitments_answer_as_a_map() { + let generator = Element::prime_subgroup_generator(); + let first = SerdeCommitment::from(generator * Fr::from(2u64)); + let second = SerdeCommitment::from(generator * Fr::from(3u64)); + + let mut commitments = PathCommitments::new(); + assert!(commitments.is_empty()); + assert_eq!(commitments.len(), 0); + assert!(!commitments.contains_key(7)); + + assert_eq!(commitments.insert(7, first), None); + assert_eq!(commitments.insert(3, second), None); + assert!(!commitments.is_empty()); + assert_eq!(commitments.ids(), &[3, 7]); + assert!(commitments.contains_key(7)); + assert!(!commitments.contains_key(5)); + assert_eq!(commitments.get(7), Some(&first)); + + assert_eq!(commitments.insert(7, second), Some(first)); + assert_eq!(commitments.len(), 2); + assert_eq!(commitments.remove(3), Some(second)); + assert_eq!(commitments.remove(3), None); + assert_eq!(commitments.ids(), &[7]); + } + + /// `PathCommitments` decodes a wire map as a `BTreeMap` of the same entries does: in key + /// order whatever the wire order, a repeated id keeping its last entry. + #[test] + fn path_commitments_decode_as_a_map_in_any_wire_order() { + // (node id, the multiple of the generator its commitment is) + let cases: [&[(NodeId, u64)]; 5] = [ + &[], + &[(1, 10), (3, 30), (5, 50)], + &[(5, 50), (3, 30), (1, 10)], + &[(5, 50), (5, 51)], + &[(5, 50), (1, 10), (5, 51), (3, 30)], + ]; + for case in cases { + let mut wire = (case.len() as u64).to_le_bytes().to_vec(); + for &(id, multiple) in case { + let point = Element::prime_subgroup_generator() * Fr::from(multiple); + wire.extend_from_slice(&id.to_le_bytes()); + wire.extend_from_slice(&point.to_bytes()); + } + + let (decoded, consumed): (PathCommitments, usize) = + bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap(); + assert_eq!(consumed, wire.len(), "{case:?}"); + let (reference, _): (BTreeMap, usize) = + bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap(); + assert_eq!(decoded.len(), reference.len(), "{case:?}"); + assert!(decoded.iter().eq(reference.iter()), "{case:?}"); + } + } + + /// A value that is not a map is refused with what was expected in its place. + #[test] + fn path_commitments_decode_names_the_expected_map() { + let error = serde_json::from_str::("7") + .unwrap_err() + .to_string(); + assert!( + error.contains("a map of node id to 32-byte commitment"), + "{error}" + ); } /// Tests successful commitment retrieval for existing node IDs. @@ -1395,7 +1918,7 @@ mod tests { let points2 = [2usize].into(); let nodes = [(&node1, &points1), (&node2, &points2)]; - let mut path_commitments = BTreeMap::new(); + let mut path_commitments = PathCommitments::new(); // Add commitments for child nodes that will be calculated let child1 = get_child_node(&logic_parent_id(node1), 0); let child2 = get_child_node(&logic_parent_id(node1), 1); @@ -1424,7 +1947,7 @@ mod tests { let node1 = 100u64; let points1 = [0usize].into(); let nodes = [(&node1, &points1)]; - let path_commitments = BTreeMap::new(); // Empty - missing child commitment + let path_commitments = PathCommitments::new(); // Empty - missing child commitment let result = children_commitments_to_scalars(&nodes, &path_commitments); @@ -1441,7 +1964,7 @@ mod tests { let mut internal_nodes = BTreeMap::new(); internal_nodes.insert(parent_node, child_points); - let mut path_commitments = BTreeMap::new(); + let mut path_commitments = PathCommitments::new(); // Add commitment for parent node path_commitments.insert(connect_parent_id(parent_node), mock_commitment()); @@ -1472,7 +1995,7 @@ mod tests { let mut leaf_nodes = BTreeMap::new(); leaf_nodes.insert(bucket_node, slot_points); - let mut path_commitments = BTreeMap::new(); + let mut path_commitments = PathCommitments::new(); path_commitments.insert(connect_parent_id(bucket_node), mock_commitment()); // Add key-value data for the slots @@ -1506,7 +2029,7 @@ mod tests { let mut leaf_nodes = BTreeMap::new(); leaf_nodes.insert(parent_node, [0usize, 255usize].into()); - let mut path_commitments = BTreeMap::new(); + let mut path_commitments = PathCommitments::new(); path_commitments.insert(connect_parent_id(parent_node), mock_commitment()); let mut kvs = BTreeMap::new(); @@ -1683,7 +2206,7 @@ mod tests { let mut leaf_nodes = BTreeMap::new(); leaf_nodes.insert(bucket_node, slot_points); - let mut path_commitments = BTreeMap::new(); + let mut path_commitments = PathCommitments::new(); path_commitments.insert(connect_parent_id(bucket_node), mock_commitment()); let kvs = BTreeMap::new(); // Empty - missing key-value data diff --git a/salt/src/proof/salt_witness.rs b/salt/src/proof/salt_witness.rs index bc332c21..5d30e9b2 100644 --- a/salt/src/proof/salt_witness.rs +++ b/salt/src/proof/salt_witness.rs @@ -292,7 +292,7 @@ impl TrieReader for SaltWitness { /// - Returns the commitment if the node is witnessed in the proof /// - Returns an error if the node is not included in the witness fn commitment(&self, node_id: NodeId) -> Result { - match self.proof.parents_commitments.get(&node_id) { + match self.proof.parents_commitments.get(node_id) { Some(commitment) => Ok(commitment.as_bytes()), None => Err(SaltError::NotInWitness { what: "Trie node" }), } @@ -682,7 +682,7 @@ mod tests { Some(BucketMeta::default()), vec![(0, None), (1, None)], ); - witness.proof.parents_commitments.remove(&ROOT_NODE_ID); + witness.proof.parents_commitments.remove(ROOT_NODE_ID); assert!(matches!( witness.state_root(), @@ -872,9 +872,9 @@ mod tests { // Build witness with two witnessed nodes let mut proof = create_mock_proof(); let gen = Element::prime_subgroup_generator(); - let c1 = SerdeCommitment(gen * Fr::from(1u64)); - let c2 = SerdeCommitment(gen * Fr::from(2u64)); - proof.parents_commitments = [(12345, c1.clone()), (67890, c2.clone())].into(); + let c1 = SerdeCommitment::from(gen * Fr::from(1u64)); + let c2 = SerdeCommitment::from(gen * Fr::from(2u64)); + proof.parents_commitments = [(12345, c1), (67890, c2)].into(); let witness = SaltWitness { kvs: BTreeMap::new(), diff --git a/salt/src/proof/shape.rs b/salt/src/proof/shape.rs index bf24cf5a..4e4f9613 100644 --- a/salt/src/proof/shape.rs +++ b/salt/src/proof/shape.rs @@ -188,6 +188,83 @@ pub(super) fn slot_position(key: &SaltKey) -> usize { (key.slot_id() & 0xFF) as usize } +/// The internal `(node, position)` pairs and the leaf `(node, position, key index)` triples of +/// [`sorted_parents_and_points`]. +pub(crate) type SortedParentsAndPoints = (Vec<(NodeId, u8)>, Vec<(NodeId, u8, u32)>); + +/// [`parents_and_points`] as two sorted, deduplicated vectors of `(node, position)` pairs, the +/// order a walk of the two maps visits them (node ascending, then position ascending), without +/// building a map and a set per key and folding them. A position is below 256 by construction +/// (`vc_position_in_parent` is taken modulo the trie width; a slot position is the low 8 bits). +/// +/// Each leaf pair carries the index in `salt_keys` of a key that produced it, so a caller +/// holding the keys in order reads the key's value by position instead of by a map lookup. +/// Consecutive keys in one bucket (or one bucket segment) share their main-trie (or subtree) +/// path, which is then pushed once; any order of `salt_keys` gives the same result. +pub(crate) fn sorted_parents_and_points( + salt_keys: &[SaltKey], + levels: &FxHashMap, +) -> SortedParentsAndPoints { + let mut internal_nodes: Vec<(NodeId, u8)> = Vec::with_capacity(salt_keys.len() * 2 + 8); + let mut slot_position_nodes: Vec<(NodeId, u8, u32)> = Vec::with_capacity(salt_keys.len()); + let mut previous_bucket = None; + let mut previous_leaf = None; + + for (index, salt_key) in salt_keys.iter().enumerate() { + let bucket_id = salt_key.bucket_id(); + let level = levels[&bucket_id]; + + // Phase 1: the bucket root's path through the main trie, once per run of one bucket. + // The main trie has a fixed depth, so the walk is exactly `MAIN_TRIE_LEVELS - 1` + // steps; bounding it by that count rather than by reaching the root keeps a corrupt + // parent id from spinning forever (as in `record_key_paths`). + if previous_bucket != Some(bucket_id) { + previous_bucket = Some(bucket_id); + let mut node = bucket_root_node_id(bucket_id); + for _ in 0..MAIN_TRIE_LEVELS - 1 { + let parent_node = get_parent_node(&node); + internal_nodes.push((parent_node, vc_position_in_parent(&node) as u8)); + node = parent_node; + } + debug_assert_eq!(node, ROOT_NODE_ID); + } + + // Phases 2 and 3: the bucket subtree's path and its bridge, once per run of one segment. + let leaf = subtree_leaf_for_key(salt_key); + if previous_leaf != Some((leaf, level)) { + previous_leaf = Some((leaf, level)); + let mut node = leaf; + let mut count = level; + while count > 2 { + let parent_node = get_parent_node(&node); + internal_nodes.push((parent_node, vc_position_in_parent(&node) as u8)); + node = parent_node; + count -= 1; + } + if count == 2 { + internal_nodes.push(( + encode_parent(bucket_root_node_id(bucket_id), level), + vc_position_in_parent(&node) as u8, + )); + } + } + + // Phase 4: the key's slot position in its segment. + let node = if level == 1 { + bucket_root_node_id(bucket_id) + } else { + leaf + }; + slot_position_nodes.push((node, (salt_key.slot_id() & 0xFF) as u8, index as u32)); + } + + internal_nodes.sort_unstable(); + internal_nodes.dedup(); + slot_position_nodes.sort_unstable(); + slot_position_nodes.dedup_by(|a, b| (a.0, a.1) == (b.0, b.1)); + (internal_nodes, slot_position_nodes) +} + /// Encodes bucket tree level information into a main trie node ID. /// /// SALT's dual addressing system requires bridging between the main trie and bucket @@ -403,6 +480,68 @@ mod tests { }); } + /// The sorted pairs are the maps' walk, pair for pair, for keys in any order, with + /// duplicates, over every level, and with slot ids that collide on one position of a + /// level-1 bucket; each leaf pair's key produces that pair. + #[test] + fn test_sorted_parents_and_points_equals_maps() { + let test_cases = [ + (0, 1, 0xFF), + (256, 1, 0xFF), + (65535, 1, 0xFFFF), + (65536, 1, 0xFF), + (65540, 2, 0xFFFF), + (1_000_000, 3, 0xFFFFFF), + (5_000_000, 4, 0xFFFFFFFF), + (16_777_215, 5, 0xFFFFFFFF), + ]; + let mut rng = StdRng::seed_from_u64(11); + for round in 0..20 { + let mut levels = FxHashMap::default(); + let mut salt_keys = Vec::new(); + for &(bucket_id, level, slot_mask) in &test_cases { + levels.insert(bucket_id, level); + for _ in 0..(1 + round * 3) { + let slot_id = rng.random::() & slot_mask; + salt_keys.push(SaltKey::from((bucket_id, slot_id))); + if rng.random::() < 40 { + salt_keys.push(SaltKey::from((bucket_id, slot_id))); + } + } + } + if round % 2 == 1 { + salt_keys.sort_unstable(); + } + + let (internal_map, slot_map) = parents_and_points(&salt_keys, &levels); + let (internal, slots) = sorted_parents_and_points(&salt_keys, &levels); + + let expected_internal: Vec<(NodeId, u8)> = internal_map + .iter() + .flat_map(|(&n, ps)| ps.iter().map(move |&p| (n, p as u8))) + .collect(); + let expected_slots: Vec<(NodeId, u8)> = slot_map + .iter() + .flat_map(|(&n, ps)| ps.iter().map(move |&p| (n, p as u8))) + .collect(); + assert_eq!(internal, expected_internal); + assert_eq!( + slots.iter().map(|&(n, p, _)| (n, p)).collect::>(), + expected_slots + ); + for &(node, position, index) in &slots { + let key = &salt_keys[index as usize]; + let level = levels[&key.bucket_id()]; + let key_node = if level == 1 { + bucket_root_node_id(key.bucket_id()) + } else { + subtree_leaf_for_key(key) + }; + assert_eq!((key_node, (key.slot_id() & 0xFF) as u8), (node, position)); + } + } + } + #[test] fn test_parents_and_points_deduplicates_duplicate_keys() { let bucket_id = crate::constant::NUM_META_BUCKETS as BucketId; diff --git a/salt/src/proof/subtrie.rs b/salt/src/proof/subtrie.rs index a150e1bb..038e59f4 100644 --- a/salt/src/proof/subtrie.rs +++ b/salt/src/proof/subtrie.rs @@ -22,7 +22,7 @@ use crate::{ proof::{ prover::slot_to_field, shape::{connect_parent_id, logic_parent_id, parents_and_points, slot_position}, - ProofError, ProofResult, SerdeCommitment, + PathCommitments, ProofError, ProofResult, SerdeCommitment, }, traits::{StateReader, TrieReader}, trie::{ @@ -37,7 +37,7 @@ use ipa_multipoint::{lagrange_basis::LagrangeBasis, multiproof::ProverQuery}; use salt_macros::prelude::*; use salt_macros::{chunks, iter, reduce, thread_chunk_size}; -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::BTreeSet; use std::{format, string::ToString, sync::Arc, vec, vec::Vec}; use hashbrown::HashMap; @@ -245,11 +245,7 @@ pub(crate) const METADATA_BUCKET_LEVEL: u8 = 1; const ROOT_LEVEL_CHILD_START: NodeId = 1; /// Information returned by the subtrie creation process -type SubTrieInfo = ( - Vec, - BTreeMap, - FxHashMap, -); +type SubTrieInfo = (Vec, PathCommitments, FxHashMap); /// 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 @@ -463,9 +459,9 @@ where }) .collect::>()? }; - let parents_commitments: BTreeMap = parents + let parents_commitments: PathCommitments = parents .iter() - .map(|parent| (parent.physical, SerdeCommitment(parent.commitment))) + .map(|parent| (parent.physical, SerdeCommitment::from(parent.commitment))) .collect(); // Step 5: Resolve the polynomial of every parent, internal nodes in parallel chunks and diff --git a/salt/src/proof/test_utils.rs b/salt/src/proof/test_utils.rs index 19207ebb..43f924e9 100644 --- a/salt/src/proof/test_utils.rs +++ b/salt/src/proof/test_utils.rs @@ -32,7 +32,7 @@ pub(crate) fn mock_data(rng: &mut StdRng, len: usize) -> Vec { /// /// Used in proof verification tests where actual commitment content doesn't matter. pub(crate) fn mock_commitment() -> SerdeCommitment { - SerdeCommitment(Element::prime_subgroup_generator() * Fr::from(42)) + SerdeCommitment::from(Element::prime_subgroup_generator() * Fr::from(42)) } /// Creates a mock SaltValue for testing. diff --git a/salt/tests/proof_allocation.rs b/salt/tests/proof_allocation.rs new file mode 100644 index 00000000..96fbada0 --- /dev/null +++ b/salt/tests/proof_allocation.rs @@ -0,0 +1,72 @@ +use salt::proof::PathCommitments; + +#[test] +fn hostile_96_byte_witness_returns_error() { + for n in [1u64 << 56, u64::MAX] { + // Empty key/value map, then a proof map with a false length and two entries. + let mut wire = 0u64.to_le_bytes().to_vec(); + wire.extend_from_slice(&n.to_le_bytes()); + for id in 0u64..2 { + wire.extend_from_slice(&id.to_le_bytes()); + wire.extend_from_slice(&[0u8; 32]); + } + assert_eq!(wire.len(), 96); + let result = std::panic::catch_unwind(|| { + bincode::serde::decode_from_slice::( + &wire, + bincode::config::legacy(), + ) + }); + assert!(result.is_ok(), "witness panic with length {n}"); + assert!(result.unwrap().is_err()); + let limited = std::panic::catch_unwind(|| { + bincode::serde::decode_from_slice::( + &wire, + bincode::config::legacy().with_limit::<1024>(), + ) + }); + assert!(limited.is_ok(), "limited witness panic with length {n}"); + assert!(limited.unwrap().is_err()); + } +} + +#[test] +fn fabricated_lengths_return_errors_without_panicking() { + for n in [0, 1, 65_535, 65_536, 65_537, 1u64 << 56, u64::MAX] { + let bytes = n.to_le_bytes(); + let result = std::panic::catch_unwind(|| { + bincode::serde::decode_from_slice::( + &bytes, + bincode::config::legacy(), + ) + }); + assert!(result.is_ok(), "panic with declared length {n}"); + assert!(result.unwrap().is_err()); + let limited = std::panic::catch_unwind(|| { + bincode::serde::decode_from_slice::( + &bytes, + bincode::config::legacy().with_limit::<1024>(), + ) + }); + assert!(limited.is_ok(), "panic with byte budget, length {n}"); + assert!(limited.unwrap().is_err()); + } +} + +#[test] +fn valid_map_larger_than_initial_cap_still_decodes() { + let n = 65_537u64; + let mut wire = n.to_le_bytes().to_vec(); + for id in 0..n { + wire.extend_from_slice(&id.to_le_bytes()); + // The zero-byte encoding is a valid identity commitment. + wire.extend_from_slice(&[0u8; 32]); + } + let (map, consumed) = + bincode::serde::decode_from_slice::(&wire, bincode::config::legacy()) + .unwrap(); + assert_eq!(map.len(), n as usize); + assert_eq!(consumed, wire.len()); + let encoded = bincode::serde::encode_to_vec(&map, bincode::config::legacy()).unwrap(); + assert_eq!(encoded, wire); +} diff --git a/salt/tests/proof_fixture_codec.rs b/salt/tests/proof_fixture_codec.rs index d4caafdc..4a131c67 100644 --- a/salt/tests/proof_fixture_codec.rs +++ b/salt/tests/proof_fixture_codec.rs @@ -1,14 +1,11 @@ use bincode::config::legacy; -use salt::{ - proof::{prover::parents_commitments_serde, SerdeCommitment}, - types::NodeId, - SaltProof, -}; +use salt::{proof::SerdeCommitment, types::NodeId, SaltProof}; use std::collections::BTreeMap; +/// The parent map decoded entry by entry through `SerdeCommitment`'s own deserializer (the scalar +/// path, one point at a time) — the reference the parallel, in-place decode is compared against. #[derive(serde::Deserialize)] struct ParentsCommitmentsFixture { - #[serde(deserialize_with = "parents_commitments_serde::deserialize")] inner: BTreeMap, } @@ -34,7 +31,7 @@ fn committed_proof_fixtures_decode_and_reencode_deterministically() { assert_eq!(consumed, bytes.len(), "{name} left trailing bytes"); assert!( - proof.parents_commitments.contains_key(&0), + proof.parents_commitments.contains_key(0), "{name} has no root commitment" ); assert!( @@ -76,9 +73,19 @@ fn committed_parent_commitment_maps_decode_through_parallel_path() { map_bytes.len(), "{name} parent map left trailing bytes" ); - assert_eq!( - decoded.inner, proof.parents_commitments, - "{name} parent map changed after optimized decode" - ); + assert_eq!(decoded.inner.len(), proof.parents_commitments.len()); + for ((id, c), (pid, pc)) in decoded.inner.iter().zip(proof.parents_commitments.iter()) { + assert_eq!( + (id, c), + (pid, pc), + "{name} parent map changed after optimized decode" + ); + assert_eq!(c.as_bytes(), pc.as_bytes()); + } + assert!(proof + .parents_commitments + .ids() + .windows(2) + .all(|w| w[0] < w[1])); } }