ff: Add PrimeField: From<u64> constraint

This commit is contained in:
Jack Grigg
2020-03-27 23:19:58 +13:00
parent b6457a905b
commit fd79de5408
11 changed files with 50 additions and 37 deletions

View File

@@ -456,7 +456,7 @@ pub struct Fq(FqRepr);
#[test]
fn test_b_coeff() {
assert_eq!(Fq::from_repr(FqRepr::from(4)).unwrap(), B_COEFF);
assert_eq!(Fq::from(4), B_COEFF);
}
#[test]
@@ -1586,7 +1586,7 @@ fn test_fq_is_valid() {
assert!(!a.is_valid());
a.0.sub_noborrow(&FqRepr::from(1));
assert!(a.is_valid());
assert!(Fq(FqRepr::from(0)).is_valid());
assert!(Fq::from(0).is_valid());
assert!(Fq(FqRepr([
0xdf4671abd14dab3e,
0xe2dc0c9f534fbd33,
@@ -2193,10 +2193,7 @@ fn test_fq_root_of_unity() {
use ff::SqrtField;
assert_eq!(Fq::S, 1);
assert_eq!(
Fq::multiplicative_generator(),
Fq::from_repr(FqRepr::from(2)).unwrap()
);
assert_eq!(Fq::multiplicative_generator(), Fq::from(2));
assert_eq!(
Fq::multiplicative_generator().pow_vartime([
0xdcff7fffffffd555u64,
@@ -2225,9 +2222,7 @@ fn test_fq_ordering() {
// FqRepr's ordering is well-tested, but we still need to make sure the Fq
// elements aren't being compared in Montgomery form.
for i in 0..100 {
assert!(
Fq::from_repr(FqRepr::from(i + 1)).unwrap() > Fq::from_repr(FqRepr::from(i)).unwrap()
);
assert!(Fq::from(i + 1) > Fq::from(i));
}
}

View File

@@ -364,7 +364,7 @@ fn test_fq2_squaring() {
a.square(),
Fq2 {
c0: Fq::zero(),
c1: Fq::from_repr(FqRepr::from(2)).unwrap(),
c1: Fq::from(2),
}
); // 2u

View File

@@ -368,7 +368,7 @@ fn test_fr_is_valid() {
assert!(!a.is_valid());
a.0.sub_noborrow(&FrRepr::from(1));
assert!(a.is_valid());
assert!(Fr(FrRepr::from(0)).is_valid());
assert!(Fr::from(0).is_valid());
assert!(Fr(FrRepr([
0xffffffff00000000,
0x53bda402fffe5bfe,
@@ -961,10 +961,7 @@ fn test_fr_root_of_unity() {
use ff::SqrtField;
assert_eq!(Fr::S, 32);
assert_eq!(
Fr::multiplicative_generator(),
Fr::from_repr(FrRepr::from(7)).unwrap()
);
assert_eq!(Fr::multiplicative_generator(), Fr::from(7));
assert_eq!(
Fr::multiplicative_generator().pow_vartime([
0xfffe5bfeffffffffu64,

View File

@@ -191,7 +191,7 @@ fn test_g1_uncompressed_invalid_vectors() {
loop {
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
x3b.add_assign(&Fq::from(4)); // TODO: perhaps expose coeff_b through API?
let y = x3b.sqrt();
if y.is_some().into() {
@@ -331,8 +331,8 @@ fn test_g2_uncompressed_invalid_vectors() {
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq2 {
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
c1: Fq::from_repr(FqRepr::from(4)).unwrap(),
c0: Fq::from(4),
c1: Fq::from(4),
}); // TODO: perhaps expose coeff_b through API?
let y = x3b.sqrt();
@@ -428,7 +428,7 @@ fn test_g1_compressed_invalid_vectors() {
loop {
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
x3b.add_assign(&Fq::from(4)); // TODO: perhaps expose coeff_b through API?
if x3b.sqrt().is_some().into() {
x.add_assign(&Fq::one());
@@ -452,7 +452,7 @@ fn test_g1_compressed_invalid_vectors() {
loop {
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
x3b.add_assign(&Fq::from(4)); // TODO: perhaps expose coeff_b through API?
if x3b.sqrt().is_some().into() {
// We know this is on the curve, but it's likely not going to be in the correct subgroup.
@@ -558,8 +558,8 @@ fn test_g2_compressed_invalid_vectors() {
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq2 {
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
c1: Fq::from_repr(FqRepr::from(4)).unwrap(),
c0: Fq::from(4),
c1: Fq::from(4),
}); // TODO: perhaps expose coeff_b through API?
if x3b.sqrt().is_some().into() {
@@ -589,8 +589,8 @@ fn test_g2_compressed_invalid_vectors() {
let mut x3b = x.square();
x3b.mul_assign(&x);
x3b.add_assign(&Fq2 {
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
c1: Fq::from_repr(FqRepr::from(4)).unwrap(),
c0: Fq::from(4),
c1: Fq::from(4),
}); // TODO: perhaps expose coeff_b through API?
if x3b.sqrt().is_some().into() {

View File

@@ -119,7 +119,7 @@ pub fn from_str_tests<F: PrimeField>() {
let n = rng.next_u64();
let a = F::from_str(&format!("{}", n)).unwrap();
let b = F::from_repr(n.into()).unwrap();
let b = F::from(n);
assert_eq!(a, b);
}