From 68a5ab928e5700deabe21cd39b390025bcde4e7a Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 4 Oct 2023 15:40:23 +0000 Subject: [PATCH 1/3] feat: Actually compute selectors --- .../dev_docs/contracts/syntax/functions.md | 3 +-- yarn-project/aztec-nr/authwit/src/account.nr | 2 +- yarn-project/aztec-nr/aztec/src/lib.nr | 1 + yarn-project/aztec-nr/aztec/src/oracle.nr | 1 - .../aztec/src/oracle/compute_selector.nr | 6 ------ yarn-project/aztec-nr/aztec/src/selector.nr | 20 +++++++++++++++++++ yarn-project/aztec-nr/aztec/src/utils.nr | 2 +- .../benchmarking_contract/src/main.nr | 2 +- .../contracts/card_game_contract/src/main.nr | 2 +- .../src/contracts/child_contract/src/main.nr | 2 +- .../src/contracts/escrow_contract/src/main.nr | 2 +- .../lending_contract/src/interfaces.nr | 2 +- .../contracts/lending_contract/src/main.nr | 2 +- .../non_native_token_contract/src/main.nr | 2 +- .../src/contracts/parent_contract/src/main.nr | 2 +- .../token_bridge_contract/src/main.nr | 2 +- .../src/token_interface.nr | 2 +- .../src/contracts/token_contract/src/main.nr | 2 +- .../uniswap_contract/src/interfaces.nr | 2 +- .../contracts/uniswap_contract/src/main.nr | 3 ++- 20 files changed, 38 insertions(+), 24 deletions(-) delete mode 100644 yarn-project/aztec-nr/aztec/src/oracle/compute_selector.nr create mode 100644 yarn-project/aztec-nr/aztec/src/selector.nr diff --git a/docs/docs/dev_docs/contracts/syntax/functions.md b/docs/docs/dev_docs/contracts/syntax/functions.md index f6a9197a79f..5085d194a23 100644 --- a/docs/docs/dev_docs/contracts/syntax/functions.md +++ b/docs/docs/dev_docs/contracts/syntax/functions.md @@ -100,7 +100,7 @@ Oracles introduce **non-determinism** into a circuit, and thus are `unconstraine ### A few useful inbuilt oracles -- [`compute_selector`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/oracle/compute_selector.nr) - Computes the selector of a function. This is useful for when you want to call a function from within a circuit, but don't have an interface at hand and don't want to hardcode the selector in hex. +- [`compute_selector`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/selector.nr) - Computes the selector of a function. This is useful for when you want to call a function from within a circuit, but don't have an interface at hand and don't want to hardcode the selector in hex. - [`debug_log`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/oracle/debug_log.nr) - Provides a couple of debug functions that can be used to log information to the console. - [`auth_witness`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/oracle/auth_witness.nr) - Provides a way to fetch the authentication witness for a given address. This is useful when building account contracts to support approve-like functionality. - [`get_l1_to_l2_message`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/oracle/get_l1_to_l2_message.nr) - Useful for application that receive messages from L1 to be consumed on L2, such as token bridges or other cross-chain applications. @@ -307,7 +307,6 @@ When a [`Storage` struct](./storage.md) is declared within a contract, the `stor Any state variables declared in the `Storage` struct can now be accessed as normal struct members. - **Returning the function context to the kernel.** #include_code context-example-finish /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust diff --git a/yarn-project/aztec-nr/authwit/src/account.nr b/yarn-project/aztec-nr/authwit/src/account.nr index 7e93e89eba4..8dd7bc83793 100644 --- a/yarn-project/aztec-nr/authwit/src/account.nr +++ b/yarn-project/aztec-nr/authwit/src/account.nr @@ -2,7 +2,7 @@ mod entrypoint; mod auth; use dep::aztec::context::{PrivateContext, PublicContext, Context}; -use dep::aztec::oracle::compute_selector::compute_selector; +use dep::aztec::selector::compute_selector; use dep::aztec::state_vars::{map::Map, public_state::PublicState}; use dep::aztec::types::type_serialization::bool_serialization::{BoolSerializationMethods,BOOL_SERIALIZED_LEN}; diff --git a/yarn-project/aztec-nr/aztec/src/lib.nr b/yarn-project/aztec-nr/aztec/src/lib.nr index 18d634ee213..10330e9c1d0 100644 --- a/yarn-project/aztec-nr/aztec/src/lib.nr +++ b/yarn-project/aztec-nr/aztec/src/lib.nr @@ -9,6 +9,7 @@ mod note; mod oracle; mod private_call_stack_item; mod public_call_stack_item; +mod selector; mod state_vars; mod types; mod utils; \ No newline at end of file diff --git a/yarn-project/aztec-nr/aztec/src/oracle.nr b/yarn-project/aztec-nr/aztec/src/oracle.nr index da760abf487..f5c27c53298 100644 --- a/yarn-project/aztec-nr/aztec/src/oracle.nr +++ b/yarn-project/aztec-nr/aztec/src/oracle.nr @@ -15,4 +15,3 @@ mod public_call; mod notes; mod storage; mod logs; -mod compute_selector; \ No newline at end of file diff --git a/yarn-project/aztec-nr/aztec/src/oracle/compute_selector.nr b/yarn-project/aztec-nr/aztec/src/oracle/compute_selector.nr deleted file mode 100644 index 89a130fecd6..00000000000 --- a/yarn-project/aztec-nr/aztec/src/oracle/compute_selector.nr +++ /dev/null @@ -1,6 +0,0 @@ -#[oracle(computeSelector)] -fn compute_selector_oracle(_selector: T, _size: Field) -> Field {} - -unconstrained fn compute_selector(signature: T) -> Field { - compute_selector_oracle(signature, 0) -} \ No newline at end of file diff --git a/yarn-project/aztec-nr/aztec/src/selector.nr b/yarn-project/aztec-nr/aztec/src/selector.nr new file mode 100644 index 00000000000..b1f3324e1e9 --- /dev/null +++ b/yarn-project/aztec-nr/aztec/src/selector.nr @@ -0,0 +1,20 @@ +global SELECTOR_SIZE = 4; + +fn compute_selector(signature: str) -> Field { + let bytes = signature.as_bytes(); + let hash = dep::std::hash::keccak256(bytes, bytes.len() as u32); + let mut selector_be_bytes = [0; SELECTOR_SIZE]; + for i in 0..SELECTOR_SIZE { + selector_be_bytes[i] = hash[i]; + } + + let mut selector_as_field = 0; + let mut offset = 1; + for i in 0..SELECTOR_SIZE { + let index = SELECTOR_SIZE - i - 1; + selector_as_field += (selector_be_bytes[index] as Field) * offset; + offset *= 256; + } + + selector_as_field +} \ No newline at end of file diff --git a/yarn-project/aztec-nr/aztec/src/utils.nr b/yarn-project/aztec-nr/aztec/src/utils.nr index 83b9863a2e3..e8f6d750af3 100644 --- a/yarn-project/aztec-nr/aztec/src/utils.nr +++ b/yarn-project/aztec-nr/aztec/src/utils.nr @@ -7,4 +7,4 @@ fn arr_copy_slice( dst[i] = src[i + offset]; } dst -} \ No newline at end of file +} diff --git a/yarn-project/noir-contracts/src/contracts/benchmarking_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/benchmarking_contract/src/main.nr index 184583d1923..a66a958f992 100644 --- a/yarn-project/noir-contracts/src/contracts/benchmarking_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/benchmarking_contract/src/main.nr @@ -13,7 +13,7 @@ contract Benchmarking { use dep::aztec::{ context::{Context}, note::note_getter_options::NoteGetterOptions, - oracle::compute_selector::compute_selector, + selector::compute_selector, log::emit_unencrypted_log, state_vars::{map::Map, public_state::PublicState, set::Set}, types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN}, diff --git a/yarn-project/noir-contracts/src/contracts/card_game_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/card_game_contract/src/main.nr index de7e017d67e..8249c02294d 100644 --- a/yarn-project/noir-contracts/src/contracts/card_game_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/card_game_contract/src/main.nr @@ -30,7 +30,7 @@ contract CardGame { note_header::NoteHeader, utils as note_utils, }, - oracle::compute_selector::compute_selector + selector::compute_selector }; use crate::cards::{ diff --git a/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr index a1d592fc0ae..510e2218509 100644 --- a/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr @@ -5,7 +5,7 @@ contract Child { use dep::aztec::{ abi::CallContext, context::{PrivateContext, PublicContext, Context}, - oracle::compute_selector::compute_selector, + selector::compute_selector, log::emit_unencrypted_log, state_vars::public_state::PublicState, types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN}, diff --git a/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr index ca6a648f51b..c12ddf56bb5 100644 --- a/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr @@ -13,7 +13,7 @@ contract Escrow { utils as note_utils, }, oracle::get_public_key::get_public_key, - oracle::compute_selector::compute_selector, + selector::compute_selector, state_vars::set::Set, }; diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr index f8b7a9a41b5..b11e56a59f6 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/interfaces.nr @@ -5,7 +5,7 @@ use dep::aztec::context::{ use crate::asset::Asset; use dep::aztec::constants_gen::RETURN_VALUES_LENGTH; -use dep::aztec::oracle::compute_selector::compute_selector; +use dep::aztec::selector::compute_selector; struct PriceFeed { address: Field, diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr index 2ee30e31e35..416e6940bf4 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr @@ -15,7 +15,7 @@ contract Lending { use dep::std::option::Option; use dep::aztec::{ context::{PrivateContext, PublicContext, Context}, - oracle::compute_selector::compute_selector, + selector::compute_selector, state_vars::{ map::Map, public_state::PublicState, diff --git a/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr index 49e06ac3a91..8fd7c7212f7 100644 --- a/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/non_native_token_contract/src/main.nr @@ -39,7 +39,7 @@ contract NonNativeToken { note_header::NoteHeader, utils as note_utils, }, - oracle::compute_selector::compute_selector, + selector::compute_selector, state_vars::{map::Map, public_state::PublicState, set::Set}, types::type_serialization::field_serialization::{ FieldSerializationMethods, FIELD_SERIALIZED_LEN, diff --git a/yarn-project/noir-contracts/src/contracts/parent_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/parent_contract/src/main.nr index d1d0c377b4a..02e893ecb4a 100644 --- a/yarn-project/noir-contracts/src/contracts/parent_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/parent_contract/src/main.nr @@ -1,6 +1,6 @@ // A contract used along with `Child` contract to test nested calls. contract Parent { - use dep::aztec::oracle::compute_selector::compute_selector; + use dep::aztec::selector::compute_selector; #[aztec(private)] fn constructor() {} diff --git a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr index f64c89c37dd..7febd157a16 100644 --- a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/main.nr @@ -15,7 +15,7 @@ contract TokenBridge { FieldSerializationMethods, FIELD_SERIALIZED_LEN, }, types::address::{AztecAddress, EthereumAddress}, - oracle::compute_selector::compute_selector, + selector::compute_selector, }; use crate::token_interface::Token; diff --git a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/token_interface.nr b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/token_interface.nr index 739e7742d33..e0a17779f77 100644 --- a/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/token_interface.nr +++ b/yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/token_interface.nr @@ -1,6 +1,6 @@ use dep::aztec::{ context::{ PrivateContext, PublicContext, Context }, - oracle::compute_selector::compute_selector, + selector::compute_selector, }; struct Token { diff --git a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr index 0a0f8fd5e2e..3c6a4427d2a 100644 --- a/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr @@ -30,7 +30,7 @@ contract Token { aztec_address_serialization::{AztecAddressSerializationMethods, AZTEC_ADDRESS_SERIALIZED_LEN}, }, types::address::{AztecAddress}, - oracle::compute_selector::compute_selector, + selector::compute_selector, }; use dep::authwit::{ diff --git a/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/interfaces.nr b/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/interfaces.nr index d77c68eca83..3c86eed3bbf 100644 --- a/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/interfaces.nr +++ b/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/interfaces.nr @@ -1,6 +1,6 @@ use dep::aztec::{ context::{ PrivateContext, PublicContext, Context }, - oracle::compute_selector::compute_selector, + selector::compute_selector, types::address::AztecAddress, }; diff --git a/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr index c71fdd4b662..1a24e357d32 100644 --- a/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/uniswap_contract/src/main.nr @@ -7,7 +7,7 @@ mod util; contract Uniswap { use dep::aztec::{ context::{PrivateContext, PublicContext, Context}, - oracle::{compute_selector::compute_selector, context::get_portal_address}, + oracle::{context::get_portal_address}, state_vars::{map::Map, public_state::PublicState}, types::address::{AztecAddress, EthereumAddress}, types::type_serialization::bool_serialization::{ @@ -16,6 +16,7 @@ contract Uniswap { types::type_serialization::field_serialization::{ FieldSerializationMethods, FIELD_SERIALIZED_LEN, }, + selector::compute_selector, }; use dep::authwit::auth::{IS_VALID_SELECTOR, assert_current_call_valid_authwit_public, compute_authwit_message_hash}; From 784f5bb227fd9114c7aec20f277ecdc55353e137 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 4 Oct 2023 15:44:19 +0000 Subject: [PATCH 2/3] refactor: extract field from bytes --- yarn-project/aztec-nr/aztec/src/selector.nr | 13 ++++--------- yarn-project/aztec-nr/aztec/src/utils.nr | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/yarn-project/aztec-nr/aztec/src/selector.nr b/yarn-project/aztec-nr/aztec/src/selector.nr index b1f3324e1e9..39951a264ec 100644 --- a/yarn-project/aztec-nr/aztec/src/selector.nr +++ b/yarn-project/aztec-nr/aztec/src/selector.nr @@ -1,20 +1,15 @@ +use crate::utils::field_from_bytes; + global SELECTOR_SIZE = 4; fn compute_selector(signature: str) -> Field { let bytes = signature.as_bytes(); let hash = dep::std::hash::keccak256(bytes, bytes.len() as u32); + let mut selector_be_bytes = [0; SELECTOR_SIZE]; for i in 0..SELECTOR_SIZE { selector_be_bytes[i] = hash[i]; } - let mut selector_as_field = 0; - let mut offset = 1; - for i in 0..SELECTOR_SIZE { - let index = SELECTOR_SIZE - i - 1; - selector_as_field += (selector_be_bytes[index] as Field) * offset; - offset *= 256; - } - - selector_as_field + field_from_bytes(selector_be_bytes, true) } \ No newline at end of file diff --git a/yarn-project/aztec-nr/aztec/src/utils.nr b/yarn-project/aztec-nr/aztec/src/utils.nr index e8f6d750af3..9d32901bc2c 100644 --- a/yarn-project/aztec-nr/aztec/src/utils.nr +++ b/yarn-project/aztec-nr/aztec/src/utils.nr @@ -8,3 +8,19 @@ fn arr_copy_slice( } dst } + +fn field_from_bytes(bytes: [u8; N], big_endian: bool) -> Field { + assert(bytes.len() as u32 < 32, "field_from_bytes: N must be less than 32"); + let mut as_field = 0; + let mut offset = 1; + for i in 0..N { + let mut index = i; + if big_endian { + index = N - i - 1; + } + as_field += (bytes[index] as Field) * offset; + offset *= 256; + } + + as_field +} \ No newline at end of file From 60be7ba6d0eaebd9348a843e557a4552dac0553e Mon Sep 17 00:00:00 2001 From: sirasistant Date: Wed, 4 Oct 2023 15:46:49 +0000 Subject: [PATCH 3/3] fix: remove old oracle call --- yarn-project/acir-simulator/src/acvm/oracle/oracle.ts | 6 ------ yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts | 4 ---- 2 files changed, 10 deletions(-) diff --git a/yarn-project/acir-simulator/src/acvm/oracle/oracle.ts b/yarn-project/acir-simulator/src/acvm/oracle/oracle.ts index e7d060e706d..14c2d9736a3 100644 --- a/yarn-project/acir-simulator/src/acvm/oracle/oracle.ts +++ b/yarn-project/acir-simulator/src/acvm/oracle/oracle.ts @@ -23,12 +23,6 @@ import { TypedOracle } from './typed_oracle.js'; export class Oracle { constructor(private typedOracle: TypedOracle, private log = createDebugLogger('aztec:simulator:oracle')) {} - computeSelector(...args: ACVMField[][]): ACVMField { - const signature = oracleDebugCallToFormattedStr(args); - const selector = this.typedOracle.computeSelector(signature); - return toACVMField(selector); - } - getRandomField(): ACVMField { const val = this.typedOracle.getRandomField(); return toACVMField(val); diff --git a/yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts index 98796969e93..7ebfc75f556 100644 --- a/yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts @@ -60,10 +60,6 @@ export interface L1ToL2MessageOracleReturnData extends MessageLoadOracleInputs { * and are unavailable by default. */ export abstract class TypedOracle { - computeSelector(signature: string): Fr { - return FunctionSelector.fromSignature(signature).toField(); - } - getRandomField(): Fr { return Fr.random(); }