Change AffinePoint::to_niels to be a const fn.
This commit is contained in:
parent
8f6d6298d0
commit
4aebd80105
25
src/fq.rs
25
src/fq.rs
|
@ -109,14 +109,7 @@ impl<'a, 'b> Add<&'b Fq> for &'a Fq {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn add(self, rhs: &'b Fq) -> Fq {
|
fn add(self, rhs: &'b Fq) -> Fq {
|
||||||
let (d0, carry) = adc(self.0[0], rhs.0[0], 0);
|
self.field_add(rhs)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -554,7 +547,7 @@ impl Fq {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
const fn multiply(&self, rhs: &Self) -> Self {
|
pub(crate) const fn multiply(&self, rhs: &Self) -> Self {
|
||||||
// Schoolbook multiplication
|
// Schoolbook multiplication
|
||||||
|
|
||||||
let (r0, carry) = mac(0, self.0[0], rhs.0[0], 0);
|
let (r0, carry) = mac(0, self.0[0], rhs.0[0], 0);
|
||||||
|
@ -581,7 +574,7 @@ impl Fq {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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 (d0, borrow) = sbb(self.0[0], rhs.0[0], 0);
|
||||||
let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow);
|
let (d1, borrow) = sbb(self.0[1], rhs.0[1], borrow);
|
||||||
let (d2, borrow) = sbb(self.0[2], rhs.0[2], borrow);
|
let (d2, borrow) = sbb(self.0[2], rhs.0[2], borrow);
|
||||||
|
@ -596,6 +589,18 @@ impl Fq {
|
||||||
|
|
||||||
Fq([d0, d1, d2, d3])
|
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] {
|
impl<'a> From<&'a Fq> for [u8; 32] {
|
||||||
|
|
|
@ -371,11 +371,11 @@ impl AffinePoint {
|
||||||
|
|
||||||
/// Performs a pre-processing step that produces an `AffineNielsPoint`
|
/// Performs a pre-processing step that produces an `AffineNielsPoint`
|
||||||
/// for use in multiple additions.
|
/// for use in multiple additions.
|
||||||
pub fn to_niels(&self) -> AffineNielsPoint {
|
pub const fn to_niels(&self) -> AffineNielsPoint {
|
||||||
AffineNielsPoint {
|
AffineNielsPoint {
|
||||||
v_plus_u: &self.v + &self.u,
|
v_plus_u: self.v.field_add(&self.u),
|
||||||
v_minus_u: &self.v - &self.u,
|
v_minus_u: self.v.subtract(&self.u),
|
||||||
t2d: &self.u * &self.v * EDWARDS_D2,
|
t2d: self.u.multiply(&self.v).multiply(&EDWARDS_D2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue