Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Circuit trait to take parameters in config #168

Merged
merged 4 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -79,6 +79,7 @@ dev-graph = ["plotters", "tabbycat"]
gadget-traces = ["backtrace"]
sanity-checks = []
batch = ["rand_core/getrandom"]
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 @@ -479,6 +479,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 @@ -517,6 +517,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 @@ -208,6 +208,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 @@ -601,6 +603,9 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<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 @@ -1536,6 +1541,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 @@ -1622,6 +1629,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 @@ -1791,6 +1800,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 @@ -1925,6 +1936,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 @@ -150,6 +150,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 @@ -22,6 +22,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