Skip to content

Commit

Permalink
Use optimized field for webnode
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiencs committed Nov 27, 2024
1 parent d0cd63b commit f1ac246
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 81 deletions.
59 changes: 27 additions & 32 deletions curves/src/pasta/fields/fp.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,60 @@
use ark_ff::{biginteger::BigInteger256 as BigInteger, FftParameters, Fp256, Fp256Parameters};
use ark_ff::{biginteger::BigInteger256 as BigInteger, FftParameters, Fp256Parameters, NewFp256};

pub type Fp = Fp256<FpParameters>;
pub type Fp = NewFp256<FpParameters>;

#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct FpParameters;

impl Fp256Parameters for FpParameters {}

#[rustfmt::skip]
impl FftParameters for FpParameters {
type BigInt = BigInteger;

const TWO_ADICITY: u32 = 32;

#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInteger([
0xa28db849bad6dbf0, 0x9083cd03d3b539df, 0xfba6b9ca9dc8448e, 0x3ec928747b89c6da
]);
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = {
const TWO_ADIC_ROOT_OF_UNITY: Fp = ark_ff::field_new!(Fp, "19814229590243028906643993866117402072516588566294623396325693409366934201135");
TWO_ADIC_ROOT_OF_UNITY.0
};
}

#[rustfmt::skip]
impl ark_ff::FpParameters for FpParameters {
// 28948022309329048855892746252171976963363056481941560715954676764349967630337
const MODULUS: BigInteger = BigInteger([
0x992d30ed00000001,
0x224698fc094cf91b,
0x0,
0x4000000000000000,
0x1, 0x9698768, 0x133e46e6, 0xd31f812, 0x224, 0x0, 0x0, 0x0, 0x400000,
]);

const R: BigInteger = BigInteger([
0x34786d38fffffffd,
0x992c350be41914ad,
0xffffffffffffffff,
0x3fffffffffffffff,
0x1fffff81, 0x14a5d367, 0x141ad3c0, 0x1435eec5, 0x1ffeefef, 0x1fffffff, 0x1fffffff,
0x1fffffff, 0x3fffff,
]);

const R2: BigInteger = BigInteger([
0x8c78ecb30000000f,
0xd7d30dbd8b0de0e7,
0x7797a99bc3c95d18,
0x96d41af7b9cb714,
0x3b6a, 0x19c10910, 0x1a6a0188, 0x12a4fd88, 0x634b36d, 0x178792ba, 0x7797a99, 0x1dce5b8a,
0x3506bd,
]);

// TODO
const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0xcc96987680000000,
0x11234c7e04a67c8d,
0x0,
0x2000000000000000,
0x0, 0x4b4c3b4, 0x99f2373, 0x698fc09, 0x112, 0x0, 0x0, 0x0, 0x200000,
]);

// T and T_MINUS_ONE_DIV_TWO, where MODULUS - 1 = 2^S * T
const T: BigInteger = BigInteger([0x94cf91b992d30ed, 0x224698fc, 0x0, 0x40000000]);
const T: BigInteger = BigInteger([
0x192d30ed, 0xa67c8dc, 0x11a63f02, 0x44, 0x0, 0x0, 0x0, 0x80000, 0x0,
]);

const T_MINUS_ONE_DIV_TWO: BigInteger =
BigInteger([0x4a67c8dcc969876, 0x11234c7e, 0x0, 0x20000000]);
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0xc969876, 0x533e46e, 0x8d31f81, 0x22, 0x0, 0x0, 0x0, 0x40000, 0x0,
]);

// GENERATOR = 5
const GENERATOR: BigInteger = BigInteger([
0xa1a55e68ffffffed,
0x74c2a54b4f4982f3,
0xfffffffffffffffd,
0x3fffffffffffffff,
]);
const GENERATOR: BigInteger = {
const FIVE: Fp = ark_ff::field_new!(Fp, "5");
FIVE.0
};

const MODULUS_BITS: u32 = 255;

Expand All @@ -68,5 +63,5 @@ impl ark_ff::FpParameters for FpParameters {
const REPR_SHAVE_BITS: u32 = 1;

// -(MODULUS^{-1} mod 2^64) mod 2^64
const INV: u64 = 11037532056220336127;
const INV: u64 = 0x1fffffff;
}
70 changes: 32 additions & 38 deletions curves/src/pasta/fields/fq.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,60 @@
use ark_ff::{
biginteger::BigInteger256 as BigInteger, FftParameters, Fp256, Fp256Parameters, FpParameters,
};
use ark_ff::{biginteger::BigInteger256 as BigInteger, FftParameters, Fp256Parameters, NewFp256};

pub type Fq = NewFp256<FqParameters>;

pub struct FqParameters;

pub type Fq = Fp256<FqParameters>;
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct FqParameters;

impl Fp256Parameters for FqParameters {}

#[rustfmt::skip]
impl FftParameters for FqParameters {
type BigInt = BigInteger;

const TWO_ADICITY: u32 = 32;

#[rustfmt::skip]
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = BigInteger([
0x218077428c9942de, 0xcc49578921b60494, 0xac2e5d27b2efbee2, 0xb79fa897f2db056
]);
const TWO_ADIC_ROOT_OF_UNITY: BigInteger = {
const TWO_ADIC_ROOT_OF_UNITY: Fq = ark_ff::field_new!(Fq, "20761624379169977859705911634190121761503565370703356079647768903521299517535");
TWO_ADIC_ROOT_OF_UNITY.0
};
}
impl FpParameters for FqParameters {
// 28948022309329048855892746252171976963363056481941647379679742748393362948097

#[rustfmt::skip]
impl ark_ff::FpParameters for FqParameters {
// 28948022309329048855892746252171976963363056481941560715954676764349967630337
const MODULUS: BigInteger = BigInteger([
0x8c46eb2100000001,
0x224698fc0994a8dd,
0x0,
0x4000000000000000,
0x1, 0x2375908, 0x52a3763, 0xd31f813, 0x224, 0x0, 0x0, 0x0, 0x400000,
]);

const R: BigInteger = BigInteger([
0x5b2b3e9cfffffffd,
0x992c350be3420567,
0xffffffffffffffff,
0x3fffffffffffffff,
0x1fffff81, 0x68ad507, 0x100e85da, 0x1435ee7e, 0x1ffeefef, 0x1fffffff, 0x1fffffff,
0x1fffffff, 0x3fffff,
]);

const R2: BigInteger = BigInteger([
0xfc9678ff0000000f,
0x67bb433d891a16e3,
0x7fae231004ccf590,
0x96d41af7ccfdaa9,
0x3b6a, 0x2b1b550, 0x1027888a, 0x1ea4ed96, 0x418ad7a, 0x999eb, 0x17fae231,
0x1e67ed54, 0x3506bd,
]);

const MODULUS_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0xc623759080000000,
0x11234c7e04ca546e,
0x0,
0x2000000000000000,
0x0, 0x111bac84, 0x12951bb1, 0x698fc09, 0x112, 0x0, 0x0, 0x0, 0x200000,
]);

// T and T_MINUS_ONE_DIV_TWO, where MODULUS - 1 = 2^S * T
const T: BigInteger = BigInteger([
0xc46eb21, 0xca546ec, 0x11a63f02, 0x44, 0x0, 0x0, 0x0, 0x80000, 0x0,
]);

const T: BigInteger = BigInteger([0x994a8dd8c46eb21, 0x224698fc, 0x0, 0x40000000]);

const T_MINUS_ONE_DIV_TWO: BigInteger =
BigInteger([0x4ca546ec6237590, 0x11234c7e, 0x0, 0x20000000]);
const T_MINUS_ONE_DIV_TWO: BigInteger = BigInteger([
0x6237590, 0x652a376, 0x8d31f81, 0x22, 0x0, 0x0, 0x0, 0x40000, 0x0,
]);

// GENERATOR = 5
const GENERATOR: BigInteger = BigInteger([
0x96bc8c8cffffffed,
0x74c2a54b49f7778e,
0xfffffffffffffffd,
0x3fffffffffffffff,
]);
const GENERATOR: BigInteger = {
const FIVE: Fq = ark_ff::field_new!(Fq, "5");
FIVE.0
};

const MODULUS_BITS: u32 = 255;

Expand All @@ -69,5 +63,5 @@ impl FpParameters for FqParameters {
const REPR_SHAVE_BITS: u32 = 1;

// -(MODULUS^{-1} mod 2^64) mod 2^64
const INV: u64 = 10108024940646105087;
const INV: u64 = 0x1fffffff;
}
4 changes: 2 additions & 2 deletions kimchi/src/circuits/polynomials/endomul_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
curve::KimchiCurve,
};
use ark_ff::{BitIteratorLE, Field, PrimeField};
use ark_ff::{BitIteratorLE, BigInteger, Field, PrimeField};
use std::array;
use std::marker::PhantomData;

Expand Down Expand Up @@ -228,7 +228,7 @@ pub fn gen_witness<F: PrimeField + std::fmt::Display>(
let bits_per_row = 2 * crumbs_per_row;
assert_eq!(num_bits % bits_per_row, 0);

let bits_lsb: Vec<_> = BitIteratorLE::new(scalar.into_repr())
let bits_lsb: Vec<_> = BitIteratorLE::new(scalar.into_repr().to_64x4())
.take(num_bits)
.collect();
let bits_msb: Vec<_> = bits_lsb.iter().rev().collect();
Expand Down
17 changes: 10 additions & 7 deletions poly-commitment/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use ark_ec::{
models::short_weierstrass_jacobian::GroupAffine as SWJAffine, AffineCurve, ProjectiveCurve,
SWModelParameters,
};
use ark_ff::{BitIteratorBE, Field, One, PrimeField, Zero};
use ark_ff::{BitIteratorBE, BigInteger, Field, One, PrimeField, Zero};
use itertools::Itertools;
use mina_poseidon::sponge::ScalarChallenge;
use rayon::prelude::*;
Expand Down Expand Up @@ -190,8 +190,8 @@ fn affine_window_combine_base<P: SWModelParameters>(
};
assert!(g1g2.len() == g1.len());

let windows1 = BitIteratorBE::new(x1.into_repr()).tuples();
let windows2 = BitIteratorBE::new(x2.into_repr()).tuples();
let windows1 = BitIteratorBE::new(x1.into_repr().to_64x4()).tuples();
let windows2 = BitIteratorBE::new(x2.into_repr().to_64x4()).tuples();

let mut points = vec![SWJAffine::<P>::zero(); g1.len()];

Expand Down Expand Up @@ -304,8 +304,11 @@ fn affine_window_combine_one_endo_base<P: SWModelParameters>(
(limbs_lsb[limb as usize] >> j) & 1
}

use ark_ff::BigInteger;

let rep = chal.0.into_repr();
let r = rep.as_ref();
let r = rep.to_64x4();
let r = r.as_ref();

let mut denominators = vec![P::BaseField::zero(); g1.len()];
// acc = 2 (phi(g2) + g2)
Expand Down Expand Up @@ -371,7 +374,7 @@ fn affine_window_combine_one_base<P: SWModelParameters>(
g2: &[SWJAffine<P>],
x2: P::ScalarField,
) -> Vec<SWJAffine<P>> {
let windows2 = BitIteratorBE::new(x2.into_repr()).tuples();
let windows2 = BitIteratorBE::new(x2.into_repr().to_64x4()).tuples();

let mut points = vec![SWJAffine::<P>::zero(); g1.len()];

Expand Down Expand Up @@ -594,8 +597,8 @@ fn window_shamir<G: AffineCurve>(
let [_g00_00, g01_00, g10_00, g11_00, g00_01, g01_01, g10_01, g11_01, g00_10, g01_10, g10_10, g11_10, g00_11, g01_11, g10_11, g11_11] =
shamir_window_table(g1, g2);

let windows1 = BitIteratorBE::new(x1.into_repr()).tuples();
let windows2 = BitIteratorBE::new(x2.into_repr()).tuples();
let windows1 = BitIteratorBE::new(x1.into_repr().to_64x4()).tuples();
let windows2 = BitIteratorBE::new(x2.into_repr().to_64x4()).tuples();

let mut res = G::Projective::zero();

Expand Down
7 changes: 5 additions & 2 deletions poseidon/src/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub fn endo_coefficient<F: PrimeField>() -> F {

let t = F::multiplicative_generator();

t.pow(p_minus_1_over_3.into_repr().as_ref())
let p_minus_1_over_3 = p_minus_1_over_3.into_repr().to_64x4();
t.pow(&p_minus_1_over_3)
}

fn get_bit(limbs_lsb: &[u64], i: u64) -> u64 {
Expand All @@ -30,7 +31,7 @@ fn get_bit(limbs_lsb: &[u64], i: u64) -> u64 {

impl<F: PrimeField> ScalarChallenge<F> {
pub fn to_field_with_length(&self, length_in_bits: usize, endo_coeff: &F) -> F {
let rep = self.0.into_repr();
let rep = self.0.into_repr().to_64x4();
let r = rep.as_ref();

let mut a: F = 2_u64.into();
Expand Down Expand Up @@ -92,6 +93,7 @@ impl<Fr: PrimeField, SC: SpongeConstants> DefaultFrSponge<Fr, SC> {
.expect("internal representation was not a valid field element")
} else {
let x = self.sponge.squeeze().into_repr();
let x = x.to_64x4();
self.last_squeezed
.extend(&x.as_ref()[0..HIGH_ENTROPY_LIMBS]);
self.squeeze(num_limbs)
Expand All @@ -112,6 +114,7 @@ where
limbs.to_vec()
} else {
let x = self.sponge.squeeze().into_repr();
let x = x.to_64x4();
self.last_squeezed
.extend(&x.as_ref()[0..HIGH_ENTROPY_LIMBS]);
self.squeeze_limbs(num_limbs)
Expand Down

0 comments on commit f1ac246

Please sign in to comment.