diff --git a/noir-projects/aztec-nr/aztec/src/context/call_interfaces.nr b/noir-projects/aztec-nr/aztec/src/context/call_interfaces.nr index 35151d1427d..39dae878652 100644 --- a/noir-projects/aztec-nr/aztec/src/context/call_interfaces.nr +++ b/noir-projects/aztec-nr/aztec/src/context/call_interfaces.nr @@ -8,26 +8,16 @@ use crate::context::{ public_context::FunctionReturns, inputs::{PrivateContextInputs, PublicContextInputs} }; -use crate::oracle::arguments; +use crate::oracle::arguments::pack_arguments; +use crate::hash::hash_args; trait CallInterface { - fn get_args(self) -> [Field]; fn get_original(self) -> fn[Env](T) -> P; - fn get_selector(self) -> FunctionSelector; - fn get_name(self) -> str; - fn get_contract_address(self) -> AztecAddress; - fn get_is_static(self) -> bool; -} -impl CallInterface for PrivateCallInterface { - fn get_args(self) -> [Field] { + fn get_args(self) -> [Field] { self.args } - fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs { - self.original - } - fn get_selector(self) -> FunctionSelector { self.selector } @@ -45,6 +35,12 @@ impl CallInterface CallInterface for PrivateCallInterface { + fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs { + self.original + } +} + struct PrivateCallInterface { target_contract: AztecAddress, selector: FunctionSelector, @@ -57,6 +53,7 @@ struct PrivateCallInterface { impl PrivateCallInterface { pub fn call(self, context: &mut PrivateContext) -> T where T: Deserialize { + assert(self.args_hash == pack_arguments(self.args)); let returns = context.call_private_function_with_packed_args( self.target_contract, self.selector, @@ -69,40 +66,22 @@ impl PrivateCallInterface { } pub fn view(self, context: &mut PrivateContext) -> T where T: Deserialize { + assert(self.args_hash == pack_arguments(self.args)); let returns = context.call_private_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false); returns.unpack_into() } pub fn delegate_call(self, context: &mut PrivateContext) -> T where T: Deserialize { + assert(self.args_hash == pack_arguments(self.args)); let returns = context.call_private_function_with_packed_args(self.target_contract, self.selector, self.args_hash, false, true); returns.unpack_into() } } impl CallInterface for PrivateVoidCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PrivateVoidCallInterface { @@ -117,6 +96,7 @@ struct PrivateVoidCallInterface { impl PrivateVoidCallInterface { pub fn call(self, context: &mut PrivateContext) { + assert(self.args_hash == pack_arguments(self.args)); context.call_private_function_with_packed_args( self.target_contract, self.selector, @@ -127,38 +107,20 @@ impl PrivateVoidCallInterface { } pub fn view(self, context: &mut PrivateContext) { + assert(self.args_hash == pack_arguments(self.args)); context.call_private_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false).assert_empty(); } pub fn delegate_call(self, context: &mut PrivateContext) { + assert(self.args_hash == pack_arguments(self.args)); context.call_private_function_with_packed_args(self.target_contract, self.selector, self.args_hash, false, true).assert_empty(); } } impl CallInterface for PrivateStaticCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PrivateStaticCallInterface { @@ -173,35 +135,16 @@ struct PrivateStaticCallInterface { impl PrivateStaticCallInterface { pub fn view(self, context: &mut PrivateContext) -> T where T: Deserialize { + assert(self.args_hash == pack_arguments(self.args)); let returns = context.call_private_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false); returns.unpack_into() } } impl CallInterface for PrivateStaticVoidCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PrivateStaticVoidCallInterface { @@ -216,34 +159,15 @@ struct PrivateStaticVoidCallInterface { impl PrivateStaticVoidCallInterface { pub fn view(self, context: &mut PrivateContext) { + assert(self.args_hash == pack_arguments(self.args)); context.call_private_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false).assert_empty(); } } impl CallInterface for PublicCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PublicContextInputs) -> T { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PublicCallInterface { @@ -278,8 +202,8 @@ impl PublicCallInterface { } pub fn enqueue(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -290,8 +214,8 @@ impl PublicCallInterface { } pub fn enqueue_view(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -302,8 +226,8 @@ impl PublicCallInterface { } pub fn delegate_enqueue(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -315,29 +239,9 @@ impl PublicCallInterface { } impl CallInterface for PublicVoidCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PublicContextInputs) -> () { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PublicVoidCallInterface { @@ -345,9 +249,9 @@ struct PublicVoidCallInterface { selector: FunctionSelector, name: str, args: [Field], - gas_opts: GasOpts, original: fn[Env](PublicContextInputs) -> (), - is_static: bool + is_static: bool, + gas_opts: GasOpts } impl PublicVoidCallInterface { @@ -372,8 +276,8 @@ impl PublicVoidCallInterface { } pub fn enqueue(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -384,8 +288,8 @@ impl PublicVoidCallInterface { } pub fn enqueue_view(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -396,8 +300,8 @@ impl PublicVoidCallInterface { } pub fn delegate_enqueue(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -409,29 +313,9 @@ impl PublicVoidCallInterface { } impl CallInterface for PublicStaticCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PublicContextInputs) -> T { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PublicStaticCallInterface { @@ -439,9 +323,9 @@ struct PublicStaticCallInterface { selector: FunctionSelector, name: str, args: [Field], - gas_opts: GasOpts, original: fn[Env](PublicContextInputs) -> T, - is_static: bool + is_static: bool, + gas_opts: GasOpts } impl PublicStaticCallInterface { @@ -457,8 +341,8 @@ impl PublicStaticCallInterface { } pub fn enqueue_view(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, @@ -470,29 +354,9 @@ impl PublicStaticCallInterface { } impl CallInterface for PublicStaticVoidCallInterface { - fn get_args(self) -> [Field] { - self.args - } - fn get_original(self) -> fn[Env](PublicContextInputs) -> () { self.original } - - fn get_selector(self) -> FunctionSelector { - self.selector - } - - fn get_name(self) -> str { - self.name - } - - fn get_contract_address(self) -> AztecAddress { - self.target_contract - } - - fn get_is_static(self) -> bool { - self.is_static - } } struct PublicStaticVoidCallInterface { @@ -500,9 +364,9 @@ struct PublicStaticVoidCallInterface { selector: FunctionSelector, name: str, args: [Field], - gas_opts: GasOpts, original: fn[Env](PublicContextInputs) -> (), - is_static: bool + is_static: bool, + gas_opts: GasOpts } impl PublicStaticVoidCallInterface { @@ -517,8 +381,8 @@ impl PublicStaticVoidCallInterface { } pub fn enqueue_view(self, context: &mut PrivateContext) { - // This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args. - let args_hash = arguments::pack_arguments(self.args); + let args_hash = hash_args(self.args); + assert(args_hash == pack_arguments(self.args)); context.call_public_function_with_packed_args( self.target_contract, self.selector, diff --git a/noir/noir-repo/aztec_macros/src/transforms/contract_interface.rs b/noir/noir-repo/aztec_macros/src/transforms/contract_interface.rs index b9323644379..e79cee66407 100644 --- a/noir/noir-repo/aztec_macros/src/transforms/contract_interface.rs +++ b/noir/noir-repo/aztec_macros/src/transforms/contract_interface.rs @@ -35,7 +35,15 @@ use crate::utils::{ // PublicCallInterface { // target_contract: self.target_contract, // selector: FunctionSelector::from_signature("SELECTOR_PLACEHOLDER"), -// args_hash +// args_hash, +// name: "a_function", +// args_hash, +// args: args_acc, +// original: | inputs: dep::aztec::context::inputs::PublicContextInputs | -> Field { +// a_function(inputs, first_arg, second_arg, third_arg) +// }, +// is_static: false, +// gas_opts: dep::aztec::context::gas::GasOpts::default() // } // } // @@ -132,73 +140,48 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call format!("{}, {}>", return_type_hint, arg_types) }; - let fn_body = if aztec_visibility != "Public" { - let args_hash = if !parameters.is_empty() { - format!( - "let mut args_acc: [Field] = &[]; - {} - let args_hash = aztec::hash::hash_args(args_acc); - assert(args_hash == aztec::oracle::arguments::pack_arguments(args_acc));", - call_args - ) + let args = format!( + "let mut args_acc: [Field] = &[]; + {} + {}", + call_args, + if aztec_visibility == "Private" { + "let args_hash = aztec::hash::hash_args(args_acc);" } else { - " - let mut args_acc: [Field] = &[]; - let args_hash = 0; - " - .to_string() - }; - - format!( - "{} - let selector = {}; - dep::aztec::context::{}{}{}CallInterface {{ - target_contract: self.target_contract, - selector, - name: \"{}\", - args_hash, - args: args_acc, - original: {}, - is_static: {} - }}", - args_hash, - fn_selector, - aztec_visibility, - is_static, - is_void, - fn_name, - original, - is_static_call - ) + "" + } + ); + + let gas_opts = if aztec_visibility == "Public" { + "gas_opts: dep::aztec::context::gas::GasOpts::default()" } else { - let args = format!( - "let mut args_acc: [Field] = &[]; - {} - ", - call_args - ); - format!( - "{} + "" + }; + + let fn_body = format!( + "{} let selector = {}; dep::aztec::context::{}{}{}CallInterface {{ target_contract: self.target_contract, selector, name: \"{}\", + {} args: args_acc, - gas_opts: dep::aztec::context::gas::GasOpts::default(), original: {}, - is_static: {} + is_static: {}, + {} }}", - args, - fn_selector, - aztec_visibility, - is_static, - is_void, - fn_name, - original, - is_static_call - ) - }; + args, + fn_selector, + aztec_visibility, + is_static, + is_void, + fn_name, + if aztec_visibility == "Private" { "args_hash," } else { "" }, + original, + is_static_call, + gas_opts + ); format!( "pub fn {}(self, {}) -> dep::aztec::context::{}{}{}CallInterface<{},{} {{