Skip to content

Commit

Permalink
feat: make public l1tol2 message consumption take leafIndex (#5805)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro authored and AztecBot committed Apr 19, 2024
1 parent 8fe89f8 commit ec5da7c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
20 changes: 18 additions & 2 deletions aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier};
use dep::protocol_types::{
address::{AztecAddress, EthAddress},
constants::{L1_TO_L2_MESSAGE_LENGTH, NESTED_CALL_L2_GAS_BUFFER}, header::Header
Expand Down Expand Up @@ -128,8 +129,23 @@ impl PublicContextInterface for AvmContext {
assert(false, "'push_unencrypted_log' not required for avm - use emit_unencrypted_log!");
}

fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress) {
assert(false, "'consume_l1_to_l2_message' not implemented!");
fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, leaf_index: Field) {
let secret_hash = compute_secret_hash(secret);
let message_hash = compute_message_hash(
sender,
self.chain_id(),
/*recipient=*/self.this_address(),
self.version(),
content,
secret_hash
);
let nullifier = compute_message_nullifier(message_hash, secret, leaf_index);

assert(!self.nullifier_exists(nullifier, self.this_address()), "L1-to-L2 message is already nullified");
assert(self.l1_to_l2_msg_exists(message_hash, leaf_index), "Tried to consume nonexistent L1-to-L2 message");

// Push nullifier (and the "commitment" corresponding to this can be "empty")
self.push_new_nullifier(nullifier, 0);
}

fn message_portal(&mut self, recipient: EthAddress, content: Field) {
Expand Down
2 changes: 1 addition & 1 deletion aztec/src/context/interface.nr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait PublicContextInterface {
fn fee_per_l1_gas(self) -> Field;
fn fee_per_l2_gas(self) -> Field;
fn message_portal(&mut self, recipient: EthAddress, content: Field);
fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress);
fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, leaf_index: Field);
fn emit_unencrypted_log<T>(&mut self, log: T);
// TODO(1165) Merge push_unencrypted_log into emit_unencrypted_log, since oracle call
// in PublicContext will no longer be needed for extracting log hash
Expand Down
3 changes: 2 additions & 1 deletion aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ impl PublicContextInterface for PublicContext {

// We can consume message with a secret in public context because the message cannot be modified and therefore
// there is no front-running risk (e.g. somebody could front run you to claim your tokens to your address).
fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress) {
// Leaf index is not used in public context, but it is used in the AVMContext which will replace it.
fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, _leaf_index: Field) {
let this = (*self).this_address();
let nullifier = process_l1_to_l2_message(
self.historical_header.state.l1_to_l2_message_tree.root,
Expand Down

0 comments on commit ec5da7c

Please sign in to comment.