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

Commit

Permalink
HyperPlonk backend (#184)
Browse files Browse the repository at this point in the history
Ready for review @leolara.

I asked Han about the best approach to testing, for which he suggested
exposing functions from his repo, for which I did over my fork of his
repo in its main branch: https://github.com/qwang98/plonkish.

---------

Co-authored-by: Leo Lara <leo@leolara.me>
  • Loading branch information
qwang98 and leolara authored Mar 26, 2024
1 parent d139e1b commit 8365674
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-04-24
toolchain: nightly-2024-02-14
components: clippy
override: true
- name: Run Clippy
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ num-bigint = { version = "0.4", features = ["rand"] }
uuid = { version = "1.4.0", features = ["v1", "rng"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
hyperplonk_benchmark = { git = "https://github.com/qwang98/plonkish.git", branch = "main", package = "benchmark" }
plonkish_backend = { git = "https://github.com/qwang98/plonkish.git", branch = "main", package = "plonkish_backend" }
regex = "1"

[dev-dependencies]
Expand Down
46 changes: 35 additions & 11 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ use std::hash::Hash;
use chiquito::{
field::Field,
frontend::dsl::circuit, // main function for constructing an AST circuit
plonkish::backend::halo2::{chiquito2Halo2, ChiquitoHalo2Circuit}, /* compiles to
plonkish::{
backend::{
halo2::{chiquito2Halo2, ChiquitoHalo2Circuit},
hyperplonk::ChiquitoHyperPlonkCircuit,
},
compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
config,
step_selector::SimpleStepSelectorBuilder,
},
ir::{assignments::AssignmentGenerator, Circuit},
}, /* compiles to
* Chiquito Halo2
* backend,
* which can be
* integrated into
* Halo2
* circuit */
plonkish::compiler::{
cell_manager::SingleRowCellManager, // input for constructing the compiler
compile, // input for constructing the compiler
config,
step_selector::SimpleStepSelectorBuilder,
},
plonkish::ir::{assignments::AssignmentGenerator, Circuit}, // compiled circuit type
poly::ToField,
sbpir::SBPIR,
};
use halo2_proofs::{dev::MockProver, 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 @@ -86,7 +91,7 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> FiboReturn<F> {
})
});

ctx.pragma_num_steps(11);
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
Expand All @@ -99,7 +104,7 @@ fn fibo_circuit<F: Field + From<u64> + Hash>() -> FiboReturn<F> {
let mut a = 1;
let mut b = 2;

for _i in 1..11 {
for _i in 1..16 {
ctx.add(&fibo_step, (a, b));

let prev_a = a;
Expand Down Expand Up @@ -169,6 +174,25 @@ fn main() {
}
}

// hyperplonk boilerplate
use hyperplonk_benchmark::proof_system::{bench_plonkish_backend, System};
use plonkish_backend::{
backend,
halo2_curves::bn256::{Bn256, Fr},
pcs::{multilinear, univariate},
};
// get Chiquito ir
let (circuit, assignment_generator, _) = fibo_circuit::<Fr>();
// get assignments
let assignments = assignment_generator.unwrap().generate(());
// get hyperplonk 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, Fr>(System::HyperPlonk, 4, &hyperplonk_circuit);

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

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2023-04-24
nightly-2024-02-14
2 changes: 1 addition & 1 deletion src/pil/backend/powdr_pil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn convert_to_pil_expr_string<F: Debug + Clone>(expr: PILExpr<F, PILQuery>) -> S
expr_string += " * ";
}
}
format!("{}", expr_string)
expr_string.to_string()
}
PILExpr::Neg(neg) => format!("(-{})", convert_to_pil_expr_string(*neg)),
PILExpr::Pow(pow, power) => {
Expand Down
23 changes: 2 additions & 21 deletions src/plonkish/backend/halo2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,6 @@ impl<F: Field + From<u64> + Hash> ChiquitoHalo2<F> {
Ok(())
}

fn instance(&self, witness: &Assignments<F>) -> Vec<F> {
let mut instance_values = Vec::new();
for (column, rotation) in &self.circuit.exposed {
let values = witness
.get(column)
.unwrap_or_else(|| panic!("exposed column not found: {}", column.annotation));

if let Some(value) = values.get(*rotation as usize) {
instance_values.push(*value);
} else {
panic!(
"assignment index out of bounds for column: {}",
column.annotation
);
}
}
instance_values
}

fn annotate_circuit(&self, region: &mut Region<F>) {
for column in self.circuit.columns.iter() {
match column.ctype {
Expand Down Expand Up @@ -379,7 +360,7 @@ impl<F: Field + From<u64> + Hash> ChiquitoHalo2Circuit<F> {
pub fn instance(&self) -> Vec<Vec<F>> {
if !self.compiled.circuit.exposed.is_empty() {
if let Some(witness) = &self.witness {
return vec![self.compiled.instance(witness)];
return vec![self.compiled.circuit.instance(witness)];
}
}
Vec::new()
Expand Down Expand Up @@ -444,7 +425,7 @@ impl<F: Field + From<u64> + Hash> ChiquitoHalo2SuperCircuit<F> {

for sub_circuit in &self.sub_circuits {
if !sub_circuit.circuit.exposed.is_empty() {
let instance_values = sub_circuit.instance(
let instance_values = sub_circuit.circuit.instance(
self.witness
.get(&sub_circuit.ir_id)
.expect("No matching witness found for given UUID."),
Expand Down
Loading

0 comments on commit 8365674

Please sign in to comment.