mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-06 20:41:14 +00:00
Add NafLookupTable8 and use for pre-computed basepoint table generation.
This commit is contained in:
parent
753a0292de
commit
62d43752df
3 changed files with 53 additions and 6 deletions
10
build.rs
10
build.rs
|
|
@ -59,7 +59,7 @@ mod scalar_mul;
|
||||||
|
|
||||||
use edwards::EdwardsBasepointTable;
|
use edwards::EdwardsBasepointTable;
|
||||||
use curve_models::AffineNielsPoint;
|
use curve_models::AffineNielsPoint;
|
||||||
use scalar_mul::window::NafLookupTable5;
|
use scalar_mul::window::NafLookupTable8;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Enable the "precomputed_tables" feature in the main build stage
|
// Enable the "precomputed_tables" feature in the main build stage
|
||||||
|
|
@ -86,7 +86,7 @@ use edwards::EdwardsBasepointTable;
|
||||||
use curve_models::AffineNielsPoint;
|
use curve_models::AffineNielsPoint;
|
||||||
|
|
||||||
use scalar_mul::window::LookupTable;
|
use scalar_mul::window::LookupTable;
|
||||||
use scalar_mul::window::NafLookupTable5;
|
use scalar_mul::window::NafLookupTable8;
|
||||||
|
|
||||||
/// Table containing precomputed multiples of the Ed25519 basepoint \\\\(B = (x, 4/5)\\\\).
|
/// Table containing precomputed multiples of the Ed25519 basepoint \\\\(B = (x, 4/5)\\\\).
|
||||||
pub const ED25519_BASEPOINT_TABLE: EdwardsBasepointTable = ED25519_BASEPOINT_TABLE_INNER_DOC_HIDDEN;
|
pub const ED25519_BASEPOINT_TABLE: EdwardsBasepointTable = ED25519_BASEPOINT_TABLE_INNER_DOC_HIDDEN;
|
||||||
|
|
@ -101,13 +101,13 @@ pub const ED25519_BASEPOINT_TABLE_INNER_DOC_HIDDEN: EdwardsBasepointTable = {:?}
|
||||||
|
|
||||||
// Now generate AFFINE_ODD_MULTIPLES_OF_BASEPOINT
|
// Now generate AFFINE_ODD_MULTIPLES_OF_BASEPOINT
|
||||||
let B = &constants::ED25519_BASEPOINT_POINT;
|
let B = &constants::ED25519_BASEPOINT_POINT;
|
||||||
let odd_multiples = NafLookupTable5::<AffineNielsPoint>::from(B);
|
let odd_multiples = NafLookupTable8::<AffineNielsPoint>::from(B);
|
||||||
|
|
||||||
f.write_all(
|
f.write_all(
|
||||||
format!(
|
format!(
|
||||||
"\n
|
"\n
|
||||||
/// Odd multiples of the basepoint `[B, 3B, 5B, 7B, 9B, 11B, 13B, 15B]`.
|
/// Odd multiples of the basepoint `[B, 3B, 5B, 7B, 9B, 11B, 13B, 15B, ..., 127B]`.
|
||||||
pub(crate) const AFFINE_ODD_MULTIPLES_OF_BASEPOINT: NafLookupTable5<AffineNielsPoint> = {:?};
|
pub(crate) const AFFINE_ODD_MULTIPLES_OF_BASEPOINT: NafLookupTable8<AffineNielsPoint> = {:?};
|
||||||
\n\n",
|
\n\n",
|
||||||
&odd_multiples
|
&odd_multiples
|
||||||
).as_bytes(),
|
).as_bytes(),
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ use scalar_mul::window::NafLookupTable5;
|
||||||
/// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint.
|
/// Compute \\(aA + bB\\) in variable time, where \\(B\\) is the Ed25519 basepoint.
|
||||||
pub fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
|
pub fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
|
||||||
let a_naf = a.non_adjacent_form(5);
|
let a_naf = a.non_adjacent_form(5);
|
||||||
let b_naf = b.non_adjacent_form(5);
|
let b_naf = b.non_adjacent_form(8);
|
||||||
|
|
||||||
// Find starting index
|
// Find starting index
|
||||||
let mut i: usize = 255;
|
let mut i: usize = 255;
|
||||||
|
|
|
||||||
|
|
@ -163,3 +163,50 @@ impl<'a> From<&'a EdwardsPoint> for NafLookupTable5<AffineNielsPoint> {
|
||||||
NafLookupTable5(Ai)
|
NafLookupTable5(Ai)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Holds stuff up to 8.
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub(crate) struct NafLookupTable8<T>(pub(crate) [T; 64]);
|
||||||
|
|
||||||
|
impl<T: Copy> NafLookupTable8<T> {
|
||||||
|
pub fn select(&self, x: usize) -> T {
|
||||||
|
debug_assert_eq!(x & 1, 1);
|
||||||
|
debug_assert!(x < 256);
|
||||||
|
|
||||||
|
self.0[x / 2]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Debug> Debug for NafLookupTable8<T> {
|
||||||
|
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
|
||||||
|
write!(f, "NafLookupTable8([\n")?;
|
||||||
|
for i in 0..64 {
|
||||||
|
write!(f, "\t{:?},\n", &self.0[i])?;
|
||||||
|
}
|
||||||
|
write!(f, "])")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a EdwardsPoint> for NafLookupTable8<ProjectiveNielsPoint> {
|
||||||
|
fn from(A: &'a EdwardsPoint) -> Self {
|
||||||
|
let mut Ai = [A.to_projective_niels(); 64];
|
||||||
|
let A2 = A.double();
|
||||||
|
for i in 0..63 {
|
||||||
|
Ai[i + 1] = (&A2 + &Ai[i]).to_extended().to_projective_niels();
|
||||||
|
}
|
||||||
|
// Now Ai = [A, 3A, 5A, 7A, 9A, 11A, 13A, 15A, ..., 127A]
|
||||||
|
NafLookupTable8(Ai)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a EdwardsPoint> for NafLookupTable8<AffineNielsPoint> {
|
||||||
|
fn from(A: &'a EdwardsPoint) -> Self {
|
||||||
|
let mut Ai = [A.to_affine_niels(); 64];
|
||||||
|
let A2 = A.double();
|
||||||
|
for i in 0..63 {
|
||||||
|
Ai[i + 1] = (&A2 + &Ai[i]).to_extended().to_affine_niels();
|
||||||
|
}
|
||||||
|
// Now Ai = [A, 3A, 5A, 7A, 9A, 11A, 13A, 15A, ..., 127A]
|
||||||
|
NafLookupTable8(Ai)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue