Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aztec_noir): generalise loop to not always inject a hasher instance #2529

Merged
merged 1 commit into from
Sep 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions crates/noirc_frontend/src/hir/def_map/aztec_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
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 serialise on each of them (no methods currently do this yet)

Check warning on line 321 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (serialise)
UnresolvedTypeData::Array(..) => add_array_to_hasher(identifier),
// `hasher.add({ident})`
UnresolvedTypeData::FieldElement => add_field_to_hasher(identifier),
Expand Down Expand Up @@ -382,13 +382,13 @@
/// Similarly; Structs will be pushed to the context, after serialize() is called on them.
/// Arrays will be iterated over and each element will be pushed to the context.
/// Any primitive type that can be cast will be casted to a field and pushed to the context.
fn abstract_return_values(func: &mut NoirFunction) -> Option<Statement> {
fn abstract_return_values(func: &NoirFunction) -> Option<Statement> {
let current_return_type = func.return_type().typ;
let len = func.def.body.len();
let last_statement = &func.def.body.0[len - 1];

// TODO: (length, type) => We can limit the size of the array returned to be limited by kernel size
// Doesnt need done until we have settled on a kernel size

Check warning on line 391 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Doesnt)
// TODO: support tuples here and in inputs -> convert into an issue

// Check if the return type is an expression, if it is, we can handle it
Expand All @@ -399,7 +399,7 @@
UnresolvedTypeData::Array(..) => Some(make_array_return_type(expression.clone())),
// Cast these types to a field before pushing
UnresolvedTypeData::Bool | UnresolvedTypeData::Integer(..) => {
Some(make_castable_return_type(expression.clone()))

Check warning on line 402 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (castable)
}
UnresolvedTypeData::FieldElement => Some(make_return_push(expression.clone())),
_ => None,
Expand Down Expand Up @@ -440,12 +440,12 @@
/// ```noir
/// `context.return_values.push_array({push_value}.serialize())`
fn make_struct_return_type(expression: Expression) -> Statement {
let serialised_call = method_call(

Check warning on line 443 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (serialised)
expression.clone(), // variable
"serialize", // method name
vec![], // args
);
make_return_push_array(serialised_call)

Check warning on line 448 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (serialised)
}

/// Make array return type
Expand All @@ -459,16 +459,22 @@
fn make_array_return_type(expression: Expression) -> Statement {
let inner_cast_expression =
cast(index_array_variable(expression.clone(), "i"), UnresolvedTypeData::FieldElement);
create_loop_over(expression.clone(), vec![inner_cast_expression])
let assignment = Statement::Semi(method_call(
context_return_values(), // variable
"push", // method name
vec![inner_cast_expression],
));

create_loop_over(expression.clone(), vec![assignment])
}

/// Castable return type

Check warning on line 471 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (Castable)
///
/// Translates to:
/// ```noir
/// context.return_values.push({ident} as Field)
/// ```
fn make_castable_return_type(expression: Expression) -> Statement {

Check warning on line 477 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (castable)
// Cast these types to a field before pushing
let cast_expression = cast(expression.clone(), UnresolvedTypeData::FieldElement);
make_return_push(cast_expression)
Expand Down Expand Up @@ -536,7 +542,7 @@

fn add_struct_to_hasher(identifier: &Ident) -> Statement {
// If this is a struct, we call serialize and add the array to the hasher
let serialised_call = method_call(

Check warning on line 545 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (serialised)
variable_path(path(identifier.clone())), // variable
"serialize", // method name
vec![], // args
Expand All @@ -545,11 +551,11 @@
Statement::Semi(method_call(
variable("hasher"), // variable
"add_multiple", // method name
vec![serialised_call], // args

Check warning on line 554 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (serialised)
))
}

fn create_loop_over(var: Expression, loop_body: Vec<Expression>) -> Statement {
fn create_loop_over(var: Expression, loop_body: Vec<Statement>) -> Statement {
// If this is an array of primitive types (integers / fields) we can add them each to the hasher
// casted to a field

Expand All @@ -562,12 +568,7 @@

// What will be looped over
// - `hasher.add({ident}[i] as Field)`
let for_loop_block =
expression(ExpressionKind::Block(BlockExpression(vec![Statement::Semi(method_call(
variable("hasher"), // variable
"add", // method name
loop_body,
))])));
let for_loop_block = expression(ExpressionKind::Block(BlockExpression(loop_body)));

// `for i in 0..{ident}.len()`
Statement::Expression(expression(ExpressionKind::For(Box::new(ForExpression {
Expand All @@ -590,12 +591,18 @@
index_array(identifier.clone(), "i"), // lhs - `ident[i]`
UnresolvedTypeData::FieldElement, // cast to - `as Field`
);
create_loop_over(variable_ident(identifier.clone()), vec![cast_expression])
let block_statement = Statement::Semi(method_call(
variable("hasher"), // variable
"add", // method name
vec![cast_expression],
));

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

fn add_field_to_hasher(identifier: &Ident) -> Statement {
// `hasher.add({ident})`
let iden = variable_path(path(identifier.clone()));

Check warning on line 605 in crates/noirc_frontend/src/hir/def_map/aztec_library.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (iden)
Statement::Semi(method_call(
variable("hasher"), // variable
"add", // method name
Expand Down