|
|
@ -118,7 +118,7 @@ impl PrimeFieldRepr for FsRepr { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
|
#[inline(always)] |
|
|
|
fn divn(&mut self, mut n: u32) { |
|
|
|
fn shr(&mut self, mut n: u32) { |
|
|
|
if n >= 64 * 4 { |
|
|
|
if n >= 64 * 4 { |
|
|
|
*self = Self::from(0); |
|
|
|
*self = Self::from(0); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -166,7 +166,7 @@ impl PrimeFieldRepr for FsRepr { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
|
#[inline(always)] |
|
|
|
fn muln(&mut self, mut n: u32) { |
|
|
|
fn shl(&mut self, mut n: u32) { |
|
|
|
if n >= 64 * 4 { |
|
|
|
if n >= 64 * 4 { |
|
|
|
*self = Self::from(0); |
|
|
|
*self = Self::from(0); |
|
|
|
return; |
|
|
|
return; |
|
|
@ -206,25 +206,21 @@ impl PrimeFieldRepr for FsRepr { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
|
#[inline(always)] |
|
|
|
fn add_nocarry(&mut self, other: &FsRepr) -> bool { |
|
|
|
fn add_nocarry(&mut self, other: &FsRepr) { |
|
|
|
let mut carry = 0; |
|
|
|
let mut carry = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (a, b) in self.0.iter_mut().zip(other.0.iter()) { |
|
|
|
for (a, b) in self.0.iter_mut().zip(other.0.iter()) { |
|
|
|
*a = adc(*a, *b, &mut carry); |
|
|
|
*a = adc(*a, *b, &mut carry); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
carry != 0 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
|
#[inline(always)] |
|
|
|
fn sub_noborrow(&mut self, other: &FsRepr) -> bool { |
|
|
|
fn sub_noborrow(&mut self, other: &FsRepr) { |
|
|
|
let mut borrow = 0; |
|
|
|
let mut borrow = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (a, b) in self.0.iter_mut().zip(other.0.iter()) { |
|
|
|
for (a, b) in self.0.iter_mut().zip(other.0.iter()) { |
|
|
|
*a = sbb(*a, *b, &mut borrow); |
|
|
|
*a = sbb(*a, *b, &mut borrow); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
borrow != 0 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -668,29 +664,29 @@ fn test_fs_repr_div2() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_fs_repr_divn() { |
|
|
|
fn test_fs_repr_shr() { |
|
|
|
let mut a = FsRepr([0xb33fbaec482a283f, 0x997de0d3a88cb3df, 0x9af62d2a9a0e5525, 0x36003ab08de70da1]); |
|
|
|
let mut a = FsRepr([0xb33fbaec482a283f, 0x997de0d3a88cb3df, 0x9af62d2a9a0e5525, 0x36003ab08de70da1]); |
|
|
|
a.divn(0); |
|
|
|
a.shr(0); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
a, |
|
|
|
a, |
|
|
|
FsRepr([0xb33fbaec482a283f, 0x997de0d3a88cb3df, 0x9af62d2a9a0e5525, 0x36003ab08de70da1]) |
|
|
|
FsRepr([0xb33fbaec482a283f, 0x997de0d3a88cb3df, 0x9af62d2a9a0e5525, 0x36003ab08de70da1]) |
|
|
|
); |
|
|
|
); |
|
|
|
a.divn(1); |
|
|
|
a.shr(1); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
a, |
|
|
|
a, |
|
|
|
FsRepr([0xd99fdd762415141f, 0xccbef069d44659ef, 0xcd7b16954d072a92, 0x1b001d5846f386d0]) |
|
|
|
FsRepr([0xd99fdd762415141f, 0xccbef069d44659ef, 0xcd7b16954d072a92, 0x1b001d5846f386d0]) |
|
|
|
); |
|
|
|
); |
|
|
|
a.divn(50); |
|
|
|
a.shr(50); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
a, |
|
|
|
a, |
|
|
|
FsRepr([0xbc1a7511967bf667, 0xc5a55341caa4b32f, 0x75611bce1b4335e, 0x6c0]) |
|
|
|
FsRepr([0xbc1a7511967bf667, 0xc5a55341caa4b32f, 0x75611bce1b4335e, 0x6c0]) |
|
|
|
); |
|
|
|
); |
|
|
|
a.divn(130); |
|
|
|
a.shr(130); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
a, |
|
|
|
a, |
|
|
|
FsRepr([0x1d5846f386d0cd7, 0x1b0, 0x0, 0x0]) |
|
|
|
FsRepr([0x1d5846f386d0cd7, 0x1b0, 0x0, 0x0]) |
|
|
|
); |
|
|
|
); |
|
|
|
a.divn(64); |
|
|
|
a.shr(64); |
|
|
|
assert_eq!( |
|
|
|
assert_eq!( |
|
|
|
a, |
|
|
|
a, |
|
|
|
FsRepr([0x1b0, 0x0, 0x0, 0x0]) |
|
|
|
FsRepr([0x1b0, 0x0, 0x0, 0x0]) |
|
|
@ -765,14 +761,6 @@ fn test_fs_repr_sub_noborrow() { |
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(csub_ab, csub_ba); |
|
|
|
assert_eq!(csub_ab, csub_ba); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Subtracting r+1 from r should produce a borrow
|
|
|
|
|
|
|
|
let mut qplusone = FsRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]); |
|
|
|
|
|
|
|
assert!(qplusone.sub_noborrow(&FsRepr([0xffffffff00000002, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Subtracting x from x should produce no borrow
|
|
|
|
|
|
|
|
let mut x = FsRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]); |
|
|
|
|
|
|
|
assert!(!x.sub_noborrow(&FsRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]))) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
@ -835,14 +823,6 @@ fn test_fr_repr_add_nocarry() { |
|
|
|
assert_eq!(abc, cab); |
|
|
|
assert_eq!(abc, cab); |
|
|
|
assert_eq!(abc, cba); |
|
|
|
assert_eq!(abc, cba); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Adding 1 to (2^256 - 1) should produce a carry
|
|
|
|
|
|
|
|
let mut x = FsRepr([0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff]); |
|
|
|
|
|
|
|
assert!(x.add_nocarry(&FsRepr::from(1))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Adding 1 to r should not produce a carry
|
|
|
|
|
|
|
|
let mut x = FsRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]); |
|
|
|
|
|
|
|
assert!(!x.add_nocarry(&FsRepr::from(1))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|