Skip to content

Commit

Permalink
More small circuit optimisations
Browse files Browse the repository at this point in the history
- Placing the Poseidon `state` columns after the `partial_sbox` column
  instead of before it causes them to line up with vast stretch of free
  space, enabling the pad-and-add region to be layed out there.

- Using the `Region::assign_advice_from_constant` API to initialise the
  Poseidon state removes fixed-column contention between that region and
  fixed-base scalar multiplication, enabling it to also be layed out
  within the free space.
  - If zcash/halo2#334 were implemented then
    this region would disappear.

- The overflow check in variable-base scalar mul is also moved into the
  columns with free space.
  • Loading branch information
str4d committed Jul 24, 2021
1 parent 274f444 commit 46bca5e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ impl plonk::Circuit<pallas::Base> for Circuit {
let poseidon_config = PoseidonChip::configure(
meta,
poseidon::OrchardNullifier,
advices[5..8].try_into().unwrap(),
advices[8],
// We place the state columns after the partial_sbox column so that the
// pad-and-add region can be layed out more efficiently.
advices[6..9].try_into().unwrap(),
advices[5],
rc_a,
rc_b,
);
Expand Down
8 changes: 5 additions & 3 deletions src/circuit/gadget/ecc/chip/mul/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ impl From<&EccConfig> for Config {
Self {
q_mul_overflow: ecc_config.q_mul_overflow,
lookup_config: ecc_config.lookup_config.clone(),
// Use advice columns that don't conflict with the either the incomplete
// additions in fixed-base scalar mul, or the lookup range checks.
advices: [
ecc_config.advices[0],
ecc_config.advices[1],
ecc_config.advices[2],
ecc_config.advices[6],
ecc_config.advices[7],
ecc_config.advices[8],
],
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/circuit/gadget/poseidon/pow5t3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,12 @@ impl<F: FieldExt, S: Spec<F, WIDTH, 2>> PoseidonDuplexInstructions<F, S, WIDTH,
|| format!("initial state for domain {:?}", domain),
|mut region| {
let mut load_state_word = |i: usize, value: F| {
let var = region.assign_advice(
let var = region.assign_advice_from_constant(
|| format!("state_{}", i),
config.state[i],
0,
|| Ok(value),
value,
)?;
let fixed = region.assign_fixed(
|| format!("state_{}", i),
config.rc_b[i],
0,
|| Ok(value),
)?;
region.constrain_equal(var, fixed)?;
Ok(StateWord {
var,
value: Some(value),
Expand Down Expand Up @@ -754,6 +747,8 @@ mod tests {
meta.fixed_column(),
];

meta.enable_constant(rc_b[0]);

Pow5T3Chip::configure(meta, OrchardNullifier, state, partial_sbox, rc_a, rc_b)
}

Expand Down

0 comments on commit 46bca5e

Please sign in to comment.