simply lookup table constructor (#56)

This commit is contained in:
zz-sol 2026-06-22 09:47:37 -04:00 committed by GitHub
parent 69e1efe684
commit 9c7161d652
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 37 deletions

View file

@ -23,7 +23,9 @@ use crate::edwards::EdwardsPoint;
use crate::scalar::Scalar;
use crate::traits::Identity;
use crate::traits::VartimePrecomputedMultiscalarMul;
use crate::window::{NafLookupTable5, NafLookupTable8};
use crate::window::{
NafLookupTable5, NafLookupTable8, build_lookup_tables, build_lookup_tables_for_optional_points,
};
#[allow(missing_docs)]
pub struct VartimePrecomputedStraus {
@ -39,10 +41,7 @@ impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
I::Item: Borrow<Self::Point>,
{
Self {
static_lookup_tables: static_points
.into_iter()
.map(|P| NafLookupTable8::<AffineNielsPoint>::from(P.borrow()))
.collect(),
static_lookup_tables: build_lookup_tables(static_points),
}
}
@ -76,10 +75,8 @@ impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
.map(|c| c.borrow().non_adjacent_form(5))
.collect::<Vec<_>>();
let dynamic_lookup_tables = dynamic_points
.into_iter()
.map(|P_opt| P_opt.map(|P| NafLookupTable5::<ProjectiveNielsPoint>::from(&P)))
.collect::<Option<Vec<_>>>()?;
let dynamic_lookup_tables: Vec<NafLookupTable5<ProjectiveNielsPoint>> =
build_lookup_tables_for_optional_points(dynamic_points)?;
let sp = self.static_lookup_tables.len();
let dp = dynamic_lookup_tables.len();

View file

@ -109,12 +109,9 @@ impl MultiscalarMul for Straus {
{
use crate::backend::serial::curve_models::ProjectiveNielsPoint;
use crate::traits::Identity;
use crate::window::LookupTable;
use crate::window::{LookupTable, build_lookup_tables};
let lookup_tables: Vec<_> = points
.into_iter()
.map(|point| LookupTable::<ProjectiveNielsPoint>::from(point.borrow()))
.collect();
let lookup_tables: Vec<LookupTable<ProjectiveNielsPoint>> = build_lookup_tables(points);
// This puts the scalar digits into a heap-allocated Vec.
// To ensure that these are erased, pass ownership of the Vec into a
@ -166,17 +163,15 @@ impl VartimeMultiscalarMul for Straus {
CompletedPoint, ProjectiveNielsPoint, ProjectivePoint,
};
use crate::traits::Identity;
use crate::window::NafLookupTable5;
use crate::window::{NafLookupTable5, build_lookup_tables_for_optional_points};
let nafs: Vec<_> = scalars
.into_iter()
.map(|c| c.borrow().non_adjacent_form(5))
.collect();
let lookup_tables = points
.into_iter()
.map(|P_opt| P_opt.map(|P| NafLookupTable5::<ProjectiveNielsPoint>::from(&P)))
.collect::<Option<Vec<_>>>()?;
let lookup_tables: Vec<NafLookupTable5<ProjectiveNielsPoint>> =
build_lookup_tables_for_optional_points(points)?;
let mut r = ProjectivePoint::identity();

View file

@ -26,7 +26,10 @@ pub mod spec {
use crate::scalar::Scalar;
use crate::traits::Identity;
use crate::traits::VartimePrecomputedMultiscalarMul;
use crate::window::{NafLookupTable5, NafLookupTable8};
use crate::window::{
NafLookupTable5, NafLookupTable8, build_lookup_tables,
build_lookup_tables_for_optional_points,
};
pub struct VartimePrecomputedStraus {
static_lookup_tables: Vec<NafLookupTable8<CachedPoint>>,
@ -41,10 +44,7 @@ pub mod spec {
I::Item: Borrow<EdwardsPoint>,
{
Self {
static_lookup_tables: static_points
.into_iter()
.map(|P| NafLookupTable8::<CachedPoint>::from(P.borrow()))
.collect(),
static_lookup_tables: build_lookup_tables(static_points),
}
}
@ -78,10 +78,8 @@ pub mod spec {
.map(|c| c.borrow().non_adjacent_form(5))
.collect::<Vec<_>>();
let dynamic_lookup_tables = dynamic_points
.into_iter()
.map(|P_opt| P_opt.map(|P| NafLookupTable5::<CachedPoint>::from(&P)))
.collect::<Option<Vec<_>>>()?;
let dynamic_lookup_tables: Vec<NafLookupTable5<CachedPoint>> =
build_lookup_tables_for_optional_points(dynamic_points)?;
let sp = self.static_lookup_tables.len();
let dp = dynamic_lookup_tables.len();

View file

@ -28,7 +28,9 @@ pub mod spec {
use crate::edwards::EdwardsPoint;
use crate::scalar::Scalar;
use crate::traits::{Identity, MultiscalarMul, VartimeMultiscalarMul};
use crate::window::{LookupTable, NafLookupTable5};
use crate::window::{
LookupTable, NafLookupTable5, build_lookup_tables, build_lookup_tables_for_optional_points,
};
/// Multiscalar multiplication using interleaved window / Straus'
/// method. See the `Straus` struct in the serial backend for more
@ -52,10 +54,7 @@ pub mod spec {
{
// Construct a lookup table of [P,2P,3P,4P,5P,6P,7P,8P]
// for each input point P
let lookup_tables: Vec<_> = points
.into_iter()
.map(|point| LookupTable::<CachedPoint>::from(point.borrow()))
.collect();
let lookup_tables: Vec<LookupTable<CachedPoint>> = build_lookup_tables(points);
let scalar_digits_vec: Vec<_> = scalars
.into_iter()
@ -91,10 +90,8 @@ pub mod spec {
.into_iter()
.map(|c| c.borrow().non_adjacent_form(5))
.collect();
let lookup_tables: Vec<_> = points
.into_iter()
.map(|P_opt| P_opt.map(|P| NafLookupTable5::<CachedPoint>::from(&P)))
.collect::<Option<Vec<_>>>()?;
let lookup_tables: Vec<NafLookupTable5<CachedPoint>> =
build_lookup_tables_for_optional_points(points)?;
let mut Q = ExtendedPoint::identity();

View file

@ -15,7 +15,11 @@
use core::fmt::Debug;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use cfg_if::cfg_if;
#[cfg(feature = "alloc")]
use core::borrow::Borrow;
use subtle::Choice;
use subtle::ConditionallyNegatable;
@ -28,6 +32,32 @@ use crate::backend::serial::curve_models::AffineNielsPoint;
use crate::backend::serial::curve_models::ProjectiveNielsPoint;
use crate::edwards::EdwardsPoint;
#[cfg(feature = "alloc")]
pub(crate) fn build_lookup_tables<T, I>(points: I) -> Vec<T>
where
T: for<'a> From<&'a EdwardsPoint>,
I: IntoIterator,
I::Item: Borrow<EdwardsPoint>,
{
points
.into_iter()
.map(|point| T::from(point.borrow()))
.collect()
}
#[cfg(feature = "alloc")]
pub(crate) fn build_lookup_tables_for_optional_points<T, I, P>(points: I) -> Option<Vec<T>>
where
T: for<'a> From<&'a EdwardsPoint>,
I: IntoIterator<Item = Option<P>>,
P: Borrow<EdwardsPoint>,
{
points
.into_iter()
.map(|point| point.map(|point| T::from(point.borrow())))
.collect()
}
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;