Skip to content

Commit

Permalink
Use ::setup
Browse files Browse the repository at this point in the history
  • Loading branch information
adria0 committed Jun 11, 2024
1 parent f81fa63 commit 3d7cca4
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 63 deletions.
4 changes: 2 additions & 2 deletions halo2_backend/src/poly/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait CommitmentScheme {
type ParamsVerifier: for<'params> ParamsVerifier<'params, Self::Curve>;

/// Wrapper for parameter generator
fn new_params(k: u32, rng: impl RngCore) -> Self::ParamsProver;
fn new_params(k: u32) -> Self::ParamsProver;

/// Wrapper for parameter reader
fn read_params<R: io::Read>(reader: &mut R) -> io::Result<Self::ParamsProver>;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub trait Params<C: CurveAffine>: Sized + Clone + Debug {
/// Parameters for circuit synthesis and prover parameters.
pub trait ParamsProver<C: CurveAffine>: Params<C> {
/// Returns new instance of parameters
fn new(k: u32, rng: impl RngCore) -> Self;
fn new(k: u32) -> Self;

/// This computes a commitment to a polynomial described by the provided
/// slice of coefficients. The commitment may be blinded by the blinding
Expand Down
13 changes: 6 additions & 7 deletions halo2_backend/src/poly/ipa/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::poly::{Coeff, LagrangeCoeff, Polynomial};

use group::{Curve, Group};
use halo2_middleware::zal::traits::MsmAccel;
use rand_core::RngCore;
use std::marker::PhantomData;

mod prover;
Expand Down Expand Up @@ -46,8 +45,8 @@ impl<C: CurveAffine> CommitmentScheme for IPACommitmentScheme<C> {
type ParamsProver = ParamsIPA<C>;
type ParamsVerifier = ParamsVerifierIPA<C>;

fn new_params(k: u32, rng: impl RngCore) -> Self::ParamsProver {
ParamsIPA::new(k, rng)
fn new_params(k: u32) -> Self::ParamsProver {
ParamsIPA::new(k)
}

fn read_params<R: io::Read>(reader: &mut R) -> io::Result<Self::ParamsProver> {
Expand Down Expand Up @@ -151,7 +150,7 @@ impl<C: CurveAffine> Params<C> for ParamsIPA<C> {
impl<C: CurveAffine> ParamsProver<C> for ParamsIPA<C> {
/// Initializes parameters for the curve, given a random oracle to draw
/// points from.
fn new(k: u32, _: impl RngCore) -> Self {
fn new(k: u32) -> Self {
// This is usually a limitation on the curve, but we also want 32-bit
// architectures to be supported.
assert!(k < 32);
Expand Down Expand Up @@ -254,7 +253,7 @@ mod test {
use halo2curves::pasta::{EpAffine, Fq};

let engine = H2cEngine::new();
let params = ParamsIPA::<EpAffine>::new(K, OsRng);
let params = ParamsIPA::<EpAffine>::new(K);
let domain = EvaluationDomain::new(1, K);

let mut a = domain.empty_lagrange();
Expand Down Expand Up @@ -283,7 +282,7 @@ mod test {
use halo2curves::pasta::{EqAffine, Fp};

let engine = H2cEngine::new();
let params: ParamsIPA<EqAffine> = ParamsIPA::<EqAffine>::new(K, OsRng);
let params: ParamsIPA<EqAffine> = ParamsIPA::<EqAffine>::new(K);
let domain = EvaluationDomain::new(1, K);

let mut a = domain.empty_lagrange();
Expand Down Expand Up @@ -323,7 +322,7 @@ mod test {
let rng = OsRng;

let engine = H2cEngine::new();
let params = ParamsIPA::<EpAffine>::new(K, OsRng);
let params = ParamsIPA::<EpAffine>::new(K);
let mut params_buffer = vec![];
<ParamsIPA<_> as Params<_>>::write(&params, &mut params_buffer).unwrap();
let params: ParamsIPA<EpAffine> = Params::read::<_>(&mut &params_buffer[..]).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions halo2_backend/src/poly/ipa/msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,14 @@ mod tests {
pasta::{Ep, EpAffine, Fp, Fq},
CurveAffine,
};
use rand_core::OsRng;

#[test]
fn msm_arithmetic() {
let base: Ep = EpAffine::from_xy(-Fp::one(), Fp::from(2)).unwrap().into();
let base_viol = base + base;

let engine = H2cEngine::new();
let params = ParamsIPA::new(4, OsRng);
let params = ParamsIPA::new(4);
let mut a: MSMIPA<EpAffine> = MSMIPA::new(&params);
a.append_term(Fq::one(), base);
// a = [1] P
Expand Down
15 changes: 7 additions & 8 deletions halo2_backend/src/poly/kzg/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use halo2_middleware::ff::{Field, PrimeField};
use halo2_middleware::zal::traits::MsmAccel;
use halo2curves::pairing::Engine;
use halo2curves::{CurveAffine, CurveExt};
use rand_core::RngCore;
use rand_core::{OsRng, RngCore};
use std::fmt::Debug;
use std::marker::PhantomData;

Expand Down Expand Up @@ -139,8 +139,8 @@ where
type ParamsProver = ParamsKZG<E>;
type ParamsVerifier = ParamsVerifierKZG<E>;

fn new_params(k: u32, rng: impl RngCore) -> Self::ParamsProver {
ParamsKZG::new(k, rng)
fn new_params(k: u32) -> Self::ParamsProver {
ParamsKZG::new(k)
}

fn read_params<R: io::Read>(reader: &mut R) -> io::Result<Self::ParamsProver> {
Expand Down Expand Up @@ -429,8 +429,8 @@ where
E::G1: CurveExt<AffineExt = E::G1Affine>,
E::G2Affine: SerdeCurveAffine,
{
fn new(k: u32, rng: impl RngCore) -> Self {
Self::setup(k, rng)
fn new(k: u32) -> Self {
Self::setup(k, OsRng)
}

fn commit(
Expand All @@ -455,7 +455,6 @@ mod test {
use crate::poly::kzg::commitment::ParamsKZG;
use halo2_middleware::ff::Field;
use halo2_middleware::zal::impls::H2cEngine;
use rand_core::OsRng;

#[test]
fn test_commit_lagrange() {
Expand All @@ -467,7 +466,7 @@ mod test {
use halo2curves::bn256::{Bn256, Fr};

let engine = H2cEngine::new();
let params = ParamsKZG::<Bn256>::new(K, OsRng);
let params = ParamsKZG::<Bn256>::new(K);
let domain = EvaluationDomain::new(1, K);

let mut a = domain.empty_lagrange();
Expand All @@ -493,7 +492,7 @@ mod test {
use super::super::commitment::Params;
use halo2curves::bn256::Bn256;

let params0 = ParamsKZG::<Bn256>::new(K, OsRng);
let params0 = ParamsKZG::<Bn256>::new(K);
let mut data = vec![];
<ParamsKZG<_> as Params<_>>::write(&params0, &mut data).unwrap();
let params1: ParamsKZG<Bn256> = Params::read::<_>(&mut &data[..]).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions halo2_backend/src/poly/multiopen_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsIPA::<EqAffine>::new(K, OsRng);
let params = ParamsIPA::<EqAffine>::new(K);

let proof = create_proof::<
IPACommitmentScheme<EqAffine>,
Expand Down Expand Up @@ -67,7 +67,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsIPA::<EqAffine>::new(K, OsRng);
let params = ParamsIPA::<EqAffine>::new(K);

let proof = create_proof::<
IPACommitmentScheme<EqAffine>,
Expand Down Expand Up @@ -105,7 +105,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsKZG::<Bn256>::new(K, OsRng);
let params = ParamsKZG::<Bn256>::new(K);

let proof = create_proof::<_, ProverGWC<_>, _, Blake2bWrite<_, _, Challenge255<_>>>(
&engine, &params,
Expand Down Expand Up @@ -138,7 +138,7 @@ mod test {
const K: u32 = 4;

let engine = H2cEngine::new();
let params = ParamsKZG::<Bn256>::new(K, OsRng);
let params = ParamsKZG::<Bn256>::new(K);

let proof = create_proof::<
KZGCommitmentScheme<Bn256>,
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/benches/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn criterion_benchmark(c: &mut Criterion) {
}

fn keygen(k: u32) -> (ParamsIPA<EqAffine>, ProvingKey<EqAffine>) {
let params: ParamsIPA<EqAffine> = ParamsIPA::new(k, OsRng);
let params: ParamsIPA<EqAffine> = ParamsIPA::new(k);
let empty_circuit: MyCircuit<Fp> = MyCircuit {
a: Value::unknown(),
k,
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/tests/frontend_backend_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ fn test_mycircuit_full_legacy() {

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"abd4d6d18640def2cb3e3b4de0afe61ec2c5e64705f4386e8f96468d110e9df9",
"c5c11281474b586795a5d97bdefeee80456d2921584b3a8b00523eebd49f2fac",
halo2_debug::keccak_hex(proof),
);
}
Expand Down Expand Up @@ -614,7 +614,7 @@ instances.clone(),

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"abd4d6d18640def2cb3e3b4de0afe61ec2c5e64705f4386e8f96468d110e9df9",
"c5c11281474b586795a5d97bdefeee80456d2921584b3a8b00523eebd49f2fac",
halo2_debug::keccak_hex(proof),
);
}
16 changes: 8 additions & 8 deletions halo2_proofs/tests/plonk_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use halo2_proofs::transcript::{
Blake2bRead, Blake2bWrite, Challenge255, EncodedChallenge, TranscriptReadBuffer,
TranscriptWriterBuffer,
};
use rand_core::{OsRng, RngCore};
use rand_core::RngCore;
use std::marker::PhantomData;

#[test]
Expand Down Expand Up @@ -437,7 +437,7 @@ fn plonk_api() {

// Check that we get an error if we try to initialize the proving key with a value of
// k that is too small for the minimum required number of rows.
let much_too_small_params= <$scheme as CommitmentScheme>::ParamsProver::new(1, OsRng);
let much_too_small_params= <$scheme as CommitmentScheme>::ParamsProver::new(1);
assert_matches!(
keygen_vk(&much_too_small_params, &empty_circuit),
Err(Error::Frontend(ErrorFront::NotEnoughRowsAvailable {
Expand All @@ -447,7 +447,7 @@ fn plonk_api() {

// Check that we get an error if we try to initialize the proving key with a value of
// k that is too small for the number of rows the circuit uses.
let slightly_too_small_params = <$scheme as CommitmentScheme>::ParamsProver::new(K-1,OsRng);
let slightly_too_small_params = <$scheme as CommitmentScheme>::ParamsProver::new(K-1);
assert_matches!(
keygen_vk(&slightly_too_small_params, &empty_circuit),
Err(Error::Frontend(ErrorFront::NotEnoughRowsAvailable {
Expand Down Expand Up @@ -579,7 +579,7 @@ fn plonk_api() {

let mut rng = one_rng();

let params = ParamsKZG::<Bn256>::new(K, &mut rng);
let params = ParamsKZG::<Bn256>::setup(K, &mut rng);
let pk = keygen::<KZGCommitmentScheme<_>>(&params);

let proof = create_proof::<_, ProverGWC<_>, _, _, Blake2bWrite<_, _, Challenge255<_>>>(
Expand All @@ -598,7 +598,7 @@ fn plonk_api() {

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"fce5b7c977f643baeafb383f1793daa6795aeae3d708616381af0f6f8e4170f7",
"50969312b469ebbc528e6c765e8483b53c92292028a85afda22fa83a7b76c667",
halo2_debug::keccak_hex(proof),
);
}
Expand All @@ -613,7 +613,7 @@ fn plonk_api() {
bad_keys!(Scheme);

let mut rng = one_rng();
let params = ParamsKZG::<Bn256>::new(K, &mut rng);
let params = ParamsKZG::<Bn256>::setup(K, &mut rng);

let pk = keygen::<KZGCommitmentScheme<_>>(&params);

Expand All @@ -633,7 +633,7 @@ fn plonk_api() {

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"e0121d6d53892969fbf4e08ddc74c4cba02695b7c6421e62a30f9c30b8ae4ae6",
"ade2d9dae7d02871c63d0a80bc0e09d536138e49b4925c62046e2e86cb288bc3",
halo2_debug::keccak_hex(proof),
);
}
Expand All @@ -648,7 +648,7 @@ fn plonk_api() {
bad_keys!(Scheme);

let mut rng = one_rng();
let params = ParamsIPA::<EqAffine>::new(K, &mut rng);
let params = ParamsIPA::<EqAffine>::new(K);

let pk = keygen::<IPACommitmentScheme<EqAffine>>(&params);

Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn test_serialization() {

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"42d69881fc453fc9eac7cb51487cf98ac049db97bae0ce05338917d18d99ef21",
"431a9b8a8e289cc85655576965f443da657e88a6cdc200179d8916c23bd8bda7",
halo2_debug::keccak_hex(proof),
)
}
6 changes: 3 additions & 3 deletions halo2_proofs/tests/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ fn test_prover<C: CurveAffine, const W: usize, const H: usize>(
where
C::Scalar: FromUniformBytes<64>,
{
let mut rng = one_rng();
let rng = one_rng();

let params = ParamsIPA::<C>::new(k, &mut rng);
let params = ParamsIPA::<C>::new(k);
let vk = keygen_vk(&params, &circuit).unwrap();
let pk = keygen_pk(&params, vk, &circuit).unwrap();

Expand Down Expand Up @@ -336,7 +336,7 @@ fn test_shuffle() {

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"deeffa8f048fedfaf412b39484899714e46d08ed349c767341c8d6373ba25edc",
"7a0cfc86f1f37b7c425a441abbe7a04a37c593c4b08c8d620c6615250d690020",
halo2_debug::keccak_hex(_proof),
);
}
Expand Down
8 changes: 4 additions & 4 deletions halo2_proofs/tests/shuffle_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{marker::PhantomData, vec};

use ff::FromUniformBytes;
use halo2_debug::one_rng;
use halo2_proofs::poly::commitment::ParamsProver;
use halo2_proofs::{
arithmetic::Field,
circuit::{Layouter, SimpleFloorPlanner, Value},
Expand All @@ -11,7 +12,6 @@ use halo2_proofs::{
},
poly::Rotation,
poly::{
commitment::ParamsProver,
ipa::{
commitment::{IPACommitmentScheme, ParamsIPA},
multiopen::{ProverIPA, VerifierIPA},
Expand Down Expand Up @@ -152,9 +152,9 @@ fn test_prover<C: CurveAffine>(k: u32, circuit: MyCircuit<C::Scalar>, expected:
where
C::Scalar: FromUniformBytes<64>,
{
let mut rng = one_rng();
let rng = one_rng();

let params = ParamsIPA::<C>::new(k, &mut rng);
let params = ParamsIPA::<C>::new(k);
let vk = keygen_vk(&params, &circuit).unwrap();
let pk = keygen_pk(&params, vk, &circuit).unwrap();

Expand Down Expand Up @@ -221,7 +221,7 @@ fn test_shuffle_api() {

#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"6f9141496227a467b91395cbc601c69764660ce6aac5624e319119887ce0453c",
"b1409b3ae49babc56f0ba6279f7f577a657082f03371740196cf00c7b259a02d",
halo2_debug::keccak_hex(_proof),
);
}
8 changes: 4 additions & 4 deletions halo2_proofs/tests/vector-ops-unblinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ fn test_prover<C: CurveAffine>(
where
C::Scalar: FromUniformBytes<64>,
{
let mut rng = one_rng();
let rng = one_rng();

let params = ParamsIPA::<C>::new(k, &mut rng);
let params = ParamsIPA::<C>::new(k);
let vk = keygen_vk(&params, &circuit).unwrap();
let pk = keygen_pk(&params, vk, &circuit).unwrap();

Expand Down Expand Up @@ -550,15 +550,15 @@ fn test_vector_ops_unbinded() {
let proof_1 = test_prover::<halo2curves::pasta::EqAffine>(k, mul_circuit, true, c_mul);
#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"99d6cadd2596dce8dc8918907ba9cb051a5d9775f9fb7ed6ac51cc75e41e6da4",
"3c24612b31ee894b843867429d0ba99bbe9ce3f3ea40dd86041239566651dba8",
halo2_debug::keccak_hex(&proof_1),
);

// the commitments will be the first columns of the proof transcript so we can compare them easily
let proof_2 = test_prover::<halo2curves::pasta::EqAffine>(k, add_circuit, true, c_add);
#[cfg(all(feature = "vector-tests", not(coverage)))]
assert_eq!(
"159fe77867a8ee7e1cac1da22d375e864816bbbbb0b8b1d6f2ace241b35c115a",
"5863afa234a433b2b7e198f51e81ba286b0db89d0794dbab4aca73e03fcd9b0e",
halo2_debug::keccak_hex(&proof_2),
);

Expand Down
1 change: 1 addition & 0 deletions p3_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ p3-matrix = { git = "https://github.com/Plonky3/Plonky3", rev = "7b5b8a6" }
p3-field = { git = "https://github.com/Plonky3/Plonky3", rev = "7b5b8a6" }
p3-uni-stark = { git = "https://github.com/Plonky3/Plonky3", rev = "7b5b8a6" }
halo2_middleware = { path = "../halo2_middleware" }
halo2_debug = { path = "../halo2_debug" }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
num-bigint = { version = "0.4.3", default-features = false }

Expand Down
Loading

0 comments on commit 3d7cca4

Please sign in to comment.