Skip to content

Commit

Permalink
refactor: nuking log_traits.nr (#8797)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored and Rumata888 committed Sep 27, 2024
1 parent 7908360 commit ed43ebd
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 492 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_log,
keys::{getters::get_public_keys, public_keys::{OvpkM, IvpkM}},
oracle::logs_traits::LensForEncryptedEvent, oracle::unsafe_rand::unsafe_rand
keys::{getters::get_public_keys, public_keys::{OvpkM, IvpkM}}, oracle::unsafe_rand::unsafe_rand
};
use dep::protocol_types::{address::AztecAddress, hash::sha256_to_field};

unconstrained fn compute_unconstrained<Event, let N: u32, let OB: u32>(
unconstrained fn compute_unconstrained<Event, let N: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> ([u8; 416 + N * 32], Field) where Event: EventInterface<N> {
compute(
contract_address,
randomness,
Expand All @@ -26,41 +25,41 @@ unconstrained fn compute_unconstrained<Event, let N: u32, let OB: u32>(
)
}

fn compute<Event, let N: u32, let OB: u32>(
fn compute<Event, let N: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> ([u8; 416 + N * 32], Field) where Event: EventInterface<N> {
let plaintext = event.private_to_be_bytes(randomness);
let encrypted_log: [u8; OB] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let encrypted_log: [u8; 416 + N * 32] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let log_hash = sha256_to_field(encrypted_log);
(encrypted_log, log_hash)
}

fn emit_with_keys<Event, let N: u32, let OB: u32>(
fn emit_with_keys<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
event: Event,
ovpk: OvpkM,
ivpk: IvpkM,
iv: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; OB], Field)
) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; 416 + N * 32], Field)
) where Event: EventInterface<N> {
let contract_address: AztecAddress = context.this_address();
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
let (encrypted_log, log_hash) = inner_compute(contract_address, randomness, ovsk_app, ovpk, ivpk, iv, event);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}

pub fn encode_and_encrypt_event<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event<Event, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -69,11 +68,11 @@ pub fn encode_and_encrypt_event<Event, let N: u32, let OB: u32>(
}
}

pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -82,75 +81,75 @@ pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32, let OB: u32>(
}
}

pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute);
}
}

pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, let N: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::{
context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface},
keys::{getters::{get_public_keys, get_ovsk_app}, public_keys::{OvpkM, IvpkM}},
encrypted_logs::payload::compute_encrypted_log, oracle::logs_traits::LensForEncryptedLog
encrypted_logs::payload::compute_encrypted_log
};
use dep::protocol_types::{hash::sha256_to_field, address::AztecAddress, abis::note_hash::NoteHash};

fn compute_raw_note_log<Note, let N: u32, let M: u32>(
fn compute_raw_note_log<Note, let N: u32>(
context: PrivateContext,
note: Note,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> (u32, [u8; M], Field) where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) -> (u32, [u8; 416 + N * 32], Field) where Note: NoteInterface<N> {
let note_header = note.get_header();
let note_hash_counter = note_header.note_hash_counter;
let storage_slot = note_header.storage_slot;
Expand All @@ -24,28 +24,28 @@ fn compute_raw_note_log<Note, let N: u32, let M: u32>(
let contract_address: AztecAddress = context.this_address();

let plaintext = note.to_be_bytes(storage_slot);
let encrypted_log: [u8; M] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let encrypted_log: [u8; 416 + N * 32] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let log_hash = sha256_to_field(encrypted_log);

(note_hash_counter, encrypted_log, log_hash)
}

unconstrained fn compute_raw_note_log_unconstrained<Note, let N: u32, let M: u32>(
unconstrained fn compute_raw_note_log_unconstrained<Note, let N: u32>(
context: PrivateContext,
note: Note,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> (u32, [u8; M], Field) where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) -> (u32, [u8; 416 + N * 32], Field) where Note: NoteInterface<N> {
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_raw_note_log(context, note, ovsk_app, ovpk, ivpk, recipient)
}

pub fn encode_and_encrypt_note<Note, let N: u32, let M: u32>(
pub fn encode_and_encrypt_note<Note, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N> {
| e: NoteEmission<Note> | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -56,11 +56,11 @@ pub fn encode_and_encrypt_note<Note, let N: u32, let M: u32>(
}
}

pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32, let M: u32>(
pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N> {
| e: NoteEmission<Note> | {
// Note: We could save a lot of gates by obtaining the following keys in an unconstrained context but this
// function is currently not used anywhere so we are not optimizing it.
Expand All @@ -76,12 +76,12 @@ pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32, let M: u32>(
}
}

pub fn encode_and_encrypt_note_with_keys<Note, let N: u32, let M: u32>(
pub fn encode_and_encrypt_note_with_keys<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N> {
| e: NoteEmission<Note> | {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());

Expand All @@ -90,12 +90,12 @@ pub fn encode_and_encrypt_note_with_keys<Note, let N: u32, let M: u32>(
}
}

pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, let N: u32, let M: u32>(
pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N> {
| e: NoteEmission<Note> | {
// Having the log hash be unconstrained here is fine because the way this works is we send the log hash
// to the kernel, and it gets included as part of its public inputs. Then we send the tx to the sequencer,
Expand Down
32 changes: 15 additions & 17 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::protocol_types::{
point::Point, traits::Hash,
hash::{sha256_to_field, poseidon2_hash_with_separator, poseidon2_hash_with_separator_slice}
};
use crate::oracle::logs_traits::ToBytesForUnencryptedLog;
use crate::utils::to_bytes::{arr_to_be_bytes_arr, str_to_be_bytes_arr};

pub use dep::protocol_types::hash::{compute_siloed_nullifier, pedersen_hash};

Expand All @@ -16,25 +16,19 @@ pub fn compute_secret_hash(secret: Field) -> Field {
poseidon2_hash_with_separator([secret], GENERATOR_INDEX__SECRET_HASH)
}

pub fn compute_unencrypted_log_hash<T, let N: u32, let M: u32>(
contract_address: AztecAddress,
log: T
) -> Field where T: ToBytesForUnencryptedLog<N, M> {
let message_bytes: [u8; N] = log.to_be_bytes_arr();
// can't use N - not in scope error
let n = message_bytes.len();
let mut hash_bytes = [0; M];
pub fn compute_unencrypted_log_hash<let N: u32>(contract_address: AztecAddress, log: [u8; N]) -> Field {
let mut hash_bytes = [0; N + 36];
// Address is converted to 32 bytes in ts
let address_bytes = contract_address.to_be_bytes_arr();
let address_bytes: [u8; 32] = contract_address.to_field().to_be_bytes();
for i in 0..32 {
hash_bytes[i] = address_bytes[i];
}
let len_bytes: [u8; 4] = (n as Field).to_be_bytes();
let len_bytes: [u8; 4] = (N as Field).to_be_bytes();
for i in 0..4 {
hash_bytes[32 + i] = len_bytes[i];
}
for i in 0..n {
hash_bytes[36 + i] = message_bytes[i];
for i in 0..N {
hash_bytes[36 + i] = log[i];
}

sha256_to_field(hash_bytes)
Expand Down Expand Up @@ -140,30 +134,34 @@ unconstrained fn compute_unenc_log_hash_array() {
0x25d0f689c4a4178a29d59306f2675824d19be6d25e44fa03b03f49c263053dd2,
0x2d513a722d6f352dc0961f156afdc5e31495b9f0e35cb069261a8e55e2df67fd
];
let hash = compute_unencrypted_log_hash(contract_address, log);
let serialized_log = arr_to_be_bytes_arr(log);
let hash = compute_unencrypted_log_hash(contract_address, serialized_log);
assert(hash == 0x0095b2d17ab72f4b27a341f7ac63e49ec73935ae8c9181a0ac02023eb12f3284);
}

#[test]
unconstrained fn compute_unenc_log_hash_addr() {
let contract_address = AztecAddress::from_field(0x233a3e0df23b2b15b324194cb4a151f26c0b7333250781d34cc269d85dc334c6);
let log = AztecAddress::from_field(0x26aa302d4715fd8a687453cb26d616b0768027bd54bcae56b09d908ecd9f8303);
let hash = compute_unencrypted_log_hash(contract_address, log);
let serialized_log: [u8; 32] = log.to_field().to_be_bytes();
let hash = compute_unencrypted_log_hash(contract_address, serialized_log);
assert(hash == 0x0083ab647dfb26e7ddee90a0f4209d049d4660cab42000c544b986aaa84c55a3);
}

#[test]
unconstrained fn compute_unenc_log_hash_str() {
let contract_address = AztecAddress::from_field(0x1b401e1146c5c507962287065c81f0ef7590adae3802c533d7549d6bf0a41bd8);
let log = "dummy";
let hash = compute_unencrypted_log_hash(contract_address, log);
let serialized_log = str_to_be_bytes_arr(log);
let hash = compute_unencrypted_log_hash(contract_address, serialized_log);
assert(hash == 0x00629e88ebd6374f44aa6cfe07e251ecf07213ebc7267e8f6b578ae57ffd6c20);
}

#[test]
unconstrained fn compute_unenc_log_hash_longer_str() {
let contract_address = AztecAddress::from_field(0x1b401e1146c5c507962287065c81f0ef7590adae3802c533d7549d6bf0a41bd8);
let log = "Hello this is a string";
let hash = compute_unencrypted_log_hash(contract_address, log);
let serialized_log = str_to_be_bytes_arr(log);
let hash = compute_unencrypted_log_hash(contract_address, serialized_log);
assert(hash == 0x0098637962f7d34fa202b7ffad8a07a238c5d1fd897b82a108f7f467fa73b841);
}
Loading

0 comments on commit ed43ebd

Please sign in to comment.