Skip to content

Commit

Permalink
minor changes for bigint
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-chuang committed Feb 6, 2021
1 parent 1242d82 commit 5dfe4ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions ff-asm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn x86_64_asm_mul(input: TokenStream) -> TokenStream {
let inner_ts: Expr = syn::parse_str(&impl_block).unwrap();
let ts = quote::quote! {
let a = &mut #a;
let b = #b;
let b = &#b;
#inner_ts
};
ts.into()
Expand Down Expand Up @@ -290,7 +290,7 @@ fn generate_impl(num_limbs: usize, is_mul: bool) -> String {
let mut ctx = Context::new();
ctx.add_declaration("a", "r", "a");
if is_mul {
ctx.add_declaration("b", "r", "&b");
ctx.add_declaration("b", "r", "b");
}
ctx.add_declaration("modulus", "r", "&P::MODULUS.0");
ctx.add_declaration("0", "i", "0u64");
Expand Down
30 changes: 26 additions & 4 deletions ff/src/biginteger/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ macro_rules! bigint_impl {
impl BigInteger for $name {
const NUM_LIMBS: usize = $num_limbs;

#[inline]
#[ark_ff_asm::unroll_for_loops]
fn add_nocarry(&mut self, other: &Self) -> bool {
let mut carry = 0;

for i in 0..$num_limbs {
#[cfg(all(target_arch = "x86_64", feature = "asm"))]
#[cfg_attr(all(target_arch = "x86_64", feature = "asm"), allow(unsafe_code))]
#[allow(unsafe_code)]
unsafe {
carry = core::arch::x86_64::_addcarry_u64(
carry,
Expand All @@ -28,7 +29,7 @@ macro_rules! bigint_impl {
)
};

#[cfg(not(feature = "asm"))]
#[cfg(not(all(target_arch = "x86_64", feature = "asm")))]
{
self.0[i] = adc!(self.0[i], other.0[i], &mut carry);
}
Expand All @@ -37,13 +38,14 @@ macro_rules! bigint_impl {
carry != 0
}

#[inline]
#[ark_ff_asm::unroll_for_loops]
fn sub_noborrow(&mut self, other: &Self) -> bool {
let mut borrow = 0;

for i in 0..$num_limbs {
#[cfg(all(target_arch = "x86_64", feature = "asm"))]
#[cfg_attr(all(target_arch = "x86_64", feature = "asm"), allow(unsafe_code))]
#[allow(unsafe_code)]
unsafe {
borrow = core::arch::x86_64::_subborrow_u64(
borrow,
Expand All @@ -53,7 +55,7 @@ macro_rules! bigint_impl {
)
};

#[cfg(not(feature = "asm"))]
#[cfg(not(all(target_arch = "x86_64", feature = "asm")))]
{
self.0[i] = sbb!(self.0[i], other.0[i], &mut borrow);
}
Expand All @@ -66,6 +68,24 @@ macro_rules! bigint_impl {
#[ark_ff_asm::unroll_for_loops]
#[allow(unused)]
fn mul2(&mut self) {
#[cfg(all(target_arch = "x86_64", feature = "asm"))]
#[allow(unsafe_code)]
{
let mut carry = 0;

for i in 0..$num_limbs {
unsafe {
carry = core::arch::x86_64::_addcarry_u64(
carry,
self.0[i],
self.0[i],
&mut self.0[i],
);
}
}
return;
}

let mut last = 0;
for i in 0..$num_limbs {
let a = &mut self.0[i];
Expand All @@ -76,6 +96,7 @@ macro_rules! bigint_impl {
}
}

#[inline]
#[ark_ff_asm::unroll_for_loops]
fn muln(&mut self, mut n: u32) {
if n >= 64 * $num_limbs {
Expand Down Expand Up @@ -118,6 +139,7 @@ macro_rules! bigint_impl {
}
}

#[inline]
#[ark_ff_asm::unroll_for_loops]
fn divn(&mut self, mut n: u32) {
if n >= 64 * $num_limbs {
Expand Down

0 comments on commit 5dfe4ee

Please sign in to comment.