noir-lang / noir

Noir is a domain specific language for zero knowledge proofs
https://noir-lang.org
Apache License 2.0
875 stars 190 forks source link

Debug compilation for Noir programs #2128

Open vezenovm opened 1 year ago

vezenovm commented 1 year ago

Problem

When working with nargo test, everything supplied to a function is a constant. This leads to issues as outlined in this PR: https://github.com/noir-lang/noir/pull/2116, where println does not work in tests with failing constraints. Essentially a failed constraint is resolved during compilation before we ever reach execution (where the println foreign call is handled).

Happy Case

We should offer a debug compile option that does not enforce constrain instructions at compile-time. This compile option will be enabled during nargo test to allow println to execute even with compile time constraint failures.

Alternatives Considered

Otherwise println just will not work during failing tests which leads to inconsistent functionality with nargo execute. No realistic alternatives.

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

TomAFrench commented 1 year ago

There are a number of other optimisations which we could turn off in order to improve debugging here, e.g. the redundant range optimisation can result in an overflow not being displayed due to the fact that it's being caught later.

kevaundray commented 1 year ago

An alternative approach is for tests to make the input be seen as non-constant, so that you can reproduce the exact environment one gets when a program is executed (with optimizations)

vezenovm commented 1 year ago

Linking this comment (https://github.com/noir-lang/noir/pull/2446#discussion_r1307701028) to note another optimization that can be removed in a debug compilation mode

TomAFrench commented 1 year ago

In the short term (and complementary to debug mode in the future), we could add something similar to Rust's blackbox function. This would just be an identity function which is listed as having side-effects, which would block any SSA optimizations from being applied to the outputs.