diff --git a/.aztec-sync-commit b/.aztec-sync-commit index 49baefec2c3..58bcd84922c 100644 --- a/.aztec-sync-commit +++ b/.aztec-sync-commit @@ -1 +1 @@ -f5bbb89b489bc85f286bcc5ed45c30f38032810c +249e50efafd306fa8cd9005972636adbddbca81e diff --git a/aztec_macros/src/transforms/events.rs b/aztec_macros/src/transforms/events.rs index 8b71bd77ae6..ede8a350bf2 100644 --- a/aztec_macros/src/transforms/events.rs +++ b/aztec_macros/src/transforms/events.rs @@ -230,7 +230,7 @@ fn generate_fn_get_event_type_id( let function_source = format!( " fn get_event_type_id() -> dep::aztec::protocol_types::abis::event_selector::EventSelector {{ - dep::aztec::protocol_types::abis::event_selector::EventSelector::from_signature(\"{event_type}({from_signature_input})\") + comptime {{ dep::aztec::protocol_types::abis::event_selector::EventSelector::from_signature(\"{event_type}({from_signature_input})\") }} }} ", ) @@ -260,8 +260,8 @@ fn generate_fn_private_to_be_bytes( fn private_to_be_bytes(self: {event_type}, randomness: Field) -> [u8; {byte_length}] {{ let mut buffer: [u8; {byte_length}] = [0; {byte_length}]; - let randomness_bytes = randomness.to_be_bytes(32); - let event_type_id_bytes = {event_type}::get_event_type_id().to_field().to_be_bytes(32); + let randomness_bytes: [u8; 32] = randomness.to_be_bytes(); + let event_type_id_bytes: [u8; 32] = {event_type}::get_event_type_id().to_field().to_be_bytes(); for i in 0..32 {{ buffer[i] = randomness_bytes[i]; @@ -271,7 +271,7 @@ fn generate_fn_private_to_be_bytes( let serialized_event = self.serialize(); for i in 0..serialized_event.len() {{ - let bytes = serialized_event[i].to_be_bytes(32); + let bytes: [u8; 32] = serialized_event[i].to_be_bytes(); for j in 0..32 {{ buffer[64 + i * 32 + j] = bytes[j]; }} @@ -308,7 +308,7 @@ fn generate_fn_to_be_bytes( fn to_be_bytes(self: {event_type}) -> [u8; {byte_length_without_randomness}] {{ let mut buffer: [u8; {byte_length_without_randomness}] = [0; {byte_length_without_randomness}]; - let event_type_id_bytes = {event_type}::get_event_type_id().to_field().to_be_bytes(32); + let event_type_id_bytes: [u8; 32] = {event_type}::get_event_type_id().to_field().to_be_bytes(); for i in 0..32 {{ buffer[i] = event_type_id_bytes[i]; @@ -317,7 +317,7 @@ fn generate_fn_to_be_bytes( let serialized_event = self.serialize(); for i in 0..serialized_event.len() {{ - let bytes = serialized_event[i].to_be_bytes(32); + let bytes: [u8; 32] = serialized_event[i].to_be_bytes(); for j in 0..32 {{ buffer[32 + i * 32 + j] = bytes[j]; }} diff --git a/aztec_macros/src/transforms/note_interface.rs b/aztec_macros/src/transforms/note_interface.rs index 8df1d128c6f..df237926486 100644 --- a/aztec_macros/src/transforms/note_interface.rs +++ b/aztec_macros/src/transforms/note_interface.rs @@ -244,8 +244,8 @@ fn generate_note_to_be_bytes( let mut buffer: [u8; {0}] = [0; {0}]; - let storage_slot_bytes = storage_slot.to_be_bytes(32); - let note_type_id_bytes = {1}::get_note_type_id().to_be_bytes(32); + let storage_slot_bytes: [u8; 32] = storage_slot.to_be_bytes(); + let note_type_id_bytes: [u8; 32] = {1}::get_note_type_id().to_be_bytes(); for i in 0..32 {{ buffer[i] = storage_slot_bytes[i]; @@ -253,7 +253,7 @@ fn generate_note_to_be_bytes( }} for i in 0..serialized_note.len() {{ - let bytes = serialized_note[i].to_be_bytes(32); + let bytes: [u8; 32] = serialized_note[i].to_be_bytes(); for j in 0..32 {{ buffer[64 + i * 32 + j] = bytes[j]; }} diff --git a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs index 802d442885f..bd9190c1cfe 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs @@ -29,6 +29,7 @@ pub(crate) fn convert_black_box_call( + brillig_context: &mut BrilligContext, + original_array_or_vector: &BrilligVariable, + converted_vector: BrilligVector, + bb_func: &BlackBoxFunc, +) { + match original_array_or_vector { + BrilligVariable::BrilligArray(_) => { + brillig_context.deallocate_register(converted_vector.size); + } + BrilligVariable::BrilligVector(_) => {} + _ => unreachable!( + "ICE: {} expected an array or a vector, but got {:?}", + bb_func.name(), + original_array_or_vector + ), + } +} diff --git a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs index d3d0e2231ad..26abafe177f 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs @@ -767,6 +767,12 @@ impl<'block> BrilligBlock<'block> { // puts the returns into the returned_registers and restores saved_registers self.brillig_context .codegen_post_call_prep_returns_load_registers(&returned_registers, &saved_registers); + + // Reset the register state to the one needed to hold the current available variables + let variables = self.variables.get_available_variables(self.function_context); + let registers = + variables.into_iter().flat_map(|variable| variable.extract_registers()).collect(); + self.brillig_context.set_allocated_registers(registers); } fn validate_array_index( @@ -1751,7 +1757,7 @@ impl<'block> BrilligBlock<'block> { dfg, ); let array = variable.extract_array(); - self.allocate_nested_array(typ, Some(array)); + self.allocate_foreign_call_result_array(typ, array); variable } @@ -1778,40 +1784,39 @@ impl<'block> BrilligBlock<'block> { } } - fn allocate_nested_array( - &mut self, - typ: &Type, - array: Option, - ) -> BrilligVariable { - match typ { - Type::Array(types, size) => { - let array = array.unwrap_or(BrilligArray { - pointer: self.brillig_context.allocate_register(), - size: *size, - rc: self.brillig_context.allocate_register(), - }); - self.brillig_context.codegen_allocate_fixed_length_array(array.pointer, array.size); - self.brillig_context.usize_const_instruction(array.rc, 1_usize.into()); - - let mut index = 0_usize; - for _ in 0..*size { - for element_type in types.iter() { - match element_type { - Type::Array(_, _) => { - let inner_array = self.allocate_nested_array(element_type, None); - let idx = - self.brillig_context.make_usize_constant_instruction(index.into()); - self.brillig_context.codegen_store_variable_in_array(array.pointer, idx, inner_array); - } - Type::Slice(_) => unreachable!("ICE: unsupported slice type in allocate_nested_array(), expects an array or a numeric type"), - _ => (), - } - index += 1; + fn allocate_foreign_call_result_array(&mut self, typ: &Type, array: BrilligArray) { + let Type::Array(types, size) = typ else { + unreachable!("ICE: allocate_foreign_call_array() expects an array, got {typ:?}") + }; + + self.brillig_context.codegen_allocate_fixed_length_array(array.pointer, array.size); + self.brillig_context.usize_const_instruction(array.rc, 1_usize.into()); + + let mut index = 0_usize; + for _ in 0..*size { + for element_type in types.iter() { + match element_type { + Type::Array(_, nested_size) => { + let inner_array = BrilligArray { + pointer: self.brillig_context.allocate_register(), + rc: self.brillig_context.allocate_register(), + size: *nested_size, + }; + self.allocate_foreign_call_result_array(element_type, inner_array); + + let idx = + self.brillig_context.make_usize_constant_instruction(index.into()); + self.brillig_context.codegen_store_variable_in_array(array.pointer, idx, BrilligVariable::BrilligArray(inner_array)); + + self.brillig_context.deallocate_single_addr(idx); + self.brillig_context.deallocate_register(inner_array.pointer); + self.brillig_context.deallocate_register(inner_array.rc); } + Type::Slice(_) => unreachable!("ICE: unsupported slice type in allocate_nested_array(), expects an array or a numeric type"), + _ => (), } - BrilligVariable::BrilligArray(array) + index += 1; } - _ => unreachable!("ICE: allocate_nested_array() expects an array, got {typ:?}"), } } diff --git a/tooling/nargo_fmt/tests/expected/contract.nr b/tooling/nargo_fmt/tests/expected/contract.nr index e3a5877725a..cb7505f845c 100644 --- a/tooling/nargo_fmt/tests/expected/contract.nr +++ b/tooling/nargo_fmt/tests/expected/contract.nr @@ -1,6 +1,6 @@ -// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. -// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. -// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. +// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. // Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. contract Benchmarking { use aztec::protocol_types::abis::function_selector::FunctionSelector; @@ -63,7 +63,9 @@ contract Benchmarking { storage.balances.at(owner).write(current + value); let _callStackItem1 = context.call_public_function( context.this_address(), - FunctionSelector::from_signature("broadcast(Field)"), + comptime { + FunctionSelector::from_signature("broadcast(Field)") + }, [owner] ); } @@ -75,5 +77,5 @@ contract Benchmarking { } } -// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1 +// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1 contract Uniswap {} diff --git a/tooling/nargo_fmt/tests/input/contract.nr b/tooling/nargo_fmt/tests/input/contract.nr index e3a5877725a..cb7505f845c 100644 --- a/tooling/nargo_fmt/tests/input/contract.nr +++ b/tooling/nargo_fmt/tests/input/contract.nr @@ -1,6 +1,6 @@ -// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. -// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. -// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. +// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. // Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. contract Benchmarking { use aztec::protocol_types::abis::function_selector::FunctionSelector; @@ -63,7 +63,9 @@ contract Benchmarking { storage.balances.at(owner).write(current + value); let _callStackItem1 = context.call_public_function( context.this_address(), - FunctionSelector::from_signature("broadcast(Field)"), + comptime { + FunctionSelector::from_signature("broadcast(Field)") + }, [owner] ); } @@ -75,5 +77,5 @@ contract Benchmarking { } } -// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1 +// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1 contract Uniswap {}