diff --git a/compiler/noirc_evaluator/src/ssa/function_builder/mod.rs b/compiler/noirc_evaluator/src/ssa/function_builder/mod.rs index 7ad9a4b4031..9e17595a033 100644 --- a/compiler/noirc_evaluator/src/ssa/function_builder/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/function_builder/mod.rs @@ -384,9 +384,9 @@ impl FunctionBuilder { } } Type::Array(..) | Type::Slice(..) => { + self.insert_instruction(Instruction::IncrementRc { value }, None); // If there are nested arrays or slices, we wait until ArrayGet // is issued to increment the count of that array. - self.insert_instruction(Instruction::IncrementRc { value }, None); } } } diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs index 2219d0264ab..6ee7f312660 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/context.rs @@ -990,7 +990,6 @@ impl<'a> FunctionContext<'a> { new_value.for_each(|value| { let value = value.eval(self); array = self.builder.insert_array_set(array, index, value); - self.builder.increment_array_reference_count(array); index = self.builder.insert_binary(index, BinaryOp::Add, one); }); array diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs index 3780477cf71..ecc8bf87597 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs @@ -724,11 +724,6 @@ impl<'a> FunctionContext<'a> { let lhs = self.extract_current_value(&assign.lvalue)?; let rhs = self.codegen_expression(&assign.expression)?; - rhs.clone().for_each(|value| { - let value = value.eval(self); - self.builder.increment_array_reference_count(value); - }); - self.assign_new_value(lhs, rhs); Ok(Self::unit_value()) } diff --git a/test_programs/execution_success/brillig_cow_assign/Nargo.toml b/test_programs/execution_success/brillig_cow_assign/Nargo.toml deleted file mode 100644 index a878566a372..00000000000 --- a/test_programs/execution_success/brillig_cow_assign/Nargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "brillig_cow_assign" -type = "bin" -authors = [""] -compiler_version = ">=0.23.0" - -[dependencies] diff --git a/test_programs/execution_success/brillig_cow_assign/Prover.toml b/test_programs/execution_success/brillig_cow_assign/Prover.toml deleted file mode 100644 index 882c73b83f8..00000000000 --- a/test_programs/execution_success/brillig_cow_assign/Prover.toml +++ /dev/null @@ -1,2 +0,0 @@ -items_to_update = 10 -index = 6 diff --git a/test_programs/execution_success/brillig_cow_assign/src/main.nr b/test_programs/execution_success/brillig_cow_assign/src/main.nr deleted file mode 100644 index e5c3e2bd2f5..00000000000 --- a/test_programs/execution_success/brillig_cow_assign/src/main.nr +++ /dev/null @@ -1,23 +0,0 @@ -global N = 10; - -unconstrained fn main() { - let mut arr = [0; N]; - let mut mid_change = arr; - - for i in 0..N { - if i == N / 2 { - mid_change = arr; - } - arr[i] = 27; - } - - // Expect: - // arr = [27, 27, 27, 27, 27, 27, 27, 27, 27, 27] - // mid_change = [27, 27, 27, 27, 27, 0, 0, 0, 0, 0] - - let modified_i = N / 2 + 1; - assert_eq(arr[modified_i], 27); - - // Fail here! - assert(mid_change[modified_i] != 27); -} diff --git a/test_programs/execution_success/brillig_cow_regression/Nargo.toml b/test_programs/execution_success/brillig_cow_regression/Nargo.toml new file mode 100644 index 00000000000..c5bf60a1e78 --- /dev/null +++ b/test_programs/execution_success/brillig_cow_regression/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "brillig_cow_regression" +type = "bin" +authors = [""] + +[dependencies] diff --git a/test_programs/execution_success/brillig_cow_regression/Prover.toml b/test_programs/execution_success/brillig_cow_regression/Prover.toml new file mode 100644 index 00000000000..f0a4dc2485d --- /dev/null +++ b/test_programs/execution_success/brillig_cow_regression/Prover.toml @@ -0,0 +1,229 @@ +[kernel_data] +encrypted_logs_hash = [ + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", +] +new_commitments = [ + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", +] +new_l2_to_l1_msgs = [ + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", +] +new_nullifiers = [ + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", +] +unencrypted_logs_hash = [ + "0x0000000000000000000000000000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000000000000000000000000000000", +] + +[[kernel_data.new_contracts]] +contract_address = "0x0000000000000000000000000000000000000000000000000000000000000000" +portal_contract_address = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" + +[[kernel_data.public_data_update_requests]] +leaf_slot = "0x0000000000000000000000000000000000000000000000000000000000000000" +new_value = "0x0000000000000000000000000000000000000000000000000000000000000000" +old_value = "0x0000000000000000000000000000000000000000000000000000000000000000" diff --git a/test_programs/execution_success/brillig_cow_regression/src/main.nr b/test_programs/execution_success/brillig_cow_regression/src/main.nr new file mode 100644 index 00000000000..974c17dfbc9 --- /dev/null +++ b/test_programs/execution_success/brillig_cow_regression/src/main.nr @@ -0,0 +1,178 @@ +// Tests a performance regression found in aztec-packages with brillig cow optimization + +global MAX_NEW_COMMITMENTS_PER_TX: Field = 64; +global MAX_NEW_NULLIFIERS_PER_TX: Field = 64; +global MAX_NEW_L2_TO_L1_MSGS_PER_TX: Field = 2; +global MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX: Field = 16; +global MAX_NEW_CONTRACTS_PER_TX: Field = 1; +global NUM_ENCRYPTED_LOGS_HASHES_PER_TX: Field = 1; +global NUM_UNENCRYPTED_LOGS_HASHES_PER_TX: Field = 1; +global NUM_FIELDS_PER_SHA256 = 2; +global CALLDATA_HASH_INPUT_SIZE = 169; +global CALL_DATA_HASH_LOG_FIELDS = 4; +global CALL_DATA_HASH_FULL_FIELDS = 165; + +struct PublicDataUpdateRequest { + leaf_slot : Field, + old_value : Field, + new_value : Field +} + +struct NewContractData { + contract_address: Field, + portal_contract_address: Field, +} + +impl NewContractData { + fn hash(self) -> Field { + dep::std::hash::pedersen_hash([self.contract_address, self.portal_contract_address]) + } +} + +struct DataToHash { + new_commitments: [Field; MAX_NEW_COMMITMENTS_PER_TX], + new_nullifiers: [Field; MAX_NEW_NULLIFIERS_PER_TX], + public_data_update_requests: [PublicDataUpdateRequest; MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX], + new_l2_to_l1_msgs: [Field; MAX_NEW_L2_TO_L1_MSGS_PER_TX], + encrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256], + unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256], + new_contracts: [NewContractData; MAX_NEW_CONTRACTS_PER_TX], +} + +struct U256 { + // This is in big-endian order, typically because + // sha256 is usually in big endian order. + // Note: this means that inner[0] has the most significant 64 bits. + inner : [u64; 4] +} + +impl U256 { + pub fn from_bytes32(bytes : [u8;32]) -> U256 { + // We use addition rather than a bitwise OR as the bitshifts ensure that none of the bytes overlap each other. + let high_0 = ((bytes[0] as u64) << 56) + + ((bytes[1] as u64) << 48) + + ((bytes[2] as u64) << 40) + + ((bytes[3] as u64) << 32) + + ((bytes[4] as u64) << 24) + + ((bytes[5] as u64) << 16) + + ((bytes[6] as u64) << 8) + + (bytes[7] as u64); + + let high_1 = ((bytes[8] as u64) << 56) + + ((bytes[9] as u64) << 48) + + ((bytes[10] as u64) << 40) + + ((bytes[11] as u64) << 32) + + ((bytes[12] as u64) << 24) + + ((bytes[13] as u64) << 16) + + ((bytes[14] as u64) << 8) + + (bytes[15] as u64); + + let low_0 = ((bytes[16] as u64) << 56) + + ((bytes[17] as u64) << 48) + + ((bytes[18] as u64) << 40) + + ((bytes[19] as u64) << 32) + + ((bytes[20] as u64) << 24) + + ((bytes[21] as u64) << 16) + + ((bytes[22] as u64) << 8) + + (bytes[23] as u64); + + let low_1 = ((bytes[24] as u64) << 56) + + ((bytes[25] as u64) << 48) + + ((bytes[26] as u64) << 40) + + ((bytes[27] as u64) << 32) + + ((bytes[28] as u64) << 24) + + ((bytes[29] as u64) << 16) + + ((bytes[30] as u64) << 8) + + (bytes[31] as u64); + + U256{inner : [high_0, high_1, low_0, low_1]} + } + + pub fn to_u128_limbs(self) -> [Field;2] { + let two_pow_64 = 2.pow_32(64); + + let high = (self.inner[0] as Field) * two_pow_64 + self.inner[1] as Field; + let low = (self.inner[2] as Field) * two_pow_64 + self.inner[3] as Field; + + [high,low] + } +} + +unconstrained fn main(kernel_data: DataToHash) -> pub [Field; NUM_FIELDS_PER_SHA256] { + let mut calldata_hash_inputs = [0; CALLDATA_HASH_INPUT_SIZE]; + + let new_commitments = kernel_data.new_commitments; + let new_nullifiers = kernel_data.new_nullifiers; + let public_data_update_requests = kernel_data.public_data_update_requests; + let newL2ToL1msgs = kernel_data.new_l2_to_l1_msgs; + let encryptedLogsHash = kernel_data.encrypted_logs_hash; + let unencryptedLogsHash = kernel_data.unencrypted_logs_hash; + + let mut offset = 0; + + for j in 0..MAX_NEW_COMMITMENTS_PER_TX { + calldata_hash_inputs[offset + j] = new_commitments[j]; + } + offset += MAX_NEW_COMMITMENTS_PER_TX ; + + for j in 0..MAX_NEW_NULLIFIERS_PER_TX { + calldata_hash_inputs[offset + j] = new_nullifiers[j]; + } + offset += MAX_NEW_NULLIFIERS_PER_TX ; + + for j in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX { + calldata_hash_inputs[offset + j * 2] = + public_data_update_requests[j].leaf_slot; + calldata_hash_inputs[offset + j * 2 + 1] = + public_data_update_requests[j].new_value; + } + offset += MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * 2; + + for j in 0..MAX_NEW_L2_TO_L1_MSGS_PER_TX { + calldata_hash_inputs[offset + j] = newL2ToL1msgs[j]; + } + offset += MAX_NEW_L2_TO_L1_MSGS_PER_TX; + + let contract_leaf = kernel_data.new_contracts[0]; + calldata_hash_inputs[offset] = contract_leaf.hash(); + + offset += MAX_NEW_CONTRACTS_PER_TX; + + let new_contracts = kernel_data.new_contracts; + calldata_hash_inputs[offset] = new_contracts[0].contract_address; + + calldata_hash_inputs[offset + 1] = new_contracts[0].portal_contract_address; + + offset += MAX_NEW_CONTRACTS_PER_TX * 2; + + for j in 0..NUM_FIELDS_PER_SHA256 { + calldata_hash_inputs[offset + j] = encryptedLogsHash[j]; + } + + offset += NUM_ENCRYPTED_LOGS_HASHES_PER_TX * NUM_FIELDS_PER_SHA256; + + for j in 0..NUM_FIELDS_PER_SHA256 { + calldata_hash_inputs[offset + j] = unencryptedLogsHash[j]; + } + + offset += NUM_UNENCRYPTED_LOGS_HASHES_PER_TX * NUM_FIELDS_PER_SHA256; + assert_eq(offset, CALLDATA_HASH_INPUT_SIZE); // Sanity check + + let mut hash_input_flattened = [0; CALL_DATA_HASH_FULL_FIELDS * 32 + CALL_DATA_HASH_LOG_FIELDS * 16]; + for offset in 0..CALL_DATA_HASH_FULL_FIELDS { + let input_as_bytes = calldata_hash_inputs[offset].to_be_bytes(32); + for byte_index in 0..32 { + hash_input_flattened[offset * 32 + byte_index] = input_as_bytes[byte_index]; + } + } + + for log_field_index in 0..CALL_DATA_HASH_LOG_FIELDS { + let input_as_bytes = calldata_hash_inputs[CALL_DATA_HASH_FULL_FIELDS + log_field_index].to_be_bytes(16); + for byte_index in 0..16 { + hash_input_flattened[CALL_DATA_HASH_FULL_FIELDS * 32 + log_field_index * 16 + byte_index] = input_as_bytes[byte_index]; + } + } + + let sha_digest = dep::std::hash::sha256(hash_input_flattened); + U256::from_bytes32(sha_digest).to_u128_limbs() +}