Skip to content

fix: Eisenstein Half-GCD convergence#680

Merged
ivokub merged 4 commits intoConsensys:masterfrom
feltroidprime:fix-eisenstein-gcd
May 2, 2025
Merged

fix: Eisenstein Half-GCD convergence#680
ivokub merged 4 commits intoConsensys:masterfrom
feltroidprime:fix-eisenstein-gcd

Conversation

@feltroidprime
Copy link
Copy Markdown
Contributor

No description provided.

@feltroidprime feltroidprime changed the title Fix Eisentein Half-GCD convergence Fix Eisenstein Half-GCD convergence Apr 30, 2025
@feltroidprime feltroidprime changed the title Fix Eisenstein Half-GCD convergence fix: Eisenstein Half-GCD convergence Apr 30, 2025
@gbotrel gbotrel requested a review from yelhousni April 30, 2025 13:15
@ivokub ivokub self-requested a review April 30, 2025 13:26
Copy link
Copy Markdown
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

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

Looks good to me - maybe we could get rid of the conditional "best-norm remainder" loop and do it always?

Otherwise - if there is good reason not to do it, then I'm good to merge it. I also added unit test for quorem. I wasn't able to come up with a regression test quickly though for inputs where the remainder norm would be bigger than the divisor - do you have a test-case by any chance? Its fine if not.

@feltroidprime
Copy link
Copy Markdown
Contributor Author

feltroidprime commented May 1, 2025

Looks good to me - maybe we could get rid of the conditional "best-norm remainder" loop and do it always?

Otherwise - if there is good reason not to do it, then I'm good to merge it. I also added unit test for quorem. I wasn't able to come up with a regression test quickly though for inputs where the remainder norm would be bigger than the divisor - do you have a test-case by any chance? Its fine if not.

Hey one example of (slow/infinite) convergence input for half gcd was (from halfgcd hint with (order - 2) as scalar for secpk1) :
200k iterations in the half gcd is not enough for it (i dont know if it converges at all)

a = 64502973549206556628585045361533709077 - 303414439467246543595250775667605759171*ω
b = -432420386565659656852420866390673177323 + 238911465918039986966665730306072050094*ω

Regarding the while loop, I think I agree, it does seem one pass through all neighbours is enough the make the norm smaller and we could replace it with a if r.Norm().Cmp(norm) >= 0 instead of for r.Norm().Cmp(norm) >= 0

Regarding passing through all neighbors each time, i think the initial guess will be correct most of the time and therefore it's a small optimization to not do it - but i didn't benchmarked it.

@ivokub
Copy link
Copy Markdown
Collaborator

ivokub commented May 2, 2025

I benchmarked locally quickly - when we always find the smallest neighbour in the QuoRem.

goos: linux
goarch: amd64
pkg: github.com/consensys/gnark-crypto/field/eisenstein
cpu: AMD Ryzen 9 7940HS w/ Radeon 780M Graphics     
           │   old.txt   │                new.txt                │
           │   sec/op    │    sec/op     vs base                 │
HalfGCD-16   73.64µ ± 6%   209.48µ ± 4%  +184.48% (p=0.000 n=20)
QuoRem-16    3.095µ ± 1%    6.668µ ± 1%  +115.48% (p=0.000 n=20)
geomean      15.10µ         37.37µ       +147.59%

           │   old.txt    │                new.txt                 │
           │     B/op     │     B/op       vs base                 │
HalfGCD-16   87.02Ki ± 5%   253.11Ki ± 5%  +190.85% (p=0.000 n=20)
QuoRem-16    2.157Ki ± 0%    6.415Ki ± 0%  +197.37% (p=0.000 n=20)
geomean      13.70Ki         40.29Ki       +194.10%

           │   old.txt   │               new.txt                │
           │  allocs/op  │  allocs/op   vs base                 │
HalfGCD-16   1.792k ± 5%   5.694k ± 5%  +217.81% (p=0.000 n=20)
QuoRem-16     47.00 ± 0%   145.00 ± 0%  +208.51% (p=0.000 n=20)
geomean       290.2         908.6       +213.12%

I guess lets stay with only finding smallest neighbour in case the norm is big (i.e. what you have right now). I only changed from for loop to if.

Copy link
Copy Markdown
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

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

Thanks for the fix. Looks good!

@ivokub ivokub merged commit 5660088 into Consensys:master May 2, 2025
3 checks passed
ivokub added a commit that referenced this pull request May 15, 2025
Co-authored-by: Ivo Kubjas <ivo.kubjas@consensys.net>
ivokub added a commit that referenced this pull request Oct 6, 2025
Co-authored-by: Ivo Kubjas <ivo.kubjas@consensys.net>
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