add_nocarry and sub_noborrow should no longer return anything.

This commit is contained in:
Sean Bowe 2018-02-13 16:41:10 -07:00
parent a8583dd818
commit a0fcf717c8
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
3 changed files with 20 additions and 40 deletions

View File

@ -364,25 +364,21 @@ impl PrimeFieldRepr for FqRepr {
}
#[inline(always)]
fn add_nocarry(&mut self, other: &FqRepr) -> bool {
fn add_nocarry(&mut self, other: &FqRepr) {
let mut carry = 0;
for (a, b) in self.0.iter_mut().zip(other.0.iter()) {
*a = ::adc(*a, *b, &mut carry);
}
carry != 0
}
#[inline(always)]
fn sub_noborrow(&mut self, other: &FqRepr) -> bool {
fn sub_noborrow(&mut self, other: &FqRepr) {
let mut borrow = 0;
for (a, b) in self.0.iter_mut().zip(other.0.iter()) {
*a = ::sbb(*a, *b, &mut borrow);
}
borrow != 0
}
}
@ -1067,13 +1063,10 @@ fn test_fq_repr_sub_noborrow() {
assert_eq!(csub_ab, csub_ba);
}
// Subtracting q+1 from q should produce a borrow
// Subtracting q+1 from q should produce -1 (mod 2**384)
let mut qplusone = FqRepr([0xb9feffffffffaaab, 0x1eabfffeb153ffff, 0x6730d2a0f6b0f624, 0x64774b84f38512bf, 0x4b1ba7b6434bacd7, 0x1a0111ea397fe69a]);
assert!(qplusone.sub_noborrow(&FqRepr([0xb9feffffffffaaac, 0x1eabfffeb153ffff, 0x6730d2a0f6b0f624, 0x64774b84f38512bf, 0x4b1ba7b6434bacd7, 0x1a0111ea397fe69a])));
// Subtracting x from x should produce no borrow
let mut x = FqRepr([0xb9feffffffffaaac, 0x1eabfffeb153ffff, 0x6730d2a0f6b0f624, 0x64774b84f38512bf, 0x4b1ba7b6434bacd7, 0x1a0111ea397fe69a]);
assert!(!x.sub_noborrow(&FqRepr([0xb9feffffffffaaac, 0x1eabfffeb153ffff, 0x6730d2a0f6b0f624, 0x64774b84f38512bf, 0x4b1ba7b6434bacd7, 0x1a0111ea397fe69a])))
qplusone.sub_noborrow(&FqRepr([0xb9feffffffffaaac, 0x1eabfffeb153ffff, 0x6730d2a0f6b0f624, 0x64774b84f38512bf, 0x4b1ba7b6434bacd7, 0x1a0111ea397fe69a]));
assert_eq!(qplusone, FqRepr([0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff]));
}
#[test]
@ -1126,13 +1119,10 @@ fn test_fq_repr_add_nocarry() {
assert_eq!(abc, cba);
}
// Adding 1 to (2^384 - 1) should produce a carry
// Adding 1 to (2^384 - 1) should produce zero
let mut x = FqRepr([0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff]);
assert!(x.add_nocarry(&FqRepr::from(1)));
// Adding 1 to q should not produce a carry
let mut x = FqRepr([0xb9feffffffffaaab, 0x1eabfffeb153ffff, 0x6730d2a0f6b0f624, 0x64774b84f38512bf, 0x4b1ba7b6434bacd7, 0x1a0111ea397fe69a]);
assert!(!x.add_nocarry(&FqRepr::from(1)));
x.add_nocarry(&FqRepr::from(1));
assert!(x.is_zero());
}
#[test]

View File

@ -201,25 +201,21 @@ impl PrimeFieldRepr for FrRepr {
}
#[inline(always)]
fn add_nocarry(&mut self, other: &FrRepr) -> bool {
fn add_nocarry(&mut self, other: &FrRepr) {
let mut carry = 0;
for (a, b) in self.0.iter_mut().zip(other.0.iter()) {
*a = ::adc(*a, *b, &mut carry);
}
carry != 0
}
#[inline(always)]
fn sub_noborrow(&mut self, other: &FrRepr) -> bool {
fn sub_noborrow(&mut self, other: &FrRepr) {
let mut borrow = 0;
for (a, b) in self.0.iter_mut().zip(other.0.iter()) {
*a = ::sbb(*a, *b, &mut borrow);
}
borrow != 0
}
}
@ -772,13 +768,10 @@ fn test_fr_repr_sub_noborrow() {
assert_eq!(csub_ab, csub_ba);
}
// Subtracting r+1 from r should produce a borrow
// Subtracting r+1 from r should produce -1 (mod 2**256)
let mut qplusone = FrRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]);
assert!(qplusone.sub_noborrow(&FrRepr([0xffffffff00000002, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48])));
// Subtracting x from x should produce no borrow
let mut x = FrRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]);
assert!(!x.sub_noborrow(&FrRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48])))
qplusone.sub_noborrow(&FrRepr([0xffffffff00000002, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]));
assert_eq!(qplusone, FrRepr([0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff]));
}
#[test]
@ -842,13 +835,10 @@ fn test_fr_repr_add_nocarry() {
assert_eq!(abc, cba);
}
// Adding 1 to (2^256 - 1) should produce a carry
// Adding 1 to (2^256 - 1) should produce zero
let mut x = FrRepr([0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff]);
assert!(x.add_nocarry(&FrRepr::from(1)));
// Adding 1 to r should not produce a carry
let mut x = FrRepr([0xffffffff00000001, 0x53bda402fffe5bfe, 0x3339d80809a1d805, 0x73eda753299d7d48]);
assert!(!x.add_nocarry(&FrRepr::from(1)));
x.add_nocarry(&FrRepr::from(1));
assert!(x.is_zero());
}
#[test]

View File

@ -352,11 +352,11 @@ pub trait PrimeFieldRepr: Sized +
AsMut<[u64]> +
From<u64>
{
/// Subtract another represetation from this one, returning the borrow bit.
fn sub_noborrow(&mut self, other: &Self) -> bool;
/// Subtract another represetation from this one.
fn sub_noborrow(&mut self, other: &Self);
/// Add another representation to this one, returning the carry bit.
fn add_nocarry(&mut self, other: &Self) -> bool;
/// Add another representation to this one.
fn add_nocarry(&mut self, other: &Self);
/// Compute the number of bits needed to encode this number. Always a
/// multiple of 64.