mirror of
https://github.com/saymrwulf/curve25519-dalek-source.git
synced 2026-09-08 21:00:40 +00:00
Implement Mul, MulAssign, zero(), one() for UnpackedScalar
This commit is contained in:
parent
b5ccb42759
commit
653f134bc7
1 changed files with 33 additions and 0 deletions
|
|
@ -31,6 +31,7 @@
|
||||||
|
|
||||||
use core::cmp::{Eq, PartialEq};
|
use core::cmp::{Eq, PartialEq};
|
||||||
use core::ops::{Neg, Index, IndexMut};
|
use core::ops::{Neg, Index, IndexMut};
|
||||||
|
use core::ops::{Mul, MulAssign};
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
|
@ -397,6 +398,20 @@ impl IndexMut<usize> for UnpackedScalar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'b> MulAssign<&'b UnpackedScalar> for UnpackedScalar {
|
||||||
|
fn mul_assign(&mut self, _rhs: &'b UnpackedScalar) {
|
||||||
|
let result = (self as &UnpackedScalar) * _rhs;
|
||||||
|
self.0 = result.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> Mul<&'b UnpackedScalar> for &'a UnpackedScalar {
|
||||||
|
type Output = UnpackedScalar;
|
||||||
|
fn mul(self, _rhs: &'b UnpackedScalar) -> UnpackedScalar {
|
||||||
|
UnpackedScalar::multiply_add(self,_rhs, &UnpackedScalar::zero())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl UnpackedScalar {
|
impl UnpackedScalar {
|
||||||
/// Pack the limbs of this `UnpackedScalar` into a `Scalar`.
|
/// Pack the limbs of this `UnpackedScalar` into a `Scalar`.
|
||||||
fn pack(&self) -> Scalar {
|
fn pack(&self) -> Scalar {
|
||||||
|
|
@ -437,6 +452,16 @@ impl UnpackedScalar {
|
||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the zero scalar.
|
||||||
|
pub fn zero() -> UnpackedScalar {
|
||||||
|
UnpackedScalar([0,0,0,0,0,0,0,0,0,0,0,0])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the one scalar.
|
||||||
|
pub fn one() -> UnpackedScalar {
|
||||||
|
UnpackedScalar([1,0,0,0,0,0,0,0,0,0,0,0])
|
||||||
|
}
|
||||||
|
|
||||||
/// Compute `ab+c (mod l)`.
|
/// Compute `ab+c (mod l)`.
|
||||||
pub fn multiply_add(a: &UnpackedScalar,
|
pub fn multiply_add(a: &UnpackedScalar,
|
||||||
b: &UnpackedScalar,
|
b: &UnpackedScalar,
|
||||||
|
|
@ -657,6 +682,14 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unpacked_mul() {
|
||||||
|
let x = X.unpack();
|
||||||
|
let y = Y.unpack();
|
||||||
|
let z = &x * &y;
|
||||||
|
assert_eq!(z.pack(), X_TIMES_Y);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn scalar_multiply_only() {
|
fn scalar_multiply_only() {
|
||||||
let zero = Scalar::zero();
|
let zero = Scalar::zero();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue