From 164bb7f5ca137aa3bc3d69aceec162ad728c8052 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 21 Jul 2021 13:23:50 -0700 Subject: [PATCH] fixes --- src/field/crandall_field.rs | 4 +++ src/field/extension_field/quadratic.rs | 7 +++++ src/field/extension_field/quartic.rs | 14 ++++++++++ src/field/field.rs | 2 ++ src/field/field_testing.rs | 38 +++++++++++++++----------- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/field/crandall_field.rs b/src/field/crandall_field.rs index 2afd00c6..3e84ae41 100644 --- a/src/field/crandall_field.rs +++ b/src/field/crandall_field.rs @@ -247,6 +247,10 @@ impl Field for CrandallField { Self(n) } + fn to_canonical_biguint(&self) -> BigUint { + BigUint::from(self.to_canonical_u64()) + } + fn from_canonical_biguint(n: BigUint) -> Self { let last_two: Vec<_> = n .to_u32_digits() diff --git a/src/field/extension_field/quadratic.rs b/src/field/extension_field/quadratic.rs index 5424193c..3d58e34e 100644 --- a/src/field/extension_field/quadratic.rs +++ b/src/field/extension_field/quadratic.rs @@ -90,6 +90,13 @@ impl Field for QuadraticCrandallField { >::BaseField::from_canonical_u64(n).into() } + fn to_canonical_biguint(&self) -> BigUint { + let first = self.0[0].to_canonical_u64(); + let second = self.0[1].to_canonical_u64(); + let combined = second as u128 * (1u128 << 64) + first as u128; + BigUint::from(combined) + } + fn from_canonical_biguint(n: BigUint) -> Self { let last_four: Vec<_> = n .to_u32_digits() diff --git a/src/field/extension_field/quartic.rs b/src/field/extension_field/quartic.rs index 63d7ef75..206d07c8 100644 --- a/src/field/extension_field/quartic.rs +++ b/src/field/extension_field/quartic.rs @@ -123,6 +123,20 @@ impl Field for QuarticCrandallField { >::BaseField::from_canonical_u64(n).into() } + fn to_canonical_biguint(&self) -> BigUint { + let first = self.0[0].to_canonical_u64(); + let second = self.0[1].to_canonical_u64(); + let third = self.0[2].to_canonical_u64(); + let fourth = self.0[2].to_canonical_u64(); + + let combined_first = second as u128 * (1u128 << 64) + first as u128; + let combined_second = fourth as u128 * (1u128 << 64) + third as u128; + + let combined = BigUint::from(combined_second) * (BigUint::from(1u32) << 128) + combined_first; + + combined + } + fn from_canonical_biguint(n: BigUint) -> Self { let last_eight: Vec<_> = n .to_u32_digits() diff --git a/src/field/field.rs b/src/field/field.rs index b69c8750..3f77fc1e 100644 --- a/src/field/field.rs +++ b/src/field/field.rs @@ -185,6 +185,8 @@ pub trait Field: Self::from_canonical_u64(n as u64) } + fn to_canonical_biguint(&self) -> BigUint; + fn from_canonical_biguint(n: BigUint) -> Self; fn rand_from_rng(rng: &mut R) -> Self; diff --git a/src/field/field_testing.rs b/src/field/field_testing.rs index 7447fae6..44ed8d81 100644 --- a/src/field/field_testing.rs +++ b/src/field/field_testing.rs @@ -33,18 +33,22 @@ pub fn test_inputs(modulus: BigUint, word_bits: usize) -> Vec { // Inputs 'difference from' maximum value let diff_max = basic_inputs .iter() - .map(|&x| word_max - x) - .filter(|&x| BigUint::from(x) < modulus) + .map(|x| x.clone()) + .map(|x| word_max.clone() - x) + .filter(|x| x < &modulus) .collect(); // Inputs 'difference from' modulus value let diff_mod = basic_inputs .iter() - .filter(|&&x| BigUint::from(x) < modulus && x != BigUint::from(0u32)) - .map(|&x| modulus - x) + .map(|x| x.clone()) + .filter(|&x| x < modulus && x != BigUint::from(0u32)) + .map(|x| x.clone()) + .map(|x| modulus - x) .collect(); let basics = basic_inputs .into_iter() - .filter(|&x| BigUint::from(x) < modulus) + .map(|x| x.clone()) + .filter(|x| x < &modulus) .collect::>(); [basics, diff_max, diff_mod].concat() @@ -74,7 +78,8 @@ pub fn run_unaryop_test_cases( let expected: Vec<_> = inputs.iter().map(|&x| expected_op(x)).collect(); let output: Vec<_> = inputs .iter() - .map(|&x| op(F::from_canonical_biguint(x)).to_canonical_biguint()) + .map(|x| x.clone()) + .map(|x| op(F::from_canonical_biguint(x)).to_canonical_biguint()) .collect(); // Compare expected outputs with actual outputs for i in 0..inputs.len() { @@ -124,8 +129,9 @@ pub fn run_binaryop_test_cases( let output: Vec<_> = inputs .iter() .zip(shifted_inputs.clone()) - .map(|(&x, &y)| { - op(F::from_canonical_biguint(x), F::from_canonical_biguint(y)).to_canonical_u64() + .map(|(x, y)| (x.clone(), y.clone())) + .map(|(x, y)| { + op(F::from_canonical_biguint(x), F::from_canonical_biguint(y)).to_canonical_biguint() }) .collect(); @@ -205,7 +211,7 @@ macro_rules! test_arithmetic { modulus, WORD_BITS, |x: $field| x.square(), - |x| x * x, + |x| x.clone() * x, ) } @@ -217,13 +223,13 @@ macro_rules! test_arithmetic { assert_eq!(zero.try_inverse(), None); - for &x in &[ + for x in [ BigUint::from(1u32), BigUint::from(2u32), BigUint::from(3u32), - order - 3u32, - order - 2u32, - order - 1u32, + order.clone() - 3u32, + order.clone() - 2u32, + order.clone() - 1u32, ] { let x = <$field>::from_canonical_biguint(x); let inv = x.inverse(); @@ -256,12 +262,12 @@ macro_rules! test_arithmetic { let zero = <$field>::ZERO; let order = <$field>::order(); - for &i in &[ + for i in [ BigUint::from(0u32), BigUint::from(1u32), BigUint::from(2u32), - order - 2u32, - order - 1u32, + order.clone() - 2u32, + order.clone() - 1u32, ] { let i_f = <$field>::from_canonical_biguint(i); assert_eq!(i_f + -i_f, zero);