diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr index 8c1293b590df..d73bd1a3a9d3 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr @@ -1,12 +1,11 @@ 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( +unconstrained fn compute_unconstrained( contract_address: AztecAddress, randomness: Field, ovsk_app: Field, @@ -14,7 +13,7 @@ unconstrained fn compute_unconstrained( ivpk: IvpkM, recipient: AztecAddress, event: Event -) -> ([u8; OB], Field) where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> ([u8; 416 + N * 32], Field) where Event: EventInterface { compute( contract_address, randomness, @@ -26,7 +25,7 @@ unconstrained fn compute_unconstrained( ) } -fn compute( +fn compute( contract_address: AztecAddress, randomness: Field, ovsk_app: Field, @@ -34,33 +33,33 @@ fn compute( ivpk: IvpkM, recipient: AztecAddress, event: Event -) -> ([u8; OB], Field) where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> ([u8; 416 + N * 32], Field) where Event: EventInterface { 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( +fn emit_with_keys( 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, [u8; N * 32 + 64]: LensForEncryptedEvent { + inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; 416 + N * 32], Field) +) where Event: EventInterface { 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( +pub fn encode_and_encrypt_event( context: &mut PrivateContext, ov: AztecAddress, iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface { | e: Event | { let ovpk = get_public_keys(ov).ovpk_m; let ivpk = get_public_keys(iv).ivpk_m; @@ -69,11 +68,11 @@ pub fn encode_and_encrypt_event( } } -pub fn encode_and_encrypt_event_unconstrained( +pub fn encode_and_encrypt_event_unconstrained( context: &mut PrivateContext, ov: AztecAddress, iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface { | e: Event | { let ovpk = get_public_keys(ov).ovpk_m; let ivpk = get_public_keys(iv).ivpk_m; @@ -82,12 +81,12 @@ pub fn encode_and_encrypt_event_unconstrained( } } -pub fn encode_and_encrypt_event_with_randomness( +pub fn encode_and_encrypt_event_with_randomness( context: &mut PrivateContext, randomness: Field, ov: AztecAddress, iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface { | e: Event | { let ovpk = get_public_keys(ov).ovpk_m; let ivpk = get_public_keys(iv).ivpk_m; @@ -95,12 +94,12 @@ pub fn encode_and_encrypt_event_with_randomness( } } -pub fn encode_and_encrypt_event_with_randomness_unconstrained( +pub fn encode_and_encrypt_event_with_randomness_unconstrained( context: &mut PrivateContext, randomness: Field, ov: AztecAddress, iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface { | e: Event | { let ovpk = get_public_keys(ov).ovpk_m; let ivpk = get_public_keys(iv).ivpk_m; @@ -108,49 +107,49 @@ pub fn encode_and_encrypt_event_with_randomness_unconstrained( +pub fn encode_and_encrypt_event_with_keys( context: &mut PrivateContext, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface { | 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( +pub fn encode_and_encrypt_event_with_keys_unconstrained( context: &mut PrivateContext, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface { | 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( +pub fn encode_and_encrypt_event_with_keys_with_randomness( context: &mut PrivateContext, randomness: Field, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface { | e: Event | { emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute); } } -pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained( +pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained( context: &mut PrivateContext, randomness: Field, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface, [u8; N * 32 + 64]: LensForEncryptedEvent { +) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface { | e: Event | { emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained); } diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr index c72c89ec9580..9b782e57edea 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr @@ -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( +fn compute_raw_note_log( context: PrivateContext, note: Note, ovsk_app: Field, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> (u32, [u8; M], Field) where Note: NoteInterface, [Field; N]: LensForEncryptedLog { +) -> (u32, [u8; 416 + N * 32], Field) where Note: NoteInterface { let note_header = note.get_header(); let note_hash_counter = note_header.note_hash_counter; let storage_slot = note_header.storage_slot; @@ -24,28 +24,28 @@ fn compute_raw_note_log( 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( +unconstrained fn compute_raw_note_log_unconstrained( context: PrivateContext, note: Note, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> (u32, [u8; M], Field) where Note: NoteInterface, [Field; N]: LensForEncryptedLog { +) -> (u32, [u8; 416 + N * 32], Field) where Note: NoteInterface { 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( +pub fn encode_and_encrypt_note( context: &mut PrivateContext, ov: AztecAddress, iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission) -> () where Note: NoteInterface, [Field; N]: LensForEncryptedLog { +) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission) -> () where Note: NoteInterface { | e: NoteEmission | { let ovpk = get_public_keys(ov).ovpk_m; let ivpk = get_public_keys(iv).ivpk_m; @@ -56,11 +56,11 @@ pub fn encode_and_encrypt_note( } } -pub fn encode_and_encrypt_note_unconstrained( +pub fn encode_and_encrypt_note_unconstrained( context: &mut PrivateContext, ov: AztecAddress, iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission) -> () where Note: NoteInterface, [Field; N]: LensForEncryptedLog { +) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission) -> () where Note: NoteInterface { | e: NoteEmission | { // 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. @@ -76,12 +76,12 @@ pub fn encode_and_encrypt_note_unconstrained( } } -pub fn encode_and_encrypt_note_with_keys( +pub fn encode_and_encrypt_note_with_keys( context: &mut PrivateContext, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission) -> () where Note: NoteInterface, [Field; N]: LensForEncryptedLog { +) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission) -> () where Note: NoteInterface { | e: NoteEmission | { let ovsk_app: Field = context.request_ovsk_app(ovpk.hash()); @@ -90,12 +90,12 @@ pub fn encode_and_encrypt_note_with_keys( } } -pub fn encode_and_encrypt_note_with_keys_unconstrained( +pub fn encode_and_encrypt_note_with_keys_unconstrained( context: &mut PrivateContext, ovpk: OvpkM, ivpk: IvpkM, recipient: AztecAddress -) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission) -> () where Note: NoteInterface, [Field; N]: LensForEncryptedLog { +) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission) -> () where Note: NoteInterface { | e: NoteEmission | { // 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, diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index 490cc13bbfdf..b96fec6e1a59 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -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}; @@ -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( - contract_address: AztecAddress, - log: T -) -> Field where T: ToBytesForUnencryptedLog { - 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(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) @@ -140,7 +134,8 @@ 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); } @@ -148,7 +143,8 @@ unconstrained fn compute_unenc_log_hash_array() { 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); } @@ -156,7 +152,8 @@ unconstrained fn compute_unenc_log_hash_addr() { 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); } @@ -164,6 +161,7 @@ unconstrained fn compute_unenc_log_hash_str() { 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); } diff --git a/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr b/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr deleted file mode 100644 index 57ab8ae15beb..000000000000 --- a/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr +++ /dev/null @@ -1,430 +0,0 @@ -use dep::protocol_types::address::AztecAddress; - -// TODO: this is awful but since we can't have a fn that maps [Field; N] -> [u8; 416 + N * 32] -// (where N is the note pre-image size and 416 + N * 32 is the encryption output size) -// The fns for LensForEncryptedLog are never used, it's just to tell the compiler what the lens are - -// The to_bytes fn for ToBytesForUnencryptedLog is used to allow us to hash some generic T - -// I could have omitted N from the trait, but wanted to keep it strictly for field arrs -// TODO(1139): Once we enc inside the circuit, we will no longer need the oracle to return -// anything, so we can remove this trait -pub trait LensForEncryptedLog { - // N = note preimage input in fields - // M = encryption output len in bytes (= 416 + N * 32) - fn output_fields(self: [Field; N]) -> [Field; N]; - fn output_bytes(self: [Field; N]) -> [u8; M]; -} - -impl LensForEncryptedLog<1, 448> for [Field; 1] { - fn output_fields(self) -> [Field; 1] { - [self[0]; 1] - } - fn output_bytes(self) -> [u8; 448] { - [self[0] as u8; 448] - } -} -impl LensForEncryptedLog<2, 480> for [Field; 2] { - fn output_fields(self) -> [Field; 2] { - [self[0]; 2] - } - fn output_bytes(self) -> [u8; 480] { - [self[0] as u8; 480] - } -} -impl LensForEncryptedLog<3, 512> for [Field; 3] { - fn output_fields(self) -> [Field; 3] { - [self[0]; 3] - } - fn output_bytes(self) -> [u8; 512] { - [self[0] as u8; 512] - } -} -impl LensForEncryptedLog<4, 544> for [Field; 4] { - fn output_fields(self) -> [Field; 4] { - [self[0]; 4] - } - fn output_bytes(self) -> [u8; 544] { - [self[0] as u8; 544] - } -} -impl LensForEncryptedLog<5, 576> for [Field; 5] { - fn output_fields(self) -> [Field; 5] { - [self[0]; 5] - } - fn output_bytes(self) -> [u8; 576] { - [self[0] as u8; 576] - } -} -impl LensForEncryptedLog<6, 608> for [Field; 6] { - fn output_fields(self) -> [Field; 6] { - [self[0]; 6] - } - fn output_bytes(self) -> [u8; 608] { - [self[0] as u8; 608] - } -} -impl LensForEncryptedLog<7, 640> for [Field; 7] { - fn output_fields(self) -> [Field; 7] { - [self[0]; 7] - } - fn output_bytes(self) -> [u8; 640] { - [self[0] as u8; 640] - } -} -impl LensForEncryptedLog<8, 672> for [Field; 8] { - fn output_fields(self) -> [Field; 8] { - [self[0]; 8] - } - fn output_bytes(self) -> [u8; 672] { - [self[0] as u8; 672] - } -} -impl LensForEncryptedLog<9, 704> for [Field; 9] { - fn output_fields(self) -> [Field; 9] { - [self[0]; 9] - } - fn output_bytes(self) -> [u8; 704] { - [self[0] as u8; 704] - } -} -impl LensForEncryptedLog<10, 736> for [Field; 10] { - fn output_fields(self) -> [Field; 10] { - [self[0]; 10] - } - fn output_bytes(self) -> [u8; 736] { - [self[0] as u8; 736] - } -} -impl LensForEncryptedLog<11, 768> for [Field; 11] { - fn output_fields(self) -> [Field; 11] { - [self[0]; 11] - } - fn output_bytes(self) -> [u8; 768] { - [self[0] as u8; 768] - } -} -impl LensForEncryptedLog<12, 800> for [Field; 12] { - fn output_fields(self) -> [Field; 12] { - [self[0]; 12] - } - fn output_bytes(self) -> [u8; 800] { - [self[0] as u8; 800] - } -} - -pub trait LensForEncryptedEvent { - // N = event preimage input in bytes - // M = encryption output len in bytes (= 416 + M) - fn output(self: [u8; N]) -> [u8; M]; -} - -impl LensForEncryptedEvent<96, 448> for [u8; 96] { - fn output(self) -> [u8; 448] { - [self[0] as u8; 448] - } -} -impl LensForEncryptedEvent<128, 480> for [u8; 128] { - fn output(self) -> [u8; 480] { - [self[0] as u8; 480] - } -} -impl LensForEncryptedEvent<160, 512> for [u8; 160] { - fn output(self) -> [u8; 512] { - [self[0] as u8; 512] - } -} -impl LensForEncryptedEvent<192, 544> for [u8; 192] { - fn output(self) -> [u8; 544] { - [self[0] as u8; 544] - } -} -impl LensForEncryptedEvent<224, 576> for [u8; 224] { - fn output(self) -> [u8; 576] { - [self[0] as u8; 576] - } -} -impl LensForEncryptedEvent<256, 608> for [u8; 256] { - fn output(self) -> [u8; 608] { - [self[0] as u8; 608] - } -} - -// This trait defines the length of the inputs in bytes to -// the unencrypted log hash fn, where the log can be any type T -// as long as the ACVM can convert to fields. -pub trait ToBytesForUnencryptedLog { - // N = preimage input in bytes (32 * num fields or chars) - // M = full log input in bytes ( = N + 40 = N + 32 for addr, + 4 for selector, + 4 for len) - fn to_be_bytes_arr(self) -> [u8; N]; - fn output_bytes(self) -> [u8; M]; -} - -impl ToBytesForUnencryptedLog<32, 68> for Field { - fn to_be_bytes_arr(self) -> [u8; 32] { - self.to_be_bytes() - } - fn output_bytes(self) -> [u8; 68] { - [self as u8; 68] - } -} - -impl ToBytesForUnencryptedLog<32, 68> for AztecAddress { - fn to_be_bytes_arr(self) -> [u8; 32] { - self.to_field().to_be_bytes() - } - fn output_bytes(self) -> [u8; 68] { - [self.to_field() as u8; 68] - } -} - -fn arr_to_be_bytes_arr(fields: [Field; L]) -> [u8; N] { - let mut bytes: [u8] = &[]; - for i in 0..L { - // Note that bytes.append() results in bound error - let to_add: [u8; 32] = fields[i].to_be_bytes(); - for j in 0..32 { - bytes = bytes.push_back(to_add[j]); - } - } - bytes.as_array() -} - -// each character of a string is converted into a byte -// then an ACVM field via the oracle => we recreate here -fn str_to_be_bytes_arr(string: str) -> [u8; N] { - let chars_bytes = string.as_bytes(); - let mut bytes: [u8] = &[]; - for i in 0..L { - let to_add: [u8; 32] = (chars_bytes[i] as Field).to_be_bytes(); - for j in 0..32 { - bytes = bytes.push_back(to_add[j]); - } - } - bytes.as_array() -} - -impl ToBytesForUnencryptedLog<32, 68> for [Field; 1] { - fn to_be_bytes_arr(self) -> [u8; 32] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 68] { - [self[0] as u8; 68] - } -} - -impl ToBytesForUnencryptedLog<64, 100> for [Field; 2] { - fn to_be_bytes_arr(self) -> [u8; 64] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 100] { - [self[0] as u8; 100] - } -} - -impl ToBytesForUnencryptedLog<96, 132> for [Field; 3] { - fn to_be_bytes_arr(self) -> [u8; 96] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 132] { - [self[0] as u8; 132] - } -} - -impl ToBytesForUnencryptedLog<128, 164> for [Field; 4] { - fn to_be_bytes_arr(self) -> [u8; 128] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 164] { - [self[0] as u8; 164] - } -} - -impl ToBytesForUnencryptedLog<160, 196> for [Field; 5] { - fn to_be_bytes_arr(self) -> [u8; 160] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 196] { - [self[0] as u8; 196] - } -} - -impl ToBytesForUnencryptedLog<192, 228> for [Field; 6] { - fn to_be_bytes_arr(self) -> [u8; 192] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 228] { - [self[0] as u8; 228] - } -} - -impl ToBytesForUnencryptedLog<224, 260> for [Field; 7] { - fn to_be_bytes_arr(self) -> [u8; 224] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 260] { - [self[0] as u8; 260] - } -} - -impl ToBytesForUnencryptedLog<256, 292> for [Field; 8] { - fn to_be_bytes_arr(self) -> [u8; 256] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 292] { - [self[0] as u8; 292] - } -} - -impl ToBytesForUnencryptedLog<288, 324> for [Field; 9] { - fn to_be_bytes_arr(self) -> [u8; 288] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 324] { - [self[0] as u8; 324] - } -} - -impl ToBytesForUnencryptedLog<320, 356> for [Field; 10] { - fn to_be_bytes_arr(self) -> [u8; 320] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 356] { - [self[0] as u8; 356] - } -} - -impl ToBytesForUnencryptedLog<352, 388> for [Field; 11] { - fn to_be_bytes_arr(self) -> [u8; 352] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 388] { - [self[0] as u8; 388] - } -} - -impl ToBytesForUnencryptedLog<384, 420> for [Field; 12] { - fn to_be_bytes_arr(self) -> [u8; 384] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 420] { - [self[0] as u8; 420] - } -} - -impl ToBytesForUnencryptedLog<416, 452> for [Field; 13] { - fn to_be_bytes_arr(self) -> [u8; 416] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 452] { - [self[0] as u8; 452] - } -} - -impl ToBytesForUnencryptedLog<448, 484> for [Field; 14] { - fn to_be_bytes_arr(self) -> [u8; 448] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 484] { - [self[0] as u8; 484] - } -} - -impl ToBytesForUnencryptedLog<480, 516> for [Field; 15] { - fn to_be_bytes_arr(self) -> [u8; 480] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 516] { - [self[0] as u8; 516] - } -} - -impl ToBytesForUnencryptedLog<512, 548> for [Field; 16] { - fn to_be_bytes_arr(self) -> [u8; 512] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 548] { - [self[0] as u8; 548] - } -} - -impl ToBytesForUnencryptedLog<544, 580> for [Field; 17] { - fn to_be_bytes_arr(self) -> [u8; 544] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 580] { - [self[0] as u8; 580] - } -} - -impl ToBytesForUnencryptedLog<576, 612> for [Field; 18] { - fn to_be_bytes_arr(self) -> [u8; 576] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 612] { - [self[0] as u8; 612] - } -} - -impl ToBytesForUnencryptedLog<608, 644> for [Field; 19] { - fn to_be_bytes_arr(self) -> [u8; 608] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 644] { - [self[0] as u8; 644] - } -} - -impl ToBytesForUnencryptedLog<640, 676> for [Field; 20] { - fn to_be_bytes_arr(self) -> [u8; 640] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 676] { - [self[0] as u8; 676] - } -} - -impl ToBytesForUnencryptedLog<672, 708> for [Field; 21] { - fn to_be_bytes_arr(self) -> [u8; 672] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 708] { - [self[0] as u8; 708] - } -} - -impl ToBytesForUnencryptedLog<704, 740> for [Field; 22] { - fn to_be_bytes_arr(self) -> [u8; 704] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 740] { - [self[0] as u8; 740] - } -} - -impl ToBytesForUnencryptedLog<736, 772> for [Field; 23] { - fn to_be_bytes_arr(self) -> [u8; 736] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 772] { - [self[0] as u8; 772] - } -} - -impl ToBytesForUnencryptedLog<768, 804> for [Field; 24] { - fn to_be_bytes_arr(self) -> [u8; 768] { - arr_to_be_bytes_arr(self) - } - fn output_bytes(self) -> [u8; 804] { - [self[0] as u8; 804] - } -} - -impl ToBytesForUnencryptedLog for str where [Field; L]: ToBytesForUnencryptedLog { - fn to_be_bytes_arr(self) -> [u8; N] { - str_to_be_bytes_arr(self) - } - fn output_bytes(_self: Self) -> [u8; M] { - [0; M] - } -} diff --git a/noir-projects/aztec-nr/aztec/src/oracle/mod.nr b/noir-projects/aztec-nr/aztec/src/oracle/mod.nr index a6e2ee0fdfd0..81d06c48a637 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/mod.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/mod.nr @@ -20,7 +20,6 @@ mod header; mod notes; mod storage; mod logs; -mod logs_traits; mod returns; // debug_log oracle is used by both noir-protocol-circuits and this crate and for this reason we just re-export it diff --git a/noir-projects/aztec-nr/aztec/src/utils/mod.nr b/noir-projects/aztec-nr/aztec/src/utils/mod.nr index 77d3a6531d27..2da0e5b639e4 100644 --- a/noir-projects/aztec-nr/aztec/src/utils/mod.nr +++ b/noir-projects/aztec-nr/aztec/src/utils/mod.nr @@ -2,5 +2,6 @@ mod collapse_array; mod comparison; mod point; mod test; +mod to_bytes; pub use crate::utils::collapse_array::collapse_array; diff --git a/noir-projects/aztec-nr/aztec/src/utils/to_bytes.nr b/noir-projects/aztec-nr/aztec/src/utils/to_bytes.nr new file mode 100644 index 000000000000..8b439c8d3884 --- /dev/null +++ b/noir-projects/aztec-nr/aztec/src/utils/to_bytes.nr @@ -0,0 +1,25 @@ +pub fn arr_to_be_bytes_arr(fields: [Field; L]) -> [u8; L * 32] { + let mut bytes = [0 as u8; L * 32]; + for i in 0..L { + // Note that bytes.append() results in bound error + let to_add: [u8; 32] = fields[i].to_be_bytes(); + for j in 0..32 { + bytes[i * 32 + j] = to_add[j]; + } + } + bytes +} + +// each character of a string is converted into a byte +// then an ACVM field via the oracle => we recreate here +pub fn str_to_be_bytes_arr(string: str) -> [u8; L * 32] { + let chars_bytes: [u8; L] = string.as_bytes(); + let mut bytes = [0 as u8; L * 32]; + for i in 0..L { + let to_add: [u8; 32] = (chars_bytes[i] as Field).to_be_bytes(); + for j in 0..32 { + bytes[i * 32 + j] = to_add[j]; + } + } + bytes +} diff --git a/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr b/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr index b14d41887130..c10c17f8545c 100644 --- a/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/contract_instance_deployer_contract/src/main.nr @@ -9,7 +9,7 @@ contract ContractInstanceDeployer { }; use dep::aztec::{ hash::compute_unencrypted_log_hash, oracle::logs::emit_unencrypted_log_private_internal, - macros::{events::event, functions::private} + macros::{events::event, functions::private}, utils::to_bytes::arr_to_be_bytes_arr }; use std::meta::derive; @@ -68,10 +68,10 @@ contract ContractInstanceDeployer { let counter = context.next_counter(); // @todo This is very inefficient, we are doing a lot of back and forth conversions. - let log_slice = payload.to_be_bytes_arr(); - let log_hash = compute_unencrypted_log_hash(contract_address, payload); + let serialized_log = arr_to_be_bytes_arr(payload); + let log_hash = compute_unencrypted_log_hash(contract_address, serialized_log); // 40 = addr (32) + raw log len (4) + processed log len (4) - let len = 40 + log_slice.len().to_field(); + let len = 40 + serialized_log.len().to_field(); let side_effect = LogHash { value: log_hash, counter, length: len }; context.unencrypted_logs_hashes.push(side_effect);