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

Commit a08da07

Browse files
noel2004lispc
andauthored
Update zktrie with native rust lib (#1198)
* update dep for new zktrie (rust-zktrie) Signed-off-by: noelwei <[email protected]> * update dep * udpate zktrie Signed-off-by: noelwei <[email protected]> * update zktrie dep --------- Signed-off-by: noelwei <[email protected]> Co-authored-by: Zhuo Zhang <[email protected]>
1 parent f9dcb2c commit a08da07

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

Cargo.lock

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

zktrie/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license.workspace = true
99
[dependencies]
1010
halo2_proofs.workspace = true
1111
mpt-circuits = { package = "halo2-mpt-circuits", git = "https://github.com/scroll-tech/mpt-circuit.git", branch = "v0.7", default-features=false }
12-
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", tag = "v0.7.1" }
12+
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", tag = "v0.8.0", features= ["rs_zktrie"] }
1313
hash-circuit.workspace = true
1414
eth-types = { path = "../eth-types" }
1515
num-bigint.workspace = true
@@ -24,4 +24,3 @@ serde_json.workspace = true
2424
[features]
2525
default = []
2626
parallel_syn = ["mpt-circuits/parallel_syn"]
27-

zktrie/src/state/builder.rs

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,55 +16,30 @@ use hash_circuit::hash::Hashable;
1616
pub fn init_hash_scheme() {
1717
static INIT: Once = Once::new();
1818
INIT.call_once(|| {
19-
zktrie::init_hash_scheme(hash_scheme);
19+
zktrie::init_hash_scheme_simple(poseidon_hash_scheme);
2020
});
2121
}
2222

23-
static FILED_ERROR_READ: &str = "invalid input field";
24-
static FILED_ERROR_OUT: &str = "output field fail";
25-
26-
extern "C" fn hash_scheme(
27-
a: *const u8,
28-
b: *const u8,
29-
domain: *const u8,
30-
out: *mut u8,
31-
) -> *const i8 {
32-
use std::slice;
33-
let a: [u8; 32] =
34-
TryFrom::try_from(unsafe { slice::from_raw_parts(a, 32) }).expect("length specified");
35-
let b: [u8; 32] =
36-
TryFrom::try_from(unsafe { slice::from_raw_parts(b, 32) }).expect("length specified");
37-
let domain: [u8; 32] =
38-
TryFrom::try_from(unsafe { slice::from_raw_parts(domain, 32) }).expect("length specified");
39-
let out = unsafe { slice::from_raw_parts_mut(out, 32) };
40-
41-
let fa = Fr::from_bytes(&a);
23+
fn poseidon_hash_scheme(a: &[u8; 32], b: &[u8; 32], domain: &[u8; 32]) -> Option<[u8; 32]> {
24+
let fa = Fr::from_bytes(a);
4225
let fa = if fa.is_some().into() {
4326
fa.unwrap()
4427
} else {
45-
return FILED_ERROR_READ.as_ptr().cast();
28+
return None;
4629
};
47-
let fb = Fr::from_bytes(&b);
30+
let fb = Fr::from_bytes(b);
4831
let fb = if fb.is_some().into() {
4932
fb.unwrap()
5033
} else {
51-
return FILED_ERROR_READ.as_ptr().cast();
34+
return None;
5235
};
53-
let fdomain = Fr::from_bytes(&domain);
36+
let fdomain = Fr::from_bytes(domain);
5437
let fdomain = if fdomain.is_some().into() {
5538
fdomain.unwrap()
5639
} else {
57-
return FILED_ERROR_READ.as_ptr().cast();
40+
return None;
5841
};
59-
60-
let h = Fr::hash_with_domain([fa, fb], fdomain);
61-
let repr_h = h.to_repr();
62-
if repr_h.len() == 32 {
63-
out.copy_from_slice(repr_h.as_ref());
64-
std::ptr::null()
65-
} else {
66-
FILED_ERROR_OUT.as_ptr().cast()
67-
}
42+
Some(Fr::hash_with_domain([fa, fb], fdomain).to_repr())
6843
}
6944

7045
pub(crate) const NODE_TYPE_MIDDLE_0: u8 = 6;

0 commit comments

Comments
 (0)