Skip to content

perf: Legendre using Pornin20#704

Merged
Tabaie merged 13 commits intomasterfrom
perf/legendre-p20-standalone
Jun 26, 2025
Merged

perf: Legendre using Pornin20#704
Tabaie merged 13 commits intomasterfrom
perf/legendre-p20-standalone

Conversation

@Tabaie
Copy link
Copy Markdown
Contributor

@Tabaie Tabaie commented Jun 26, 2025

This PR implements Legendre symbol calculation based on the Binary GCD algorithm.
For large fields, the approximation approach from https://eprint.iacr.org/2020/972 is used to achieve about 50% additional speedup over "vanilla" Binary GCD. (Benchmarked over the base field of BLS12-381 https://github.com/Consensys/gnark-crypto/blob/perf/legendre-symbol-p20/ecc/bls12-381/fp/element_test.go#L2954)

@Tabaie Tabaie requested a review from Copilot June 26, 2025 00:28

This comment was marked as outdated.

@Tabaie Tabaie requested a review from Copilot June 26, 2025 02:09

This comment was marked as outdated.

@Tabaie Tabaie requested a review from Copilot June 26, 2025 02:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the exponentiation-based Legendre symbol calculation with an optimized binary GCD algorithm (Pornin20) across all field and curve implementations, adds a zero-value test case, and updates the code generator and configuration to support the new approach.

  • Implement the Pornin20 binary GCD method for Legendre() in all element.go
  • Add require.Equal(t, 0, new(Element).Legendre(), "(0|q) must be zero") to all element_test.go
  • Update generator templates (tests.go, sqrt.go, inverse.go, base.go) and config (field_config.go) to support the new algorithm

Reviewed Changes

Copilot reviewed 51 out of 51 changed files in this pull request and generated no comments.

Show a summary per file
File Description
field/koalabear/element.go Replace exponentiation with Pornin20 binary GCD algorithm
field/koalabear/element_test.go Add zero-case Legendre test
field/goldilocks/element.go Same as above
field/goldilocks/element_test.go Same as above
field/babybear/element.go Same as above
field/babybear/element_test.go Same as above
ecc/stark-curve/fr/element.go Same as above for ECC curve
ecc/stark-curve/fr/element_test.go Same as above
ecc/stark-curve/fp/element.go Same as above for FP
ecc/stark-curve/fp/element_test.go Same as above
ecc/secp256k1/... Same as above for secp256k1
ecc/grumpkin/... Same as above for grumpkin
... (all other curve backends) Same as above
field/generator/internal/templates/element/tests.go Add zero-case test in generator
field/generator/internal/templates/element/sqrt.go Inject Pornin20 Legendre in template
field/generator/internal/templates/element/inverse.go Fix approximate mask constant
field/generator/internal/templates/element/base.go Add missing period to comment
field/generator/config/field_config.go Add new slice NbWordsIndexesNoZeroNoLast and fix ranges
Comments suppressed due to low confidence (1)

field/generator/config/field_config.go:200

  • The loop for i := range F.NbWords attempts to range over an integer. It should iterate over the slice F.NbWordsIndexesFull, for example:
for i := range F.NbWordsIndexesFull {
    F.NbWordsIndexesFull[i] = i
}
	for i := range F.NbWords {

@Tabaie Tabaie marked this pull request as ready for review June 26, 2025 02:30
@Tabaie Tabaie requested a review from yelhousni June 26, 2025 02:30
Copy link
Copy Markdown
Collaborator

@yelhousni yelhousni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR and great documentation!

I would just remove expByLegendreExp from element_exp.go or maybe keep it and add a test against Legendre() to check that the GCD approach and Euler's criterion output the same result (the test with big.Jacobi also uses GCD).

@Tabaie
Copy link
Copy Markdown
Contributor Author

Tabaie commented Jun 26, 2025

Great PR and great documentation!

I would just remove expByLegendreExp from element_exp.go or maybe keep it and add a test against Legendre() to check that the GCD approach and Euler's criterion output the same result (the test with big.Jacobi also uses GCD).

Thank you! Since it was a lot of code I opted for deleting.

@Tabaie Tabaie merged commit 2b9835b into master Jun 26, 2025
6 checks passed
@Tabaie Tabaie deleted the perf/legendre-p20-standalone branch June 26, 2025 21:00
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.

3 participants