Skip to content

Commit

Permalink
fix: avoid huge unrolling in hash_args (#5703)
Browse files Browse the repository at this point in the history
After noir-lang/noir#4736 was fixed, now this
is possible :)

---------

Co-authored-by: Alvaro Rodriguez <sirasistant@MacBook-Pro-de-Alvaro.local>
  • Loading branch information
sirasistant and Alvaro Rodriguez authored Apr 12, 2024
1 parent 495f254 commit 10d9ad9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
29 changes: 16 additions & 13 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,24 @@ pub fn hash_args(args: [Field]) -> Field {
if args.len() == 0 {
0
} else {
assert(args.len() < ARGS_HASH_CHUNK_COUNT * ARGS_HASH_CHUNK_LENGTH);
let mut chunks_hashes = [0; ARGS_HASH_CHUNK_COUNT];
for i in 0..ARGS_HASH_CHUNK_COUNT {
let mut chunk_hash = 0;
let start_chunk_index = i * ARGS_HASH_CHUNK_LENGTH;
if start_chunk_index < args.len() {
let mut chunk_args = [0; ARGS_HASH_CHUNK_LENGTH];
for j in 0..ARGS_HASH_CHUNK_LENGTH {
let item_index = i * ARGS_HASH_CHUNK_LENGTH + j;
if item_index < args.len() {
chunk_args[j] = args[item_index];
}
}
chunk_hash = pedersen_hash(chunk_args, GENERATOR_INDEX__FUNCTION_ARGS);
let mut current_chunk_values = [0; ARGS_HASH_CHUNK_LENGTH];

let mut current_chunk_index = 0;
let mut index_inside_current_chunk = 0;
for i in 0..args.len() {
current_chunk_values[index_inside_current_chunk] = args[i];
index_inside_current_chunk+=1;
if index_inside_current_chunk == ARGS_HASH_CHUNK_LENGTH {
chunks_hashes[current_chunk_index] = pedersen_hash(current_chunk_values, GENERATOR_INDEX__FUNCTION_ARGS);
current_chunk_values = [0; ARGS_HASH_CHUNK_LENGTH];
current_chunk_index+=1;
index_inside_current_chunk = 0;
}
chunks_hashes[i] = chunk_hash;
}
if index_inside_current_chunk > 0 {
chunks_hashes[current_chunk_index] = pedersen_hash(current_chunk_values, GENERATOR_INDEX__FUNCTION_ARGS);
}
pedersen_hash(chunks_hashes, GENERATOR_INDEX__FUNCTION_ARGS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ impl Loops {
if next_loop.blocks.iter().any(|block| self.modified_blocks.contains(block)) {
let mut new_context = find_all_loops(function);
new_context.failed_to_unroll = self.failed_to_unroll;
return new_context.unroll_each_loop(function);
return unroll_errors
.into_iter()
.chain(new_context.unroll_each_loop(function))
.collect();
}

// Don't try to unroll the loop again if it is known to fail
Expand Down

0 comments on commit 10d9ad9

Please sign in to comment.