-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
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
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) | ||
} | ||
} |