-
Notifications
You must be signed in to change notification settings - Fork 57
/
merkle_proof.rs
23 lines (22 loc) 路 1.2 KB
/
merkle_proof.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//! These functions deal with verification of Merkle trees (hash trees).
//! Direct port of https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/cryptography/MerkleProof.sol
/// Returns true if a `leaf` can be proved to be a part of a Merkle tree
/// defined by `root`. For this, a `proof` must be provided, containing
/// sibling hashes on the branch from the leaf to the root of the tree. Each
/// pair of leaves and each pair of pre-images are assumed to be sorted.
pub fn verify(proof: Vec<[u8; 32]>, root: [u8; 32], leaf: [u8; 32]) -> bool {
let mut computed_hash = leaf;
for proof_element in proof.into_iter() {
if computed_hash <= proof_element {
// Hash(current computed hash + current element of the proof)
computed_hash =
anchor_lang::solana_program::keccak::hashv(&[&computed_hash, &proof_element]).0;
} else {
// Hash(current element of the proof + current computed hash)
computed_hash =
anchor_lang::solana_program::keccak::hashv(&[&proof_element, &computed_hash]).0;
}
}
// Check if the computed hash (root) is equal to the provided root
computed_hash == root
}