From 3eab4acbd8d3fb2fea1be36af154f84b0f4a0b61 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Sun, 12 Mar 2017 23:18:31 -0700 Subject: [PATCH] Add p constant for subtraction --- src/constants.rs | 3 +++ src/field.rs | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 1dd931f..487d557 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -26,6 +26,9 @@ use curve::CompressedEdwardsY; use curve::EdwardsBasepointTable; use scalar::Scalar; +#[cfg(feature="radix_51")] +pub const p: FieldElement = FieldElement([2251799813685229, 2251799813685247, 2251799813685247, 2251799813685247, 2251799813685247]); + #[cfg(feature="radix_25_5")] pub const d: FieldElement = FieldElement([ -10913610, 13857413, -15372611, 6949391, 114729, diff --git a/src/field.rs b/src/field.rs index b2bde0c..8b448c9 100644 --- a/src/field.rs +++ b/src/field.rs @@ -244,11 +244,11 @@ impl<'b> SubAssign<&'b FieldElement> for FieldElement { fn sub_assign(&mut self, _rhs: &'b FieldElement) { // To avoid underflow, first add p // XXX how many copies should we add to preserve headroom? - self.0[0] += 2251799813685229; - self.0[1] += 2251799813685247; - self.0[2] += 2251799813685247; - self.0[3] += 2251799813685247; - self.0[4] += 2251799813685247; + self.0[0] += constants::p.0[0]; + self.0[1] += constants::p.0[1]; + self.0[2] += constants::p.0[2]; + self.0[3] += constants::p.0[3]; + self.0[4] += constants::p.0[4]; // then subtract _rhs self.0[0] -= _rhs.0[0]; self.0[1] -= _rhs.0[1]; @@ -338,6 +338,15 @@ impl FieldElement { self[i] = -self[i]; } } + #[cfg(feature="radix_51")] + pub fn negate(&mut self) { + // XXX how many copies of p + self.0[0] = constants::p.0[0] - self.0[0]; + self.0[1] = constants::p.0[1] - self.0[1]; + self.0[2] = constants::p.0[2] - self.0[2]; + self.0[3] = constants::p.0[3] - self.0[3]; + self.0[4] = constants::p.0[4] - self.0[4]; + } /// Construct the additive identity #[cfg(feature="radix_25_5")]