Skip to content
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
9 changes: 8 additions & 1 deletion noir-projects/aztec-nr/aztec/src/event/event_interface.nr
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub unconstrained fn compute_private_serialized_event_commitment(
event_type_id: Field,
) -> Field {
let mut commitment_preimage =
BoundedVec::<_, 1 + MAX_EVENT_SERIALIZED_LEN>::from_array([randomness, event_type_id]);
BoundedVec::<_, 2 + MAX_EVENT_SERIALIZED_LEN>::from_array([randomness, event_type_id]);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was actually a bug. We are adding two fields, so it should have been + 2 all along

The new test below would fail before this fix

commitment_preimage.extend_from_bounded_vec(serialized_event);

poseidon2_hash_with_separator_bounded_vec(commitment_preimage, DOM_SEP__EVENT_COMMITMENT)
Expand All @@ -46,12 +46,19 @@ mod test {
use crate::event::event_interface::{
compute_private_event_commitment, compute_private_serialized_event_commitment, EventInterface,
};
use crate::messages::logs::event::MAX_EVENT_SERIALIZED_LEN;
use crate::protocol::traits::{Serialize, ToField};
use crate::test::mocks::mock_event::MockEvent;

global VALUE: Field = 7;
global RANDOMNESS: Field = 10;

#[test]
unconstrained fn max_size_serialized_event_commitment() {
let serialized_event = BoundedVec::from_array([0; MAX_EVENT_SERIALIZED_LEN]);
let _ = compute_private_serialized_event_commitment(serialized_event, 0, 0);
}

#[test]
unconstrained fn event_commitment_equivalence() {
let event = MockEvent::new(VALUE).build_event();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
utils::array,
};

use crate::logging::aztecnr_debug_log_format;
use crate::logging::{aztecnr_debug_log_format, aztecnr_warn_log_format};
use crate::protocol::{address::AztecAddress, hash::sha256_to_field, traits::{Deserialize, Serialize}};

/// The slot in the PXE capsules where we store a `CapsuleArray` of `DeliveredPendingPartialNote`.
Expand All @@ -38,26 +38,36 @@ pub(crate) unconstrained fn process_partial_note_private_msg(
recipient: AztecAddress,
msg_metadata: u64,
msg_content: BoundedVec<Field, MAX_MESSAGE_CONTENT_LEN>,
tx_hash: Field,
) {
// We store the information of the partial note we found in a persistent capsule in PXE, so that we can later
// search for the public log that will complete it.
let (owner, randomness, note_completion_log_tag, note_type_id, packed_private_note_content) =
decode_partial_note_private_message(msg_metadata, msg_content);

let pending = DeliveredPendingPartialNote {
owner,
randomness,
note_completion_log_tag,
note_type_id,
packed_private_note_content,
recipient,
};

CapsuleArray::at(
contract_address,
DELIVERED_PENDING_PARTIAL_NOTE_ARRAY_LENGTH_CAPSULES_SLOT,
)
.push(pending);
let decoded = decode_partial_note_private_message(msg_metadata, msg_content);

if decoded.is_some() {
// We store the information of the partial note we found in a persistent capsule in PXE, so that we can later
// search for the public log that will complete it.
let (owner, randomness, note_completion_log_tag, note_type_id, packed_private_note_content) = decoded.unwrap();

let pending = DeliveredPendingPartialNote {
owner,
randomness,
note_completion_log_tag,
note_type_id,
packed_private_note_content,
recipient,
};

CapsuleArray::at(
contract_address,
DELIVERED_PENDING_PARTIAL_NOTE_ARRAY_LENGTH_CAPSULES_SLOT,
)
.push(pending);
} else {
aztecnr_warn_log_format!(
"Could not decode partial note private message from tx {0}, ignoring",
)(
[tx_hash],
);
}
}

/// Searches for logs that would result in the completion of pending partial notes, ultimately resulting in the notes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
event::event_interface::compute_private_serialized_event_commitment,
logging::aztecnr_warn_log_format,
messages::{
encoding::MAX_MESSAGE_CONTENT_LEN, logs::event::decode_private_event_message,
processing::enqueue_event_for_validation,
Expand All @@ -14,18 +15,28 @@ pub(crate) unconstrained fn process_private_event_msg(
msg_content: BoundedVec<Field, MAX_MESSAGE_CONTENT_LEN>,
tx_hash: Field,
) {
let (event_type_id, randomness, serialized_event) = decode_private_event_message(msg_metadata, msg_content);
let decoded = decode_private_event_message(msg_metadata, msg_content);

let event_commitment =
compute_private_serialized_event_commitment(serialized_event, randomness, event_type_id.to_field());
if decoded.is_some() {
let (event_type_id, randomness, serialized_event) = decoded.unwrap();

enqueue_event_for_validation(
contract_address,
event_type_id,
randomness,
serialized_event,
event_commitment,
tx_hash,
recipient,
);
let event_commitment =
compute_private_serialized_event_commitment(serialized_event, randomness, event_type_id.to_field());

enqueue_event_for_validation(
contract_address,
event_type_id,
randomness,
serialized_event,
event_commitment,
tx_hash,
recipient,
);
} else {
aztecnr_warn_log_format!(
"Could not decode private event message from tx {0}, ignoring",
)(
[tx_hash],
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::logging::aztecnr_debug_log_format;
use crate::logging::{aztecnr_debug_log_format, aztecnr_warn_log_format};
use crate::messages::{
discovery::{ComputeNoteHashAndNullifier, nonce_discovery::attempt_note_nonce_discovery},
encoding::MAX_MESSAGE_CONTENT_LEN,
Expand All @@ -17,22 +17,31 @@ pub(crate) unconstrained fn process_private_note_msg<Env>(
msg_metadata: u64,
msg_content: BoundedVec<Field, MAX_MESSAGE_CONTENT_LEN>,
) {
let (note_type_id, owner, storage_slot, randomness, packed_note) =
decode_private_note_message(msg_metadata, msg_content);
let decoded = decode_private_note_message(msg_metadata, msg_content);

attempt_note_discovery(
contract_address,
tx_hash,
unique_note_hashes_in_tx,
first_nullifier_in_tx,
recipient,
compute_note_hash_and_nullifier,
owner,
storage_slot,
randomness,
note_type_id,
packed_note,
);
if decoded.is_some() {
let (note_type_id, owner, storage_slot, randomness, packed_note) = decoded.unwrap();

attempt_note_discovery(
contract_address,
tx_hash,
unique_note_hashes_in_tx,
first_nullifier_in_tx,
recipient,
compute_note_hash_and_nullifier,
owner,
storage_slot,
randomness,
note_type_id,
packed_note,
);
} else {
aztecnr_warn_log_format!(
"Could not decode private note message from tx {0}, ignoring",
)(
[tx_hash],
);
}
}

/// Attempts discovery of a note given information about its contents and the transaction in which it is suspected the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,64 +59,71 @@ pub(crate) unconstrained fn process_message_plaintext<ComputeNoteHashAndNullifie
// have 3 message types: private notes, partial notes and events.

// We decode the message to obtain the message type id, metadata and content.
let (msg_type_id, msg_metadata, msg_content) = decode_message(message_plaintext);
let decoded = decode_message(message_plaintext);

if msg_type_id == PRIVATE_NOTE_MSG_TYPE_ID {
aztecnr_debug_log!("Processing private note msg");
if decoded.is_some() {
let (msg_type_id, msg_metadata, msg_content) = decoded.unwrap();

process_private_note_msg(
contract_address,
message_context.tx_hash,
message_context.unique_note_hashes_in_tx,
message_context.first_nullifier_in_tx,
message_context.recipient,
compute_note_hash_and_nullifier,
msg_metadata,
msg_content,
);
} else if msg_type_id == PARTIAL_NOTE_PRIVATE_MSG_TYPE_ID {
aztecnr_debug_log!("Processing partial note private msg");
if msg_type_id == PRIVATE_NOTE_MSG_TYPE_ID {
aztecnr_debug_log!("Processing private note msg");

process_partial_note_private_msg(
contract_address,
message_context.recipient,
msg_metadata,
msg_content,
);
} else if msg_type_id == PRIVATE_EVENT_MSG_TYPE_ID {
aztecnr_debug_log!("Processing private event msg");
process_private_note_msg(
contract_address,
message_context.tx_hash,
message_context.unique_note_hashes_in_tx,
message_context.first_nullifier_in_tx,
message_context.recipient,
compute_note_hash_and_nullifier,
msg_metadata,
msg_content,
);
} else if msg_type_id == PARTIAL_NOTE_PRIVATE_MSG_TYPE_ID {
aztecnr_debug_log!("Processing partial note private msg");

process_private_event_msg(
contract_address,
message_context.recipient,
msg_metadata,
msg_content,
message_context.tx_hash,
);
} else if msg_type_id < MIN_CUSTOM_MSG_TYPE_ID {
// The message type ID falls in the range reserved for aztec.nr built-in types but wasn't matched above. This
// most likely means the message is malformed or a custom message was incorrectly assigned a reserved ID.
// Custom message types must use IDs allocated via `custom_msg_type_id`.
aztecnr_warn_log_format!(
"Message type ID {0} is in the reserved range but is not recognized, ignoring. See https://docs.aztec.network/errors/3",
)(
[msg_type_id as Field],
);
} else if process_custom_message.is_some() {
process_custom_message.unwrap()(
contract_address,
msg_type_id,
msg_metadata,
msg_content,
message_context,
);
process_partial_note_private_msg(
contract_address,
message_context.recipient,
msg_metadata,
msg_content,
message_context.tx_hash,
);
} else if msg_type_id == PRIVATE_EVENT_MSG_TYPE_ID {
aztecnr_debug_log!("Processing private event msg");

process_private_event_msg(
contract_address,
message_context.recipient,
msg_metadata,
msg_content,
message_context.tx_hash,
);
} else if msg_type_id < MIN_CUSTOM_MSG_TYPE_ID {
// The message type ID falls in the range reserved for aztec.nr built-in types but wasn't matched above.
// This most likely means the message is malformed or a custom message was incorrectly assigned a reserved
// ID. Custom message types must use IDs allocated via `custom_msg_type_id`.
aztecnr_warn_log_format!(
"Message type ID {0} is in the reserved range but is not recognized, ignoring. See https://docs.aztec.network/errors/3",
)(
[msg_type_id as Field],
);
} else if process_custom_message.is_some() {
process_custom_message.unwrap()(
contract_address,
msg_type_id,
msg_metadata,
msg_content,
message_context,
);
} else {
// A custom message was received but no handler is configured. This likely means the contract emits custom
// messages but forgot to register a handler via `AztecConfig::custom_message_handler`.
aztecnr_warn_log_format!(
"Received custom message with type id {0} but no handler is configured, ignoring. See https://docs.aztec.network/errors/2",
)(
[msg_type_id as Field],
);
}
} else {
// A custom message was received but no handler is configured. This likely means the contract emits custom
// messages but forgot to register a handler via `AztecConfig::custom_message_handler`.
aztecnr_warn_log_format!(
"Received custom message with type id {0} but no handler is configured, ignoring. See https://docs.aztec.network/errors/2",
)(
[msg_type_id as Field],
);
aztecnr_warn_log_format!("Could not decode message plaintext from tx {0}, ignoring")([message_context.tx_hash]);
}
}
Loading
Loading