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

Taikquito experiment #80

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
79 changes: 79 additions & 0 deletions examples/taikquito_fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use halo2_proofs::dev::MockProver;
use halo2curves::bn256::Fr;

use chiquito::{
ast::ToField,
circuit,
plonkish::{
backend::halo2::{chiquito2Halo2, ChiquitoHalo2Circuit},
compiler::{cell_manager::SingleRowCellManager, step_selector::SimpleStepSelectorBuilder},
ir::{assignments::AssignmentGenerator, Circuit},
},
};

fn fibo_circuit() -> (Circuit<Fr>, Option<AssignmentGenerator<Fr, ()>>) {
let fibo = circuit!(fibonacci, {
let a = forward!(a);
let b = forward!("b");

let fibo_step = step_type_def!(fibo_step {
let c = internal!(c);

setup!({
require!(a + b => c);

require!(transition b => a.next());

require!(transition c => b.next());
});

wg!(a_value: u32, b_value: u32 {
assign!(a => a_value.field());
assign!(b => b_value.field());
assign!(c => (a_value + b_value).field());
})
});

pragma_num_steps!(11);

trace!({
add!(&fibo_step, (1, 1));
let mut a = 1;
let mut b = 2;

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

let prev_a = a;
a = b;
b += prev_a;
}
});
});

println!("=== AST ===\n{:#?}", fibo);

chiquito::plonkish::compiler::compile(
chiquito::plonkish::compiler::config(SingleRowCellManager {}, SimpleStepSelectorBuilder {}),
&fibo,
)
}

fn main() {
let (chiquito, wit_gen) = fibo_circuit();
println!("=== IR ===\n{:#?}", chiquito);
let compiled = chiquito2Halo2(chiquito);
let circuit = ChiquitoHalo2Circuit::new(compiled, wit_gen.map(|g| g.generate(())));

let prover = MockProver::<Fr>::run(7, &circuit, Vec::new()).unwrap();

let result = prover.verify_par();

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

if let Err(failures) = &result {
for failure in failures.iter() {
println!("{}", failure);
}
}
}
13 changes: 11 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ pub struct Circuit<F, TraceArgs> {
pub trace: Option<Rc<Trace<F, TraceArgs>>>,
pub fixed_gen: Option<Rc<FixedGen<F>>>,

pub num_steps: usize,

pub first_step: Option<StepTypeUUID>,
pub last_step: Option<StepTypeUUID>,
pub num_steps: usize,
pub q_enable: bool,

pub id: UUID,
Expand All @@ -43,8 +44,16 @@ impl<F: Debug, TraceArgs: Debug> Debug for Circuit<F, TraceArgs> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Circuit")
.field("forward_signals", &self.forward_signals)
.field("shared_signals", &self.shared_signals)
.field("fixed_signals", &self.fixed_signals)
.field("halo2_advice", &self.halo2_advice)
.field("halo2_fixed", &self.halo2_fixed)
.field("exposed", &self.exposed)
.field("step_types", &self.step_types)
.field("num_steps", &self.num_steps)
.field("first_step", &self.first_step)
.field("last_step", &self.last_step)
.field("q_enable", &self.q_enable)
.field("annotations", &self.annotations)
.finish()
}
Expand Down Expand Up @@ -450,7 +459,7 @@ impl FixedSignal {
}
}

#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub enum ExposeOffset {
First,
Last,
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ where
pub mod cb;
pub mod lb;
pub mod sc;
#[macro_use]
pub mod taikquito;

#[cfg(test)]
mod tests {
Expand Down
135 changes: 135 additions & 0 deletions src/frontend/dsl/taikquito.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#[macro_export]
macro_rules! circuit {
($id:ident, $content:block) => {{
use halo2_proofs::halo2curves::bn256::Fr;

chiquito::frontend::dsl::circuit::<Fr, (), _>(stringify!($id), |ctx| {
use $crate::circuit_context;
circuit_context!(ctx);

$content
})
}};
}

#[macro_export]
macro_rules! circuit_context {
($ctx: expr) => {
macro_rules! forward {
($id_forward:ident) => {
$ctx.forward(stringify!($id_forward))
};

($id_forward:literal) => {
$ctx.forward($id_forward)
};
};

macro_rules! step_type_def {
($id_step: ident $step_def_content: block) => {
$ctx.step_type_def(stringify!($id_step), |ctx| {
use $crate::step_type_context;
step_type_context!(ctx, $);

$step_def_content
})
};

($id_step: literal $step_def_content: block) => {
$ctx.step_type_def($id_step, |ctx| {
use $crate::step_type_context;
step_type_context!(ctx, $);

$step_def_content
})
};
};

macro_rules! pragma_num_steps {
($num_steps:expr) => {
$ctx.pragma_num_steps($num_steps);
};
}

macro_rules! trace {
($trace_content:block) => {
$ctx.trace(move |ctx, _| {
use $crate::circuit_trace_context;
circuit_trace_context!(ctx);

$trace_content
})
};
}
};
}

#[macro_export]
macro_rules! step_type_context {
($ctx: expr, $d:tt) => {
macro_rules! internal {
($id_internal:ident) => {
$ctx.internal(stringify!($id_internal))
};
}

macro_rules! setup {
($setup_content: block) => {
$ctx.setup(|ctx| {
use $crate::step_type_setup_context;
step_type_setup_context!(ctx);

$setup_content
});
};
}

macro_rules! wg {
($d($args_id: ident : $args_ty:ty),+ $wg_content: block) => {
$ctx.wg(move |ctx, ($d($args_id),*): ($d($args_ty),*)| {
use $crate::step_type_wg_context;
step_type_wg_context!(ctx);

$wg_content
})
};
}
};
}

#[macro_export]
macro_rules! step_type_setup_context {
($ctx: expr) => {
macro_rules! require {
($lhs:expr => $rhs:expr) => {{
$ctx.constr($crate::frontend::dsl::cb::eq($lhs, $rhs));
}};

(transition $lhs:expr => $rhs:expr) => {{
$ctx.transition($crate::frontend::dsl::cb::eq($lhs, $rhs));
}};
}
};
}

#[macro_export]
macro_rules! step_type_wg_context {
($ctx: expr) => {
macro_rules! assign {
($signal:expr => $value:expr) => {
$ctx.assign($signal, $value)
};
}
};
}

#[macro_export]
macro_rules! circuit_trace_context {
($ctx: expr) => {
macro_rules! add {
($step_type: expr, $args: expr) => {
$ctx.add($step_type, $args);
};
};
};
}
1 change: 1 addition & 0 deletions src/frontend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[macro_use]
pub mod dsl;
pub mod pychiquito;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod ast;
#[macro_use]
pub mod frontend;
pub mod plonkish;
pub mod stdlib;
Expand Down
12 changes: 1 addition & 11 deletions src/plonkish/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use self::assignments::Assignments;
pub mod assignments;
pub mod sc;

#[derive(Clone, Default)]
#[derive(Debug, Clone, Default)]
pub struct Circuit<F> {
pub columns: Vec<Column>,
pub exposed: Vec<(Column, i32)>,
Expand All @@ -26,16 +26,6 @@ pub struct Circuit<F> {
pub ast_id: UUID,
}

impl<F: Debug> Debug for Circuit<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Circuit")
.field("columns", &self.columns)
.field("polys", &self.polys)
.field("lookups", &self.lookups)
.finish()
}
}

#[derive(Clone, Debug, Hash)]
pub enum ColumnType {
Advice,
Expand Down
Loading