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 44b70c1
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 261 deletions.
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 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 {
fn get_event_type_id(self) -> FunctionSelector {
FunctionSelector::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
9 changes: 5 additions & 4 deletions noir-projects/aztec-nr/aztec/src/event/event_interface.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::context::PrivateContext;
use crate::note::note_header::NoteHeader;
use dep::protocol_types::{grumpkin_point::GrumpkinPoint, abis::function_selector::FunctionSelector};

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) -> FunctionSelector;
}
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
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 44b70c1

Please sign in to comment.