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

Compile to Halo2 middleware #263

Merged
merged 16 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ authors = ["Leo Lara <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[patch.crates-io]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v0.3.0" }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", default-features = false, rev = "bc857a7" }

[patch."https://github.com/scroll-tech/halo2.git"]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v0.3.0" }

halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", default-features = false, rev = "bc857a7" }

[dependencies]
pyo3 = { version = "0.19.1", features = ["extension-module"] }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", features = [
"circuit-params",
"derive_serde",
], tag = "v0.3.0" }

polyexen = { git = "https://github.com/Dhole/polyexen.git", rev = "16a85c5411f804dc49bbf373d24ff9eedadedfbe" }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", default-features = false, features = [ "circuit-params", "derive_serde"], rev = "bc857a7"}

halo2_middleware = { git = "https://github.com/privacy-scaling-explorations/halo2.git", rev = "bc857a7" }
halo2_frontend = { git = "https://github.com/privacy-scaling-explorations/halo2.git", default-features = false, features = ["bits"], rev = "bc857a7" }
halo2_backend = { git = "https://github.com/privacy-scaling-explorations/halo2.git", rev = "bc857a7" }

num-bigint = { version = "0.4", features = ["rand"] }
uuid = { version = "1.4.0", features = ["v1", "rng"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
40 changes: 27 additions & 13 deletions examples/blake2f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use chiquito::{
CircuitContext, StepTypeSetupContext, StepTypeWGHandler,
},
plonkish::{
backend::halo2::{chiquitoSuperCircuit2Halo2, ChiquitoHalo2SuperCircuit},
backend::halo2::{
chiquitoSuperCircuit2Halo2, get_super_circuit_halo2_setup, halo2_prove, halo2_verify,
ChiquitoHalo2SuperCircuit, OneNg,
},
compiler::{
cell_manager::{MaxWidthCellManager, SingleRowCellManager},
config,
Expand All @@ -18,10 +21,8 @@ use chiquito::{
poly::ToExpr,
sbpir::query::Queriable,
};
use halo2_proofs::{
dev::MockProver,
halo2curves::{bn256::Fr, group::ff::PrimeField},
};
use halo2_proofs::halo2curves::{bn256::Fr, group::ff::PrimeField};
use rand_chacha::rand_core::block::BlockRng;
use std::{fmt::Write, hash::Hash};

pub const IV_LEN: usize = 8;
Expand Down Expand Up @@ -1480,17 +1481,30 @@ fn main() {
f: true, // 8bits
};

let circuit =
ChiquitoHalo2SuperCircuit::new(compiled, super_circuit.get_mapping().generate(values));
let witness = super_circuit.get_mapping().generate(values);
let mut circuit = ChiquitoHalo2SuperCircuit::new(compiled, witness.clone());

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

let prover = MockProver::run(9, &circuit, Vec::new()).unwrap();
let result = prover.verify();
let (cs, params, vk, pk) = get_super_circuit_halo2_setup(9, &mut circuit, rng);

let rng = BlockRng::new(OneNg {});
let instance = circuit.instance();
let proof = halo2_prove(
&params,
pk,
rng,
cs,
witness.values().collect(),
circuit.sub_circuits,
instance.clone(),
);

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

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

if let Err(failures) = &result {
for failure in failures.iter() {
println!("{}", failure);
}
if let Err(failure) = &result {
println!("{}", failure);
}
}
96 changes: 56 additions & 40 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::{chiquito2Halo2, ChiquitoHalo2Circuit},
backend::halo2::{get_halo2_setup, halo2_prove, halo2_verify, OneNg},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
Expand All @@ -22,7 +22,8 @@ use chiquito::{
* circuit */
poly::ToField,
};
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use halo2_proofs::halo2curves::bn256::Fr;
use rand_chacha::rand_core::block::BlockRng;

const MAX_FACTORIAL: usize = 10;

Expand Down Expand Up @@ -134,50 +135,65 @@ fn generate<F: Field + From<u64> + Hash>() -> (Circuit<F>, Option<AssignGen<F>>)

// standard main function for a Halo2 circuit
fn main() {
let (chiquito, wit_gen) = generate::<Fr>();
let compiled = chiquito2Halo2(chiquito);
let circuit = ChiquitoHalo2Circuit::new(compiled, wit_gen.map(|g| g.generate(0)));

let prover = MockProver::<Fr>::run(10, &circuit, circuit.instance()).unwrap();

let result = prover.verify();
let plonkish = generate::<Fr>();
let rng = BlockRng::new(OneNg {});

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

let rng = BlockRng::new(OneNg {});
let witness = plonkish.1.unwrap().generate(0);
let instances = &chiquito_halo2.instance(&witness);
let instance = if instances.is_empty() {
vec![]
} else {
vec![instances.clone()]
};
let proof = halo2_prove(
&params,
pk,
rng,
cs,
vec![&witness],
vec![chiquito_halo2],
instance.clone(),
);

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

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

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

// plaf boilerplate
use chiquito::plonkish::backend::plaf::chiquito2Plaf;
use polyexen::plaf::backends::halo2::PlafH2Circuit;

// get Chiquito ir
let (circuit, wit_gen) = generate::<Fr>();
// get Plaf
let (plaf, plaf_wit_gen) = chiquito2Plaf(circuit, 8, false);
let wit = plaf_wit_gen.generate(wit_gen.map(|v| v.generate(7)));

// debug only: print witness
// println!("{}", polyexen::plaf::WitnessDisplayCSV(&wit));
let plonkish = generate::<Fr>();
let rng = BlockRng::new(OneNg {});

let (cs, params, vk, pk, chiquito_halo2) = get_halo2_setup(8, plonkish.0, rng);

let rng = BlockRng::new(OneNg {});
let witness = plonkish.1.unwrap().generate(7);
let instances = &chiquito_halo2.instance(&witness);
let instance = if instances.is_empty() {
vec![]
} else {
vec![instances.clone()]
};
let proof = halo2_prove(
&params,
pk,
rng,
cs,
vec![&witness],
vec![chiquito_halo2],
instance.clone(),
);

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

// get Plaf halo2 circuit from Plaf's halo2 backend
// this is just a proof of concept, because Plaf only has backend for halo2
// this is unnecessary because Chiquito has a halo2 backend already
let plaf_circuit = PlafH2Circuit { plaf, wit };

// same as halo2 boilerplate above
let prover_plaf = MockProver::<Fr>::run(8, &plaf_circuit, Vec::new()).unwrap();

let result_plaf = prover_plaf.verify();

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

if let Err(failures) = &result_plaf {
for failure in failures.iter() {
println!("{}", failure);
}
if let Err(error) = &result {
println!("{}", error);
}
}
67 changes: 26 additions & 41 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::{chiquito2Halo2, ChiquitoHalo2Circuit},
backend::halo2::{get_halo2_setup, halo2_prove, halo2_verify, OneNg},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
Expand All @@ -22,7 +22,8 @@ use chiquito::{
* circuit */
poly::ToField,
};
use halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use halo2_proofs::halo2curves::bn256::Fr;
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.
Expand Down Expand Up @@ -206,49 +207,33 @@ 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 compiled = chiquito2Halo2(chiquito);
let circuit = ChiquitoHalo2Circuit::new(compiled, wit_gen.map(|g| g.generate(7)));
let rng = BlockRng::new(OneNg {});

let prover = MockProver::<Fr>::run(7, &circuit, circuit.instance()).unwrap();
let (cs, params, vk, pk, chiquito_halo2) = get_halo2_setup(7, chiquito, rng);

let result = prover.verify();
let rng = BlockRng::new(OneNg {});
let witness = &wit_gen.unwrap().generate(7);
let instances = &chiquito_halo2.instance(witness);
let instance = if instances.is_empty() {
vec![]
} else {
vec![instances.clone()]
};
let proof = halo2_prove(
&params,
pk,
rng,
cs,
vec![&witness],
vec![chiquito_halo2],
instance.clone(),
);

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

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

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

// plaf boilerplate
use chiquito::plonkish::backend::plaf::chiquito2Plaf;
use polyexen::plaf::{backends::halo2::PlafH2Circuit, WitnessDisplayCSV};

// get Chiquito ir
let (circuit, wit_gen) = fibo_circuit::<Fr>();
// get Plaf
let (plaf, plaf_wit_gen) = chiquito2Plaf(circuit, 8, false);
let wit = plaf_wit_gen.generate(wit_gen.map(|v| v.generate(7)));

// debug only: print witness
println!("{}", WitnessDisplayCSV(&wit));

// get Plaf halo2 circuit from Plaf's halo2 backend
// this is just a proof of concept, because Plaf only has backend for halo2
// this is unnecessary because Chiquito has a halo2 backend already
let plaf_circuit = PlafH2Circuit { plaf, wit };

// same as halo2 boilerplate above
let prover_plaf = MockProver::<Fr>::run(8, &plaf_circuit, plaf_circuit.instance()).unwrap();

let result_plaf = prover_plaf.verify();

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

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