This commit is contained in:
Nicholas Ward 2021-07-21 13:23:50 -07:00
parent 7f92a33964
commit 164bb7f5ca
5 changed files with 49 additions and 16 deletions

View File

@ -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()

View File

@ -90,6 +90,13 @@ impl Field for QuadraticCrandallField {
<Self as FieldExtension<2>>::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()

View File

@ -123,6 +123,20 @@ impl Field for QuarticCrandallField {
<Self as FieldExtension<4>>::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()

View File

@ -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<R: Rng>(rng: &mut R) -> Self;

View File

@ -33,18 +33,22 @@ pub fn test_inputs(modulus: BigUint, word_bits: usize) -> Vec<BigUint> {
// 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::<Vec<BigUint>>();
[basics, diff_max, diff_mod].concat()
@ -74,7 +78,8 @@ pub fn run_unaryop_test_cases<F, UnaryOp, ExpectedOp>(
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<F, BinaryOp, ExpectedOp>(
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);