Skip to content

Commit

Permalink
chore: share advice columns between MerkleSumTreeChip and `Overflow…
Browse files Browse the repository at this point in the history
…Chip`
  • Loading branch information
enricobottazzi committed Jun 20, 2023
1 parent 8dae130 commit 8e8b9fd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
Binary file modified zk_prover/prints/merkle-sum-tree-layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 17 additions & 13 deletions zk_prover/src/chips/merkle_sum_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use crate::chips::overflow::overflow_check::{OverflowCheckConfig, OverflowChip};
use crate::chips::poseidon::hash::{PoseidonChip, PoseidonConfig};
use crate::chips::poseidon::poseidon_spec::PoseidonSpec;
use crate::merkle_sum_tree::L_NODE;
use gadgets::less_than::{LtChip, LtConfig, LtInstruction};
use halo2_proofs::circuit::{AssignedCell, Layouter, Value};
use halo2_proofs::halo2curves::bn256::Fr as Fp;
use halo2_proofs::plonk::{
Advice, Column, ConstraintSystem, Error, Expression, Instance, Selector,
};
use halo2_proofs::poly::Rotation;

const ACC_COLS: usize = 31;
const MAX_BITS: u8 = 8;
const ACC_COLS: usize = 5;
const MAX_BITS: u8 = 5;
const WIDTH: usize = 7;
const RATE: usize = 6;
const L: usize = L_NODE;
Expand Down Expand Up @@ -100,8 +99,15 @@ impl<const MST_WIDTH: usize, const N_ASSETS: usize> MerkleSumTreeChip<MST_WIDTH,
.collect::<Vec<_>>()
});

// fill decomposed_values with the values [0, ACC_COLS) of the advice vector
let mut decomposed_values = [advice[0]; ACC_COLS];
decomposed_values[..ACC_COLS].copy_from_slice(&advice[..ACC_COLS]);

// fill a with the value at index ACC_COLS of the advice vector
let a = advice[ACC_COLS];

// configure overflow chip
let overflow_check_config = OverflowChip::configure(meta);
let overflow_check_config = OverflowChip::configure(meta, a, decomposed_values);

// Enforces that input_left_balance[i] + input_right_balance[i] = computed_sum[i]
meta.create_gate("sum constraint", |meta| {
Expand All @@ -116,12 +122,12 @@ impl<const MST_WIDTH: usize, const N_ASSETS: usize> MerkleSumTreeChip<MST_WIDTH,
.collect::<Vec<_>>()
});

// Slice a advice into a vector of WIDTH + 1 columns
let hash_inputs = (0..WIDTH)
.map(|i| advice[i])
.collect::<Vec<Column<Advice>>>();
// fill decomposed_values with the values [0, WIDTH) of the advice vector
let mut hash_inputs = [advice[0]; WIDTH];
hash_inputs[..WIDTH].copy_from_slice(&advice[..WIDTH]);

// fill partial_sbox with the value at index WIDTH of the advice vector

// extract a further instance column partial_sbox at index WIDTH + 1
let partial_sbox = advice[WIDTH];

let poseidon_config = PoseidonChip::<PoseidonSpec, WIDTH, RATE, L>::configure(
Expand All @@ -130,7 +136,7 @@ impl<const MST_WIDTH: usize, const N_ASSETS: usize> MerkleSumTreeChip<MST_WIDTH,
partial_sbox,
);

let config = MerkleSumTreeConfig::<MST_WIDTH> {
MerkleSumTreeConfig::<MST_WIDTH> {
advice,
bool_selector,
swap_selector,
Expand All @@ -139,9 +145,7 @@ impl<const MST_WIDTH: usize, const N_ASSETS: usize> MerkleSumTreeChip<MST_WIDTH,
instance,
poseidon_config,
overflow_check_config,
};

config
}
}

pub fn assign_leaf_hash_and_balances(
Expand Down
21 changes: 12 additions & 9 deletions zk_prover/src/chips/overflow/overflow_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct OverflowCheckConfig<const MAX_BITS: u8, const ACC_COLS: usize> {
pub a: Column<Advice>,
pub decomposed_values: [Column<Advice>; ACC_COLS],
pub range: Column<Fixed>,
pub selector: Selector,
pub toggle_overflow_check: Selector,
}

#[derive(Debug, Clone)]
Expand All @@ -24,18 +24,20 @@ impl<const MAX_BITS: u8, const ACC_COLS: usize> OverflowChip<MAX_BITS, ACC_COLS>
Self { config }
}

pub fn configure(meta: &mut ConstraintSystem<Fp>) -> OverflowCheckConfig<MAX_BITS, ACC_COLS> {
let selector = meta.selector();
pub fn configure(
meta: &mut ConstraintSystem<Fp>,
a: Column<Advice>,
decomposed_values: [Column<Advice>; ACC_COLS],
) -> OverflowCheckConfig<MAX_BITS, ACC_COLS> {
let toggle_overflow_check = meta.complex_selector();
let range = meta.fixed_column();
let a = meta.advice_column();
let decomposed_values = [(); ACC_COLS].map(|_| meta.advice_column());

meta.enable_equality(a);

meta.create_gate(
"equality check between decomposed_value and value",
|meta| {
let s_doc = meta.query_selector(selector);
let s_doc = meta.query_selector(toggle_overflow_check);

let value = meta.query_advice(a, Rotation::cur());

Expand Down Expand Up @@ -87,15 +89,16 @@ impl<const MAX_BITS: u8, const ACC_COLS: usize> OverflowChip<MAX_BITS, ACC_COLS>
meta.lookup_any("range check for MAXBITS", |meta| {
let cell = meta.query_advice(*column, Rotation::cur());
let range = meta.query_fixed(range, Rotation::cur());
vec![(cell, range)]
let enable_lookup = meta.query_selector(toggle_overflow_check);
vec![(enable_lookup * cell, range)]
});
});

OverflowCheckConfig {
a,
decomposed_values,
range,
selector,
toggle_overflow_check,
}
}

Expand All @@ -108,7 +111,7 @@ impl<const MAX_BITS: u8, const ACC_COLS: usize> OverflowChip<MAX_BITS, ACC_COLS>
|| "assign decomposed values",
|mut region| {
// enable selector
self.config.selector.enable(&mut region, 0)?;
self.config.toggle_overflow_check.enable(&mut region, 0)?;

// Assign input value to the cell inside the region
value.copy_advice(|| "assign value", &mut region, self.config.a, 0)?;
Expand Down
6 changes: 5 additions & 1 deletion zk_prover/src/chips/overflow/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ impl<const MAX_BITS: u8, const ACC_COLS: usize> Circuit<Fp>

fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
let addchip_config = AddChip::configure(meta);
let overflow_check_config = OverflowChip::<MAX_BITS, ACC_COLS>::configure(meta);

let a = meta.advice_column();
let decomposed_values = [(); ACC_COLS].map(|_| meta.advice_column());

let overflow_check_config = OverflowChip::<MAX_BITS, ACC_COLS>::configure(meta, a, decomposed_values);

{
OverflowCheckTestConfig {
Expand Down
5 changes: 2 additions & 3 deletions zk_prover/src/chips/poseidon/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ impl<S: Spec<Fp, WIDTH, RATE>, const WIDTH: usize, const RATE: usize, const L: u
}

// Configuration of the PoseidonChip
// TO DO: Check security of using vectors here!
pub fn configure(
meta: &mut ConstraintSystem<Fp>,
hash_inputs: Vec<Column<Advice>>,
hash_inputs: [Column<Advice>; WIDTH],
partial_sbox: Column<Advice>,
) -> PoseidonConfig<WIDTH, RATE, L> {
let rc_a = (0..WIDTH).map(|_| meta.fixed_column()).collect::<Vec<_>>();
Expand All @@ -62,7 +61,7 @@ impl<S: Spec<Fp, WIDTH, RATE>, const WIDTH: usize, const RATE: usize, const L: u

let pow5_config = Pow5Chip::configure::<S>(
meta,
hash_inputs.try_into().unwrap(),
hash_inputs,
partial_sbox,
rc_a.try_into().unwrap(),
rc_b.try_into().unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions zk_prover/src/circuits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod test {

#[test]
fn test_standard_on_chain_verifier() {
let params = generate_setup_params(11);
let params = generate_setup_params(10);

let circuit = MstInclusionCircuit::<LEVELS, MST_WIDTH, N_ASSETS>::init(
"src/merkle_sum_tree/csv/entry_16.csv",
Expand Down Expand Up @@ -116,7 +116,7 @@ mod test {
#[ignore]
fn test_valid_merkle_sum_tree_with_full_recursive_prover() {
// params for the aggregation circuit
let params_agg = generate_setup_params(23);
let params_agg = generate_setup_params(22);

// downsize params for our application specific snark
let mut params_app = params_agg.clone();
Expand Down

0 comments on commit 8e8b9fd

Please sign in to comment.