mirror of
https://github.com/pseXperiments/ff-Goldilocks.git
synced 2026-01-09 15:38:06 -05:00
impl from uniform bytes for Goldilocks
This commit is contained in:
15
src/fp.rs
15
src/fp.rs
@@ -2,7 +2,7 @@ use crate::util::{add_no_canonicalize_trashing_input, branch_hint, split, sqrt_t
|
||||
use crate::util::{assume, try_inverse_u64};
|
||||
use core::iter::{Product, Sum};
|
||||
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
||||
use ff::{Field, PrimeField};
|
||||
use ff::{Field, FromUniformBytes, PrimeField};
|
||||
use rand_core::RngCore;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
@@ -32,6 +32,19 @@ pub const MODULUS: u64 = 0xffffffff00000001;
|
||||
/// 2^32 - 1
|
||||
pub const EPSILON: u64 = 0xffffffff;
|
||||
|
||||
impl FromUniformBytes<64> for Goldilocks {
|
||||
fn from_uniform_bytes(bytes: &[u8; 64]) -> Self {
|
||||
<Self as FromUniformBytes<32>>::from_uniform_bytes(bytes[0..32].try_into().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromUniformBytes<32> for Goldilocks {
|
||||
fn from_uniform_bytes(bytes: &[u8; 32]) -> Self {
|
||||
// FIXME: this is biased.
|
||||
Goldilocks(u64::from_le_bytes(bytes[..8].try_into().unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Field for Goldilocks {
|
||||
/// The zero element of the field, the additive identity.
|
||||
const ZERO: Self = Self(0);
|
||||
|
||||
@@ -8,17 +8,16 @@ use rand_core::RngCore;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
|
||||
|
||||
/// Degree 3 Goldilocks extension field mod x^2 - 7
|
||||
/// Degree 3 Goldilocks extension field mod x^2 - 7
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct GoldilocksExt2(pub [Goldilocks; 2]);
|
||||
|
||||
/// For a = (a1, a2) and b = (b1, b2)
|
||||
/// The multiplication is define as
|
||||
/// c := a * b = a(x) * b(x) % (x^2 - 7)
|
||||
/// = x*a2*b1 + x*a1*b2
|
||||
/// = x*a2*b1 + x*a1*b2
|
||||
/// + a1*b1 + 7*a2*b2
|
||||
|
||||
|
||||
/// This requires 9 multiplications and 6 1 additions
|
||||
fn mul_internal(a: &GoldilocksExt2, b: &GoldilocksExt2) -> GoldilocksExt2 {
|
||||
// todo: optimizations?
|
||||
|
||||
Reference in New Issue
Block a user