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

Replace constant calls to black box functions with their result #1962

Closed
TomAFrench opened this issue Jul 18, 2023 · 1 comment · Fixed by #1981
Closed

Replace constant calls to black box functions with their result #1962

TomAFrench opened this issue Jul 18, 2023 · 1 comment · Fixed by #1981
Assignees
Labels
enhancement New feature or request

Comments

@TomAFrench
Copy link
Member

TomAFrench commented Jul 18, 2023

Problem

Consider the Noir program

use dep::std;

fn main() -> pub [u8; 32]  {
    std::hash::keccak256([1], 1)
}

This currently results in the ACIR

Compiled ACIR for main:
current witness index : 34
public parameters indices : []
return value indices : [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
EXPR [ (-1, _1) 1 ]
EXPR [ (-1, _2) 1 ]
BLACKBOX::KECCAK256 [(_1, num_bits: 8), (_2, num_bits: 32)] [ (_3,...,_34)]

We're then including a keccak256 constraint which needs to be proven with a whopping 21553 gates. This is unnecessary as we can bake in this knowledge at compile time, this circuit should instead just assign the return witnesses to the expected values without performing the hash.

Happy Case

We should add an logic in simplify_call so that if we have a black box function with all constant inputs then we simplify it to the black box function's outputs.

fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) -> SimplifyResult {

Alternatives Considered

No response

Additional Context

Some black box functions rely on the backend to provide a value so we cannot perform this optimisation for them currently.

Would you like to submit a PR for this Issue?

Yes

Support Needs

No response

@TomAFrench TomAFrench added the enhancement New feature or request label Jul 18, 2023
@kevaundray
Copy link
Contributor

See here for a small discussion on this: https://github.com/noir-lang/noir/pull/1896/files#r1258872431

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants