diff --git a/babyjub/babyjub_test.go b/babyjub/babyjub_test.go index 8791d67..01f8589 100644 --- a/babyjub/babyjub_test.go +++ b/babyjub/babyjub_test.go @@ -3,8 +3,10 @@ package babyjub import ( "encoding/hex" "math/big" + "math/rand" "testing" + "github.com/iden3/go-iden3-crypto/constants" "github.com/iden3/go-iden3-crypto/utils" "github.com/stretchr/testify/assert" ) @@ -231,3 +233,74 @@ func TestCompressDecompressRnd(t *testing.T) { assert.Equal(t, p1, p2) } } + +func BenchmarkBabyjub(b *testing.B) { + const n = 256 + + rnd := rand.New(rand.NewSource(42)) + + var badpoints [n]*Point + for i := 0; i < n; i++ { + x := new(big.Int).Rand(rnd, constants.Q) + y := new(big.Int).Rand(rnd, constants.Q) + badpoints[i] = &Point{X: x, Y: y} + } + + var points [n]*Point + baseX := utils.NewIntFromString( + "17777552123799933955779906779655732241715742912184938656739573121738514868268") + baseY := utils.NewIntFromString( + "2626589144620713026669568689430873010625803728049924121243784502389097019475") + base := &Point{X: baseX, Y: baseY} + for i := 0; i < n; i++ { + s := new(big.Int).Rand(rnd, constants.Q) + points[i] = NewPoint().Mul(s, base) + } + + var scalars [n]*big.Int + for i := 0; i < n; i++ { + scalars[i] = new(big.Int).Rand(rnd, constants.Q) + } + + b.Run("AddConst", func(b *testing.B) { + p0 := &Point{X: big.NewInt(0), Y: big.NewInt(1)} + p1 := &Point{X: big.NewInt(0), Y: big.NewInt(1)} + + p2 := NewPoint() + for i := 0; i < b.N; i++ { + p2.Add(p0, p1) + } + }) + + b.Run("AddRnd", func(b *testing.B) { + res := NewPoint() + for i := 0; i < b.N; i++ { + res.Add(points[i%(n/2)], points[i%(n/2)+1]) + } + }) + + b.Run("MulRnd", func(b *testing.B) { + res := NewPoint() + for i := 0; i < b.N; i++ { + res.Mul(scalars[i%n], points[i%n]) + } + }) + + b.Run("Compress", func(b *testing.B) { + for i := 0; i < b.N; i++ { + points[i%n].Compress() + } + }) + + b.Run("InCurve", func(b *testing.B) { + for i := 0; i < b.N; i++ { + badpoints[i%n].InCurve() + } + }) + + b.Run("InSubGroup", func(b *testing.B) { + for i := 0; i < b.N; i++ { + points[i%n].InCurve() + } + }) +} diff --git a/mimc7/mimc7_test.go b/mimc7/mimc7_test.go index 4f0a4ef..990cefa 100644 --- a/mimc7/mimc7_test.go +++ b/mimc7/mimc7_test.go @@ -91,9 +91,7 @@ func BenchmarkMIMC7(b *testing.B) { b41 := big.NewInt(int64(41)) bigArray4 := []*big.Int{b12, b45, b78, b41} - var h4 *big.Int for i := 0; i < b.N; i++ { - h4, _ = Hash(bigArray4, nil) + Hash(bigArray4, nil) } - println(h4) } diff --git a/poseidon/poseidon_test.go b/poseidon/poseidon_test.go index c4e8581..60aa908 100644 --- a/poseidon/poseidon_test.go +++ b/poseidon/poseidon_test.go @@ -65,3 +65,15 @@ func TestPoseidon(t *testing.T) { assert.Nil(t, err) assert.Equal(t, "10747013384255785702102976082726575658403084163954725275481577373644732938016", hmsg2.String()) } + +func BenchmarkPoseidon(b *testing.B) { + b12 := big.NewInt(int64(12)) + b45 := big.NewInt(int64(45)) + b78 := big.NewInt(int64(78)) + b41 := big.NewInt(int64(41)) + bigArray4 := []*big.Int{b12, b45, b78, b41} + + for i := 0; i < b.N; i++ { + Hash(bigArray4) + } +}