Make basepoint table constants &'static references (#488)

* Make basepoint table constants static references

This ensures they have a fixed address and aren't duplicated across
compilation units.

Since they were already always borrowed, this changes the static values
to be `&'static` addresses to ensure they're always borrowed rather than
potentially copied.

* rustfmt
This commit is contained in:
Tony Arcieri 2022-12-28 01:24:46 -07:00 committed by GitHub
parent 1b000d271d
commit 6a51f4fa40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 9645 additions and 9640 deletions

View file

@ -32,7 +32,7 @@ mod edwards_benches {
} }
fn consttime_fixed_base_scalar_mul(c: &mut Criterion) { fn consttime_fixed_base_scalar_mul(c: &mut Criterion) {
let B = &constants::ED25519_BASEPOINT_TABLE; let B = constants::ED25519_BASEPOINT_TABLE;
let s = Scalar::from(897987897u64).invert(); let s = Scalar::from(897987897u64).invert();
c.bench_function("Constant-time fixed-base scalar mul", move |b| { c.bench_function("Constant-time fixed-base scalar mul", move |b| {
b.iter(|| B * &s) b.iter(|| B * &s)
@ -50,7 +50,7 @@ mod edwards_benches {
fn vartime_double_base_scalar_mul(c: &mut Criterion) { fn vartime_double_base_scalar_mul(c: &mut Criterion) {
c.bench_function("Variable-time aA+bB, A variable, B fixed", |bench| { c.bench_function("Variable-time aA+bB, A variable, B fixed", |bench| {
let mut rng = thread_rng(); let mut rng = thread_rng();
let A = &Scalar::random(&mut rng) * &constants::ED25519_BASEPOINT_TABLE; let A = &Scalar::random(&mut rng) * constants::ED25519_BASEPOINT_TABLE;
bench.iter_batched( bench.iter_batched(
|| (Scalar::random(&mut rng), Scalar::random(&mut rng)), || (Scalar::random(&mut rng), Scalar::random(&mut rng)),
|(a, b)| EdwardsPoint::vartime_double_scalar_mul_basepoint(&a, &A, &b), |(a, b)| EdwardsPoint::vartime_double_scalar_mul_basepoint(&a, &A, &b),
@ -88,7 +88,7 @@ mod multiscalar_benches {
fn construct_points(n: usize) -> Vec<EdwardsPoint> { fn construct_points(n: usize) -> Vec<EdwardsPoint> {
let mut rng = thread_rng(); let mut rng = thread_rng();
(0..n) (0..n)
.map(|_| &Scalar::random(&mut rng) * &constants::ED25519_BASEPOINT_TABLE) .map(|_| &Scalar::random(&mut rng) * constants::ED25519_BASEPOINT_TABLE)
.collect() .collect()
} }

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -441,7 +441,7 @@ mod test {
println!("Testing B +- kB"); println!("Testing B +- kB");
let P = constants::ED25519_BASEPOINT_POINT; let P = constants::ED25519_BASEPOINT_POINT;
let Q = &constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64); let Q = constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64);
addition_test_helper(P, Q); addition_test_helper(P, Q);
} }
@ -520,7 +520,7 @@ mod test {
doubling_test_helper(P); doubling_test_helper(P);
println!("Testing [2]([k]B)"); println!("Testing [2]([k]B)");
let P = &constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64); let P = constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64);
doubling_test_helper(P); doubling_test_helper(P);
} }

View file

@ -278,7 +278,7 @@ mod test {
println!("Testing B +- kB"); println!("Testing B +- kB");
let P = constants::ED25519_BASEPOINT_POINT; let P = constants::ED25519_BASEPOINT_POINT;
let Q = &constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64); let Q = constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64);
addition_test_helper(P, Q); addition_test_helper(P, Q);
} }
@ -309,7 +309,7 @@ mod test {
doubling_test_helper(P); doubling_test_helper(P);
println!("Testing [2]([k]B)"); println!("Testing [2]([k]B)");
let P = &constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64); let P = constants::ED25519_BASEPOINT_TABLE * &Scalar::from(8475983829u64);
doubling_test_helper(P); doubling_test_helper(P);
} }
} }

View file

@ -19,7 +19,7 @@
//! use curve25519_dalek::constants; //! use curve25519_dalek::constants;
//! use curve25519_dalek::traits::IsIdentity; //! use curve25519_dalek::traits::IsIdentity;
//! //!
//! let B = &constants::RISTRETTO_BASEPOINT_TABLE; //! let B = constants::RISTRETTO_BASEPOINT_TABLE;
//! let l = &constants::BASEPOINT_ORDER; //! let l = &constants::BASEPOINT_ORDER;
//! //!
//! let A = l * B; //! let A = l * B;
@ -30,7 +30,7 @@
use cfg_if::cfg_if; use cfg_if::cfg_if;
use crate::edwards::CompressedEdwardsY; use crate::edwards::{CompressedEdwardsY, EdwardsBasepointTable};
use crate::montgomery::MontgomeryPoint; use crate::montgomery::MontgomeryPoint;
use crate::ristretto::CompressedRistretto; use crate::ristretto::CompressedRistretto;
use crate::ristretto::RistrettoPoint; use crate::ristretto::RistrettoPoint;
@ -93,8 +93,11 @@ pub const BASEPOINT_ORDER: Scalar = Scalar {
use crate::ristretto::RistrettoBasepointTable; use crate::ristretto::RistrettoBasepointTable;
/// The Ristretto basepoint, as a `RistrettoBasepointTable` for scalar multiplication. /// The Ristretto basepoint, as a `RistrettoBasepointTable` for scalar multiplication.
pub const RISTRETTO_BASEPOINT_TABLE: RistrettoBasepointTable = pub static RISTRETTO_BASEPOINT_TABLE: &'static RistrettoBasepointTable = unsafe {
RistrettoBasepointTable(ED25519_BASEPOINT_TABLE); // SAFETY: `RistrettoBasepointTable` is a `#[repr(transparent)]` newtype of
// `EdwardsBasepointTable`
&*(ED25519_BASEPOINT_TABLE as *const EdwardsBasepointTable as *const RistrettoBasepointTable)
};
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View file

@ -837,7 +837,7 @@ macro_rules! impl_basepoint_table {
/// ///
/// * [`EdwardsBasepointTableRadix16`]: 30KB, 64A /// * [`EdwardsBasepointTableRadix16`]: 30KB, 64A
/// (this is the default size, and is used for /// (this is the default size, and is used for
/// [`constants::ED25519_BASEPOINT_TABLE`]) /// [`ED25519_BASEPOINT_TABLE`])
/// * [`EdwardsBasepointTableRadix64`]: 120KB, 43A /// * [`EdwardsBasepointTableRadix64`]: 120KB, 43A
/// * [`EdwardsBasepointTableRadix128`]: 240KB, 37A /// * [`EdwardsBasepointTableRadix128`]: 240KB, 37A
/// * [`EdwardsBasepointTableRadix256`]: 480KB, 33A /// * [`EdwardsBasepointTableRadix256`]: 480KB, 33A
@ -858,6 +858,7 @@ macro_rules! impl_basepoint_table {
/// When \\(w = 8\\), we can't fit \\(carry \cdot 2^{w}\\) into an `i8`, so we /// When \\(w = 8\\), we can't fit \\(carry \cdot 2^{w}\\) into an `i8`, so we
/// add the carry bit onto an additional coefficient. /// add the carry bit onto an additional coefficient.
#[derive(Clone)] #[derive(Clone)]
#[repr(transparent)]
pub struct $name(pub(crate) [$table<AffineNielsPoint>; 32]); pub struct $name(pub(crate) [$table<AffineNielsPoint>; 32]);
impl BasepointTable for $name { impl BasepointTable for $name {
@ -1117,7 +1118,7 @@ impl Debug for EdwardsPoint {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::constants; use crate::constants::ED25519_BASEPOINT_TABLE;
use crate::field::FieldElement; use crate::field::FieldElement;
use crate::scalar::Scalar; use crate::scalar::Scalar;
use subtle::ConditionallySelectable; use subtle::ConditionallySelectable;
@ -1213,7 +1214,7 @@ mod test {
/// Test that computing 1*basepoint gives the correct basepoint. /// Test that computing 1*basepoint gives the correct basepoint.
#[test] #[test]
fn basepoint_mult_one_vs_basepoint() { fn basepoint_mult_one_vs_basepoint() {
let bp = &constants::ED25519_BASEPOINT_TABLE * &Scalar::ONE; let bp = ED25519_BASEPOINT_TABLE * &Scalar::ONE;
let compressed = bp.compress(); let compressed = bp.compress();
assert_eq!(compressed, constants::ED25519_BASEPOINT_COMPRESSED); assert_eq!(compressed, constants::ED25519_BASEPOINT_COMPRESSED);
} }
@ -1221,7 +1222,7 @@ mod test {
/// Test that `EdwardsBasepointTable::basepoint()` gives the correct basepoint. /// Test that `EdwardsBasepointTable::basepoint()` gives the correct basepoint.
#[test] #[test]
fn basepoint_table_basepoint_function_correct() { fn basepoint_table_basepoint_function_correct() {
let bp = constants::ED25519_BASEPOINT_TABLE.basepoint(); let bp = ED25519_BASEPOINT_TABLE.basepoint();
assert_eq!(bp.compress(), constants::ED25519_BASEPOINT_COMPRESSED); assert_eq!(bp.compress(), constants::ED25519_BASEPOINT_COMPRESSED);
} }
@ -1273,7 +1274,7 @@ mod test {
#[test] #[test]
fn to_affine_niels_clears_denominators() { fn to_affine_niels_clears_denominators() {
// construct a point as aB so it has denominators (ie. Z != 1) // construct a point as aB so it has denominators (ie. Z != 1)
let aB = &constants::ED25519_BASEPOINT_TABLE * &A_SCALAR; let aB = ED25519_BASEPOINT_TABLE * &A_SCALAR;
let aB_affine_niels = aB.as_affine_niels(); let aB_affine_niels = aB.as_affine_niels();
let also_aB = (&EdwardsPoint::identity() + &aB_affine_niels).as_extended(); let also_aB = (&EdwardsPoint::identity() + &aB_affine_niels).as_extended();
assert_eq!(aB.compress(), also_aB.compress()); assert_eq!(aB.compress(), also_aB.compress());
@ -1282,14 +1283,14 @@ mod test {
/// Test basepoint_mult versus a known scalar multiple from ed25519.py /// Test basepoint_mult versus a known scalar multiple from ed25519.py
#[test] #[test]
fn basepoint_mult_vs_ed25519py() { fn basepoint_mult_vs_ed25519py() {
let aB = &constants::ED25519_BASEPOINT_TABLE * &A_SCALAR; let aB = ED25519_BASEPOINT_TABLE * &A_SCALAR;
assert_eq!(aB.compress(), A_TIMES_BASEPOINT); assert_eq!(aB.compress(), A_TIMES_BASEPOINT);
} }
/// Test that multiplication by the basepoint order kills the basepoint /// Test that multiplication by the basepoint order kills the basepoint
#[test] #[test]
fn basepoint_mult_by_basepoint_order() { fn basepoint_mult_by_basepoint_order() {
let B = &constants::ED25519_BASEPOINT_TABLE; let B = ED25519_BASEPOINT_TABLE;
let should_be_id = B * &constants::BASEPOINT_ORDER; let should_be_id = B * &constants::BASEPOINT_ORDER;
assert!(should_be_id.is_identity()); assert!(should_be_id.is_identity());
} }
@ -1297,7 +1298,7 @@ mod test {
/// Test precomputed basepoint mult /// Test precomputed basepoint mult
#[test] #[test]
fn test_precomputed_basepoint_mult() { fn test_precomputed_basepoint_mult() {
let aB_1 = &constants::ED25519_BASEPOINT_TABLE * &A_SCALAR; let aB_1 = ED25519_BASEPOINT_TABLE * &A_SCALAR;
let aB_2 = constants::ED25519_BASEPOINT_POINT * A_SCALAR; let aB_2 = constants::ED25519_BASEPOINT_POINT * A_SCALAR;
assert_eq!(aB_1.compress(), aB_2.compress()); assert_eq!(aB_1.compress(), aB_2.compress());
} }
@ -1322,7 +1323,7 @@ mod test {
#[test] #[test]
fn basepoint_mult_two_vs_basepoint2() { fn basepoint_mult_two_vs_basepoint2() {
let two = Scalar::from(2u64); let two = Scalar::from(2u64);
let bp2 = &constants::ED25519_BASEPOINT_TABLE * &two; let bp2 = ED25519_BASEPOINT_TABLE * &two;
assert_eq!(bp2.compress(), BASE2_CMPRSSD); assert_eq!(bp2.compress(), BASE2_CMPRSSD);
} }
@ -1338,7 +1339,7 @@ mod test {
let table_radix128 = EdwardsBasepointTableRadix128::create(P); let table_radix128 = EdwardsBasepointTableRadix128::create(P);
let table_radix256 = EdwardsBasepointTableRadix256::create(P); let table_radix256 = EdwardsBasepointTableRadix256::create(P);
let aP = (&constants::ED25519_BASEPOINT_TABLE * &a).compress(); let aP = (ED25519_BASEPOINT_TABLE * &a).compress();
let aP16 = (&table_radix16 * &a).compress(); let aP16 = (&table_radix16 * &a).compress();
let aP32 = (&table_radix32 * &a).compress(); let aP32 = (&table_radix32 * &a).compress();
let aP64 = (&table_radix64 * &a).compress(); let aP64 = (&table_radix64 * &a).compress();
@ -1368,7 +1369,7 @@ mod test {
let table_radix128 = EdwardsBasepointTableRadix128::create(P); let table_radix128 = EdwardsBasepointTableRadix128::create(P);
let table_radix256 = EdwardsBasepointTableRadix256::create(P); let table_radix256 = EdwardsBasepointTableRadix256::create(P);
let aP = (&constants::ED25519_BASEPOINT_TABLE * &a).compress(); let aP = (ED25519_BASEPOINT_TABLE * &a).compress();
let aP16 = (&table_radix16 * &a).compress(); let aP16 = (&table_radix16 * &a).compress();
let aP32 = (&table_radix32 * &a).compress(); let aP32 = (&table_radix32 * &a).compress();
let aP64 = (&table_radix64 * &a).compress(); let aP64 = (&table_radix64 * &a).compress();
@ -1518,7 +1519,7 @@ mod test {
// Construct points G_i = x_i * B // Construct points G_i = x_i * B
let Gs = xs let Gs = xs
.iter() .iter()
.map(|xi| xi * &constants::ED25519_BASEPOINT_TABLE) .map(|xi| xi * ED25519_BASEPOINT_TABLE)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Compute H1 = <xs, Gs> (consttime) // Compute H1 = <xs, Gs> (consttime)
@ -1526,7 +1527,7 @@ mod test {
// Compute H2 = <xs, Gs> (vartime) // Compute H2 = <xs, Gs> (vartime)
let H2 = EdwardsPoint::vartime_multiscalar_mul(&xs, &Gs); let H2 = EdwardsPoint::vartime_multiscalar_mul(&xs, &Gs);
// Compute H3 = <xs, Gs> = sum(xi^2) * B // Compute H3 = <xs, Gs> = sum(xi^2) * B
let H3 = &check * &constants::ED25519_BASEPOINT_TABLE; let H3 = &check * ED25519_BASEPOINT_TABLE;
assert_eq!(H1, H3); assert_eq!(H1, H3);
assert_eq!(H2, H3); assert_eq!(H2, H3);
@ -1576,7 +1577,7 @@ mod test {
fn vartime_precomputed_vs_nonprecomputed_multiscalar() { fn vartime_precomputed_vs_nonprecomputed_multiscalar() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let B = &crate::constants::ED25519_BASEPOINT_TABLE; let B = ED25519_BASEPOINT_TABLE;
let static_scalars = (0..128) let static_scalars = (0..128)
.map(|_| Scalar::random(&mut rng)) .map(|_| Scalar::random(&mut rng))

View file

@ -476,7 +476,7 @@ mod test {
let mut csprng: OsRng = OsRng; let mut csprng: OsRng = OsRng;
let s: Scalar = Scalar::random(&mut csprng); let s: Scalar = Scalar::random(&mut csprng);
let p_edwards: EdwardsPoint = &constants::ED25519_BASEPOINT_TABLE * &s; let p_edwards: EdwardsPoint = constants::ED25519_BASEPOINT_TABLE * &s;
let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery(); let p_montgomery: MontgomeryPoint = p_edwards.to_montgomery();
let expected = s * p_edwards; let expected = s * p_edwards;

View file

@ -1034,13 +1034,14 @@ impl RistrettoPoint {
/// A precomputed table of multiples of the Ristretto basepoint is /// A precomputed table of multiples of the Ristretto basepoint is
/// available in the `constants` module: /// available in the `constants` module:
/// ``` /// ```
/// use curve25519_dalek::constants; /// use curve25519_dalek::constants::RISTRETTO_BASEPOINT_TABLE;
/// use curve25519_dalek::scalar::Scalar; /// use curve25519_dalek::scalar::Scalar;
/// ///
/// let a = Scalar::from(87329482u64); /// let a = Scalar::from(87329482u64);
/// let P = &a * &constants::RISTRETTO_BASEPOINT_TABLE; /// let P = &a * RISTRETTO_BASEPOINT_TABLE;
/// ``` /// ```
#[derive(Clone)] #[derive(Clone)]
#[repr(transparent)]
pub struct RistrettoBasepointTable(pub(crate) EdwardsBasepointTable); pub struct RistrettoBasepointTable(pub(crate) EdwardsBasepointTable);
impl<'a, 'b> Mul<&'b Scalar> for &'a RistrettoBasepointTable { impl<'a, 'b> Mul<&'b Scalar> for &'a RistrettoBasepointTable {
@ -1157,7 +1158,7 @@ mod test {
use rand_core::OsRng; use rand_core::OsRng;
use super::*; use super::*;
use crate::constants; use crate::constants::RISTRETTO_BASEPOINT_TABLE;
use crate::edwards::CompressedEdwardsY; use crate::edwards::CompressedEdwardsY;
use crate::scalar::Scalar; use crate::scalar::Scalar;
use crate::traits::Identity; use crate::traits::Identity;
@ -1354,7 +1355,7 @@ mod test {
#[test] #[test]
fn four_torsion_random() { fn four_torsion_random() {
let mut rng = OsRng; let mut rng = OsRng;
let B = &constants::RISTRETTO_BASEPOINT_TABLE; let B = RISTRETTO_BASEPOINT_TABLE;
let P = B * &Scalar::random(&mut rng); let P = B * &Scalar::random(&mut rng);
let P_coset = P.coset4(); let P_coset = P.coset4();
for point in P_coset { for point in P_coset {
@ -1680,7 +1681,7 @@ mod test {
#[test] #[test]
fn random_roundtrip() { fn random_roundtrip() {
let mut rng = OsRng; let mut rng = OsRng;
let B = &constants::RISTRETTO_BASEPOINT_TABLE; let B = RISTRETTO_BASEPOINT_TABLE;
for _ in 0..100 { for _ in 0..100 {
let P = B * &Scalar::random(&mut rng); let P = B * &Scalar::random(&mut rng);
let compressed_P = P.compress(); let compressed_P = P.compress();
@ -1711,7 +1712,7 @@ mod test {
fn vartime_precomputed_vs_nonprecomputed_multiscalar() { fn vartime_precomputed_vs_nonprecomputed_multiscalar() {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let B = &crate::constants::RISTRETTO_BASEPOINT_TABLE; let B = RISTRETTO_BASEPOINT_TABLE;
let static_scalars = (0..128) let static_scalars = (0..128)
.map(|_| Scalar::random(&mut rng)) .map(|_| Scalar::random(&mut rng))