Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bandersnatch/fp: use optimized sqrt #38

Merged
merged 3 commits into from
Apr 10, 2023

Conversation

jsign
Copy link
Collaborator

@jsign jsign commented Mar 23, 2023

This PR includes optimization for sqrt.

My approach here is to make the minimal changes from the Bandersnatch repo implementation so we can take advantage of the speedup as soon as possible.

Upstreaming this to gnark is a separate beast, in the sense that from what I saw it might require some refactors in their fft package to extract roots of unity and might not be entirely trivial. Still is a good idea to do it, but I don't want that to block our progress in the VKT speedups.

My strategy was to include this separate "faster" sqrt so that doesn't mess up with generated code from gnark, since that would be painful for us in the near future when we try to (attempt) to not depend on generated code and try using gnark directly. (Again, that's a different topic!)

For more context or info:

jsign added 3 commits March 23, 2023 10:18
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
Comment on lines +5 to +17
// The following code is _almost_ the original code from:
// https://github.com/GottfriedHerold/Bandersnatch/blob/f665f90b64892b9c4c89cff3219e70456bb431e5/bandersnatch/fieldElements/field_element_square_root.go
//
// We had to do some changes to make it work with gnark:
// - The type `feType_SquareRoot` was aliased to `Element` so everything looks the same. These types didn't have the exact
// same underlying representation, so it leaded to some minor adjustements. (e.g: accessing the limbs)
// - Original APIs regarding finite-field multiplications (e.g: MulEq) were adjusted to use gnark Mul APIs.
// - The original code had to explicitely do `Normalize()` after field element operations, but this isn't needed in gnark.
// - The primitive 2^32-root-of unity value (see init()) was pulled from gnark FFT domain code.
// - The original code used anonymous functions to define global vars, but we changed to use a init() function.
// This was required since we have other init() in the package that configure other globals (e.g: _modulus).
// By the way init() functions execution order works, we'll have these configured before the sqrt init() is called,
// compared with the original anonymous function global calls.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wrote a summary here of the work I did to adjust things to work for go-ipa.
Sorry if sounds a bit cryptic. LMK any questions.

@@ -0,0 +1,293 @@
package fp
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note this is a new file. We should try to avoid as much as possible "pollute" generated code to avoid headaches in the future.

Comment on lines +7 to +44
func TestCustomSqrt(t *testing.T) {
for i := 0; i < 10_000; i++ {
// Take a random fp.
var a Element
a.SetUint64(uint64(i))

// Calculate the square root with thew optimized algorithm.
var sqrtNew Element
if sqrtNew.SqrtPrecomp(&a) == nil {
continue // Doesn't exist? Skip.
}

// Recalculate the original element using the calculated sqrt.
var regenNew Element
regenNew.Mul(&sqrtNew, &sqrtNew)

// Check the obvious: regenNew should be equal to the original element.
if !regenNew.Equal(&a) {
t.Fatalf("regenNew != a for %d", i)
}

// Calculate the sqrt with the original gnark code.
var sqrtOld Element
sqrtOld.Sqrt(&a)
var regenOld Element
regenOld.Mul(&sqrtOld, &sqrtOld)
if !regenOld.Equal(&a) { // Somewhat obvious, but still.
t.Fatalf("regenOld != a for %d", i)
}

// Check that both sqrt's are equal, *considering* the case that they have opposite signs.
// We need to do that since both algorithm can return either the positive or negative sqrt,
// which is fine.
if !sqrtNew.Equal(&sqrtOld) && !sqrtNew.Neg(&sqrtNew).Equal(&sqrtOld) {
t.Fatalf("sqrtNew != sqrtOld for %d", i)
}
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wrote this extra test while I was integrating the work and double checking it was working correctly with the original implementation (that still is available).

is_nil := y.Sqrt(&y)
is_nil := y.SqrtPrecomp(&y)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For computeY use the faster implementation now.
This is the only line change in generated files code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Open the file diff and see a comment! (Mentioning it since can be hidden since is marked as generated)

@jsign jsign marked this pull request as ready for review March 23, 2023 15:42
@jsign
Copy link
Collaborator Author

jsign commented Apr 6, 2023

@kevaundray, is this PR mergeable?

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