Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 1 addition & 78 deletions noir-projects/aztec-nr/aztec/src/macros/dispatch.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::compute_fn_selector;
use super::utils::{compute_fn_selector, size_in_fields};
use poseidon::poseidon2::Poseidon2Hasher;
use std::{collections::umap::UHashMap, hash::BuildHasherDefault, panic};

Expand Down Expand Up @@ -119,83 +119,6 @@ pub comptime fn generate_public_dispatch(m: Module) -> Quoted {
}
}

comptime fn size_in_fields(typ: Type) -> u32 {
let size = array_size_in_fields(typ);
let size = size.or_else(|| bool_size_in_fields(typ));
let size = size.or_else(|| constant_size_in_fields(typ));
let size = size.or_else(|| field_size_in_fields(typ));
let size = size.or_else(|| int_size_in_fields(typ));
let size = size.or_else(|| str_size_in_fields(typ));
let size = size.or_else(|| struct_size_in_fields(typ));
let size = size.or_else(|| tuple_size_in_fields(typ));
if size.is_some() {
size.unwrap()
} else {
panic(f"Can't determine size in fields of {typ}")
}
}

comptime fn array_size_in_fields(typ: Type) -> Option<u32> {
typ.as_array().and_then(|typ: (Type, Type)| {
let (typ, element_size) = typ;
element_size.as_constant().map(|x: u32| x * size_in_fields(typ))
})
}

comptime fn bool_size_in_fields(typ: Type) -> Option<u32> {
if typ.is_bool() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn field_size_in_fields(typ: Type) -> Option<u32> {
if typ.is_field() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn int_size_in_fields(typ: Type) -> Option<u32> {
if typ.as_integer().is_some() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn constant_size_in_fields(typ: Type) -> Option<u32> {
typ.as_constant()
}

comptime fn str_size_in_fields(typ: Type) -> Option<u32> {
typ.as_str().map(|typ| size_in_fields(typ))
}

comptime fn struct_size_in_fields(typ: Type) -> Option<u32> {
typ.as_data_type().map(|typ: (TypeDefinition, [Type])| {
let struct_type = typ.0;
let generics = typ.1;
let mut size = 0;
for field in struct_type.fields(generics) {
size += size_in_fields(field.1);
}
size
})
}

comptime fn tuple_size_in_fields(typ: Type) -> Option<u32> {
typ.as_tuple().map(|types: [Type]| {
let mut size = 0;
for typ in types {
size += size_in_fields(typ);
}
size
})
}

comptime fn get_type<T>() -> Type {
let t: T = std::mem::zeroed();
std::meta::type_of(t)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::macros::utils::{
add_to_field_slice, AsStrQuote, compute_fn_selector, is_fn_private, is_fn_public, is_fn_view,
size_in_fields,
};
use std::meta::{type_of, unquote};

Expand Down Expand Up @@ -100,9 +101,10 @@ comptime fn create_private_stub(f: FunctionDefinition) -> Quoted {
let (fn_name, fn_parameters_list, serialized_args_slice_construction, fn_name_str, fn_name_len, fn_selector) =
create_stub_base(f);
let fn_return_type = f.return_type();
let size = size_in_fields(fn_return_type);

quote {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PrivateCallInterface<$fn_name_len, $fn_return_type, _> {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PrivateCallInterface<$fn_name_len, $fn_return_type, $size> {
$serialized_args_slice_construction
let selector = $FROM_FIELD($fn_selector);
dep::aztec::context::call_interfaces::PrivateCallInterface::new(
Expand All @@ -120,9 +122,10 @@ comptime fn create_private_static_stub(f: FunctionDefinition) -> Quoted {
let (fn_name, fn_parameters_list, serialized_args_slice_construction, fn_name_str, fn_name_len, fn_selector) =
create_stub_base(f);
let fn_return_type = f.return_type();
let size = size_in_fields(fn_return_type);

quote {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PrivateStaticCallInterface<$fn_name_len, $fn_return_type, _> {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PrivateStaticCallInterface<$fn_name_len, $fn_return_type, $size> {
$serialized_args_slice_construction
let selector = $FROM_FIELD($fn_selector);
dep::aztec::context::call_interfaces::PrivateStaticCallInterface::new(
Expand Down Expand Up @@ -176,9 +179,10 @@ comptime fn create_public_stub(f: FunctionDefinition) -> Quoted {
let (fn_name, fn_parameters_list, serialized_args_slice_construction, fn_name_str, fn_name_len, fn_selector) =
create_stub_base(f);
let fn_return_type = f.return_type();
let size = size_in_fields(fn_return_type);

quote {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PublicCallInterface<$fn_name_len, $fn_return_type, _> {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PublicCallInterface<$fn_name_len, $fn_return_type, $size> {
$serialized_args_slice_construction
let selector = $FROM_FIELD($fn_selector);
dep::aztec::context::call_interfaces::PublicCallInterface::new(
Expand All @@ -196,9 +200,10 @@ comptime fn create_public_static_stub(f: FunctionDefinition) -> Quoted {
let (fn_name, fn_parameters_list, serialized_args_slice_construction, fn_name_str, fn_name_len, fn_selector) =
create_stub_base(f);
let fn_return_type = f.return_type();
let size = size_in_fields(fn_return_type);

quote {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PublicStaticCallInterface<$fn_name_len, $fn_return_type, _> {
pub fn $fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::PublicStaticCallInterface<$fn_name_len, $fn_return_type, $size> {
$serialized_args_slice_construction
let selector = $FROM_FIELD($fn_selector);
dep::aztec::context::call_interfaces::PublicStaticCallInterface::new(
Expand Down Expand Up @@ -252,12 +257,13 @@ comptime fn create_utility_stub(f: FunctionDefinition) -> Quoted {
let (fn_name, fn_parameters_list, serialized_args_slice_construction, fn_name_str, fn_name_len, fn_selector) =
create_stub_base(f);
let fn_return_type = f.return_type();
let size = size_in_fields(fn_return_type);

// This is here because utility function call interfaces can only be used within TXe tests.
let modified_fn_name = f"_experimental_{fn_name}".quoted_contents();

quote {
pub fn $modified_fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::UtilityCallInterface<$fn_name_len, $fn_return_type, _> {
pub fn $modified_fn_name(self, $fn_parameters_list) -> dep::aztec::context::call_interfaces::UtilityCallInterface<$fn_name_len, $fn_return_type, $size> {
$serialized_args_slice_construction
let selector = $FROM_FIELD($fn_selector);
dep::aztec::context::call_interfaces::UtilityCallInterface::new(
Expand Down
73 changes: 73 additions & 0 deletions noir-projects/aztec-nr/aztec/src/macros/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,76 @@ pub(crate) comptime fn get_trait_impl_method(
.filter(|m| m.name() == target_method)[0]
.as_typed_expr()
}

pub comptime fn size_in_fields(typ: Type) -> u32 {
array_size_in_fields(typ)
.or_else(|| bool_size_in_fields(typ))
.or_else(|| constant_size_in_fields(typ))
.or_else(|| field_size_in_fields(typ))
.or_else(|| int_size_in_fields(typ))
.or_else(|| str_size_in_fields(typ))
.or_else(|| struct_size_in_fields(typ))
.or_else(|| tuple_size_in_fields(typ))
.expect(f"Can't determine size in fields of {typ}")
}

comptime fn array_size_in_fields(typ: Type) -> Option<u32> {
typ.as_array().and_then(|typ: (Type, Type)| {
let (typ, element_size) = typ;
element_size.as_constant().map(|x: u32| x * size_in_fields(typ))
})
}

comptime fn bool_size_in_fields(typ: Type) -> Option<u32> {
if typ.is_bool() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn field_size_in_fields(typ: Type) -> Option<u32> {
if typ.is_field() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn int_size_in_fields(typ: Type) -> Option<u32> {
if typ.as_integer().is_some() {
Option::some(1)
} else {
Option::none()
}
}

comptime fn constant_size_in_fields(typ: Type) -> Option<u32> {
typ.as_constant()
}

comptime fn str_size_in_fields(typ: Type) -> Option<u32> {
typ.as_str().map(|typ| size_in_fields(typ))
}

comptime fn struct_size_in_fields(typ: Type) -> Option<u32> {
typ.as_data_type().map(|typ: (TypeDefinition, [Type])| {
let struct_type = typ.0;
let generics = typ.1;
let mut size = 0;
for field in struct_type.fields(generics) {
size += size_in_fields(field.1);
}
size
})
}

comptime fn tuple_size_in_fields(typ: Type) -> Option<u32> {
typ.as_tuple().map(|types: [Type]| {
let mut size = 0;
for typ in types {
size += size_in_fields(typ);
}
size
})
}
Loading