-
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.
Adds a `NoteEmission` struct that is returned when new notes are created. The struct is the note and a `NoteHashCount` that can be used when emitting the note to ensure that squashing is possible. There are fairly large change to return this struct throughout, and at the same time getting rid of the `broadcast` that we have currently been using whenever the `create_note` life cycle have been encountered. This also means that keys are not fed into the lifecycle! Meaning that the aztec nr libraries are generally getting less contaminated with log decisions. Instead of always forcing an emit, the returned value can now be emitted using the `emit` which can be provided a function. For ease of use, we have also added `encrypted_note_emission.nr` which have a `encode_and_encrypt` and `encode_and_encrypt_with_keys` functions. These functions can be used along with the emit to encrypt the note and then emit it. This allows us to execute it as: ```rust let amount = U128::from_integer(amount); storage.balances.sub(from, amount).emit(encode_and_encrypt(&mut context, from, from)); storage.balances.add(to, amount).emit(encode_and_encrypt(&mut context, from, to)); ``` **TODO:** - [x] Remove `broadcast` from note interface - [x] Remove `encrypt_and_emit_note` from private context --- There are some build issues that need to be ironed out, but I think that the flow is nicer than before as it is more clear when things are emitted, and for cases where we are doing a `get_note` and then `replace` it is easier for us to not emit the read but only the update afterwards.
- Loading branch information
Showing
48 changed files
with
297 additions
and
374 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
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.