Implement Neg for Scalar.

This commit is contained in:
Isis Lovecruft 2017-01-06 17:08:37 +00:00
parent 51f59565f5
commit 32da4c7d50
Failed to extract signature
2 changed files with 35 additions and 0 deletions

View file

@ -21,6 +21,7 @@
use field::FieldElement;
use curve::PreComputedPoint;
use curve::CompressedEdwardsY;
use scalar::Scalar;
pub const d: FieldElement = FieldElement([
-10913610, 13857413, -15372611, 6949391, 114729,
@ -66,6 +67,20 @@ pub const BASE_CMPRSSD: CompressedEdwardsY =
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66]);
/// `l` is the order of base point, i.e. 2^252 +
/// 27742317777372353535851937790883648493, in little-endian form
pub const l: Scalar = Scalar([ 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 ]);
/// `lminus1` is the order of base point minus one, i.e. 2^252 +
/// 27742317777372353535851937790883648493 - 1, in little-endian form
pub const lminus1: Scalar = Scalar([ 0xec, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 ]);
pub const bi: [PreComputedPoint; 8] = [
PreComputedPoint{
y_plus_x: FieldElement([25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605]),

View file

@ -30,11 +30,13 @@
//! limbs.
use core::ops::{Index, IndexMut};
use core::ops::{Neg};
#[cfg(feature = "std")]
use rand::Rng;
// XXX should these be in a utility module ?
use constants;
use field::{load3, load4};
use util::CTAssignable;
@ -62,6 +64,15 @@ impl IndexMut<usize> for Scalar {
}
}
impl Neg for Scalar {
type Output = Scalar;
/// Negate this scalar by computing (l - 1) * self - 0 (mod l).
fn neg(self) -> Scalar {
Scalar::multiply_add(&constants::lminus1, &self, &Scalar::zero())
}
}
impl CTAssignable for Scalar {
/// Conditionally assign another Scalar to this one.
///
@ -627,4 +638,13 @@ mod test {
assert!(test_red[i] == reduced[i]);
}
}
// Negating a scalar twice should result in the original scalar.
#[test]
fn test_scalar_neg() {
let negative_x: Scalar = -X;
let orig: Scalar = -negative_x;
assert!(orig == X);
}
}