Browse Source

Add poseidon and babyjub benchmarks

fix/issue-9
Eduard S 5 years ago
parent
commit
a5b6afcb16
3 changed files with 86 additions and 3 deletions
  1. +73
    -0
      babyjub/babyjub_test.go
  2. +1
    -3
      mimc7/mimc7_test.go
  3. +12
    -0
      poseidon/poseidon_test.go

+ 73
- 0
babyjub/babyjub_test.go

@ -3,8 +3,10 @@ package babyjub
import ( import (
"encoding/hex" "encoding/hex"
"math/big" "math/big"
"math/rand"
"testing" "testing"
"github.com/iden3/go-iden3-crypto/constants"
"github.com/iden3/go-iden3-crypto/utils" "github.com/iden3/go-iden3-crypto/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -231,3 +233,74 @@ func TestCompressDecompressRnd(t *testing.T) {
assert.Equal(t, p1, p2) 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()
}
})
}

+ 1
- 3
mimc7/mimc7_test.go

@ -91,9 +91,7 @@ func BenchmarkMIMC7(b *testing.B) {
b41 := big.NewInt(int64(41)) b41 := big.NewInt(int64(41))
bigArray4 := []*big.Int{b12, b45, b78, b41} bigArray4 := []*big.Int{b12, b45, b78, b41}
var h4 *big.Int
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
h4, _ = Hash(bigArray4, nil)
Hash(bigArray4, nil)
} }
println(h4)
} }

+ 12
- 0
poseidon/poseidon_test.go

@ -65,3 +65,15 @@ func TestPoseidon(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, "10747013384255785702102976082726575658403084163954725275481577373644732938016", hmsg2.String()) 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)
}
}

Loading…
Cancel
Save