Merge pull request #1092 from matthiasgoergens/matthias/move_to_field

Move operations to Field
This commit is contained in:
Nicholas Ward 2023-07-11 12:40:49 -07:00 committed by GitHub
commit 398f86af1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 24 deletions

View File

@ -109,6 +109,14 @@ impl<F: Extendable<2>> Field for QuadraticExtension<F> {
fn from_noncanonical_u128(n: u128) -> Self {
F::from_noncanonical_u128(n).into()
}
fn from_noncanonical_i64(n: i64) -> Self {
F::from_noncanonical_i64(n).into()
}
fn from_noncanonical_u64(n: u64) -> Self {
F::from_noncanonical_u64(n).into()
}
}
impl<F: Extendable<2>> Display for QuadraticExtension<F> {

View File

@ -119,6 +119,14 @@ impl<F: Extendable<4>> Field for QuarticExtension<F> {
fn from_noncanonical_u128(n: u128) -> Self {
F::from_noncanonical_u128(n).into()
}
fn from_noncanonical_i64(n: i64) -> Self {
F::from_noncanonical_i64(n).into()
}
fn from_noncanonical_u64(n: u64) -> Self {
F::from_noncanonical_u64(n).into()
}
}
impl<F: Extendable<4>> Display for QuarticExtension<F> {

View File

@ -126,6 +126,14 @@ impl<F: Extendable<5>> Field for QuinticExtension<F> {
fn from_noncanonical_u128(n: u128) -> Self {
F::from_noncanonical_u128(n).into()
}
fn from_noncanonical_i64(n: i64) -> Self {
F::from_noncanonical_i64(n).into()
}
fn from_noncanonical_u64(n: u64) -> Self {
F::from_noncanonical_u64(n).into()
}
}
impl<F: Extendable<5>> Display for QuinticExtension<F> {

View File

@ -118,22 +118,6 @@ impl Field for GoldilocksField {
reduce128(n)
}
#[inline]
fn multiply_accumulate(&self, x: Self, y: Self) -> Self {
// u64 + u64 * u64 cannot overflow.
reduce128((self.0 as u128) + (x.0 as u128) * (y.0 as u128))
}
}
impl PrimeField for GoldilocksField {
fn to_canonical_biguint(&self) -> BigUint {
self.to_canonical_u64().into()
}
}
impl Field64 for GoldilocksField {
const ORDER: u64 = 0xFFFFFFFF00000001;
#[inline]
fn from_noncanonical_u64(n: u64) -> Self {
Self(n)
@ -151,6 +135,22 @@ impl Field64 for GoldilocksField {
})
}
#[inline]
fn multiply_accumulate(&self, x: Self, y: Self) -> Self {
// u64 + u64 * u64 cannot overflow.
reduce128((self.0 as u128) + (x.0 as u128) * (y.0 as u128))
}
}
impl PrimeField for GoldilocksField {
fn to_canonical_biguint(&self) -> BigUint {
self.to_canonical_u64().into()
}
}
impl Field64 for GoldilocksField {
const ORDER: u64 = 0xFFFFFFFF00000001;
#[inline]
unsafe fn add_canonical_u64(&self, rhs: u64) -> Self {
let (res_wrapped, carry) = self.0.overflowing_add(rhs);

View File

@ -142,6 +142,19 @@ impl Field for Secp256K1Base {
fn from_noncanonical_u96(n: (u64, u32)) -> Self {
Self([n.0, n.1 as u64, 0, 0])
}
fn from_noncanonical_i64(n: i64) -> Self {
let f = Self::from_canonical_u64(n.unsigned_abs());
if n < 0 {
-f
} else {
f
}
}
fn from_noncanonical_u64(n: u64) -> Self {
Self::from_canonical_u64(n)
}
}
impl PrimeField for Secp256K1Base {

View File

@ -150,6 +150,19 @@ impl Field for Secp256K1Scalar {
fn from_noncanonical_u96(n: (u64, u32)) -> Self {
Self([n.0, n.1 as u64, 0, 0])
}
fn from_noncanonical_i64(n: i64) -> Self {
let f = Self::from_canonical_u64(n.unsigned_abs());
if n < 0 {
-f
} else {
f
}
}
fn from_noncanonical_u64(n: u64) -> Self {
Self::from_canonical_u64(n)
}
}
impl PrimeField for Secp256K1Scalar {

View File

@ -341,6 +341,12 @@ pub trait Field:
/// Returns `n % Self::characteristic()`.
fn from_noncanonical_u128(n: u128) -> Self;
/// Returns `x % Self::CHARACTERISTIC`.
fn from_noncanonical_u64(n: u64) -> Self;
/// Returns `n` as an element of this field.
fn from_noncanonical_i64(n: i64) -> Self;
/// Returns `n % Self::characteristic()`. May be cheaper than from_noncanonical_u128 when we know
/// that `n < 2 ** 96`.
#[inline]
@ -501,14 +507,6 @@ pub trait PrimeField: Field {
pub trait Field64: Field {
const ORDER: u64;
/// Returns `x % Self::CHARACTERISTIC`.
// TODO: Move to `Field`.
fn from_noncanonical_u64(n: u64) -> Self;
/// Returns `n` as an element of this field.
// TODO: Move to `Field`.
fn from_noncanonical_i64(n: i64) -> Self;
/// Returns `n` as an element of this field. Assumes that `0 <= n < Self::ORDER`.
// TODO: Move to `Field`.
// TODO: Should probably be unsafe.