Skip to content

Commit

Permalink
fix: compute witness when println evaluated before input (#891)
Browse files Browse the repository at this point in the history
* compute witness when var from var_cache is not const during println evaluation

* move regression test for std::println to merkle_insert

* include comment pointing to regression test PR
  • Loading branch information
vezenovm authored Feb 21, 2023
1 parent adba24c commit 2727b34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions crates/nargo/tests/test_data/merkle_insert/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ fn main(
constrain new_leaf_exists == 1;

let h = std::hash::mimc_bn254(mimc_input);
// Regression test for PR #891
std::println(h);
constrain h == 18226366069841799622585958305961373004333097209608110160936134895615261821931;
}

25 changes: 16 additions & 9 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,25 @@ fn evaluate_println(
log_string = format_field_string(field);
}
None => {
let var = var_cache.get(&node_id).unwrap_or_else(|| {
panic!(
"invalid input for print statement: {:?}",
ctx.try_get_node(node_id).expect("node is missing from SSA")
)
});
let mut var = var_cache
.get(&node_id)
.unwrap_or_else(|| {
panic!(
"invalid input for print statement: {:?}",
ctx.try_get_node(node_id).expect("node is missing from SSA")
)
})
.clone();
if let Some(field) = var.to_const() {
log_string = format_field_string(field);
} else if let Some(w) = var.cached_witness() {
log_witnesses.push(*w);
} else if let Some(w) = var.get_or_compute_witness(evaluator, false) {
// We check whether there has already been a cached witness for this node. If not, we generate a new witness and include it in the logs
// TODO we need a witness because of the directive, but we should use an expression
log_witnesses.push(w);
} else {
unreachable!("array element to be logged is missing a witness");
unreachable!(
"a witness should have been computed for the non-constant expression"
);
}
}
},
Expand Down

0 comments on commit 2727b34

Please sign in to comment.