Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Encapsulate functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkzmn committed Jul 5, 2024
1 parent 3ea84a1 commit ba930c7
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 251 deletions.
9 changes: 5 additions & 4 deletions examples/blake2f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use chiquito::{
},
plonkish::{
backend::halo2::{
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_prove, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng,
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng, Halo2Setup,
},
compiler::{
cell_manager::{MaxWidthCellManager, SingleRowCellManager},
Expand Down Expand Up @@ -1487,11 +1487,12 @@ fn main() {
let rng = BlockRng::new(DummyRng {});

let (cs, params, vk, pk) = get_super_circuit_halo2_setup(9, &mut circuit, rng);
let halo2_setup = Halo2Setup::new(cs, params, vk, pk, circuit.sub_circuits, witness);

let rng = BlockRng::new(DummyRng {});
let (proof, instance) = halo2_prove(&params, pk, rng, cs, witness, &circuit.sub_circuits);
let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, params, vk, instance);
let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand Down
44 changes: 13 additions & 31 deletions examples/factorial.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::{collections::HashMap, hash::Hash};
use std::hash::Hash;

use chiquito::{
field::Field,
frontend::dsl::{circuit, trace::DSLTraceGenerator}, /* main function for constructing an AST
* circuit */
plonkish::{
backend::halo2::{get_halo2_setup, halo2_prove, halo2_verify, ChiquitoHalo2, DummyRng},
backend::halo2::{halo2_verify, DummyRng, PlonkishHalo2},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
config,
step_selector::SimpleStepSelectorBuilder,
PlonkishCompilationResult,
},
ir::{assignments::AssignmentGenerator, Circuit},
}, /* compiles to
* Chiquito Halo2
* backend,
Expand All @@ -27,8 +27,8 @@ use rand_chacha::rand_core::block::BlockRng;

const MAX_FACTORIAL: usize = 10;

type AssignGen<F> = AssignmentGenerator<F, DSLTraceGenerator<F, u32>>;
fn generate<F: Field + From<u64> + Hash>() -> (Circuit<F>, Option<AssignGen<F>>) {
fn generate<F: Field + From<u64> + Hash>() -> PlonkishCompilationResult<F, DSLTraceGenerator<F, u32>>
{
// table for the circuit:
// | step_type | i | x |
// ----------------------------------
Expand Down Expand Up @@ -138,22 +138,13 @@ fn main() {
let plonkish = generate::<Fr>();
let rng = BlockRng::new(DummyRng {});

let (cs, params, vk, pk, chiquito_halo2) =
get_halo2_setup(10, ChiquitoHalo2::new(plonkish.0), rng);
let halo2_setup = plonkish.get_halo2_setup(10, rng, 0);

let rng = BlockRng::new(DummyRng {});
let witness = plonkish.1.unwrap().generate(0);

let (proof, instance) = halo2_prove(
&params,
pk,
rng,
cs,
HashMap::from([(chiquito_halo2.ir_id, witness)]),
&vec![chiquito_halo2],
);
let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, params, vk, instance);
let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand All @@ -164,22 +155,13 @@ fn main() {
let plonkish = generate::<Fr>();
let rng = BlockRng::new(DummyRng {});

let (cs, params, vk, pk, chiquito_halo2) =
get_halo2_setup(8, ChiquitoHalo2::new(plonkish.0), rng);
let halo2_setup = plonkish.get_halo2_setup(8, rng, 7);

let rng = BlockRng::new(DummyRng {});
let witness = plonkish.1.unwrap().generate(7);

let (proof, instance) = halo2_prove(
&params,
pk,
rng,
cs,
HashMap::from([(chiquito_halo2.ir_id, witness)]),
&vec![chiquito_halo2],
);

let result = halo2_verify(proof, params, vk, instance);

let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand Down
32 changes: 11 additions & 21 deletions examples/fibo_with_padding.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use std::{collections::HashMap, hash::Hash};
use std::hash::Hash;

use chiquito::{
field::Field,
frontend::dsl::{circuit, trace::DSLTraceGenerator}, /* main function for constructing an AST
* circuit */
plonkish::{
backend::halo2::{get_halo2_setup, halo2_prove, halo2_verify, ChiquitoHalo2, DummyRng},
backend::halo2::{halo2_verify, DummyRng, PlonkishHalo2},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
config,
step_selector::SimpleStepSelectorBuilder,
PlonkishCompilationResult,
},
ir::{assignments::AssignmentGenerator, Circuit},
}, /* compiles to
* Chiquito Halo2
* backend,
Expand All @@ -28,10 +28,9 @@ use rand_chacha::rand_core::block::BlockRng;
// This example file extends the rust example file 'fibonacci.rs',
// describing usage of multiple steptypes, padding, and exposing signals.

type AssignGen<F> = AssignmentGenerator<F, DSLTraceGenerator<F, u32>>;

// the main circuit function
fn fibo_circuit<F: Field + From<u64> + Hash>() -> (Circuit<F>, Option<AssignGen<F>>)
fn fibo_circuit<F: Field + From<u64> + Hash>(
) -> PlonkishCompilationResult<F, DSLTraceGenerator<F, u32>>
// u32 is for external input that indicates the number of fibnoacci iterations
{
use chiquito::{
Expand Down Expand Up @@ -206,25 +205,16 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> (Circuit<F>, Option<AssignGen<

// standard main function for a Halo2 circuit
fn main() {
let (chiquito, wit_gen) = fibo_circuit::<Fr>();
let plonkish_compilation_result = fibo_circuit::<Fr>();
let rng = BlockRng::new(DummyRng {});

let (cs, params, vk, pk, chiquito_halo2) =
get_halo2_setup(7, ChiquitoHalo2::new(chiquito), rng);
let halo2_setup = plonkish_compilation_result.get_halo2_setup(7, rng, 7);

let rng = BlockRng::new(DummyRng {});
let witness = wit_gen.unwrap().generate(7);

let (proof, instance) = halo2_prove(
&params,
pk,
rng,
cs,
HashMap::from([(chiquito_halo2.ir_id, witness)]),
&vec![chiquito_halo2],
);

let result = halo2_verify(proof, params, vk, instance);

let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("{:#?}", result);

Expand Down
49 changes: 24 additions & 25 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use std::{collections::HashMap, hash::Hash};
use std::hash::Hash;

use chiquito::{
field::Field,
frontend::dsl::{circuit, trace::DSLTraceGenerator}, /* main function for constructing an AST
* circuit */
plonkish::{
backend::{
halo2::{get_halo2_setup, halo2_prove, halo2_verify, ChiquitoHalo2, DummyRng},
halo2::{halo2_verify, DummyRng, PlonkishHalo2},
hyperplonk::ChiquitoHyperPlonkCircuit,
},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
config,
step_selector::SimpleStepSelectorBuilder,
PlonkishCompilationResult,
},
ir::{assignments::AssignmentGenerator, Circuit},
}, /* compiles to
* Chiquito Halo2
* backend,
Expand All @@ -36,8 +36,7 @@ use rand_chacha::rand_core::block::BlockRng;
// 3. two witness generation arguments both of u64 type, i.e. (u64, u64)

type FiboReturn<F> = (
Circuit<F>,
Option<AssignmentGenerator<F>>,
PlonkishCompilationResult<F, DSLTraceGenerator<F>>,
SBPIR<F, DSLTraceGenerator<F>>,
);

Expand Down Expand Up @@ -125,34 +124,25 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> FiboReturn<F> {
&fibo,
);

(compiled.0, compiled.1, fibo)
(compiled, fibo)
}

// After compiling Chiquito AST to an IR, it is further parsed by a Chiquito Halo2 backend and
// integrated into a Halo2 circuit, which is done by the boilerplate code below.

// standard main function for a Halo2 circuit
fn main() {
let (chiquito, wit_gen, _) = fibo_circuit::<Fr>();
let (chiquito, _) = fibo_circuit::<Fr>();

let rng = BlockRng::new(DummyRng {});

let (cs, params, vk, pk, chiquito_halo2) =
get_halo2_setup(7, ChiquitoHalo2::new(chiquito), rng);
let halo2_setup = chiquito.get_halo2_setup(7, rng, ());

let rng = BlockRng::new(DummyRng {});
let witness = wit_gen.unwrap().generate(());

let (proof, instance) = halo2_prove(
&params,
pk,
rng,
cs,
HashMap::from([(chiquito_halo2.ir_id, witness)]),
&vec![chiquito_halo2],
);

let result = halo2_verify(proof, params, vk, instance);
let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand All @@ -168,11 +158,15 @@ fn main() {
pcs::{multilinear, univariate},
};
// get Chiquito ir
let (circuit, assignment_generator, _) = fibo_circuit::<hpFr>();
let (plonkish_compilation_result, _) = fibo_circuit::<hpFr>();
// get assignments
let assignments = assignment_generator.unwrap().generate(());
let assignments = plonkish_compilation_result
.assignment_generator
.unwrap()
.generate(());
// get hyperplonk circuit
let mut hyperplonk_circuit = ChiquitoHyperPlonkCircuit::new(4, circuit);
let mut hyperplonk_circuit =
ChiquitoHyperPlonkCircuit::new(4, plonkish_compilation_result.circuit);
hyperplonk_circuit.set_assignment(assignments);

type GeminiKzg = multilinear::Gemini<univariate::UnivariateKzg<Bn256>>;
Expand All @@ -182,10 +176,15 @@ fn main() {
// pil boilerplate
use chiquito::pil::backend::powdr_pil::chiquito2Pil;

let (_, wit_gen, circuit) = fibo_circuit::<hpFr>();
let (plonkish_compilation_result, circuit) = fibo_circuit::<hpFr>();
let pil = chiquito2Pil(
circuit,
Some(wit_gen.unwrap().generate_trace_witness(())),
Some(
plonkish_compilation_result
.assignment_generator
.unwrap()
.generate_trace_witness(()),
),
String::from("FiboCircuit"),
);
print!("{}", pil);
Expand Down
12 changes: 7 additions & 5 deletions examples/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use chiquito::{
},
plonkish::{
backend::halo2::{
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_prove, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng,
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng, Halo2Setup,
},
compiler::{
cell_manager::{MaxWidthCellManager, SingleRowCellManager},
Expand Down Expand Up @@ -2265,11 +2265,13 @@ fn main() {

let (cs, params, vk, pk) = get_super_circuit_halo2_setup(9, &mut circuit, rng);

let rng = BlockRng::new(DummyRng {});
let witness = super_circuit.get_mapping().generate(circuit_param);
let (proof, instance) = halo2_prove(&params, pk, rng, cs, witness, &circuit.sub_circuits);
let halo2_setup = Halo2Setup::new(cs, params, vk, pk, circuit.sub_circuits, witness);

let rng = BlockRng::new(DummyRng {});
let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, params, vk, instance);
let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand Down
13 changes: 7 additions & 6 deletions examples/mimc7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use chiquito::{
frontend::dsl::{lb::LookupTable, super_circuit, trace::DSLTraceGenerator, CircuitContext},
plonkish::{
backend::halo2::{
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_prove, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng,
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng, Halo2Setup,
},
compiler::{
cell_manager::SingleRowCellManager, config, step_selector::SimpleStepSelectorBuilder,
Expand Down Expand Up @@ -211,12 +211,13 @@ fn main() {

let (cs, params, vk, pk) = get_super_circuit_halo2_setup(10, &mut circuit, rng);

let rng = BlockRng::new(DummyRng {});

let witness = super_circuit.get_mapping().generate((x_in_value, k_value));
let (proof, instance) = halo2_prove(&params, pk, rng, cs, witness, &circuit.sub_circuits);
let halo2_setup = Halo2Setup::new(cs, params, vk, pk, circuit.sub_circuits, witness);

let rng = BlockRng::new(DummyRng {});
let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, params, vk, instance);
let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand Down
9 changes: 5 additions & 4 deletions examples/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use chiquito::{
frontend::dsl::{lb::LookupTable, super_circuit, trace::DSLTraceGenerator, CircuitContext},
plonkish::{
backend::halo2::{
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_prove, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng,
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_verify,
ChiquitoHalo2SuperCircuit, DummyRng, Halo2Setup,
},
compiler::{
cell_manager::{MaxWidthCellManager, SingleRowCellManager},
Expand Down Expand Up @@ -712,11 +712,12 @@ fn main() {
let rng = BlockRng::new(DummyRng {});

let (cs, params, vk, pk) = get_super_circuit_halo2_setup(12, &mut circuit, rng);
let halo2_setup = Halo2Setup::new(cs, params, vk, pk, circuit.sub_circuits, witness);

let rng = BlockRng::new(DummyRng {});
let (proof, instance) = halo2_prove(&params, pk, rng, cs, witness, &circuit.sub_circuits);
let (proof, instance) = halo2_setup.generate_proof(rng);

let result = halo2_verify(proof, params, vk, instance);
let result = halo2_verify(proof, halo2_setup.params, halo2_setup.vk, instance);

println!("result = {:#?}", result);

Expand Down
7 changes: 2 additions & 5 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
ast::{debug_sym_factory::DebugSymRefFactory, tl::TLDecl, Identifiable, Identifier},
lang::TLDeclsParser,
},
plonkish,
plonkish::{self, compiler::PlonkishCompilationResult},
poly::{self, mielim::mi_elimination, reduce::reduce_degree, Expr},
sbpir::{query::Queriable, InternalSignal, SBPIR},
wit_gen::{NullTraceGenerator, SymbolSignalMapping, TraceGenerator},
Expand Down Expand Up @@ -41,10 +41,7 @@ impl<F: Field + Hash> CompilerResult<F> {
>(
&self,
config: plonkish::compiler::CompilerConfig<CM, SSB>,
) -> (
crate::plonkish::ir::Circuit<F>,
Option<plonkish::ir::assignments::AssignmentGenerator<F, InterpreterTraceGenerator>>,
) {
) -> PlonkishCompilationResult<F, InterpreterTraceGenerator> {
plonkish::compiler::compile(config, &self.circuit)
}
}
Expand Down
Loading

0 comments on commit ba930c7

Please sign in to comment.