Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Built in function to generate and validate merkle proofs for vanilla Sparse Merkle Tree implementations.
Implement a built-in function for computing merkle roots from uncompressed merkle proofs for the sparse merkle tree (SMT) data structure, such as those commonly employed for Plasma and Plasma Cash constructions.
Merkle proofs are useful and awesome, and can be difficult to implement due to many considerations and different possible optimizations. Having a built in function similar to
def calc_smt_root(key: uint256, # key determines order of siblings at each height leaf_hash: bytes32, # hash_function(abi.encode(leaf)) proof: bytes32[N], # proof array where 'N' is the SMT depth (static) function=hash_function, # configures the function used ) -> bytes32 # Returns calculated root for above
An example of using this would be
root_hash: bytes32 N: constant(uint256) = 32 def validate_root(root_hash: bytes32, key: uint256, leaf: int128, proof: bytes32[N]): leaf_hash: bytes32 = keccak256(leaf) assert self.root_hash == calc_smt_root(key, leaf_hash, proof, function=keccak256) # ... do actions!
Copyright and related rights waived via CC0