From 54b52957187b10c4279f2391ae7552cfa73f4a97 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 07:45:49 +0000 Subject: [PATCH 01/10] wip delay logs --- ...e_kernel_circuit_public_inputs_composer.nr | 8 +- .../src/components/tail_output_composer.nr | 4 +- .../src/components/tail_output_validator.nr | 38 +-- .../tail_output_hints.nr | 33 +- .../meter_gas_used.nr | 2 +- .../tail_to_public_output_validator.nr | 12 +- .../tail_to_public_output_hints.nr | 15 +- .../src/private_kernel_tail.nr | 171 +++++----- .../validate_accumulated_values.nr | 72 ++--- .../tail_to_public_output_composer.nr | 292 +++++++++--------- .../src/public_kernel_tail.nr | 89 ++++-- .../crates/rollup-lib/src/components.nr | 30 +- .../combined_accumulated_data.nr | 68 ++-- .../public_accumulated_data.nr | 6 +- .../public_accumulated_data_builder.nr | 2 +- .../crates/types/src/abis/log_hash.nr | 10 +- .../crates/types/src/hash.nr | 31 +- .../crates/types/src/tests/fixture_builder.nr | 40 +-- .../circuit-types/src/logs/tx_l2_logs.ts | 47 ++- yarn-project/circuit-types/src/mocks.ts | 34 +- .../circuit-types/src/tx/processed_tx.ts | 12 +- yarn-project/circuit-types/src/tx/tx.ts | 2 +- .../src/structs/kernel/combine_hints.ts | 39 ++- .../kernel/combined_accumulated_data.ts | 40 ++- .../structs/kernel/public_accumulated_data.ts | 8 +- .../kernel/public_accumulated_data_builder.ts | 8 +- .../circuits.js/src/tests/factories.ts | 22 +- .../composed/integration_l1_publisher.test.ts | 9 +- .../end-to-end/src/e2e_block_building.test.ts | 2 - .../src/type_conversion.ts | 25 +- .../prover-client/src/mocks/fixtures.ts | 13 +- .../src/orchestrator/orchestrator.ts | 36 ++- 32 files changed, 701 insertions(+), 519 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/private_kernel_circuit_public_inputs_composer.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/private_kernel_circuit_public_inputs_composer.nr index 496460a498a..092c5149894 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/private_kernel_circuit_public_inputs_composer.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/private_kernel_circuit_public_inputs_composer.nr @@ -10,7 +10,7 @@ use dep::types::{ MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NOTE_HASHES_PER_TX, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL }, - hash::{silo_encrypted_log_hash, silo_note_hash, silo_nullifier}, traits::is_empty, + hash::{silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, traits::is_empty, transaction::tx_request::TxRequest, utils::arrays::{array_length, array_to_bounded_vec, sort_by_counters_asc, sort_by_counters_desc} }; @@ -276,7 +276,7 @@ impl PrivateKernelCircuitPublicInputsComposer { fn silo_scoped_values(&mut self) { self.silo_note_hashes(); self.silo_nullifiers(); - self.silo_encrypted_logs(); + self.mask_encrypted_logs(); } fn silo_note_hashes(&mut self) { @@ -298,10 +298,10 @@ impl PrivateKernelCircuitPublicInputsComposer { } } - fn silo_encrypted_logs(&mut self) { + fn mask_encrypted_logs(&mut self) { let logs = self.public_inputs.end.encrypted_logs_hashes.storage; for i in 0..logs.len() { - self.public_inputs.end.encrypted_logs_hashes.storage[i].log_hash.value = silo_encrypted_log_hash(logs[i]); + self.public_inputs.end.encrypted_logs_hashes.storage[i].contract_address = mask_encrypted_log_hash(logs[i]); } } } diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_composer.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_composer.nr index d282bedcc70..a645c56f0fa 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_composer.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_composer.nr @@ -43,8 +43,8 @@ impl TailOutputComposer { data.note_hashes = source.note_hashes.storage.map(|n: ScopedNoteHash| n.note_hash.value); data.nullifiers = source.nullifiers.storage.map(|n: ScopedNullifier| n.nullifier.value); data.l2_to_l1_msgs = source.l2_to_l1_msgs.storage.map(|m: ScopedL2ToL1Message| m.expose_to_public()); - data.note_encrypted_logs_hash = compute_tx_note_logs_hash(source.note_encrypted_logs_hashes.storage.map(|l: NoteLogHash| l.expose_to_public())); - data.encrypted_logs_hash = compute_tx_logs_hash(source.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.expose_to_public())); + data.note_encrypted_logs_hashes = source.note_encrypted_logs_hashes.storage.map(|l: NoteLogHash| l.expose_to_public()); + data.encrypted_logs_hashes = source.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.expose_to_public()); data.unencrypted_logs_hashes = source.unencrypted_logs_hashes.storage.map(|l: ScopedLogHash| l.expose_to_public()); data.note_encrypted_log_preimages_length = source.note_encrypted_logs_hashes.storage.fold(0, |len, l: NoteLogHash| len + l.length); data.encrypted_log_preimages_length = source.encrypted_logs_hashes.storage.fold(0, |len, l: ScopedEncryptedLogHash| len + l.log_hash.length); diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr index 815233c9cd8..8cae411675d 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr @@ -15,7 +15,10 @@ use dep::types::{ log_hash::{LogHash, NoteLogHash, ScopedEncryptedLogHash, ScopedLogHash} }, messaging::l2_to_l1_message::ScopedL2ToL1Message, - hash::{compute_tx_logs_hash, compute_tx_note_logs_hash, silo_encrypted_log_hash, silo_note_hash, silo_nullifier}, + hash::{ + compute_tx_logs_hash, compute_tx_note_logs_hash, silo_note_hash, + silo_nullifier, mask_encrypted_log_hash +}, traits::is_empty, utils::arrays::{assert_sorted_transformed_value_array, assert_sorted_array_with_order_hints} }; @@ -107,38 +110,37 @@ impl TailOutputValidator { fn validate_accumulated_values(self) { // note_encrypted_log_hashes - validate_value_transformation( - self.previous_kernel.end.note_encrypted_logs_hashes, - self.hints.note_encrypted_log_hashes, - |nlh: NoteLogHash, lh: LogHash| (nlh.value == lh.value) & (nlh.length == lh.length) - ); - - assert_sorted_transformed_value_array( + assert_sorted_array_with_order_hints( self.previous_kernel.end.note_encrypted_logs_hashes, - self.hints.note_encrypted_log_hashes, self.hints.sorted_note_encrypted_log_hashes, self.hints.sorted_note_encrypted_log_hash_hints ); - let hash = compute_tx_note_logs_hash(self.hints.sorted_note_encrypted_log_hashes); - assert_eq(hash, self.output.end.note_encrypted_logs_hash, "mismatch note_encrypted_logs_hash"); + assert_eq( + self.hints.sorted_note_encrypted_log_hashes.map(|log: NoteLogHash| log.expose_to_public()), self.output.end.note_encrypted_logs_hashes, "mismatch note_encrypted_logs_hashes" + ); // encrypted_log_hashes validate_value_transformation( self.previous_kernel.end.encrypted_logs_hashes, - self.hints.siloed_encrypted_log_hashes, - |slh: ScopedEncryptedLogHash, lh: LogHash| (lh.value == silo_encrypted_log_hash(slh)) & (lh.length == slh.log_hash.length) + self.hints.masked_encrypted_log_hashes, + |lh: ScopedEncryptedLogHash, mlh: ScopedLogHash| + (mlh.contract_address == mask_encrypted_log_hash(lh)) & + (lh.log_hash.value == mlh.log_hash.value) & + (lh.log_hash.length == mlh.log_hash.length) & + (mlh.log_hash.counter == 0) ); assert_sorted_transformed_value_array( self.previous_kernel.end.encrypted_logs_hashes, - self.hints.siloed_encrypted_log_hashes, - self.hints.sorted_siloed_encrypted_log_hashes, - self.hints.sorted_encrypted_log_hash_hints + self.hints.masked_encrypted_log_hashes, + self.hints.sorted_masked_encrypted_log_hashes, + self.hints.sorted_masked_encrypted_log_hash_hints ); - let hash = compute_tx_logs_hash(self.hints.sorted_siloed_encrypted_log_hashes); - assert_eq(hash, self.output.end.encrypted_logs_hash, "mismatch encrypted_logs_hash"); + assert_eq( + self.hints.sorted_masked_encrypted_log_hashes, self.output.end.encrypted_logs_hashes, "mismatch encrypted_logs_hashes" + ); // unencrypted_log_hashes assert_sorted_array_with_order_hints( diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr index d8326c2b00f..b73fcd0cce2 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr @@ -7,7 +7,7 @@ use dep::types::{ MAX_ENCRYPTED_LOGS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_UNENCRYPTED_LOGS_PER_TX }, - hash::{silo_encrypted_log_hash, silo_note_hash, silo_nullifier}, + hash::{ silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, messaging::l2_to_l1_message::ScopedL2ToL1Message, traits::{Empty, is_empty}, utils::arrays::{OrderHint, sort_by_counters_asc, sort_get_order_hints_asc} }; @@ -21,13 +21,12 @@ struct TailOutputHints { // L2 to l1 msgs. sorted_l2_to_l1_msg_hints: [OrderHint; MAX_L2_TO_L1_MSGS_PER_TX], // Note encrypted log hashes. - note_encrypted_log_hashes: [LogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], - sorted_note_encrypted_log_hashes: [LogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], + sorted_note_encrypted_log_hashes: [NoteLogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], sorted_note_encrypted_log_hash_hints: [OrderHint; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], // Encrypted log hashes. - siloed_encrypted_log_hashes: [LogHash; MAX_ENCRYPTED_LOGS_PER_TX], - sorted_siloed_encrypted_log_hashes: [LogHash; MAX_ENCRYPTED_LOGS_PER_TX], - sorted_encrypted_log_hash_hints: [OrderHint; MAX_ENCRYPTED_LOGS_PER_TX], + masked_encrypted_log_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], + sorted_masked_encrypted_log_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], + sorted_masked_encrypted_log_hash_hints: [OrderHint; MAX_ENCRYPTED_LOGS_PER_TX], // Unencrypted log hashes. sorted_unencrypted_log_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX], sorted_unencrypted_log_hash_hints: [OrderHint; MAX_UNENCRYPTED_LOGS_PER_TX], @@ -45,18 +44,17 @@ unconstrained pub fn generate_tail_output_hints(previous_kernel: PrivateKernelCi let sorted_l2_to_l1_msg_hints = sort_get_order_hints_asc(previous_kernel.end.l2_to_l1_msgs); // note_encrypted_logs - let note_encrypted_log_hashes = previous_kernel.end.note_encrypted_logs_hashes.map(|h: NoteLogHash| h.expose_to_public()); - let sorted_note_encrypted_log_hashes = sort_by_counters_asc(previous_kernel.end.note_encrypted_logs_hashes).map(|h: NoteLogHash| h.expose_to_public()); + let sorted_note_encrypted_log_hashes = sort_by_counters_asc(previous_kernel.end.note_encrypted_logs_hashes); let sorted_note_encrypted_log_hash_hints = sort_get_order_hints_asc(previous_kernel.end.note_encrypted_logs_hashes); // encrypted_logs - let mut siloed_log_hashes = previous_kernel.end.encrypted_logs_hashes; - for i in 0..siloed_log_hashes.len() { - siloed_log_hashes[i].log_hash.value = silo_encrypted_log_hash(previous_kernel.end.encrypted_logs_hashes[i]); + let mut masked_log_hashes = previous_kernel.end.encrypted_logs_hashes; + for i in 0..masked_log_hashes.len() { + masked_log_hashes[i].contract_address = mask_encrypted_log_hash(previous_kernel.end.encrypted_logs_hashes[i]); } - let sorted_siloed_encrypted_log_hashes = sort_by_counters_asc(siloed_log_hashes).map(|h: ScopedEncryptedLogHash| h.expose_to_public()); - let siloed_encrypted_log_hashes = siloed_log_hashes.map(|h: ScopedEncryptedLogHash| h.expose_to_public()); - let sorted_encrypted_log_hash_hints = sort_get_order_hints_asc(previous_kernel.end.encrypted_logs_hashes); + let sorted_masked_encrypted_log_hashes = sort_by_counters_asc(masked_log_hashes).map(|h: ScopedEncryptedLogHash| h.expose_to_public()); + let masked_encrypted_log_hashes = masked_log_hashes.map(|h: ScopedEncryptedLogHash| h.expose_to_public()); + let sorted_masked_encrypted_log_hash_hints = sort_get_order_hints_asc(previous_kernel.end.encrypted_logs_hashes); // unencrypted_logs let sorted_unencrypted_log_hashes = sort_by_counters_asc(previous_kernel.end.unencrypted_logs_hashes); @@ -67,12 +65,11 @@ unconstrained pub fn generate_tail_output_hints(previous_kernel: PrivateKernelCi sorted_nullifier_hints, siloed_nullifiers, sorted_l2_to_l1_msg_hints, - note_encrypted_log_hashes, sorted_note_encrypted_log_hashes, - sorted_siloed_encrypted_log_hashes, sorted_note_encrypted_log_hash_hints, - siloed_encrypted_log_hashes, - sorted_encrypted_log_hash_hints, + masked_encrypted_log_hashes, + sorted_masked_encrypted_log_hashes, + sorted_masked_encrypted_log_hash_hints, sorted_unencrypted_log_hashes, sorted_unencrypted_log_hash_hints } diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_composer/meter_gas_used.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_composer/meter_gas_used.nr index 3614a49df5d..4d75aa162ab 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_composer/meter_gas_used.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_composer/meter_gas_used.nr @@ -28,7 +28,7 @@ fn meter_gas_used(data: PublicAccumulatedData) -> Gas { metered_da_bytes += note_encrypted_log_preimages_length as u32; metered_l2_gas += note_encrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE; - let encrypted_log_preimages_length = data.encrypted_logs_hashes.fold(0, |len, l: LogHash| len + l.length); + let encrypted_log_preimages_length = data.encrypted_logs_hashes.fold(0, |len, l: ScopedLogHash| len + l.log_hash.length); metered_da_bytes += encrypted_log_preimages_length as u32; metered_l2_gas += encrypted_log_preimages_length as u32 * L2_GAS_PER_LOG_BYTE; diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator.nr index 837718312c5..7cd33f5aac1 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator.nr @@ -13,7 +13,7 @@ use dep::types::{ nullifier::Nullifier, public_call_request::PublicCallRequest }, messaging::l2_to_l1_message::ScopedL2ToL1Message, address::AztecAddress, - hash::{silo_encrypted_log_hash, silo_note_hash, silo_nullifier}, + hash::{silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, traits::{Empty, is_empty, is_empty_array}, utils::arrays::{ array_length, assert_split_sorted_transformed_value_arrays_asc, @@ -157,13 +157,17 @@ impl TailToPublicOutputValidator { // encrypted_logs_hashes validate_value_transformation( prev_data.encrypted_logs_hashes, - hints.siloed_encrypted_logs_hashes, - |slh: ScopedEncryptedLogHash, lh: LogHash| (lh.value == silo_encrypted_log_hash(slh)) & (lh.length == slh.log_hash.length) & (lh.counter == 0) + hints.masked_encrypted_logs_hashes, + |lh: ScopedEncryptedLogHash, mlh: ScopedLogHash| + (mlh.contract_address == mask_encrypted_log_hash(lh)) & + (lh.log_hash.value == mlh.log_hash.value) & + (lh.log_hash.length == mlh.log_hash.length) & + (mlh.log_hash.counter == 0) ); assert_split_sorted_transformed_value_arrays_asc( prev_data.encrypted_logs_hashes, - hints.siloed_encrypted_logs_hashes, + hints.masked_encrypted_logs_hashes, split_counter, output_non_revertible.encrypted_logs_hashes, output_revertible.encrypted_logs_hashes, diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator/tail_to_public_output_hints.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator/tail_to_public_output_hints.nr index 576e3b840de..e4248176275 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator/tail_to_public_output_hints.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_to_public_output_validator/tail_to_public_output_hints.nr @@ -8,7 +8,7 @@ use dep::types::{ MAX_ENCRYPTED_LOGS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_UNENCRYPTED_LOGS_PER_TX }, - hash::{silo_encrypted_log_hash, silo_note_hash, silo_nullifier}, + hash::{silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, messaging::l2_to_l1_message::ScopedL2ToL1Message, utils::arrays::{sort_get_split_order_hints_asc, sort_get_split_order_hints_desc, SplitOrderHints} }; @@ -25,7 +25,7 @@ struct TailToPublicOutputHints { note_encrypted_logs_hashes: [LogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], sorted_note_encrypted_log_hash_hints: SplitOrderHints, // Encrypted log hashes. - siloed_encrypted_logs_hashes: [LogHash; MAX_ENCRYPTED_LOGS_PER_TX], + masked_encrypted_logs_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], sorted_encrypted_log_hash_hints: SplitOrderHints, // Unencrypted log hashes. sorted_unencrypted_log_hash_hints: SplitOrderHints, @@ -59,11 +59,12 @@ unconstrained pub fn generate_tail_to_public_output_hints(previous_kernel: Priva let sorted_note_encrypted_log_hash_hints = sort_get_split_order_hints_asc(previous_kernel.end.note_encrypted_logs_hashes, split_counter); // encrypted_logs - let mut siloed_log_hashes = previous_kernel.end.encrypted_logs_hashes; - for i in 0..siloed_log_hashes.len() { - siloed_log_hashes[i].log_hash.value = silo_encrypted_log_hash(previous_kernel.end.encrypted_logs_hashes[i]); + let masked_encrypted_logs_hashes = previous_kernel.end.encrypted_logs_hashes.map( + |mut h: ScopedEncryptedLogHash| { + h.contract_address = mask_encrypted_log_hash(h); + h.expose_to_public() } - let siloed_encrypted_logs_hashes = siloed_log_hashes.map(|h: ScopedEncryptedLogHash| h.expose_to_public()); + ); let sorted_encrypted_log_hash_hints = sort_get_split_order_hints_asc(previous_kernel.end.encrypted_logs_hashes, split_counter); // unencrypted_logs @@ -80,7 +81,7 @@ unconstrained pub fn generate_tail_to_public_output_hints(previous_kernel: Priva sorted_l2_to_l1_msg_hints, note_encrypted_logs_hashes, sorted_note_encrypted_log_hash_hints, - siloed_encrypted_logs_hashes, + masked_encrypted_logs_hashes, sorted_encrypted_log_hash_hints, sorted_unencrypted_log_hash_hints, public_call_requests, diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr index fa796b323d2..d170742d07e 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr @@ -69,9 +69,8 @@ mod tests { log_hash::ScopedLogHash }, address::{AztecAddress, EthAddress}, scalar::Scalar, - hash::{sha256_to_field, silo_note_hash, silo_nullifier, compute_siloed_encrypted_log_hash}, - tests::fixture_builder::FixtureBuilder, utils::{arrays::array_length}, - traits::{Empty, is_empty}, point::Point + hash::{sha256_to_field, silo_note_hash, silo_nullifier}, tests::fixture_builder::FixtureBuilder, + utils::{arrays::array_length}, traits::{Empty, is_empty}, point::Point }; // TODO: Reduce the duplicated code/tests for PrivateKernelTailInputs and PrivateKernelTailToPublicInputs. @@ -140,49 +139,49 @@ mod tests { assert_eq(public_inputs.rollup_validation_requests.max_block_number.unwrap(), 13); } - #[test] - fn logs_are_handled_as_expected() { - let mut builder = PrivateKernelTailInputsBuilder::new(); - // Logs for the previous call stack. - let prev_encrypted_logs_hash = 80; - let prev_encrypted_log_preimages_length = 13; - let prev_unencrypted_logs_hash = 956; - let prev_unencrypted_log_preimages_length = 24; - builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); - builder.previous_kernel.add_unencrypted_log_hash( - prev_unencrypted_logs_hash, - prev_unencrypted_log_preimages_length - ); - // Logs for the current call stack. - let unencrypted_logs_hash = 26; - let unencrypted_log_preimages_length = 50; - builder.previous_kernel.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); - - let public_inputs = builder.execute(); - - assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); - assert_eq( - public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length - ); - - let siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( - builder.previous_kernel.storage_contract_address, - builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness, - prev_encrypted_logs_hash - ); - - // noir-fmt:ignore - let hash_bytes: [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] = siloed_encrypted_logs_hash - .to_be_bytes(32) - .append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 32]) - .as_array(); - let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); - assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); - - assert_eq( - public_inputs.end.unencrypted_logs_hashes, builder.previous_kernel.unencrypted_logs_hashes.storage.map(|log: ScopedLogHash| log.expose_to_public()) - ); - } + // #[test] + // fn logs_are_handled_as_expected() { + // let mut builder = PrivateKernelTailInputsBuilder::new(); + // // Logs for the previous call stack. + // let prev_encrypted_logs_hash = 80; + // let prev_encrypted_log_preimages_length = 13; + // let prev_unencrypted_logs_hash = 956; + // let prev_unencrypted_log_preimages_length = 24; + // builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); + // builder.previous_kernel.add_unencrypted_log_hash( + // prev_unencrypted_logs_hash, + // prev_unencrypted_log_preimages_length + // ); + // // Logs for the current call stack. + // let unencrypted_logs_hash = 26; + // let unencrypted_log_preimages_length = 50; + // builder.previous_kernel.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); + + // let public_inputs = builder.execute(); + + // assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); + // assert_eq( + // public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length + // ); + + // let siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( + // builder.previous_kernel.storage_contract_address, + // builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness, + // prev_encrypted_logs_hash + // ); + + // // noir-fmt:ignore + // let hash_bytes: [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] = siloed_encrypted_logs_hash + // .to_be_bytes(32) + // .append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 32]) + // .as_array(); + // let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); + // assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); + + // assert_eq( + // public_inputs.end.unencrypted_logs_hashes, builder.previous_kernel.unencrypted_logs_hashes.storage.map(|log: ScopedLogHash| log.expose_to_public()) + // ); + // } unconstrained fn compute_hash_bytes( siloed_encrypted_logs_hash: Field, @@ -191,46 +190,46 @@ mod tests { siloed_encrypted_logs_hash.to_be_bytes(32).append(new_siloed_encrypted_logs_hash.to_be_bytes(32)).append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 64]).as_array() } - #[test] - fn encrypted_logs_are_hashed_as_expected() { - let mut builder = PrivateKernelTailInputsBuilder::new(); - // Logs for the previous call stack. - let prev_encrypted_logs_hash = 80; - let prev_encrypted_log_preimages_length = 13; - builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); - - // Set the randomness to 0 to signal not masking the address to silo with - builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness = 0; - - let new_encrypted_logs_hash = 26; - let new_encrypted_log_preimages_length = 50; - builder.previous_kernel.add_encrypted_log_hash(new_encrypted_logs_hash, new_encrypted_log_preimages_length); - - let public_inputs = builder.execute(); - - assert_eq( - public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length + new_encrypted_log_preimages_length - ); - - let siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( - builder.previous_kernel.storage_contract_address, - builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness, - prev_encrypted_logs_hash - ); - - let siloed_hash_bytes: [u8; 64] = builder.previous_kernel.storage_contract_address.to_field().to_be_bytes(32).append(prev_encrypted_logs_hash.to_be_bytes(32)).as_array(); - assert_eq(siloed_encrypted_logs_hash, sha256_to_field(siloed_hash_bytes)); - - let new_siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( - builder.previous_kernel.storage_contract_address, - builder.previous_kernel.encrypted_logs_hashes.storage[1].log_hash.randomness, - new_encrypted_logs_hash - ); - - let hash_bytes = compute_hash_bytes(siloed_encrypted_logs_hash, new_siloed_encrypted_logs_hash); - let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); - assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); - } + // #[test] + // fn encrypted_logs_are_hashed_as_expected() { + // let mut builder = PrivateKernelTailInputsBuilder::new(); + // // Logs for the previous call stack. + // let prev_encrypted_logs_hash = 80; + // let prev_encrypted_log_preimages_length = 13; + // builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); + + // // Set the randomness to 0 to signal not masking the address to silo with + // builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness = 0; + + // let new_encrypted_logs_hash = 26; + // let new_encrypted_log_preimages_length = 50; + // builder.previous_kernel.add_encrypted_log_hash(new_encrypted_logs_hash, new_encrypted_log_preimages_length); + + // let public_inputs = builder.execute(); + + // assert_eq( + // public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length + new_encrypted_log_preimages_length + // ); + + // let siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( + // builder.previous_kernel.storage_contract_address, + // builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness, + // prev_encrypted_logs_hash + // ); + + // let siloed_hash_bytes: [u8; 64] = builder.previous_kernel.storage_contract_address.to_field().to_be_bytes(32).append(prev_encrypted_logs_hash.to_be_bytes(32)).as_array(); + // assert_eq(siloed_encrypted_logs_hash, sha256_to_field(siloed_hash_bytes)); + + // let new_siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( + // builder.previous_kernel.storage_contract_address, + // builder.previous_kernel.encrypted_logs_hashes.storage[1].log_hash.randomness, + // new_encrypted_logs_hash + // ); + + // let hash_bytes = compute_hash_bytes(siloed_encrypted_logs_hash, new_siloed_encrypted_logs_hash); + // let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); + // assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); + // } #[test] fn ordering_of_note_hashes_and_nullifiers() { diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr index 46109355c55..5dde4e9f67d 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr @@ -48,42 +48,42 @@ fn validate_accumulated_values_note_encrypted_log_hashes_wrong_hash_fails() { * encrypted_log_hashes */ -#[test] -fn validate_accumulated_values_encrypted_log_hashes_succeeds() { - let mut builder = TailOutputValidatorBuilder::new(); - - builder.previous_kernel.append_encrypted_log_hashes(3); - builder.output.append_encrypted_log_hashes(3); - builder.output.hash_encrypted_log_hashes(); - - builder.validate(); -} - -#[test] -fn validate_accumulated_values_encrypted_log_hashes_unordered_succeeds() { - let mut builder = TailOutputValidatorBuilder::new(); - - builder.previous_kernel.append_encrypted_log_hashes(3); - // Swap the items at index 0 and 2. - swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 0, 2); - builder.output.append_encrypted_log_hashes(3); - builder.output.hash_encrypted_log_hashes(); - - builder.validate(); -} - -#[test(should_fail_with="mismatch encrypted_logs_hash")] -fn validate_accumulated_values_encrypted_log_hashes_wrong_hash_fails() { - let mut builder = TailOutputValidatorBuilder::new(); - - builder.previous_kernel.append_encrypted_log_hashes(3); - builder.output.append_encrypted_log_hashes(3); - // Swap the items in the output, making them out of order, and then hash. - swap_items(&mut builder.output.encrypted_logs_hashes, 0, 2); - builder.output.hash_encrypted_log_hashes(); - - builder.validate(); -} +// #[test] +// fn validate_accumulated_values_encrypted_log_hashes_succeeds() { +// let mut builder = TailOutputValidatorBuilder::new(); + +// builder.previous_kernel.append_encrypted_log_hashes(3); +// builder.output.append_encrypted_log_hashes(3); +// builder.output.hash_encrypted_log_hashes(); + +// builder.validate(); +// } + +// #[test] +// fn validate_accumulated_values_encrypted_log_hashes_unordered_succeeds() { +// let mut builder = TailOutputValidatorBuilder::new(); + +// builder.previous_kernel.append_encrypted_log_hashes(3); +// // Swap the items at index 0 and 2. +// swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 0, 2); +// builder.output.append_encrypted_log_hashes(3); +// builder.output.hash_encrypted_log_hashes(); + +// builder.validate(); +// } + +// #[test(should_fail_with="mismatch encrypted_logs_hash")] +// fn validate_accumulated_values_encrypted_log_hashes_wrong_hash_fails() { +// let mut builder = TailOutputValidatorBuilder::new(); + +// builder.previous_kernel.append_encrypted_log_hashes(3); +// builder.output.append_encrypted_log_hashes(3); +// // Swap the items in the output, making them out of order, and then hash. +// swap_items(&mut builder.output.encrypted_logs_hashes, 0, 2); +// builder.output.hash_encrypted_log_hashes(); + +// builder.validate(); +// } /** * unencrypted_log_hashes diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr index 5f504f44a46..2b14a0255dc 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr @@ -8,149 +8,149 @@ use dep::types::{ tests::utils::{assert_array_eq, swap_items} }; -#[test] -fn tail_to_public_output_composer_succeeds() { - let mut (builder, siloed_data_builder) = TailToPublicOutputComposerBuilder::new().with_siloed_data_builder(); - - let teardown_gas = Gas::new(789, 3254); - builder.previous_kernel.tx_context.gas_settings.teardown_gas_limits = teardown_gas; - - // Non-revertibles. - builder.previous_kernel.append_note_hashes(4); - siloed_data_builder.append_siloed_note_hashes(4); - - builder.previous_kernel.append_nullifiers(2); - siloed_data_builder.append_siloed_nullifiers(2); - - builder.previous_kernel.append_l2_to_l1_msgs(1); - siloed_data_builder.append_exposed_l2_to_l1_msgs(1); - - builder.previous_kernel.add_note_encrypted_log_hash(1001, 12, 0); - builder.previous_kernel.add_note_encrypted_log_hash(1002, 8, 0); - - builder.previous_kernel.add_encrypted_log_hash(2001, 2); - siloed_data_builder.add_siloed_encrypted_log_hash(2001, 2); - - builder.previous_kernel.add_unencrypted_log_hash(3001, 51); - - builder.previous_kernel.append_public_call_requests(2); - - builder.previous_kernel.end_setup(); - - // Revertibles. - builder.previous_kernel.append_note_hashes(2); - siloed_data_builder.append_siloed_note_hashes(2); - - builder.previous_kernel.append_nullifiers(1); - siloed_data_builder.append_siloed_nullifiers(1); - - builder.previous_kernel.append_l2_to_l1_msgs(1); - siloed_data_builder.append_exposed_l2_to_l1_msgs(1); - - builder.previous_kernel.add_note_encrypted_log_hash(1003, 20, 0); - - builder.previous_kernel.add_encrypted_log_hash(2002, 6); - siloed_data_builder.add_siloed_encrypted_log_hash(2002, 6); - builder.previous_kernel.add_encrypted_log_hash(2003, 24); - siloed_data_builder.add_siloed_encrypted_log_hash(2003, 24); - - builder.previous_kernel.add_unencrypted_log_hash(3002, 4); - - builder.previous_kernel.append_public_call_requests(3); - - // Get ordered items before shuffling for verifying with the output later. - let siloed_data = siloed_data_builder.to_exposed_public_accumulated_data(); - let unsiloed_data = builder.previous_kernel.to_exposed_public_accumulated_data(); - - // Shuffle ordered items. - swap_items(&mut builder.previous_kernel.note_hashes, 4, 0); - swap_items(&mut builder.previous_kernel.note_hashes, 3, 2); - swap_items(&mut builder.previous_kernel.nullifiers, 1, 3); - swap_items(&mut builder.previous_kernel.l2_to_l1_msgs, 0, 1); - swap_items(&mut builder.previous_kernel.note_encrypted_logs_hashes, 1, 2); - swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 1, 2); - swap_items(&mut builder.previous_kernel.public_call_requests, 1, 2); - - // Output. - let output = builder.finish(); - - // note_hashes - let siloed = siloed_data.note_hashes; - assert_array_eq( - output.end_non_revertible.note_hashes, - [siloed[0], siloed[1], siloed[2], siloed[3]] - ); - assert_array_eq(output.end.note_hashes, [siloed[4], siloed[5]]); - - // nullifiers - let siloed = siloed_data.nullifiers; - let unsiloed = unsiloed_data.nullifiers; - assert_array_eq( - output.end_non_revertible.nullifiers, - [unsiloed[0], siloed[1], siloed[2]] - ); - assert_array_eq(output.end.nullifiers, [siloed[3]]); - - // l2_to_l1_msgs - let siloed = siloed_data.l2_to_l1_msgs; - assert_array_eq(output.end_non_revertible.l2_to_l1_msgs, [siloed[0]]); - assert_array_eq(output.end.l2_to_l1_msgs, [siloed[1]]); - - // note_encrypted_logs_hashes - let unsiloed = unsiloed_data.note_encrypted_logs_hashes; - assert_array_eq( - output.end_non_revertible.note_encrypted_logs_hashes, - [unsiloed[0], unsiloed[1]] - ); - assert_array_eq(output.end.note_encrypted_logs_hashes, [unsiloed[2]]); - - // encrypted_logs_hashes - let siloed = siloed_data.encrypted_logs_hashes; - assert_array_eq(output.end_non_revertible.encrypted_logs_hashes, [siloed[0]]); - assert_array_eq(output.end.encrypted_logs_hashes, [siloed[1], siloed[2]]); - - // unencrypted_logs_hashes - let unsiloed = unsiloed_data.unencrypted_logs_hashes; - assert_array_eq( - output.end_non_revertible.unencrypted_logs_hashes, - [unsiloed[0]] - ); - assert_array_eq(output.end.unencrypted_logs_hashes, [unsiloed[1]]); - - // public_call_stack - let unsiloed = unsiloed_data.public_call_stack; - assert_array_eq( - output.end_non_revertible.public_call_stack, - [unsiloed[1], unsiloed[0]] - ); - assert_array_eq( - output.end.public_call_stack, - [unsiloed[4], unsiloed[3], unsiloed[2]] - ); - - // Gas: non-revertible - let total_num_side_effects = 4 + 3 + 1; - let total_log_length = 12 + 8 // note_encrypted_log_hash - + 2 // encrypted_log_hash - + 51; // unencrypted_log_hash - let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; - let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH - + 3 * L2_GAS_PER_NULLIFIER - + total_log_length * L2_GAS_PER_LOG_BYTE - + 2 * FIXED_AVM_STARTUP_L2_GAS; - assert_eq( - output.end_non_revertible.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + Gas::tx_overhead() - ); - - // Gas: revertible - let total_num_side_effects = 2 + 1 + 1; - let total_log_length = 20 // note_encrypted_log_hash - + 6 + 24 // encrypted_log_hash - + 4; // unencrypted_log_hash - let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; - let computed_l2_gas = 2 * L2_GAS_PER_NOTE_HASH - + 1 * L2_GAS_PER_NULLIFIER - + total_log_length * L2_GAS_PER_LOG_BYTE - + 3 * FIXED_AVM_STARTUP_L2_GAS; - assert_eq(output.end.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + teardown_gas); -} +// #[test] +// fn tail_to_public_output_composer_succeeds() { +// let mut (builder, siloed_data_builder) = TailToPublicOutputComposerBuilder::new().with_siloed_data_builder(); + +// let teardown_gas = Gas::new(789, 3254); +// builder.previous_kernel.tx_context.gas_settings.teardown_gas_limits = teardown_gas; + +// // Non-revertibles. +// builder.previous_kernel.append_note_hashes(4); +// siloed_data_builder.append_siloed_note_hashes(4); + +// builder.previous_kernel.append_nullifiers(2); +// siloed_data_builder.append_siloed_nullifiers(2); + +// builder.previous_kernel.append_l2_to_l1_msgs(1); +// siloed_data_builder.append_exposed_l2_to_l1_msgs(1); + +// builder.previous_kernel.add_note_encrypted_log_hash(1001, 12, 0); +// builder.previous_kernel.add_note_encrypted_log_hash(1002, 8, 0); + +// builder.previous_kernel.add_encrypted_log_hash(2001, 2); +// siloed_data_builder.add_siloed_encrypted_log_hash(2001, 2); + +// builder.previous_kernel.add_unencrypted_log_hash(3001, 51); + +// builder.previous_kernel.append_public_call_requests(2); + +// builder.previous_kernel.end_setup(); + +// // Revertibles. +// builder.previous_kernel.append_note_hashes(2); +// siloed_data_builder.append_siloed_note_hashes(2); + +// builder.previous_kernel.append_nullifiers(1); +// siloed_data_builder.append_siloed_nullifiers(1); + +// builder.previous_kernel.append_l2_to_l1_msgs(1); +// siloed_data_builder.append_exposed_l2_to_l1_msgs(1); + +// builder.previous_kernel.add_note_encrypted_log_hash(1003, 20, 0); + +// builder.previous_kernel.add_encrypted_log_hash(2002, 6); +// siloed_data_builder.add_siloed_encrypted_log_hash(2002, 6); +// builder.previous_kernel.add_encrypted_log_hash(2003, 24); +// siloed_data_builder.add_siloed_encrypted_log_hash(2003, 24); + +// builder.previous_kernel.add_unencrypted_log_hash(3002, 4); + +// builder.previous_kernel.append_public_call_requests(3); + +// // Get ordered items before shuffling for verifying with the output later. +// let siloed_data = siloed_data_builder.to_exposed_public_accumulated_data(); +// let unsiloed_data = builder.previous_kernel.to_exposed_public_accumulated_data(); + +// // Shuffle ordered items. +// swap_items(&mut builder.previous_kernel.note_hashes, 4, 0); +// swap_items(&mut builder.previous_kernel.note_hashes, 3, 2); +// swap_items(&mut builder.previous_kernel.nullifiers, 1, 3); +// swap_items(&mut builder.previous_kernel.l2_to_l1_msgs, 0, 1); +// swap_items(&mut builder.previous_kernel.note_encrypted_logs_hashes, 1, 2); +// swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 1, 2); +// swap_items(&mut builder.previous_kernel.public_call_requests, 1, 2); + +// // Output. +// let output = builder.finish(); + +// // note_hashes +// let siloed = siloed_data.note_hashes; +// assert_array_eq( +// output.end_non_revertible.note_hashes, +// [siloed[0], siloed[1], siloed[2], siloed[3]] +// ); +// assert_array_eq(output.end.note_hashes, [siloed[4], siloed[5]]); + +// // nullifiers +// let siloed = siloed_data.nullifiers; +// let unsiloed = unsiloed_data.nullifiers; +// assert_array_eq( +// output.end_non_revertible.nullifiers, +// [unsiloed[0], siloed[1], siloed[2]] +// ); +// assert_array_eq(output.end.nullifiers, [siloed[3]]); + +// // l2_to_l1_msgs +// let siloed = siloed_data.l2_to_l1_msgs; +// assert_array_eq(output.end_non_revertible.l2_to_l1_msgs, [siloed[0]]); +// assert_array_eq(output.end.l2_to_l1_msgs, [siloed[1]]); + +// // note_encrypted_logs_hashes +// let unsiloed = unsiloed_data.note_encrypted_logs_hashes; +// assert_array_eq( +// output.end_non_revertible.note_encrypted_logs_hashes, +// [unsiloed[0], unsiloed[1]] +// ); +// assert_array_eq(output.end.note_encrypted_logs_hashes, [unsiloed[2]]); + +// // encrypted_logs_hashes +// let siloed = siloed_data.encrypted_logs_hashes; +// assert_array_eq(output.end_non_revertible.encrypted_logs_hashes, [siloed[0]]); +// assert_array_eq(output.end.encrypted_logs_hashes, [siloed[1], siloed[2]]); + +// // unencrypted_logs_hashes +// let unsiloed = unsiloed_data.unencrypted_logs_hashes; +// assert_array_eq( +// output.end_non_revertible.unencrypted_logs_hashes, +// [unsiloed[0]] +// ); +// assert_array_eq(output.end.unencrypted_logs_hashes, [unsiloed[1]]); + +// // public_call_stack +// let unsiloed = unsiloed_data.public_call_stack; +// assert_array_eq( +// output.end_non_revertible.public_call_stack, +// [unsiloed[1], unsiloed[0]] +// ); +// assert_array_eq( +// output.end.public_call_stack, +// [unsiloed[4], unsiloed[3], unsiloed[2]] +// ); + +// // Gas: non-revertible +// let total_num_side_effects = 4 + 3 + 1; +// let total_log_length = 12 + 8 // note_encrypted_log_hash +// + 2 // encrypted_log_hash +// + 51; // unencrypted_log_hash +// let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; +// let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH +// + 3 * L2_GAS_PER_NULLIFIER +// + total_log_length * L2_GAS_PER_LOG_BYTE +// + 2 * FIXED_AVM_STARTUP_L2_GAS; +// assert_eq( +// output.end_non_revertible.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + Gas::tx_overhead() +// ); + +// // Gas: revertible +// let total_num_side_effects = 2 + 1 + 1; +// let total_log_length = 20 // note_encrypted_log_hash +// + 6 + 24 // encrypted_log_hash +// + 4; // unencrypted_log_hash +// let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; +// let computed_l2_gas = 2 * L2_GAS_PER_NOTE_HASH +// + 1 * L2_GAS_PER_NULLIFIER +// + total_log_length * L2_GAS_PER_LOG_BYTE +// + 3 * FIXED_AVM_STARTUP_L2_GAS; +// assert_eq(output.end.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + teardown_gas); +// } diff --git a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr index d4bf8494520..f7e087a6b97 100644 --- a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr +++ b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr @@ -337,6 +337,25 @@ mod tests { let sorted_unencrypted_logs_hashes = sorted.sorted_array; let sorted_unencrypted_logs_hashes_indexes = sorted.sorted_index_hints; + let merged = array_merge( + previous_kernel.public_inputs.end_non_revertible.encrypted_logs_hashes, + previous_kernel.public_inputs.end.encrypted_logs_hashes + ); + let sorted = sort_get_sorted_hints( + merged, + |a: ScopedLogHash, b: ScopedLogHash| a.counter() < b.counter() + ); + let sorted_encrypted_logs_hashes = sorted.sorted_array; + let sorted_encrypted_logs_hashes_indexes = sorted.sorted_index_hints; + + let merged = array_merge( + previous_kernel.public_inputs.end_non_revertible.note_encrypted_logs_hashes, + previous_kernel.public_inputs.end.note_encrypted_logs_hashes + ); + let sorted = sort_get_sorted_hints(merged, |a: LogHash, b: LogHash| a.counter() < b.counter()); + let sorted_note_encrypted_logs_hashes = sorted.sorted_array; + let sorted_note_encrypted_logs_hashes_indexes = sorted.sorted_index_hints; + let merged = array_merge( previous_kernel.public_inputs.end_non_revertible.public_data_update_requests, previous_kernel.public_inputs.end.public_data_update_requests @@ -357,6 +376,10 @@ mod tests { sorted_note_hashes_indexes, sorted_unencrypted_logs_hashes, sorted_unencrypted_logs_hashes_indexes, + sorted_encrypted_logs_hashes, + sorted_encrypted_logs_hashes_indexes, + sorted_note_encrypted_logs_hashes, + sorted_note_encrypted_logs_hashes_indexes, sorted_public_data_update_requests: merged, sorted_public_data_update_requests_indexes, deduped_public_data_update_requests: merged, @@ -392,39 +415,39 @@ mod tests { // TODO: Check the values in public inputs. } - #[test] - fn logs_are_handled_as_expected() { - let mut builder = PublicKernelTailCircuitPrivateInputsBuilder::new(); - // Logs for the previous call stack. - let prev_encrypted_logs_hash = 80; - let prev_encrypted_log_preimages_length = 13; - let prev_unencrypted_logs_hash = 956; - let prev_unencrypted_log_preimages_length = 24; - builder.previous_revertible.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); - builder.previous_revertible.add_unencrypted_log_hash( - prev_unencrypted_logs_hash, - prev_unencrypted_log_preimages_length - ); - // Logs for the current call stack. - let unencrypted_logs_hash = 26; - let unencrypted_log_preimages_length = 50; - builder.previous_revertible.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); - - let public_inputs = builder.execute(); - - assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); - assert_eq( - public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length - ); - - let hash_bytes: [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] = prev_encrypted_logs_hash.to_be_bytes(32).append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 32]).as_array(); - let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); - assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); - - assert_eq( - public_inputs.end.unencrypted_logs_hashes, builder.previous_revertible.unencrypted_logs_hashes.storage - ); - } + // #[test] + // fn logs_are_handled_as_expected() { + // let mut builder = PublicKernelTailCircuitPrivateInputsBuilder::new(); + // // Logs for the previous call stack. + // let prev_encrypted_logs_hash = 80; + // let prev_encrypted_log_preimages_length = 13; + // let prev_unencrypted_logs_hash = 956; + // let prev_unencrypted_log_preimages_length = 24; + // builder.previous_revertible.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); + // builder.previous_revertible.add_unencrypted_log_hash( + // prev_unencrypted_logs_hash, + // prev_unencrypted_log_preimages_length + // ); + // // Logs for the current call stack. + // let unencrypted_logs_hash = 26; + // let unencrypted_log_preimages_length = 50; + // builder.previous_revertible.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); + + // let public_inputs = builder.execute(); + + // assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); + // assert_eq( + // public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length + // ); + + // let hash_bytes: [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] = prev_encrypted_logs_hash.to_be_bytes(32).append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 32]).as_array(); + // let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); + // assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); + + // assert_eq( + // public_inputs.end.unencrypted_logs_hashes, builder.previous_revertible.unencrypted_logs_hashes.storage + // ); + // } #[test] unconstrained fn one_pending_nullifier_read_request() { diff --git a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/components.nr b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/components.nr index 1c98477839c..3d2a27be0e1 100644 --- a/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/components.nr +++ b/noir-projects/noir-protocol-circuits/crates/rollup-lib/src/components.nr @@ -1,7 +1,10 @@ use crate::abis::base_or_merge_rollup_public_inputs::BaseOrMergeRollupPublicInputs; use crate::abis::previous_rollup_data::PreviousRollupData; use dep::types::{ - hash::{accumulate_sha256, silo_unencrypted_log_hash, compute_tx_logs_hash}, + hash::{ + accumulate_sha256, silo_unencrypted_log_hash, compute_tx_logs_hash, silo_encrypted_log_hash, + compute_tx_note_logs_hash +}, merkle_tree::VariableMerkleTree, constants::{ MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, @@ -112,8 +115,8 @@ pub fn compute_txs_effects_hash(previous_rollup_data: [PreviousRollupData; 2]) - ) } -fn scope_and_hash_unencrypted_logs(unencrypted_logs_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX]) -> Field { - let scoped_logs = unencrypted_logs_hashes.map( +fn silo_and_hash_unencrypted_logs(unencrypted_logs_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX]) -> Field { + let siloed_logs = unencrypted_logs_hashes.map( |log: ScopedLogHash| { LogHash { value: silo_unencrypted_log_hash(log), @@ -122,7 +125,20 @@ fn scope_and_hash_unencrypted_logs(unencrypted_logs_hashes: [ScopedLogHash; MAX_ } } ); - compute_tx_logs_hash(scoped_logs) + compute_tx_logs_hash(siloed_logs) +} + +fn silo_and_hash_encrypted_logs(encrypted_logs_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX]) -> Field { + let siloed_encrypted_logs = encrypted_logs_hashes.map( + |log: ScopedLogHash| { + LogHash { + value: silo_encrypted_log_hash(log), + counter: log.log_hash.counter, + length: log.log_hash.length + } + } + ); + compute_tx_logs_hash(siloed_encrypted_logs) } // Tx effects hash consists of @@ -161,9 +177,9 @@ pub fn compute_tx_effects_hash( let note_logs_length = combined.note_encrypted_log_preimages_length; let encrypted_logs_length = combined.encrypted_log_preimages_length; let unencrypted_logs_length = combined.unencrypted_log_preimages_length; - let note_encrypted_logs_hash = combined.note_encrypted_logs_hash; - let encrypted_logs_hash = combined.encrypted_logs_hash; - let unencrypted_logs_hash = scope_and_hash_unencrypted_logs(combined.unencrypted_logs_hashes); + let note_encrypted_logs_hash = compute_tx_note_logs_hash(combined.note_encrypted_logs_hashes); + let encrypted_logs_hash = silo_and_hash_encrypted_logs(combined.encrypted_logs_hashes); + let unencrypted_logs_hash = silo_and_hash_unencrypted_logs(combined.unencrypted_logs_hashes); let mut offset = 0; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr index 79196bb3137..789eafcbfaa 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/combined_accumulated_data.nr @@ -9,7 +9,7 @@ use crate::{ constants::{ MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, COMBINED_ACCUMULATED_DATA_LENGTH, - MAX_UNENCRYPTED_LOGS_PER_TX + MAX_UNENCRYPTED_LOGS_PER_TX, MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_ENCRYPTED_LOGS_PER_TX }, hash::silo_note_hash, utils::{arrays::{array_merge, assert_sorted_array, assert_deduped_array, check_permutation}, reader::Reader}, @@ -19,6 +19,10 @@ use crate::{ struct CombineHints { sorted_note_hashes: [ScopedNoteHash; MAX_NOTE_HASHES_PER_TX], sorted_note_hashes_indexes: [u32; MAX_NOTE_HASHES_PER_TX], + sorted_note_encrypted_logs_hashes: [LogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], + sorted_note_encrypted_logs_hashes_indexes: [u32; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], + sorted_encrypted_logs_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], + sorted_encrypted_logs_hashes_indexes: [u32; MAX_ENCRYPTED_LOGS_PER_TX], sorted_unencrypted_logs_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX], sorted_unencrypted_logs_hashes_indexes: [u32; MAX_UNENCRYPTED_LOGS_PER_TX], // the public data update requests are sorted by their leaf index AND counter @@ -34,8 +38,8 @@ struct CombinedAccumulatedData { nullifiers: [Field; MAX_NULLIFIERS_PER_TX], l2_to_l1_msgs: [ScopedL2ToL1Message; MAX_L2_TO_L1_MSGS_PER_TX], - note_encrypted_logs_hash: Field, - encrypted_logs_hash: Field, + note_encrypted_logs_hashes: [LogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], + encrypted_logs_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], unencrypted_logs_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX], // Here so that the gas cost of this request can be measured by circuits, without actually needing to feed in the @@ -98,18 +102,26 @@ impl CombinedAccumulatedData { combine_hints.deduped_public_data_update_requests_runs ); - // TODO(Miranda): Hash here or elsewhere? - let note_encrypted_logs_hash = compute_tx_note_logs_hash( - array_merge( - non_revertible.note_encrypted_logs_hashes, - revertible.note_encrypted_logs_hashes - ) + let merged_note_encrypted_logs_hashes = array_merge( + non_revertible.note_encrypted_logs_hashes, + revertible.note_encrypted_logs_hashes ); - let encrypted_logs_hash = compute_tx_logs_hash( - array_merge( - non_revertible.encrypted_logs_hashes, - revertible.encrypted_logs_hashes - ) + assert_sorted_array( + merged_note_encrypted_logs_hashes, + combine_hints.sorted_note_encrypted_logs_hashes, + combine_hints.sorted_note_encrypted_logs_hashes_indexes, + asc_sort_by_counters + ); + + let merged_encrypted_logs_hashes = array_merge( + non_revertible.encrypted_logs_hashes, + revertible.encrypted_logs_hashes + ); + assert_sorted_array( + merged_encrypted_logs_hashes, + combine_hints.sorted_encrypted_logs_hashes, + combine_hints.sorted_encrypted_logs_hashes_indexes, + asc_sort_by_counters ); let merged_unencrypted_logs_hashes = array_merge( @@ -125,16 +137,16 @@ impl CombinedAccumulatedData { let note_encrypted_log_preimages_length = non_revertible.note_encrypted_logs_hashes.fold(0, |a, b: LogHash| a + b.length) + revertible.note_encrypted_logs_hashes.fold(0, |a, b: LogHash| a + b.length); - let encrypted_log_preimages_length = non_revertible.encrypted_logs_hashes.fold(0, |a, b: LogHash| a + b.length) - + revertible.encrypted_logs_hashes.fold(0, |a, b: LogHash| a + b.length); + let encrypted_log_preimages_length = non_revertible.encrypted_logs_hashes.fold(0, |a, b: ScopedLogHash| a + b.log_hash.length) + + revertible.encrypted_logs_hashes.fold(0, |a, b: ScopedLogHash| a + b.log_hash.length); let unencrypted_log_preimages_length = non_revertible.unencrypted_logs_hashes.fold(0, |a, b: ScopedLogHash| a + b.log_hash.length) + revertible.unencrypted_logs_hashes.fold(0, |a, b: ScopedLogHash| a + b.log_hash.length); CombinedAccumulatedData { note_hashes: siloed_note_hashes, nullifiers: array_merge(non_revertible.nullifiers, revertible.nullifiers).map(|n: Nullifier| n.value), l2_to_l1_msgs: array_merge(non_revertible.l2_to_l1_msgs, revertible.l2_to_l1_msgs), - note_encrypted_logs_hash, - encrypted_logs_hash, + note_encrypted_logs_hashes: combine_hints.sorted_note_encrypted_logs_hashes, + encrypted_logs_hashes: combine_hints.sorted_encrypted_logs_hashes, unencrypted_logs_hashes: combine_hints.sorted_unencrypted_logs_hashes, note_encrypted_log_preimages_length, encrypted_log_preimages_length, @@ -151,8 +163,8 @@ impl Empty for CombinedAccumulatedData { note_hashes: [0; MAX_NOTE_HASHES_PER_TX], nullifiers: [0; MAX_NULLIFIERS_PER_TX], l2_to_l1_msgs: [ScopedL2ToL1Message::empty(); MAX_L2_TO_L1_MSGS_PER_TX], - note_encrypted_logs_hash: 0, - encrypted_logs_hash: 0, + note_encrypted_logs_hashes: [LogHash::empty(); MAX_NOTE_ENCRYPTED_LOGS_PER_TX], + encrypted_logs_hashes: [ScopedLogHash::empty(); MAX_ENCRYPTED_LOGS_PER_TX], unencrypted_logs_hashes: [ScopedLogHash::empty(); MAX_UNENCRYPTED_LOGS_PER_TX], note_encrypted_log_preimages_length: 0, encrypted_log_preimages_length: 0, @@ -172,8 +184,12 @@ impl Serialize for CombinedAccumulatedData { for i in 0..self.l2_to_l1_msgs.len() { fields.extend_from_array(self.l2_to_l1_msgs[i].serialize()); } - fields.push(self.note_encrypted_logs_hash); - fields.push(self.encrypted_logs_hash); + for i in 0..self.note_encrypted_logs_hashes.len() { + fields.extend_from_array(self.note_encrypted_logs_hashes[i].serialize()); + } + for i in 0..self.encrypted_logs_hashes.len() { + fields.extend_from_array(self.encrypted_logs_hashes[i].serialize()); + } for i in 0..self.unencrypted_logs_hashes.len() { fields.extend_from_array(self.unencrypted_logs_hashes[i].serialize()); } @@ -201,8 +217,8 @@ impl Deserialize for CombinedAccumulatedData { note_hashes: reader.read_array([0; MAX_NOTE_HASHES_PER_TX]), nullifiers: reader.read_array([0; MAX_NULLIFIERS_PER_TX]), l2_to_l1_msgs: reader.read_struct_array(ScopedL2ToL1Message::deserialize,[ScopedL2ToL1Message::empty(); MAX_L2_TO_L1_MSGS_PER_TX]), - note_encrypted_logs_hash: reader.read(), - encrypted_logs_hash: reader.read(), + note_encrypted_logs_hashes: reader.read_struct_array(LogHash::deserialize, [LogHash::empty(); MAX_NOTE_ENCRYPTED_LOGS_PER_TX]), + encrypted_logs_hashes: reader.read_struct_array(ScopedLogHash::deserialize, [ScopedLogHash::empty(); MAX_ENCRYPTED_LOGS_PER_TX]), unencrypted_logs_hashes: reader.read_struct_array(ScopedLogHash::deserialize, [ScopedLogHash::empty(); MAX_UNENCRYPTED_LOGS_PER_TX]), note_encrypted_log_preimages_length: reader.read(), encrypted_log_preimages_length: reader.read(), @@ -220,8 +236,8 @@ impl Eq for CombinedAccumulatedData { (self.note_hashes == other.note_hashes) & (self.nullifiers == other.nullifiers) & (self.l2_to_l1_msgs == other.l2_to_l1_msgs) & - (self.note_encrypted_logs_hash == other.note_encrypted_logs_hash) & - (self.encrypted_logs_hash == other.encrypted_logs_hash) & + (self.note_encrypted_logs_hashes == other.note_encrypted_logs_hashes) & + (self.encrypted_logs_hashes == other.encrypted_logs_hashes) & (self.unencrypted_logs_hashes == other.unencrypted_logs_hashes) & (self.note_encrypted_log_preimages_length == other.note_encrypted_log_preimages_length) & (self.encrypted_log_preimages_length == other.encrypted_log_preimages_length) & diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr index aa43a1c4fe9..0367a9d9673 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data.nr @@ -19,7 +19,7 @@ struct PublicAccumulatedData { l2_to_l1_msgs: [ScopedL2ToL1Message; MAX_L2_TO_L1_MSGS_PER_TX], note_encrypted_logs_hashes: [LogHash; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], - encrypted_logs_hashes: [LogHash; MAX_ENCRYPTED_LOGS_PER_TX], + encrypted_logs_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], unencrypted_logs_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX], public_data_update_requests: [PublicDataUpdateRequest; MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX], @@ -36,7 +36,7 @@ impl Empty for PublicAccumulatedData { nullifiers: [Nullifier::empty(); MAX_NULLIFIERS_PER_TX], l2_to_l1_msgs: [ScopedL2ToL1Message::empty(); MAX_L2_TO_L1_MSGS_PER_TX], note_encrypted_logs_hashes: [LogHash::empty(); MAX_NOTE_ENCRYPTED_LOGS_PER_TX], - encrypted_logs_hashes: [LogHash::empty(); MAX_ENCRYPTED_LOGS_PER_TX], + encrypted_logs_hashes: [ScopedLogHash::empty(); MAX_ENCRYPTED_LOGS_PER_TX], unencrypted_logs_hashes: [ScopedLogHash::empty(); MAX_UNENCRYPTED_LOGS_PER_TX], public_data_update_requests: [PublicDataUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX], public_call_stack: [PublicCallRequest::empty(); MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX], @@ -98,7 +98,7 @@ impl Deserialize for PublicAccumulatedData { nullifiers: reader.read_struct_array(Nullifier::deserialize, [Nullifier::empty(); MAX_NULLIFIERS_PER_TX]), l2_to_l1_msgs: reader.read_struct_array(ScopedL2ToL1Message::deserialize, [ScopedL2ToL1Message::empty(); MAX_L2_TO_L1_MSGS_PER_TX]), note_encrypted_logs_hashes: reader.read_struct_array(LogHash::deserialize, [LogHash::empty(); MAX_NOTE_ENCRYPTED_LOGS_PER_TX]), - encrypted_logs_hashes: reader.read_struct_array(LogHash::deserialize, [LogHash::empty(); MAX_ENCRYPTED_LOGS_PER_TX]), + encrypted_logs_hashes: reader.read_struct_array(ScopedLogHash::deserialize, [ScopedLogHash::empty(); MAX_ENCRYPTED_LOGS_PER_TX]), unencrypted_logs_hashes: reader.read_struct_array(ScopedLogHash::deserialize, [ScopedLogHash::empty(); MAX_UNENCRYPTED_LOGS_PER_TX]), public_data_update_requests: reader.read_struct_array(PublicDataUpdateRequest::deserialize, [PublicDataUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX]), public_call_stack: reader.read_struct_array(PublicCallRequest::deserialize, [PublicCallRequest::empty(); MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX]), diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr index 8c9f42ab5e5..d193809e0e4 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/accumulated_data/public_accumulated_data_builder.nr @@ -19,7 +19,7 @@ struct PublicAccumulatedDataBuilder { l2_to_l1_msgs: BoundedVec, note_encrypted_logs_hashes: BoundedVec, - encrypted_logs_hashes: BoundedVec, + encrypted_logs_hashes: BoundedVec, unencrypted_logs_hashes: BoundedVec, public_data_update_requests: BoundedVec, diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/log_hash.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/log_hash.nr index f5983f4dbde..9945e7b4fa7 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/log_hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/log_hash.nr @@ -221,11 +221,13 @@ impl Scoped for ScopedEncryptedLogHash { } impl ScopedEncryptedLogHash { - pub fn expose_to_public(self) -> LogHash { + pub fn expose_to_public(self) -> ScopedLogHash { // Hide the secret randomness and counter when exposing to public - // Expose as a LogHash rather than EncryptedLogHash to avoid bringing an unnec. 0 value around - // The log hash will already be silo'd when we call this - LogHash { value: self.log_hash.value, counter: 0, length: self.log_hash.length } + // Expose as a ScopedLogHash. The contract address is assumed to be masked before calling this. + ScopedLogHash { + contract_address: self.contract_address, + log_hash: LogHash { value: self.log_hash.value, counter: 0, length: self.log_hash.length } + } } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr index bd82a508952..bd689c65c5a 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -88,27 +88,24 @@ pub fn silo_nullifier(nullifier: ScopedNullifier) -> Field { } } -pub fn compute_siloed_encrypted_log_hash(address: AztecAddress, randomness: Field, log_hash: Field) -> Field { - // TODO: Using 0 GENERATOR_INDEX here as interim before we move to posiedon - // NB: A unique separator will be needed for masked_contract_address - let mut masked_contract_address = poseidon2_hash_with_separator([address.to_field(), randomness], 0); - if randomness == 0 { - // In some cases, we actually want to reveal the contract address we are siloing with: - // e.g. 'handshaking' contract w/ known address - // An app providing randomness = 0 signals to not mask the address. - masked_contract_address = address.to_field(); +pub fn silo_encrypted_log_hash(log_hash: ScopedLogHash) -> Field { + // We assume contract address has already been masked + if log_hash.contract_address.is_zero() { + 0 + } else { + accumulate_sha256([log_hash.contract_address.to_field(), log_hash.log_hash.value]) } - accumulate_sha256([masked_contract_address, log_hash]) } -pub fn silo_encrypted_log_hash(log_hash: ScopedEncryptedLogHash) -> Field { - if log_hash.contract_address.is_zero() { - 0 +pub fn mask_encrypted_log_hash(scoped_log: ScopedEncryptedLogHash) -> AztecAddress { + if scoped_log.contract_address.is_zero() | (scoped_log.log_hash.randomness == 0) { + AztecAddress::from_field(0) } else { - compute_siloed_encrypted_log_hash( - log_hash.contract_address, - log_hash.log_hash.randomness, - log_hash.log_hash.value + AztecAddress::from_field( + poseidon2_hash_with_separator( + [scoped_log.contract_address.to_field(), scoped_log.log_hash.randomness], + 0 + ) ) } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr index 8f067225330..9f22bfc551e 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr @@ -322,7 +322,9 @@ impl FixtureBuilder { len: self.note_encrypted_logs_hashes.len() }; let encrypted_logs_hashes = BoundedVec { - storage: self.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.log_hash).map(|l: EncryptedLogHash| LogHash { value: l.value, counter: l.counter, length: l.length }), + storage: self.encrypted_logs_hashes.storage.map( + |l: ScopedEncryptedLogHash| ScopedLogHash { log_hash: LogHash { value: l.log_hash.value, counter: l.log_hash.counter, length: l.log_hash.length}, contract_address: l.contract_address } + ), len: self.encrypted_logs_hashes.len() }; @@ -362,8 +364,8 @@ impl FixtureBuilder { note_hashes: self.note_hashes.storage.map(|n: ScopedNoteHash| n.note_hash.value), nullifiers: self.nullifiers.storage.map(|n: ScopedNullifier| n.nullifier.value), l2_to_l1_msgs: self.l2_to_l1_msgs.storage.map(|m: ScopedL2ToL1Message| m.expose_to_public()), - note_encrypted_logs_hash: self.note_encrypted_logs_hash, - encrypted_logs_hash: self.encrypted_logs_hash, + note_encrypted_logs_hashes: self.note_encrypted_logs_hashes.storage.map(|l: NoteLogHash| l.expose_to_public()), + encrypted_logs_hashes: self.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.expose_to_public()), unencrypted_logs_hashes: self.unencrypted_logs_hashes.storage.map(|l: ScopedLogHash| l.expose_to_public()), note_encrypted_log_preimages_length: self.note_encrypted_log_preimages_length, encrypted_log_preimages_length: self.encrypted_log_preimages_length, @@ -728,12 +730,12 @@ impl FixtureBuilder { self.encrypted_log_preimages_length += length; } - pub fn add_siloed_encrypted_log_hash(&mut self, hash: Field, length: Field) { - let mut log_hash = EncryptedLogHash { value: hash, counter: self.next_counter(), length, randomness: 2 }.scope(self.storage_contract_address); - log_hash.log_hash.value = silo_encrypted_log_hash(log_hash); - self.encrypted_logs_hashes.push(log_hash); - self.encrypted_log_preimages_length += length; - } + // pub fn add_siloed_encrypted_log_hash(&mut self, hash: Field, length: Field) { + // let mut log_hash = EncryptedLogHash { value: hash, counter: self.next_counter(), length, randomness: 2 }.scope(self.storage_contract_address); + // log_hash.log_hash.value = silo_encrypted_log_hash(log_hash); + // self.encrypted_logs_hashes.push(log_hash); + // self.encrypted_log_preimages_length += length; + // } pub fn append_encrypted_log_hashes(&mut self, num: u32) { let index_offset = self.encrypted_logs_hashes.len(); @@ -745,16 +747,16 @@ impl FixtureBuilder { } } - pub fn hash_encrypted_log_hashes(&mut self) { - let mut log_hashes = self.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.expose_to_public()); - for i in 0..self.encrypted_logs_hashes.max_len() { - let log_hash = self.encrypted_logs_hashes.storage[i]; - if !log_hash.contract_address.is_zero() { - log_hashes[i].value = silo_encrypted_log_hash(log_hash); - } - } - self.encrypted_logs_hash = compute_tx_logs_hash(log_hashes); - } + // pub fn hash_encrypted_log_hashes(&mut self) { + // let mut log_hashes = self.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.expose_to_public()); + // for i in 0..self.encrypted_logs_hashes.max_len() { + // let log_hash = self.encrypted_logs_hashes.storage[i]; + // if !log_hash.contract_address.is_zero() { + // log_hashes[i].value = silo_encrypted_log_hash(log_hash); + // } + // } + // self.encrypted_logs_hash = compute_tx_logs_hash(log_hashes); + // } pub fn add_unencrypted_log_hash(&mut self, hash: Field, length: Field) { let log_hash = LogHash { value: hash, counter: self.next_counter(), length }; diff --git a/yarn-project/circuit-types/src/logs/tx_l2_logs.ts b/yarn-project/circuit-types/src/logs/tx_l2_logs.ts index 37687e45016..69117cb3a97 100644 --- a/yarn-project/circuit-types/src/logs/tx_l2_logs.ts +++ b/yarn-project/circuit-types/src/logs/tx_l2_logs.ts @@ -301,21 +301,29 @@ export class EncryptedNoteTxL2Logs extends TxL2Logs { * for more details. */ public override hash(): Buffer { - const unrolledLogs = this.unrollLogs(); - if (unrolledLogs.length == 0) { + return EncryptedNoteTxL2Logs.hashNoteLogs(this.unrollLogs().map(log => log.hash())); + } + + /** + * Hashes encrypted note logs hashes as in the same way as the base rollup would. + * @param siloedLogHashes - The note log hashes + * @returns The hash of the log hashes. + */ + public static hashNoteLogs(logHashes: Buffer[]): Buffer { + if (logHashes.length == 0) { return Buffer.alloc(32); } - let flattenedLogs = Buffer.alloc(0); - for (const logsFromSingleFunctionCall of unrolledLogs) { - flattenedLogs = Buffer.concat([flattenedLogs, logsFromSingleFunctionCall.hash()]); + let allSiloedLogHashes = Buffer.alloc(0); + for (const siloedLogHash of logHashes) { + allSiloedLogHashes = Buffer.concat([allSiloedLogHashes, siloedLogHash]); } // pad the end of logs with 0s - for (let i = 0; i < MAX_NOTE_ENCRYPTED_LOGS_PER_TX - unrolledLogs.length; i++) { - flattenedLogs = Buffer.concat([flattenedLogs, Buffer.alloc(32)]); + for (let i = 0; i < MAX_UNENCRYPTED_LOGS_PER_TX - logHashes.length; i++) { + allSiloedLogHashes = Buffer.concat([allSiloedLogHashes, Buffer.alloc(32)]); } - return sha256Trunc(flattenedLogs); + return sha256Trunc(allSiloedLogHashes); } } @@ -381,19 +389,28 @@ export class EncryptedTxL2Logs extends TxL2Logs { */ public override hash(): Buffer { const unrolledLogs = this.unrollLogs(); - if (unrolledLogs.length == 0) { + return EncryptedTxL2Logs.hashSiloedLogs(unrolledLogs.map(log => log.getSiloedHash())); + } + + /** + * Hashes siloed unencrypted logs as in the same way as the base rollup would. + * @param siloedLogHashes - The siloed log hashes + * @returns The hash of the logs. + */ + public static hashSiloedLogs(siloedLogHashes: Buffer[]): Buffer { + if (siloedLogHashes.length == 0) { return Buffer.alloc(32); } - let flattenedLogs = Buffer.alloc(0); - for (const logsFromSingleFunctionCall of unrolledLogs) { - flattenedLogs = Buffer.concat([flattenedLogs, logsFromSingleFunctionCall.getSiloedHash()]); + let allSiloedLogHashes = Buffer.alloc(0); + for (const siloedLogHash of siloedLogHashes) { + allSiloedLogHashes = Buffer.concat([allSiloedLogHashes, siloedLogHash]); } // pad the end of logs with 0s - for (let i = 0; i < MAX_ENCRYPTED_LOGS_PER_TX - unrolledLogs.length; i++) { - flattenedLogs = Buffer.concat([flattenedLogs, Buffer.alloc(32)]); + for (let i = 0; i < MAX_UNENCRYPTED_LOGS_PER_TX - siloedLogHashes.length; i++) { + allSiloedLogHashes = Buffer.concat([allSiloedLogHashes, Buffer.alloc(32)]); } - return sha256Trunc(flattenedLogs); + return sha256Trunc(allSiloedLogHashes); } } diff --git a/yarn-project/circuit-types/src/mocks.ts b/yarn-project/circuit-types/src/mocks.ts index 6ddd14e64b0..a72c6d8a7d8 100644 --- a/yarn-project/circuit-types/src/mocks.ts +++ b/yarn-project/circuit-types/src/mocks.ts @@ -4,6 +4,8 @@ import { ClientIvcProof, GasSettings, LogHash, + MAX_ENCRYPTED_LOGS_PER_TX, + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, MAX_UNENCRYPTED_LOGS_PER_TX, @@ -23,7 +25,7 @@ import { } from '@aztec/circuits.js/testing'; import { type ContractArtifact, NoteSelector } from '@aztec/foundation/abi'; import { makeTuple } from '@aztec/foundation/array'; -import { times } from '@aztec/foundation/collection'; +import { padArrayEnd, times } from '@aztec/foundation/collection'; import { randomBytes } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { type ContractInstanceWithAddress, SerializableContractInstance } from '@aztec/types/contracts'; @@ -118,11 +120,14 @@ export const mockTx = ( functionLog.logs.forEach(log => { // ts complains if we dont check .forPublic here, even though it is defined ^ if (data.forPublic) { - const hash = new LogHash( - Fr.fromBuffer(log.getSiloedHash()), - i++, - // +4 for encoding the length of the buffer - new Fr(log.length + 4), + const hash = new ScopedLogHash( + new LogHash( + Fr.fromBuffer(log.getSiloedHash()), + i++, + // +4 for encoding the length of the buffer + new Fr(log.length + 4), + ), + log.maskedContractAddress, ); // make the first log non-revertible if (functionCount === 0) { @@ -162,8 +167,21 @@ export const mockTx = ( } } else { data.forRollup!.end.nullifiers[0] = firstNullifier.value; - data.forRollup!.end.noteEncryptedLogsHash = Fr.fromBuffer(noteEncryptedLogs.hash()); - data.forRollup!.end.encryptedLogsHash = Fr.fromBuffer(encryptedLogs.hash()); + data.forRollup!.end.noteEncryptedLogsHashes = padArrayEnd( + noteEncryptedLogs.unrollLogs().map(log => new LogHash(Fr.fromBuffer(log.hash()), 0, new Fr(log.length))), + LogHash.empty(), + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, + ); + data.forRollup!.end.encryptedLogsHashes = padArrayEnd( + encryptedLogs + .unrollLogs() + .map( + log => + new ScopedLogHash(new LogHash(Fr.fromBuffer(log.hash()), 0, new Fr(log.length)), log.maskedContractAddress), + ), + ScopedLogHash.empty(), + MAX_ENCRYPTED_LOGS_PER_TX, + ); data.forRollup!.end.unencryptedLogsHashes = makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash.empty); unencryptedLogs.unrollLogs().forEach((log, i) => { data.forRollup!.end.unencryptedLogsHashes[i] = new ScopedLogHash( diff --git a/yarn-project/circuit-types/src/tx/processed_tx.ts b/yarn-project/circuit-types/src/tx/processed_tx.ts index 9617c048210..4e73bf0561c 100644 --- a/yarn-project/circuit-types/src/tx/processed_tx.ts +++ b/yarn-project/circuit-types/src/tx/processed_tx.ts @@ -284,7 +284,11 @@ export function toTxEffect(tx: ProcessedTx, gasFees: GasFees): TxEffect { function validateProcessedTxLogs(tx: ProcessedTx): void { const noteEncryptedLogs = tx.noteEncryptedLogs || EncryptedNoteTxL2Logs.empty(); - let kernelHash = tx.data.end.noteEncryptedLogsHash; + let kernelHash = Fr.fromBuffer( + EncryptedNoteTxL2Logs.hashNoteLogs( + tx.data.end.noteEncryptedLogsHashes.filter(hash => !hash.isEmpty()).map(h => h.value.toBuffer()), + ), + ); let referenceHash = Fr.fromBuffer(noteEncryptedLogs.hash()); if (!referenceHash.equals(kernelHash)) { throw new Error( @@ -293,7 +297,11 @@ function validateProcessedTxLogs(tx: ProcessedTx): void { ); } const encryptedLogs = tx.encryptedLogs || EncryptedTxL2Logs.empty(); - kernelHash = tx.data.end.encryptedLogsHash; + kernelHash = kernelHash = Fr.fromBuffer( + EncryptedTxL2Logs.hashSiloedLogs( + tx.data.end.encryptedLogsHashes.filter(hash => !hash.isEmpty()).map(h => h.getSiloedHash()), + ), + ); referenceHash = Fr.fromBuffer(encryptedLogs.hash()); if (!referenceHash.equals(kernelHash)) { throw new Error( diff --git a/yarn-project/circuit-types/src/tx/tx.ts b/yarn-project/circuit-types/src/tx/tx.ts index 74d5d26993d..75bc8a4e211 100644 --- a/yarn-project/circuit-types/src/tx/tx.ts +++ b/yarn-project/circuit-types/src/tx/tx.ts @@ -279,7 +279,7 @@ export class Tx { * @param out the output to put passing logs in, to keep this function abstract */ public filterRevertedLogs(kernelOutput: PublicKernelCircuitPublicInputs) { - this.encryptedLogs = this.encryptedLogs.filter( + this.encryptedLogs = this.encryptedLogs.filterScoped( kernelOutput.endNonRevertibleData.encryptedLogsHashes, EncryptedTxL2Logs.empty(), ); diff --git a/yarn-project/circuits.js/src/structs/kernel/combine_hints.ts b/yarn-project/circuits.js/src/structs/kernel/combine_hints.ts index bbeb121b3d2..f3e9e039fc5 100644 --- a/yarn-project/circuits.js/src/structs/kernel/combine_hints.ts +++ b/yarn-project/circuits.js/src/structs/kernel/combine_hints.ts @@ -6,6 +6,7 @@ import { inspect } from 'util'; import { MAX_ENCRYPTED_LOGS_PER_TX, + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, MAX_UNENCRYPTED_LOGS_PER_TX, @@ -17,7 +18,7 @@ import { sortByCounterGetSortedHints, sortByPositionThenCounterGetSortedHints, } from '../../utils/index.js'; -import { ScopedLogHash } from '../log_hash.js'; +import { LogHash, ScopedLogHash } from '../log_hash.js'; import { ScopedNoteHash } from '../note_hash.js'; import { PublicDataUpdateRequest } from '../public_data_update_request.js'; import { type PublicAccumulatedData } from './public_accumulated_data.js'; @@ -26,6 +27,10 @@ export class CombineHints { constructor( public readonly sortedNoteHashes: Tuple, public readonly sortedNoteHashesIndexes: Tuple, + public readonly sortedNoteEncryptedLogsHashes: Tuple, + public readonly sortedNoteEncryptedLogsHashesIndexes: Tuple, + public readonly sortedEncryptedLogsHashes: Tuple, + public readonly sortedEncryptedLogsHashesIndexes: Tuple, public readonly sortedUnencryptedLogsHashes: Tuple, public readonly sortedUnencryptedLogsHashesIndexes: Tuple, public readonly sortedPublicDataUpdateRequests: Tuple< @@ -44,6 +49,10 @@ export class CombineHints { return [ fields.sortedNoteHashes, fields.sortedNoteHashesIndexes, + fields.sortedNoteEncryptedLogsHashes, + fields.sortedNoteEncryptedLogsHashesIndexes, + fields.sortedEncryptedLogsHashes, + fields.sortedEncryptedLogsHashesIndexes, fields.sortedUnencryptedLogsHashes, fields.sortedUnencryptedLogsHashesIndexes, fields.sortedPublicDataUpdateRequests, @@ -66,6 +75,10 @@ export class CombineHints { return new CombineHints( reader.readArray(MAX_NOTE_HASHES_PER_TX, ScopedNoteHash), reader.readNumbers(MAX_NOTE_HASHES_PER_TX), + reader.readArray(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, LogHash), + reader.readNumbers(MAX_NOTE_ENCRYPTED_LOGS_PER_TX), + reader.readArray(MAX_ENCRYPTED_LOGS_PER_TX, ScopedLogHash), + reader.readNumbers(MAX_ENCRYPTED_LOGS_PER_TX), reader.readArray(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash), reader.readNumbers(MAX_UNENCRYPTED_LOGS_PER_TX), reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest), @@ -93,6 +106,26 @@ export class CombineHints { MAX_NOTE_HASHES_PER_TX, ); + const mergedNoteEncryptedLogsHashes = mergeAccumulatedData( + nonRevertibleData.noteEncryptedLogsHashes, + revertibleData.noteEncryptedLogsHashes, + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, + ); + const [sortedNoteEncryptedLogsHashes, sortedNoteEncryptedLogsHashesIndexes] = sortByCounterGetSortedHints( + mergedNoteEncryptedLogsHashes, + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, + ); + + const mergedEncryptedLogsHashes = mergeAccumulatedData( + nonRevertibleData.encryptedLogsHashes, + revertibleData.encryptedLogsHashes, + MAX_ENCRYPTED_LOGS_PER_TX, + ); + const [sortedEncryptedLogsHashes, sortedEncryptedLogsHashesIndexes] = sortByCounterGetSortedHints( + mergedEncryptedLogsHashes, + MAX_ENCRYPTED_LOGS_PER_TX, + ); + const unencryptedLogHashes = mergeAccumulatedData( nonRevertibleData.unencryptedLogsHashes, revertibleData.unencryptedLogsHashes, @@ -133,6 +166,10 @@ export class CombineHints { return CombineHints.from({ sortedNoteHashes, sortedNoteHashesIndexes, + sortedNoteEncryptedLogsHashes, + sortedNoteEncryptedLogsHashesIndexes, + sortedEncryptedLogsHashes, + sortedEncryptedLogsHashesIndexes, sortedUnencryptedLogsHashes, sortedUnencryptedLogsHashesIndexes, sortedPublicDataUpdateRequests, diff --git a/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts b/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts index e483fefe644..88ba4af36d3 100644 --- a/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/combined_accumulated_data.ts @@ -6,7 +6,9 @@ import { BufferReader, type Tuple, serializeToBuffer } from '@aztec/foundation/s import { inspect } from 'util'; import { + MAX_ENCRYPTED_LOGS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, @@ -14,7 +16,7 @@ import { } from '../../constants.gen.js'; import { Gas } from '../gas.js'; import { ScopedL2ToL1Message } from '../l2_to_l1_message.js'; -import { ScopedLogHash } from '../log_hash.js'; +import { LogHash, ScopedLogHash } from '../log_hash.js'; import { PublicDataUpdateRequest } from '../public_data_update_request.js'; /** @@ -35,15 +37,13 @@ export class CombinedAccumulatedData { */ public l2ToL1Msgs: Tuple, /** - * Accumulated encrypted note logs hash from all the previous kernel iterations. - * Note: Truncated to 31 bytes to fit in Fr. + * Accumulated note logs hashes from all the previous kernel iterations. */ - public noteEncryptedLogsHash: Fr, + public noteEncryptedLogsHashes: Tuple, /** - * Accumulated encrypted logs hash from all the previous kernel iterations. - * Note: Truncated to 31 bytes to fit in Fr. + * Accumulated encrypted logs hashes from all the previous kernel iterations. */ - public encryptedLogsHash: Fr, + public encryptedLogsHashes: Tuple, /** * Accumulated unencrypted logs hash from all the previous kernel iterations. * Note: Truncated to 31 bytes to fit in Fr. @@ -75,8 +75,8 @@ export class CombinedAccumulatedData { arraySerializedSizeOfNonEmpty(this.noteHashes) + arraySerializedSizeOfNonEmpty(this.nullifiers) + arraySerializedSizeOfNonEmpty(this.l2ToL1Msgs) + - this.noteEncryptedLogsHash.size + - this.encryptedLogsHash.size + + arraySerializedSizeOfNonEmpty(this.noteEncryptedLogsHashes) + + arraySerializedSizeOfNonEmpty(this.encryptedLogsHashes) + arraySerializedSizeOfNonEmpty(this.unencryptedLogsHashes) + this.noteEncryptedLogPreimagesLength.size + this.encryptedLogPreimagesLength.size + @@ -91,8 +91,8 @@ export class CombinedAccumulatedData { fields.noteHashes, fields.nullifiers, fields.l2ToL1Msgs, - fields.noteEncryptedLogsHash, - fields.encryptedLogsHash, + fields.noteEncryptedLogsHashes, + fields.encryptedLogsHashes, fields.unencryptedLogsHashes, fields.noteEncryptedLogPreimagesLength, fields.encryptedLogPreimagesLength, @@ -125,8 +125,8 @@ export class CombinedAccumulatedData { reader.readArray(MAX_NOTE_HASHES_PER_TX, Fr), reader.readArray(MAX_NULLIFIERS_PER_TX, Fr), reader.readArray(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message), - Fr.fromBuffer(reader), - Fr.fromBuffer(reader), + reader.readArray(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, LogHash), + reader.readArray(MAX_ENCRYPTED_LOGS_PER_TX, ScopedLogHash), reader.readArray(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash), Fr.fromBuffer(reader), Fr.fromBuffer(reader), @@ -150,8 +150,8 @@ export class CombinedAccumulatedData { makeTuple(MAX_NOTE_HASHES_PER_TX, Fr.zero), makeTuple(MAX_NULLIFIERS_PER_TX, Fr.zero), makeTuple(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message.empty), - Fr.zero(), - Fr.zero(), + makeTuple(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, LogHash.empty), + makeTuple(MAX_ENCRYPTED_LOGS_PER_TX, ScopedLogHash.empty), makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash.empty), Fr.zero(), Fr.zero(), @@ -175,8 +175,14 @@ export class CombinedAccumulatedData { .filter(x => !x.isEmpty()) .map(x => inspect(x)) .join(', ')}], - noteEncryptedLogsHash: ${this.noteEncryptedLogsHash.toString()}, - encryptedLogsHash: ${this.encryptedLogsHash.toString()}, + noteEncryptedLogsHash: [${this.noteEncryptedLogsHashes + .filter(x => !x.isEmpty()) + .map(x => inspect(x)) + .join(', ')}] + encryptedLogsHash: [${this.encryptedLogsHashes + .filter(x => !x.isEmpty()) + .map(x => inspect(x)) + .join(', ')}] unencryptedLogsHashes: : [${this.unencryptedLogsHashes .filter(x => !x.isEmpty()) .map(x => inspect(x)) diff --git a/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts b/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts index 6a895bf1cb2..e81eb230b00 100644 --- a/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data.ts @@ -46,7 +46,7 @@ export class PublicAccumulatedData { * Accumulated encrypted logs hashes from all the previous kernel iterations. * Note: Truncated to 31 bytes to fit in Fr. */ - public readonly encryptedLogsHashes: Tuple, + public readonly encryptedLogsHashes: Tuple, /** * Accumulated unencrypted logs hashes from all the previous kernel iterations. * Note: Truncated to 31 bytes to fit in Fr. @@ -165,7 +165,7 @@ export class PublicAccumulatedData { reader.readArray(MAX_NULLIFIERS_PER_TX, Nullifier), reader.readArray(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message), reader.readArray(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, LogHash), - reader.readArray(MAX_ENCRYPTED_LOGS_PER_TX, LogHash), + reader.readArray(MAX_ENCRYPTED_LOGS_PER_TX, ScopedLogHash), reader.readArray(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash), reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest), reader.readArray(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, PublicCallRequest), @@ -180,7 +180,7 @@ export class PublicAccumulatedData { reader.readArray(MAX_NULLIFIERS_PER_TX, Nullifier), reader.readArray(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message), reader.readArray(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, LogHash), - reader.readArray(MAX_ENCRYPTED_LOGS_PER_TX, LogHash), + reader.readArray(MAX_ENCRYPTED_LOGS_PER_TX, ScopedLogHash), reader.readArray(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash), reader.readArray(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest), reader.readArray(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, PublicCallRequest), @@ -203,7 +203,7 @@ export class PublicAccumulatedData { makeTuple(MAX_NULLIFIERS_PER_TX, Nullifier.empty), makeTuple(MAX_L2_TO_L1_MSGS_PER_TX, ScopedL2ToL1Message.empty), makeTuple(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, LogHash.empty), - makeTuple(MAX_ENCRYPTED_LOGS_PER_TX, LogHash.empty), + makeTuple(MAX_ENCRYPTED_LOGS_PER_TX, ScopedLogHash.empty), makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, ScopedLogHash.empty), makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, PublicDataUpdateRequest.empty), makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, PublicCallRequest.empty), diff --git a/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data_builder.ts b/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data_builder.ts index ff9d3081f88..b652345abd2 100644 --- a/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data_builder.ts +++ b/yarn-project/circuits.js/src/structs/kernel/public_accumulated_data_builder.ts @@ -30,7 +30,7 @@ export class PublicAccumulatedDataBuilder { private nullifiers: Nullifier[] = []; private l2ToL1Msgs: ScopedL2ToL1Message[] = []; private noteEncryptedLogsHashes: LogHash[] = []; - private encryptedLogsHashes: LogHash[] = []; + private encryptedLogsHashes: ScopedLogHash[] = []; private unencryptedLogsHashes: ScopedLogHash[] = []; private publicDataUpdateRequests: PublicDataUpdateRequest[] = []; private publicCallStack: PublicCallRequest[] = []; @@ -76,12 +76,12 @@ export class PublicAccumulatedDataBuilder { return this; } - pushEncryptedLogsHash(encryptedLogsHash: LogHash) { + pushEncryptedLogsHash(encryptedLogsHash: ScopedLogHash) { this.encryptedLogsHashes.push(encryptedLogsHash); return this; } - withEncryptedLogsHashes(encryptedLogsHashes: LogHash[]) { + withEncryptedLogsHashes(encryptedLogsHashes: ScopedLogHash[]) { this.encryptedLogsHashes = encryptedLogsHashes; return this; } @@ -127,7 +127,7 @@ export class PublicAccumulatedDataBuilder { padArrayEnd(this.nullifiers, Nullifier.empty(), MAX_NULLIFIERS_PER_TX), padArrayEnd(this.l2ToL1Msgs, ScopedL2ToL1Message.empty(), MAX_L2_TO_L1_MSGS_PER_TX), padArrayEnd(this.noteEncryptedLogsHashes, LogHash.empty(), MAX_NOTE_ENCRYPTED_LOGS_PER_TX), - padArrayEnd(this.encryptedLogsHashes, LogHash.empty(), MAX_ENCRYPTED_LOGS_PER_TX), + padArrayEnd(this.encryptedLogsHashes, ScopedLogHash.empty(), MAX_ENCRYPTED_LOGS_PER_TX), padArrayEnd(this.unencryptedLogsHashes, ScopedLogHash.empty(), MAX_UNENCRYPTED_LOGS_PER_TX), padArrayEnd( this.publicDataUpdateRequests, diff --git a/yarn-project/circuits.js/src/tests/factories.ts b/yarn-project/circuits.js/src/tests/factories.ts index dc713a88f37..6366e654cce 100644 --- a/yarn-project/circuits.js/src/tests/factories.ts +++ b/yarn-project/circuits.js/src/tests/factories.ts @@ -336,8 +336,8 @@ export function makeCombinedAccumulatedData(seed = 1, full = false): CombinedAcc tupleGenerator(MAX_NOTE_HASHES_PER_TX, fr, seed + 0x120, Fr.zero), tupleGenerator(MAX_NULLIFIERS_PER_TX, fr, seed + 0x200, Fr.zero), tupleGenerator(MAX_L2_TO_L1_MSGS_PER_TX, makeScopedL2ToL1Message, seed + 0x600, ScopedL2ToL1Message.empty), - fr(seed + 0x700), // note encrypted logs hash - fr(seed + 0x800), // encrypted logs hash + tupleGenerator(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, makeLogHash, seed + 0x700, LogHash.empty), + tupleGenerator(MAX_ENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x800, ScopedLogHash.empty), tupleGenerator(MAX_UNENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x900, ScopedLogHash.empty), // unencrypted logs fr(seed + 0xa00), // note_encrypted_log_preimages_length fr(seed + 0xb00), // encrypted_log_preimages_length @@ -369,7 +369,7 @@ function makePublicAccumulatedData(seed = 1, full = false): PublicAccumulatedDat tupleGenerator(MAX_NULLIFIERS_PER_TX, makeNullifier, seed + 0x200, Nullifier.empty), tupleGenerator(MAX_L2_TO_L1_MSGS_PER_TX, makeScopedL2ToL1Message, seed + 0x600, ScopedL2ToL1Message.empty), tupleGenerator(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, makeLogHash, seed + 0x700, LogHash.empty), // note encrypted logs hashes - tupleGenerator(MAX_ENCRYPTED_LOGS_PER_TX, makeLogHash, seed + 0x800, LogHash.empty), // encrypted logs hashes + tupleGenerator(MAX_ENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x800, ScopedLogHash.empty), // encrypted logs hashes tupleGenerator(MAX_UNENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x900, ScopedLogHash.empty), // unencrypted logs hashes tupleGenerator( MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, @@ -671,20 +671,24 @@ export function makeCombineHints(seed = 1): CombineHints { return CombineHints.from({ sortedNoteHashes: makeTuple(MAX_NOTE_HASHES_PER_TX, makeScopedNoteHash, seed + 0x100), sortedNoteHashesIndexes: makeTuple(MAX_NOTE_HASHES_PER_TX, i => i, seed + 0x200), - sortedUnencryptedLogsHashes: makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x300), - sortedUnencryptedLogsHashesIndexes: makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, i => i, seed + 0x400), + sortedNoteEncryptedLogsHashes: makeTuple(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, makeLogHash, seed + 0x300), + sortedNoteEncryptedLogsHashesIndexes: makeTuple(MAX_NOTE_ENCRYPTED_LOGS_PER_TX, i => i, seed + 0x400), + sortedEncryptedLogsHashes: makeTuple(MAX_ENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x500), + sortedEncryptedLogsHashesIndexes: makeTuple(MAX_ENCRYPTED_LOGS_PER_TX, i => i, seed + 0x600), + sortedUnencryptedLogsHashes: makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, makeScopedLogHash, seed + 0x700), + sortedUnencryptedLogsHashesIndexes: makeTuple(MAX_UNENCRYPTED_LOGS_PER_TX, i => i, seed + 0x800), sortedPublicDataUpdateRequests: makeTuple( MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, makePublicDataUpdateRequest, - seed + 0x300, + seed + 0x900, ), - sortedPublicDataUpdateRequestsIndexes: makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, i => i, seed + 0x400), + sortedPublicDataUpdateRequestsIndexes: makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, i => i, seed + 0x1000), dedupedPublicDataUpdateRequests: makeTuple( MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, makePublicDataUpdateRequest, - seed + 0x500, + seed + 0x1100, ), - dedupedPublicDataUpdateRequestsRuns: makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, i => i, seed + 0x600), + dedupedPublicDataUpdateRequestsRuns: makeTuple(MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, i => i, seed + 0x1200), }); } diff --git a/yarn-project/end-to-end/src/composed/integration_l1_publisher.test.ts b/yarn-project/end-to-end/src/composed/integration_l1_publisher.test.ts index bf4bd673a8d..3a92661e5aa 100644 --- a/yarn-project/end-to-end/src/composed/integration_l1_publisher.test.ts +++ b/yarn-project/end-to-end/src/composed/integration_l1_publisher.test.ts @@ -13,12 +13,14 @@ import { GasFees, type Header, KernelCircuitPublicInputs, + LogHash, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX, NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP, PublicDataUpdateRequest, + ScopedLogHash, } from '@aztec/circuits.js'; import { fr, makeScopedL2ToL1Message } from '@aztec/circuits.js/testing'; import { type L1ContractAddresses, createEthereumChain } from '@aztec/ethereum'; @@ -177,7 +179,12 @@ describe('L1Publisher integration', () => { processedTx.data.end.nullifiers = makeTuple(MAX_NULLIFIERS_PER_TX, fr, seed + 0x200); processedTx.data.end.nullifiers[processedTx.data.end.nullifiers.length - 1] = Fr.ZERO; processedTx.data.end.l2ToL1Msgs = makeTuple(MAX_L2_TO_L1_MSGS_PER_TX, makeScopedL2ToL1Message, seed + 0x300); - processedTx.data.end.encryptedLogsHash = Fr.fromBuffer(processedTx.encryptedLogs.hash()); + processedTx.encryptedLogs.unrollLogs().forEach((log, i) => { + processedTx.data.end.encryptedLogsHashes[i] = new ScopedLogHash( + new LogHash(Fr.fromBuffer(log.hash()), 0, new Fr(log.length)), + log.maskedContractAddress, + ); + }); return processedTx; }; diff --git a/yarn-project/end-to-end/src/e2e_block_building.test.ts b/yarn-project/end-to-end/src/e2e_block_building.test.ts index d88811cdb8d..caf6b83f7bc 100644 --- a/yarn-project/end-to-end/src/e2e_block_building.test.ts +++ b/yarn-project/end-to-end/src/e2e_block_building.test.ts @@ -301,8 +301,6 @@ describe('e2e_block_building', () => { ); // Setting randomness = 0 in app means 'do not mask the address' expect(encryptedLogs[2].maskedContractAddress).toEqual(testContract.address.toField()); - const expectedEncryptedLogsHash = tx.encryptedLogs.hash(); - expect(tx.data.forRollup?.end.encryptedLogsHash).toEqual(new Fr(expectedEncryptedLogsHash)); // TODO(1139 | 6408): We currently encrypted generic event logs the same way as notes, so the below // will likely not be useful when complete. diff --git a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts index d4525bca0e8..973956fafaa 100644 --- a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts +++ b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts @@ -1304,7 +1304,7 @@ export function mapPublicAccumulatedDataFromNoir( MAX_NOTE_ENCRYPTED_LOGS_PER_TX, mapLogHashFromNoir, ), - mapTupleFromNoir(publicAccumulatedData.encrypted_logs_hashes, MAX_ENCRYPTED_LOGS_PER_TX, mapLogHashFromNoir), + mapTupleFromNoir(publicAccumulatedData.encrypted_logs_hashes, MAX_ENCRYPTED_LOGS_PER_TX, mapScopedLogHashFromNoir), mapTupleFromNoir( publicAccumulatedData.unencrypted_logs_hashes, MAX_UNENCRYPTED_LOGS_PER_TX, @@ -1396,8 +1396,16 @@ export function mapCombinedAccumulatedDataFromNoir( mapTupleFromNoir(combinedAccumulatedData.note_hashes, MAX_NOTE_HASHES_PER_TX, mapFieldFromNoir), mapTupleFromNoir(combinedAccumulatedData.nullifiers, MAX_NULLIFIERS_PER_TX, mapFieldFromNoir), mapTupleFromNoir(combinedAccumulatedData.l2_to_l1_msgs, MAX_L2_TO_L1_MSGS_PER_TX, mapScopedL2ToL1MessageFromNoir), - mapFieldFromNoir(combinedAccumulatedData.note_encrypted_logs_hash), - mapFieldFromNoir(combinedAccumulatedData.encrypted_logs_hash), + mapTupleFromNoir( + combinedAccumulatedData.note_encrypted_logs_hashes, + MAX_NOTE_ENCRYPTED_LOGS_PER_TX, + mapLogHashFromNoir, + ), + mapTupleFromNoir( + combinedAccumulatedData.encrypted_logs_hashes, + MAX_ENCRYPTED_LOGS_PER_TX, + mapScopedLogHashFromNoir, + ), mapTupleFromNoir( combinedAccumulatedData.unencrypted_logs_hashes, MAX_UNENCRYPTED_LOGS_PER_TX, @@ -1422,8 +1430,8 @@ export function mapCombinedAccumulatedDataToNoir( note_hashes: mapTuple(combinedAccumulatedData.noteHashes, mapFieldToNoir), nullifiers: mapTuple(combinedAccumulatedData.nullifiers, mapFieldToNoir), l2_to_l1_msgs: mapTuple(combinedAccumulatedData.l2ToL1Msgs, mapScopedL2ToL1MessageToNoir), - note_encrypted_logs_hash: mapFieldToNoir(combinedAccumulatedData.noteEncryptedLogsHash), - encrypted_logs_hash: mapFieldToNoir(combinedAccumulatedData.encryptedLogsHash), + note_encrypted_logs_hashes: mapTuple(combinedAccumulatedData.noteEncryptedLogsHashes, mapLogHashToNoir), + encrypted_logs_hashes: mapTuple(combinedAccumulatedData.encryptedLogsHashes, mapScopedLogHashToNoir), unencrypted_logs_hashes: mapTuple(combinedAccumulatedData.unencryptedLogsHashes, mapScopedLogHashToNoir), note_encrypted_log_preimages_length: mapFieldToNoir(combinedAccumulatedData.noteEncryptedLogPreimagesLength), encrypted_log_preimages_length: mapFieldToNoir(combinedAccumulatedData.encryptedLogPreimagesLength), @@ -1713,6 +1721,13 @@ export function mapCombineHintsToNoir(combineHints: CombineHints): CombineHintsN return { sorted_note_hashes: mapTuple(combineHints.sortedNoteHashes, mapScopedNoteHashToNoir), sorted_note_hashes_indexes: mapTuple(combineHints.sortedNoteHashesIndexes, mapNumberToNoir), + sorted_note_encrypted_logs_hashes: mapTuple(combineHints.sortedNoteEncryptedLogsHashes, mapLogHashToNoir), + sorted_note_encrypted_logs_hashes_indexes: mapTuple( + combineHints.sortedNoteEncryptedLogsHashesIndexes, + mapNumberToNoir, + ), + sorted_encrypted_logs_hashes: mapTuple(combineHints.sortedEncryptedLogsHashes, mapScopedEncryptedLogHashToNoir), + sorted_encrypted_logs_hashes_indexes: mapTuple(combineHints.sortedEncryptedLogsHashesIndexes, mapNumberToNoir), sorted_unencrypted_logs_hashes: mapTuple(combineHints.sortedUnencryptedLogsHashes, mapScopedLogHashToNoir), sorted_unencrypted_logs_hashes_indexes: mapTuple(combineHints.sortedUnencryptedLogsHashesIndexes, mapNumberToNoir), sorted_public_data_update_requests: mapTuple( diff --git a/yarn-project/prover-client/src/mocks/fixtures.ts b/yarn-project/prover-client/src/mocks/fixtures.ts index 8a7de0c2fb1..eb8ab0fc407 100644 --- a/yarn-project/prover-client/src/mocks/fixtures.ts +++ b/yarn-project/prover-client/src/mocks/fixtures.ts @@ -12,6 +12,7 @@ import { GasFees, GlobalVariables, KernelCircuitPublicInputs, + LogHash, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, @@ -20,6 +21,7 @@ import { PUBLIC_DATA_SUBTREE_HEIGHT, PublicDataTreeLeaf, PublicDataUpdateRequest, + ScopedLogHash, } from '@aztec/circuits.js'; import { fr, makeScopedL2ToL1Message } from '@aztec/circuits.js/testing'; import { makeTuple } from '@aztec/foundation/array'; @@ -121,8 +123,15 @@ export const makeBloatedProcessedTx = (builderDb: MerkleTreeOperations, seed = 0 processedTx.data.end.nullifiers[tx.data.forPublic!.end.nullifiers.length - 1] = Fr.zero(); processedTx.data.end.l2ToL1Msgs = makeTuple(MAX_L2_TO_L1_MSGS_PER_TX, makeScopedL2ToL1Message, seed + 0x300); - processedTx.data.end.noteEncryptedLogsHash = Fr.fromBuffer(processedTx.noteEncryptedLogs.hash()); - processedTx.data.end.encryptedLogsHash = Fr.fromBuffer(processedTx.encryptedLogs.hash()); + processedTx.noteEncryptedLogs.unrollLogs().forEach((log, i) => { + processedTx.data.end.noteEncryptedLogsHashes[i] = new LogHash(Fr.fromBuffer(log.hash()), 0, new Fr(log.length)); + }); + processedTx.encryptedLogs.unrollLogs().forEach((log, i) => { + processedTx.data.end.encryptedLogsHashes[i] = new ScopedLogHash( + new LogHash(Fr.fromBuffer(log.hash()), 0, new Fr(log.length)), + log.maskedContractAddress, + ); + }); return processedTx; }; diff --git a/yarn-project/prover-client/src/orchestrator/orchestrator.ts b/yarn-project/prover-client/src/orchestrator/orchestrator.ts index e3ad5b357af..d4310716aaa 100644 --- a/yarn-project/prover-client/src/orchestrator/orchestrator.ts +++ b/yarn-project/prover-client/src/orchestrator/orchestrator.ts @@ -1,5 +1,7 @@ import { Body, + EncryptedNoteTxL2Logs, + EncryptedTxL2Logs, L2Block, MerkleTreeId, type PaddingProcessedTx, @@ -618,28 +620,30 @@ export class ProvingOrchestrator { logger.debug('Not running base rollup, state invalid'); return; } - if ( - !tx.baseRollupInputs.kernelData.publicInputs.end.noteEncryptedLogsHash - .toBuffer() - .equals(tx.processedTx.noteEncryptedLogs.hash()) - ) { + const txNoteEncryptedLogs = EncryptedNoteTxL2Logs.hashNoteLogs( + tx.baseRollupInputs.kernelData.publicInputs.end.noteEncryptedLogsHashes + .filter(log => !log.isEmpty()) + .map(log => log.value.toBuffer()), + ); + if (!txNoteEncryptedLogs.equals(tx.processedTx.noteEncryptedLogs.hash())) { provingState.reject( - `Note encrypted logs hash mismatch: ${ - tx.baseRollupInputs.kernelData.publicInputs.end.noteEncryptedLogsHash - } === ${Fr.fromBuffer(tx.processedTx.noteEncryptedLogs.hash())}`, + `Note encrypted logs hash mismatch: ${Fr.fromBuffer(txNoteEncryptedLogs)} === ${Fr.fromBuffer( + tx.processedTx.noteEncryptedLogs.hash(), + )}`, ); return; } - if ( - !tx.baseRollupInputs.kernelData.publicInputs.end.encryptedLogsHash - .toBuffer() - .equals(tx.processedTx.encryptedLogs.hash()) - ) { + const txEncryptedLogs = EncryptedTxL2Logs.hashSiloedLogs( + tx.baseRollupInputs.kernelData.publicInputs.end.encryptedLogsHashes + .filter(log => !log.isEmpty()) + .map(log => log.getSiloedHash()), + ); + if (!txEncryptedLogs.equals(tx.processedTx.encryptedLogs.hash())) { // @todo This rejection messages is never seen. Never making it out to the logs provingState.reject( - `Encrypted logs hash mismatch: ${ - tx.baseRollupInputs.kernelData.publicInputs.end.encryptedLogsHash - } === ${Fr.fromBuffer(tx.processedTx.encryptedLogs.hash())}`, + `Encrypted logs hash mismatch: ${Fr.fromBuffer(txEncryptedLogs)} === ${Fr.fromBuffer( + tx.processedTx.encryptedLogs.hash(), + )}`, ); return; } From b72feac8760d8fb3acc0fdaf7a36bc4c1cad840e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 09:06:58 +0000 Subject: [PATCH 02/10] fixes --- .../src/components/tail_output_validator.nr | 5 +---- .../components/tail_output_validator/tail_output_hints.nr | 2 +- yarn-project/circuit-types/src/logs/tx_l2_logs.ts | 2 +- .../noir-protocol-circuits-types/src/type_conversion.ts | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr index 8cae411675d..323d949933f 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr @@ -15,10 +15,7 @@ use dep::types::{ log_hash::{LogHash, NoteLogHash, ScopedEncryptedLogHash, ScopedLogHash} }, messaging::l2_to_l1_message::ScopedL2ToL1Message, - hash::{ - compute_tx_logs_hash, compute_tx_note_logs_hash, silo_note_hash, - silo_nullifier, mask_encrypted_log_hash -}, + hash::{compute_tx_logs_hash, compute_tx_note_logs_hash, silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, traits::is_empty, utils::arrays::{assert_sorted_transformed_value_array, assert_sorted_array_with_order_hints} }; diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr index b73fcd0cce2..f0e75a0bc90 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr @@ -7,7 +7,7 @@ use dep::types::{ MAX_ENCRYPTED_LOGS_PER_TX, MAX_L2_TO_L1_MSGS_PER_TX, MAX_NOTE_HASHES_PER_TX, MAX_NULLIFIERS_PER_TX, MAX_NOTE_ENCRYPTED_LOGS_PER_TX, MAX_UNENCRYPTED_LOGS_PER_TX }, - hash::{ silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, + hash::{silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, messaging::l2_to_l1_message::ScopedL2ToL1Message, traits::{Empty, is_empty}, utils::arrays::{OrderHint, sort_by_counters_asc, sort_get_order_hints_asc} }; diff --git a/yarn-project/circuit-types/src/logs/tx_l2_logs.ts b/yarn-project/circuit-types/src/logs/tx_l2_logs.ts index 69117cb3a97..6ebeac3499b 100644 --- a/yarn-project/circuit-types/src/logs/tx_l2_logs.ts +++ b/yarn-project/circuit-types/src/logs/tx_l2_logs.ts @@ -319,7 +319,7 @@ export class EncryptedNoteTxL2Logs extends TxL2Logs { allSiloedLogHashes = Buffer.concat([allSiloedLogHashes, siloedLogHash]); } // pad the end of logs with 0s - for (let i = 0; i < MAX_UNENCRYPTED_LOGS_PER_TX - logHashes.length; i++) { + for (let i = 0; i < MAX_NOTE_ENCRYPTED_LOGS_PER_TX - logHashes.length; i++) { allSiloedLogHashes = Buffer.concat([allSiloedLogHashes, Buffer.alloc(32)]); } diff --git a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts index 973956fafaa..a557dbe6c44 100644 --- a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts +++ b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts @@ -1332,7 +1332,7 @@ export function mapPublicAccumulatedDataToNoir( nullifiers: mapTuple(publicAccumulatedData.nullifiers, mapNullifierToNoir), l2_to_l1_msgs: mapTuple(publicAccumulatedData.l2ToL1Msgs, mapScopedL2ToL1MessageToNoir), note_encrypted_logs_hashes: mapTuple(publicAccumulatedData.noteEncryptedLogsHashes, mapLogHashToNoir), - encrypted_logs_hashes: mapTuple(publicAccumulatedData.encryptedLogsHashes, mapLogHashToNoir), + encrypted_logs_hashes: mapTuple(publicAccumulatedData.encryptedLogsHashes, mapScopedLogHashToNoir), unencrypted_logs_hashes: mapTuple(publicAccumulatedData.unencryptedLogsHashes, mapScopedLogHashToNoir), public_data_update_requests: mapTuple( publicAccumulatedData.publicDataUpdateRequests, From 5ab51bf5b03523f687ddef2170b4b695ae6f5d06 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 09:28:19 +0000 Subject: [PATCH 03/10] fix --- .../noir-protocol-circuits-types/src/type_conversion.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts index a557dbe6c44..b797d51f2a8 100644 --- a/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts +++ b/yarn-project/noir-protocol-circuits-types/src/type_conversion.ts @@ -1726,7 +1726,7 @@ export function mapCombineHintsToNoir(combineHints: CombineHints): CombineHintsN combineHints.sortedNoteEncryptedLogsHashesIndexes, mapNumberToNoir, ), - sorted_encrypted_logs_hashes: mapTuple(combineHints.sortedEncryptedLogsHashes, mapScopedEncryptedLogHashToNoir), + sorted_encrypted_logs_hashes: mapTuple(combineHints.sortedEncryptedLogsHashes, mapScopedLogHashToNoir), sorted_encrypted_logs_hashes_indexes: mapTuple(combineHints.sortedEncryptedLogsHashesIndexes, mapNumberToNoir), sorted_unencrypted_logs_hashes: mapTuple(combineHints.sortedUnencryptedLogsHashes, mapScopedLogHashToNoir), sorted_unencrypted_logs_hashes_indexes: mapTuple(combineHints.sortedUnencryptedLogsHashesIndexes, mapNumberToNoir), From b20ee52acc3db73c6d519ff908d062ec7761ed4d Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 10:31:12 +0000 Subject: [PATCH 04/10] fix address masking --- .../src/private_kernel_tail.nr | 156 ++++++++---------- .../crates/types/src/hash.nr | 4 +- 2 files changed, 68 insertions(+), 92 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr index d170742d07e..1ead05b78a4 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr @@ -66,11 +66,12 @@ mod tests { abis::{ kernel_circuit_public_inputs::KernelCircuitPublicInputs, max_block_number::MaxBlockNumber, note_hash::{NoteHash, ScopedNoteHash}, nullifier::{Nullifier, ScopedNullifier}, gas::Gas, - log_hash::ScopedLogHash + log_hash::{ScopedLogHash, ScopedEncryptedLogHash, LogHash} }, address::{AztecAddress, EthAddress}, scalar::Scalar, - hash::{sha256_to_field, silo_note_hash, silo_nullifier}, tests::fixture_builder::FixtureBuilder, - utils::{arrays::array_length}, traits::{Empty, is_empty}, point::Point + hash::{sha256_to_field, silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, + tests::fixture_builder::FixtureBuilder, utils::{arrays::array_length}, + traits::{Empty, is_empty}, point::Point }; // TODO: Reduce the duplicated code/tests for PrivateKernelTailInputs and PrivateKernelTailToPublicInputs. @@ -139,97 +140,70 @@ mod tests { assert_eq(public_inputs.rollup_validation_requests.max_block_number.unwrap(), 13); } - // #[test] - // fn logs_are_handled_as_expected() { - // let mut builder = PrivateKernelTailInputsBuilder::new(); - // // Logs for the previous call stack. - // let prev_encrypted_logs_hash = 80; - // let prev_encrypted_log_preimages_length = 13; - // let prev_unencrypted_logs_hash = 956; - // let prev_unencrypted_log_preimages_length = 24; - // builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); - // builder.previous_kernel.add_unencrypted_log_hash( - // prev_unencrypted_logs_hash, - // prev_unencrypted_log_preimages_length - // ); - // // Logs for the current call stack. - // let unencrypted_logs_hash = 26; - // let unencrypted_log_preimages_length = 50; - // builder.previous_kernel.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); - - // let public_inputs = builder.execute(); - - // assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); - // assert_eq( - // public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length - // ); - - // let siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( - // builder.previous_kernel.storage_contract_address, - // builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness, - // prev_encrypted_logs_hash - // ); - - // // noir-fmt:ignore - // let hash_bytes: [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] = siloed_encrypted_logs_hash - // .to_be_bytes(32) - // .append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 32]) - // .as_array(); - // let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); - // assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); - - // assert_eq( - // public_inputs.end.unencrypted_logs_hashes, builder.previous_kernel.unencrypted_logs_hashes.storage.map(|log: ScopedLogHash| log.expose_to_public()) - // ); - // } + #[test] + fn measuring_of_log_lengths() { + let mut builder = PrivateKernelTailInputsBuilder::new(); + // Logs for the previous call stack. + let prev_encrypted_logs_hash = 80; + let prev_encrypted_log_preimages_length = 13; + let prev_unencrypted_logs_hash = 956; + let prev_unencrypted_log_preimages_length = 24; + builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); + builder.previous_kernel.add_unencrypted_log_hash( + prev_unencrypted_logs_hash, + prev_unencrypted_log_preimages_length + ); + // Logs for the current call stack. + let unencrypted_logs_hash = 26; + let unencrypted_log_preimages_length = 50; + builder.previous_kernel.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); + + let public_inputs = builder.execute(); - unconstrained fn compute_hash_bytes( - siloed_encrypted_logs_hash: Field, - new_siloed_encrypted_logs_hash: Field - ) -> [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] { - siloed_encrypted_logs_hash.to_be_bytes(32).append(new_siloed_encrypted_logs_hash.to_be_bytes(32)).append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 64]).as_array() + assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); + assert_eq( + public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length + ); } - // #[test] - // fn encrypted_logs_are_hashed_as_expected() { - // let mut builder = PrivateKernelTailInputsBuilder::new(); - // // Logs for the previous call stack. - // let prev_encrypted_logs_hash = 80; - // let prev_encrypted_log_preimages_length = 13; - // builder.previous_kernel.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); - - // // Set the randomness to 0 to signal not masking the address to silo with - // builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness = 0; - - // let new_encrypted_logs_hash = 26; - // let new_encrypted_log_preimages_length = 50; - // builder.previous_kernel.add_encrypted_log_hash(new_encrypted_logs_hash, new_encrypted_log_preimages_length); - - // let public_inputs = builder.execute(); - - // assert_eq( - // public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length + new_encrypted_log_preimages_length - // ); - - // let siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( - // builder.previous_kernel.storage_contract_address, - // builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness, - // prev_encrypted_logs_hash - // ); - - // let siloed_hash_bytes: [u8; 64] = builder.previous_kernel.storage_contract_address.to_field().to_be_bytes(32).append(prev_encrypted_logs_hash.to_be_bytes(32)).as_array(); - // assert_eq(siloed_encrypted_logs_hash, sha256_to_field(siloed_hash_bytes)); - - // let new_siloed_encrypted_logs_hash = compute_siloed_encrypted_log_hash( - // builder.previous_kernel.storage_contract_address, - // builder.previous_kernel.encrypted_logs_hashes.storage[1].log_hash.randomness, - // new_encrypted_logs_hash - // ); - - // let hash_bytes = compute_hash_bytes(siloed_encrypted_logs_hash, new_siloed_encrypted_logs_hash); - // let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); - // assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); - // } + #[test] + fn ordering_of_encrypted_logs() { + let mut builder = PrivateKernelTailInputsBuilder::new(); + // Logs for the previous call stack. + let prev_log_hashes = [80, 90]; + let prev_log_lengths = [13, 14]; + for i in 0..prev_log_hashes.len() { + builder.previous_kernel.add_encrypted_log_hash(prev_log_hashes[i], prev_log_lengths[i]); + } + + // Set the randomness in the first log to 0 to signal not masking the address to silo with + builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness = 0; + + // Reorder the logs + let mut reversed_logs = [ScopedEncryptedLogHash::empty(); 2]; + for i in 0..reversed_logs.len() { + reversed_logs[i] = builder.previous_kernel.encrypted_logs_hashes.pop(); + } + builder.previous_kernel.encrypted_logs_hashes.extend_from_array(reversed_logs); + + let public_inputs = builder.execute(); + + let resulting_encrypted_logs = public_inputs.end.encrypted_logs_hashes; + + assert_eq( + resulting_encrypted_logs[0], ScopedLogHash { + contract_address: builder.previous_kernel.storage_contract_address, // Not masked, randomness = 0 + log_hash: LogHash { value: prev_log_hashes[0], counter: 0, length: prev_log_lengths[0] } + } + ); + + assert_eq( + resulting_encrypted_logs[1], ScopedLogHash { + contract_address: mask_encrypted_log_hash(reversed_logs[0]), + log_hash: LogHash { value: prev_log_hashes[1], counter: 0, length: prev_log_lengths[1] } + } + ); + } #[test] fn ordering_of_note_hashes_and_nullifiers() { diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr index bd689c65c5a..a7f76ee5ba4 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -98,8 +98,10 @@ pub fn silo_encrypted_log_hash(log_hash: ScopedLogHash) -> Field { } pub fn mask_encrypted_log_hash(scoped_log: ScopedEncryptedLogHash) -> AztecAddress { - if scoped_log.contract_address.is_zero() | (scoped_log.log_hash.randomness == 0) { + if scoped_log.contract_address.is_zero() { AztecAddress::from_field(0) + } else if (scoped_log.log_hash.randomness == 0) { + scoped_log.contract_address } else { AztecAddress::from_field( poseidon2_hash_with_separator( From 1cdbd5d713c29e7e31c414169f503c356084fa4a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 12:31:35 +0000 Subject: [PATCH 05/10] chore: update constants --- l1-contracts/src/core/libraries/ConstantsGen.sol | 8 ++++---- .../noir-protocol-circuits/crates/types/src/constants.nr | 4 ++-- yarn-project/circuits.js/src/constants.gen.ts | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/l1-contracts/src/core/libraries/ConstantsGen.sol b/l1-contracts/src/core/libraries/ConstantsGen.sol index 8f8c1a8c45b..5452b7b9891 100644 --- a/l1-contracts/src/core/libraries/ConstantsGen.sol +++ b/l1-contracts/src/core/libraries/ConstantsGen.sol @@ -184,13 +184,13 @@ library Constants { uint256 internal constant PRIVATE_VALIDATION_REQUESTS_LENGTH = 772; uint256 internal constant PUBLIC_VALIDATION_REQUESTS_LENGTH = 514; uint256 internal constant PUBLIC_DATA_UPDATE_REQUEST_LENGTH = 3; - uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 364; + uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 610; uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 43; uint256 internal constant PRIVATE_ACCUMULATED_DATA_LENGTH = 1336; uint256 internal constant PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2167; - uint256 internal constant PUBLIC_ACCUMULATED_DATA_LENGTH = 1279; - uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 3565; - uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 417; + uint256 internal constant PUBLIC_ACCUMULATED_DATA_LENGTH = 1311; + uint256 internal constant PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 3629; + uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 663; uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 12; uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 29; uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index f3cb9300fc8..cda0336fa0c 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -236,13 +236,13 @@ global PRIVATE_VALIDATION_REQUESTS_LENGTH = ROLLUP_VALIDATION_REQUESTS_LENGTH + global PUBLIC_VALIDATION_REQUESTS_LENGTH = ROLLUP_VALIDATION_REQUESTS_LENGTH + (SCOPED_READ_REQUEST_LEN * MAX_NULLIFIER_READ_REQUESTS_PER_TX) + (SCOPED_READ_REQUEST_LEN * MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX) + (PUBLIC_DATA_READ_LENGTH * MAX_PUBLIC_DATA_READS_PER_TX); global PUBLIC_DATA_UPDATE_REQUEST_LENGTH = 3; -global COMBINED_ACCUMULATED_DATA_LENGTH = MAX_NOTE_HASHES_PER_TX + MAX_NULLIFIERS_PER_TX + (MAX_L2_TO_L1_MSGS_PER_TX * SCOPED_L2_TO_L1_MESSAGE_LENGTH) + 5 + (MAX_UNENCRYPTED_LOGS_PER_TX * SCOPED_LOG_HASH_LENGTH) + (MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * PUBLIC_DATA_UPDATE_REQUEST_LENGTH) + GAS_LENGTH; +global COMBINED_ACCUMULATED_DATA_LENGTH = MAX_NOTE_HASHES_PER_TX + MAX_NULLIFIERS_PER_TX + (MAX_L2_TO_L1_MSGS_PER_TX * SCOPED_L2_TO_L1_MESSAGE_LENGTH) + (LOG_HASH_LENGTH * MAX_NOTE_ENCRYPTED_LOGS_PER_TX) + (SCOPED_LOG_HASH_LENGTH * MAX_ENCRYPTED_LOGS_PER_TX) + 3 + (MAX_UNENCRYPTED_LOGS_PER_TX * SCOPED_LOG_HASH_LENGTH) + (MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * PUBLIC_DATA_UPDATE_REQUEST_LENGTH) + GAS_LENGTH; global COMBINED_CONSTANT_DATA_LENGTH = HEADER_LENGTH + TX_CONTEXT_LENGTH + GLOBAL_VARIABLES_LENGTH + 1; global PRIVATE_ACCUMULATED_DATA_LENGTH = (SCOPED_NOTE_HASH_LENGTH * MAX_NOTE_HASHES_PER_TX) + (SCOPED_NULLIFIER_LENGTH * MAX_NULLIFIERS_PER_TX) + (MAX_L2_TO_L1_MSGS_PER_TX * SCOPED_L2_TO_L1_MESSAGE_LENGTH) + (NOTE_LOG_HASH_LENGTH * MAX_NOTE_ENCRYPTED_LOGS_PER_TX) + (SCOPED_ENCRYPTED_LOG_HASH_LENGTH * MAX_ENCRYPTED_LOGS_PER_TX) + (SCOPED_LOG_HASH_LENGTH * MAX_UNENCRYPTED_LOGS_PER_TX) + (PRIVATE_CALL_REQUEST_LENGTH * MAX_PRIVATE_CALL_STACK_LENGTH_PER_TX) + (PUBLIC_CALL_REQUEST_LENGTH * MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX); global PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1 + PRIVATE_VALIDATION_REQUESTS_LENGTH + PRIVATE_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH + PUBLIC_CALL_REQUEST_LENGTH + AZTEC_ADDRESS_LENGTH; -global PUBLIC_ACCUMULATED_DATA_LENGTH = (MAX_NOTE_HASHES_PER_TX * SCOPED_NOTE_HASH_LENGTH) + (MAX_NULLIFIERS_PER_TX * NULLIFIER_LENGTH) + (MAX_L2_TO_L1_MSGS_PER_TX * SCOPED_L2_TO_L1_MESSAGE_LENGTH) + (MAX_NOTE_ENCRYPTED_LOGS_PER_TX * LOG_HASH_LENGTH) + (MAX_ENCRYPTED_LOGS_PER_TX * LOG_HASH_LENGTH) + (MAX_UNENCRYPTED_LOGS_PER_TX * SCOPED_LOG_HASH_LENGTH) + (MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * PUBLIC_DATA_UPDATE_REQUEST_LENGTH) + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * PUBLIC_CALL_REQUEST_LENGTH) + GAS_LENGTH; +global PUBLIC_ACCUMULATED_DATA_LENGTH = (MAX_NOTE_HASHES_PER_TX * SCOPED_NOTE_HASH_LENGTH) + (MAX_NULLIFIERS_PER_TX * NULLIFIER_LENGTH) + (MAX_L2_TO_L1_MSGS_PER_TX * SCOPED_L2_TO_L1_MESSAGE_LENGTH) + (MAX_NOTE_ENCRYPTED_LOGS_PER_TX * LOG_HASH_LENGTH) + (MAX_ENCRYPTED_LOGS_PER_TX * SCOPED_LOG_HASH_LENGTH) + (MAX_UNENCRYPTED_LOGS_PER_TX * SCOPED_LOG_HASH_LENGTH) + (MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * PUBLIC_DATA_UPDATE_REQUEST_LENGTH) + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * PUBLIC_CALL_REQUEST_LENGTH) + GAS_LENGTH; global PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = PUBLIC_VALIDATION_REQUESTS_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH + PUBLIC_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH + 1 + (MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX * PUBLIC_CALL_REQUEST_LENGTH) + AZTEC_ADDRESS_LENGTH; global KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = ROLLUP_VALIDATION_REQUESTS_LENGTH + COMBINED_ACCUMULATED_DATA_LENGTH + COMBINED_CONSTANT_DATA_LENGTH + PARTIAL_STATE_REFERENCE_LENGTH + 1 + AZTEC_ADDRESS_LENGTH; diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index e3efc23cd07..6dd080e103c 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -168,13 +168,13 @@ export const PUBLIC_DATA_READ_LENGTH = 2; export const PRIVATE_VALIDATION_REQUESTS_LENGTH = 772; export const PUBLIC_VALIDATION_REQUESTS_LENGTH = 514; export const PUBLIC_DATA_UPDATE_REQUEST_LENGTH = 3; -export const COMBINED_ACCUMULATED_DATA_LENGTH = 364; +export const COMBINED_ACCUMULATED_DATA_LENGTH = 610; export const COMBINED_CONSTANT_DATA_LENGTH = 43; export const PRIVATE_ACCUMULATED_DATA_LENGTH = 1336; export const PRIVATE_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 2167; -export const PUBLIC_ACCUMULATED_DATA_LENGTH = 1279; -export const PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 3565; -export const KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 417; +export const PUBLIC_ACCUMULATED_DATA_LENGTH = 1311; +export const PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 3629; +export const KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 663; export const CONSTANT_ROLLUP_DATA_LENGTH = 12; export const BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 29; export const GET_NOTES_ORACLE_RETURN_LENGTH = 674; From c51076221f4791de92202e573b9884f98dada0f4 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 13:28:48 +0000 Subject: [PATCH 06/10] fix: mock logs --- yarn-project/circuit-types/src/mocks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/circuit-types/src/mocks.ts b/yarn-project/circuit-types/src/mocks.ts index a72c6d8a7d8..62bae58036f 100644 --- a/yarn-project/circuit-types/src/mocks.ts +++ b/yarn-project/circuit-types/src/mocks.ts @@ -122,7 +122,7 @@ export const mockTx = ( if (data.forPublic) { const hash = new ScopedLogHash( new LogHash( - Fr.fromBuffer(log.getSiloedHash()), + Fr.fromBuffer(log.hash()), i++, // +4 for encoding the length of the buffer new Fr(log.length + 4), From f255d425817b0e74d2cc429decb63803a46da8d9 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 13:34:32 +0000 Subject: [PATCH 07/10] fix: encrypted logs filtering --- yarn-project/circuit-types/src/logs/tx_l2_logs.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/yarn-project/circuit-types/src/logs/tx_l2_logs.ts b/yarn-project/circuit-types/src/logs/tx_l2_logs.ts index 6ebeac3499b..6cb5f7b613f 100644 --- a/yarn-project/circuit-types/src/logs/tx_l2_logs.ts +++ b/yarn-project/circuit-types/src/logs/tx_l2_logs.ts @@ -133,12 +133,17 @@ export abstract class TxL2Logs slh.contractAddress.equals(log.contractAddress) && slh.value.equals(Fr.fromBuffer(log.hash())), + slh => slh.contractAddress.equals(contractAddress) && slh.value.equals(Fr.fromBuffer(log.hash())), ) != -1 ) { include = true; From 7bbca03741cc216a2d94db3827200b6908ae0df0 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 14:19:56 +0000 Subject: [PATCH 08/10] chore: fix some noir tests --- .../src/private_kernel_tail.nr | 69 ++++- .../validate_accumulated_values.nr | 61 ++-- .../validate_gas_used.nr | 1 - .../tail_to_public_output_composer.nr | 292 +++++++++--------- .../crates/types/src/tests/fixture_builder.nr | 34 +- 5 files changed, 247 insertions(+), 210 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr index 1ead05b78a4..a52abc72a81 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_tail.nr @@ -66,7 +66,7 @@ mod tests { abis::{ kernel_circuit_public_inputs::KernelCircuitPublicInputs, max_block_number::MaxBlockNumber, note_hash::{NoteHash, ScopedNoteHash}, nullifier::{Nullifier, ScopedNullifier}, gas::Gas, - log_hash::{ScopedLogHash, ScopedEncryptedLogHash, LogHash} + log_hash::{ScopedLogHash, ScopedEncryptedLogHash, LogHash, NoteLogHash} }, address::{AztecAddress, EthAddress}, scalar::Scalar, hash::{sha256_to_field, silo_note_hash, silo_nullifier, mask_encrypted_log_hash}, @@ -169,12 +169,8 @@ mod tests { #[test] fn ordering_of_encrypted_logs() { let mut builder = PrivateKernelTailInputsBuilder::new(); - // Logs for the previous call stack. - let prev_log_hashes = [80, 90]; - let prev_log_lengths = [13, 14]; - for i in 0..prev_log_hashes.len() { - builder.previous_kernel.add_encrypted_log_hash(prev_log_hashes[i], prev_log_lengths[i]); - } + + builder.previous_kernel.append_encrypted_log_hashes(2); // Set the randomness in the first log to 0 to signal not masking the address to silo with builder.previous_kernel.encrypted_logs_hashes.storage[0].log_hash.randomness = 0; @@ -193,15 +189,70 @@ mod tests { assert_eq( resulting_encrypted_logs[0], ScopedLogHash { contract_address: builder.previous_kernel.storage_contract_address, // Not masked, randomness = 0 - log_hash: LogHash { value: prev_log_hashes[0], counter: 0, length: prev_log_lengths[0] } + log_hash: LogHash { value: reversed_logs[1].log_hash.value, counter: 0, length: reversed_logs[1].log_hash.length } } ); assert_eq( resulting_encrypted_logs[1], ScopedLogHash { contract_address: mask_encrypted_log_hash(reversed_logs[0]), - log_hash: LogHash { value: prev_log_hashes[1], counter: 0, length: prev_log_lengths[1] } + log_hash: LogHash { value: reversed_logs[0].log_hash.value, counter: 0, length: reversed_logs[0].log_hash.length } + } + ); + } + + #[test] + fn ordering_of_note_encrypted_logs() { + let mut builder = PrivateKernelTailInputsBuilder::new(); + + builder.previous_kernel.append_note_encrypted_log_hashes(2); + + // Reorder the logs + let original_logs = builder.previous_kernel.note_encrypted_logs_hashes.storage; + let mut reversed_logs = [NoteLogHash::empty(); 2]; + for i in 0..reversed_logs.len() { + reversed_logs[i] = builder.previous_kernel.note_encrypted_logs_hashes.pop(); } + builder.previous_kernel.note_encrypted_logs_hashes.extend_from_array(reversed_logs); + + let public_inputs = builder.execute(); + + let resulting_encrypted_logs = public_inputs.end.note_encrypted_logs_hashes; + + assert_eq( + resulting_encrypted_logs, original_logs.map( + |log: NoteLogHash| { + LogHash { value: log.value, counter: 0, length: log.length } + } + ) + ); + } + + #[test] + fn ordering_of_unencrypted_logs() { + let mut builder = PrivateKernelTailInputsBuilder::new(); + + builder.previous_kernel.append_unencrypted_log_hashes(2); + + // Reorder the logs + let original_logs = builder.previous_kernel.unencrypted_logs_hashes.storage; + let mut reversed_logs = [ScopedLogHash::empty(); 2]; + for i in 0..reversed_logs.len() { + reversed_logs[i] = builder.previous_kernel.unencrypted_logs_hashes.pop(); + } + builder.previous_kernel.unencrypted_logs_hashes.extend_from_array(reversed_logs); + + let public_inputs = builder.execute(); + + let resulting_encrypted_logs = public_inputs.end.unencrypted_logs_hashes; + + assert_eq( + resulting_encrypted_logs, original_logs.map( + |mut log: ScopedLogHash| { + log.log_hash.counter = 0; + log + } + ) ); } diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr index 5dde4e9f67d..ffdb03c7f9e 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr @@ -13,7 +13,6 @@ fn validate_accumulated_values_note_encrypted_log_hashes_succeeds() { builder.previous_kernel.append_note_encrypted_log_hashes(3); builder.output.append_note_encrypted_log_hashes(3); - builder.output.hash_note_encrypted_log_hashes(); builder.validate(); } @@ -26,12 +25,11 @@ fn validate_accumulated_values_note_encrypted_log_hashes_unordered_succeeds() { // Swap the items at index 0 and 2. swap_items(&mut builder.previous_kernel.note_encrypted_logs_hashes, 0, 2); builder.output.append_note_encrypted_log_hashes(3); - builder.output.hash_note_encrypted_log_hashes(); builder.validate(); } -#[test(should_fail_with="mismatch note_encrypted_logs_hash")] +#[test(should_fail_with="mismatch note_encrypted_logs_hashes")] fn validate_accumulated_values_note_encrypted_log_hashes_wrong_hash_fails() { let mut builder = TailOutputValidatorBuilder::new(); @@ -39,7 +37,6 @@ fn validate_accumulated_values_note_encrypted_log_hashes_wrong_hash_fails() { builder.output.append_note_encrypted_log_hashes(3); // Swap the items in the output, making them out of order, and then hash. swap_items(&mut builder.output.note_encrypted_logs_hashes, 0, 2); - builder.output.hash_note_encrypted_log_hashes(); builder.validate(); } @@ -48,42 +45,42 @@ fn validate_accumulated_values_note_encrypted_log_hashes_wrong_hash_fails() { * encrypted_log_hashes */ -// #[test] -// fn validate_accumulated_values_encrypted_log_hashes_succeeds() { -// let mut builder = TailOutputValidatorBuilder::new(); +#[test] +fn validate_accumulated_values_encrypted_log_hashes_succeeds() { + let mut builder = TailOutputValidatorBuilder::new(); -// builder.previous_kernel.append_encrypted_log_hashes(3); -// builder.output.append_encrypted_log_hashes(3); -// builder.output.hash_encrypted_log_hashes(); + builder.previous_kernel.append_encrypted_log_hashes(3); + builder.output.append_encrypted_log_hashes(3); + builder.output.mask_encrypted_log_hashes(); -// builder.validate(); -// } + builder.validate(); +} -// #[test] -// fn validate_accumulated_values_encrypted_log_hashes_unordered_succeeds() { -// let mut builder = TailOutputValidatorBuilder::new(); +#[test] +fn validate_accumulated_values_encrypted_log_hashes_unordered_succeeds() { + let mut builder = TailOutputValidatorBuilder::new(); -// builder.previous_kernel.append_encrypted_log_hashes(3); -// // Swap the items at index 0 and 2. -// swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 0, 2); -// builder.output.append_encrypted_log_hashes(3); -// builder.output.hash_encrypted_log_hashes(); + builder.previous_kernel.append_encrypted_log_hashes(3); + // Swap the items at index 0 and 2. + swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 0, 2); + builder.output.append_encrypted_log_hashes(3); + builder.output.mask_encrypted_log_hashes(); -// builder.validate(); -// } + builder.validate(); +} -// #[test(should_fail_with="mismatch encrypted_logs_hash")] -// fn validate_accumulated_values_encrypted_log_hashes_wrong_hash_fails() { -// let mut builder = TailOutputValidatorBuilder::new(); +#[test(should_fail_with="mismatch encrypted_logs_hash")] +fn validate_accumulated_values_encrypted_log_hashes_wrong_hash_fails() { + let mut builder = TailOutputValidatorBuilder::new(); -// builder.previous_kernel.append_encrypted_log_hashes(3); -// builder.output.append_encrypted_log_hashes(3); -// // Swap the items in the output, making them out of order, and then hash. -// swap_items(&mut builder.output.encrypted_logs_hashes, 0, 2); -// builder.output.hash_encrypted_log_hashes(); + builder.previous_kernel.append_encrypted_log_hashes(3); + builder.output.append_encrypted_log_hashes(3); + // Swap the items in the output, making them out of order, and then hash. + swap_items(&mut builder.output.encrypted_logs_hashes, 0, 2); + builder.output.mask_encrypted_log_hashes(); -// builder.validate(); -// } + builder.validate(); +} /** * unencrypted_log_hashes diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_gas_used.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_gas_used.nr index de9c3b78ff9..7e5bb139132 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_gas_used.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_gas_used.nr @@ -9,7 +9,6 @@ impl TailOutputValidatorBuilder { builder.previous_kernel.append_note_encrypted_log_hashes(3); builder.output.append_note_encrypted_log_hashes(3); - builder.output.hash_note_encrypted_log_hashes(); builder } diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr index 2b14a0255dc..53c8d473488 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_to_public_output_composer_builder/tail_to_public_output_composer.nr @@ -8,149 +8,149 @@ use dep::types::{ tests::utils::{assert_array_eq, swap_items} }; -// #[test] -// fn tail_to_public_output_composer_succeeds() { -// let mut (builder, siloed_data_builder) = TailToPublicOutputComposerBuilder::new().with_siloed_data_builder(); - -// let teardown_gas = Gas::new(789, 3254); -// builder.previous_kernel.tx_context.gas_settings.teardown_gas_limits = teardown_gas; - -// // Non-revertibles. -// builder.previous_kernel.append_note_hashes(4); -// siloed_data_builder.append_siloed_note_hashes(4); - -// builder.previous_kernel.append_nullifiers(2); -// siloed_data_builder.append_siloed_nullifiers(2); - -// builder.previous_kernel.append_l2_to_l1_msgs(1); -// siloed_data_builder.append_exposed_l2_to_l1_msgs(1); - -// builder.previous_kernel.add_note_encrypted_log_hash(1001, 12, 0); -// builder.previous_kernel.add_note_encrypted_log_hash(1002, 8, 0); - -// builder.previous_kernel.add_encrypted_log_hash(2001, 2); -// siloed_data_builder.add_siloed_encrypted_log_hash(2001, 2); - -// builder.previous_kernel.add_unencrypted_log_hash(3001, 51); - -// builder.previous_kernel.append_public_call_requests(2); - -// builder.previous_kernel.end_setup(); - -// // Revertibles. -// builder.previous_kernel.append_note_hashes(2); -// siloed_data_builder.append_siloed_note_hashes(2); - -// builder.previous_kernel.append_nullifiers(1); -// siloed_data_builder.append_siloed_nullifiers(1); - -// builder.previous_kernel.append_l2_to_l1_msgs(1); -// siloed_data_builder.append_exposed_l2_to_l1_msgs(1); - -// builder.previous_kernel.add_note_encrypted_log_hash(1003, 20, 0); - -// builder.previous_kernel.add_encrypted_log_hash(2002, 6); -// siloed_data_builder.add_siloed_encrypted_log_hash(2002, 6); -// builder.previous_kernel.add_encrypted_log_hash(2003, 24); -// siloed_data_builder.add_siloed_encrypted_log_hash(2003, 24); - -// builder.previous_kernel.add_unencrypted_log_hash(3002, 4); - -// builder.previous_kernel.append_public_call_requests(3); - -// // Get ordered items before shuffling for verifying with the output later. -// let siloed_data = siloed_data_builder.to_exposed_public_accumulated_data(); -// let unsiloed_data = builder.previous_kernel.to_exposed_public_accumulated_data(); - -// // Shuffle ordered items. -// swap_items(&mut builder.previous_kernel.note_hashes, 4, 0); -// swap_items(&mut builder.previous_kernel.note_hashes, 3, 2); -// swap_items(&mut builder.previous_kernel.nullifiers, 1, 3); -// swap_items(&mut builder.previous_kernel.l2_to_l1_msgs, 0, 1); -// swap_items(&mut builder.previous_kernel.note_encrypted_logs_hashes, 1, 2); -// swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 1, 2); -// swap_items(&mut builder.previous_kernel.public_call_requests, 1, 2); - -// // Output. -// let output = builder.finish(); - -// // note_hashes -// let siloed = siloed_data.note_hashes; -// assert_array_eq( -// output.end_non_revertible.note_hashes, -// [siloed[0], siloed[1], siloed[2], siloed[3]] -// ); -// assert_array_eq(output.end.note_hashes, [siloed[4], siloed[5]]); - -// // nullifiers -// let siloed = siloed_data.nullifiers; -// let unsiloed = unsiloed_data.nullifiers; -// assert_array_eq( -// output.end_non_revertible.nullifiers, -// [unsiloed[0], siloed[1], siloed[2]] -// ); -// assert_array_eq(output.end.nullifiers, [siloed[3]]); - -// // l2_to_l1_msgs -// let siloed = siloed_data.l2_to_l1_msgs; -// assert_array_eq(output.end_non_revertible.l2_to_l1_msgs, [siloed[0]]); -// assert_array_eq(output.end.l2_to_l1_msgs, [siloed[1]]); - -// // note_encrypted_logs_hashes -// let unsiloed = unsiloed_data.note_encrypted_logs_hashes; -// assert_array_eq( -// output.end_non_revertible.note_encrypted_logs_hashes, -// [unsiloed[0], unsiloed[1]] -// ); -// assert_array_eq(output.end.note_encrypted_logs_hashes, [unsiloed[2]]); - -// // encrypted_logs_hashes -// let siloed = siloed_data.encrypted_logs_hashes; -// assert_array_eq(output.end_non_revertible.encrypted_logs_hashes, [siloed[0]]); -// assert_array_eq(output.end.encrypted_logs_hashes, [siloed[1], siloed[2]]); - -// // unencrypted_logs_hashes -// let unsiloed = unsiloed_data.unencrypted_logs_hashes; -// assert_array_eq( -// output.end_non_revertible.unencrypted_logs_hashes, -// [unsiloed[0]] -// ); -// assert_array_eq(output.end.unencrypted_logs_hashes, [unsiloed[1]]); - -// // public_call_stack -// let unsiloed = unsiloed_data.public_call_stack; -// assert_array_eq( -// output.end_non_revertible.public_call_stack, -// [unsiloed[1], unsiloed[0]] -// ); -// assert_array_eq( -// output.end.public_call_stack, -// [unsiloed[4], unsiloed[3], unsiloed[2]] -// ); - -// // Gas: non-revertible -// let total_num_side_effects = 4 + 3 + 1; -// let total_log_length = 12 + 8 // note_encrypted_log_hash -// + 2 // encrypted_log_hash -// + 51; // unencrypted_log_hash -// let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; -// let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH -// + 3 * L2_GAS_PER_NULLIFIER -// + total_log_length * L2_GAS_PER_LOG_BYTE -// + 2 * FIXED_AVM_STARTUP_L2_GAS; -// assert_eq( -// output.end_non_revertible.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + Gas::tx_overhead() -// ); - -// // Gas: revertible -// let total_num_side_effects = 2 + 1 + 1; -// let total_log_length = 20 // note_encrypted_log_hash -// + 6 + 24 // encrypted_log_hash -// + 4; // unencrypted_log_hash -// let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; -// let computed_l2_gas = 2 * L2_GAS_PER_NOTE_HASH -// + 1 * L2_GAS_PER_NULLIFIER -// + total_log_length * L2_GAS_PER_LOG_BYTE -// + 3 * FIXED_AVM_STARTUP_L2_GAS; -// assert_eq(output.end.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + teardown_gas); -// } +#[test] +fn tail_to_public_output_composer_succeeds() { + let mut (builder, siloed_data_builder) = TailToPublicOutputComposerBuilder::new().with_siloed_data_builder(); + + let teardown_gas = Gas::new(789, 3254); + builder.previous_kernel.tx_context.gas_settings.teardown_gas_limits = teardown_gas; + + // Non-revertibles. + builder.previous_kernel.append_note_hashes(4); + siloed_data_builder.append_siloed_note_hashes(4); + + builder.previous_kernel.append_nullifiers(2); + siloed_data_builder.append_siloed_nullifiers(2); + + builder.previous_kernel.append_l2_to_l1_msgs(1); + siloed_data_builder.append_exposed_l2_to_l1_msgs(1); + + builder.previous_kernel.add_note_encrypted_log_hash(1001, 12, 0); + builder.previous_kernel.add_note_encrypted_log_hash(1002, 8, 0); + + builder.previous_kernel.add_encrypted_log_hash(2001, 2); + siloed_data_builder.add_masked_encrypted_log_hash(2001, 2); + + builder.previous_kernel.add_unencrypted_log_hash(3001, 51); + + builder.previous_kernel.append_public_call_requests(2); + + builder.previous_kernel.end_setup(); + + // Revertibles. + builder.previous_kernel.append_note_hashes(2); + siloed_data_builder.append_siloed_note_hashes(2); + + builder.previous_kernel.append_nullifiers(1); + siloed_data_builder.append_siloed_nullifiers(1); + + builder.previous_kernel.append_l2_to_l1_msgs(1); + siloed_data_builder.append_exposed_l2_to_l1_msgs(1); + + builder.previous_kernel.add_note_encrypted_log_hash(1003, 20, 0); + + builder.previous_kernel.add_encrypted_log_hash(2002, 6); + siloed_data_builder.add_masked_encrypted_log_hash(2002, 6); + builder.previous_kernel.add_encrypted_log_hash(2003, 24); + siloed_data_builder.add_masked_encrypted_log_hash(2003, 24); + + builder.previous_kernel.add_unencrypted_log_hash(3002, 4); + + builder.previous_kernel.append_public_call_requests(3); + + // Get ordered items before shuffling for verifying with the output later. + let siloed_data = siloed_data_builder.to_exposed_public_accumulated_data(); + let unsiloed_data = builder.previous_kernel.to_exposed_public_accumulated_data(); + + // Shuffle ordered items. + swap_items(&mut builder.previous_kernel.note_hashes, 4, 0); + swap_items(&mut builder.previous_kernel.note_hashes, 3, 2); + swap_items(&mut builder.previous_kernel.nullifiers, 1, 3); + swap_items(&mut builder.previous_kernel.l2_to_l1_msgs, 0, 1); + swap_items(&mut builder.previous_kernel.note_encrypted_logs_hashes, 1, 2); + swap_items(&mut builder.previous_kernel.encrypted_logs_hashes, 1, 2); + swap_items(&mut builder.previous_kernel.public_call_requests, 1, 2); + + // Output. + let output = builder.finish(); + + // note_hashes + let siloed = siloed_data.note_hashes; + assert_array_eq( + output.end_non_revertible.note_hashes, + [siloed[0], siloed[1], siloed[2], siloed[3]] + ); + assert_array_eq(output.end.note_hashes, [siloed[4], siloed[5]]); + + // nullifiers + let siloed = siloed_data.nullifiers; + let unsiloed = unsiloed_data.nullifiers; + assert_array_eq( + output.end_non_revertible.nullifiers, + [unsiloed[0], siloed[1], siloed[2]] + ); + assert_array_eq(output.end.nullifiers, [siloed[3]]); + + // l2_to_l1_msgs + let siloed = siloed_data.l2_to_l1_msgs; + assert_array_eq(output.end_non_revertible.l2_to_l1_msgs, [siloed[0]]); + assert_array_eq(output.end.l2_to_l1_msgs, [siloed[1]]); + + // note_encrypted_logs_hashes + let unsiloed = unsiloed_data.note_encrypted_logs_hashes; + assert_array_eq( + output.end_non_revertible.note_encrypted_logs_hashes, + [unsiloed[0], unsiloed[1]] + ); + assert_array_eq(output.end.note_encrypted_logs_hashes, [unsiloed[2]]); + + // encrypted_logs_hashes + let siloed = siloed_data.encrypted_logs_hashes; + assert_array_eq(output.end_non_revertible.encrypted_logs_hashes, [siloed[0]]); + assert_array_eq(output.end.encrypted_logs_hashes, [siloed[1], siloed[2]]); + + // unencrypted_logs_hashes + let unsiloed = unsiloed_data.unencrypted_logs_hashes; + assert_array_eq( + output.end_non_revertible.unencrypted_logs_hashes, + [unsiloed[0]] + ); + assert_array_eq(output.end.unencrypted_logs_hashes, [unsiloed[1]]); + + // public_call_stack + let unsiloed = unsiloed_data.public_call_stack; + assert_array_eq( + output.end_non_revertible.public_call_stack, + [unsiloed[1], unsiloed[0]] + ); + assert_array_eq( + output.end.public_call_stack, + [unsiloed[4], unsiloed[3], unsiloed[2]] + ); + + // Gas: non-revertible + let total_num_side_effects = 4 + 3 + 1; + let total_log_length = 12 + 8 // note_encrypted_log_hash + + 2 // encrypted_log_hash + + 51; // unencrypted_log_hash + let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; + let computed_l2_gas = 4 * L2_GAS_PER_NOTE_HASH + + 3 * L2_GAS_PER_NULLIFIER + + total_log_length * L2_GAS_PER_LOG_BYTE + + 2 * FIXED_AVM_STARTUP_L2_GAS; + assert_eq( + output.end_non_revertible.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + Gas::tx_overhead() + ); + + // Gas: revertible + let total_num_side_effects = 2 + 1 + 1; + let total_log_length = 20 // note_encrypted_log_hash + + 6 + 24 // encrypted_log_hash + + 4; // unencrypted_log_hash + let computed_da_gas = (total_num_side_effects * DA_BYTES_PER_FIELD + total_log_length) * DA_GAS_PER_BYTE; + let computed_l2_gas = 2 * L2_GAS_PER_NOTE_HASH + + 1 * L2_GAS_PER_NULLIFIER + + total_log_length * L2_GAS_PER_LOG_BYTE + + 3 * FIXED_AVM_STARTUP_L2_GAS; + assert_eq(output.end.gas_used, Gas::new(computed_da_gas, computed_l2_gas) + teardown_gas); +} diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr index 9f22bfc551e..a03d4ddb24a 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/fixture_builder.nr @@ -34,7 +34,7 @@ use crate::{ }, hash::{ compute_l2_to_l1_hash, compute_tx_logs_hash, compute_tx_note_logs_hash, compute_siloed_nullifier, - silo_encrypted_log_hash, silo_note_hash, silo_unencrypted_log_hash + silo_encrypted_log_hash, silo_note_hash, silo_unencrypted_log_hash, mask_encrypted_log_hash }, header::Header, merkle_tree::membership::MembershipWitness, messaging::l2_to_l1_message::{L2ToL1Message, ScopedL2ToL1Message}, @@ -719,23 +719,18 @@ impl FixtureBuilder { } } - pub fn hash_note_encrypted_log_hashes(&mut self) { - let log_hashes = self.note_encrypted_logs_hashes.storage.map(|l: NoteLogHash| l.expose_to_public()); - self.note_encrypted_logs_hash = compute_tx_note_logs_hash(log_hashes); - } - pub fn add_encrypted_log_hash(&mut self, hash: Field, length: Field) { let log_hash = EncryptedLogHash { value: hash, counter: self.next_counter(), length, randomness: 2 }; self.encrypted_logs_hashes.push(log_hash.scope(self.storage_contract_address)); self.encrypted_log_preimages_length += length; } - // pub fn add_siloed_encrypted_log_hash(&mut self, hash: Field, length: Field) { - // let mut log_hash = EncryptedLogHash { value: hash, counter: self.next_counter(), length, randomness: 2 }.scope(self.storage_contract_address); - // log_hash.log_hash.value = silo_encrypted_log_hash(log_hash); - // self.encrypted_logs_hashes.push(log_hash); - // self.encrypted_log_preimages_length += length; - // } + pub fn add_masked_encrypted_log_hash(&mut self, hash: Field, length: Field) { + let mut log_hash = EncryptedLogHash { value: hash, counter: self.next_counter(), length, randomness: 2 }.scope(self.storage_contract_address); + log_hash.contract_address = mask_encrypted_log_hash(log_hash); + self.encrypted_logs_hashes.push(log_hash); + self.encrypted_log_preimages_length += length; + } pub fn append_encrypted_log_hashes(&mut self, num: u32) { let index_offset = self.encrypted_logs_hashes.len(); @@ -747,16 +742,11 @@ impl FixtureBuilder { } } - // pub fn hash_encrypted_log_hashes(&mut self) { - // let mut log_hashes = self.encrypted_logs_hashes.storage.map(|l: ScopedEncryptedLogHash| l.expose_to_public()); - // for i in 0..self.encrypted_logs_hashes.max_len() { - // let log_hash = self.encrypted_logs_hashes.storage[i]; - // if !log_hash.contract_address.is_zero() { - // log_hashes[i].value = silo_encrypted_log_hash(log_hash); - // } - // } - // self.encrypted_logs_hash = compute_tx_logs_hash(log_hashes); - // } + pub fn mask_encrypted_log_hashes(&mut self) { + for i in 0..self.encrypted_logs_hashes.max_len() { + self.encrypted_logs_hashes.storage[i].contract_address = mask_encrypted_log_hash(self.encrypted_logs_hashes.storage[i]); + } + } pub fn add_unencrypted_log_hash(&mut self, hash: Field, length: Field) { let log_hash = LogHash { value: hash, counter: self.next_counter(), length }; From edf2e24c4f5136f0204733c2d0a782685edd4c4e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 14:28:54 +0000 Subject: [PATCH 09/10] fix public tail test --- .../src/public_kernel_tail.nr | 62 +++++++++---------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr index f7e087a6b97..9debf85bfe1 100644 --- a/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr +++ b/noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/public_kernel_tail.nr @@ -415,39 +415,35 @@ mod tests { // TODO: Check the values in public inputs. } - // #[test] - // fn logs_are_handled_as_expected() { - // let mut builder = PublicKernelTailCircuitPrivateInputsBuilder::new(); - // // Logs for the previous call stack. - // let prev_encrypted_logs_hash = 80; - // let prev_encrypted_log_preimages_length = 13; - // let prev_unencrypted_logs_hash = 956; - // let prev_unencrypted_log_preimages_length = 24; - // builder.previous_revertible.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); - // builder.previous_revertible.add_unencrypted_log_hash( - // prev_unencrypted_logs_hash, - // prev_unencrypted_log_preimages_length - // ); - // // Logs for the current call stack. - // let unencrypted_logs_hash = 26; - // let unencrypted_log_preimages_length = 50; - // builder.previous_revertible.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); - - // let public_inputs = builder.execute(); - - // assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); - // assert_eq( - // public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length - // ); - - // let hash_bytes: [u8; MAX_ENCRYPTED_LOGS_PER_TX * 32] = prev_encrypted_logs_hash.to_be_bytes(32).append(&[0; MAX_ENCRYPTED_LOGS_PER_TX * 32 - 32]).as_array(); - // let expected_encrypted_logs_hash = sha256_to_field(hash_bytes); - // assert_eq(public_inputs.end.encrypted_logs_hash, expected_encrypted_logs_hash); - - // assert_eq( - // public_inputs.end.unencrypted_logs_hashes, builder.previous_revertible.unencrypted_logs_hashes.storage - // ); - // } + #[test] + fn measuring_of_log_lengths() { + let mut builder = PublicKernelTailCircuitPrivateInputsBuilder::new(); + // Logs for the previous call stack. + let prev_encrypted_logs_hash = 80; + let prev_encrypted_log_preimages_length = 13; + let prev_unencrypted_logs_hash = 956; + let prev_unencrypted_log_preimages_length = 24; + builder.previous_revertible.add_encrypted_log_hash(prev_encrypted_logs_hash, prev_encrypted_log_preimages_length); + builder.previous_revertible.add_unencrypted_log_hash( + prev_unencrypted_logs_hash, + prev_unencrypted_log_preimages_length + ); + // Logs for the current call stack. + let unencrypted_logs_hash = 26; + let unencrypted_log_preimages_length = 50; + builder.previous_revertible.add_unencrypted_log_hash(unencrypted_logs_hash, unencrypted_log_preimages_length); + + let public_inputs = builder.execute(); + + assert_eq(public_inputs.end.encrypted_log_preimages_length, prev_encrypted_log_preimages_length); + assert_eq( + public_inputs.end.unencrypted_log_preimages_length, unencrypted_log_preimages_length + prev_unencrypted_log_preimages_length + ); + + assert_eq( + public_inputs.end.unencrypted_logs_hashes, builder.previous_revertible.unencrypted_logs_hashes.storage + ); + } #[test] unconstrained fn one_pending_nullifier_read_request() { From f042f7dd0829b8e665ca779a6de20d1a1f33ad0b Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 7 Aug 2024 15:03:43 +0000 Subject: [PATCH 10/10] refactor validator --- .../src/components/tail_output_validator.nr | 6 +----- .../components/tail_output_validator/tail_output_hints.nr | 3 --- .../validate_accumulated_values.nr | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr index 323d949933f..c81d74a8623 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator.nr @@ -131,14 +131,10 @@ impl TailOutputValidator { assert_sorted_transformed_value_array( self.previous_kernel.end.encrypted_logs_hashes, self.hints.masked_encrypted_log_hashes, - self.hints.sorted_masked_encrypted_log_hashes, + self.output.end.encrypted_logs_hashes, self.hints.sorted_masked_encrypted_log_hash_hints ); - assert_eq( - self.hints.sorted_masked_encrypted_log_hashes, self.output.end.encrypted_logs_hashes, "mismatch encrypted_logs_hashes" - ); - // unencrypted_log_hashes assert_sorted_array_with_order_hints( self.previous_kernel.end.unencrypted_logs_hashes, diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr index f0e75a0bc90..32736bb7535 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/tail_output_validator/tail_output_hints.nr @@ -25,7 +25,6 @@ struct TailOutputHints { sorted_note_encrypted_log_hash_hints: [OrderHint; MAX_NOTE_ENCRYPTED_LOGS_PER_TX], // Encrypted log hashes. masked_encrypted_log_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], - sorted_masked_encrypted_log_hashes: [ScopedLogHash; MAX_ENCRYPTED_LOGS_PER_TX], sorted_masked_encrypted_log_hash_hints: [OrderHint; MAX_ENCRYPTED_LOGS_PER_TX], // Unencrypted log hashes. sorted_unencrypted_log_hashes: [ScopedLogHash; MAX_UNENCRYPTED_LOGS_PER_TX], @@ -52,7 +51,6 @@ unconstrained pub fn generate_tail_output_hints(previous_kernel: PrivateKernelCi for i in 0..masked_log_hashes.len() { masked_log_hashes[i].contract_address = mask_encrypted_log_hash(previous_kernel.end.encrypted_logs_hashes[i]); } - let sorted_masked_encrypted_log_hashes = sort_by_counters_asc(masked_log_hashes).map(|h: ScopedEncryptedLogHash| h.expose_to_public()); let masked_encrypted_log_hashes = masked_log_hashes.map(|h: ScopedEncryptedLogHash| h.expose_to_public()); let sorted_masked_encrypted_log_hash_hints = sort_get_order_hints_asc(previous_kernel.end.encrypted_logs_hashes); @@ -68,7 +66,6 @@ unconstrained pub fn generate_tail_output_hints(previous_kernel: PrivateKernelCi sorted_note_encrypted_log_hashes, sorted_note_encrypted_log_hash_hints, masked_encrypted_log_hashes, - sorted_masked_encrypted_log_hashes, sorted_masked_encrypted_log_hash_hints, sorted_unencrypted_log_hashes, sorted_unencrypted_log_hash_hints diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr index ffdb03c7f9e..e38c86f8ef9 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/tail_output_validator_builder/validate_accumulated_values.nr @@ -69,7 +69,7 @@ fn validate_accumulated_values_encrypted_log_hashes_unordered_succeeds() { builder.validate(); } -#[test(should_fail_with="mismatch encrypted_logs_hash")] +#[test(should_fail_with="mismatch sorted values")] fn validate_accumulated_values_encrypted_log_hashes_wrong_hash_fails() { let mut builder = TailOutputValidatorBuilder::new();