Skip to content

Latest commit

 

History

History
67 lines (39 loc) · 2.26 KB

README.md

File metadata and controls

67 lines (39 loc) · 2.26 KB

zktrie

zktrie is a binary poseidon trie used in Scroll Network.

Go and Rust implementations are provided inside this repo.

Design Doc

See the technical docs here.

Example codes

Rust example code
Go example code

Rust Usage

We must init the crate with a poseidon hash scheme before any actions. This is an example

All the zktrie can share one underlying database, which can be initialized by putting the encoded trie node data directly

let mut db = ZkMemoryDb::new();

/* for some trie node data encoded as bytes `buf` */
db.add_node_data(&buf).unwrap();

/* or if we have both trie node data and key encoded as bytes */
db.add_node_bytes(&buf, Some(&key)).unwrap();

We must prove the root for a trie to create it, the corresponding root node must have been input in the database

let root = hex::decode("079a038fbf78f25a2590e5a1d2fa34ce5e5f30e9a332713b43fa0e51b8770ab8")
    .unwrap();
let root: Hash = root.as_slice().try_into().unwrap();

let mut trie = db.new_trie(&root).unwrap();

The trie can be updated by a single 32-bytes buffer if it is storage trie, or a [[u8;32];4] array for the account data {nonce, balance, codehash, storageRoot} if it is account trie

let acc_buf = hex::decode("4cb1aB63aF5D8931Ce09673EbD8ae2ce16fD6571").unwrap();
let code_hash: [u8;32] = hex::decode("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap().as_slice().try_into().unwrap();

/* update an externally-owned account (so its storageRoot is all zero and code_hash equal to keccak256(nil)) */
let newacc: AccountData = [nonce, balance, code_hash, [0; FIELDSIZE]];
trie.update_account(&acc_buf, &newacc).unwrap();

The root and mpt path for an address can be query from trie by ZkTrie::root and ZkTrie::prove

License

Licensed under either of

at your discretion.