Skip to content

Commit

Permalink
Downsize parameters to match size of the circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
iquerejeta committed Jan 21, 2025
1 parent c0f10db commit a37694d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benches/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ fn criterion_benchmark(c: &mut Criterion) {
a: Value::unknown(),
k,
};
let vk = keygen_vk(&params, &empty_circuit).expect("keygen_vk should not fail");
let vk = keygen_vk_with_k(&params, &empty_circuit, k).expect("keygen_vk should not fail");
let pk = keygen_pk(vk, &empty_circuit).expect("keygen_pk should not fail");
(params, pk)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use halo2_proofs::transcript::{CircuitTranscript, Transcript};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
plonk::{
create_proof, keygen_pk, keygen_vk, prepare, Advice, Circuit, Column, ConstraintSystem,
Error, Fixed, Instance, ProvingKey,
create_proof, keygen_pk, keygen_vk_with_k, prepare, Advice, Circuit, Column,
ConstraintSystem, Error, Fixed, Instance, ProvingKey,
},
poly::{
kzg::{params::ParamsKZG, KZGCommitmentScheme},
Expand Down Expand Up @@ -128,7 +128,7 @@ fn main() {
let k = 4;
let circuit = StandardPlonk(Fr::random(OsRng));
let params = ParamsKZG::<Bn256>::unsafe_setup(k, OsRng);
let vk = keygen_vk::<_, KZGCommitmentScheme<Bn256>, _>(&params, &circuit)
let vk = keygen_vk_with_k::<_, KZGCommitmentScheme<Bn256>, _>(&params, &circuit, k)
.expect("vk should not fail");
let pk = keygen_pk(vk, &circuit).expect("pk should not fail");

Expand Down
2 changes: 1 addition & 1 deletion examples/vector-ops-unblinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ where
+ Ord,
{
let params: ParamsKZG<E> = ParamsKZG::unsafe_setup(k, OsRng);
let vk = keygen_vk(&params, &circuit).unwrap();
let vk = keygen_vk_with_k(&params, &circuit, k).unwrap();
let pk = keygen_pk(vk, &circuit).unwrap();

let proof = {
Expand Down
16 changes: 11 additions & 5 deletions src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ pub fn k_from_circuit<F: Ord + Field + FromUniformBytes<64>, C: Circuit<F>>(circ
.expect("A circuit which can be implemented with at most 2^24 rows.")
}

/// Generate a `VerifyingKey` from an instance of `Circuit`.
/// Generates a `VerifyingKey` from a `Circuit` instance.
///
/// This function automatically generates the VK using the smallest
/// value of k required for the ConcreteCircuit.
/// To specify a particular value for k, use keygen_vk_with_k instead.
/// Automatically determines the smallest `k` required for the given circuit
/// and adjusts the received parameters to match the circuit's size.
/// Use `keygen_vk_with_k` to specify a custom `k` value.
pub fn keygen_vk<F, CS, ConcreteCircuit>(
params: &CS::Parameters,
params: &mut CS::Parameters,
circuit: &ConcreteCircuit,
) -> Result<VerifyingKey<F, CS>, Error>
where
Expand All @@ -259,6 +259,12 @@ where
{
let k = k_from_circuit(circuit);

if params.max_k() < k {
return Err(Error::not_enough_rows_available(params.max_k()));
}

params.downsize(k);

keygen_vk_with_k(params, circuit, k)
}

Expand Down
4 changes: 2 additions & 2 deletions src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ fn test_create_proof() {
}
}

let params: ParamsKZG<Bn256> = ParamsKZG::unsafe_setup(3, OsRng);
let vk = keygen_vk(&params, &MyCircuit).expect("keygen_vk should not fail");
let mut params: ParamsKZG<Bn256> = ParamsKZG::unsafe_setup(3, OsRng);
let vk = keygen_vk(&mut params, &MyCircuit).expect("keygen_vk should not fail");
let pk = keygen_pk(vk, &MyCircuit).expect("keygen_pk should not fail");
let mut transcript = CircuitTranscript::<_>::init();

Expand Down
3 changes: 3 additions & 0 deletions src/poly/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ pub trait Guard<F: PrimeField, CS: PolynomialCommitmentScheme<F>>: Sized {
pub trait Params {
/// Returns the max size of polynomials that these parameters can commit to
fn max_k(&self) -> u32;

/// Downsize the params to work with a circuit of size `new_k`
fn downsize(&mut self, new_k: u32);
}
8 changes: 8 additions & 0 deletions src/poly/kzg/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ impl<E: Engine> Params for ParamsKZG<E> {

self.g.len().ilog2()
}

fn downsize(&mut self, new_k: u32) {
ParamsKZG::<E>::downsize(self, new_k)
}
}

impl<E: Engine + Debug> ParamsKZG<E> {
/// Downsize the current parameters to match a smaller `k`.
pub fn downsize(&mut self, new_k: u32) {
if self.max_k() == new_k {
return;
}

let n = 1 << new_k;
assert!(n < self.g_lagrange.len() as u32);
self.g.truncate(n as usize);
Expand Down
15 changes: 8 additions & 7 deletions tests/plonk_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use ff::{FromUniformBytes, WithSmallOrderMulGroup};
use halo2_proofs::circuit::{Cell, Layouter, SimpleFloorPlanner, Value};
use halo2_proofs::dev::MockProver;
use halo2_proofs::plonk::{
create_proof as create_plonk_proof, keygen_pk, keygen_vk, prepare as prepare_plonk_proof,
Advice, Circuit, Column, ConstraintSystem, Error, Fixed, ProvingKey, TableColumn, VerifyingKey,
create_proof as create_plonk_proof, keygen_pk, keygen_vk, keygen_vk_with_k,
prepare as prepare_plonk_proof, Advice, Circuit, Column, ConstraintSystem, Error, Fixed,
ProvingKey, TableColumn, VerifyingKey,
};
use halo2_proofs::poly::commitment::{Guard, PolynomialCommitmentScheme};
use halo2_proofs::poly::kzg::params::ParamsKZG;
Expand Down Expand Up @@ -414,7 +415,7 @@ fn plonk_api() {
// k that is too small for the minimum required number of rows.
let much_too_small_params= <$scheme as PolynomialCommitmentScheme<$field>>::gen_params(1);
assert_matches!(
keygen_vk::<_, $scheme, _>(&much_too_small_params, &empty_circuit),
keygen_vk_with_k::<_, $scheme, _>(&much_too_small_params, &empty_circuit, 1),
Err(Error::NotEnoughRowsAvailable {
current_k: 1,
})
Expand All @@ -424,7 +425,7 @@ fn plonk_api() {
// k that is too small for the number of rows the circuit uses.
let slightly_too_small_params = <$scheme as PolynomialCommitmentScheme<$field>>::gen_params(K-1);
assert_matches!(
keygen_vk::<_, $scheme, _>(&slightly_too_small_params, &empty_circuit),
keygen_vk_with_k::<_, $scheme, _>(&slightly_too_small_params, &empty_circuit, K - 1),
Err(Error::NotEnoughRowsAvailable {
current_k,
}) if current_k == K - 1
Expand All @@ -433,7 +434,7 @@ fn plonk_api() {
}

fn keygen<F, Scheme: PolynomialCommitmentScheme<F>>(
params: &Scheme::Parameters,
params: &mut Scheme::Parameters,
) -> ProvingKey<F, Scheme>
where
F: FromUniformBytes<64> + WithSmallOrderMulGroup<3> + Ord,
Expand Down Expand Up @@ -529,9 +530,9 @@ fn plonk_api() {
bad_keys!(Fr, Scheme);

let rng = OsRng;
let params = ParamsKZG::<Bn256>::unsafe_setup(K, rng);
let mut params = ParamsKZG::<Bn256>::unsafe_setup(K, rng);

let pk = keygen::<Fr, Scheme>(&params);
let pk = keygen::<Fr, Scheme>(&mut params);

let proof = create_proof::<Fr, Scheme, CircuitTranscript<State>, _>(rng, &params, &pk);

Expand Down

0 comments on commit a37694d

Please sign in to comment.