Skip to content

Commit

Permalink
tendermint_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor O'Hara authored and Connor O'Hara committed Apr 23, 2024
1 parent 89f8cef commit d98a921
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/tendermint_hash.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d98a921

Please sign in to comment.