mirror of
https://github.com/saymrwulf/risc0-curve25519-dalek-source.git
synced 2026-09-10 21:00:41 +00:00
rustfmt src/backend/vector/scalar_mul (no changes besides formatting)
This commit is contained in:
parent
0db8783be8
commit
219995dbc9
5 changed files with 402 additions and 406 deletions
|
|
@ -15,27 +15,27 @@
|
||||||
)]
|
)]
|
||||||
pub mod spec {
|
pub mod spec {
|
||||||
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
use core::borrow::Borrow;
|
use core::borrow::Borrow;
|
||||||
use core::cmp::Ordering;
|
use core::cmp::Ordering;
|
||||||
|
|
||||||
#[for_target_feature("avx2")]
|
#[for_target_feature("avx2")]
|
||||||
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
#[for_target_feature("avx512ifma")]
|
#[for_target_feature("avx512ifma")]
|
||||||
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
use crate::edwards::EdwardsPoint;
|
use crate::edwards::EdwardsPoint;
|
||||||
use crate::scalar::Scalar;
|
use crate::scalar::Scalar;
|
||||||
use crate::traits::{Identity, VartimeMultiscalarMul};
|
use crate::traits::{Identity, VartimeMultiscalarMul};
|
||||||
|
|
||||||
/// Implements a version of Pippenger's algorithm.
|
/// Implements a version of Pippenger's algorithm.
|
||||||
///
|
///
|
||||||
/// See the documentation in the serial `scalar_mul::pippenger` module for details.
|
/// See the documentation in the serial `scalar_mul::pippenger` module for details.
|
||||||
pub struct Pippenger;
|
pub struct Pippenger;
|
||||||
|
|
||||||
impl VartimeMultiscalarMul for Pippenger {
|
impl VartimeMultiscalarMul for Pippenger {
|
||||||
type Point = EdwardsPoint;
|
type Point = EdwardsPoint;
|
||||||
|
|
||||||
fn optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
|
fn optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
|
||||||
|
|
@ -134,10 +134,10 @@ impl VartimeMultiscalarMul for Pippenger {
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_vartime_pippenger() {
|
fn test_vartime_pippenger() {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -173,6 +173,5 @@ mod test {
|
||||||
n = n / 2;
|
n = n / 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,28 +17,28 @@
|
||||||
)]
|
)]
|
||||||
pub mod spec {
|
pub mod spec {
|
||||||
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
use core::borrow::Borrow;
|
use core::borrow::Borrow;
|
||||||
use core::cmp::Ordering;
|
use core::cmp::Ordering;
|
||||||
|
|
||||||
#[for_target_feature("avx2")]
|
#[for_target_feature("avx2")]
|
||||||
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
#[for_target_feature("avx512ifma")]
|
#[for_target_feature("avx512ifma")]
|
||||||
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
use crate::edwards::EdwardsPoint;
|
use crate::edwards::EdwardsPoint;
|
||||||
use crate::scalar::Scalar;
|
use crate::scalar::Scalar;
|
||||||
use crate::traits::Identity;
|
use crate::traits::Identity;
|
||||||
use crate::traits::VartimePrecomputedMultiscalarMul;
|
use crate::traits::VartimePrecomputedMultiscalarMul;
|
||||||
use crate::window::{NafLookupTable5, NafLookupTable8};
|
use crate::window::{NafLookupTable5, NafLookupTable8};
|
||||||
|
|
||||||
pub struct VartimePrecomputedStraus {
|
pub struct VartimePrecomputedStraus {
|
||||||
static_lookup_tables: Vec<NafLookupTable8<CachedPoint>>,
|
static_lookup_tables: Vec<NafLookupTable8<CachedPoint>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
|
impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
|
||||||
type Point = EdwardsPoint;
|
type Point = EdwardsPoint;
|
||||||
|
|
||||||
fn new<I>(static_points: I) -> Self
|
fn new<I>(static_points: I) -> Self
|
||||||
|
|
@ -123,6 +123,5 @@ impl VartimePrecomputedMultiscalarMul for VartimePrecomputedStraus {
|
||||||
|
|
||||||
Some(R.into())
|
Some(R.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,35 +17,35 @@
|
||||||
)]
|
)]
|
||||||
pub mod spec {
|
pub mod spec {
|
||||||
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
use core::borrow::Borrow;
|
use core::borrow::Borrow;
|
||||||
use core::cmp::Ordering;
|
use core::cmp::Ordering;
|
||||||
|
|
||||||
use zeroize::Zeroizing;
|
use zeroize::Zeroizing;
|
||||||
|
|
||||||
#[for_target_feature("avx2")]
|
#[for_target_feature("avx2")]
|
||||||
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
#[for_target_feature("avx512ifma")]
|
#[for_target_feature("avx512ifma")]
|
||||||
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
use crate::edwards::EdwardsPoint;
|
use crate::edwards::EdwardsPoint;
|
||||||
use crate::scalar::Scalar;
|
use crate::scalar::Scalar;
|
||||||
use crate::traits::{Identity, MultiscalarMul, VartimeMultiscalarMul};
|
use crate::traits::{Identity, MultiscalarMul, VartimeMultiscalarMul};
|
||||||
use crate::window::{LookupTable, NafLookupTable5};
|
use crate::window::{LookupTable, NafLookupTable5};
|
||||||
|
|
||||||
/// Multiscalar multiplication using interleaved window / Straus'
|
/// Multiscalar multiplication using interleaved window / Straus'
|
||||||
/// method. See the `Straus` struct in the serial backend for more
|
/// method. See the `Straus` struct in the serial backend for more
|
||||||
/// details.
|
/// details.
|
||||||
///
|
///
|
||||||
/// This exists as a seperate implementation from that one because the
|
/// This exists as a seperate implementation from that one because the
|
||||||
/// AVX2 code uses different curve models (it does not pass between
|
/// AVX2 code uses different curve models (it does not pass between
|
||||||
/// multiple models during scalar mul), and it has to convert the
|
/// multiple models during scalar mul), and it has to convert the
|
||||||
/// point representation on the fly.
|
/// point representation on the fly.
|
||||||
pub struct Straus {}
|
pub struct Straus {}
|
||||||
|
|
||||||
impl MultiscalarMul for Straus {
|
impl MultiscalarMul for Straus {
|
||||||
type Point = EdwardsPoint;
|
type Point = EdwardsPoint;
|
||||||
|
|
||||||
fn multiscalar_mul<I, J>(scalars: I, points: J) -> EdwardsPoint
|
fn multiscalar_mul<I, J>(scalars: I, points: J) -> EdwardsPoint
|
||||||
|
|
@ -80,9 +80,9 @@ impl MultiscalarMul for Straus {
|
||||||
}
|
}
|
||||||
Q.into()
|
Q.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VartimeMultiscalarMul for Straus {
|
impl VartimeMultiscalarMul for Straus {
|
||||||
type Point = EdwardsPoint;
|
type Point = EdwardsPoint;
|
||||||
|
|
||||||
fn optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
|
fn optional_multiscalar_mul<I, J>(scalars: I, points: J) -> Option<EdwardsPoint>
|
||||||
|
|
@ -120,6 +120,5 @@ impl VartimeMultiscalarMul for Straus {
|
||||||
|
|
||||||
Some(Q.into())
|
Some(Q.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,19 +6,19 @@
|
||||||
)]
|
)]
|
||||||
pub mod spec {
|
pub mod spec {
|
||||||
|
|
||||||
#[for_target_feature("avx2")]
|
#[for_target_feature("avx2")]
|
||||||
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
#[for_target_feature("avx512ifma")]
|
#[for_target_feature("avx512ifma")]
|
||||||
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
use crate::edwards::EdwardsPoint;
|
use crate::edwards::EdwardsPoint;
|
||||||
use crate::scalar::Scalar;
|
use crate::scalar::Scalar;
|
||||||
use crate::traits::Identity;
|
use crate::traits::Identity;
|
||||||
use crate::window::LookupTable;
|
use crate::window::LookupTable;
|
||||||
|
|
||||||
/// Perform constant-time, variable-base scalar multiplication.
|
/// Perform constant-time, variable-base scalar multiplication.
|
||||||
pub fn mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
|
pub fn mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
|
||||||
// Construct a lookup table of [P,2P,3P,4P,5P,6P,7P,8P]
|
// Construct a lookup table of [P,2P,3P,4P,5P,6P,7P,8P]
|
||||||
let lookup_table = LookupTable::<CachedPoint>::from(point);
|
let lookup_table = LookupTable::<CachedPoint>::from(point);
|
||||||
// Setting s = scalar, compute
|
// Setting s = scalar, compute
|
||||||
|
|
@ -40,6 +40,5 @@ pub fn mul(point: &EdwardsPoint, scalar: &Scalar) -> EdwardsPoint {
|
||||||
Q = &Q + &lookup_table.select(scalar_digits[i]);
|
Q = &Q + &lookup_table.select(scalar_digits[i]);
|
||||||
}
|
}
|
||||||
Q.into()
|
Q.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,29 +17,29 @@
|
||||||
)]
|
)]
|
||||||
pub mod spec {
|
pub mod spec {
|
||||||
|
|
||||||
use core::cmp::Ordering;
|
use core::cmp::Ordering;
|
||||||
|
|
||||||
#[for_target_feature("avx2")]
|
#[for_target_feature("avx2")]
|
||||||
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::avx2::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
#[for_target_feature("avx512ifma")]
|
#[for_target_feature("avx512ifma")]
|
||||||
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
use crate::backend::vector::ifma::{CachedPoint, ExtendedPoint};
|
||||||
|
|
||||||
#[cfg(feature = "precomputed-tables")]
|
#[cfg(feature = "precomputed-tables")]
|
||||||
#[for_target_feature("avx2")]
|
#[for_target_feature("avx2")]
|
||||||
use crate::backend::vector::avx2::constants::BASEPOINT_ODD_LOOKUP_TABLE;
|
use crate::backend::vector::avx2::constants::BASEPOINT_ODD_LOOKUP_TABLE;
|
||||||
|
|
||||||
#[cfg(feature = "precomputed-tables")]
|
#[cfg(feature = "precomputed-tables")]
|
||||||
#[for_target_feature("avx512ifma")]
|
#[for_target_feature("avx512ifma")]
|
||||||
use crate::backend::vector::ifma::constants::BASEPOINT_ODD_LOOKUP_TABLE;
|
use crate::backend::vector::ifma::constants::BASEPOINT_ODD_LOOKUP_TABLE;
|
||||||
|
|
||||||
use crate::edwards::EdwardsPoint;
|
use crate::edwards::EdwardsPoint;
|
||||||
use crate::scalar::Scalar;
|
use crate::scalar::Scalar;
|
||||||
use crate::traits::Identity;
|
use crate::traits::Identity;
|
||||||
use crate::window::NafLookupTable5;
|
use crate::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);
|
||||||
|
|
||||||
#[cfg(feature = "precomputed-tables")]
|
#[cfg(feature = "precomputed-tables")]
|
||||||
|
|
@ -62,7 +62,8 @@ pub fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
|
||||||
let table_B = &BASEPOINT_ODD_LOOKUP_TABLE;
|
let table_B = &BASEPOINT_ODD_LOOKUP_TABLE;
|
||||||
|
|
||||||
#[cfg(not(feature = "precomputed-tables"))]
|
#[cfg(not(feature = "precomputed-tables"))]
|
||||||
let table_B = &NafLookupTable5::<CachedPoint>::from(&crate::constants::ED25519_BASEPOINT_POINT);
|
let table_B =
|
||||||
|
&NafLookupTable5::<CachedPoint>::from(&crate::constants::ED25519_BASEPOINT_POINT);
|
||||||
|
|
||||||
let mut Q = ExtendedPoint::identity();
|
let mut Q = ExtendedPoint::identity();
|
||||||
|
|
||||||
|
|
@ -96,6 +97,5 @@ pub fn mul(a: &Scalar, A: &EdwardsPoint, b: &Scalar) -> EdwardsPoint {
|
||||||
}
|
}
|
||||||
|
|
||||||
Q.into()
|
Q.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue