Skip to content

Commit 4eb7f15

Browse files
author
Mason Liang
committed
Use separate assign_regions call for mpt updates
1 parent 69fc6e8 commit 4eb7f15

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

src/gadgets/mpt_update.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ use halo2_proofs::{
2929
arithmetic::FieldExt,
3030
circuit::{Region, Value},
3131
halo2curves::{bn256::Fr, group::ff::PrimeField},
32-
plonk::ConstraintSystem,
32+
plonk::{ConstraintSystem, Error},
3333
};
3434
use lazy_static::lazy_static;
35+
use std::iter::{once, repeat};
3536
use strum::IntoEnumIterator;
3637

3738
lazy_static! {
@@ -361,6 +362,35 @@ impl MptUpdateConfig {
361362
}
362363
}
363364

365+
pub fn assignments(
366+
&self,
367+
proofs: Vec<Proof>,
368+
n_rows: usize,
369+
randomness: Value<Fr>,
370+
) -> Vec<impl FnMut(Region<'_, Fr>) -> Result<(), Error> + '_> {
371+
let n_padding_rows = n_rows - Self::n_rows_required(&proofs);
372+
let n_closures = 1 + proofs.len() + n_padding_rows;
373+
dbg!(n_closures);
374+
once(None)
375+
.chain(proofs.into_iter().map(Some).chain(repeat(None)))
376+
.take(n_closures)
377+
.enumerate()
378+
.map(move |(i, maybe_proof)| {
379+
move |mut region: Region<'_, Fr>| {
380+
if let Some(proof) = maybe_proof.clone() {
381+
self.assign_proof(&mut region, 0, &proof, randomness);
382+
} else if i == 0 {
383+
// Need make one assignment so region size is calculated correctly.
384+
self.key.assign(&mut region, 0, 0);
385+
} else {
386+
self.assign_padding_row(&mut region, 0);
387+
}
388+
Ok(())
389+
}
390+
})
391+
.collect()
392+
}
393+
364394
pub fn n_rows_required(proofs: &[Proof]) -> usize {
365395
// +1 because assigment starts on offset = 1 instead of offset = 0.
366396
proofs.iter().map(Proof::n_rows).sum::<usize>() + 1

src/mpt.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -121,33 +121,26 @@ impl MptCircuitConfig {
121121
n_rows: usize,
122122
) -> Result<(), Error> {
123123
let randomness = self.rlc_randomness.value(layouter);
124-
let (u32s, u64s, u128s, frs) = byte_representations(proofs);
125124

126-
layouter.assign_region(
127-
|| "mpt circuit",
128-
|mut region| {
129-
self.mpt_update
130-
.assign(&mut region, proofs, n_rows, randomness);
131-
Ok(())
132-
},
125+
let proofs_vec: Vec<_> = proofs.iter().cloned().collect();
126+
layouter.assign_regions(
127+
|| "mpt circuit parallel assignment 1",
128+
self.mpt_update.assignments(proofs_vec, n_rows, randomness),
133129
)?;
134130

135-
let mut keys = mpt_update_keys(proofs);
136-
keys.sort();
137-
keys.dedup();
138-
139-
let selector_assignments = self.selector_assignments(n_rows);
140-
let byte_bit_assignments = self.byte_bit.assignments();
131+
let (u32s, u64s, u128s, frs) = byte_representations(proofs);
141132
let byte_representation_assignments = self
142133
.byte_representation
143134
.assignments(u32s, u64s, u128s, frs, randomness);
144-
let canonical_representation_assignments = self
145-
.canonical_representation
146-
.assignments(keys, n_rows, randomness);
135+
let selector_assignments = self.selector_assignments(n_rows);
136+
let byte_bit_assignments = self.byte_bit.assignments();
137+
let canonical_representation_assignments =
138+
self.canonical_representation
139+
.assignments(mpt_update_keys(proofs), n_rows, randomness);
147140
let key_bit_assignments = self.key_bit.assignments(key_bit_lookups(proofs));
148141

149142
layouter.assign_regions(
150-
|| "mpt circuit parallel assignment",
143+
|| "mpt circuit parallel assignment 2",
151144
AssignmentMap::new(
152145
selector_assignments
153146
.chain(byte_bit_assignments)

0 commit comments

Comments
 (0)