-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docs): Historical trees docs (#3895)
Please provide a paragraph or two giving a summary of the change, including relevant motivation and context. # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [x] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [x] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [x] Every change is related to the PR description. - [x] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). --------- Co-authored-by: josh crites <critesjosh@gmail.com> Co-authored-by: Jan Beneš <janbenes1234@gmail.com> Co-authored-by: Rahul Kothari <rahul.kothari.201@gmail.com>
- Loading branch information
1 parent
84c9fdb
commit 8c3efba
Showing
5 changed files
with
282 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
docs/docs/dev_docs/contracts/syntax/historical_access/history_lib_reference.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
--- | ||
title: History Reference | ||
--- | ||
|
||
<!-- Note: This will soon be moved into an Aztec.nr reference category under Aztec.nr smart contracts --> | ||
|
||
## Note inclusion | ||
|
||
Note inclusion proves that a note existed (its hash was included in a note hash tree) at a specific block number. | ||
|
||
## prove_note_inclusion | ||
|
||
`prove_note_inclusion` takes 4 parameters: | ||
|
||
| Name | Type | Description | | ||
|-----------------|------------------------|-----------------------------------------------------| | ||
| note_interface | NoteInterface<Note, N> | Interface for the note with necessary functionality| | ||
| note_with_header| Note | The note you are proving inclusion for | | ||
| block_number | u32 | Block number for proving note's existence | | ||
| context | PrivateContext | Private context | | ||
|
||
## prove_note_commitment_inclusion | ||
|
||
A **commitment**, also referred to as a **note hash** is a public acknowledgment of the existence of a note without revealing the content of the note. You can learn more about how to compress a note to a note hash [here](../../../../concepts/advanced/data_structures/trees.md#example-note). | ||
|
||
`prove_note_commitment_inclusion` takes 3 parameters: | ||
|
||
| Name | Type | Description | | ||
|-----------------|------------------------|-----------------------------------------------------| | ||
| commitment | Field | Note commitment we are checking inclusion of | | ||
| block_number | u32 | Block number for proving note's existence | | ||
| context| PrivateContext | Private Context | | ||
|
||
## Note validity | ||
|
||
This proves that a note exists and has not been nullified at a specified block. | ||
|
||
### prove_note_validity | ||
|
||
`prove_note_validity` takes 4 parameters: | ||
|
||
| Name | Type | Description | | ||
|-----------------|------------------------|-----------------------------------------------------| | ||
| note_interface | NoteInterface<Note, N> | Interface for the note with necessary functionality| | ||
| note_with_header| Note | The note you are proving inclusion for | | ||
| block_number | u32 | Block number for proving note's existence | | ||
| context | PrivateContext | Private context | | ||
|
||
## Nullifier inclusion | ||
|
||
This proves that a nullifier was included in a certain block (can be used to prove that a note had been nullified). | ||
|
||
### prove_nullifier_inclusion | ||
|
||
`prove_nullifier_inclusion` takes 3 parameters: | ||
|
||
| Name | Type | Description | | ||
|-----------------|------------------------|-----------------------------------------------------| | ||
| nullifier | Field | The nullifier you are proving inclusion for | | ||
| block_number | u32 | Block number for proving note's existence | | ||
| context | PrivateContext | Private context | | ||
|
||
## Nullifier non inclusion | ||
|
||
This proves that a nullifier was not included in a certain block (can be used to prove that a note had not yet been nullified in a given block). | ||
|
||
### prove_nullifier_non_inclusion | ||
|
||
`prove_nullifier_non_inclusion` takes 3 parameters: | ||
|
||
| Name | Type | Description | | ||
|-----------------|------------------------|-----------------------------------------------------| | ||
| nullifier | Field | The nullifier you are proving inclusion for | | ||
| block_number | u32 | Block number for proving note's existence | | ||
| context | PrivateContext | Private context | | ||
|
||
### note_not_nullified | ||
|
||
Instead of passing the nullifier, you can check that a note has not been nullified by passing the note. | ||
|
||
## Public value inclusion | ||
|
||
This proves that a public value exists at a certain block. | ||
|
||
### prove_public_value_inclusion | ||
|
||
`prove_public_value_inclusion` takes 4 parameters: | ||
|
||
| Name | Type | Description | | ||
|-----------------|------------------------|-----------------------------------------------------| | ||
| value | Field | The public value you are proving inclusion for | | ||
| storage_slot | Field | Storage slot the value exists in | | ||
| block_number | u32 | Block number for proving value's existence | | ||
| context | PrivateContext | Private context | | ||
|
||
## Contract inclusion | ||
|
||
This proves that a contract exists in, ie had been deployed before or in, a certain block. | ||
|
||
### prove_contract_inclusion | ||
|
||
`prove_contract_inclusion` takes 7 parameters: | ||
|
||
| Name | Type | Description | | ||
|---------------------------|-----------------|-------------------------------------------------------| | ||
| deployer_public_key | GrumpkinPoint | Public key of the contract deployer | | ||
| contract_address_salt | Field | Unique identifier for the contract's address | | ||
| function_tree_root | Field | Root of the contract's function tree | | ||
| constructor_hash | Field | Hash of the contract's constructor | | ||
| portal_contract_address | EthAddress | Ethereum address of the associated portal contract | | ||
| block_number | u32 | Block number for proof verification | | ||
| context | PrivateContext | Private context | | ||
|
||
If there is no associated portal contract, you can use a zero Ethereum address: | ||
|
||
```ts | ||
new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES)); | ||
``` |
102 changes: 102 additions & 0 deletions
102
docs/docs/dev_docs/contracts/syntax/historical_access/how_to_prove_history.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--- | ||
title: How to prove existence of historical notes and nullifiers | ||
--- | ||
|
||
The Aztec Protocol uses an append-only Merkle tree to store hashes of the headers of all previous blocks in the chain as its leaves. This is known as an archive tree. You can learn more about how it works in the [concepts section](../../../../concepts/advanced/data_structures/trees.md#archive-tree). | ||
|
||
# History library | ||
|
||
The history library allows you to prove any of the following at a given block height before the current height: | ||
* [Note inclusion](./history_lib_reference.md#note-inclusion) | ||
* [Nullifier inclusion](./history_lib_reference.md#nullifier-inclusion) | ||
* [Note validity](./history_lib_reference.md#note-validity) | ||
* [Existence of public value](./history_lib_reference.md#public-value-inclusion) | ||
* [Contract inclusion](./history_lib_reference.md#contract-inclusion) | ||
|
||
Using this library, you can check that specific notes or nullifiers were part of Aztec network state at specific blocks. This can be useful for things such as: | ||
|
||
* Verifying a minimum timestamp from a private context | ||
* Checking eligibility based on historical events (e.g. for an airdrop by proving that you owned a note) | ||
* Verifying historic ownership / relinquishing of assets | ||
* Proving existence of a value in public data tree at a given contract slot | ||
* Proving that a contract was deployed in a given block with some parameters | ||
|
||
**In this guide you will learn how to** | ||
* Prove a note was included in a specified block | ||
* Create a nullifier and prove it was not included in a specified block | ||
|
||
For a more extensive reference, go to [the reference page](./history_lib_reference.md). | ||
|
||
## 1. Import the `history` library from `aztec` | ||
|
||
```rust | ||
aztec::{ | ||
#include_code imports yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr raw | ||
} | ||
``` | ||
|
||
This imports all functions from the `history` library. You should only import the functions you will use in your contract. | ||
|
||
## 2. Create a note to prove inclusion of | ||
|
||
In general you will likely have the note you want to prove inclusion of. But if you are just experimenting you can create a note with a function like below: | ||
|
||
#include_code create_note yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
## 3. Get the note from the PXE | ||
|
||
Retrieve the note from the user's PXE. | ||
|
||
#include_code get_note_from_pxe yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
In this example, the user's notes are stored in a map called `private_values`. We retrieve this map, then select 1 note from it with the value of `1`. | ||
|
||
## 4. Prove that a note was included in a specified block | ||
|
||
To prove that a note existed in a specified block, call `prove_note_inclusion` as shown in this example: | ||
|
||
#include_code prove_note_inclusion yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
This function takes in 4 arguments: | ||
|
||
1. The note interface (`ValueNoteMethods`) | ||
2. The note (`maybe_note.unwrap_unchecked()`). Here, `unwrap_unchecked()` returns the inner value without asserting `self.is_some()` | ||
3. The block number | ||
4. Private context | ||
|
||
Note: for this to work, you will need to import `ValueNoteMethods` at the beginning of the contract: | ||
|
||
#include_code value_note_imports yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
This will only prove the note existed, not whether or not the note has been nullified. You can prove that a note existed and had not been nullified in a specified block by using `prove_note_validity` which takes the same arguments: | ||
|
||
#include_code prove_note_validity yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
## 5. Create a nullifier to prove inclusion of | ||
|
||
You can easily nullify a note like so: | ||
|
||
#include_code nullify_note yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
This function gets a note from the PXE like we did in [step 3](#3-get-the-note-from-the-pxe) and nullifies it with `remove()`. | ||
|
||
You can then compute this nullifier with `note.compute_nullifier(&mut context)`. | ||
|
||
## 6. Prove that a nullifier was included in a specified block | ||
|
||
Call `prove_nullifier_inclusion` like so: | ||
|
||
#include_code prove_nullifier_inclusion yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
This takes three arguments: | ||
1. The nullifier | ||
2. Block number | ||
3. Private context | ||
|
||
You can also prove that a nullifier was not included in a specified block by using `prove_nullifier_non_inclusion` which takes the same arguments: | ||
|
||
#include_code prove_nullifier_non_inclusion yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr rust | ||
|
||
## Prove contract inclusion, public value inclusion, and note commitment inclusion | ||
|
||
To see what else you can do with the `history` library, check out the [reference](./history_lib_reference.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.