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: compute witness when println evaluated before input #891

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
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
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