-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmerkle_tree.rs
More file actions
29 lines (24 loc) · 848 Bytes
/
merkle_tree.rs
File metadata and controls
29 lines (24 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::aggregators::AlignedProof;
use sha3::{Digest, Keccak256};
pub fn combine_hashes(hash_a: &[u8; 32], hash_b: &[u8; 32]) -> [u8; 32] {
let mut hasher = Keccak256::new();
hasher.update(hash_a);
hasher.update(hash_b);
hasher.finalize().into()
}
/// Returns (merkle_root, leaves)
pub fn compute_proofs_merkle_root(proofs: &[AlignedProof]) -> ([u8; 32], Vec<[u8; 32]>) {
let leaves: Vec<[u8; 32]> = proofs.iter().map(|proof| proof.hash()).collect();
let mut root = leaves.clone();
while root.len() > 1 {
root = root
.chunks(2)
.map(|chunk| match chunk {
[a, b] => combine_hashes(a, b),
[a] => combine_hashes(a, a),
_ => panic!("Unexpected chunk size in leaves"),
})
.collect()
}
(root[0], leaves)
}