Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 25, 2024
1 parent 943f5dc commit fb9379f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 303 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
context::PrivateContext, event::event_interface::EventInterface,
encrypted_logs::payload::compute_encrypted_log,
keys::{getters::get_public_keys, public_keys::{OvpkM, IvpkM}},
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};

Expand Down
25 changes: 9 additions & 16 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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;

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

Expand All @@ -16,25 +15,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 @@ -148,22 +141,22 @@ fn compute_unenc_log_hash_array() {
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 hash = compute_unencrypted_log_hash(contract_address, log.to_field().to_be_bytes());
assert(hash == 0x0083ab647dfb26e7ddee90a0f4209d049d4660cab42000c544b986aaa84c55a3);
}

#[test]
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 hash = compute_unencrypted_log_hash(contract_address, log.to_field().to_be_bytes());
assert(hash == 0x00629e88ebd6374f44aa6cfe07e251ecf07213ebc7267e8f6b578ae57ffd6c20);
}

#[test]
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 hash = compute_unencrypted_log_hash(contract_address, log.to_field().to_be_bytes());
assert(hash == 0x0098637962f7d34fa202b7ffad8a07a238c5d1fd897b82a108f7f467fa73b841);
}
280 changes: 0 additions & 280 deletions noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr

This file was deleted.

1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/utils/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mod collapse_array;
mod comparison;
mod point;
mod test;
mod to_bytes;

pub use crate::utils::collapse_array::collapse_array;
25 changes: 25 additions & 0 deletions noir-projects/aztec-nr/aztec/src/utils/to_bytes.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub fn arr_to_be_bytes_arr<let L: u32>(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<let L: u32>(string: str<L>) -> [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
}
Loading

0 comments on commit fb9379f

Please sign in to comment.