mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-04 20:24:10 +00:00
Make scalar multiplication go both ways.
Being able to do `P * s`, but not `s * P`, is slightly annoying, particularly with longer equations when it is desired to be able to glance at the maths and see that the code is the same. Now either syntax is allowed.
This commit is contained in:
parent
c18627f7c2
commit
4da1d795a1
1 changed files with 23 additions and 0 deletions
23
src/curve.rs
23
src/curve.rs
|
|
@ -817,6 +817,18 @@ impl<'a, 'b> Mul<&'b Scalar> for &'a ExtendedPoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> Mul<&'b ExtendedPoint> for &'a Scalar {
|
||||
type Output = ExtendedPoint;
|
||||
|
||||
/// Scalar multiplication: compute `self * point`.
|
||||
///
|
||||
/// Uses a window of size 4. Note: for scalar multiplication of
|
||||
/// the basepoint, `basepoint_mult` is approximately 4x faster.
|
||||
fn mul(self, point: &'b ExtendedPoint) -> ExtendedPoint {
|
||||
point * &self
|
||||
}
|
||||
}
|
||||
|
||||
/// Precomputation
|
||||
#[derive(Clone)]
|
||||
pub struct EdwardsBasepointTable(pub [[AffineNielsPoint; 8]; 32]);
|
||||
|
|
@ -1459,6 +1471,17 @@ mod test {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalarmult_works_both_ways() {
|
||||
let G: ExtendedPoint = constants::ED25519_BASEPOINT;
|
||||
let s: Scalar = A_SCALAR;
|
||||
|
||||
let P1 = &G * &s;
|
||||
let P2 = &s * &G;
|
||||
|
||||
assert!(P1.compress_edwards().to_bytes() == P2.compress_edwards().to_bytes());
|
||||
}
|
||||
|
||||
mod vartime {
|
||||
use super::super::*;
|
||||
use super::{A_SCALAR, B_SCALAR, A_TIMES_BASEPOINT, DOUBLE_SCALAR_MULT_RESULT};
|
||||
|
|
|
|||
Loading…
Reference in a new issue