Skip to content

Commit

Permalink
Add Fp64
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Dec 16, 2020
1 parent 5fede71 commit 27dcb65
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
36 changes: 28 additions & 8 deletions ff/src/fields/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@
/// `P::MODULUS` has (a) a non-zero MSB, and (b) at least one
/// zero bit in the rest of the modulus.
macro_rules! impl_field_mul_assign {
(1) => {
fn mul_assign(&mut self, other: &Self) {
#[inline(always)]
fn mont_reduce(&mut self, mul_result: u128) {
let m = (mul_result as u64).wrapping_mul(P::INV) as u128;
(self.0).0 = ((mul_result + m * u128::from(P::MODULUS.0)) >> 64) as u64;
self.reduce();
}

let prod = (self.0)[0] as u128 * (other.0)[0] as u128;
self.mont_reduce(prod);
}
};
($limbs:expr) => {
#[inline]
#[ark_ff_asm::unroll_for_loops]
fn mul_assign(&mut self, other: &Self) {
// Checking the modulus at compile time
let first_bit_set = P::MODULUS.0[$limbs - 1] >> 63 != 0;
#[allow(unused_mut)]
let mut all_bits_set = P::MODULUS.0[$limbs - 1] == !0 - (1 << 63);
for i in 1..$limbs {
all_bits_set &= P::MODULUS.0[$limbs - i - 1] == !0u64;
Expand Down Expand Up @@ -80,22 +94,28 @@ macro_rules! impl_field_into_repr {
}

macro_rules! impl_field_square_in_place {
(1) => {
fn square_in_place(&mut self) -> &mut Self {
*self = *self * &self;
self
}
};
($limbs: expr) => {
#[inline]
#[ark_ff_asm::unroll_for_loops]
#[allow(unused_braces, clippy::absurd_extreme_comparisons)]
fn square_in_place(&mut self) -> &mut Self {
// Checking the modulus at compile time
let first_bit_set = P::MODULUS.0[$limbs - 1] >> 63 != 0;
let mut all_bits_set = P::MODULUS.0[$limbs - 1] == !0 - (1 << 63);
for i in 1..$limbs {
all_bits_set &= P::MODULUS.0[$limbs - i - 1] == core::u64::MAX;
}
let _no_carry: bool = !(first_bit_set || all_bits_set);

#[cfg(use_asm)]
#[allow(unsafe_code, unused_mut)]
{
// Checking the modulus at compile time
let first_bit_set = P::MODULUS.0[$limbs - 1] >> 63 != 0;
let mut all_bits_set = P::MODULUS.0[$limbs - 1] == !0 - (1 << 63);
for i in 1..$limbs {
all_bits_set &= P::MODULUS.0[$limbs - i - 1] == core::u64::MAX;
}
let _no_carry: bool = !(first_bit_set || all_bits_set);

if $limbs <= 6 && _no_carry {
assert!($limbs <= 6);
ark_ff_asm::x86_64_asm_square!($limbs, (self.0).0);
Expand Down
3 changes: 2 additions & 1 deletion ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,10 @@ impl<Slice: AsRef<[u64]>> Iterator for BitIteratorLE<Slice> {
}

use crate::biginteger::{
BigInteger256, BigInteger320, BigInteger384, BigInteger768, BigInteger832,
BigInteger256, BigInteger320, BigInteger384, BigInteger64, BigInteger768, BigInteger832,
};

impl_field_bigint_conv!(Fp64, BigInteger64, Fp64Parameters);
impl_field_bigint_conv!(Fp256, BigInteger256, Fp256Parameters);
impl_field_bigint_conv!(Fp320, BigInteger320, Fp320Parameters);
impl_field_bigint_conv!(Fp384, BigInteger384, Fp384Parameters);
Expand Down
3 changes: 2 additions & 1 deletion ff/src/fields/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use num_traits::{One, Zero};
use crate::{
biginteger::{
arithmetic as fa, BigInteger as _BigInteger, BigInteger256, BigInteger320, BigInteger384,
BigInteger768, BigInteger832,
BigInteger64, BigInteger768, BigInteger832,
},
bytes::{FromBytes, ToBytes},
fields::{FftField, Field, FpParameters, LegendreSymbol, PrimeField, SquareRootField},
};

impl_Fp!(Fp64, Fp64Parameters, BigInteger64, BigInteger64, 1);
impl_Fp!(Fp256, Fp256Parameters, BigInteger256, BigInteger256, 4);
impl_Fp!(Fp320, Fp320Parameters, BigInteger320, BigInteger320, 5);
impl_Fp!(Fp384, Fp384Parameters, BigInteger384, BigInteger384, 6);
Expand Down

0 comments on commit 27dcb65

Please sign in to comment.