Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
Initial
  • Loading branch information
sklppy88 committed Jun 10, 2024
1 parent ec5cd7b commit 3a27a34
Show file tree
Hide file tree
Showing 50 changed files with 1,607 additions and 575 deletions.
8 changes: 4 additions & 4 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
key_validation_request::get_key_validation_request, arguments, returns::pack_returns,
call_private_function::call_private_function_internal, header::get_header_at,
logs::{
emit_encrypted_log, emit_encrypted_note_log, compute_encrypted_log,
emit_encrypted_note_log, emit_encrypted_event_log, compute_encrypted_event_log,
emit_contract_class_unencrypted_log_private_internal, emit_unencrypted_log_private_internal
},
logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog},
Expand Down Expand Up @@ -311,7 +311,7 @@ impl PrivateContext {

// NB: A randomness value of 0 signals that the kernels should not mask the contract address
// used in siloing later on e.g. 'handshaking' contract w/ known address.
pub fn encrypt_and_emit_log<N, M>(
pub fn encrypt_and_emit_event<N, M>(
&mut self,
contract_address: AztecAddress,
randomness: Field, // Secret random value used later for masked_contract_address
Expand All @@ -324,7 +324,7 @@ impl PrivateContext {

// We are currently just encrypting it unconstrained, but otherwise the same way as if it was a note.
let counter = self.next_counter();
let encrypted_log: [u8; M] = compute_encrypted_log(
let encrypted_log: [u8; M] = compute_encrypted_event_log(
contract_address,
randomness,
event_type_id,
Expand All @@ -333,7 +333,7 @@ impl PrivateContext {
ivpk_m,
preimage
);
emit_encrypted_log(contract_address, randomness, encrypted_log, counter);
emit_encrypted_event_log(contract_address, randomness, encrypted_log, counter);
let len = 32 + 32 + 64 + 48 + 48 + 176 + 64 + (preimage.len() as Field * 32) + 16 + 4;
let log_hash = sha256_to_field(encrypted_log);
let side_effect = EncryptedLogHash { value: log_hash, counter, length: len, randomness };
Expand Down
99 changes: 91 additions & 8 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::note::{note_interface::NoteInterface};
use crate::note::note_interface::NoteInterface;
use crate::event::event_interface::EventInterface;
use dep::protocol_types::{grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint};

use dep::std::aes128::aes128_encrypt;
Expand All @@ -14,6 +15,11 @@ 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);
EncryptedLogIncomingBody { plaintext }
}

pub fn compute_ciphertext(self, eph_sk: GrumpkinPrivateKey, ivpk_app: GrumpkinPoint) -> [u8] {
let full_key = point_to_symmetric_key(eph_sk, ivpk_app);
let mut sym_key = [0; 16];
Expand All @@ -31,12 +37,14 @@ mod test {
use crate::encrypted_logs::incoming_body::EncryptedLogIncomingBody;
use dep::protocol_types::{
address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint
grumpkin_private_key::GrumpkinPrivateKey, grumpkin_point::GrumpkinPoint, traits::Serialize,
abis::function_selector::FunctionSelector
};

use crate::{
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption},
oracle::unsafe_rand::unsafe_rand, context::PrivateContext
event::event_interface::EventInterface, oracle::unsafe_rand::unsafe_rand,
context::PrivateContext
};

struct AddressNote {
Expand Down Expand Up @@ -100,7 +108,7 @@ mod test {
}

#[test]
fn test_encrypted_log_incoming_body() {
fn test_encrypted_note_log_incoming_body() {
let note = AddressNote::new(
AztecAddress::from_field(0x1),
AztecAddress::from_field(0x2),
Expand All @@ -122,14 +130,89 @@ mod test {

let ciphertext = body.compute_ciphertext(eph_sk, ivpk_app);

let expected_body_ciphertext = [
let expected_note_body_ciphertext = [
131, 119, 105, 129, 244, 32, 151, 205, 12, 99, 93, 62, 10, 180, 72, 21, 47, 232, 95, 17, 240, 230, 80, 129, 174, 158, 23, 76, 114, 185, 43, 18, 254, 148, 147, 230, 66, 216, 167, 62, 180, 213, 238, 33, 108, 29, 84, 139, 99, 206, 212, 253, 92, 116, 137, 31, 0, 104, 45, 91, 250, 109, 141, 114, 189, 53, 35, 60, 108, 156, 170, 206, 150, 114, 150, 187, 198, 13, 62, 153, 133, 13, 169, 167, 242, 221, 40, 168, 186, 203, 104, 82, 47, 238, 142, 179, 90, 37, 9, 70, 245, 176, 122, 247, 42, 87, 75, 7, 20, 89, 166, 123, 14, 26, 230, 156, 49, 94, 0, 94, 72, 58, 171, 239, 115, 174, 155, 7, 151, 17, 60, 206, 193, 134, 70, 87, 215, 88, 21, 194, 63, 26, 106, 105, 124, 213, 252, 152, 192, 71, 115, 13, 181, 5, 169, 15, 170, 196, 174, 228, 170, 192, 91, 76, 110, 220, 89, 47, 248, 144, 189, 251, 167, 149, 248, 226
];

assert_eq(expected_body_ciphertext.len(), ciphertext.len());
assert_eq(expected_note_body_ciphertext.len(), ciphertext.len());

for i in 0..expected_note_body_ciphertext.len() {
assert_eq(ciphertext[i], expected_note_body_ciphertext[i]);
}
}

struct TestEvent {
value0: Field,
value1: Field,
value2: Field,
}

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

global TEST_EVENT_LEN: Field = 3;
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 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);

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
}
}

#[test]
fn test_encrypted_log_event_incoming_body() {
let test_event = TestEvent { value0: 1, value1: 2, value2: 3 };

let eph_sk = GrumpkinPrivateKey::new(
0x0000000000000000000000000000000023b3127c127b1f29a7adff5cccf8fb06,
0x00000000000000000000000000000000649e7ca01d9de27b21624098b897babd
);

let ivpk_app = GrumpkinPoint::new(
0x2688431c705a5ff3e6c6f2573c9e3ba1c1026d2251d0dbbf2d810aa53fd1d186,
0x1e96887b117afca01c00468264f4f80b5bb16d94c1808a448595f115556e5c8e
);

let randomness = 2;

let body = EncryptedLogIncomingBody::from_event(test_event, randomness);

let ciphertext = body.compute_ciphertext(eph_sk, ivpk_app);

let expected_event_body_ciphertext = [
131, 119, 105, 129, 244, 32, 151, 205, 12, 99, 93, 62, 10, 180, 72, 21, 47, 232, 95, 17, 240, 230, 80, 129, 174, 158, 23, 76, 114, 185, 43, 18, 254, 148, 147, 230, 66, 216, 167, 62, 180, 213, 238, 33, 108, 29, 84, 139, 157, 165, 187, 138, 35, 3, 236, 75, 197, 105, 102, 247, 224, 253, 13, 217, 145, 62, 96, 167, 93, 23, 18, 198, 187, 91, 8, 3, 197, 195, 127, 9, 218, 111, 125, 97, 141, 129, 142, 1, 230, 108, 35, 211, 170, 170, 170, 249, 249, 104, 68, 191, 245, 207, 182, 245, 248, 82, 175, 83, 155, 138, 208, 65, 31, 129, 251, 242, 219, 76, 17, 61, 178, 187, 108, 114, 177, 215, 175, 189, 166, 221, 94, 9, 22, 57, 151, 204, 57, 220, 129, 243, 217, 18, 101, 128, 229, 40, 254, 175, 2, 21, 31, 198, 18, 152, 169, 32, 113, 92, 37, 65, 169, 119, 95, 149, 239, 8, 23, 182, 22, 209, 207, 120, 133, 90, 252, 106
];

assert_eq(expected_event_body_ciphertext.len(), ciphertext.len());

for i in 0..expected_body_ciphertext.len() {
assert_eq(ciphertext[i], expected_body_ciphertext[i]);
for i in 0..expected_event_body_ciphertext.len() {
assert_eq(ciphertext[i], expected_event_body_ciphertext[i]);
}
}
}
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/event.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod event_interface;
9 changes: 9 additions & 0 deletions noir-projects/aztec-nr/aztec/src/event/event_interface.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,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];
}
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod initializer;
mod keys;
mod messaging;
mod note;
mod event;
mod oracle;
mod state_vars;
mod prelude;
Expand Down
52 changes: 42 additions & 10 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ unconstrained pub fn emit_encrypted_note_log<M>(
emit_encrypted_note_log_oracle(note_hash_counter, encrypted_note, counter)
}

#[oracle(emitEncryptedLog)]
fn emit_encrypted_log_oracle<M>(
#[oracle(emitEncryptedEventLog)]
fn emit_encrypted_event_log_oracle<M>(
_contract_address: AztecAddress,
_randomness: Field,
_encrypted_note: [u8; M],
_encrypted_event: [u8; M],
_counter: u32
) {}

unconstrained pub fn emit_encrypted_log<M>(
unconstrained pub fn emit_encrypted_event_log<M>(
contract_address: AztecAddress,
randomness: Field,
encrypted_note: [u8; M],
encrypted_event: [u8; M],
counter: u32
) {
emit_encrypted_log_oracle(contract_address, randomness, encrypted_note, counter)
emit_encrypted_event_log_oracle(contract_address, randomness, encrypted_event, counter)
}

// = 480 + 32 * N bytes
#[oracle(computeEncryptedLog)]
fn compute_encrypted_log_oracle<N, M>(
#[oracle(computeEncryptedNoteLog)]
fn compute_encrypted_note_log_oracle<N, M>(
_contract_address: AztecAddress,
_storage_slot: Field,
_note_type_id: Field,
Expand All @@ -45,7 +45,7 @@ fn compute_encrypted_log_oracle<N, M>(
_preimage: [Field; N]
) -> [u8; M] {}

unconstrained pub fn compute_encrypted_log<N, M>(
unconstrained pub fn compute_encrypted_note_log<N, M>(
contract_address: AztecAddress,
storage_slot: Field,
note_type_id: Field,
Expand All @@ -54,7 +54,7 @@ unconstrained pub fn compute_encrypted_log<N, M>(
ivpk_m: GrumpkinPoint,
preimage: [Field; N]
) -> [u8; M] {
compute_encrypted_log_oracle(
compute_encrypted_note_log_oracle(
contract_address,
storage_slot,
note_type_id,
Expand All @@ -65,6 +65,38 @@ unconstrained pub fn compute_encrypted_log<N, M>(
)
}

// = 480 + 32 * N bytes
#[oracle(computeEncryptedEventLog)]
fn compute_encrypted_event_log_oracle<N, M>(
_contract_address: AztecAddress,
_randomness: Field,
_event_type_id: Field,
_ovsk_app: Field,
_ovpk_m: GrumpkinPoint,
_ivpk_m: GrumpkinPoint,
_preimage: [Field; N]
) -> [u8; M] {}

unconstrained pub fn compute_encrypted_event_log<N, M>(
contract_address: AztecAddress,
randomness: Field,
event_type_id: Field,
ovsk_app: Field,
ovpk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
preimage: [Field; N]
) -> [u8; M] {
compute_encrypted_event_log_oracle(
contract_address,
randomness,
event_type_id,
ovsk_app,
ovpk_m,
ivpk_m,
preimage
)
}

#[oracle(emitUnencryptedLog)]
fn emit_unencrypted_log_oracle_private<T>(
_contract_address: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ contract Test {
let header = context.get_header();
let outgoing_viewer_ovpk_m = header.get_ovpk_m(&mut context, outgoing_viewer);
let owner_ivpk_m = header.get_ivpk_m(&mut context, owner);
context.encrypt_and_emit_log(
context.encrypt_and_emit_event(
context.this_address(),
5, // testing only - this should be a secret random value to salt the addr
1,
Expand All @@ -281,7 +281,7 @@ contract Test {
// to test nested and non nested encrypted logs
if nest {
Test::at(context.this_address()).emit_array_as_encrypted_log([0, 0, 0, 0, 0], owner, outgoing_viewer, false).call(&mut context);
context.encrypt_and_emit_log(
context.encrypt_and_emit_event(
context.this_address(),
0, // testing only - this signals to the kerels to not mask the address
1,
Expand Down
Loading

0 comments on commit 3a27a34

Please sign in to comment.