-
Notifications
You must be signed in to change notification settings - Fork 592
feat: add note metadata #12240
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
Merged
Merged
feat: add note metadata #12240
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
603c5d8
Add note metadata
nventuro 70f483d
Some docs
nventuro e999f39
Apply renamings
nventuro 622e89b
Fix test
nventuro 7045db6
Remove note hash counter
nventuro 8aea1a3
Fix from trait
nventuro e8ee039
Add comment
nventuro fe2298a
Fix private exec test
nventuro a8bbad4
apply review comments
nventuro ae7cfb6
Merge branch 'master' into nv/note-metadata
nventuro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,187 @@ | ||
| use protocol_types::traits::Serialize; | ||
|
|
||
| // There's temporarily quite a bit of boilerplate here since Noir does not yet support enums. This file will eventually | ||
| // be simplified into something closer to: | ||
| // | ||
| // pub enum NoteMetadata { | ||
| // Transient{ note_hash_counter: u32 }, | ||
| // TransientNonRevertible{ note_hash_counter: u32, nonce: Field }, | ||
| // Settled{ nonce: Field }, | ||
| // } | ||
| // | ||
| // For now, we have `NoteMetadata` acting as a sort of tagged union. | ||
|
|
||
| /// The different stages in which a note can be. | ||
| /// - TRANSIENT_REVERTIBLE: a note that was created in the transaction that is currently being executed during the | ||
| /// revertible phase | ||
| /// - TRANSIENT_NON_REVERTIBLE: same as transient, except the note was created during the non-revertible phase | ||
| /// - SETTLED: a note that was created in a prior transaction and is therefore in the note hash tree | ||
| struct NoteStageEnum { | ||
| TRANSIENT_REVERTIBLE: u8, | ||
| TRANSIENT_NON_REVERTIBLE: u8, | ||
| SETTLED: u8, | ||
| } | ||
|
|
||
| global NoteStage: NoteStageEnum = | ||
| NoteStageEnum { TRANSIENT_REVERTIBLE: 1, TRANSIENT_NON_REVERTIBLE: 2, SETTLED: 3 }; | ||
|
|
||
| /// The metadata required to both prove a note's existence and destroy it, by computing the correct note hash for kernel | ||
| /// read requests, as well as the correct nullifier to avoid double-spends. | ||
| /// | ||
| /// This represents a note in any of the tree valid stages (transient revertible, transient non-revertible or settled), | ||
| /// in order to access the underlying fields callers must first find the appropriate stage (e.g. via `is_settled()`) and | ||
| /// then convert this into the appropriate type (e.g. via `to_settled()`). | ||
| #[derive(Eq, Serialize)] | ||
| pub struct NoteMetadata { | ||
| stage: u8, | ||
| // todo: replace with a boolean | ||
| maybe_note_hash_counter: u32, | ||
| maybe_nonce: Field, | ||
| } | ||
|
|
||
| impl NoteMetadata { | ||
| /// Constructs a `NoteMetadata` object from optional note hash and nullifiers. Both a zero note hash counter and a | ||
nventuro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// zero nonce are invalid, so those are used to signal non-existent values. | ||
| pub fn from_raw_data(maybe_note_hash_counter: u32, maybe_nonce: Field) -> Self { | ||
| if maybe_note_hash_counter != 0 { | ||
| if maybe_nonce == 0 { | ||
| Self { stage: NoteStage.TRANSIENT_REVERTIBLE, maybe_note_hash_counter, maybe_nonce } | ||
| } else { | ||
| Self { | ||
| stage: NoteStage.TRANSIENT_NON_REVERTIBLE, | ||
| maybe_note_hash_counter, | ||
| maybe_nonce, | ||
| } | ||
| } | ||
| } else if maybe_nonce != 0 { | ||
| Self { stage: NoteStage.SETTLED, maybe_note_hash_counter, maybe_nonce } | ||
| } else { | ||
| panic( | ||
| f"Note has no note hash counter nor nonce - existence cannot be proven", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// Returns true if the note is transient and revertible, i.e. if it's been created in the current transaction | ||
| /// during the revertible phase. | ||
| /// | ||
| /// To test if the note is transient consider doing `!metadata.is_settled()`. | ||
nventuro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn is_transient_revertible(self) -> bool { | ||
| self.stage == NoteStage.TRANSIENT_REVERTIBLE | ||
| } | ||
|
|
||
| /// Returns true if the note is transient and non-revertible, i.e. if it's been created in the current transaction | ||
| /// during the non-revertible phase. | ||
| /// | ||
| /// To test if the note is transient consider doing `!metadata.is_settled()`. | ||
nventuro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub fn is_transient_non_revertible(self) -> bool { | ||
| self.stage == NoteStage.TRANSIENT_NON_REVERTIBLE | ||
| } | ||
|
|
||
| /// Returns true if the note is settled, i.e. if it's been created in a prior transaction and is therefore in the | ||
| /// note hash tree. | ||
| pub fn is_settled(self) -> bool { | ||
| self.stage == NoteStage.SETTLED | ||
| } | ||
|
|
||
| /// Asserts that the metadata is that of a transient revertible note and converts it accordingly. | ||
| pub fn to_transient_revertible(self) -> TransientRevertibleNoteMetadata { | ||
| assert_eq(self.stage, NoteStage.TRANSIENT_REVERTIBLE); | ||
| TransientRevertibleNoteMetadata { note_hash_counter: self.maybe_note_hash_counter } | ||
| } | ||
|
|
||
| /// Asserts that the metadata is that of a transient non-revertible note and converts it accordingly. | ||
| pub fn to_transient_non_revertible(self) -> TransientNonRevertibleNoteMetadata { | ||
| assert_eq(self.stage, NoteStage.TRANSIENT_NON_REVERTIBLE); | ||
| TransientNonRevertibleNoteMetadata { | ||
| note_hash_counter: self.maybe_note_hash_counter, | ||
| nonce: self.maybe_nonce, | ||
| } | ||
| } | ||
|
|
||
| /// Asserts that the metadata is that of a settled note and converts it accordingly. | ||
| pub fn to_settled(self) -> SettledNoteMetadata { | ||
| assert_eq(self.stage, NoteStage.SETTLED); | ||
| SettledNoteMetadata { nonce: self.maybe_nonce } | ||
| } | ||
| } | ||
|
|
||
| impl From<TransientRevertibleNoteMetadata> for NoteMetadata { | ||
| fn from(value: TransientRevertibleNoteMetadata) -> Self { | ||
| NoteMetadata::from_raw_data(value.note_hash_counter(), std::mem::zeroed()) | ||
| } | ||
| } | ||
|
|
||
| impl From<TransientNonRevertibleNoteMetadata> for NoteMetadata { | ||
| fn from(value: TransientNonRevertibleNoteMetadata) -> Self { | ||
| NoteMetadata::from_raw_data(value.note_hash_counter(), value.nonce()) | ||
| } | ||
| } | ||
|
|
||
| impl From<SettledNoteMetadata> for NoteMetadata { | ||
| fn from(value: SettledNoteMetadata) -> Self { | ||
| NoteMetadata::from_raw_data(std::mem::zeroed(), value.nonce()) | ||
| } | ||
| } | ||
|
|
||
| /// The metadata required to both prove a note's existence and destroy it, by computing the correct note hash for kernel | ||
| /// read requests, as well as the correct nullifier to avoid double-spends. | ||
| /// | ||
| /// This represents a transient revertible note, i.e. a note that was created in the transaction that is currently being | ||
| /// executed during the revertible phase. | ||
| pub struct TransientRevertibleNoteMetadata { | ||
| note_hash_counter: u32, | ||
| } | ||
|
|
||
| /// The metadata required to both prove a note's existence and destroy it, by computing the correct note hash for kernel | ||
| /// read requests, as well as the correct nullifier to avoid double-spends. | ||
| /// | ||
| /// This represents a transient non-revertible note, i.e. a note that was created in a prior transaction and is | ||
| /// therefore in the note hash tree. | ||
nventuro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| impl TransientRevertibleNoteMetadata { | ||
| pub fn new(note_hash_counter: u32) -> Self { | ||
| Self { note_hash_counter } | ||
| } | ||
|
|
||
| pub fn note_hash_counter(self) -> u32 { | ||
| self.note_hash_counter | ||
| } | ||
| } | ||
|
|
||
| pub struct TransientNonRevertibleNoteMetadata { | ||
| note_hash_counter: u32, | ||
| nonce: Field, | ||
| } | ||
|
|
||
| impl TransientNonRevertibleNoteMetadata { | ||
| pub fn new(note_hash_counter: u32, nonce: Field) -> Self { | ||
| Self { note_hash_counter, nonce } | ||
| } | ||
|
|
||
| pub fn note_hash_counter(self) -> u32 { | ||
| self.note_hash_counter | ||
| } | ||
|
|
||
| pub fn nonce(self) -> Field { | ||
| self.nonce | ||
| } | ||
| } | ||
|
|
||
| /// The metadata required to both prove a note's existence and destroy it, by computing the correct note hash for kernel | ||
| /// read requests, as well as the correct nullifier to avoid double-spends. | ||
| /// | ||
| /// This represents a settled note, i.e. a note that was created in the transaction that is currently | ||
| /// being executed during the non-revertible phase. | ||
nventuro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pub struct SettledNoteMetadata { | ||
| nonce: Field, | ||
| } | ||
|
|
||
| impl SettledNoteMetadata { | ||
| pub fn new(nonce: Field) -> Self { | ||
| Self { nonce } | ||
| } | ||
|
|
||
| pub fn nonce(self) -> Field { | ||
| self.nonce | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,34 +1,27 @@ | ||
| use protocol_types::{address::AztecAddress, traits::Serialize, utils::arrays::array_concat}; | ||
| use crate::note::note_metadata::NoteMetadata; | ||
| use protocol_types::{ | ||
| address::AztecAddress, | ||
| traits::{Serialize, ToField}, | ||
| utils::arrays::array_concat, | ||
| }; | ||
|
|
||
| /// A container of a note and the metadata required to constrain its existence, regardless of whether the note is | ||
| /// historical (created in a previous transaction) or transient (created in the current transaction). | ||
| #[derive(Eq)] | ||
| pub struct RetrievedNote<NOTE> { | ||
| pub note: NOTE, | ||
| pub contract_address: AztecAddress, | ||
| pub nonce: Field, | ||
| pub note_hash_counter: u32, // a note_hash_counter of 0 means non-transient | ||
| pub metadata: NoteMetadata, | ||
| } | ||
|
|
||
| impl<NOTE> Eq for RetrievedNote<NOTE> | ||
| where | ||
| NOTE: Eq, | ||
| { | ||
| fn eq(self, other: Self) -> bool { | ||
| (self.note == other.note) | ||
| & (self.contract_address == other.contract_address) | ||
| & (self.nonce == other.nonce) | ||
| & (self.note_hash_counter == other.note_hash_counter) | ||
| } | ||
| } | ||
|
|
||
| impl<NOTE, let N: u32> Serialize<N + 3> for RetrievedNote<NOTE> | ||
| impl<NOTE, let N: u32> Serialize<N + 1 + 3> for RetrievedNote<NOTE> | ||
| where | ||
| NOTE: Serialize<N>, | ||
| { | ||
| fn serialize(self) -> [Field; N + 3] { | ||
| fn serialize(self) -> [Field; N + 1 + 3] { | ||
| array_concat( | ||
| self.note.serialize(), | ||
| [self.contract_address.to_field(), self.nonce, self.note_hash_counter as Field], | ||
| array_concat(self.note.serialize(), [self.contract_address.to_field()]), | ||
| self.metadata.serialize(), | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.