Skip to content

BN254 multi-precision support for WASM backend #420

Description

@VanshSahay

The WASM backend (PR #415) supports single-word field arithmetic for primes < 2^63. BN254 (254-bit prime) needs 4-word arithmetic.

What's already in place

  • compileModule accepts numWords parameter (default 1)
  • snarkjs ABI handles n32 = numWords * 2 and signalBytes = n32 * 4
  • fieldHelpers has a multi-word path (currently stubbed)

Work needed

1. Multi-word field arithmetic in WAT for 4-word elements

  • fadd_4: word-by-word add with carry + conditional sub of prime
  • fsub_4: word-by-word sub with borrow + conditional add of prime
  • fmul_4: double-and-add (requires only fadd, about 254 iterations per mul)
  • finv_4: Fermat little theorem via fpow_4

2. Expression compilers parameterized by numWords

Each FExpr operation pushes/pops numWords i64 values instead of 1. NExpr and BExpr unchanged.

3. Witness function signature

For numWords > 1, the direct witness function takes numWords * numInputs params and returns numWords * tw results.

Approach

Write the multi-word arithmetic as a standalone .wat file, test with wat2wasm + Node.js against known BN254 test vectors, then integrate into the Lean WAT generator in Compile.lean.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions