Skip to main content

aztec_core/abi/
checkers.rs

1use super::types::{AbiType, FunctionArtifact};
2
3/// Returns true if the ABI type represents an AztecAddress or EthAddress struct.
4pub fn is_address_struct(typ: &AbiType) -> bool {
5    is_aztec_address_struct(typ) || is_eth_address_struct(typ)
6}
7
8/// Returns true if the ABI type is an `AztecAddress` struct.
9pub fn is_aztec_address_struct(typ: &AbiType) -> bool {
10    matches!(typ, AbiType::Struct { name, .. } if name.ends_with("address::AztecAddress"))
11}
12
13/// Returns true if the ABI type is an `EthAddress` struct.
14pub fn is_eth_address_struct(typ: &AbiType) -> bool {
15    matches!(typ, AbiType::Struct { name, .. } if name.ends_with("address::EthAddress"))
16}
17
18/// Returns true if the ABI type is a `FunctionSelector` struct.
19pub fn is_function_selector_struct(typ: &AbiType) -> bool {
20    matches!(typ, AbiType::Struct { name, .. }
21        if name.ends_with("function_selector::FunctionSelector"))
22}
23
24/// Returns true if the ABI type is a struct wrapping a single `inner: Field`.
25pub fn is_wrapped_field_struct(typ: &AbiType) -> bool {
26    matches!(typ, AbiType::Struct { fields, .. }
27        if fields.len() == 1
28           && fields[0].name == "inner"
29           && fields[0].typ == AbiType::Field)
30}
31
32/// Returns true if the ABI type is a `PublicKeys` struct (Noir ABI representation).
33pub fn is_public_keys_struct(typ: &AbiType) -> bool {
34    matches!(typ, AbiType::Struct { name, fields, .. }
35        if name.ends_with("public_keys::PublicKeys")
36           && fields.len() == 4
37           && fields[0].name == "npk_m"
38           && fields[1].name == "ivpk_m"
39           && fields[2].name == "ovpk_m"
40           && fields[3].name == "tpk_m")
41}
42
43/// Returns true if the ABI type is a `BoundedVec` struct.
44pub fn is_bounded_vec_struct(typ: &AbiType) -> bool {
45    matches!(typ, AbiType::Struct { name, fields, .. }
46        if name.ends_with("bounded_vec::BoundedVec")
47           && fields.len() == 2
48           && fields[0].name == "storage"
49           && fields[1].name == "len")
50}
51
52/// Returns true if the ABI type is an `Option` struct.
53pub fn is_option_struct(typ: &AbiType) -> bool {
54    matches!(typ, AbiType::Struct { name, fields, .. }
55        if name.ends_with("option::Option")
56           && fields.len() == 2
57           && fields[0].name == "_is_some"
58           && fields[1].name == "_value")
59}
60
61/// Compute the number of field elements an ABI type occupies when flattened.
62pub fn abi_type_size(typ: &AbiType) -> usize {
63    match typ {
64        AbiType::Field | AbiType::Boolean | AbiType::Integer { .. } => 1,
65        AbiType::String { length } => *length,
66        AbiType::Array { element, length } => length * abi_type_size(element),
67        AbiType::Struct { fields, .. } => fields.iter().map(|f| abi_type_size(&f.typ)).sum(),
68        AbiType::Tuple { elements } => elements.iter().map(abi_type_size).sum(),
69    }
70}
71
72/// Compute the total flattened field-element count for a function's parameters.
73pub fn count_arguments_size(function: &FunctionArtifact) -> usize {
74    function
75        .parameters
76        .iter()
77        .map(|p| abi_type_size(&p.typ))
78        .sum()
79}