Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 5ae54f4

Browse files
noel2004Mason Liang
andauthored
[feat] Update mpt and poseidon circuit with new hash scheme (#667)
* update dep for new hash scheme in mpt * fmt * update poseidon codehash lookup * update poseidon circuit * correct wrong leaf node type * fix read proof when node is empty * update branch * update cargo.lock * update mpt-circuit to v0.5 * obtain leaf's node type from parsed data --------- Co-authored-by: Mason Liang <[email protected]>
1 parent 2b4e90d commit 5ae54f4

File tree

12 files changed

+103
-52
lines changed

12 files changed

+103
-52
lines changed

Cargo.lock

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bus-mapping/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ethers-core = "0.17.0"
1515
ethers-signers = "0.17.0"
1616
ethers-providers = "0.17.0"
1717
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_02_02" }
18-
poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0619", features=["short"]}
18+
poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0723"}
1919
itertools = "0.10"
2020
lazy_static = "1.4"
2121
log = "0.4.14"

bus-mapping/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) fn hash_code_poseidon(code: &[u8]) -> Hash {
5757
let h = if msgs.is_empty() {
5858
// the empty code hash is overlapped with simple hash on [0, 0]
5959
// an issue in poseidon primitive prevent us calculate it from hash_msg
60-
Fr::hash([Fr::zero(), Fr::zero()])
60+
Fr::hash_with_domain([Fr::zero(), Fr::zero()], Fr::zero())
6161
} else {
6262
Fr::hash_msg(&msgs, Some(code.len() as u128 * HASHABLE_DOMAIN_SPEC))
6363
};

eth-types/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ num = "0.4"
2424
num-bigint = { version = "0.4" }
2525
strum_macros = "0.24"
2626
strum = "0.24"
27-
poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0619", features=["short"]}
27+
poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0723"}
2828
[features]
2929
default = ["warn-unimplemented"]
3030
warn-unimplemented = []

zkevm-circuits/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ env_logger = "0.9"
3131
serde = { version = "1.0", features = ["derive"] }
3232
serde_json = "1.0.78"
3333

34-
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0619", features=['short']}
34+
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0723"}
3535
#mpt-circuits = { package = "halo2-mpt-circuits", path = "../../mpt-circuit" }
3636
misc-precompiled-circuit = { package = "rmd160-circuits", git = "https://github.com/scroll-tech/misc-precompiled-circuit.git", branch = "integration" }
3737

zkevm-circuits/src/bytecode_circuit/circuit/to_poseidon_hash.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ impl<F: Field, const BYTES_IN_FIELD: usize> ToHashBlockCircuitConfig<F, BYTES_IN
306306
Rotation::cur(),
307307
),
308308
meta.query_advice(poseidon_table.control, Rotation::cur()),
309+
meta.query_advice(poseidon_table.domain_spec, Rotation::cur()),
309310
]
310311
};
311312

@@ -338,6 +339,7 @@ impl<F: Field, const BYTES_IN_FIELD: usize> ToHashBlockCircuitConfig<F, BYTES_IN
338339
meta.query_advice(field_input, Rotation::cur()),
339340
meta.query_advice(control_length, Rotation::cur())
340341
* domain_spec_factor.clone(),
342+
0.expr(),
341343
];
342344

343345
for (input_expr, table_expr) in lookup_inputs
@@ -366,6 +368,7 @@ impl<F: Field, const BYTES_IN_FIELD: usize> ToHashBlockCircuitConfig<F, BYTES_IN
366368
meta.query_advice(code_hash, Rotation::cur()),
367369
0.expr(),
368370
meta.query_advice(control_length, Rotation::cur()) * domain_spec_factor,
371+
0.expr(),
369372
];
370373
for (input_expr, table_expr) in lookup_inputs
371374
.into_iter()

zkevm-circuits/src/mpt_circuit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ use itertools::Itertools;
1515
use mpt_zktrie::mpt_circuits::{gadgets::poseidon::PoseidonLookup, mpt, types::Proof};
1616

1717
impl PoseidonLookup for PoseidonTable {
18-
fn lookup_columns_generic(&self) -> (Column<Fixed>, [Column<Advice>; 5]) {
18+
fn lookup_columns_generic(&self) -> (Column<Fixed>, [Column<Advice>; 6]) {
1919
(
2020
self.q_enable,
2121
[
2222
self.hash_id,
2323
self.input0,
2424
self.input1,
2525
self.control,
26+
self.domain_spec,
2627
self.heading_mark,
2728
],
2829
)

zkevm-circuits/src/poseidon_circuit.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl<F: Field> SubCircuitConfig<F> for PoseidonCircuitConfig<F> {
4343
poseidon_table.input0,
4444
poseidon_table.input1,
4545
poseidon_table.control,
46+
poseidon_table.domain_spec,
4647
poseidon_table.heading_mark,
4748
],
4849
);
@@ -62,15 +63,15 @@ impl<F: Field> SubCircuit<F> for PoseidonCircuit<F> {
6263
// without any feature we just synthesis an empty poseidon circuit
6364
#[cfg(feature = "zktrie")]
6465
{
65-
let triples = get_storage_poseidon_witness(block);
66-
if triples.len() > max_hashes {
66+
let mpt_hashes = get_storage_poseidon_witness(block);
67+
if mpt_hashes.len() > max_hashes {
6768
log::error!(
6869
"poseidon max_hashes: {:?} not enough. {:?} needed by zktrie proof",
6970
max_hashes,
70-
triples.len()
71+
mpt_hashes.len()
7172
);
7273
}
73-
poseidon_table_data.constant_inputs_with_check(&triples);
74+
poseidon_table_data.fixed_inputs(&mpt_hashes);
7475
}
7576
#[cfg(feature = "poseidon-codehash")]
7677
{
@@ -166,7 +167,9 @@ impl<F: Field + Hashable> Circuit<F> for PoseidonCircuit<F> {
166167
}
167168

168169
#[cfg(feature = "zktrie")]
169-
fn get_storage_poseidon_witness<F: Field>(block: &crate::witness::Block<F>) -> Vec<(F, F, F)> {
170+
fn get_storage_poseidon_witness<F: Field>(
171+
block: &crate::witness::Block<F>,
172+
) -> Vec<([F; 2], F, Option<F>)> {
170173
use itertools::Itertools;
171174
use mpt_zktrie::mpt_circuits::{gadgets::mpt_update::hash_traces, types::Proof};
172175
hash_traces(
@@ -180,7 +183,13 @@ fn get_storage_poseidon_witness<F: Field>(block: &crate::witness::Block<F>) -> V
180183
.collect_vec(),
181184
)
182185
.into_iter()
183-
.unique_by(|(a, b, c)| (a.to_bytes(), b.to_bytes(), c.to_bytes()))
184-
.map(|(a, b, c)| (a.into(), b.into(), c.into()))
186+
.unique_by(|(inp, domain, hash)| {
187+
(
188+
inp.map(|f| f.to_bytes()),
189+
domain.to_bytes(),
190+
hash.to_bytes(),
191+
)
192+
})
193+
.map(|(inp, domain, hash)| (inp.map(F::from), domain.into(), Some(F::from(hash))))
185194
.collect()
186195
}

zkevm-circuits/src/table.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,8 @@ pub struct PoseidonTable {
813813
pub input1: Column<Advice>,
814814
/// control
815815
pub control: Column<Advice>,
816+
/// domain spec
817+
pub domain_spec: Column<Advice>,
816818
/// heading_mark
817819
pub heading_mark: Column<Advice>,
818820
}
@@ -825,6 +827,7 @@ impl<F: Field> LookupTable<F> for PoseidonTable {
825827
self.input0.into(),
826828
self.input1.into(),
827829
self.control.into(),
830+
self.domain_spec.into(),
828831
self.heading_mark.into(),
829832
]
830833
}
@@ -836,6 +839,7 @@ impl<F: Field> LookupTable<F> for PoseidonTable {
836839
String::from("input0"),
837840
String::from("input1"),
838841
String::from("control"),
842+
String::from("domain spec"),
839843
String::from("heading_mark"),
840844
]
841845
}
@@ -856,6 +860,7 @@ impl PoseidonTable {
856860
input0: meta.advice_column(),
857861
input1: meta.advice_column(),
858862
control: meta.advice_column(),
863+
domain_spec: meta.advice_column(),
859864
heading_mark: meta.advice_column(),
860865
}
861866
}
@@ -906,7 +911,7 @@ impl PoseidonTable {
906911
region: &mut Region<'_, F>,
907912
hashes: impl Iterator<Item = &'d [Value<F>]>,
908913
) -> Result<(), Error> {
909-
self.assign(region, 0, [Value::known(F::zero()); 5].as_slice())?;
914+
self.assign(region, 0, [Value::known(F::zero()); 6].as_slice())?;
910915
for (offset, row) in hashes.enumerate() {
911916
self.assign(region, offset + 1, row)?;
912917
}
@@ -927,7 +932,7 @@ impl PoseidonTable {
927932
use hash_circuit::hash::HASHABLE_DOMAIN_SPEC;
928933

929934
layouter.assign_region(
930-
|| "poseidon table",
935+
|| "poseidon codehash table",
931936
|mut region| {
932937
let mut offset = 0;
933938
let poseidon_table_columns =
@@ -937,7 +942,7 @@ impl PoseidonTable {
937942
|| "poseidon table all-zero row",
938943
self.q_enable,
939944
offset,
940-
|| Value::known(F::one()),
945+
|| Value::known(F::zero()),
941946
)?;
942947
for column in poseidon_table_columns.iter().copied() {
943948
region.assign_advice(
@@ -948,26 +953,26 @@ impl PoseidonTable {
948953
)?;
949954
}
950955
offset += 1;
951-
let nil_hash =
952-
Value::known(CodeDB::empty_code_hash().to_word().to_scalar().unwrap());
953-
region.assign_fixed(
954-
|| "poseidon table nil input row",
955-
self.q_enable,
956-
offset,
957-
|| Value::known(F::one()),
958-
)?;
959-
for (column, value) in poseidon_table_columns
960-
.iter()
961-
.copied()
962-
.zip(once(nil_hash).chain(repeat(Value::known(F::zero()))))
963-
{
964-
region.assign_advice(
965-
|| "poseidon table nil input row",
966-
column,
967-
offset,
968-
|| value,
969-
)?;
970-
}
956+
// let nil_hash =
957+
// Value::known(CodeDB::empty_code_hash().to_word().to_scalar().unwrap());
958+
// region.assign_fixed(
959+
// || "poseidon table nil input row",
960+
// self.q_enable,
961+
// offset,
962+
// || Value::known(F::one()),
963+
// )?;
964+
// for (column, value) in poseidon_table_columns
965+
// .iter()
966+
// .copied()
967+
// .zip(once(nil_hash).chain(repeat(Value::known(F::zero()))))
968+
// {
969+
// region.assign_advice(
970+
// || "poseidon table nil input row",
971+
// column,
972+
// offset,
973+
// || value,
974+
// )?;
975+
// }
971976
offset += 1;
972977

973978
for input in inputs.clone() {
@@ -1000,6 +1005,7 @@ impl PoseidonTable {
10001005
once(ref_hash)
10011006
.chain(row.map(Value::known))
10021007
.chain(once(Value::known(control_len_as_flag)))
1008+
.chain(once(Value::known(F::zero()))) // always use domain 0 in codehash
10031009
.chain(once(Value::known(if first_row {
10041010
F::one()
10051011
} else {

zktrie/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ license = "MIT OR Apache-2.0"
88

99
[dependencies]
1010
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_02_02" }
11-
#mpt-circuits = { package = "halo2-mpt-circuits", path = "../../mpt-circuit" }
12-
mpt-circuits = { package = "halo2-mpt-circuits", git = "https://github.com/scroll-tech/mpt-circuit.git", branch = "v0.4" }
13-
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", branch = "scroll-dev-0226", features = ["dual_codehash"] }
14-
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0619", features=['short']}
11+
mpt-circuits = { package = "halo2-mpt-circuits", git = "https://github.com/scroll-tech/mpt-circuit.git", branch = "v0.5" }
12+
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", branch = "v0.6" }
13+
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0723"}
1514
bus-mapping = { path = "../bus-mapping" }
1615
eth-types = { path = "../eth-types" }
1716
lazy_static = "1.4"

0 commit comments

Comments
 (0)