Skip to content

Commit

Permalink
Extend Circuit trait to take parameters in config (privacy-scaling-ex…
Browse files Browse the repository at this point in the history
…plorations#168)

* Extend Circuit trait to take parameters in config

The Circuit trait is extended with the following:
```
pub trait Circuit<F: Field> {
    /// [...]
    type Params: Default;

    fn params(&self) -> Self::Params {
        Self::Params::default()
    }

    fn configure_with_params(meta: &mut ConstraintSystem<F>, params: &Self::Params) -> Self::Config {
        Self::configure(meta)
    }

    fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config;
}
```

This allows runtime parametrization of the circuit configuration.  The extension to the Circuit trait has been designed to minimize the breaking change: existing circuits only need to define the associated `type Params`.

Unfortunately "Associated type defaults" are unstable in Rust, otherwise this would be a non-breaking change.  See rust-lang/rust#29661

* Implement circuit params under feature flag

* Don't overwrite configure method

* Fix doc test
  • Loading branch information
ed255 authored and Velaciela committed Oct 9, 2023
1 parent 6adc633 commit b26a0ae
Show file tree
Hide file tree
Showing 36 changed files with 163 additions and 9 deletions.
1 change: 1 addition & 0 deletions halo2_gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ bench = false

[features]
dev-graph = ["halo2_proofs/dev-graph", "plotters"]
circuit-params = ["halo2_proofs/circuit-params"]
test-dependencies = ["proptest"]
unstable = []

Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/benches/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ where
{
type Config = MyConfig<WIDTH, RATE, L>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self {
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ fn bench(name: &str, k: u32, c: &mut Criterion) {
impl Circuit<pallas::Base> for MyCircuit {
type Config = Table16Config;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/ecc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ pub(crate) mod tests {
impl Circuit<pallas::Base> for MyCircuit {
type Config = EccConfig<TestFixedBases>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit { test_errors: false }
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/ecc/chip/mul_fixed/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ pub mod tests {
impl Circuit<pallas::Base> for MyCircuit {
type Config = EccConfig<TestFixedBases>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
4 changes: 4 additions & 0 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ mod tests {
{
type Config = Pow5Config<Fp, WIDTH, RATE>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
PermuteCircuit::<S, WIDTH, RATE>(PhantomData)
Expand Down Expand Up @@ -735,6 +737,8 @@ mod tests {
{
type Config = Pow5Config<Fp, WIDTH, RATE>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self {
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sha256/table16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ mod tests {
impl Circuit<pallas::Base> for MyCircuit {
type Config = Table16Config;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sha256/table16/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,8 @@ mod tests {
impl Circuit<pallas::Base> for MyCircuit {
type Config = Table16Config;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sha256/table16/message_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ mod tests {
impl Circuit<pallas::Base> for MyCircuit {
type Config = Table16Config;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sha256/table16/spread_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ mod tests {
impl<F: PrimeField> Circuit<F> for MyCircuit {
type Config = SpreadTableConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sinsemilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ pub(crate) mod tests {
SinsemillaConfig<TestHashDomain, TestCommitDomain, TestFixedBases>,
);
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/sinsemilla/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ pub mod tests {
MerkleConfig<TestHashDomain, TestCommitDomain, TestFixedBases>,
);
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ mod tests {
impl<const RANGE: usize> Circuit<pallas::Base> for MyCircuit<RANGE> {
type Config = Config;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit(self.0)
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/utilities/cond_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ mod tests {
impl<F: PrimeField> Circuit<F> for MyCircuit<F> {
type Config = CondSwapConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_gadgets/src/utilities/decompose_running_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ mod tests {
{
type Config = RunningSumConfig<F, WINDOW_NUM_BITS>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self {
Expand Down
4 changes: 4 additions & 0 deletions halo2_gadgets/src/utilities/lookup_range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ mod tests {
impl<F: PrimeFieldBits> Circuit<F> for MyCircuit<F> {
type Config = LookupRangeCheckConfig<F, K>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
*self
Expand Down Expand Up @@ -506,6 +508,8 @@ mod tests {
impl<F: PrimeFieldBits> Circuit<F> for MyCircuit<F> {
type Config = LookupRangeCheckConfig<F, K>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {
Expand Down
1 change: 1 addition & 0 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ parallel_syn = []
phase-check = []
profile = ["ark-std/print-trace"]
mock-batch-inv = []
circuit-params = []

[lib]
bench = false
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/benches/dev_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ fn criterion_benchmark(c: &mut Criterion) {
impl<F: PrimeField> Circuit<F> for MyCircuit<F> {
type Config = MyConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/benches/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ fn criterion_benchmark(c: &mut Criterion) {
impl<F: Field> Circuit<F> for MyCircuit<F> {
type Config = PlonkConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self {
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/examples/circuit-layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ impl<FF: Field> StandardCs<FF> for StandardPlonk<FF> {
impl<F: Field> Circuit<F> for MyCircuit<F> {
type Config = PlonkConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self {
Expand Down
12 changes: 10 additions & 2 deletions halo2_proofs/examples/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ struct StandardPlonk(Fr);
impl Circuit<Fr> for StandardPlonk {
type Config = StandardPlonkConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down Expand Up @@ -140,8 +142,14 @@ fn main() {

let f = File::open("serialization-test.pk").unwrap();
let mut reader = BufReader::new(f);
let pk = ProvingKey::<G1Affine>::read::<_, StandardPlonk>(&mut reader, SerdeFormat::RawBytes)
.unwrap();
#[allow(clippy::unit_arg)]
let pk = ProvingKey::<G1Affine>::read::<_, StandardPlonk>(
&mut reader,
SerdeFormat::RawBytes,
#[cfg(feature = "circuit-params")]
circuit.params(),
)
.unwrap();

std::fs::remove_file("serialization-test.pk").unwrap();

Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/examples/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ impl<F: Field, const W: usize, const H: usize> MyCircuit<F, W, H> {
impl<F: Field, const W: usize, const H: usize> Circuit<F> for MyCircuit<F, W, H> {
type Config = MyConfig<W>;
type FloorPlanner = V1;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/examples/simple-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ impl<F: Field> Circuit<F> for MyCircuit<F> {
// Since we are using a single chip for everything, we can just reuse its config.
type Config = FieldConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/examples/two-chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ impl<F: Field> Circuit<F> for MyCircuit<F> {
// Since we are using a single chip for everything, we can just reuse its config.
type Config = FieldConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/src/circuit/floor_planner/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ mod tests {
impl Circuit<vesta::Scalar> for MyCircuit {
type Config = Column<Advice>;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
2 changes: 2 additions & 0 deletions halo2_proofs/src/circuit/floor_planner/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ mod tests {
impl Circuit<vesta::Scalar> for MyCircuit {
type Config = Column<Advice>;
type FloorPlanner = super::V1;
#[cfg(feature = "circuit-params")]
type Params = ();

fn without_witnesses(&self) -> Self {
MyCircuit {}
Expand Down
13 changes: 13 additions & 0 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ impl<F: Field> Mul<F> for Value<F> {
/// impl<F: PrimeField> Circuit<F> for MyCircuit {
/// type Config = MyConfig;
/// type FloorPlanner = SimpleFloorPlanner;
/// #[cfg(feature = "circuit-params")]
/// type Params = ();
///
/// fn without_witnesses(&self) -> Self {
/// Self::default()
Expand Down Expand Up @@ -925,6 +927,9 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
let n = 1 << k;

let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params());
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);
let cs = cs;

Expand Down Expand Up @@ -1983,6 +1988,8 @@ mod tests {
impl Circuit<Fp> for FaultyCircuit {
type Config = FaultyCircuitConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
let a = meta.advice_column();
Expand Down Expand Up @@ -2069,6 +2076,8 @@ mod tests {
impl Circuit<Fp> for FaultyCircuit {
type Config = FaultyCircuitConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
let a = meta.advice_column();
Expand Down Expand Up @@ -2238,6 +2247,8 @@ mod tests {
impl Circuit<Fp> for FaultyCircuit {
type Config = FaultyCircuitConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
let a = meta.advice_column();
Expand Down Expand Up @@ -2372,6 +2383,8 @@ mod tests {
impl Circuit<Fp> for FaultyCircuit {
type Config = FaultyCircuitConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();

fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
let a = meta.advice_column();
Expand Down
3 changes: 3 additions & 0 deletions halo2_proofs/src/dev/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ impl<G: PrimeGroup, ConcreteCircuit: Circuit<G::Scalar>> CircuitCost<G, Concrete
pub fn measure(k: usize, circuit: &ConcreteCircuit) -> Self {
// Collect the layout details.
let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params());
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);
let mut assembly = Assembly {
selectors: vec![vec![false; 1 << k]; cs.num_selectors],
Expand Down
12 changes: 11 additions & 1 deletion halo2_proofs/src/dev/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct Gate {
/// impl<F: Field> Circuit<F> for MyCircuit {
/// type Config = MyConfig;
/// type FloorPlanner = SimpleFloorPlanner;
/// #[cfg(feature = "circuit-params")]
/// type Params = ();
///
/// fn without_witnesses(&self) -> Self {
/// Self::default()
Expand Down Expand Up @@ -79,6 +81,9 @@ struct Gate {
/// }
/// }
///
/// #[cfg(feature = "circuit-params")]
/// let gates = CircuitGates::collect::<pallas::Base, MyCircuit>(());
/// #[cfg(not(feature = "circuit-params"))]
/// let gates = CircuitGates::collect::<pallas::Base, MyCircuit>();
/// assert_eq!(
/// format!("{}", gates),
Expand All @@ -103,9 +108,14 @@ pub struct CircuitGates {

impl CircuitGates {
/// Collects the gates from within the circuit.
pub fn collect<F: PrimeField, C: Circuit<F>>() -> Self {
pub fn collect<F: PrimeField, C: Circuit<F>>(
#[cfg(feature = "circuit-params")] params: C::Params,
) -> Self {
// Collect the graph details.
let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let _ = C::configure_with_params(&mut cs, params);
#[cfg(not(feature = "circuit-params"))]
let _ = C::configure(&mut cs);

let gates = cs
Expand Down
3 changes: 3 additions & 0 deletions halo2_proofs/src/dev/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub fn circuit_dot_graph<F: Field, ConcreteCircuit: Circuit<F>>(
) -> String {
// Collect the graph details.
let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params());
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);
let mut graph = Graph::default();
ConcreteCircuit::FloorPlanner::synthesize(&mut graph, circuit, config, cs.constants).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions halo2_proofs/src/dev/graph/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ impl CircuitLayout {
let n = 1 << k;
// Collect the layout details.
let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params());
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);
let mut layout = Layout::new(k, n, cs.num_selectors);
ConcreteCircuit::FloorPlanner::synthesize(
Expand Down
Loading

0 comments on commit b26a0ae

Please sign in to comment.