Skip to content

Commit

Permalink
mpcs to plonky3 field
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jan 10, 2025
1 parent 1126d58 commit e189724
Show file tree
Hide file tree
Showing 29 changed files with 653 additions and 543 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions ff_ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ use std::{array::from_fn, iter::repeat_with};

pub use ff;
use p3_field::{
ExtensionField as P3ExtensionField, Field as P3Field, PackedValue, PrimeField,
ExtensionField as P3ExtensionField, Field as P3Field, PackedValue, PrimeField, TwoAdicField,
extension::BinomialExtensionField,
};
use p3_goldilocks::Goldilocks;
use rand_core::RngCore;
use serde::Serialize;

pub type GoldilocksExt2 = BinomialExtensionField<Goldilocks, 2>;

fn array_try_from_uniform_bytes<
Expand Down Expand Up @@ -92,7 +91,7 @@ pub trait SmallField: Serialize + P3Field {
pub trait ExtensionField: P3ExtensionField<Self::BaseField> + FromUniformBytes {
const DEGREE: usize;

type BaseField: SmallField + Ord + PrimeField + FromUniformBytes;
type BaseField: SmallField + Ord + PrimeField + FromUniformBytes + TwoAdicField;

fn from_bases(bases: &[Self::BaseField]) -> Self;

Expand Down Expand Up @@ -176,3 +175,13 @@ mod impl_goldilocks {
}
}
}

#[cfg(test)]
mod test {
use p3_field::TwoAdicField;
use p3_goldilocks::Goldilocks;
#[test]
fn test() {
println!("{:?}", Goldilocks::two_adic_generator(21));
}
}
4 changes: 4 additions & 0 deletions mpcs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ num-bigint = "0.4"
num-integer = "0.1"
plonky2.workspace = true
poseidon.workspace = true
p3-field.workspace = true
p3-goldilocks.workspace = true
p3-mds.workspace = true
p3-symmetric.workspace = true
rand.workspace = true
rand_chacha.workspace = true
rayon = { workspace = true, optional = true }
Expand Down
7 changes: 4 additions & 3 deletions mpcs/benches/basecode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::time::Duration;

use criterion::*;
use ff::Field;
use goldilocks::GoldilocksExt2;

use ff_ext::GoldilocksExt2;
use itertools::Itertools;
use mpcs::{
Basefold, BasefoldBasecodeParams, BasefoldSpec, EncodingScheme, PolynomialCommitmentScheme,
Expand All @@ -13,12 +12,14 @@ use mpcs::{
},
};

use ff_ext::FromUniformBytes;
use multilinear_extensions::mle::{DenseMultilinearExtension, FieldType};
use p3_goldilocks::MdsMatrixGoldilocks;
use rand::{SeedableRng, rngs::OsRng};
use rand_chacha::ChaCha8Rng;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

type Pcs = Basefold<GoldilocksExt2, BasefoldBasecodeParams>;
type Pcs = Basefold<GoldilocksExt2, BasefoldBasecodeParams, MdsMatrixGoldilocks>;
type E = GoldilocksExt2;

const NUM_SAMPLES: usize = 10;
Expand Down
36 changes: 18 additions & 18 deletions mpcs/benches/basefold.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::time::Duration;

use criterion::*;
use ff_ext::ExtensionField;
use goldilocks::GoldilocksExt2;
use ff_ext::{ExtensionField, GoldilocksExt2};

use itertools::{Itertools, chain};
use mpcs::{
Expand All @@ -18,11 +17,12 @@ use multilinear_extensions::{
mle::{DenseMultilinearExtension, MultilinearExtension},
virtual_poly::ArcMultilinearExtension,
};
use p3_goldilocks::MdsMatrixGoldilocks;
use transcript::{BasicTranscript, Transcript};

type PcsGoldilocksRSCode = Basefold<GoldilocksExt2, BasefoldRSParams>;
type PcsGoldilocksBasecode = Basefold<GoldilocksExt2, BasefoldBasecodeParams>;
type T = BasicTranscript<GoldilocksExt2>;
type PcsGoldilocksRSCode = Basefold<GoldilocksExt2, BasefoldRSParams, MdsMatrixGoldilocks>;
type PcsGoldilocksBasecode = Basefold<GoldilocksExt2, BasefoldBasecodeParams, MdsMatrixGoldilocks>;
type T = BasicTranscript<GoldilocksExt2, MdsMatrixGoldilocks>;
type E = GoldilocksExt2;

const NUM_SAMPLES: usize = 10;
Expand Down Expand Up @@ -73,12 +73,12 @@ fn bench_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentScheme<E>>(
let point = get_point_from_challenge(num_vars, &mut transcript);
let eval = poly.evaluate(point.as_slice());
transcript.append_field_element_ext(&eval);
let transcript_for_bench = transcript;
let transcript_for_bench = transcript.clone();
let proof = Pcs::open(&pp, &poly, &comm, &point, &eval, &mut transcript).unwrap();

group.bench_function(BenchmarkId::new("open", format!("{}", num_vars)), |b| {
b.iter_batched(
|| transcript_for_bench,
|| transcript_for_bench.clone(),
|mut transcript| {
Pcs::open(&pp, &poly, &comm, &point, &eval, &mut transcript).unwrap();
},
Expand All @@ -91,11 +91,11 @@ fn bench_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentScheme<E>>(
Pcs::write_commitment(&comm, &mut transcript).unwrap();
let point = get_point_from_challenge(num_vars, &mut transcript);
transcript.append_field_element_ext(&eval);
let transcript_for_bench = transcript;
let transcript_for_bench = transcript.clone();
Pcs::verify(&vp, &comm, &point, &eval, &proof, &mut transcript).unwrap();
group.bench_function(BenchmarkId::new("verify", format!("{}", num_vars)), |b| {
b.iter_batched(
|| transcript_for_bench,
|| transcript_for_bench.clone(),
|mut transcript| {
Pcs::verify(&vp, &comm, &point, &eval, &proof, &mut transcript).unwrap();
},
Expand Down Expand Up @@ -163,15 +163,15 @@ fn bench_batch_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentScheme<E>>
.collect_vec();
let values: Vec<E> = evals.iter().map(Evaluation::value).copied().collect();
transcript.append_field_element_exts(values.as_slice());
let transcript_for_bench = transcript;
let transcript_for_bench = transcript.clone();
let proof =
Pcs::batch_open(&pp, &polys, &comms, &points, &evals, &mut transcript).unwrap();

group.bench_function(
BenchmarkId::new("batch_open", format!("{}-{}", num_vars, batch_size)),
|b| {
b.iter_batched(
|| transcript_for_bench,
|| transcript_for_bench.clone(),
|mut transcript| {
Pcs::batch_open(&pp, &polys, &comms, &points, &evals, &mut transcript)
.unwrap();
Expand Down Expand Up @@ -203,15 +203,15 @@ fn bench_batch_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentScheme<E>>
.collect::<Vec<E>>();
transcript.append_field_element_exts(values.as_slice());

let backup_transcript = transcript;
let backup_transcript = transcript.clone();

Pcs::batch_verify(&vp, &comms, &points, &evals, &proof, &mut transcript).unwrap();

group.bench_function(
BenchmarkId::new("batch_verify", format!("{}-{}", num_vars, batch_size)),
|b| {
b.iter_batched(
|| backup_transcript,
|| backup_transcript.clone(),
|mut transcript| {
Pcs::batch_verify(
&vp,
Expand Down Expand Up @@ -261,7 +261,7 @@ fn bench_simple_batch_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentSch
let point = get_point_from_challenge(num_vars, &mut transcript);
let evals = polys.iter().map(|poly| poly.evaluate(&point)).collect_vec();
transcript.append_field_element_exts(&evals);
let transcript_for_bench = transcript;
let transcript_for_bench = transcript.clone();
let polys = polys
.iter()
.map(|poly| ArcMultilinearExtension::from(poly.clone()))
Expand All @@ -273,7 +273,7 @@ fn bench_simple_batch_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentSch
BenchmarkId::new("batch_open", format!("{}-{}", num_vars, batch_size)),
|b| {
b.iter_batched(
|| transcript_for_bench,
|| transcript_for_bench.clone(),
|mut transcript| {
Pcs::simple_batch_open(
&pp,
Expand All @@ -292,20 +292,20 @@ fn bench_simple_batch_commit_open_verify_goldilocks<Pcs: PolynomialCommitmentSch
let comm = Pcs::get_pure_commitment(&comm);

// Batch verify
let mut transcript = BasicTranscript::new(b"BaseFold");
let mut transcript = BasicTranscript::<E, MdsMatrixGoldilocks>::new(b"BaseFold");
Pcs::write_commitment(&comm, &mut transcript).unwrap();

let point = get_point_from_challenge(num_vars, &mut transcript);
transcript.append_field_element_exts(&evals);
let backup_transcript = transcript;
let backup_transcript = transcript.clone();

Pcs::simple_batch_verify(&vp, &comm, &point, &evals, &proof, &mut transcript).unwrap();

group.bench_function(
BenchmarkId::new("batch_verify", format!("{}-{}", num_vars, batch_size)),
|b| {
b.iter_batched(
|| backup_transcript,
|| backup_transcript.clone(),
|mut transcript| {
Pcs::simple_batch_verify(
&vp,
Expand Down
7 changes: 4 additions & 3 deletions mpcs/benches/fft.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::time::Duration;

use criterion::*;
use ff::{Field, PrimeField};
use goldilocks::{Goldilocks, GoldilocksExt2};
use ff_ext::{FromUniformBytes, GoldilocksExt2};

use itertools::Itertools;
use mpcs::{coset_fft, fft_root_table};

use multilinear_extensions::mle::DenseMultilinearExtension;
use p3_field::Field;
use p3_goldilocks::Goldilocks;
use rand::{SeedableRng, rngs::OsRng};
use rand_chacha::ChaCha8Rng;
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
Expand Down Expand Up @@ -51,7 +52,7 @@ fn bench_fft(c: &mut Criterion, is_base: bool) {
polys.par_iter_mut().for_each(|poly| {
coset_fft::<GoldilocksExt2>(
&mut poly.evaluations,
Goldilocks::MULTIPLICATIVE_GENERATOR,
Goldilocks::GENERATOR,
0,
&root_table,
);
Expand Down
9 changes: 5 additions & 4 deletions mpcs/benches/hashing.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ark_std::test_rng;
use criterion::{Criterion, criterion_group, criterion_main};
use ff::Field;
use goldilocks::Goldilocks;

use ff_ext::FromUniformBytes;
use mpcs::util::hash::{Digest, hash_two_digests};
use p3_goldilocks::{Goldilocks, MdsMatrixGoldilocks};
use poseidon::poseidon_hash::PoseidonHash;

fn random_ceno_goldy() -> Goldilocks {
Expand All @@ -12,12 +13,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let left = Digest(vec![random_ceno_goldy(); 4].try_into().unwrap());
let right = Digest(vec![random_ceno_goldy(); 4].try_into().unwrap());
c.bench_function("ceno hash 2 to 1", |bencher| {
bencher.iter(|| hash_two_digests(&left, &right))
bencher.iter(|| hash_two_digests::<Goldilocks, MdsMatrixGoldilocks>(&left, &right))
});

let values = (0..60).map(|_| random_ceno_goldy()).collect::<Vec<_>>();
c.bench_function("ceno hash 60 to 1", |bencher| {
bencher.iter(|| PoseidonHash::hash_or_noop(&values))
bencher.iter(|| PoseidonHash::<Goldilocks, MdsMatrixGoldilocks>::hash_or_noop(&values))
});
}

Expand Down
3 changes: 1 addition & 2 deletions mpcs/benches/interpolate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::time::Duration;

use criterion::*;
use ff::Field;
use goldilocks::GoldilocksExt2;

use ff_ext::{FromUniformBytes, GoldilocksExt2};
use itertools::Itertools;
use mpcs::util::arithmetic::interpolate_field_type_over_boolean_hypercube;

Expand Down
6 changes: 3 additions & 3 deletions mpcs/benches/rscode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::time::Duration;

use criterion::*;
use ff::Field;
use goldilocks::GoldilocksExt2;

use ff_ext::{FromUniformBytes, GoldilocksExt2};
use itertools::Itertools;
use mpcs::{
Basefold, BasefoldRSParams, BasefoldSpec, EncodingScheme, PolynomialCommitmentScheme,
Expand All @@ -14,11 +13,12 @@ use mpcs::{
};

use multilinear_extensions::mle::{DenseMultilinearExtension, FieldType};
use p3_goldilocks::MdsMatrixGoldilocks;
use rand::{SeedableRng, rngs::OsRng};
use rand_chacha::ChaCha8Rng;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams>;
type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams, MdsMatrixGoldilocks>;
type E = GoldilocksExt2;

const NUM_SAMPLES: usize = 10;
Expand Down
3 changes: 1 addition & 2 deletions mpcs/benches/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::time::Duration;

use criterion::*;
use ff::Field;
use goldilocks::GoldilocksExt2;

use ff_ext::{FromUniformBytes, GoldilocksExt2};
use mpcs::{one_level_eval_hc, one_level_interp_hc};

use rand::rngs::OsRng;
Expand Down
Loading

0 comments on commit e189724

Please sign in to comment.