Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 23, 2024
1 parent fbefe7e commit a0b45b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 54 deletions.
48 changes: 26 additions & 22 deletions noir-projects/aztec-nr/aztec/src/macros/notes/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ comptime fn generate_finalization_payload(
impl $finalization_payload_name {
fn new(mut self, hiding_point: aztec::protocol_types::point::Point, $args) -> $finalization_payload_name {
$aux_vars_for_serialization
self.log = [$fields];
self.public_values = [$fields];

$msm_aux_vars
let finalization_hiding_point = std::embedded_curve_ops::multi_scalar_mul(
Expand All @@ -601,35 +601,39 @@ comptime fn generate_finalization_payload(
self.note_hash = finalization_hiding_point.x;
self
}
}

fn get_log(self, context: &mut PublicContext, slot: Field) -> [u8; $finalization_log_byte_length] {
let setup_log_fields: [Field; $setup_log_field_length] = context.storage_read(slot);
let setup_log: [u8; $setup_log_byte_length] = aztec::utils::bytes::fields_to_bytes(setup_log_fields);
fn get_log(self, context: &mut aztec::prelude::PublicContext, slot: Field) -> [u8; $finalization_log_byte_length] {
let setup_log_fields: [Field; $setup_log_field_length] = context.storage_read(slot);
let setup_log: [u8; $setup_log_byte_length] = aztec::utils::bytes::fields_to_bytes(setup_log_fields);

// First byte should be zero as the number of public values is populated in public
assert(setup_log[0] == 0, "Non-zero first byte");
// First byte should be zero as the number of public values is populated in public
assert(setup_log[0] == 0, "Non-zero first byte");

// We append the public value to the log and emit it as unencrypted log
let mut finalization_log = [0; $finalization_log_byte_length];
// We append the public value to the log and emit it as unencrypted log
let mut finalization_log = [0; $finalization_log_byte_length];

// Iterate over the partial log and copy it to the final log
for i in 1..setup_log.len() {
finalization_log[i] = setup_log[i];
}
// Iterate over the partial log and copy it to the final log
for i in 1..setup_log.len() {
finalization_log[i] = setup_log[i];
}

// Now we populate the first byte with number of public values
finalization_log[0] = 1;
// Now we populate the first byte with number of public values
finalization_log[0] = 1;

// Iterate over the public values and append them to the log
for i in 0..$public_values_length {
let public_value_bytes: [u8; 32] = self.public_values[i].to_be_bytes();
for j in 0..public_value_bytes.len() {
finalization_log[$setup_log_byte_length + i * 32 + j] = public_value_bytes[j];
// Iterate over the public values and append them to the log
for i in 0..$public_values_length {
let public_value_bytes: [u8; 32] = self.public_values[i].to_be_bytes();
for j in 0..public_value_bytes.len() {
finalization_log[$setup_log_byte_length + i * 32 + j] = public_value_bytes[j];
}
}
}

finalization_log
// We reset public storage to zero to achieve the effect of transient storage - kernels will squash
// the writes
context.storage_write(slot, [0; $setup_log_field_length]);

finalization_log
}
}

impl aztec::protocol_types::traits::Empty for $finalization_payload_name {
Expand Down
33 changes: 3 additions & 30 deletions noir-projects/noir-contracts/contracts/nft_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -252,38 +252,11 @@ contract NFT {
// We insert the finalization note hash
context.push_note_hash(finalization_payload.note_hash);

// ############### HACKY NEW CODE TO BE POLISHED LATER ###############
let setup_log_fields: [Field; 16] = context.storage_read(hiding_point_slot + 3);
let setup_log: [u8; 481] = fields_to_bytes(setup_log_fields);

// First byte should be zero as the number of public values is populated in public
assert(setup_log[0] == 0, "Non-zero first byte");

// We append the public value to the log and emit it as unencrypted log
let mut log = [0; 513];

// Iterate over the partial log and copy it to the final log
for i in 1..setup_log.len() {
log[i] = setup_log[i];
}

// Now we populate the first byte with number of public values
log[0] = 1;

// Iterate over the finalization log and copy it to the final log
let finalization_log: [u8; 32] = finalization_payload.log[0].to_be_bytes();

for i in 0..finalization_log.len() {
log[481 + i] = finalization_log[i];
}

// We emit the `token_id` as unencrypted event such that the `NoteProcessor` can use it to reconstruct the note
context.emit_unencrypted_log(log);
let finalization_log = finalization_payload.get_log(context, hiding_point_slot + 3);

// At last we reset public storage to zero to achieve the effect of transient storage - kernels will squash
// the writes
context.storage_write(hiding_point_slot, Point::empty());
context.storage_write(hiding_point_slot + 3, [0; 16]);
// We emit the finalization log via the unencrypted logs stream
context.emit_unencrypted_log(finalization_log);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ contract Token {

// 4. We emit the `tx_fee` and `refund_amount` as unencrypted event such that the `NoteProcessor` can use it
// to reconstruct the note.
context.emit_unencrypted_log(fee_payer_finalization_payload.log);
context.emit_unencrypted_log(user_finalization_payload.log);
context.emit_unencrypted_log(fee_payer_finalization_payload.public_values);
context.emit_unencrypted_log(user_finalization_payload.public_values);

// 5. At last we emit the note hashes.
context.push_note_hash(fee_payer_finalization_payload.note_hash);
Expand Down

0 comments on commit a0b45b4

Please sign in to comment.