Skip to content

BLS12-381: avoid inversion in G2 square roots - #2579

Open
carstenartur wants to merge 1 commit into
bcgit:mainfrom
carstenartur:optimize/bls-g2-inverse-free-sqrt-nov26
Open

carstenartur wants to merge 1 commit into
bcgit:mainfrom
carstenartur:optimize/bls-g2-inverse-free-sqrt-nov26

Conversation

@carstenartur

@carstenartur carstenartur commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The non-real Fp² square-root branch used by BLS12-381 G2 hash-to-curve currently uses one or two component exponentiations and a field inversion. This change derives both root coordinates from one inverse-square-root exponentiation, removing the inversion and conditional second exponentiation from that branch.

The patch changes only the implementation and its regression tests. The comparison baseline is upstream commit 6560e91d0a164b8ab58266f141ee6daeec21a340.

Correctness

For p = 3 (mod 4), i² = -1, b != 0, and a supplied norm root g² = a² + b², compute:

delta = (a + g)/2
t = delta^((p - 3)/4)
c = delta*t
d = b*t/2

Return (c,d) if c² = delta, otherwise (d,-c).

Here delta != 0: otherwise g = -a would imply b² = 0. Euler's criterion gives epsilon = delta*t² in {1,-1}. The norm relation implies delta² - b²/4 = a*delta, hence (c+d*i)² = epsilon*(a+b*i). When epsilon = -1, multiplying by -i gives the required root. Testing c² = delta identifies the branch without another exponentiation.

The zero/real-input path is retained. The helper may choose the opposite root, which the existing RFC 9380 sign normalization maps to the same curve point. There is no public API change or constant-time claim.

This applies a known technique: Michael Scott, A note on the calculation of some functions in finite fields: Tricks of the Trade, including the quadratic-extension discussion. Sign normalization follows RFC 9380, Appendix F.2.

Measurements

Identical compilation, inputs and JVM settings for baseline and optimized sources; Temurin 21.0.13.1 on AMD EPYC 7763. Each operation used three paired forks per variant, five 1-second warmup iterations, eight 1-second measurement iterations, one benchmark thread and GC profiling.

Operation Baseline µs/op Optimized µs/op Mean latency change
sqrtOrSqrtZ 133.85 90.96 −32.0%
hashToCurve 1171.98 1074.95 −8.3%
Sign, reused key 2833.38 2734.38 −3.5%
Sign, fresh key object 2832.03 2714.98 −4.1%
Verify, reused key 7663.46 7547.35 −1.5%; unresolved
Verify, fresh key object 7923.41 7926.82 +0.04%; unresolved

Both verification speedup intervals include 1; no verification gain is claimed. Helper allocation decreased from 16,196 to 13,115 B/op. These measurements describe this machine and workload.

The complete evidence is retained separately at immutable commit 8fb34a6, whose production and test sources are byte-identical to this patch:

Validation

  • Both variants passed the registered 220-test hash-to-curve/BLS suite, including RFC and BLS/Eth2 vectors.
  • New tests cover 14 edge fixtures and 256 deterministic differential inputs against the previous formula, including comparison up to sign.
  • Encoded public-key, hash-point and signature fingerprints matched for 16 messages; signatures verified with fresh and reused key objects under both variants.
  • Changed production, test and benchmark sources passed -Xlint:all -Werror with Error Prone enabled under the existing check policy. Ordinary Java 8 compatibility compilation was retained; the additional strict diagnostics pass used release 11.
  • Full main-source Checkstyle passed.

The broader core test suite was interrupted before completion and is not claimed as passing. The recorded validation and measurements above use the same production and test sources retained in this focused patch.

AI tools were used in preparing and checking this contribution.

@carstenartur
carstenartur marked this pull request as ready for review November 26, 2026 04:19
@dghgit dghgit self-assigned this Dec 4, 2026
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