Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions banderwagon/src/element.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ark_ec::{twisted_edwards::TECurveConfig, CurveGroup, PrimeGroup, ScalarMul, VariableBaseMSM};
use ark_ec::{twisted_edwards::TECurveConfig, CurveGroup, PrimeGroup, ScalarMul};
use ark_ed_on_bls12_381_bandersnatch::{BandersnatchConfig, EdwardsAffine, EdwardsProjective, Fq};
use ark_ff::{serial_batch_inversion_and_mul, Field, One, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
Expand Down Expand Up @@ -413,15 +413,26 @@ fn subgroup_check(point: &EdwardsProjective) -> bool {
}

pub fn multi_scalar_mul(bases: &[Element], scalars: &[Fr]) -> Element {
let bases_inner: Vec<_> = bases.iter().map(|element| element.0).collect();

// XXX: Converting all of these to affine hurts performance
let bases = EdwardsProjective::batch_convert_to_mul_base(&bases_inner);

let result = EdwardsProjective::msm(&bases, scalars)
.expect("number of bases should equal number of scalars");
assert_eq!(
bases.len(),
scalars.len(),
"number of bases should equal number of scalars"
);

// The MSM needs affine bases. Points loaded from storage or deserialized
// proofs already have `z = 1`, so skip the batch inversion entirely in
// that common case.
let bases: Vec<EdwardsAffine> = if bases.iter().all(|element| element.0.z.is_one()) {
bases
.iter()
.map(|element| EdwardsAffine::new_unchecked(element.0.x, element.0.y))
.collect()
} else {
let bases_inner: Vec<_> = bases.iter().map(|element| element.0).collect();
EdwardsProjective::batch_convert_to_mul_base(&bases_inner)
};

Element(result)
Element(crate::msm::msm_windowed(&bases, scalars))
}

/// Multiplies an `Element` by a scalar field element.
Expand Down
245 changes: 241 additions & 4 deletions banderwagon/src/msm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,154 @@
use crate::Element;
use ark_ec::scalar_mul::wnaf::WnafContext;
use ark_ed_on_bls12_381_bandersnatch::{EdwardsProjective, Fr};
use ark_ff::Zero;
use ark_ed_on_bls12_381_bandersnatch::{EdwardsAffine, EdwardsProjective, Fr};
use ark_ff::{AdditiveGroup, PrimeField, Zero};
use core::cmp::Ordering;

use salt_macros::iter;
use salt_macros::prelude::*;
use std::vec::Vec;
use salt_macros::{into_iter, iter};
use std::{vec, vec::Vec};

/// Window-parallel Pippenger multi-scalar multiplication over affine bases.
///
/// Mirrors the signed-digit bucket algorithm of arkworks' `msm_bigint_wnaf`,
/// but converts scalars and builds the digit matrix serially: at the MSM
/// sizes SALT handles (tens to a few thousand points), the parallel
/// conversion collects inside arkworks cost more in thread-pool traffic than
/// the entire digit pass. Windows are processed in parallel only when each
/// carries enough work to pay for the dispatch.
pub(crate) fn msm_windowed(bases: &[EdwardsAffine], scalars: &[Fr]) -> EdwardsProjective {
let size = bases.len();
let c = if size < 32 {
3
} else {
// ln_without_floats: log2(size) * ln(2), plus 2
(usize::BITS - (size - 1).leading_zeros()) as usize * 69 / 100 + 2
};
msm_windowed_with_c(bases, scalars, c)
}

/// [`msm_windowed`] with the window width given, so the tests can exercise every
/// width — and with it every bucket-count branch of [`bucket_len`] — at a size
/// that keeps a debug-mode run cheap.
fn msm_windowed_with_c(bases: &[EdwardsAffine], scalars: &[Fr], c: usize) -> EdwardsProjective {
debug_assert_eq!(bases.len(), scalars.len());
let size = bases.len();
if size == 0 {
return EdwardsProjective::zero();
}

let num_bits = <Fr as PrimeField>::MODULUS_BIT_SIZE as usize;
let digits_count = num_bits.div_ceil(c);

// Digit matrix: scalar `i` owns scalar_digits[i * digits_count..][..digits_count].
let mut scalar_digits = vec![0i64; size * digits_count];
for (scalar, digits) in scalars
.iter()
.zip(scalar_digits.chunks_exact_mut(digits_count))
{
make_digits(&scalar.into_bigint().0, c, digits);
}

let window_sum = |window: usize| -> EdwardsProjective {
let mut buckets =
vec![EdwardsProjective::zero(); bucket_len(c, num_bits, digits_count, window)];
for (digits, base) in scalar_digits.chunks_exact(digits_count).zip(bases) {
let digit = digits[window];
match 0.cmp(&digit) {
Ordering::Less => buckets[(digit - 1) as usize] += base,
Ordering::Greater => buckets[(-digit - 1) as usize] -= base,
Ordering::Equal => (),
}
}

let mut running_sum = EdwardsProjective::zero();
let mut res = EdwardsProjective::zero();
buckets.into_iter().rev().for_each(|b| {
running_sum += &b;
res += &running_sum;
});
res
};

// Below this size the per-window work is too small to farm out.
const MIN_PARALLEL_SIZE: usize = 64;
let window_sums: Vec<EdwardsProjective> = if size >= MIN_PARALLEL_SIZE {
into_iter!(0..digits_count).map(window_sum).collect()
} else {
(0..digits_count).map(window_sum).collect()
};

// Traverse windows from high to low, doubling `c` times between windows.
let lowest = *window_sums.first().expect("digits_count >= 1");
lowest
+ window_sums[1..]
.iter()
.rev()
.fold(EdwardsProjective::zero(), |mut total, sum_i| {
total += sum_i;
for _ in 0..c {
total.double_in_place();
}
total
})
}

/// Buckets one window of [`msm_windowed_with_c`] needs, which is half of the
/// window's `2^c` digit values for every window but the last.
///
/// [`make_digits`] recenters each digit to `[-2^(c-1), 2^(c-1) - 1]`, so a
/// bucket index `|digit| - 1` stays below `2^(c-1)` — the upper half of a
/// `2^c` array is never written, and reducing over it costs one full point
/// addition per empty bucket. The last window is the exception: it absorbs the
/// outstanding carry instead of being recentered, so its digits run
/// `0..=2^last_bits`, where `last_bits` is what the modulus leaves for it.
/// `last_bits == c` when `c` divides the modulus width (c = 11 and c = 23 at
/// 253 bits), and then the last window does need all `2^c` buckets.
fn bucket_len(c: usize, num_bits: usize, digits_count: usize, window: usize) -> usize {
if window == digits_count - 1 {
1 << (num_bits - (digits_count - 1) * c)
} else {
1 << (c - 1)
}
}

/// Decomposes a scalar into signed `w`-bit digits in `[-2^(w-1), 2^(w-1)]`,
/// written into `digits`. Identical digit semantics to arkworks'
/// `make_digits` (the final digit absorbs the outstanding carry).
fn make_digits(scalar: &[u64], w: usize, digits: &mut [i64]) {
let radix: u64 = 1 << w;
let window_mask: u64 = radix - 1;

let mut carry = 0u64;
let digits_count = digits.len();
for (i, digit_out) in digits.iter_mut().enumerate() {
// Construct a buffer of bits of the scalar, starting at `bit_offset`.
let bit_offset = i * w;
let u64_idx = bit_offset / 64;
let bit_idx = bit_offset % 64;
// Read the bits from the scalar
let bit_buf = if bit_idx < 64 - w || u64_idx == scalar.len() - 1 {
// This window's bits are contained in a single u64,
// or it's the last u64 anyway.
scalar[u64_idx] >> bit_idx
} else {
// Combine the current u64's bits with the bits from the next u64
(scalar[u64_idx] >> bit_idx) | (scalar[1 + u64_idx] << (64 - bit_idx))
};

// Read the actual coefficient value from the window
let coef = carry + (bit_buf & window_mask); // coef = [0, 2^w)

// Recenter coefficients from [0,2^w) to [-2^w/2, 2^w/2)
carry = (coef + radix / 2) >> w;
let mut digit = (coef as i64) - (carry << w) as i64;

if i == digits_count - 1 {
digit += (carry << w) as i64;
}
*digit_out = digit;
}
}

#[derive(Clone, Debug)]
pub struct MSMPrecompWnaf {
Expand Down Expand Up @@ -68,6 +211,100 @@ mod tests {
use crate::{multi_scalar_mul, Element};
use std::vec;

/// `size` random affine points and scalars, with the edge cases `0`, `r - 1` (which
/// maximizes the last window's digit, the one the last bucket count has to cover) and `1`
/// patched into the first scalars.
fn msm_input(seed: u8, size: usize) -> (Vec<EdwardsAffine>, Vec<Fr>) {
use ark_ec::CurveGroup;
use ark_ff::UniformRand;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha20Rng;

let mut rng = ChaCha20Rng::from_seed([seed; 32]);
let bases_proj: Vec<EdwardsProjective> = (0..size)
.map(|_| EdwardsProjective::rand(&mut rng))
.collect();
let bases = EdwardsProjective::normalize_batch(&bases_proj);
let mut scalars: Vec<Fr> = (0..size).map(|_| Fr::rand(&mut rng)).collect();
for (scalar, edge) in scalars
.iter_mut()
.zip([Fr::zero(), -Fr::from(1u64), Fr::from(1u64)])
{
*scalar = edge;
}
(bases, scalars)
}

/// The hand-rolled windowed MSM must agree with arkworks' generic MSM for
/// every size class (serial windows, parallel windows, small-c) and for
/// edge-case scalars.
#[test]
fn msm_windowed_matches_arkworks() {
use ark_ec::VariableBaseMSM;

for size in [1usize, 2, 16, 31, 32, 33, 129, 256, 1000] {
let (bases, scalars) = msm_input(9, size);
let expected = EdwardsProjective::msm(&bases, &scalars).unwrap();
let got = msm_windowed(&bases, &scalars);
assert_eq!(got, expected, "size {size}");
}

// Empty input yields the identity.
assert!(msm_windowed(&[], &[]).is_zero());
}

/// Every window width must agree with arkworks, not only the ones the size
/// heuristic happens to pick: [`bucket_len`] sizes the last window from the
/// bits the modulus leaves it, and `c = 11` — the width chosen for 8,193 to
/// 16,384 points — is the one where that is the full `2^c`.
#[test]
fn msm_windowed_matches_arkworks_for_every_window_width() {
use ark_ec::VariableBaseMSM;

let (bases, scalars) = msm_input(21, 200);
let expected = EdwardsProjective::msm(&bases, &scalars).unwrap();
for c in 3..=13 {
assert_eq!(
msm_windowed_with_c(&bases, &scalars, c),
expected,
"c = {c}"
);
}
}

/// The bucket counts [`bucket_len`] hands out must cover every digit
/// [`make_digits`] can produce: a bucket index is `|digit| - 1`, so every
/// window needs `|digit| <= bucket_len`.
#[test]
fn make_digits_stays_within_the_bucket_counts() {
use ark_ff::UniformRand;
use rand_chacha::rand_core::SeedableRng;
use rand_chacha::ChaCha20Rng;

let mut rng = ChaCha20Rng::from_seed([7u8; 32]);
let num_bits = <Fr as PrimeField>::MODULUS_BIT_SIZE as usize;
let mut scalars: Vec<Fr> = (0..64).map(|_| Fr::rand(&mut rng)).collect();
scalars.extend([Fr::zero(), Fr::from(1u64), -Fr::from(1u64)]);

for c in 3..=16 {
let digits_count = num_bits.div_ceil(c);
let mut digits = vec![0i64; digits_count];
for scalar in &scalars {
make_digits(&scalar.into_bigint().0, c, &mut digits);
for (window, &digit) in digits.iter().enumerate() {
let len = bucket_len(c, num_bits, digits_count, window) as i64;
assert!(
digit.abs() <= len,
"c = {c}, window {window}: digit {digit} needs more than {len} buckets"
);
if window == digits_count - 1 {
assert!(digit >= 0, "c = {c}: the last digit absorbs the carry");
}
}
}
}
}

#[test]
fn correctness_smoke_test() {
let mut crs = Vec::with_capacity(256);
Expand Down
24 changes: 23 additions & 1 deletion banderwagon/src/salt_committer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use ark_ed_on_bls12_381_bandersnatch::{EdwardsAffine, EdwardsProjective, Fq, Fr}
use ark_ff::PrimeField;
use ark_ff::Zero;

use salt_macros::iter;
use salt_macros::prelude::*;
use salt_macros::{chunks, iter, thread_chunk_size};
use std::{vec, vec::Vec};

/// Precomputed Multi-Scalar Multiplication engine for fixed base points.
Expand Down Expand Up @@ -187,6 +187,11 @@ impl Committer {
}
}

/// Returns the number of base points covered by the precomputed tables.
pub fn num_bases(&self) -> usize {
self.tables.len()
}

/// Multiplies a precomputed base point by a scalar using windowed NAF.
///
/// This x86_64-optimized version uses CPU prefetching instructions to
Expand Down Expand Up @@ -345,6 +350,23 @@ impl Committer {
/// * `result` - The projective point to update (in-place)
/// * `p2_x` - X-coordinate of the affine point to add
/// * `p2_y` - Y-coordinate of the affine point to add
impl Committer {
/// Sums `scalar · G[index]` over `terms`, splitting them across threads.
pub fn msm(&self, terms: &[(usize, Fr)]) -> Element {
chunks!(terms, thread_chunk_size!(terms.len()))
.map(|chunk| {
let mut acc = Element::zero();
for (index, scalar) in chunk {
if !scalar.is_zero() {
acc += self.mul_index(scalar, *index);
}
}
acc
})
.sum()
}
}

#[cfg(not(target_arch = "x86_64"))]
fn add_affine_point(result: &mut EdwardsProjective, p2_x: &Fq, p2_y: &Fq) {
use ark_ff::biginteger::BigInt;
Expand Down
2 changes: 1 addition & 1 deletion ipa-multipoint/benches/benchmarks/multipoint_prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let prover_query = ProverQuery {
commitment: poly_comm,
poly,
poly: poly.into(),
point,
result: y_i,
};
Expand Down
2 changes: 1 addition & 1 deletion ipa-multipoint/benches/benchmarks/multipoint_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {

let prover_query = ProverQuery {
commitment: poly_comm,
poly,
poly: poly.into(),
point,
result: y_i,
};
Expand Down
15 changes: 14 additions & 1 deletion ipa-multipoint/src/crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! approach, ensuring reproducibility and verifiability of the setup.

use crate::{default_crs, ipa::slow_vartime_multiscalar_mul, lagrange_basis::LagrangeBasis};
use banderwagon::{try_reduce_to_element, Element, SerializationError};
use banderwagon::{salt_committer::Committer, try_reduce_to_element, Element, SerializationError};
use std::{string::String, vec::Vec};
use thiserror::Error;

Expand Down Expand Up @@ -177,6 +177,19 @@ impl CRS {
///
/// # Returns
/// The elliptic curve point representing the polynomial commitment
/// Fixed-base tables over this CRS: `G` in order, then `Q` as base `n`. This is the shape
/// [`crate::multiproof::MultiPoint::open_with_committer`] and [`crate::ipa::create_with_precomp`]
/// take, so every MSM they compute — the IPA's blinding terms included — is fixed-base.
pub fn committer(&self, window_size: usize) -> Committer {
let bases: Vec<Element> = self
.G
.iter()
.copied()
.chain(core::iter::once(self.Q))
.collect();
Committer::new(&bases, window_size)
}

pub fn commit_lagrange_poly(&self, polynomial: &LagrangeBasis) -> Element {
slow_vartime_multiscalar_mul(polynomial.values().iter(), self.G.iter())
}
Expand Down
Loading
Loading