Skip to content

Commit

Permalink
replace double logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ashWhiteHat committed Jan 28, 2023
1 parent 9578fc8 commit 3036891
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lazy_static::lazy_static;
#[cfg(feature = "bits")]
use ff::{FieldBits, PrimeFieldBits};

use crate::arithmetic::{adc, mac, sbb, SqrtTableHelpers};
use crate::arithmetic::{adc, mac, sbb, shl1, SqrtTableHelpers};

#[cfg(feature = "sqrt-table")]
use crate::arithmetic::SqrtTables;
Expand Down Expand Up @@ -275,8 +275,12 @@ impl Fp {
/// Doubles this field element.
#[inline]
pub const fn double(&self) -> Fp {
// TODO: This can be achieved more efficiently with a bitshift.
self.add(self)
let (d0, c) = shl1(self.0[0], 0);
let (d1, c) = shl1(self.0[1], c);
let (d2, c) = shl1(self.0[2], c);
let (d3, _) = shl1(self.0[3], c);

(&Fp([d0, d1, d2, d3])).sub(&MODULUS)
}

fn from_u512(limbs: [u64; 8]) -> Fp {
Expand Down
12 changes: 9 additions & 3 deletions src/fields/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lazy_static::lazy_static;
#[cfg(feature = "bits")]
use ff::{FieldBits, PrimeFieldBits};

use crate::arithmetic::{adc, mac, sbb, SqrtTableHelpers};
use crate::arithmetic::{adc, mac, sbb, shl1, SqrtTableHelpers};

#[cfg(feature = "sqrt-table")]
use crate::arithmetic::SqrtTables;
Expand Down Expand Up @@ -275,8 +275,14 @@ impl Fq {
/// Doubles this field element.
#[inline]
pub const fn double(&self) -> Fq {
// TODO: This can be achieved more efficiently with a bitshift.
self.add(self)
let (d0, carry) = shl1(self.0[0], 0);
let (d1, carry) = shl1(self.0[1], carry);
let (d2, carry) = shl1(self.0[2], carry);
let (d3, _) = shl1(self.0[3], carry);

// Attempt to subtract the modulus, to ensure the value
// is smaller than the modulus.
(&Fq([d0, d1, d2, d3])).sub(&MODULUS)
}

fn from_u512(limbs: [u64; 8]) -> Fq {
Expand Down

0 comments on commit 3036891

Please sign in to comment.