Problem
Fp.add at 4.6-5.7ns vs arkworks' 3-4ns (~40% gap). The conditional modular reduction likely causes branch misprediction overhead.
Approach
Replace conditional branch with branchless reduction:
- x86 ASM using
sbb to generate mask + cmov chain for conditional subtraction
- Or: always subtract modulus, use carry flag to select result (
sbb + cmov)
- Ensure no unnecessary loads/stores around the operation
Impact
Add/sub are called even more frequently than mul (every field operation involves adds for Karatsuba cross-terms, point coordinate updates, etc.). A 30% improvement compounds significantly.
Files
src/field/mod.zig — MontgomeryField.add(), .sub(), .addNoReduce(), .reduce()
Benchmark baseline
- Fp.add: 4.6-5.7 ns/op
- Target: 3-4 ns/op
Problem
Fp.add at 4.6-5.7ns vs arkworks' 3-4ns (~40% gap). The conditional modular reduction likely causes branch misprediction overhead.
Approach
Replace conditional branch with branchless reduction:
sbbto generate mask +cmovchain for conditional subtractionsbb+cmov)Impact
Add/sub are called even more frequently than mul (every field operation involves adds for Karatsuba cross-terms, point coordinate updates, etc.). A 30% improvement compounds significantly.
Files
src/field/mod.zig—MontgomeryField.add(),.sub(),.addNoReduce(),.reduce()Benchmark baseline