diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 31000a2..680b88a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,13 +10,75 @@ env: CARGO_TERM_COLOR: always jobs: - build: - + skip_check: runs-on: ubuntu-latest + outputs: + should_skip: ${{ steps.skip_check.outputs.should_skip }} + steps: + - id: skip_check + uses: fkirc/skip-duplicate-actions@v5 + with: + cancel_others: 'true' + concurrent_skipping: 'same_content_newer' + paths_ignore: '["**/README.md", "**/Docs**", "**/Appendix.md"]' + build: + needs: [skip_check] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build run: cargo build --verbose - name: Run tests run: cargo test --verbose + + clippy_check: + needs: [skip_check] + name: Clippy Check + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Setup Rust + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + components: clippy + override: true + - name: Run Clippy + run: cargo clippy --all-targets --all-features -- -D warnings + + fmt: + needs: [skip_check] + if: | + github.event.pull_request.draft == false && + (github.event.action == 'ready_for_review' || needs.skip_check.outputs.should_skip != 'true') + name: Rustfmt + timeout-minutes: 30 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + override: false + - name: Cargo cache + uses: actions/cache@v3 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + - name: cargo check + uses: actions-rs/cargo@v1 + with: + command: check + args: --all-features + - run: rustup component add rustfmt + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check diff --git a/examples/bytecode.rs b/examples/bytecode.rs index fa9d34e..bd813fd 100644 --- a/examples/bytecode.rs +++ b/examples/bytecode.rs @@ -27,7 +27,7 @@ impl IsZero { let value: Expr = value.into(); let is_zero_expression = (1.expr() - value.clone()) * value_inv; - ctx.constr(value.clone() * is_zero_expression.clone()); + ctx.constr(value * is_zero_expression.clone()); IsZero { value_inv, @@ -48,37 +48,33 @@ impl IsZero { #[derive(Debug)] struct BytecodeLine { - hash: F, - index: F, - length: F, - is_code: F, - value: F, - push_data_left: F, + _hash: F, + _index: F, + _length: F, + _is_code: F, + _value: F, + _push_data_left: F, } fn main() { - let mut bytecode_circuit = + let bytecode_circuit = circuit::>>, BytecodeLine, _>("bytecode circuit", |ctx| { use chiquito::dsl::cb::*; let length = ctx.forward("length"); let index = ctx.forward("index"); - let hash = ctx.forward("hash"); let is_code = ctx.forward("is_code"); let value = ctx.forward("value"); - let value_rlc = ctx.forward("value_rlc"); let s1 = ctx.step_type("header"); ctx.step_type_def(s1, |ctx| { ctx.constr(isz(index)); ctx.constr(eq(value, length)); - - ctx.wg(|ctx, v| {}) }); let s2 = ctx.step_type("byte"); ctx.step_type_def(s2, |ctx| { let push_data_left = ctx.internal("push_data_left"); - let push_data_size = ctx.internal("push_data_size"); + // let push_data_size = ctx.internal("push_data_size"); let push_data_left_inv = ctx.internal("push_data_left_inv"); let push_data_left_is_zero = IsZero::setup(ctx, push_data_left, push_data_left_inv); @@ -95,12 +91,6 @@ fn main() { push_data_left_is_zero.wg(ctx, Fr::zero()); }); }); - - ctx.trace(|ctx, bytecodes| { - for bytecode in bytecodes { - println!("todo"); - } - }) }); let compiler = Compiler::new(SingleRowCellManager {}, SimpleStepSelectorBuilder {}); diff --git a/examples/example.rs b/examples/example.rs index a195c56..f746d00 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -3,7 +3,7 @@ use chiquito::{ compiler::{ cell_manager::SingleRowCellManager, step_selector::SimpleStepSelectorBuilder, Compiler, }, - dsl::{circuit, cb::lookup}, + dsl::{cb::lookup, circuit}, }; use halo2_proofs::halo2curves::bn256::Fr; @@ -23,7 +23,7 @@ fn main() { ctx.transition(a + 1); ctx.add_lookup(lookup().add(a, b).enable(d).add(d + a, f * c)); - + ctx.wg(move |ctx, _| { ctx.assign(a, 13.field()); ctx.assign(b, 13.field()); diff --git a/examples/fibonacci.rs b/examples/fibonacci.rs index 6504007..f483f4f 100644 --- a/examples/fibonacci.rs +++ b/examples/fibonacci.rs @@ -108,32 +108,21 @@ fn fibo_circuit() -> Circuit { let mut a = 1; let mut b = 2; - for i in 1..10 { - // input values for witness generation function are (a, b) in this step instance + for _i in 1..10 { ctx.add(&fibo_step, (a, b)); let prev_a = a; a = b; - b = prev_a + b; + b += prev_a; } ctx.add(&fibo_last_step, (a, b)); }) }); - - // uncomment for debugging: - // println!("{:#?}", fibo); - - // create a new compiler using a cell manager instance and a selector builder instance + let compiler = Compiler::new(SingleRowCellManager {}, SimpleStepSelectorBuilder {}); - - // compile the circuit into an IR object - let compiled = compiler.compile(&fibo); - // uncomment for debugging: - //println!("{:#?}", compiled); - - compiled + compiler.compile(&fibo) } // After compiling Chiquito AST to an IR, it is further parsed by a Chiquito Halo2 backend and integrated into a Halo2 circuit, diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..b35236f --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,9 @@ +edition = "2021" + +comment_width = 100 +imports_granularity = "Crate" +max_width = 100 +newline_style = "Unix" +normalize_comments = true +reorder_imports = true +wrap_comments = true diff --git a/src/ast.rs b/src/ast.rs index aa983b2..79d89cd 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1,11 +1,12 @@ pub mod expr; -use std::fmt::Debug; -use std::{collections::HashMap, rc::Rc}; +use std::{collections::HashMap, fmt::Debug, rc::Rc}; -use crate::compiler::{FixedGenContext, TraceContext, WitnessGenContext}; -use crate::dsl::StepTypeHandler; -use crate::util::uuid; +use crate::{ + compiler::{FixedGenContext, TraceContext, WitnessGenContext}, + dsl::StepTypeHandler, + util::uuid, +}; pub use expr::*; @@ -256,8 +257,9 @@ impl Default for Lookup { } impl Lookup { - // Function: adds (constraint, expression) to exprs if there's no enabler, OR add (enabler * constraint, expression) to exprs if there's enabler - // Note that constraint_annotation and constraint_expr are passed in as separate parameters, and then reconstructed as Constraint, + // Function: adds (constraint, expression) to exprs if there's no enabler, OR add (enabler * + // constraint, expression) to exprs if there's enabler Note that constraint_annotation and + // constraint_expr are passed in as separate parameters, and then reconstructed as Constraint, // because dsl uses cb::Constraint while ast uses ast::Constraint pub fn add( &mut self, @@ -283,7 +285,8 @@ impl Lookup { } } - // Function: setup the enabler field and multiply all LHS constraints by the enabler if there's no enabler, OR panic if there's an enabler already + // Function: setup the enabler field and multiply all LHS constraints by the enabler if there's + // no enabler, OR panic if there's an enabler already pub fn enable(&mut self, enable_annotation: String, enable_expr: Expr) { let enable = Constraint { annotation: enable_annotation.clone(), @@ -305,7 +308,11 @@ impl Lookup { // Function: helper function for multiplying enabler to constraint fn multiply_constraints(enable: Constraint, constraint: Constraint) -> Constraint { Constraint { - annotation: constraint.annotation.clone(), // annotation only takes the constraint's annotation, because enabler's annotation is already included in the enable function above in the format of "if {enable}" + annotation: constraint.annotation.clone(), /* annotation only takes the constraint's + * annotation, because enabler's + * annotation is already included in the + * enable function above in the format of + * "if {enable}" */ expr: enable.expr * constraint.expr, } } diff --git a/src/ast/expr.rs b/src/ast/expr.rs index 0076b4d..97cfcdf 100644 --- a/src/ast/expr.rs +++ b/src/ast/expr.rs @@ -1,5 +1,7 @@ -use std::fmt::Debug; -use std::ops::{Add, Mul, Neg, Sub}; +use std::{ + fmt::Debug, + ops::{Add, Mul, Neg, Sub}, +}; use halo2_proofs::{arithmetic::Field, plonk::Expression}; diff --git a/src/backend/halo2.rs b/src/backend/halo2.rs index e4b9f21..1ad7ab0 100644 --- a/src/backend/halo2.rs +++ b/src/backend/halo2.rs @@ -19,8 +19,9 @@ use crate::{ }, dsl::StepTypeHandler, ir::{ - Circuit, Column as cColumn, ColumnType::Advice as cAdvice, ColumnType::Fixed as cFixed, - ColumnType::Halo2Advice, ColumnType::Halo2Fixed, PolyExpr, + Circuit, Column as cColumn, + ColumnType::{Advice as cAdvice, Fixed as cFixed, Halo2Advice, Halo2Fixed}, + PolyExpr, }, }; @@ -226,7 +227,9 @@ impl ChiquitoHalo2 ChiquitoHalo2 ChiquitoHalo2