Skip to main content

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.

View the History lib reference here.

History library

The history library allows you to prove any of the following at a given block height before the current height:

  • Note inclusion
  • Nullifier inclusion
  • Note validity
  • Existence of public value
  • 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 knew the nullifier key for 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.

1. Import the history library from aztec

aztec::{
// Imports are not needed as inclusion / non_inclusion proofs are accessible on the header.
}

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:

create_note
// Creates a value note owned by `owner`.
#[aztec(private)]
fn create_note(owner: AztecAddress, value: Field) {
let owner_private_values = storage.private_values.at(owner);
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner);

let mut note = ValueNote::new(value, owner_npk_m_hash);
owner_private_values.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner));
}
Source code: noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr#L30-L41

3. Get the note from the PXE

Retrieve the note from the user's PXE.

get_note_from_pxe
// 1) Get the note from PXE.
let private_values = storage.private_values.at(owner);
let mut options = NoteGetterOptions::new();
options = options.set_limit(1);
if (nullified) {
options = options.set_status(NoteStatus.ACTIVE_OR_NULLIFIED);
}
let note = private_values.get_notes(options).get_unchecked(0);
Source code: noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr#L50-L59

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_at as shown in this example:

prove_note_inclusion
header.prove_note_inclusion(note);
Source code: noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr#L67-L69

This function takes in 3 arguments:

  1. The note (maybe_note.unwrap_unchecked()). Here, unwrap_unchecked() returns the inner value without asserting self.is_some()
  2. The block number
  3. Private context

This will only prove the note existed at the specific block number, 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_at which takes the same arguments:

prove_note_validity
header.prove_note_validity(note, &mut context);
Source code: noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr#L143-L145

5. Create a nullifier to prove inclusion of

You can easily nullify a note like so:

nullify_note
#[aztec(private)]
fn nullify_note(owner: AztecAddress) {
let private_values = storage.private_values.at(owner);
let mut options = NoteGetterOptions::new();
options = options.set_limit(1);
let notes = private_values.get_notes(options);
let note = notes.get(0);

private_values.remove(note);
}
Source code: noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr#L148-L159

This function gets a note from the PXE like we did in step 3 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_at like so:

prove_nullifier_inclusion
header.prove_nullifier_inclusion(nullifier);
Source code: noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr#L175-L177

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_not_included_at which takes the same arguments.

Prove contract inclusion, public value inclusion, and use current state in lookups

To see what else you can do with the history library, check out the reference.