mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-06 20:20:34 +00:00
fields: Use ff::PrimeField instead of FieldExt where possible
This commit is contained in:
parent
6a47700b1d
commit
8fabb44ad4
3 changed files with 25 additions and 20 deletions
|
|
@ -5,7 +5,8 @@ use core::cmp;
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use core::iter::Sum;
|
use core::iter::Sum;
|
||||||
use core::ops::{Add, Mul, Neg, Sub};
|
use core::ops::{Add, Mul, Neg, Sub};
|
||||||
use ff::Field;
|
|
||||||
|
use ff::{Field, PrimeField};
|
||||||
use group::{
|
use group::{
|
||||||
cofactor::{CofactorCurve, CofactorGroup},
|
cofactor::{CofactorCurve, CofactorGroup},
|
||||||
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
|
prime::{PrimeCurve, PrimeCurveAffine, PrimeGroup},
|
||||||
|
|
@ -68,7 +69,7 @@ macro_rules! new_curve_impl {
|
||||||
let x3 = x.square() * x;
|
let x3 = x.square() * x;
|
||||||
let y = (x3 + $name::curve_constant_b()).sqrt();
|
let y = (x3 + $name::curve_constant_b()).sqrt();
|
||||||
if let Some(y) = Option::<$base>::from(y) {
|
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 y = if ysign ^ sign == 0 { y } else { -y };
|
||||||
|
|
||||||
let p = $name_affine {
|
let p = $name_affine {
|
||||||
|
|
@ -465,7 +466,7 @@ macro_rules! new_curve_impl {
|
||||||
//
|
//
|
||||||
// NOTE: We skip the leading bit because it's always unset.
|
// NOTE: We skip the leading bit because it's always unset.
|
||||||
for bit in other
|
for bit in other
|
||||||
.to_bytes()
|
.to_repr()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8)))
|
.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.
|
// NOTE: We skip the leading bit because it's always unset.
|
||||||
for bit in other
|
for bit in other
|
||||||
.to_bytes()
|
.to_repr()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.flat_map(|byte| (0..8).rev().map(move |i| Choice::from((byte >> i) & 1u8)))
|
.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);
|
let ysign = Choice::from(tmp[31] >> 7);
|
||||||
tmp[31] &= 0b0111_1111;
|
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(|| {
|
CtOption::new(Self::identity(), x.is_zero() & (!ysign)).or_else(|| {
|
||||||
let x3 = x.square() * x;
|
let x3 = x.square() * x;
|
||||||
(x3 + $name::curve_constant_b()).sqrt().and_then(|y| {
|
(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);
|
let y = $base::conditional_select(&y, &-y, ysign ^ sign);
|
||||||
|
|
||||||
|
|
@ -678,8 +679,8 @@ macro_rules! new_curve_impl {
|
||||||
[0; 32]
|
[0; 32]
|
||||||
} else {
|
} else {
|
||||||
let (x, y) = (self.x, self.y);
|
let (x, y) = (self.x, self.y);
|
||||||
let sign = (y.to_bytes()[0] & 1) << 7;
|
let sign = y.is_odd().unwrap_u8() << 7;
|
||||||
let mut xbytes = x.to_bytes();
|
let mut xbytes = x.to_repr();
|
||||||
xbytes[31] |= sign;
|
xbytes[31] |= sign;
|
||||||
xbytes
|
xbytes
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ use core::convert::TryInto;
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use core::ops::{Add, Mul, Neg, Sub};
|
use core::ops::{Add, Mul, Neg, Sub};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
use ff::PrimeField;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
|
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
|
||||||
|
|
||||||
|
|
@ -23,7 +25,7 @@ pub struct Fp(pub(crate) [u64; 4]);
|
||||||
|
|
||||||
impl fmt::Debug for Fp {
|
impl fmt::Debug for Fp {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let tmp = self.to_bytes();
|
let tmp = self.to_repr();
|
||||||
write!(f, "0x")?;
|
write!(f, "0x")?;
|
||||||
for &b in tmp.iter().rev() {
|
for &b in tmp.iter().rev() {
|
||||||
write!(f, "{:02x}", b)?;
|
write!(f, "{:02x}", b)?;
|
||||||
|
|
@ -66,8 +68,8 @@ impl PartialEq for Fp {
|
||||||
|
|
||||||
impl std::cmp::Ord for Fp {
|
impl std::cmp::Ord for Fp {
|
||||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
let left = self.to_bytes();
|
let left = self.to_repr();
|
||||||
let right = other.to_bytes();
|
let right = other.to_repr();
|
||||||
left.iter()
|
left.iter()
|
||||||
.zip(right.iter())
|
.zip(right.iter())
|
||||||
.rev()
|
.rev()
|
||||||
|
|
@ -437,13 +439,13 @@ impl Fp {
|
||||||
|
|
||||||
impl From<Fp> for [u8; 32] {
|
impl From<Fp> for [u8; 32] {
|
||||||
fn from(value: Fp) -> [u8; 32] {
|
fn from(value: Fp) -> [u8; 32] {
|
||||||
value.to_bytes()
|
value.to_repr()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Fp> for [u8; 32] {
|
impl<'a> From<&'a Fp> for [u8; 32] {
|
||||||
fn from(value: &'a Fp) -> [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)]
|
#[cfg(test)]
|
||||||
use ff::{Field, PrimeField};
|
use ff::Field;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inv() {
|
fn test_inv() {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ use core::convert::TryInto;
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use core::ops::{Add, Mul, Neg, Sub};
|
use core::ops::{Add, Mul, Neg, Sub};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
use ff::PrimeField;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
|
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
|
||||||
|
|
||||||
|
|
@ -23,7 +25,7 @@ pub struct Fq(pub(crate) [u64; 4]);
|
||||||
|
|
||||||
impl fmt::Debug for Fq {
|
impl fmt::Debug for Fq {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let tmp = self.to_bytes();
|
let tmp = self.to_repr();
|
||||||
write!(f, "0x")?;
|
write!(f, "0x")?;
|
||||||
for &b in tmp.iter().rev() {
|
for &b in tmp.iter().rev() {
|
||||||
write!(f, "{:02x}", b)?;
|
write!(f, "{:02x}", b)?;
|
||||||
|
|
@ -66,8 +68,8 @@ impl PartialEq for Fq {
|
||||||
|
|
||||||
impl std::cmp::Ord for Fq {
|
impl std::cmp::Ord for Fq {
|
||||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||||
let left = self.to_bytes();
|
let left = self.to_repr();
|
||||||
let right = other.to_bytes();
|
let right = other.to_repr();
|
||||||
left.iter()
|
left.iter()
|
||||||
.zip(right.iter())
|
.zip(right.iter())
|
||||||
.rev()
|
.rev()
|
||||||
|
|
@ -437,13 +439,13 @@ impl Fq {
|
||||||
|
|
||||||
impl From<Fq> for [u8; 32] {
|
impl From<Fq> for [u8; 32] {
|
||||||
fn from(value: Fq) -> [u8; 32] {
|
fn from(value: Fq) -> [u8; 32] {
|
||||||
value.to_bytes()
|
value.to_repr()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Fq> for [u8; 32] {
|
impl<'a> From<&'a Fq> for [u8; 32] {
|
||||||
fn from(value: &'a Fq) -> [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)]
|
#[cfg(test)]
|
||||||
use ff::{Field, PrimeField};
|
use ff::Field;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_inv() {
|
fn test_inv() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue