Skip to content

Commit

Permalink
update to use less copy
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 d5e7a6c commit 1c76d89
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/tendermint_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ use sha2::{Sha256, Digest};

use crate::simple_merkle::tree::MerkleHash;

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())
//hash([LEAF_PREFIX, bytes].concat().as_slice())
let mut hasher = Sha256::new();
hasher.update(LEAF_PREFIX);
hasher.update(bytes);
let result: [u8; 32] = hasher.finalize().into();
result
}

fn inner_hash(left: &[u8], right: &[u8]) -> [u8; 32] {
hash([INNER_PREFIX, left, right].concat().as_slice())
let mut hasher = Sha256::new();
hasher.update(INNER_PREFIX);
hasher.update(left);
hasher.update(right);
let result: [u8; 32] = hasher.finalize().into();
result
}

/// A sha256 hasher, compatible with [Tendermint merkle hash](https://github.com/informalsystems/tendermint-rs/blob/979456c9f33463944f97f7ea3900640e59f7ea6d/tendermint/src/merkle.rs)
Expand Down

0 comments on commit 1c76d89

Please sign in to comment.