Skip to content

Commit

Permalink
chore: use new let syntax for generic numerics (#7503)
Browse files Browse the repository at this point in the history
Continuation of #7246. We move off from implicit numeric generics to
explicitly marking them as such via `let N: u32` (or whatever numeric
type).

This was quite painful, since the only way is to compile, find the
warnings and clear them. With these changes, the aztec-nr tests can be
compiled with no warnings, though I'm sure there's more warnings to be
found in untested parts of aztec-nr, as well as contract code. We need
to do this however since the old mechanism will eventually be removed.

There's really nothing noteworthy here except the `merkle_tree_utils`
tests, where some of the array lengths were actually being inferred to
be `u8` due to other contraints. @sirasistant you may want to take a
look at these:
https://github.com/AztecProtocol/aztec-packages/pull/7503/files#diff-e8408101fbf34ae0076db1eebe84256f8a01269dec683ef5cc99922cd04f0770
  • Loading branch information
nventuro authored Jul 17, 2024
1 parent 2a5ee4f commit 25e6684
Show file tree
Hide file tree
Showing 64 changed files with 346 additions and 298 deletions.
66 changes: 33 additions & 33 deletions noir-projects/aztec-nr/aztec/src/context/call_interfaces.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::context::{
use crate::oracle::arguments::pack_arguments;
use crate::hash::hash_args;

trait CallInterface<N, T, P, Env> {
trait CallInterface<let N: u32, T, P, Env> {
fn get_original(self) -> fn[Env](T) -> P;

fn get_args(self) -> [Field] {
Expand All @@ -35,13 +35,13 @@ trait CallInterface<N, T, P, Env> {
}
}

impl<N, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateCallInterface<N, T, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs {
self.original
}
}

struct PrivateCallInterface<N, T, Env> {
struct PrivateCallInterface<let N: u32, T, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -51,8 +51,8 @@ struct PrivateCallInterface<N, T, Env> {
is_static: bool
}

impl<N, T, Env> PrivateCallInterface<N, T, Env> {
pub fn call<M>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
impl<let N: u32, T, Env> PrivateCallInterface<N, T, Env> {
pub fn call<let M: u32>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
assert(self.args_hash == pack_arguments(self.args));
let returns = context.call_private_function_with_packed_args(
self.target_contract,
Expand All @@ -65,26 +65,26 @@ impl<N, T, Env> PrivateCallInterface<N, T, Env> {
unpacked
}

pub fn view<M>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
pub fn view<let M: u32>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
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<M>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
pub fn delegate_call<let M: u32>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
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<N, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateVoidCallInterface<N, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs {
self.original
}
}

struct PrivateVoidCallInterface<N, Env> {
struct PrivateVoidCallInterface<let N: u32, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -94,7 +94,7 @@ struct PrivateVoidCallInterface<N, Env> {
is_static: bool
}

impl<N, Env> PrivateVoidCallInterface<N, Env> {
impl<let N: u32, Env> PrivateVoidCallInterface<N, Env> {
pub fn call(self, context: &mut PrivateContext) {
assert(self.args_hash == pack_arguments(self.args));
context.call_private_function_with_packed_args(
Expand All @@ -117,13 +117,13 @@ impl<N, Env> PrivateVoidCallInterface<N, Env> {
}
}

impl<N, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateStaticCallInterface<N, T, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateStaticCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs {
self.original
}
}

struct PrivateStaticCallInterface<N, T, Env> {
struct PrivateStaticCallInterface<let N: u32, T, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -133,21 +133,21 @@ struct PrivateStaticCallInterface<N, T, Env> {
is_static: bool
}

impl<N, T, Env> PrivateStaticCallInterface<N, T, Env> {
pub fn view<M>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
impl<let N: u32, T, Env> PrivateStaticCallInterface<N, T, Env> {
pub fn view<let M: u32>(self, context: &mut PrivateContext) -> T where T: Deserialize<M> {
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<N, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateStaticVoidCallInterface<N, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PrivateContextInputs, PrivateCircuitPublicInputs, Env> for PrivateStaticVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env](PrivateContextInputs) -> PrivateCircuitPublicInputs {
self.original
}
}

struct PrivateStaticVoidCallInterface<N, Env> {
struct PrivateStaticVoidCallInterface<let N: u32, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -157,20 +157,20 @@ struct PrivateStaticVoidCallInterface<N, Env> {
is_static: bool
}

impl<N, Env> PrivateStaticVoidCallInterface<N, Env> {
impl<let N: u32, Env> PrivateStaticVoidCallInterface<N, Env> {
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<N, T, P, Env> CallInterface<N, PublicContextInputs, T, Env> for PublicCallInterface<N, T, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, T, Env> for PublicCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> T {
self.original
}
}

struct PublicCallInterface<N, T, Env> {
struct PublicCallInterface<let N: u32, T, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -180,23 +180,23 @@ struct PublicCallInterface<N, T, Env> {
is_static: bool
}

impl<N, T, Env> PublicCallInterface<N, T, Env> {
impl<let N: u32, T, Env> PublicCallInterface<N, T, Env> {
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
}

pub fn call<M>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
pub fn call<let M: u32>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.deserialize_into()
}

pub fn view<M>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
pub fn view<let M: u32>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.deserialize_into()
}

pub fn delegate_call<M>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
pub fn delegate_call<let M: u32>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
let returns = context.delegate_call_public_function(self.target_contract, self.selector, self.args);
returns.deserialize_into()
}
Expand Down Expand Up @@ -238,13 +238,13 @@ impl<N, T, Env> PublicCallInterface<N, T, Env> {
}
}

impl<N, T, P, Env> CallInterface<N, PublicContextInputs, (), Env> for PublicVoidCallInterface<N, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, (), Env> for PublicVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> () {
self.original
}
}

struct PublicVoidCallInterface<N, Env> {
struct PublicVoidCallInterface<let N: u32, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -254,7 +254,7 @@ struct PublicVoidCallInterface<N, Env> {
gas_opts: GasOpts
}

impl<N, Env> PublicVoidCallInterface<N, Env> {
impl<let N: u32, Env> PublicVoidCallInterface<N, Env> {
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
Expand Down Expand Up @@ -312,13 +312,13 @@ impl<N, Env> PublicVoidCallInterface<N, Env> {
}
}

impl<N, T, P, Env> CallInterface<N, PublicContextInputs, T, Env> for PublicStaticCallInterface<N, T, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, T, Env> for PublicStaticCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> T {
self.original
}
}

struct PublicStaticCallInterface<N, T, Env> {
struct PublicStaticCallInterface<let N: u32, T, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -328,13 +328,13 @@ struct PublicStaticCallInterface<N, T, Env> {
gas_opts: GasOpts
}

impl<N, T, Env> PublicStaticCallInterface<N, T, Env> {
impl<let N: u32, T, Env> PublicStaticCallInterface<N, T, Env> {
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
}

pub fn view<M>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
pub fn view<let M: u32>(self, context: &mut PublicContext) -> T where T: Deserialize<M> {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
let unpacked: T = returns.deserialize_into();
unpacked
Expand All @@ -353,13 +353,13 @@ impl<N, T, Env> PublicStaticCallInterface<N, T, Env> {
}
}

impl<N, T, P, Env> CallInterface<N, PublicContextInputs, (), Env> for PublicStaticVoidCallInterface<N, Env> {
impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, (), Env> for PublicStaticVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> () {
self.original
}
}

struct PublicStaticVoidCallInterface<N, Env> {
struct PublicStaticVoidCallInterface<let N: u32, Env> {
target_contract: AztecAddress,
selector: FunctionSelector,
name: str<N>,
Expand All @@ -369,7 +369,7 @@ struct PublicStaticVoidCallInterface<N, Env> {
gas_opts: GasOpts
}

impl<N, Env> PublicStaticVoidCallInterface<N, Env> {
impl<let N: u32, Env> PublicStaticVoidCallInterface<N, Env> {
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/packed_returns.nr
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ impl PackedReturns {
self.packed_returns
}

pub fn unpack<N>(self) -> [Field; N] {
pub fn unpack<let N: u32>(self) -> [Field; N] {
let unpacked: [Field; N] = unpack_returns(self.packed_returns);
assert_eq(self.packed_returns, hash_args_array(unpacked));
unpacked
}

pub fn unpack_into<T, N>(self) -> T where T: Deserialize<N> {
pub fn unpack_into<T, let N: u32>(self) -> T where T: Deserialize<N> {
let unpacked: [Field; N] = self.unpack();
Deserialize::deserialize(unpacked)
}
Expand Down
27 changes: 16 additions & 11 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ impl PrivateContext {

// NB: A randomness value of 0 signals that the kernels should not mask the contract address
// used in siloing later on e.g. 'handshaking' contract w/ known address.
pub fn emit_raw_event_log_with_masked_address<M>(&mut self, randomness: Field, log: [u8; M], log_hash: Field) {
pub fn emit_raw_event_log_with_masked_address<let M: u32>(
&mut self,
randomness: Field,
log: [u8; M],
log_hash: Field
) {
let counter = self.next_counter();
let contract_address = self.this_address();
let len = log.len() as Field + 4;
Expand All @@ -272,7 +277,7 @@ impl PrivateContext {
emit_encrypted_event_log(contract_address, randomness, log, counter);
}

pub fn emit_raw_note_log<M>(&mut self, note_hash_counter: u32, log: [u8; M], log_hash: Field) {
pub fn emit_raw_note_log<let M: u32>(&mut self, note_hash_counter: u32, log: [u8; M], log_hash: Field) {
let counter = self.next_counter();
let len = log.len() as Field + 4;
let side_effect = NoteLogHash { value: log_hash, counter, length: len, note_hash_counter };
Expand All @@ -281,7 +286,7 @@ impl PrivateContext {
emit_encrypted_note_log(note_hash_counter, log, counter);
}

pub fn call_private_function<ARGS_COUNT>(
pub fn call_private_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand All @@ -292,7 +297,7 @@ impl PrivateContext {
self.call_private_function_with_packed_args(contract_address, function_selector, args_hash, false, false)
}

pub fn static_call_private_function<ARGS_COUNT>(
pub fn static_call_private_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand All @@ -303,7 +308,7 @@ impl PrivateContext {
self.call_private_function_with_packed_args(contract_address, function_selector, args_hash, true, false)
}

pub fn delegate_call_private_function<ARGS_COUNT>(
pub fn delegate_call_private_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand All @@ -330,7 +335,7 @@ impl PrivateContext {
self.call_private_function_with_packed_args(contract_address, function_selector, 0, true, false)
}

pub fn delegate_call_private_function_no_args<ARGS_COUNT>(
pub fn delegate_call_private_function_no_args<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector
Expand Down Expand Up @@ -419,7 +424,7 @@ impl PrivateContext {
PackedReturns::new(item.public_inputs.returns_hash)
}

pub fn call_public_function<ARGS_COUNT>(
pub fn call_public_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand All @@ -430,7 +435,7 @@ impl PrivateContext {
self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, false, false)
}

pub fn static_call_public_function<ARGS_COUNT>(
pub fn static_call_public_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand All @@ -441,7 +446,7 @@ impl PrivateContext {
self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, true, false)
}

pub fn delegate_call_public_function<ARGS_COUNT>(
pub fn delegate_call_public_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand Down Expand Up @@ -508,7 +513,7 @@ impl PrivateContext {
self.public_call_stack_hashes.push(item.get_compressed().hash());
}

pub fn set_public_teardown_function<ARGS_COUNT>(
pub fn set_public_teardown_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand All @@ -519,7 +524,7 @@ impl PrivateContext {
self.set_public_teardown_function_with_packed_args(contract_address, function_selector, args_hash, false, false)
}

pub fn set_public_teardown_function_with_packed_args<ARGS_COUNT>(
pub fn set_public_teardown_function_with_packed_args<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand Down
Loading

0 comments on commit 25e6684

Please sign in to comment.