Skip to content

Commit

Permalink
fix(aztec_nr): serialise arrays of structs (#3401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Nov 2, 2023
1 parent 7511724 commit e979a58
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions compiler/noirc_frontend/src/hir/aztec_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,9 @@ fn create_context(ty: &str, params: &[(Pattern, UnresolvedType, Visibility)]) ->
let expression = match unresolved_type {
// `hasher.add_multiple({ident}.serialize())`
UnresolvedTypeData::Named(..) => add_struct_to_hasher(identifier),
// TODO: if this is an array of structs, we should call serialize on each of them (no methods currently do this yet)
UnresolvedTypeData::Array(..) => add_array_to_hasher(identifier),
UnresolvedTypeData::Array(_, arr_type) => {
add_array_to_hasher(identifier, &arr_type)
}
// `hasher.add({ident})`
UnresolvedTypeData::FieldElement => add_field_to_hasher(identifier),
// Add the integer to the hasher, casted to a field
Expand Down Expand Up @@ -900,20 +901,39 @@ fn create_loop_over(var: Expression, loop_body: Vec<Statement>) -> Statement {
}))
}

fn add_array_to_hasher(identifier: &Ident) -> Statement {
fn add_array_to_hasher(identifier: &Ident, arr_type: &UnresolvedType) -> Statement {
// If this is an array of primitive types (integers / fields) we can add them each to the hasher
// casted to a field

// Wrap in the semi thing - does that mean ended with semi colon?
// `hasher.add({ident}[i] as Field)`
let cast_expression = cast(
index_array(identifier.clone(), "i"), // lhs - `ident[i]`
UnresolvedTypeData::FieldElement, // cast to - `as Field`
);

let arr_index = index_array(identifier.clone(), "i");
let (add_expression, hasher_method_name) = match arr_type.typ {
UnresolvedTypeData::Named(..) => {
let hasher_method_name = "add_multiple".to_owned();
let call = method_call(
// All serialise on each element
arr_index, // variable
"serialize", // method name
vec![], // args
);
(call, hasher_method_name)
}
_ => {
let hasher_method_name = "add".to_owned();
let call = cast(
arr_index, // lhs - `ident[i]`
UnresolvedTypeData::FieldElement, // cast to - `as Field`
);
(call, hasher_method_name)
}
};

let block_statement = make_statement(StatementKind::Semi(method_call(
variable("hasher"), // variable
"add", // method name
vec![cast_expression],
variable("hasher"), // variable
&hasher_method_name, // method name
vec![add_expression],
)));

create_loop_over(variable_ident(identifier.clone()), vec![block_statement])
Expand Down

0 comments on commit e979a58

Please sign in to comment.