Change AffinePoint::to_niels to be a const fn.

This commit is contained in:
Sean Bowe 2019-05-21 16:31:50 -06:00
parent 8f6d6298d0
commit 4aebd80105
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
2 changed files with 19 additions and 14 deletions

View File

@ -109,14 +109,7 @@ impl<'a, 'b> Add<&'b Fq> for &'a Fq {
#[inline]
fn add(self, rhs: &'b Fq) -> Fq {
let (d0, carry) = adc(self.0[0], rhs.0[0], 0);
let (d1, carry) = adc(self.0[1], rhs.0[1], carry);
let (d2, carry) = adc(self.0[2], rhs.0[2], carry);
let (d3, _) = adc(self.0[3], rhs.0[3], carry);
// Attempt to subtract the modulus, to ensure the value
// is smaller than the modulus.
Fq([d0, d1, d2, d3]) - &MODULUS
self.field_add(rhs)
}
}
@ -554,7 +547,7 @@ impl Fq {
}
#[inline]
const fn multiply(&self, rhs: &Self) -> Self {
pub(crate) const fn multiply(&self, rhs: &Self) -> Self {
// Schoolbook multiplication
let (r0, carry) = mac(0, self.0[0], rhs.0[0], 0);
@ -581,7 +574,7 @@ impl Fq {
}
#[inline]
const fn subtract(&self, rhs: &Self) -> Self {
pub(crate) const fn subtract(&self, rhs: &Self) -> Self {
let (d0, borrow) = sbb(self.0[0], rhs.0[0], 0);
let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow);
let (d2, borrow) = sbb(self.0[2], rhs.0[2], borrow);
@ -596,6 +589,18 @@ impl Fq {
Fq([d0, d1, d2, d3])
}
#[inline]
pub(crate) const fn field_add(&self, rhs: &Self) -> Self {
let (d0, carry) = adc(self.0[0], rhs.0[0], 0);
let (d1, carry) = adc(self.0[1], rhs.0[1], carry);
let (d2, carry) = adc(self.0[2], rhs.0[2], carry);
let (d3, _) = adc(self.0[3], rhs.0[3], carry);
// Attempt to subtract the modulus, to ensure the value
// is smaller than the modulus.
Fq([d0, d1, d2, d3]).subtract(&MODULUS)
}
}
impl<'a> From<&'a Fq> for [u8; 32] {

View File

@ -371,11 +371,11 @@ impl AffinePoint {
/// Performs a pre-processing step that produces an `AffineNielsPoint`
/// for use in multiple additions.
pub fn to_niels(&self) -> AffineNielsPoint {
pub const fn to_niels(&self) -> AffineNielsPoint {
AffineNielsPoint {
v_plus_u: &self.v + &self.u,
v_minus_u: &self.v - &self.u,
t2d: &self.u * &self.v * EDWARDS_D2,
v_plus_u: self.v.field_add(&self.u),
v_minus_u: self.v.subtract(&self.u),
t2d: self.u.multiply(&self.v).multiply(&EDWARDS_D2)
}
}