Conversation
gbotrel
previously approved these changes
Oct 20, 2025
Collaborator
gbotrel
left a comment
There was a problem hiding this comment.
lgtm, just I think the ExpByXXXX functions that are now public should have better names / docs, see comments
gbotrel
approved these changes
Oct 27, 2025
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.
Description
Optimise the square root computations in the quadratic extension
Fp2. Applications: Hash-to-G2 and G2-points decompression. This PR implements https://eprint.iacr.org/2024/1563.pdf (Alg. 3) for the casep = 3 mod 4and https://eprint.iacr.org/2020/1497.pdf (Sec. 6.3) otherwise.N.B.: We can further optimize the generic case (e.g. BLS12-377, KoalaBear) by merging the inverse and sqrt computations as1/x = x^{p-2} = x^{2^{e-1}-1} * (x * y^4)^{2^{e-1}}whereyis the same exponentiation computed in Tonelli-Shanks sqrt (y = x^{(p-2^e-1)/(2^{e+1})}).Type of change
How has this been tested?
Current Sqrt tests pass.
How has this been benchmarked?
A few benchmarks on AWS
z1d.large:Fp2:Fp2:Fp2:Fp2andFp4:Checklist:
golangci-lintdoes not output errors locallyNote
Optimizes Fp2 (E2) Sqrt using new algorithms (p≡3 mod 4 and p≡1 mod 4), adds exported exponentiation helpers, updates codegen/templates, and refreshes tests/benchmarks; also introduces a combined SqrtAndInverse for bls12-377.
E2.Sqrtacross curves:ExpBySqrtPp1o4/ExpBySqrtPm3o4.bls12-377/fp.Element.SqrtAndInverseand internalexpByC1.ExpBySqrtExp,ExpBySqrtPp1o4,ExpBySqrtPm3o4,ExpBySqrtPm5o8,ExpByLegendreExp(replaceexpBy*).ExpBy…helpers (pp1o4/pm3o4/pm5o8) and updatedE2.Sqrttemplates.SqrtQ3Mod4Exponent2; wire addchain data accordingly.Sqrtin variousfp/frpackages to use new helpers; adjust comments.SqrtinFpfor q≡3 mod 4 to useExpBySqrtPp1o4.BenchmarkE2Sqrt/E4Sqrtto pre-square inputs and avoid aliasing.Written by Cursor Bugbot for commit d08787a. This will update automatically on new commits. Configure here.