Skip to content

Commit

Permalink
Hash type + Into<u256, Hash> impl
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Aug 12, 2024
1 parent 09d4330 commit 2eff4f1
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/merkle_tree.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
use super::utils::double_sha256;
use super::utils::{shr, double_sha256};

#[derive(Copy, Drop)]
pub struct Hash {
pub value: [u32; 8]
}

pub impl U256IntoHash of Into<u256, Hash> {
fn into(self: u256) -> Hash {
let mut result: Array<u32> = array![];
let mut value = self;

let mut i = 0;
loop {
if i == 8 {
break;
}
result.append((value & 0xffffffff).try_into().unwrap());
value = shr(value, 32_u32);
i += 1;
};

Hash {
value: [
*result[7],
*result[6],
*result[5],
*result[4],
*result[3],
*result[2],
*result[1],
*result[0],
]
}
}
}

pub fn merkle_root(ref txids: Array<u256>) -> u256 {
let len = txids.len();
Expand Down

0 comments on commit 2eff4f1

Please sign in to comment.