Skip to content

Commit ed83542

Browse files
curve: add precomputation length to MSM structs (#685)
1 parent 4570d80 commit ed83542

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

curve25519-dalek/src/backend/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,32 @@ impl VartimePrecomputedStraus {
128128
}
129129
}
130130

131+
/// Return the number of static points in the precomputation.
132+
pub fn len(&self) -> usize {
133+
use crate::traits::VartimePrecomputedMultiscalarMul;
134+
135+
match self {
136+
#[cfg(curve25519_dalek_backend = "simd")]
137+
VartimePrecomputedStraus::Avx2(inner) => inner.len(),
138+
#[cfg(all(curve25519_dalek_backend = "unstable_avx512", nightly))]
139+
VartimePrecomputedStraus::Avx512ifma(inner) => inner.len(),
140+
VartimePrecomputedStraus::Scalar(inner) => inner.len(),
141+
}
142+
}
143+
144+
/// Determine if the precomputation is empty.
145+
pub fn is_empty(&self) -> bool {
146+
use crate::traits::VartimePrecomputedMultiscalarMul;
147+
148+
match self {
149+
#[cfg(curve25519_dalek_backend = "simd")]
150+
VartimePrecomputedStraus::Avx2(inner) => inner.is_empty(),
151+
#[cfg(all(curve25519_dalek_backend = "unstable_avx512", nightly))]
152+
VartimePrecomputedStraus::Avx512ifma(inner) => inner.is_empty(),
153+
VartimePrecomputedStraus::Scalar(inner) => inner.is_empty(),
154+
}
155+
}
156+
131157
pub fn optional_mixed_multiscalar_mul<I, J, K>(
132158
&self,
133159
static_scalars: I,

curve25519-dalek/src/backend/serial/scalar_mul/precomputed_straus.rs

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
4646
}
4747
}
4848

49+
fn len(&self) -> usize {
50+
self.static_lookup_tables.len()
51+
}
52+
53+
fn is_empty(&self) -> bool {
54+
self.static_lookup_tables.is_empty()
55+
}
56+
4957
fn optional_mixed_multiscalar_mul<I, J, K>(
5058
&self,
5159
static_scalars: I,

curve25519-dalek/src/backend/vector/scalar_mul/precomputed_straus.rs

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ pub mod spec {
5757
}
5858
}
5959

60+
fn len(&self) -> usize {
61+
self.static_lookup_tables.len()
62+
}
63+
64+
fn is_empty(&self) -> bool {
65+
self.static_lookup_tables.is_empty()
66+
}
67+
6068
fn optional_mixed_multiscalar_mul<I, J, K>(
6169
&self,
6270
static_scalars: I,

curve25519-dalek/src/edwards.rs

+11
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,14 @@ impl VartimePrecomputedMultiscalarMul for VartimeEdwardsPrecomputation {
879879
Self(crate::backend::VartimePrecomputedStraus::new(static_points))
880880
}
881881

882+
fn len(&self) -> usize {
883+
self.0.len()
884+
}
885+
886+
fn is_empty(&self) -> bool {
887+
self.0.is_empty()
888+
}
889+
882890
fn optional_mixed_multiscalar_mul<I, J, K>(
883891
&self,
884892
static_scalars: I,
@@ -2136,6 +2144,9 @@ mod test {
21362144

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

2147+
assert_eq!(precomputation.len(), 128);
2148+
assert!(!precomputation.is_empty());
2149+
21392150
let P = precomputation.vartime_mixed_multiscalar_mul(
21402151
&static_scalars,
21412152
&dynamic_scalars,

curve25519-dalek/src/ristretto.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,14 @@ impl VartimePrecomputedMultiscalarMul for VartimeRistrettoPrecomputation {
10271027
))
10281028
}
10291029

1030+
fn len(&self) -> usize {
1031+
self.0.len()
1032+
}
1033+
1034+
fn is_empty(&self) -> bool {
1035+
self.0.is_empty()
1036+
}
1037+
10301038
fn optional_mixed_multiscalar_mul<I, J, K>(
10311039
&self,
10321040
static_scalars: I,
@@ -1852,6 +1860,9 @@ mod test {
18521860

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

1863+
assert_eq!(precomputation.len(), 128);
1864+
assert!(!precomputation.is_empty());
1865+
18551866
let P = precomputation.vartime_mixed_multiscalar_mul(
18561867
&static_scalars,
18571868
&dynamic_scalars,

curve25519-dalek/src/traits.rs

+6
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ pub trait VartimePrecomputedMultiscalarMul: Sized {
299299
I: IntoIterator,
300300
I::Item: Borrow<Self::Point>;
301301

302+
/// Return the number of static points in the precomputation.
303+
fn len(&self) -> usize;
304+
305+
/// Determine if the precomputation is empty.
306+
fn is_empty(&self) -> bool;
307+
302308
/// Given `static_scalars`, an iterator of public scalars
303309
/// \\(b_i\\), compute
304310
/// $$

0 commit comments

Comments
 (0)