@aztec/noir-contracts.js 0.87.4 → 0.87.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -10056,7 +10056,7 @@
|
|
|
10056
10056
|
"file_map": {
|
|
10057
10057
|
"101": {
|
|
10058
10058
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/macros/utils.nr",
|
|
10059
|
-
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
10059
|
+
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_tuple().is_some() {\n let tuple_len = typ.as_tuple().unwrap().len();\n let mut tuple_quotes: [Quoted] = [];\n for i in 0..tuple_len {\n let element_quote = quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name.$i)); };\n tuple_quotes = tuple_quotes.push_back(element_quote);\n }\n tuple_quotes.join(quote {})\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
10060
10060
|
},
|
|
10061
10061
|
"102": {
|
|
10062
10062
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/messages/discovery/mod.nr",
|
|
@@ -6634,7 +6634,7 @@
|
|
|
6634
6634
|
},
|
|
6635
6635
|
"110": {
|
|
6636
6636
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/macros/utils.nr",
|
|
6637
|
-
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
6637
|
+
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_tuple().is_some() {\n let tuple_len = typ.as_tuple().unwrap().len();\n let mut tuple_quotes: [Quoted] = [];\n for i in 0..tuple_len {\n let element_quote = quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name.$i)); };\n tuple_quotes = tuple_quotes.push_back(element_quote);\n }\n tuple_quotes.join(quote {})\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
6638
6638
|
},
|
|
6639
6639
|
"111": {
|
|
6640
6640
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/messages/discovery/mod.nr",
|
|
@@ -11765,7 +11765,7 @@
|
|
|
11765
11765
|
},
|
|
11766
11766
|
"103": {
|
|
11767
11767
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/macros/utils.nr",
|
|
11768
|
-
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
11768
|
+
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_tuple().is_some() {\n let tuple_len = typ.as_tuple().unwrap().len();\n let mut tuple_quotes: [Quoted] = [];\n for i in 0..tuple_len {\n let element_quote = quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name.$i)); };\n tuple_quotes = tuple_quotes.push_back(element_quote);\n }\n tuple_quotes.join(quote {})\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
11769
11769
|
},
|
|
11770
11770
|
"104": {
|
|
11771
11771
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/messages/discovery/mod.nr",
|
|
@@ -6863,7 +6863,7 @@
|
|
|
6863
6863
|
"file_map": {
|
|
6864
6864
|
"100": {
|
|
6865
6865
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/macros/utils.nr",
|
|
6866
|
-
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
6866
|
+
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_tuple().is_some() {\n let tuple_len = typ.as_tuple().unwrap().len();\n let mut tuple_quotes: [Quoted] = [];\n for i in 0..tuple_len {\n let element_quote = quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name.$i)); };\n tuple_quotes = tuple_quotes.push_back(element_quote);\n }\n tuple_quotes.join(quote {})\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
6867
6867
|
},
|
|
6868
6868
|
"101": {
|
|
6869
6869
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/messages/discovery/mod.nr",
|
|
@@ -3381,7 +3381,7 @@
|
|
|
3381
3381
|
},
|
|
3382
3382
|
"109": {
|
|
3383
3383
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/macros/utils.nr",
|
|
3384
|
-
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
3384
|
+
"source": "use std::meta::unquote;\n\npub(crate) comptime fn get_fn_visibility(f: FunctionDefinition) -> Quoted {\n if f.has_named_attribute(\"private\") {\n quote { private }\n } else if f.has_named_attribute(\"public\") {\n quote { public }\n } else {\n panic(f\"Function is neither private nor public\")\n }\n}\n\npub(crate) comptime fn is_fn_private(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"private\")\n}\n\npub(crate) comptime fn is_fn_public(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"public\")\n}\n\npub(crate) comptime fn is_fn_utility(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"utility\")\n}\n\npub(crate) comptime fn is_fn_contract_library_method(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"contract_library_method\")\n}\n\npub(crate) comptime fn is_fn_test(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"test\")\n}\n\npub(crate) comptime fn is_fn_view(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"view\")\n}\n\npub(crate) comptime fn is_fn_internal(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"internal\")\n}\n\npub(crate) comptime fn is_fn_initializer(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"initializer\")\n}\n\npub(crate) comptime fn fn_has_noinitcheck(f: FunctionDefinition) -> bool {\n f.has_named_attribute(\"noinitcheck\")\n}\n\n/// Takes a function body as a collection of expressions, and alters it by prepending and appending quoted values.\npub(crate) comptime fn modify_fn_body(body: [Expr], prepend: Quoted, append: Quoted) -> Expr {\n // We need to quote the body before we can alter its contents, so we fold it by quoting each expression.\n let mut body_quote = body.fold(quote {}, |full_quote: Quoted, expr: Expr| {\n let expr_quote = expr.quoted();\n quote {\n $full_quote\n $expr_quote\n }\n });\n body_quote = quote {\n {\n $prepend\n $body_quote\n $append\n }\n };\n let body_expr = body_quote.as_expr();\n body_expr.expect(f\"Body is not an expression: {body_quote}\")\n}\n\npub(crate) comptime fn add_to_field_slice(slice_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $slice_name = $slice_name.push_back($name as Field); }\n } else if typ.as_data_type().is_some() {\n // We invoke serialize as a static trait function rather than calling $name.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote { $slice_name = $slice_name.append(aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n // We invoke serialize as a static trait function rather than calling x.serialize() directly in the quote\n // to avoid \"trait not in scope\" compiler warnings.\n quote {\n let $serialized_name = $name.map(|x: $element_type | aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $slice_name = $slice_name.append($serialized_name[i].as_slice());\n }\n }\n } else if typ.as_str().is_some() {\n quote {\n $slice_name = $slice_name.append($name.as_bytes().map(| byte: u8 | byte as Field).as_slice());\n }\n } else {\n panic(\n f\"Cannot add to slice: unsupported type {typ} variable {name}\",\n )\n }\n}\n\n/// Adds a value to a hash::ArgsHasher. Structs and values inside arrays are required to implement the Serialize trait.\npub(crate) comptime fn add_to_hasher(hasher_name: Quoted, name: Quoted, typ: Type) -> Quoted {\n if typ.is_field() | typ.as_integer().is_some() | typ.is_bool() {\n quote { $hasher_name.add($name as Field); }\n } else if typ.as_data_type().is_some() {\n quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name)); }\n } else if typ.as_array().is_some() {\n let (element_type, _) = typ.as_array().unwrap();\n let serialized_name = f\"{name}_serialized\".quoted_contents();\n quote {\n let $serialized_name = $name.map(|x: $element_type | dep::aztec::protocol_types::traits::Serialize::serialize(x));\n for i in 0..$name.len() {\n $hasher_name.add_multiple($serialized_name[i]);\n }\n }\n } else if typ.as_tuple().is_some() {\n let tuple_len = typ.as_tuple().unwrap().len();\n let mut tuple_quotes: [Quoted] = [];\n for i in 0..tuple_len {\n let element_quote = quote { $hasher_name.add_multiple(dep::aztec::protocol_types::traits::Serialize::serialize($name.$i)); };\n tuple_quotes = tuple_quotes.push_back(element_quote);\n }\n tuple_quotes.join(quote {})\n } else if typ.as_str().is_some() {\n quote {\n $hasher_name.add_multiple($name.as_bytes().map(| byte: u8 | byte as Field));\n }\n } else {\n panic(\n f\"Cannot add to hasher: unsupported type {typ} of variable {name}\",\n )\n }\n}\n\ncomptime fn signature_of_type(typ: Type) -> Quoted {\n if typ.is_field() {\n quote {Field}\n } else if typ.as_integer().is_some() {\n let (is_signed, bit_size) = typ.as_integer().unwrap();\n if is_signed {\n f\"i{bit_size}\".quoted_contents()\n } else {\n f\"u{bit_size}\".quoted_contents()\n }\n } else if typ.is_bool() {\n quote {bool}\n } else if typ.as_str().is_some() {\n let str_len_typ = typ.as_str().unwrap();\n let str_len = str_len_typ.as_constant().unwrap();\n f\"str<{str_len}>\".quoted_contents()\n } else if typ.as_array().is_some() {\n let (element_type, array_len) = typ.as_array().unwrap();\n let array_len = array_len.as_constant().unwrap();\n let element_typ_quote = signature_of_type(element_type);\n f\"[{element_typ_quote};{array_len}]\".quoted_contents()\n } else if typ.as_data_type().is_some() {\n let (s, generics) = typ.as_data_type().unwrap();\n let field_signatures = s\n .fields(generics)\n .map(|(_, typ): (Quoted, Type)| signature_of_type(typ))\n .join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else if typ.as_tuple().is_some() {\n // Note that tuples are handled the same way as structs\n let types = typ.as_tuple().unwrap();\n let field_signatures = types.map(|typ: Type| signature_of_type(typ)).join(quote {,});\n f\"({field_signatures})\".quoted_contents()\n } else {\n panic(f\"Unsupported type {typ}\")\n }\n}\n\npub(crate) trait AsStrQuote {\n fn as_str_quote(self) -> (Self, u32);\n}\n\nimpl<let N: u32, Env> AsStrQuote for Quoted {\n // Used to convert an arbitrary quoted type into a quoted string, removing whitespace between tokens\n comptime fn as_str_quote(self) -> (Quoted, u32) {\n let tokens = self.tokens();\n let mut acc: [u8] = &[];\n let mut total_len: u32 = 0;\n for token in tokens {\n let token_as_fmt_str = f\"{token}\";\n let token_as_str = unquote!(quote {$token_as_fmt_str});\n let token_len = unquote!(quote { $token_as_str.as_bytes().len() });\n let token_as_bytes = unquote!(quote { $token_as_str.as_bytes().as_slice() });\n total_len += token_len;\n acc = acc.append(token_as_bytes);\n }\n let result = unquote!(\n quote {\n let signature_as_array: [u8; $total_len] = $acc.as_array();\n signature_as_array.as_str_unchecked()\n },\n );\n (quote { $result }, total_len)\n }\n}\n\npub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {\n // The function selector is computed from the function signature, which is made up of the function name and types of\n // parameters, but not including the return type. For example, given:\n //\n // fn foo(a: Field, b: AztecAddress) -> Field\n //\n // The signature will be \"foo(Field,AztecAddress)\".\n let fn_name = f.name();\n let args_signatures =\n f.parameters().map(|(_, typ): (Quoted, Type)| signature_of_type(typ)).join(quote {,});\n let signature_quote = quote { $fn_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::function_selector::FunctionSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\npub(crate) comptime fn compute_event_selector(s: TypeDefinition) -> Field {\n // The event selector is computed from the type signature of the struct in the event, similar to how one might type\n // the constructor function. For example, given:\n //\n // struct Foo {\n // a: Field,\n // b: AztecAddress,\n // }\n //\n // The signature will be \"Foo(Field,AztecAddress)\".\n let event_name = s.name();\n let args_signatures = s\n .fields_as_written()\n .map(|(_, typ): (Quoted, Type)| {\n // signature_of_type can handle structs, so this supports nested structs\n // FIXME: Field generics are not handled here!\n signature_of_type(typ)\n })\n .join(quote {,});\n let signature_quote = quote { $event_name($args_signatures) };\n let (signature_str_quote, _) = signature_quote.as_str_quote();\n\n let computation_quote = quote {\n protocol_types::traits::ToField::to_field(protocol_types::abis::event_selector::EventSelector::from_signature($signature_str_quote))\n };\n unquote!(computation_quote)\n}\n\n/// Returns how many storage slots a type needs to reserve for itself. State variables must implement the Storage trait\n/// for slots to be allocated for them.\npub(crate) comptime fn get_storage_size(typ: Type) -> u32 {\n // We create a type variable for the storage size. We can't simply read the value used in the implementation because\n // it may not be a constant (e.g. N + 1). We then bind it to the implementation of the Storage trait.\n let storage_size = std::meta::typ::fresh_type_variable();\n assert(\n typ.implements(quote { crate::state_vars::Storage<$storage_size> }.as_trait_constraint()),\n f\"Attempted to fetch storage size, but {typ} does not implement the Storage trait\",\n );\n\n storage_size.as_constant().unwrap()\n}\n\npub(crate) comptime fn module_has_storage(m: Module) -> bool {\n m.structs().any(|s: TypeDefinition| {\n s.has_named_attribute(\"storage\") | s.has_named_attribute(\"storage_no_init\")\n })\n}\n\npub(crate) comptime fn module_has_initializer(m: Module) -> bool {\n m.functions().any(|f: FunctionDefinition| is_fn_initializer(f))\n}\n\n/// Returns the typed expression of a trait method implementation.\n///\n/// This helper function is preferred over directly inlining with `$typ::target_method()` in a quote,\n/// as direct inlining would result in missing import warnings in the generated code (specifically,\n/// warnings that the trait implementation is not in scope).\n///\n/// # Note\n/// A copy of this function exists in `noir-protocol-circuits/crates/types/src/meta/mod.nr`. We maintain separate\n/// copies because importing it here from there would cause the `target_trait` to be interpreted in the context\n/// of the protocol circuits types crate, making it impossible to compile code for traits from this crate\n/// (e.g. NoteType).\npub(crate) comptime fn get_trait_impl_method(\n typ: Type,\n target_trait: Quoted,\n target_method: Quoted,\n) -> TypedExpr {\n let trait_constraint = target_trait.as_trait_constraint();\n typ\n .get_trait_impl(trait_constraint)\n .expect(f\"Type does not implement trait\")\n .methods()\n .filter(|m| m.name() == target_method)[0]\n .as_typed_expr()\n}\n"
|
|
3385
3385
|
},
|
|
3386
3386
|
"110": {
|
|
3387
3387
|
"path": "/home/aztec-dev/aztec-packages/noir-projects/aztec-nr/aztec/src/messages/discovery/mod.nr",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/noir-contracts.js",
|
|
3
|
-
"version": "0.87.
|
|
3
|
+
"version": "0.87.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
]
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@aztec/aztec.js": "0.87.
|
|
56
|
+
"@aztec/aztec.js": "0.87.6",
|
|
57
57
|
"tslib": "^2.4.0"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@aztec/builder": "0.87.
|
|
60
|
+
"@aztec/builder": "0.87.6",
|
|
61
61
|
"@jest/globals": "^29.5.0",
|
|
62
62
|
"@types/jest": "^29.5.0",
|
|
63
63
|
"jest": "^29.5.0",
|