Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Jun 18, 2024
1 parent 7f62a90 commit dbc7b29
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 264 deletions.
12 changes: 6 additions & 6 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<M> EncryptedLogIncomingBody<M> {
}

pub fn from_event<T>(event: T, randomness: Field) -> Self where T: EventInterface<M> {
let mut plaintext = event.to_be_bytes(randomness);
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,15 +155,15 @@ mod test {
global TEST_EVENT_BYTES_LEN = 32 * 3 + 64;

impl EventInterface<TEST_EVENT_BYTES_LEN> for TestEvent {
fn _selector(self) -> FunctionSelector {
FunctionSelector::from_signature("TestEvent(Field,Field,Field)")
fn get_event_type_id(self) -> 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 = self.get_event_type_id().to_field().to_be_bytes(32);

for i in 0..32 {
buffer[i] = randomness_bytes[i];
Expand Down
11 changes: 6 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,10 @@
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<M> {
fn private_to_be_bytes(self, randomness: Field) -> [u8; M];
// More than one generic breaks this
// fn to_be_bytes(self) -> [u8; M];
fn get_event_type_id(self) -> 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
Expand Up @@ -14,56 +14,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
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 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 }
}
}
15 changes: 6 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,8 @@ use transforms::{
contract_interface::{
generate_contract_interface, stub_function, update_fn_signatures_in_contract_interface,
},
events::{generate_selector_impl, transform_events},
event_interface::{generate_event_interface_impl, transform_event_abi},
// events::transform_events,
functions::{
check_for_public_args, export_fn_abi, transform_function, transform_unconstrained,
},
Expand Down Expand Up @@ -72,6 +73,7 @@ fn transform(
}
}

generate_event_interface_impl(&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 +103,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 +217,9 @@ fn transform_hir(
context: &mut HirContext,
) -> Result<(), (AztecMacroError, FileId)> {
if has_aztec_dependency(crate_id, context) {
transform_events(crate_id, context)?;
// The old event transformer is commented out but still kept for reference
// 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

0 comments on commit dbc7b29

Please sign in to comment.