Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curve: add precomputation length #685

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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
10 changes: 10 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,8 @@ mod test {

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

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

let P = precomputation.vartime_mixed_multiscalar_mul(
&static_scalars,
&dynamic_scalars,
Expand Down
10 changes: 10 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,8 @@ mod test {

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

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

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
Loading