noir-lang / noir

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

Global arrays #439

Closed vezenovm closed 1 year ago

vezenovm commented 1 year ago

Problem

Many users are requesting the ability to have global arrays. This will allow users to specify lists of compile time values in a separate file that can be referenced throughout a Noir project.

For example even in our standard lib we could move out the mimc constants to a separate file that does not clutter up our hash.nr module: https://github.com/noir-lang/noir/blob/23c17040b19620bce9e392d5b8a3ea83746b5ea8/crates/std_lib/src/hash.nr#L37 This can be applied to many other cryptographic functionality that requires lists of primes and/or constants.

Solution

Allow Noir developers to use global arrays. Follow the current scheme of not allowing mutable globals. Keeping the same feature of not allowing mutable globals should make implementation simpler. Either way, moving to mutable globals would be a separate issue that should be discussed internally before implementation.

Alternatives considered

(Describe any alternative solutions you have considered.)

Additional context

(If applicable.)

michaelneuder commented 1 year ago

Agreed! this would be helpful for implementing sha256 in noir for example: https://github.com/michaelneuder/crypto-primitives/blob/14ff0241182a7fac84c5e66ad4ddc8e75182b7bc/sha256/sha256/src/main.nr#L27-L39. making this global and compile time would be better.

kevaundray commented 1 year ago

@vezenovm can we close this?

vezenovm commented 1 year ago

@vezenovm can we close this?

I am helping @michaelneuder submit a PR for this as I think it is a good first issue. Should hopefully be able to close it soon.

jfecher commented 1 year ago

Something we may have to verify in these literals is that they contain no other globals themselves. For example,

global N = 10;
global A = [N, M];
global M = 11;

The global A is now dependent on N and M being evaluated first which I don't believe we guarantee with the current compiler's handling of globals since globals are restricted to only literals. Array literals may contain other expressions as elements, so we may have to verify that all these sub expressions are literals as well.