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

Commit

Permalink
Merge branch 'chiquito-2024' into 280-refactoring-compiler-to-return-…
Browse files Browse the repository at this point in the history
…new-structure-for-several-machines
  • Loading branch information
alxkzmn committed Aug 6, 2024
2 parents e61318f + 7086456 commit 96b8608
Show file tree
Hide file tree
Showing 13 changed files with 911 additions and 330 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ rand_chacha = "0.3"

[build-dependencies]
lalrpop = "0.20.0"

[features]
debug_pil = []
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);
}
}
}
62 changes: 27 additions & 35 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chiquito::{
* circuit */
plonkish::{
backend::{
halo2::{halo2_verify, Halo2Provable},
halo2_legacy::{chiquito2Halo2, ChiquitoHalo2Circuit},
hyperplonk::ChiquitoHyperPlonkCircuit,
},
compiler::{
Expand All @@ -26,7 +26,7 @@ use chiquito::{
poly::ToField,
sbpir::SBPIRLegacy,
};
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 Down Expand Up @@ -97,12 +97,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 Down Expand Up @@ -131,60 +131,52 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> FiboReturn<F> {

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

let params_path = "examples/ptau/hermez-raw-11";

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

let (proof, instance) =
halo2_prover.generate_proof(chiquito.assignment_generator.unwrap().generate(()));
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 = {:#?}", result);
println!("{:#?}", 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 (plonkish, _) = fibo_circuit::<Fr>();
// get assignments
let assignments = plonkish_compilation_result
.assignment_generator
.unwrap()
.generate(());
let assignments = plonkish.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, plonkish.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 (plonkish, circuit) = fibo_circuit::<Fr>();
let pil = chiquito2Pil(
circuit,
Some(
plonkish_compilation_result
plonkish
.assignment_generator
.unwrap()
.generate_trace_witness(()),
Expand Down
Loading

0 comments on commit 96b8608

Please sign in to comment.