Skip to content

feat(frontend): make the front half of the compiler field-generic - #11

Merged
BornPsych merged 10 commits into
provekit-v2from
ys/goldilocks-field-genericity
Sep 8, 2026
Merged

feat(frontend): make the front half of the compiler field-generic#11
BornPsych merged 10 commits into
provekit-v2from
ys/goldilocks-field-genericity

Conversation

@BornPsych

@BornPsych BornPsych commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

The front half of the compiler, from source text to the monomorphized AST, is now correct for more than one prime field: an integer is never carried through the field where it might not fit, a value outside the field is an error rather than a reduction, and stdlib items that only work on bn254 are gated to it. bn254 stays the default and fully green. The goldilocks feature of noirc_driver selects Goldilocks (p = 2^64 − 2^32 + 1) in every crate below it, and the Goldilocks CI lane now runs the ABI, frontend and stdlib checks under that build.

The rules, in one place:

  • x as Field is the identity on the value and is defined only for source types whose every value lies below p. An unsigned type that can hold a value at or above p (u64 and u128 under Goldilocks) is refused at type check; the escape hatch is an explicit narrowing cast. Signed sources stay a type error.
  • A Field literal or a comptime Field result must lie in [0, p); nothing reduces a value into the field.
  • Integer literals up to u128 stay exact in the monomorphized AST under every field.
  • At the entry point, one field element carries one scalar: an integer is accepted iff it is in the range of its type and its fixed-width bit pattern is below p.

Changes

  • acir_field: to_i128 reads the shorter of an element's two spellings on the field's own byte width (8 bytes under Goldilocks, 32 under bn254), with a round-trip test generic over the field; assert_unique_feature! covers goldilocks.
  • Parser and AST: impl blocks and trait impls keep their attributes. The formatter prints them and the LSP remaps their locations; function-only attributes on an impl are now a parse error instead of being dropped silently.
  • #[field(..)] gates impl blocks, impl methods, trait impls (whole blocks), inline modules, mod x; declarations and comptime-generated impls. A gated-out item is dropped at definition collection before anything about it is interned, so two same-named items gated to different fields coexist and a gated-out mod x; needs no file.
  • Comptime casts evaluate on the source value as a BigInt: an integer target takes the two's complement pattern at the target width and reads it by the target's signedness; a Field target takes the source's own-width pattern exactly and is an error if it is not below p.
  • ABI input parsing: scalar_to_field applies the one-element rule for both formats and for quoted and native spellings, and noirc_abi forwards the goldilocks feature.
  • Type checker: IntegerTypeExceedsField refuses as Field from an unsigned type whose width reaches the field's. The monomorphizer re-checks a cast whose source type was still a type variable at type check.
  • stdlib: From<u64> and From<u128> for Field, Hash for u64/u128/i64, and the wrapping arithmetic on those types (implemented through the field) are gated to bn254. The AsPrimitive<Field> impls are generated only for widths below the modulus. The driver test stdlib_field_casts pins that the stdlib never casts a wide integer to Field under either build.
  • CI: the Goldilocks lane runs acir_field, noirc_abi, the frontend suite minus six bn254-pinned tests, and the driver's stdlib checks.

Each place where the feature flag stands in for a runtime field selection carries a TODO naming the follow-up: a runtime FieldConfig, struct and trait gating with a negated form and a width predicate, and native wrapping arithmetic on wide types.

@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Thank you for your contribution to the Noir language.

Please do not force push to this branch after the Noir team have started review of this PR. Doing so will only delay us merging your PR as we will need to start the review process from scratch.

Thanks for your understanding.

@BornPsych
BornPsych merged commit 5f55fba into provekit-v2 Sep 8, 2026
134 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant