Skip to content

Commit

Permalink
Update zktrie with native rust lib (#1198)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
noel2004 and lispc authored Apr 18, 2024
1 parent f9dcb2c commit a08da07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
30 changes: 28 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions zktrie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license.workspace = true
[dependencies]
halo2_proofs.workspace = true
mpt-circuits = { package = "halo2-mpt-circuits", git = "https://github.com/scroll-tech/mpt-circuit.git", branch = "v0.7", default-features=false }
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", tag = "v0.7.1" }
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", tag = "v0.8.0", features= ["rs_zktrie"] }
hash-circuit.workspace = true
eth-types = { path = "../eth-types" }
num-bigint.workspace = true
Expand All @@ -24,4 +24,3 @@ serde_json.workspace = true
[features]
default = []
parallel_syn = ["mpt-circuits/parallel_syn"]

43 changes: 9 additions & 34 deletions zktrie/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,30 @@ use hash_circuit::hash::Hashable;
pub fn init_hash_scheme() {
static INIT: Once = Once::new();
INIT.call_once(|| {
zktrie::init_hash_scheme(hash_scheme);
zktrie::init_hash_scheme_simple(poseidon_hash_scheme);
});
}

static FILED_ERROR_READ: &str = "invalid input field";
static FILED_ERROR_OUT: &str = "output field fail";

extern "C" fn hash_scheme(
a: *const u8,
b: *const u8,
domain: *const u8,
out: *mut u8,
) -> *const i8 {
use std::slice;
let a: [u8; 32] =
TryFrom::try_from(unsafe { slice::from_raw_parts(a, 32) }).expect("length specified");
let b: [u8; 32] =
TryFrom::try_from(unsafe { slice::from_raw_parts(b, 32) }).expect("length specified");
let domain: [u8; 32] =
TryFrom::try_from(unsafe { slice::from_raw_parts(domain, 32) }).expect("length specified");
let out = unsafe { slice::from_raw_parts_mut(out, 32) };

let fa = Fr::from_bytes(&a);
fn poseidon_hash_scheme(a: &[u8; 32], b: &[u8; 32], domain: &[u8; 32]) -> Option<[u8; 32]> {
let fa = Fr::from_bytes(a);
let fa = if fa.is_some().into() {
fa.unwrap()
} else {
return FILED_ERROR_READ.as_ptr().cast();
return None;
};
let fb = Fr::from_bytes(&b);
let fb = Fr::from_bytes(b);
let fb = if fb.is_some().into() {
fb.unwrap()
} else {
return FILED_ERROR_READ.as_ptr().cast();
return None;
};
let fdomain = Fr::from_bytes(&domain);
let fdomain = Fr::from_bytes(domain);
let fdomain = if fdomain.is_some().into() {
fdomain.unwrap()
} else {
return FILED_ERROR_READ.as_ptr().cast();
return None;
};

let h = Fr::hash_with_domain([fa, fb], fdomain);
let repr_h = h.to_repr();
if repr_h.len() == 32 {
out.copy_from_slice(repr_h.as_ref());
std::ptr::null()
} else {
FILED_ERROR_OUT.as_ptr().cast()
}
Some(Fr::hash_with_domain([fa, fb], fdomain).to_repr())
}

pub(crate) const NODE_TYPE_MIDDLE_0: u8 = 6;
Expand Down

0 comments on commit a08da07

Please sign in to comment.