Skip to content
Merged
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
38 changes: 30 additions & 8 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ impl<M> EncryptedLogIncomingBody<M> {
EncryptedLogIncomingBody { plaintext }
}

pub fn from_event<T>(event: T, randomness: Field) -> Self where T: EventInterface<M> {
let mut plaintext = event.to_be_bytes(randomness);
pub fn from_event<T, MR>(event: T, randomness: Field) -> Self where T: EventInterface<M, MR> {
let mut plaintext = event.private_to_be_bytes(randomness);
EncryptedLogIncomingBody { plaintext }
}

Expand All @@ -38,7 +38,7 @@ mod test {
use dep::protocol_types::{
address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, traits::Serialize,
abis::function_selector::FunctionSelector
abis::event_selector::EventSelector
};

use crate::{
Expand Down Expand Up @@ -155,17 +155,18 @@ mod test {

global TEST_EVENT_LEN: Field = 3;
global TEST_EVENT_BYTES_LEN = 32 * 3 + 64;
global TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS = 32 * 3 + 32;

impl EventInterface<TEST_EVENT_BYTES_LEN> for TestEvent {
fn _selector(self) -> FunctionSelector {
FunctionSelector::from_signature("TestEvent(Field,Field,Field)")
impl EventInterface<TEST_EVENT_BYTES_LEN, TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS> for TestEvent {
fn get_event_type_id() -> EventSelector {
EventSelector::from_signature("TestEvent(Field,Field,Field)")
}

fn to_be_bytes(self, randomness: Field) -> [u8; TEST_EVENT_BYTES_LEN] {
fn private_to_be_bytes(self, randomness: Field) -> [u8; TEST_EVENT_BYTES_LEN] {
let mut buffer: [u8; TEST_EVENT_BYTES_LEN] = [0; TEST_EVENT_BYTES_LEN];

let randomness_bytes = randomness.to_be_bytes(32);
let event_type_id_bytes = self._selector().to_field().to_be_bytes(32);
let event_type_id_bytes = TestEvent::get_event_type_id().to_field().to_be_bytes(32);

for i in 0..32 {
buffer[i] = randomness_bytes[i];
Expand All @@ -183,6 +184,27 @@ mod test {

buffer
}

fn to_be_bytes(self) -> [u8; TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS] {
let mut buffer: [u8; TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS] = [0; TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS];

let event_type_id_bytes = TestEvent::get_event_type_id().to_field().to_be_bytes(32);

for i in 0..32 {
buffer[i] = event_type_id_bytes[i];
}

let serialized_event = self.serialize();

for i in 0..serialized_event.len() {
let bytes = serialized_event[i].to_be_bytes(32);
for j in 0..32 {
buffer[32 + i * 32 + j] = bytes[j];
}
}

buffer
}
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions noir-projects/aztec-nr/aztec/src/event/event_interface.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::context::PrivateContext;
use crate::note::note_header::NoteHeader;
use dep::protocol_types::{grumpkin_point::GrumpkinPoint, abis::function_selector::FunctionSelector};
use dep::protocol_types::{grumpkin_point::GrumpkinPoint, abis::event_selector::EventSelector};

trait EventInterface<N> {
// Should be autogenerated by the #[aztec(event)] macro unless it is overridden by a custom implementation
fn _selector(self) -> FunctionSelector;
fn to_be_bytes(self, randomness: Field) -> [u8; N];
trait EventInterface<NB, MB> {
fn private_to_be_bytes(self, randomness: Field) -> [u8; NB];
fn to_be_bytes(self) -> [u8; MB];
fn get_event_type_id() -> EventSelector;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@ contract Crowdfunding {

#[aztec(event)]
struct WithdrawalProcessed {
who: AztecAddress,
amount: u64,
}

impl Serialize<2> for WithdrawalProcessed {
fn serialize(self: Self) -> [Field; 2] {
[self.who.to_field(), self.amount as Field]
}
who: Field,
amount: Field,
}

// docs:start:storage
Expand Down Expand Up @@ -103,7 +97,7 @@ contract Crowdfunding {
Token::at(storage.donation_token.read_private()).transfer(operator_address, amount as Field).call(&mut context);

// 3) Emit an unencrypted event so that anyone can audit how much the operator has withdrawn
let event = WithdrawalProcessed { amount, who: operator_address };
let event = WithdrawalProcessed { amount: amount as Field, who: operator_address.to_field() };
context.emit_unencrypted_log(event.serialize());
}
// docs:end:operator-withdrawals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
contract TestLog {
use dep::aztec::prelude::PrivateSet;
use dep::aztec::protocol_types::{
traits::Serialize, grumpkin_point::GrumpkinPoint, grumpkin_private_key::GrumpkinPrivateKey,
abis::function_selector::FunctionSelector
};
use dep::aztec::protocol_types::{traits::Serialize, grumpkin_point::GrumpkinPoint, grumpkin_private_key::GrumpkinPrivateKey};
use dep::value_note::value_note::ValueNote;
use dep::aztec::encrypted_logs::incoming_body::EncryptedLogIncomingBody;
use dep::aztec::event::event_interface::EventInterface;
Expand All @@ -14,56 +11,12 @@ contract TestLog {
value1: Field,
}

// This should be autogenerated by the macros
global EXAMPLE_EVENT_0_BYTES_LEN = 32 * 2 + 32 + 32;

impl EventInterface<EXAMPLE_EVENT_0_BYTES_LEN> for ExampleEvent0 {
fn _selector(self) -> FunctionSelector {
FunctionSelector::from_signature("TestEvent(Field,Field,Field)")
}

fn to_be_bytes(self, randomness: Field) -> [u8; EXAMPLE_EVENT_0_BYTES_LEN] {
let mut buffer: [u8; EXAMPLE_EVENT_0_BYTES_LEN] = [0; EXAMPLE_EVENT_0_BYTES_LEN];

let randomness_bytes = randomness.to_be_bytes(32);
let event_type_id_bytes = self._selector().to_field().to_be_bytes(32);

for i in 0..32 {
buffer[i] = randomness_bytes[i];
buffer[32 + i] = event_type_id_bytes[i];
}

let serialized_event = self.serialize();

for i in 0..serialized_event.len() {
let bytes = serialized_event[i].to_be_bytes(32);
for j in 0..32 {
buffer[64 + i * 32 + j] = bytes[j];
}
}

buffer
}
}

#[aztec(event)]
struct ExampleEvent1 {
value2: Field,
value3: Field,
}

impl Serialize<2> for ExampleEvent0 {
fn serialize(self) -> [Field; 2] {
[self.value0, self.value1]
}
}

impl Serialize<2> for ExampleEvent1 {
fn serialize(self) -> [Field; 2] {
[self.value2, self.value3]
}
}

#[aztec(storage)]
struct Storage {
example_set: PrivateSet<ValueNote>,
Expand Down Expand Up @@ -107,20 +60,24 @@ contract TestLog {
let msg_sender_ivpk_m = header.get_ivpk_m(&mut context, context.msg_sender());
let msg_sender_ovpk_m = header.get_ovpk_m(&mut context, context.msg_sender());

let event0 = ExampleEvent0 { value0: preimages[0], value1: preimages[1] };

context.encrypt_and_emit_event(
randomness[0],
ExampleEvent0::selector().to_field(),
ExampleEvent0::get_event_type_id().to_field(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the constrained encryption we should be able to ged rid of this 👀 should be nice

msg_sender_ovpk_m,
msg_sender_ivpk_m,
ExampleEvent0 { value0: preimages[0], value1: preimages[1] }.serialize()
event0.serialize()
);

let event1 = ExampleEvent1 { value2: preimages[2], value3: preimages[3] };

context.encrypt_and_emit_event(
randomness[1],
ExampleEvent1::selector().to_field(),
ExampleEvent1::get_event_type_id().to_field(),
msg_sender_ovpk_m,
msg_sender_ivpk_m,
ExampleEvent1 { value2: preimages[2], value3: preimages[3] }.serialize()
event1.serialize()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod append_only_tree_snapshot;

mod contract_class_function_leaf_preimage;

mod event_selector;
mod function_selector;
mod function_data;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::utils::field::field_from_bytes;
use dep::std::cmp::Eq;
use crate::traits::{Serialize, Deserialize, FromField, ToField, Empty};

global SELECTOR_SIZE = 4;

struct EventSelector {
// 1st 4-bytes (big-endian leftmost) of abi-encoding of an event.
inner: u32,
}

impl Eq for EventSelector {
fn eq(self, other: EventSelector) -> bool {
other.inner == self.inner
}
}

impl Serialize<1> for EventSelector {
fn serialize(self: Self) -> [Field; 1] {
[self.inner as Field]
}
}

impl Deserialize<1> for EventSelector {
fn deserialize(fields: [Field; 1]) -> Self {
Self {
inner: fields[0] as u32
}
}
}

impl FromField for EventSelector {
fn from_field(field: Field) -> Self {
Self { inner: field as u32 }
}
}

impl ToField for EventSelector {
fn to_field(self) -> Field {
self.inner as Field
}
}

impl Empty for EventSelector {
fn empty() -> Self {
Self { inner: 0 as u32 }
}
}

impl EventSelector {
pub fn from_u32(value: u32) -> Self {
Self { inner: value }
}

pub fn from_signature<N>(signature: str<N>) -> Self {
let bytes = signature.as_bytes();
let hash = dep::std::hash::keccak256(bytes, bytes.len() as u32);

let mut selector_be_bytes = [0; SELECTOR_SIZE];
for i in 0..SELECTOR_SIZE {
selector_be_bytes[i] = hash[i];
}

EventSelector::from_field(field_from_bytes(selector_be_bytes, true))
}

pub fn zero() -> Self {
Self { inner: 0 }
}
}
12 changes: 3 additions & 9 deletions noir/noir-repo/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use transforms::{
contract_interface::{
generate_contract_interface, stub_function, update_fn_signatures_in_contract_interface,
},
events::{generate_selector_impl, transform_events},
events::{generate_event_impls, transform_event_abi},
functions::{
check_for_public_args, export_fn_abi, transform_function, transform_unconstrained,
},
Expand Down Expand Up @@ -72,6 +72,7 @@ fn transform(
}
}

generate_event_impls(&mut ast).map_err(|err| (err.into(), file_id))?;
generate_note_interface_impl(&mut ast).map_err(|err| (err.into(), file_id))?;

Ok(ast)
Expand Down Expand Up @@ -101,13 +102,6 @@ fn transform_module(
generate_storage_layout(module, storage_struct_name.clone(), module_name)?;
}

for structure in module.types.iter_mut() {
if structure.attributes.iter().any(|attr| is_custom_attribute(attr, "aztec(event)")) {
module.impls.push(generate_selector_impl(structure));
has_transformed_module = true;
}
}

let has_initializer = module.functions.iter().any(|func| {
func.def
.attributes
Expand Down Expand Up @@ -222,7 +216,7 @@ fn transform_hir(
context: &mut HirContext,
) -> Result<(), (AztecMacroError, FileId)> {
if has_aztec_dependency(crate_id, context) {
transform_events(crate_id, context)?;
transform_event_abi(crate_id, context)?;
inject_compute_note_hash_and_optionally_a_nullifier(crate_id, context)?;
assign_storage_slots(crate_id, context)?;
inject_note_exports(crate_id, context)?;
Expand Down
Loading