Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleaning up messaging types #5442

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
use dep::protocol_types::{
address::AztecAddress,
constants::GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET,
hash::{pedersen_hash, silo_nullifier}};
address::{AztecAddress, EthAddress},
constants::{GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__NULLIFIER},
hash::{pedersen_hash, silo_nullifier}
};

use dep::protocol_types::hash::{hash_args, hash_args_array};
use dep::protocol_types::hash::{hash_args, hash_args_array, sha256_to_field};

pub fn compute_secret_hash(secret: Field) -> Field {
// TODO(#1205) This is probably not the right index to use
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
}

pub fn compute_message_hash(
sender: EthAddress,
chain_id: Field,
recipient: AztecAddress,
version: Field,
content: Field,
secret_hash: Field
) -> Field {
let mut hash_bytes = [0 as u8; 192];
let sender_bytes = sender.to_field().to_be_bytes(32);
let chain_id_bytes = chain_id.to_be_bytes(32);
let recipient_bytes = recipient.to_field().to_be_bytes(32);
let version_bytes = version.to_be_bytes(32);
let content_bytes = content.to_be_bytes(32);
let secret_hash_bytes = secret_hash.to_be_bytes(32);

for i in 0..32 {
hash_bytes[i] = sender_bytes[i];
hash_bytes[i + 32] = chain_id_bytes[i];
hash_bytes[i + 64] = recipient_bytes[i];
hash_bytes[i + 96] = version_bytes[i];
hash_bytes[i + 128] = content_bytes[i];
hash_bytes[i + 160] = secret_hash_bytes[i];
}

sha256_to_field(hash_bytes)
}

// The nullifier of a l1 to l2 message is the hash of the message salted with the secret and index of the message hash
// in the L1 to L2 message tree
pub fn compute_message_nullifier(message_hash: Field, secret: Field, leaf_index: Field) -> Field {
pedersen_hash(
[message_hash, secret, leaf_index],
GENERATOR_INDEX__NULLIFIER
)
}

pub fn compute_siloed_nullifier(address: AztecAddress, nullifier: Field) -> Field {
silo_nullifier(address, nullifier)
}
}
28 changes: 7 additions & 21 deletions noir-projects/aztec-nr/aztec/src/messaging.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod l1_to_l2_message;

use crate::oracle::get_l1_to_l2_membership_witness::get_l1_to_l2_membership_witness;
use crate::{
hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier},
oracle::get_l1_to_l2_membership_witness::get_l1_to_l2_membership_witness
};

use dep::std::merkle::compute_merkle_root;
use crate::messaging::l1_to_l2_message::L1ToL2Message;
use dep::protocol_types::{constants::L1_TO_L2_MSG_TREE_HEIGHT, address::{AztecAddress, EthAddress}, utils::arr_copy_slice};

pub fn process_l1_to_l2_message(
Expand All @@ -15,15 +15,8 @@ pub fn process_l1_to_l2_message(
content: Field,
secret: Field
) -> Field {
let mut msg = L1ToL2Message::new(
portal_contract_address,
chain_id,
storage_contract_address,
version,
content,
secret
);
let message_hash = msg.hash();
let secret_hash = compute_secret_hash(secret);
let message_hash = compute_message_hash(portal_contract_address, chain_id, storage_contract_address, version, content, secret_hash);

let returned_message = get_l1_to_l2_membership_witness(storage_contract_address, message_hash, secret);
let leaf_index = returned_message[0];
Expand All @@ -34,12 +27,5 @@ pub fn process_l1_to_l2_message(
let root = compute_merkle_root(message_hash, leaf_index, sibling_path);
assert(root == l1_to_l2_root, "Message not in state");

// Note: Had to add this line to make it work (wasted an hour debugging this). L1ToL2Message noir struct is
// an abomination and it would not have ever got to that state if people followed the logical rule of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

// "deserialize(serialize(object)) == object" always being true (there are a few params which are not
// serialzied). Will refactor that in a separate PR.
// TODO(#5420)
msg.tree_index = leaf_index;

msg.compute_nullifier()
compute_message_nullifier(message_hash, secret, leaf_index)
}
89 changes: 0 additions & 89 deletions noir-projects/aztec-nr/aztec/src/messaging/l1_to_l2_message.nr

This file was deleted.

Loading