Repository navigation
JIT: Montgomery rows, CMOV mask selects and carry chains through composite field kernels - #2
Merged
Merged
Conversation
Recognize the rows of a Montgomery multiplication (several rows per block, any width, the short first lanes of CIOS rows) and emit them as MULX with dual ADCX/ADOX carry chains. Turn the constant-time select t ^ ((t ^ s) & (0 - carry)) after a carry chain into CMOVs on the carry flag. Let loads, LEAs, non-zero constants and casts sit between a carry and its consumer, follow the last carry of a long chain past 64 nodes, and run the carry passes in OSR methods, excluding only root-method locals.
Accept first carries and borrows seeded from independent bit-valued locals, propagate CF/OF across nonvolatile integer stores, hoist closed invariant AND/OR/XOR operand trees ahead of a chain, fold the final ADC/SBB in place when moving a load would cross an aliased store, keep flags across unsigned scalar products between carry limbs, keep a shared MULX multiplier in RDX and out of the result registers, recompute operand containment after the multiply-carry rewrites (fixing a checked-JIT assert in BigIntegerCalculator.MulAdd1 under tiering with JitStressRegs=1), and raise the bounded carry-use scan allowance so wide subtractions keep their flags. Add regression tests for each pattern.
benaadams
marked this pull request as ready for review
October 15, 2026 11:10
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.
This builds on dotnet#134039. It targets that PR's
adcbranch, so the diff shows only the additions.It came out of Nethermind.Crypto.BlsNet, a pure C# BLS12-381 library (fields, G1/G2, MSM, map-to-curve, pairing) ported from blst and used to implement Nethermind's EIP-2537 precompiles without native code. Its field arithmetic is written in the carry idioms dotnet#134039 recognizes. With dotnet#134039 alone, those kernels still ran 1.5–1.7x slower than blst's hand-written MULX/ADCX/ADOX assembly. With this change, the same C# beats blst in every EIP-2537 precompile.
Results
All numbers are from a Ryzen 9 9950X3D VM running Windows x64. "dotnet#134039" is the PR at
331fa2b3; "dotnet#134039 + this PR" adds this branch. Both runtimes report as .NET 12.0.0-dev. The managed binaries are identical across the runtime columns: only the JIT changes.Fr/Fp Montgomery mul+add
Times are in ns per
acc = acc * a + b, over 1,000,000 dependent iterations. Each run takes the best of 7 rounds, and the table shows the best of 5 interleaved runs (benchmark):EIP-2537 precompiles in Nethermind
These come from Nethermind's own BenchmarkDotNet precompile benchmarks (in-process toolchain, Short job), with the benchmark inputs from
bench_precompiles. The MSM inputs use 1–4096 distinct points; the pairing inputs use distinct points.Nethermind.Crypto.Bls0.3.17.15 (blst with ADX).Changes
All changes are x64-only and live in
lower.cpp, plus small additions inlower.h,lsraxarch.cppandemitxarch.cpp. The multiply parts use the same ADX gating as dotnet#134039.jit/README.mdhas more detail.TryLowerMultiplyCarryRowskeeps CF and OF across the rows of a Montgomery multiplication: several rows per block, rows of any width, and the short first lanes of CIOS rows. Each lane becomes amulx.adcxcarries the previous high word plus the accumulator in CF, andadoxadds the low product in OF. The last high word is drained as hi + CF + OF.LowerMaskSelectsturns the constant-time conditional subtractionmask = 0 - carry; r = t ^ ((t ^ s) & mask)intocmovccreading the chain's flags. This replacessetb; movzx; neg, plusxor; and; xorper limb.IsCarryFlagsCrossinglets loads,lea, non-zero constants, int→long casts and contained nodes sit between a carry and its consumer.BigMulbetween carry limbs uses flag-preserving MULX.BigIntegerCalculator.MulAdd1under tiering withJitStressRegs=1, which also reproduces with JIT: recognition of ADC/SBB, MULX/ADCX/ADOX patterns for BigInteger, Decimal, UInt128 dotnet/runtime#134039 alone.The safety conditions are unchanged. Each rewritten temporary must be the only reader of its value and dead afterwards. Only flag-neutral nodes may sit inside a chain. Memory accesses move only when alias checks allow it. Methods with EH are skipped, and every scan is budgeted.
Testing
Regression tests under
src/tests/JIT/opt/Add/:MontgomeryRows;MaskSelect;SeededMaskedCarry;MulxMultiplierCopies;ComplementedCarry;CompositeCarry, sixteen unrolled six-word additions and subtractions checked againstBigInteger;ScalarProductCarry.The tests cover the accepted shapes, the shapes that must be rejected, and aliasing.
Stress runs: all 18 entry points pass on the checked JIT under the default configuration,
JitStress=2andJitStressRegs=1/3/8, each withTieredCompilation=0/1. They also pass with AVX2 disabled and with ADX disabled.Library tests: 82,198 curve checks and 1,295,823 pairing and field checks, differential against native blst, pass on the release and checked JITs, including the stress modes.
Nethermind: all 2,184 BLS precompile tests pass on the patched runtime. These include thousands of random inputs per precompile that must match blst exactly.
Pre-existing failure: JIT: recognition of ADC/SBB, MULX/ADCX/ADOX patterns for BigInteger, Decimal, UInt128 dotnet/runtime#134039's own
CarryTestscrash underJitStress=1, and intermittently underJitStressRegswith tiering. This happens with and without this change.