Skip to content

Commit

Permalink
feat: Actually compute selectors (#2686)
Browse files Browse the repository at this point in the history
Please provide a paragraph or two giving a summary of the change,
including relevant motivation and context.

# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [ ] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [ ] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [ ] Every change is related to the PR description.
- [ ] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
sirasistant authored Oct 5, 2023
1 parent b63663e commit dcb65e1
Show file tree
Hide file tree
Showing 22 changed files with 48 additions and 33 deletions.
3 changes: 1 addition & 2 deletions docs/docs/dev_docs/contracts/syntax/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions yarn-project/acir-simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/authwit/src/account.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
1 change: 1 addition & 0 deletions yarn-project/aztec-nr/aztec/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 0 additions & 1 deletion yarn-project/aztec-nr/aztec/src/oracle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ mod public_call;
mod notes;
mod storage;
mod logs;
mod compute_selector;
6 changes: 0 additions & 6 deletions yarn-project/aztec-nr/aztec/src/oracle/compute_selector.nr

This file was deleted.

15 changes: 15 additions & 0 deletions yarn-project/aztec-nr/aztec/src/selector.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::utils::field_from_bytes;

global SELECTOR_SIZE = 4;

fn compute_selector<N>(signature: str<N>) -> 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];
}

field_from_bytes(selector_be_bytes, true)
}
16 changes: 16 additions & 0 deletions yarn-project/aztec-nr/aztec/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,20 @@ fn arr_copy_slice<T, N, M>(
dst[i] = src[i + offset];
}
dst
}

fn field_from_bytes<N>(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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract CardGame {
note_header::NoteHeader,
utils as note_utils,
},
oracle::compute_selector::compute_selector
selector::compute_selector
};

use crate::cards::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::{
context::{ PrivateContext, PublicContext, Context },
oracle::compute_selector::compute_selector,
selector::compute_selector,
};

struct Token {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::aztec::{
context::{ PrivateContext, PublicContext, Context },
oracle::compute_selector::compute_selector,
selector::compute_selector,
types::address::AztecAddress,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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};
Expand Down

0 comments on commit dcb65e1

Please sign in to comment.