Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: re-do partial notes #12391

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions docs/docs/aztec/concepts/advanced/storage/partial_notes.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
---
title: Partial Notes
title: Partial Notes [OUTDATED DOCS]
description: Describes how partial notes are used in Aztec
tags: [notes, storage]
sidebar_position: 4
---

:::warning OUTDATED DOCUMENTATION
This documentation is outdated and may not reflect the current state of the Aztec protocol. This is to be updated when tackling [this issue](https://github.com/AztecProtocol/aztec-packages/issues/12414).
TODO(#12414): UPDATE THIS
:::

Partial notes are a concept that allows users to commit to an encrypted value, and allows a counterparty to update that value without knowing the specific details of the encrypted value.

## Use cases
Expand Down Expand Up @@ -133,13 +138,7 @@ Then we just emit `P_a.x` and `P_b.x` as a note hashes, and we're done!

### Private Fee Payment Implementation

[`NoteInterface.nr`](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/aztec-nr/aztec/src/note/note_interface.nr) implements `compute_note_hiding_point`, which takes a note and computes the point "hides" it.

This is implemented by applying the `partial_note` attribute:

#include_code UintNote noir-projects/aztec-nr/uint-note/src/uint_note.nr rust

Those `G_x` are generators that are generated [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir/noir-repo/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs) (at the bottom of the file). Anyone can use them for separating different fields in a "partial note".
TODO(#12414): `setup_refund` no longer exists.

We can see the complete implementation of creating and completing partial notes in an Aztec contract in the `setup_refund` and `complete_refund` functions.

Expand Down
16 changes: 11 additions & 5 deletions noir-projects/aztec-nr/aztec/src/discovery/partial_notes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ pub unconstrained fn fetch_and_process_public_partial_note_completion_logs<Env>(
let maybe_log = get_log_by_tag(pending_partial_note.note_completion_log_tag);
if maybe_log.is_none() {
debug_log_format(
"Found no completion logs for partial note #{}",
[(*i) as Field],
"Found no completion logs for partial note with tag {}",
[pending_partial_note.note_completion_log_tag],
);
*i += 1 as u32;
// Note that we're not removing the pending partial note from the PXE DB, so we will continue searching
// for this tagged log when performing note discovery in the future until we either find it or the entry
// is somehow removed from the PXE DB.
} else {
debug_log_format("Completion log found for partial note #{}", [(*i) as Field]);
debug_log_format(
"Completion log found for partial note with tag {}",
[pending_partial_note.note_completion_log_tag],
);
let log = maybe_log.unwrap();

// Public logs have an extra field at the beginning with the contract address, which we use to verify
Expand Down Expand Up @@ -108,8 +111,8 @@ pub unconstrained fn fetch_and_process_public_partial_note_completion_logs<Env>(
);

debug_log_format(
"Discovered {0} notes for partial note {1}",
[discovered_notes.len() as Field, (*i) as Field],
"Discovered {0} notes for partial note with tag {1}",
[discovered_notes.len() as Field, pending_partial_note.note_completion_log_tag],
);

array::for_each_in_bounded_vec(
Expand Down Expand Up @@ -138,6 +141,9 @@ pub unconstrained fn fetch_and_process_public_partial_note_completion_logs<Env>(
// TODO(#11627): only remove the pending entry if we actually process a log that results in the note
// being completed.
pending_partial_notes.remove(*i);

// We don't increment `i` here, because CapsuleArray is contiguous and its `remove(...)` function
// shifts the elements to the left if the removed element is not the last element.
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/discovery/private_logs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ unconstrained fn destructure_log_plaintext(
// combined type ID. We can do this because the note type ID is only 7 bits long, and so use an 8th bit to
// distinguish private note logs and partial note logs.
// This abuses the fact that the encoding of both of these logs is extremely similar, and will need improving and
// more formalization once we introduce other disimilar log types, such as events. Ideally we'd be able to leverage
// enums and tagged unions to achieve this goal.
// more formalization once we introduce other dissimilar log types, such as events. Ideally we'd be able to
// leverage enums and tagged unions to achieve this goal.
let combined_type_id = log_plaintext.get(1);
let note_type_id = ((combined_type_id as u64) % 128) as Field;
let log_type_id = ((combined_type_id as u64) / 128) as Field;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod event;
pub mod note;
pub mod partial_note; // TEMPORARY!

This file was deleted.

2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/macros/dispatch/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub comptime fn generate_public_dispatch(m: Module) -> Quoted {
// No dispatch function if there are no public functions
quote {}
} else {
let ifs = ifs.push_back(quote { panic(f"Unknown selector") });
let ifs = ifs.push_back(quote { panic(f"Unknown selector {selector}") });
let dispatch = ifs.join(quote { });

let body = quote {
Expand Down
10 changes: 8 additions & 2 deletions noir-projects/aztec-nr/aztec/src/macros/events/mod.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::compute_event_selector;
use super::utils::{compute_event_selector, get_trait_impl_method};
use protocol_types::meta::generate_serialize_to_fields;

comptime fn generate_event_interface(s: StructDefinition) -> Quoted {
Expand All @@ -9,10 +9,16 @@ comptime fn generate_event_interface(s: StructDefinition) -> Quoted {

let event_type_id = compute_event_selector(s);

let from_field = get_trait_impl_method(
quote { crate::protocol_types::abis::event_selector::EventSelector }.as_type(),
quote { crate::protocol_types::traits::FromField },
quote { from_field },
);

quote {
impl aztec::event::event_interface::EventInterface<$content_len> for $name {
fn get_event_type_id() -> aztec::protocol_types::abis::event_selector::EventSelector {
aztec::protocol_types::traits::FromField::from_field($event_type_id)
$from_field($event_type_id)
}

fn emit<Env>(self, _emit: fn[Env](Self) -> ()) {
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,15 @@ comptime fn create_init_check(f: FunctionDefinition) -> Quoted {

/// Injects a call to `aztec::discovery::discover_new_notes`, causing for new notes to be added to PXE and made
/// available for the current execution.
comptime fn create_note_discovery_call() -> Quoted {
pub(crate) comptime fn create_note_discovery_call() -> Quoted {
quote {
/// Safety: note discovery returns nothing and is performed solely for its side-effects. It is therefore always
/// safe to call.
unsafe {
dep::aztec::discovery::discover_new_notes(
context.this_address(),
_compute_note_hash_and_nullifier,
)
);
};
}
}
11 changes: 9 additions & 2 deletions noir-projects/aztec-nr/aztec/src/macros/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod notes;
pub mod storage;
pub mod events;

use functions::{stub_registry, utils::transform_unconstrained};
use functions::{stub_registry, utils::{create_note_discovery_call, transform_unconstrained}};
use notes::{generate_note_export, NOTES};
use storage::STORAGE_LAYOUT_NAME;

Expand Down Expand Up @@ -255,6 +255,7 @@ comptime fn generate_contract_library_method_compute_note_hash_and_nullifier() -
_storage_slot: Field,
_note_type_id: Field,
_contract_address: aztec::protocol_types::address::AztecAddress,
_nonce: Field,
) -> Option<aztec::discovery::NoteHashAndNullifier> {
panic(f"This contract does not use private notes")
}
Expand Down Expand Up @@ -328,9 +329,15 @@ comptime fn generate_note_exports() -> Quoted {
}

comptime fn generate_sync_notes() -> Quoted {
let note_discovery_call = create_note_discovery_call();
quote {
unconstrained fn sync_notes() {
aztec::oracle::note_discovery::sync_notes();
// Because this unconstrained function is injected after the contract is processed by the macros, it'll not
// be modified by the macros that alter unconstrained functions. As such, we need to manually inject the
// unconstrained execution context since it will not be available otherwise.
let context = dep::aztec::context::unconstrained_context::UnconstrainedContext::new();

$note_discovery_call
}
}
}
Loading
Loading