impl from uniform bytes for Goldilocks

This commit is contained in:
zhenfei
2023-11-30 17:54:08 -05:00
parent 6b8bbc07f6
commit 61108432c6
2 changed files with 16 additions and 4 deletions

View File

@@ -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);

View File

@@ -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?