From 8fabb44ad4d2bfb2ee309838e52c3834b06be2e0 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 20 Sep 2021 17:26:34 +0100 Subject: [PATCH] fields: Use `ff::PrimeField` instead of `FieldExt` where possible --- src/curves.rs | 17 +++++++++-------- src/fields/fp.rs | 14 ++++++++------ src/fields/fq.rs | 14 ++++++++------ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/curves.rs b/src/curves.rs index 93778e2..dc01128 100644 --- a/src/curves.rs +++ b/src/curves.rs @@ -5,7 +5,8 @@ use core::cmp; use core::fmt::Debug; use core::iter::Sum; use core::ops::{Add, Mul, Neg, Sub}; -use ff::Field; + +use ff::{Field, PrimeField}; use group::{ cofactor::{CofactorCurve, CofactorGroup}, prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup}, @@ -68,7 +69,7 @@ macro_rules! new_curve_impl { let x3 = x.square() * x; let y = (x3 + $name::curve_constant_b()).sqrt(); if let Some(y) = Option::<$base>::from(y) { - let sign = y.to_bytes()[0] & 1; + let sign = y.is_odd().unwrap_u8(); let y = if ysign ^ sign == 0 { y } else { -y }; let p = $name_affine { @@ -465,7 +466,7 @@ macro_rules! new_curve_impl { // // NOTE: We skip the leading bit because it's always unset. for bit in other - .to_bytes() + .to_repr() .iter() .rev() .flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8))) @@ -576,7 +577,7 @@ macro_rules! new_curve_impl { // // NOTE: We skip the leading bit because it's always unset. for bit in other - .to_bytes() + .to_repr() .iter() .rev() .flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8))) @@ -646,11 +647,11 @@ macro_rules! new_curve_impl { let ysign = Choice::from(tmp[31] >> 7); tmp[31] &= 0b0111_1111; - $base::from_bytes(&tmp).and_then(|x| { + $base::from_repr(tmp).and_then(|x| { CtOption::new(Self::identity(), x.is_zero() & (!ysign)).or_else(|| { let x3 = x.square() * x; (x3 + $name::curve_constant_b()).sqrt().and_then(|y| { - let sign = Choice::from(y.to_bytes()[0] & 1); + let sign = y.is_odd(); let y = $base::conditional_select(&y, &-y, ysign ^ sign); @@ -678,8 +679,8 @@ macro_rules! new_curve_impl { [0; 32] } else { let (x, y) = (self.x, self.y); - let sign = (y.to_bytes()[0] & 1) << 7; - let mut xbytes = x.to_bytes(); + let sign = y.is_odd().unwrap_u8() << 7; + let mut xbytes = x.to_repr(); xbytes[31] |= sign; xbytes } diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 065a453..b8573c1 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -2,6 +2,8 @@ use core::convert::TryInto; use core::fmt; use core::ops::{Add, Mul, Neg, Sub}; use lazy_static::lazy_static; + +use ff::PrimeField; use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -23,7 +25,7 @@ pub struct Fp(pub(crate) [u64; 4]); impl fmt::Debug for Fp { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let tmp = self.to_bytes(); + let tmp = self.to_repr(); write!(f, "0x")?; for &b in tmp.iter().rev() { write!(f, "{:02x}", b)?; @@ -66,8 +68,8 @@ impl PartialEq for Fp { impl std::cmp::Ord for Fp { fn cmp(&self, other: &Self) -> std::cmp::Ordering { - let left = self.to_bytes(); - let right = other.to_bytes(); + let left = self.to_repr(); + let right = other.to_repr(); left.iter() .zip(right.iter()) .rev() @@ -437,13 +439,13 @@ impl Fp { impl From for [u8; 32] { fn from(value: Fp) -> [u8; 32] { - value.to_bytes() + value.to_repr() } } impl<'a> From<&'a Fp> for [u8; 32] { fn from(value: &'a Fp) -> [u8; 32] { - value.to_bytes() + value.to_repr() } } @@ -767,7 +769,7 @@ impl FieldExt for Fp { } #[cfg(test)] -use ff::{Field, PrimeField}; +use ff::Field; #[test] fn test_inv() { diff --git a/src/fields/fq.rs b/src/fields/fq.rs index c8be606..6c2518f 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -2,6 +2,8 @@ use core::convert::TryInto; use core::fmt; use core::ops::{Add, Mul, Neg, Sub}; use lazy_static::lazy_static; + +use ff::PrimeField; use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -23,7 +25,7 @@ pub struct Fq(pub(crate) [u64; 4]); impl fmt::Debug for Fq { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let tmp = self.to_bytes(); + let tmp = self.to_repr(); write!(f, "0x")?; for &b in tmp.iter().rev() { write!(f, "{:02x}", b)?; @@ -66,8 +68,8 @@ impl PartialEq for Fq { impl std::cmp::Ord for Fq { fn cmp(&self, other: &Self) -> std::cmp::Ordering { - let left = self.to_bytes(); - let right = other.to_bytes(); + let left = self.to_repr(); + let right = other.to_repr(); left.iter() .zip(right.iter()) .rev() @@ -437,13 +439,13 @@ impl Fq { impl From for [u8; 32] { fn from(value: Fq) -> [u8; 32] { - value.to_bytes() + value.to_repr() } } impl<'a> From<&'a Fq> for [u8; 32] { fn from(value: &'a Fq) -> [u8; 32] { - value.to_bytes() + value.to_repr() } } @@ -767,7 +769,7 @@ impl FieldExt for Fq { } #[cfg(test)] -use ff::{Field, PrimeField}; +use ff::Field; #[test] fn test_inv() {