mirror of
https://github.com/luxfi/go-ipa.git
synced 2026-10-30 16:24:04 +00:00
* bandersnatch/fp: use Fp directly from gnark Signed-off-by: Ignacio Hagopian <[email protected]> * banderwagon: add precomp MSM benchmark Signed-off-by: Ignacio Hagopian <[email protected]> * mod: update to gnar-crypto v0.11.1 Signed-off-by: Ignacio Hagopian <[email protected]> * rebase and update to [email protected].2 Signed-off-by: Ignacio Hagopian <[email protected]> * mod: bump go min version to v1.19 Signed-off-by: Ignacio Hagopian <[email protected]> * mod: bump go min version to v1.18 Signed-off-by: Ignacio Hagopian <[email protected]> * fix comments Signed-off-by: Ignacio Hagopian <[email protected]> * fixes Signed-off-by: Ignacio Hagopian <[email protected]> --------- Signed-off-by: Ignacio Hagopian <[email protected]>
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package fp
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
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.
|
|
sqrtNew := SqrtPrecomp(&a)
|
|
if sqrtNew == 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)
|
|
}
|
|
}
|
|
}
|