Skip to content

Commit

Permalink
feat(docs): document the merkle proof verifier functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfertel committed Jul 25, 2024
1 parent 4b07d68 commit 2b83c2e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion docs/modules/ROOT/pages/crypto.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,24 @@ The OpenZeppelin Rust Contracts provide a crate for common cryptographic procedu

== Verifying Merkle Proofs

TODO: Fill this section out.
Developers can build a Merkle Tree off-chain, which allows for verifying that an element (leaf) is part of a set by using a Merkle Proof. This technique is widely used for creating whitelists (e.g. for airdrops) and other advanced use cases.

TIP: OpenZeppelin Contracts provides a https://github.com/OpenZeppelin/merkle-tree[JavaScript library] for building trees off-chain and generating proofs.

https://docs.rs/crypto/latest/merkle/struct.Verifier.html[`MerkleProof`] provides:

* https://docs.rs/crypto/latest/merkle/struct.Verifier.html#method.verify[`verify`] - can prove that some value is part of a https://en.wikipedia.org/wiki/Merkle_tree[Merkle tree].

* https://docs.rs/crypto/latest/merkle/struct.Verifier.html#method.verify_multi_proof[`verify_multi_proof`] - can prove multiple values are part of a Merkle tree.

[source,rust]
----
pub fn verify(&self, proof: Vec<B256>, root: B256, leaf: B256) -> bool {
let proof: Vec<[u8; 32]> = proof.into_iter().map(|m| *m).collect();
Verifier::<KeccakBuilder>::verify(&proof, *root, *leaf)
}
----

Note that these functions use `keccak256` as the hashing algorithm, but our library also provides generic counterparts: https://docs.rs/crypto/latest/merkle/struct.Verifier.html#method.verify_with_builder[`verify_with_builer`] and https://docs.rs/crypto/latest/merkle/struct.Verifier.html#method.verify_multi_proof_with_builder[`verify_multi_proof_with_builder`].

We also provide an adapter https://docs.rs/crypto/latest/hash/index.html[`hash`] module to use your own hashers in conjunction with them that resembles Rust's standard library's API.

0 comments on commit 2b83c2e

Please sign in to comment.