Skip to content
This repository was archived by the owner on Jun 7, 2026. It is now read-only.

Commit d5ef277

Browse files
authored
Add WnafBase::multiscalar_mul (#14)
Computes a sum-of-products `aA + bB + ...` in variable time with w-NAF multi-exponentiation using the interleaved window method, also known as Straus' method. The key insight is that when computing this sum by means of additions and doublings, the doublings can be shared by performing the additions within an inner loop. The API and implementation are inspired in part by `curve25519-dalek`, namely the `VartimeMultiscalarMul` trait and corresponding implementation in `straus.rs`. This results in ~28% speedup on `p256` for a 3 scalar/point input: ProjectivePoint operations/point-scalar lincomb (variable-time) time: [149.13 µs 149.80 µs 150.84 µs] change: [−27.999% −27.645% −27.267%] (p = 0.00 < 0.05)
1 parent 2b828b9 commit d5ef277

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

src/wnaf.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,42 @@ pub(crate) fn wnaf_form<S: AsRef<[u8]>>(wnaf: &mut Vec<i64>, c: S, window: usize
157157
///
158158
/// This function must be provided a `table` and `wnaf` that were constructed with
159159
/// the same window size; otherwise, it may panic or produce invalid results.
160+
#[inline]
160161
pub(crate) fn wnaf_exp<G: Group>(table: &[G], wnaf: &[i64]) -> G {
161-
let mut result = G::identity();
162+
wnaf_multi_exp(&[table], &[wnaf])
163+
}
162164

165+
/// Performs w-NAF multi-exponentiation using the interleaved window method, also known as
166+
/// Straus' method.
167+
///
168+
/// The key insight is that when computing this sum by means of additions and doublings, the
169+
/// doublings can be shared by performing the additions within an inner loop.
170+
///
171+
/// This function must be provided with `tables` and `wnafs` that were constructed with
172+
/// the same window size; otherwise, it may panic or produce invalid results.
173+
pub(crate) fn wnaf_multi_exp<G: Group>(tables: &[&[G]], wnafs: &[&[i64]]) -> G {
174+
debug_assert_eq!(tables.len(), wnafs.len());
175+
let window_size = wnafs.iter().map(|w| w.len()).max().unwrap_or(0);
176+
177+
let mut result = G::identity();
163178
let mut found_one = false;
164179

165-
for n in wnaf.iter().rev() {
180+
for i in (0..window_size).rev() {
181+
// Only double once per iteration of the loop
166182
if found_one {
167183
result = result.double();
168184
}
169185

170-
if *n != 0 {
171-
found_one = true;
186+
for (&table, &wnaf) in tables.iter().zip(wnafs.iter()) {
187+
let n = wnaf.get(i).copied().unwrap_or(0);
188+
if n != 0 {
189+
found_one = true;
172190

173-
if *n > 0 {
174-
result += &table[(n / 2) as usize];
175-
} else {
176-
result -= &table[((-n) / 2) as usize];
191+
if n > 0 {
192+
result += &table[(n / 2) as usize];
193+
} else {
194+
result -= &table[((-n) / 2) as usize];
195+
}
177196
}
178197
}
179198
}
@@ -506,6 +525,21 @@ impl<G: Group, const WINDOW_SIZE: usize> WnafBase<G, WINDOW_SIZE> {
506525

507526
WnafBase { table }
508527
}
528+
529+
/// Perform a multiscalar multiplication.
530+
pub fn multiscalar_mul<'a, I, J>(scalars: I, bases: J) -> G
531+
where
532+
I: Iterator<Item = &'a WnafScalar<G::Scalar, WINDOW_SIZE>>,
533+
J: Iterator<Item = &'a Self>,
534+
{
535+
let wnafs = scalars
536+
.map(|scalar| scalar.wnaf.as_slice())
537+
.collect::<Vec<_>>();
538+
539+
let tables = bases.map(|base| base.table.as_slice()).collect::<Vec<_>>();
540+
541+
wnaf_multi_exp(tables.as_slice(), wnafs.as_slice())
542+
}
509543
}
510544

511545
impl<G: Group, const WINDOW_SIZE: usize> Mul<&WnafScalar<G::Scalar, WINDOW_SIZE>>

0 commit comments

Comments
 (0)