From a0fcf717c82a5d1587f36a9d309b1b2de4b4a3b8 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Tue, 13 Feb 2018 16:41:10 -0700 Subject: [PATCH] add_nocarry and sub_noborrow should no longer return anything. --- src/bls12_381/fq.rs | 26 ++++++++------------------ src/bls12_381/fr.rs | 26 ++++++++------------------ src/lib.rs | 8 ++++---- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/src/bls12_381/fq.rs b/src/bls12_381/fq.rs index 749f251..ffb2356 100644 --- a/src/bls12_381/fq.rs +++ b/src/bls12_381/fq.rs @@ -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] diff --git a/src/bls12_381/fr.rs b/src/bls12_381/fr.rs index 96ef599..9966a67 100644 --- a/src/bls12_381/fr.rs +++ b/src/bls12_381/fr.rs @@ -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] diff --git a/src/lib.rs b/src/lib.rs index b61fbc7..dab1277 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -352,11 +352,11 @@ pub trait PrimeFieldRepr: Sized + AsMut<[u64]> + From { - /// 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.