mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-04 06:43:07 +00:00
Move operations to Field
Implement the TODOs and move `from_noncanonical_u64` and `from_noncanonical_i64` from `Field64` to `Field`.
This commit is contained in:
parent
3de92d9ed1
commit
6122dccb6e
@ -118,22 +118,6 @@ impl Field for GoldilocksField {
|
|||||||
reduce128(n)
|
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]
|
#[inline]
|
||||||
fn from_noncanonical_u64(n: u64) -> Self {
|
fn from_noncanonical_u64(n: u64) -> Self {
|
||||||
Self(n)
|
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]
|
#[inline]
|
||||||
unsafe fn add_canonical_u64(&self, rhs: u64) -> Self {
|
unsafe fn add_canonical_u64(&self, rhs: u64) -> Self {
|
||||||
let (res_wrapped, carry) = self.0.overflowing_add(rhs);
|
let (res_wrapped, carry) = self.0.overflowing_add(rhs);
|
||||||
|
|||||||
@ -341,6 +341,26 @@ pub trait Field:
|
|||||||
/// Returns `n % Self::characteristic()`.
|
/// Returns `n % Self::characteristic()`.
|
||||||
fn from_noncanonical_u128(n: u128) -> Self;
|
fn from_noncanonical_u128(n: u128) -> Self;
|
||||||
|
|
||||||
|
/// Returns `x % Self::CHARACTERISTIC`.
|
||||||
|
///
|
||||||
|
/// Implemented by default via `from_noncanonical_u128`
|
||||||
|
/// Override, if you have a faster implementation for your field.
|
||||||
|
fn from_noncanonical_u64(n: u64) -> Self {
|
||||||
|
Self::from_noncanonical_u128(n as u128)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `n` as an element of this field.
|
||||||
|
///
|
||||||
|
/// Implemented by default via `from_noncanonical_u128` and case analysis.
|
||||||
|
/// Override, if you have a faster implementation for your field.
|
||||||
|
fn from_noncanonical_i64(n: i64) -> Self {
|
||||||
|
if n < 0 {
|
||||||
|
-Self::from_noncanonical_u128((n as i128).unsigned_abs())
|
||||||
|
} else {
|
||||||
|
Self::from_noncanonical_u128(n as u128)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `n % Self::characteristic()`. May be cheaper than from_noncanonical_u128 when we know
|
/// Returns `n % Self::characteristic()`. May be cheaper than from_noncanonical_u128 when we know
|
||||||
/// that `n < 2 ** 96`.
|
/// that `n < 2 ** 96`.
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -501,14 +521,6 @@ pub trait PrimeField: Field {
|
|||||||
pub trait Field64: Field {
|
pub trait Field64: Field {
|
||||||
const ORDER: u64;
|
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`.
|
/// Returns `n` as an element of this field. Assumes that `0 <= n < Self::ORDER`.
|
||||||
// TODO: Move to `Field`.
|
// TODO: Move to `Field`.
|
||||||
// TODO: Should probably be unsafe.
|
// TODO: Should probably be unsafe.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user