Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #189 from noir-lang/merkle
Browse files Browse the repository at this point in the history
update merkle stdlib functions docs
  • Loading branch information
TomAFrench authored Jun 10, 2023
2 parents 5381370 + 3832f89 commit 6b42ef0
Showing 1 changed file with 6 additions and 70 deletions.
76 changes: 6 additions & 70 deletions docs/standard_library/merkle_trees.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: Merkle Trees
description:
Learn about Merkle Trees in Noir with this tutorial. Explore the basics of check membership and
computing root from leaf, and implement them in your code with the help of code
Learn about Merkle Trees in Noir with this tutorial. Explore the basics of computing a merkle root using a proof, with examples.
keywords:
[
Merkle trees in Noir,
Expand All @@ -17,59 +16,12 @@ keywords:
]
---

## check_membership

Returns 1 if the specified leaf is at the given index on a tree.

```rust
fn check_membership(_root : Field, _leaf : Field, _index : Field, _hash_path: [Field]) -> Field
```

example:

```rust
/**
*
index = "0"
priv_key = "0x000000000000000000000000000000000000000000000000000000616c696365"
secret = "0x1929ea3ab8d9106a899386883d9428f8256cfedb3c4f6b66bf4aa4d28a79988f"
root = "0x2f36d4404719a30512af45be47c9732e916cb131933102b04ba6432602db209c"
hash_path = [
"0x1e61bdae0f027b1b2159e1f9d3f8d00fa668a952dddd822fda80dc745d6f65cc",
"0x0e4223f3925f98934393c74975142bd73079ab0621f4ee133cee050a3c194f1a",
"0x2fd7bb412155bf8693a3bd2a3e7581a679c95c68a052f835dddca85fa1569a40"
]
*/

fn main(root : Field, index : Field, hash_path : [Field; 3], secret: Field, priv_key: Field) {
constrain index == index;

let pubkey = std::scalar_mul::fixed_base(priv_key);
let pubkey_x = pubkey[0];
let pubkey_y = pubkey[1];
let note_commitment = std::hash::pedersen([pubkey_x, pubkey_y, secret]);

let root = std::merkle::check_membership(root, note_commitment[0], index, hash_path);
std::println(root);
}
```

## check_membership_in_noir

Behaves exactly the same as above, but it's computed in Noir in order to accept many backends.

```rust
fn check_membership_in_noir(root : Field, leaf : Field, index : Field, hash_path: [Field]) -> Field
```

For examples, you can literally replace `check_membership` for this method, in the above example.

## compute_root_from_leaf
## compute_merkle_root

Returns the root of the tree from the provided leaf and its hash path, using a pedersen hash.
Returns the root of the tree from the provided leaf and its hash path, using a [Pedersen hash](cryptographic_primitives/00_hashes.mdx#pedersen).

```rust
fn compute_root_from_leaf(leaf : Field, index : Field, hash_path: [Field]) -> Field
fn compute_merkle_root(leaf : Field, index : Field, hash_path: [Field]) -> Field
```

example:
Expand All @@ -85,30 +37,14 @@ example:
"0x2fd7bb412155bf8693a3bd2a3e7581a679c95c68a052f835dddca85fa1569a40"
]
*/
fn main(index : Field, priv_key : Field, secret : Field, note_hash_path : [Field; 3]) {
constrain index == index;
fn main(index: Field, priv_key: Field, secret: Field, note_hash_path: [Field; 3]) {

let pubkey = std::scalar_mul::fixed_base(priv_key);
let pubkey_x = pubkey[0];
let pubkey_y = pubkey[1];
let note_commitment = std::hash::pedersen([pubkey_x, pubkey_y, secret]);

let root = std::merkle::compute_root_from_leaf(note_commitment[0], index, note_hash_path);
let root = std::merkle::compute_merkle_root(note_commitment[0], index, note_hash_path);
std::println(root);
}
```

## compute_merkle_root

Returns the root of the tree from the provided leaf and its hash path, using a pedersen hash.

```rust
#[foreign(compute_merkle_root)]
fn compute_merkle_root(_leaf : Field, _index : Field, _hash_path: [Field]) -> Field {}
```

:::info

This is a black box function. Read [this section](black_box_fns) to learn more about black box functions in Noir.

:::

0 comments on commit 6b42ef0

Please sign in to comment.