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

Commit

Permalink
Revert halo2 legacy compilation to pre-middleware version
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkzmn committed Aug 5, 2024
1 parent a142e84 commit ed7e16f
Show file tree
Hide file tree
Showing 9 changed files with 638 additions and 176 deletions.
33 changes: 15 additions & 18 deletions examples/blake2f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use chiquito::{
CircuitContext, StepTypeSetupContext, StepTypeWGHandler,
},
plonkish::{
backend::halo2::{halo2_verify, Halo2Provable},
backend::halo2_legacy::{chiquitoSuperCircuit2Halo2, ChiquitoHalo2SuperCircuit},
compiler::{
cell_manager::{MaxWidthCellManager, SingleRowCellManager},
config,
Expand All @@ -18,7 +18,10 @@ use chiquito::{
poly::ToExpr,
sbpir::query::Queriable,
};
use halo2_proofs::halo2curves::{bn256::Fr, group::ff::PrimeField};
use halo2_proofs::{
dev::MockProver,
halo2curves::{bn256::Fr, group::ff::PrimeField},
};
use std::{fmt::Write, hash::Hash};

pub const IV_LEN: usize = 8;
Expand Down Expand Up @@ -1394,7 +1397,8 @@ fn blake2f_super_circuit<F: PrimeField + Hash>() -> SuperCircuit<F, InputValues>
}

fn main() {
let mut super_circuit = blake2f_super_circuit::<Fr>();
let super_circuit = blake2f_super_circuit::<Fr>();
let compiled = chiquitoSuperCircuit2Halo2(&super_circuit);

// h[0] = hex"48c9bdf267e6096a 3ba7ca8485ae67bb 2bf894fe72f36e3c f1361d5f3af54fa5";
// h[1] = hex"d182e6ad7f520e51 1f6c3e2b8c68059b 6bbd41fbabd9831f 79217e1319cde05b";
Expand Down Expand Up @@ -1476,24 +1480,17 @@ fn main() {
f: true, // 8bits
};

let witness = super_circuit.get_mapping().generate(values);

let params_path = "examples/ptau/hermez-raw-11";
let halo2_prover = super_circuit.create_halo2_prover(params_path);
println!("k={}", halo2_prover.get_k());
let circuit =
ChiquitoHalo2SuperCircuit::new(compiled, super_circuit.get_mapping().generate(values));

let (proof, instance) = halo2_prover.generate_proof(witness);

let result = halo2_verify(
proof,
halo2_prover.get_params(),
halo2_prover.get_vk(),
instance,
);
let prover = MockProver::run(9, &circuit, Vec::new()).unwrap();
let result = prover.verify();

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

if let Err(failure) = &result {
println!("{}", failure);
if let Err(failures) = &result {
for failure in failures.iter() {
println!("{}", failure);
}
}
}
53 changes: 15 additions & 38 deletions examples/factorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chiquito::{
frontend::dsl::{circuit, trace::DSLTraceGenerator}, /* main function for constructing an AST
* circuit */
plonkish::{
backend::halo2::{halo2_verify, Halo2Provable},
backend::halo2_legacy::{chiquito2Halo2, ChiquitoHalo2Circuit},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
Expand All @@ -22,7 +22,7 @@ use chiquito::{
* circuit */
poly::ToField,
};
use halo2_proofs::halo2curves::bn256::Fr;
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};

const MAX_FACTORIAL: usize = 10;

Expand Down Expand Up @@ -134,47 +134,24 @@ fn generate<F: Field + From<u64> + Hash>() -> PlonkishCompilationResult<F, DSLTr

// standard main function for a Halo2 circuit
fn main() {
let mut plonkish = generate::<Fr>();
let params_path = "examples/ptau/hermez-raw-11";

let halo2_prover = plonkish.create_halo2_prover(params_path);
println!("k={}", halo2_prover.get_k());

let (proof, instance) =
halo2_prover.generate_proof(plonkish.assignment_generator.unwrap().generate(0));

let result = halo2_verify(
proof,
halo2_prover.get_params(),
halo2_prover.get_vk(),
instance,
let compilation_result = generate::<Fr>();
let compiled = chiquito2Halo2(compilation_result.circuit);
let circuit = ChiquitoHalo2Circuit::new(
compiled,
compilation_result
.assignment_generator
.map(|g| g.generate(0)),
);

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

if let Err(error) = &result {
println!("{}", error);
}

let mut plonkish = generate::<Fr>();
let params_path = "examples/ptau/hermez-raw-11";

let halo2_prover = plonkish.create_halo2_prover(params_path);
println!("k={}", halo2_prover.get_k());
let prover = MockProver::<Fr>::run(10, &circuit, circuit.instance()).unwrap();

let (proof, instance) =
halo2_prover.generate_proof(plonkish.assignment_generator.unwrap().generate(7));

let result = halo2_verify(
proof,
halo2_prover.get_params(),
halo2_prover.get_vk(),
instance,
);
let result = prover.verify();

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

if let Err(error) = &result {
println!("{}", error);
if let Err(failures) = &result {
for failure in failures.iter() {
println!("{}", failure);
}
}
}
33 changes: 16 additions & 17 deletions examples/fibo_with_padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chiquito::{
frontend::dsl::{circuit, trace::DSLTraceGenerator}, /* main function for constructing an AST
* circuit */
plonkish::{
backend::halo2::{halo2_verify, Halo2Provable},
backend::halo2_legacy::{chiquito2Halo2, ChiquitoHalo2Circuit},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
Expand All @@ -22,7 +22,7 @@ use chiquito::{
* circuit */
poly::ToField,
};
use halo2_proofs::halo2curves::bn256::Fr;
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};

// This example file extends the rust example file 'fibonacci.rs',
// describing usage of multiple steptypes, padding, and exposing signals.
Expand Down Expand Up @@ -204,25 +204,24 @@ fn fibo_circuit<F: Field + From<u64> + Hash>(

// standard main function for a Halo2 circuit
fn main() {
let mut plonkish = fibo_circuit::<Fr>();
let params_path = "examples/ptau/hermez-raw-11";

let halo2_prover = plonkish.create_halo2_prover(params_path);
println!("k={}", halo2_prover.get_k());
let compilation_result = fibo_circuit::<Fr>();
let compiled = chiquito2Halo2(compilation_result.circuit);
let circuit = ChiquitoHalo2Circuit::new(
compiled,
compilation_result
.assignment_generator
.map(|g| g.generate(7)),
);

let (proof, instance) =
halo2_prover.generate_proof(plonkish.assignment_generator.unwrap().generate(7));
let prover = MockProver::<Fr>::run(7, &circuit, circuit.instance()).unwrap();

let result = halo2_verify(
proof,
halo2_prover.get_params(),
halo2_prover.get_vk(),
instance,
);
let result = prover.verify();

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

if let Err(failure) = &result {
println!("{}", failure);
if let Err(failures) = &result {
for failure in failures.iter() {
println!("{}", failure);
}
}
}
71 changes: 28 additions & 43 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ use chiquito::{
* circuit */
plonkish::{
backend::{
halo2::{halo2_verify, Halo2Provable},
halo2_legacy::{chiquito2Halo2, ChiquitoHalo2Circuit},
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 @@ -26,7 +26,7 @@ use chiquito::{
poly::ToField,
sbpir::SBPIR,
};
use halo2_proofs::halo2curves::bn256::Fr;
use halo2_proofs::dev::MockProver;

// the main circuit function: returns the compiled IR of a Chiquito circuit
// Generic types F, (), (u64, 64) stand for:
Expand All @@ -35,7 +35,8 @@ use halo2_proofs::halo2curves::bn256::Fr;
// 3. two witness generation arguments both of u64 type, i.e. (u64, u64)

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

Expand Down Expand Up @@ -97,12 +98,12 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> FiboReturn<F> {

ctx.pragma_num_steps(16);

// Trace function is responsible for adding step instantiations defined in `step_type_def`
// function above. Trace function is Turing-complete and allows arbitrary user-defined
// logic for assigning witness values
// trace function is responsible for adding step instantiations defined in step_type_def
// function above trace function is Turing complete and allows arbitrary user
// defined logics for assigning witness values
ctx.trace(move |ctx: _, _| {
// Add function adds a step instantiation to the main circuit and calls witness
// generation function defined in step_type_def. Input values for witness
// add function adds a step instantiation to the main circuit and calls witness
// generation function defined in step_type_def input values for witness
// generation function are (1, 1) in this step instance
ctx.add(&fibo_step, (1, 1));
let mut a = 1;
Expand All @@ -123,72 +124,56 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> FiboReturn<F> {
&fibo,
);

(compiled, fibo)
(compiled.circuit, compiled.assignment_generator, 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 (mut chiquito, _) = fibo_circuit::<Fr>();
let (chiquito, wit_gen, _) = fibo_circuit::<Fr>();
let compiled = chiquito2Halo2(chiquito);
let circuit = ChiquitoHalo2Circuit::new(compiled, wit_gen.map(|g| g.generate(())));

let params_path = "examples/ptau/hermez-raw-11";
let prover = MockProver::<Fr>::run(7, &circuit, circuit.instance()).unwrap();

let halo2_prover = chiquito.create_halo2_prover(params_path);
println!("k={}", halo2_prover.get_k());
let result = prover.verify();

let (proof, instance) =
halo2_prover.generate_proof(chiquito.assignment_generator.unwrap().generate(()));
println!("{:#?}", result);

let result = halo2_verify(
proof,
halo2_prover.get_params(),
halo2_prover.get_vk(),
instance,
);

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

if let Err(error) = &result {
println!("{}", error);
if let Err(failures) = &result {
for failure in failures.iter() {
println!("{}", failure);
}
}

// hyperplonk boilerplate
use hyperplonk_benchmark::proof_system::{bench_plonkish_backend, System};
use plonkish_backend::{
backend,
halo2_curves::bn256::{Bn256, Fr as hpFr},
halo2_curves::bn256::{Bn256, Fr},
pcs::{multilinear, univariate},
};
// get Chiquito ir
let (plonkish_compilation_result, _) = fibo_circuit::<hpFr>();
let (circuit, assignment_generator, _) = fibo_circuit::<Fr>();
// get assignments
let assignments = plonkish_compilation_result
.assignment_generator
.unwrap()
.generate(());
let assignments = assignment_generator.unwrap().generate(());
// get hyperplonk circuit
let mut hyperplonk_circuit =
ChiquitoHyperPlonkCircuit::new(4, plonkish_compilation_result.circuit);
let mut hyperplonk_circuit = ChiquitoHyperPlonkCircuit::new(4, circuit);
hyperplonk_circuit.set_assignment(assignments);

type GeminiKzg = multilinear::Gemini<univariate::UnivariateKzg<Bn256>>;
type HyperPlonk = backend::hyperplonk::HyperPlonk<GeminiKzg>;
bench_plonkish_backend::<HyperPlonk, hpFr>(System::HyperPlonk, 4, &hyperplonk_circuit);
bench_plonkish_backend::<HyperPlonk, Fr>(System::HyperPlonk, 4, &hyperplonk_circuit);

// pil boilerplate
use chiquito::pil::backend::powdr_pil::chiquito2Pil;

let (plonkish_compilation_result, circuit) = fibo_circuit::<hpFr>();
let (_, wit_gen, circuit) = fibo_circuit::<Fr>();
let pil = chiquito2Pil(
circuit,
Some(
plonkish_compilation_result
.assignment_generator
.unwrap()
.generate_trace_witness(()),
),
Some(wit_gen.unwrap().generate_trace_witness(())),
String::from("FiboCircuit"),
);
print!("{}", pil);
Expand Down
Loading

0 comments on commit ed7e16f

Please sign in to comment.