Repository navigation
modexp: take the inverse modulo a known prime from the hint tape - #13
Merged
Merged
Conversation
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>
5 tasks done
… 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
changed the base branch from
perf/airbender-hints-native-trie
to
perf/modexp-one-digit-montgomery
November 21, 2026 10:50
Collaborator
|
Rebased in effect onto #12 (merged it in, base changed to its branch), since both change the one-digit path. The check of |
maximmenshikov
changed the base branch from
perf/modexp-one-digit-montgomery
to
main
November 21, 2026 13:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #11 (base
perf/airbender-hints-native-trie).Changes
zkvm_modexptakes 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 andmont(x, y) = mont(1, 1), that isx 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 tofield_hint_emit, so a recorded tape matches the order of the takes.mont_prime(), whichmodexp_montand 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 atairbender_read_prehashed_input), Airbender CLI7a147dacycles, 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: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, inbuild/ci.sh):modexp.con 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 thex y = 1check fails 7 of the cases.build/ci.sh: all 1554 vectors and the existing host tests pass.