-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from Chia-Network/tree-hash-crate
move tree_hash() into its own crate (clvm-utils)
- Loading branch information
Showing
11 changed files
with
181 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "clvm-utils" | ||
version = "0.1.14" | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
description = "Utility functions for processing clvm programs and structures" | ||
authors = ["Arvid Norberg <[email protected]>"] | ||
homepage = "https://github.com/Chia-Network/chia_rs/" | ||
repository = "https://github.com/Chia-Network/chia_rs/clvm-utils" | ||
|
||
[dependencies] | ||
clvmr = "=0.1.24" | ||
|
||
[profile.release] | ||
lto = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Library providing utility functions for handling CLVM programs | ||
|
||
|
||
computing tree hash: | ||
|
||
```rust | ||
fn tree_hash(a: &Allocator, node: NodePtr) -> [u8; 32] | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "clvm-utils-fuzz" | ||
version = "0.0.0" | ||
authors = ["Automatically generated"] | ||
publish = false | ||
edition = "2021" | ||
|
||
[package.metadata] | ||
cargo-fuzz = true | ||
|
||
[dependencies] | ||
libfuzzer-sys = "0.4" | ||
clvmr = "=0.1.24" | ||
|
||
[dependencies.clvm-utils] | ||
path = ".." | ||
|
||
[dependencies.chia] | ||
path = "../.." | ||
|
||
# Prevent this from interfering with workspaces | ||
[workspace] | ||
members = ["."] | ||
|
||
[[bin]] | ||
name = "tree-hash" | ||
path = "fuzz_targets/tree-hash.rs" | ||
test = false | ||
doc = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod tree_hash; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use clvmr::allocator::{Allocator, NodePtr, SExp}; | ||
use clvmr::sha2::{Digest, Sha256}; | ||
|
||
enum TreeOp { | ||
SExp(NodePtr), | ||
Cons, | ||
} | ||
|
||
pub fn tree_hash(a: &Allocator, node: NodePtr) -> [u8; 32] { | ||
let mut hashes: Vec<[u8; 32]> = vec![]; | ||
let mut ops = vec![TreeOp::SExp(node)]; | ||
|
||
while !ops.is_empty() { | ||
match ops.pop().unwrap() { | ||
TreeOp::SExp(node) => match a.sexp(node) { | ||
SExp::Atom(atom) => { | ||
let mut sha256 = Sha256::new(); | ||
sha256.update([1_u8]); | ||
sha256.update(a.buf(&atom)); | ||
hashes.push(sha256.finalize().into()) | ||
} | ||
SExp::Pair(left, right) => { | ||
ops.push(TreeOp::Cons); | ||
ops.push(TreeOp::SExp(left)); | ||
ops.push(TreeOp::SExp(right)); | ||
} | ||
}, | ||
TreeOp::Cons => { | ||
let mut sha256 = Sha256::new(); | ||
sha256.update([2_u8]); | ||
sha256.update(hashes.pop().unwrap()); | ||
sha256.update(hashes.pop().unwrap()); | ||
hashes.push(sha256.finalize().into()); | ||
} | ||
} | ||
} | ||
|
||
assert!(hashes.len() == 1); | ||
hashes[0] | ||
} | ||
|
||
#[test] | ||
fn test_tree_hash() { | ||
let mut a = Allocator::new(); | ||
let atom1 = a.new_atom(&[1, 2, 3]).unwrap(); | ||
let atom2 = a.new_atom(&[4, 5, 6]).unwrap(); | ||
let root = a.new_pair(atom1, atom2).unwrap(); | ||
|
||
// test atom1 hash | ||
let atom1_hash = { | ||
let mut sha256 = Sha256::new(); | ||
sha256.update(&[1_u8]); | ||
sha256.update(&[1, 2, 3]); | ||
let atom1_hash = sha256.finalize(); | ||
|
||
assert_eq!(tree_hash(&a, atom1), atom1_hash.as_slice()); | ||
atom1_hash | ||
}; | ||
|
||
// test atom2 hash | ||
let atom2_hash = { | ||
let mut sha256 = Sha256::new(); | ||
sha256.update(&[1_u8]); | ||
sha256.update(&[4, 5, 6]); | ||
let atom2_hash = sha256.finalize(); | ||
|
||
assert_eq!(tree_hash(&a, atom2), atom2_hash.as_slice()); | ||
atom2_hash | ||
}; | ||
|
||
// test tree hash | ||
let root_hash = { | ||
let mut sha256 = Sha256::new(); | ||
sha256.update(&[2_u8]); | ||
sha256.update(atom1_hash.as_slice()); | ||
sha256.update(atom2_hash.as_slice()); | ||
let root_hash = sha256.finalize(); | ||
|
||
assert_eq!(tree_hash(&a, root), root_hash.as_slice()); | ||
root_hash | ||
}; | ||
|
||
let atom3 = a.new_atom(&[7, 8, 9]).unwrap(); | ||
let root2 = a.new_pair(root, atom3).unwrap(); | ||
|
||
let atom3_hash = { | ||
let mut sha256 = Sha256::new(); | ||
sha256.update(&[1_u8]); | ||
sha256.update(&[7, 8, 9]); | ||
sha256.finalize() | ||
}; | ||
|
||
// test deeper tree hash | ||
{ | ||
let mut sha256 = Sha256::new(); | ||
sha256.update(&[2_u8]); | ||
sha256.update(root_hash.as_slice()); | ||
sha256.update(atom3_hash.as_slice()); | ||
|
||
assert_eq!(tree_hash(&a, root2), sha256.finalize().as_slice()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters