Skip to content

modexp: take the inverse modulo a known prime from the hint tape - #13

Merged
maximmenshikov merged 10 commits into
mainfrom
perf/modexp-inverse-hint
Nov 21, 2026
Merged

maximmenshikov merged 10 commits into
mainfrom
perf/modexp-inverse-hint

Conversation

@LukaszRozmej

Copy link
Copy Markdown
Member

Stacked on #11 (base perf/airbender-hints-native-trie).

Changes

  • zkvm_modexp takes the result from the field hint tape when it is an inversion: the modulus is one of seven known primes of at most 256 bits (BN254 scalar and base, secp256k1 base and scalar, P-256 base and scalar, BLS12-381 scalar) and the exponent is m - 2, so by Fermat the result is x^-1. The tape value y is accepted when it is below m and mont(x, y) = mont(1, 1), that is x y = 1 mod m: two Montgomery products in place of about 340. x = 0 gives 0 and takes no value. A missing or wrong value is computed as before and given to field_hint_emit, so a recorded tape matches the order of the takes.
  • The Newton step for -N^-1 moves into mont_prime(), which modexp_mont and the check share.

SNARK verifier contracts invert modulo the BN254 scalar prime with MODEXP: on block 26279477, 466 of its 470 MODEXP calls compute x^(r-2) mod r, and 3 more do the same modulo the P-256 prime.

Measured on the Nethermind trusted-input Airbender guest (nethermind#16097 head d3470f3 + nethermind#16117, its input binding pointed at airbender_read_prehashed_input), Airbender CLI 7a147da cycles, 27 mainnet blocks (26279462 to 26279540, every third block). Both arms take recorded division hints and a recorded field tape (slopes; no pairing witness), each recorded with its own library:

Block #11 head With this PR Change
26279522 140.61M 127.93M -9.02%
26279477 143.42M 130.67M -8.89%
26279465 171.39M 158.66M -7.43%
26279528 195.53M 182.72M -6.55%
Other 23 blocks -0.25% to 0.00%
Geomean, 27 blocks -1.25%

MODEXP on 26279477 goes from about 24,800 to about 4,500 cycles per call. The four blocks above are the verifier blocks; no block is slower.

Testing

  • tests/modexp/run.sh (new, in build/ci.sh): modexp.c on the host with the u256 delegation's C model, ASan and UBSan. For each of the seven primes: a right value is taken, a value of the inverse + 1 or of m is rejected and the computed result is given out, an empty tape computes and gives out, x = 0 takes nothing, a base above m works, an exponent with a leading zero byte is still m - 2, another exponent takes nothing. A composite modulus takes nothing. Removing the x y = 1 check fails 7 of the cases.
  • build/ci.sh: all 1554 vectors and the existing host tests pass.
  • The guest's block hash and status are the same as without the hint on all 27 blocks.

maximmenshikov and others added 9 commits November 18, 2026 14:42
The field hint tape is a section of the input after the trusted part:
32-byte values that the code takes one after another and checks.

- The slope of an affine addition, in the bucket sums of the secp256k1
  batch verification and in the additions of BN254 and BLS12-381: one
  product checks it, where computing it takes an inversion.
- The witness of a pairing check: f w = c^lambda with w in Fp6 replaces
  the final exponentiation.

An input without the section runs the code as before. An exhausted or
wrong tape only costs the computation. field_hint_emit is called with
each value that was computed for want of a hint, which is the point a
tape is recorded at.

Also:

- One multi-scalar multiplication for up to 2048 signatures, with the
  window chosen by cost.
- BN254 G1 multiplication on affine points.
- The Miller loop keeps the line coefficients of a G2 point it has seen.
  kzg_lines.c has them for the two G2 points of the KZG check.
- rev8() is Zbb's rev8, which every Airbender machine decodes.

Signed-off-by: Maxim Menshikov <maksim.menshikov@nethermind.io>
The low half of a b + m N is zero by the choice of m, so only its carry
is needed, without the general loop over the digits.

Signed-off-by: Maxim Menshikov <maksim.menshikov@nethermind.io>
JitZkRev8=1 makes the riscv32 JIT of dotnet-riscv emit Zbb's rev8 for
ReverseEndianness. Every Airbender machine decodes that one instruction
of Zbb. A JIT without the option ignores it.

Signed-off-by: Maxim Menshikov <maksim.menshikov@nethermind.io>
…ivision hints

- read_input hashes the witness nodes and the codes of the Trusted input
  while it copies them (airbender_prehashed_nodes,
  airbender_prehashed_code), and keeps the sponge states of the full
  branches.
- src/mpt.c is a Merkle-Patricia trie over those nodes (the
  airbender_mpt_ calls): account and storage reads and writes, and the
  root. It builds each node once and walks pointers. It hashes a changed
  branch from a copy of its witness node with the changed child hashes
  written over, and resumes the retained sponge state. It checks the full
  branches as the guest's SecuredTrieNodeValidator does.
- airbender_division_hints gives the guest the quotients and remainders
  that it checks with the u256 delegation.
- airbender_bloom_set sets the bits of a log bloom.
- __udivdi3 and __umoddi3 of compiler-rt are wrapped onto a division by
  16-bit digits with divu.

tests/mpt/run.sh compares the engine with a trie built from scratch, on
the host with the sanitizers. build/ci.sh runs it.

Signed-off-by: Maxim Menshikov <maksim.menshikov@nethermind.io>
… line

The Ssz build of the Nethermind guest did not fit the 4 MiB ROM: 4227760
bytes. It is 4181992 now.

- kzg_lines.c (39.5 KB) is gone. The Miller loop computes the lines of
  the two KZG points at the first point evaluation of a block and keeps
  them, as for any other G2 point: +0.13% cycles on six mainnet blocks.
- The slope computed for want of a hint is one function for every field
  width, and an affine addition of equal points calls the doubling: both
  were inlined into each of three specializations, 6 KB.

Signed-off-by: Maxim Menshikov <maksim.menshikov@nethermind.io>
The code scanner reads mpt.c alone, where uint32_t was unknown, and
took eight words for eight bytes; the states were a uint64_t pointer
that every use cast to words. libairbender.a is byte for byte the same.

Signed-off-by: Maxim Menshikov <maksim.menshikov@nethermind.io>
… mont_mul

The one-digit product is mont_mul of mont.h now, with n^-1 in MontSingle,
so the check of x y = 1 uses it. The Newton step is mont_inverse, which
modexp_mont and the check share.
@maximmenshikov
maximmenshikov changed the base branch from perf/airbender-hints-native-trie to perf/modexp-one-digit-montgomery November 21, 2026 10:50
@maximmenshikov

Copy link
Copy Markdown
Collaborator

Rebased in effect onto #12 (merged it in, base changed to its branch), since both change the one-digit path. The check of x y = 1 uses mont_mul with MontSingle now, and the Newton step is mont_inverse, shared with modexp_mont. CI does not run for a base other than main, so I ran it by hand: https://github.com/NethermindEth/bflat-airbender/actions/runs/44896520491 (1556/1556 vectors, the host tests pass).

@maximmenshikov
maximmenshikov changed the base branch from perf/modexp-one-digit-montgomery to main November 21, 2026 13:07
@maximmenshikov
maximmenshikov merged commit fbe4bb3 into main Nov 21, 2026
1 check 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.

2 participants