-
Notifications
You must be signed in to change notification settings - Fork 317
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
refactor: note emission #7003
Merged
Merged
refactor: note emission #7003
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
31a4c43
refactor: note emission
LHerskind 620ea32
refactor: add note emission wrapper
LHerskind 8e2fda0
feat: replace note emits
LHerskind 03bd399
chore: rename
LHerskind 32d9af1
test: fix private mutable test
LHerskind 9b7da33
chore: fix docs
LHerskind 6d64e2f
chore: fix boxes
LHerskind 27fe351
chore: address comments
LHerskind 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 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ mod header; | |
mod incoming_body; | ||
mod outgoing_body; | ||
mod payload; | ||
mod encrypted_note_emission; |
57 changes: 57 additions & 0 deletions
57
noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr
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,57 @@ | ||
use crate::{ | ||
context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface}, | ||
encrypted_logs::payload::compute_encrypted_note_log, oracle::logs_traits::LensForEncryptedLog | ||
}; | ||
use dep::protocol_types::{ | ||
address::AztecAddress, grumpkin_point::GrumpkinPoint, abis::note_hash::NoteHash, | ||
constants::MAX_NEW_NOTE_HASHES_PER_CALL, utils::arrays::find_index | ||
}; | ||
|
||
fn emit_with_keys<Note, N, NB, M>( | ||
context: &mut PrivateContext, | ||
note: Note, | ||
ovpk: GrumpkinPoint, | ||
ivpk: GrumpkinPoint | ||
) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> { | ||
let note_header = note.get_header(); | ||
let note_hash_counter = note_header.note_hash_counter; | ||
let storage_slot = note_header.storage_slot; | ||
|
||
let note_exists_index = find_index( | ||
context.new_note_hashes.storage, | ||
|n: NoteHash| n.counter == note_hash_counter | ||
); | ||
assert( | ||
note_exists_index as u32 != MAX_NEW_NOTE_HASHES_PER_CALL, "Can only emit a note log for an existing note." | ||
); | ||
|
||
let contract_address: AztecAddress = context.this_address(); | ||
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash()); | ||
|
||
let encrypted_log: [u8; M] = compute_encrypted_note_log(contract_address, storage_slot, ovsk_app, ovpk, ivpk, note); | ||
|
||
context.emit_raw_note_log(note_hash_counter, encrypted_log); | ||
} | ||
|
||
pub fn encode_and_encrypt<Note, N, NB, M>( | ||
context: &mut PrivateContext, | ||
ov: AztecAddress, | ||
iv: AztecAddress | ||
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> { | ||
| e: NoteEmission<Note> | { | ||
let header = context.get_header(); | ||
let ovpk = header.get_ovpk_m(context, ov); | ||
let ivpk = header.get_ivpk_m(context, iv); | ||
emit_with_keys(context, e.note, ovpk, ivpk); | ||
} | ||
} | ||
|
||
pub fn encode_and_encrypt_with_keys<Note, N, NB, M>( | ||
context: &mut PrivateContext, | ||
ovpk: GrumpkinPoint, | ||
ivpk: GrumpkinPoint | ||
) -> fn[(&mut PrivateContext, GrumpkinPoint, GrumpkinPoint)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> { | ||
| e: NoteEmission<Note> | { | ||
emit_with_keys(context, e.note, ovpk, ivpk); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ mod note_header; | |
mod note_interface; | ||
mod note_viewer_options; | ||
mod utils; | ||
mod note_emission; |
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
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,45 @@ | ||
/** | ||
* A note emission struct containing the information required for emitting a note. | ||
* The exact `emit` logic is passed in by the application code | ||
*/ | ||
struct NoteEmission<Note> { | ||
note: Note | ||
} | ||
|
||
impl<Note> NoteEmission<Note> { | ||
pub fn new(note: Note) -> Self { | ||
Self { note } | ||
} | ||
|
||
pub fn emit<Env>(self, _emit: fn[Env](Self) -> ()) { | ||
_emit(self); | ||
} | ||
|
||
pub fn discard(self) {} | ||
} | ||
|
||
/** | ||
* A struct wrapping note emission in `Option<T>`. | ||
* This is the struct provided to application codes, which can be used to emit | ||
* only when a note was actually inserted. | ||
* It is fairly common to have cases where a function conditionally inserts, | ||
* and this allows us to keep the same API for emission in both cases (e.g. inserting | ||
* a change note in a token's transfer function only when there is "change" left). | ||
*/ | ||
struct OuterNoteEmission<Note> { | ||
emission: Option<NoteEmission<Note>>, | ||
} | ||
|
||
impl<Note> OuterNoteEmission<Note> { | ||
pub fn new(emission: Option<NoteEmission<Note>>) -> Self { | ||
Self { emission } | ||
} | ||
|
||
pub fn emit<Env>(self, _emit: fn[Env](NoteEmission<Note>) -> ()) { | ||
if self.emission.is_some() { | ||
_emit(self.emission.unwrap()); | ||
} | ||
} | ||
|
||
pub fn discard(self) {} | ||
} |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super nice!