Skip to content

Commit

Permalink
Add precomputation length
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Jan 3, 2025
1 parent 0964f80 commit 6f7001c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions curve25519-dalek/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ impl VartimePrecomputedStraus {
}
}

pub fn len(&self) -> usize {
use crate::traits::VartimePrecomputedMultiscalarMul;

match self {
#[cfg(curve25519_dalek_backend = "simd")]
VartimePrecomputedStraus::Avx2(inner) => inner.len(),
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
VartimePrecomputedStraus::Avx512ifma(inner) => inner.len(),
VartimePrecomputedStraus::Scalar(inner) => inner.len(),
}
}

pub fn is_empty(&self) -> bool {
use crate::traits::VartimePrecomputedMultiscalarMul;

match self {
#[cfg(curve25519_dalek_backend = "simd")]
VartimePrecomputedStraus::Avx2(inner) => inner.is_empty(),
#[cfg(all(curve25519_dalek_backend = "simd", nightly))]
VartimePrecomputedStraus::Avx512ifma(inner) => inner.is_empty(),
VartimePrecomputedStraus::Scalar(inner) => inner.is_empty(),
}
}

pub fn optional_mixed_multiscalar_mul<I, J, K>(
&self,
static_scalars: I,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
}
}

fn len(&self) -> usize {
self.static_lookup_tables.len()
}

fn is_empty(&self) -> bool {
self.static_lookup_tables.is_empty()
}

fn optional_mixed_multiscalar_mul<I, J, K>(
&self,
static_scalars: I,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ pub mod spec {
}
}

fn len(&self) -> usize {
self.static_lookup_tables.len()
}

fn is_empty(&self) -> bool {
self.static_lookup_tables.is_empty()
}

fn optional_mixed_multiscalar_mul<I, J, K>(
&self,
static_scalars: I,
Expand Down
11 changes: 11 additions & 0 deletions curve25519-dalek/src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,14 @@ impl VartimePrecomputedMultiscalarMul for VartimeEdwardsPrecomputation {
Self(crate::backend::VartimePrecomputedStraus::new(static_points))
}

fn len(&self) -> usize {
self.0.len()
}

fn is_empty(&self) -> bool {
self.0.is_empty()
}

fn optional_mixed_multiscalar_mul<I, J, K>(
&self,
static_scalars: I,
Expand Down Expand Up @@ -2136,6 +2144,9 @@ mod test {

let precomputation = VartimeEdwardsPrecomputation::new(static_points.iter());

assert_eq!(precomputation.len(), 128);
assert!(!precomputation.is_empty());

let P = precomputation.vartime_mixed_multiscalar_mul(
&static_scalars,
&dynamic_scalars,
Expand Down
11 changes: 11 additions & 0 deletions curve25519-dalek/src/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,14 @@ impl VartimePrecomputedMultiscalarMul for VartimeRistrettoPrecomputation {
))
}

fn len(&self) -> usize {
self.0.len()
}

fn is_empty(&self) -> bool {
self.0.is_empty()
}

fn optional_mixed_multiscalar_mul<I, J, K>(
&self,
static_scalars: I,
Expand Down Expand Up @@ -1852,6 +1860,9 @@ mod test {

let precomputation = VartimeRistrettoPrecomputation::new(static_points.iter());

assert_eq!(precomputation.len(), 128);
assert!(!precomputation.is_empty());

let P = precomputation.vartime_mixed_multiscalar_mul(
&static_scalars,
&dynamic_scalars,
Expand Down
6 changes: 6 additions & 0 deletions curve25519-dalek/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ pub trait VartimePrecomputedMultiscalarMul: Sized {
I: IntoIterator,
I::Item: Borrow<Self::Point>;

/// Return the number of static points in the precomputation.
fn len(&self) -> usize;

/// Determine if the precomputation is empty.
fn is_empty(&self) -> bool;

/// Given `static_scalars`, an iterator of public scalars
/// \\(b_i\\), compute
/// $$
Expand Down

0 comments on commit 6f7001c

Please sign in to comment.