Skip to content

Commit

Permalink
Remove Rng from PCS type paramter. (#549)
Browse files Browse the repository at this point in the history
Extracting a small PR from #294.

The random generator is used only when the encoding scheme is Basecode,
and not used for RS code. So remove it from the type parameter for
Basefold, i.e., `Basefold<E, Spec, Rng>` is replaced with `Basefold<E,
Spec>` to make the API simpler. Fix it to `ChaCha8Rng` in the
implementation of Basecode.
  • Loading branch information
yczhangsjtu authored Nov 5, 2024
1 parent f36a5b1 commit 2d7baf7
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 30 deletions.
3 changes: 1 addition & 2 deletions ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use ff_ext::ff::Field;
use goldilocks::GoldilocksExt2;
use itertools::Itertools;
use mpcs::{Basefold, BasefoldRSParams, PolynomialCommitmentScheme};
use rand_chacha::ChaCha8Rng;
use tracing_flame::FlameLayer;
use tracing_subscriber::{EnvFilter, Registry, fmt, layer::SubscriberExt};
use transcript::Transcript;
Expand Down Expand Up @@ -76,7 +75,7 @@ struct Args {
fn main() {
let args = Args::parse();
type E = GoldilocksExt2;
type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams, ChaCha8Rng>;
type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams>;

let program = Program::new(
CENO_PLATFORM.pc_base(),
Expand Down
3 changes: 1 addition & 2 deletions ceno_zkvm/src/scheme/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use ff_ext::ExtensionField;
use goldilocks::GoldilocksExt2;
use itertools::Itertools;
use mpcs::{Basefold, BasefoldDefault, BasefoldRSParams, PolynomialCommitmentScheme};
use rand_chacha::ChaCha8Rng;
use transcript::Transcript;

use crate::{
Expand Down Expand Up @@ -197,7 +196,7 @@ const PROGRAM_CODE: [u32; PROGRAM_SIZE] = {
#[test]
fn test_single_add_instance_e2e() {
type E = GoldilocksExt2;
type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams, ChaCha8Rng>;
type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams>;

// set up program
let program = Program::new(
Expand Down
2 changes: 1 addition & 1 deletion mpcs/benches/basecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rand::{SeedableRng, rngs::OsRng};
use rand_chacha::ChaCha8Rng;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

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

const NUM_SAMPLES: usize = 10;
Expand Down
2 changes: 1 addition & 1 deletion mpcs/benches/commit_open_verify_basecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rand::{SeedableRng, rngs::OsRng};
use rand_chacha::ChaCha8Rng;
use transcript::Transcript;

type Pcs = Basefold<GoldilocksExt2, BasefoldBasecodeParams, ChaCha8Rng>;
type Pcs = Basefold<GoldilocksExt2, BasefoldBasecodeParams>;
type T = Transcript<GoldilocksExt2>;
type E = GoldilocksExt2;

Expand Down
2 changes: 1 addition & 1 deletion mpcs/benches/commit_open_verify_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rand_chacha::ChaCha8Rng;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use transcript::Transcript;

type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams, ChaCha8Rng>;
type Pcs = Basefold<GoldilocksExt2, BasefoldRSParams>;
type T = Transcript<GoldilocksExt2>;
type E = GoldilocksExt2;

Expand Down
2 changes: 1 addition & 1 deletion mpcs/benches/rscode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rand::{SeedableRng, rngs::OsRng};
use rand_chacha::ChaCha8Rng;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

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

const NUM_SAMPLES: usize = 10;
Expand Down
15 changes: 5 additions & 10 deletions mpcs/src/basefold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ use multilinear_extensions::{
virtual_poly::build_eq_x_r_vec,
};

use rand_chacha::{ChaCha8Rng, rand_core::RngCore};
use rayon::{
iter::IntoParallelIterator,
prelude::{IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator},
Expand Down Expand Up @@ -80,7 +79,7 @@ enum PolyEvalsCodeword<E: ExtensionField> {
TooBig(usize),
}

impl<E: ExtensionField, Spec: BasefoldSpec<E>, Rng: RngCore> Basefold<E, Spec, Rng>
impl<E: ExtensionField, Spec: BasefoldSpec<E>> Basefold<E, Spec>
where
E: Serialize + DeserializeOwned,
E::BaseField: Serialize + DeserializeOwned,
Expand Down Expand Up @@ -266,8 +265,7 @@ where
/// positions are (i >> k) and (i >> k) XOR 1.
/// (c) The verifier checks that the folding has been correctly computed
/// at these positions.
impl<E: ExtensionField, Spec: BasefoldSpec<E>, Rng: RngCore + std::fmt::Debug>
PolynomialCommitmentScheme<E> for Basefold<E, Spec, Rng>
impl<E: ExtensionField, Spec: BasefoldSpec<E>> PolynomialCommitmentScheme<E> for Basefold<E, Spec>
where
E: Serialize + DeserializeOwned,
E::BaseField: Serialize + DeserializeOwned,
Expand All @@ -279,7 +277,6 @@ where
type Commitment = BasefoldCommitment<E>;
type CommitmentChunk = Digest<E::BaseField>;
type Proof = BasefoldProof<E>;
type Rng = ChaCha8Rng;

fn setup(poly_size: usize) -> Result<Self::Param, Error> {
let pp = <Spec::EncodingScheme as EncodingScheme<E>>::setup(log2_strict(poly_size));
Expand Down Expand Up @@ -1170,8 +1167,7 @@ where
}
}

impl<E: ExtensionField, Spec: BasefoldSpec<E>, Rng: RngCore + std::fmt::Debug> NoninteractivePCS<E>
for Basefold<E, Spec, Rng>
impl<E: ExtensionField, Spec: BasefoldSpec<E>> NoninteractivePCS<E> for Basefold<E, Spec>
where
E: Serialize + DeserializeOwned,
E::BaseField: Serialize + DeserializeOwned,
Expand All @@ -1188,12 +1184,11 @@ mod test {
},
};
use goldilocks::GoldilocksExt2;
use rand_chacha::ChaCha8Rng;

use super::{BasefoldRSParams, structure::BasefoldBasecodeParams};

type PcsGoldilocksRSCode = Basefold<GoldilocksExt2, BasefoldRSParams, ChaCha8Rng>;
type PcsGoldilocksBaseCode = Basefold<GoldilocksExt2, BasefoldBasecodeParams, ChaCha8Rng>;
type PcsGoldilocksRSCode = Basefold<GoldilocksExt2, BasefoldRSParams>;
type PcsGoldilocksBaseCode = Basefold<GoldilocksExt2, BasefoldBasecodeParams>;

#[test]
fn commit_open_verify_goldilocks_basecode_base() {
Expand Down
10 changes: 3 additions & 7 deletions mpcs/src/basefold/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ use crate::{
use core::fmt::Debug;
use ff_ext::ExtensionField;

use rand::RngCore;
use serde::{Deserialize, Serialize, de::DeserializeOwned};

use multilinear_extensions::mle::FieldType;

use rand_chacha::ChaCha8Rng;
use std::{marker::PhantomData, slice};

pub use super::encoding::{EncodingProverParameters, EncodingScheme, RSCode, RSCodeDefaultSpec};
Expand Down Expand Up @@ -248,13 +246,11 @@ where
}

#[derive(Debug)]
pub struct Basefold<E: ExtensionField, Spec: BasefoldSpec<E>, Rng: RngCore>(
PhantomData<(E, Spec, Rng)>,
);
pub struct Basefold<E: ExtensionField, Spec: BasefoldSpec<E>>(PhantomData<(E, Spec)>);

pub type BasefoldDefault<F> = Basefold<F, BasefoldRSParams, ChaCha8Rng>;
pub type BasefoldDefault<F> = Basefold<F, BasefoldRSParams>;

impl<E: ExtensionField, Spec: BasefoldSpec<E>, Rng: RngCore> Clone for Basefold<E, Spec, Rng> {
impl<E: ExtensionField, Spec: BasefoldSpec<E>> Clone for Basefold<E, Spec> {
fn clone(&self) -> Self {
Self(PhantomData)
}
Expand Down
8 changes: 3 additions & 5 deletions mpcs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ff_ext::ExtensionField;
use itertools::Itertools;
use multilinear_extensions::mle::DenseMultilinearExtension;
use rand::RngCore;
use serde::{Serialize, de::DeserializeOwned};
use std::fmt::Debug;
use transcript::Transcript;
Expand Down Expand Up @@ -116,7 +115,6 @@ pub trait PolynomialCommitmentScheme<E: ExtensionField>: Clone + Debug {
type Commitment: Clone + Debug + Default + Serialize + DeserializeOwned;
type CommitmentChunk: Clone + Debug + Default;
type Proof: Clone + Debug + Serialize + DeserializeOwned;
type Rng: RngCore + Clone;

fn setup(poly_size: usize) -> Result<Self::Param, Error>;

Expand Down Expand Up @@ -375,7 +373,7 @@ pub mod test_util {
num_vars_start: usize,
num_vars_end: usize,
) where
Pcs: PolynomialCommitmentScheme<E, Rng = ChaCha8Rng>,
Pcs: PolynomialCommitmentScheme<E>,
{
for num_vars in num_vars_start..num_vars_end {
// Setup
Expand Down Expand Up @@ -434,7 +432,7 @@ pub mod test_util {
num_vars_end: usize,
) where
E: ExtensionField,
Pcs: PolynomialCommitmentScheme<E, Rng = ChaCha8Rng>,
Pcs: PolynomialCommitmentScheme<E>,
{
for num_vars in num_vars_start..num_vars_end {
let batch_size = 2;
Expand Down Expand Up @@ -550,7 +548,7 @@ pub mod test_util {
batch_size: usize,
) where
E: ExtensionField,
Pcs: PolynomialCommitmentScheme<E, Rng = ChaCha8Rng>,
Pcs: PolynomialCommitmentScheme<E>,
{
for num_vars in num_vars_start..num_vars_end {
let rng = ChaCha8Rng::from_seed([0u8; 32]);
Expand Down

0 comments on commit 2d7baf7

Please sign in to comment.