diff --git a/src/tendermint_hash.rs b/src/tendermint_hash.rs new file mode 100644 index 0000000..6a6a08e --- /dev/null +++ b/src/tendermint_hash.rs @@ -0,0 +1,40 @@ +use sha2::{Sha256, Digest}; + +fn hash(bytes: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(bytes); + let result: [u8; 32] = hasher.finalize().into(); + result +} + +const LEAF_PREFIX: &[u8] = &[0]; +const INNER_PREFIX: &[u8] = &[1]; + +fn leaf_hash(bytes: &[u8]) -> [u8; 32] { + hash([LEAF_PREFIX, bytes].concat().as_slice()) +} + +fn inner_hash(left: &[u8], right: &[u8]) -> [u8; 32] { + hash([INNER_PREFIX, left, right].concat().as_slice()) +} + +pub struct TmSha2Hasher; + +impl TmSha2Hasher { + pub fn new() -> Self { + TmSha2Hasher + } +} + +impl MerkleHash for TmSha2Hasher { + type Output = [u8; 32]; + + const EMPTY_ROOT : Self::Output = [227, 176, 196, 66, 152, 252, 28, 20, 154, 251, 244, 200, 153, 111, 185, 36, 39, 174, 65, 228, 100, 155, 147, 76, 164, 149, 153, 27, 120, 82, 184, 85]; + + fn hash_leaf(&self, data: &[u8]) -> Self::Output { + leaf_hash(data) + } + fn hash_nodes(&self, left: &Self::Output, right: &Self::Output) -> Self::Output { + inner_hash(left, right) + } +} \ No newline at end of file