package bn256

/*
Benchmark Result:

goos: darwin
goarch: arm64
pkg: xdx.jelly/xgcl/sm/sm9/internal/bn256
BenchmarkGFpAdd
BenchmarkGFpAdd-10       	469129488	         2.532 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFpMul
BenchmarkGFpMul-10       	71532715	        16.08 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFp2Add
BenchmarkGFp2Add-10      	201261476	         5.944 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFp2Mul
BenchmarkGFp2Mul-10      	15537591	        74.29 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFp6Add
BenchmarkGFp6Add-10      	61707891	        19.06 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFp6Mul
BenchmarkGFp6Mul-10      	 2176302	       551.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFp12Add
BenchmarkGFp12Add-10     	29860556	        39.36 ns/op	       0 B/op	       0 allocs/op
BenchmarkGFp12Mul
BenchmarkGFp12Mul-10     	  517333	      2276 ns/op	       0 B/op	       0 allocs/op
BenchmarkPairing
BenchmarkPairing-10      	    1660	    712120 ns/op	       0 B/op	       0 allocs/op
BenchmarkG1BaseMul
BenchmarkG1BaseMul-10    	   78440	     15443 ns/op	       0 B/op	       0 allocs/op
BenchmarkG2BaseMul
BenchmarkG2BaseMul-10    	   22156	     53968 ns/op	       0 B/op	       0 allocs/op
BenchmarkGTBaseMul
BenchmarkGTBaseMul-10    	   22365	     53395 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	xdx.jelly/xgcl/sm/sm9/internal/bn256	17.212s

*/

import (
	"crypto/rand"
	"testing"

	"xdx.jelly/xgcl/grand"
)

func BenchmarkGFpAdd(b *testing.B) {
	x := &gfP{}
	y := &gfP{}
	z := &gfP{}
	x.random(grand.Reader)
	y.random(grand.Reader)
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		gfpAdd(z, x, y)
	}
}

func BenchmarkGFpMul(b *testing.B) {
	x := gfPFromBigInt(randomInt(rand.Reader, P))
	y := gfPFromBigInt(randomInt(rand.Reader, P))
	z := &gfP{}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		gfpMul(z, x, y)
	}
}

func BenchmarkGFp2Add(b *testing.B) {
	x := &gfP2{}
	y := &gfP2{}
	z := &gfP2{}
	x.random(grand.Reader)
	y.random(grand.Reader)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		z.Add(x, y)
	}
}

func BenchmarkGFp2Mul(b *testing.B) {
	x := &gfP2{}
	y := &gfP2{}
	z := &gfP2{}
	x.random(grand.Reader)
	y.random(grand.Reader)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		z.Mul(x, y)
	}
}

func BenchmarkGFp6Add(b *testing.B) {
	x := &gfP6{}
	y := &gfP6{}
	z := &gfP6{}
	x.random(grand.Reader)
	y.random(grand.Reader)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		z.Add(x, y)
	}
}

func BenchmarkGFp6Mul(b *testing.B) {
	x := &gfP6{}
	y := &gfP6{}
	z := &gfP6{}
	x.random(grand.Reader)
	y.random(grand.Reader)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		z.Mul(x, y)
	}
}

func BenchmarkGFp12Add(b *testing.B) {
	x := &gfP12{}
	y := &gfP12{}
	z := &gfP12{}
	x.random(grand.Reader)
	y.random(grand.Reader)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		z.Add(x, y)
	}
}

func BenchmarkGFp12Mul(b *testing.B) {
	x := &gfP12{}
	y := &gfP12{}
	z := &gfP12{}
	x.random(grand.Reader)
	y.random(grand.Reader)

	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		z.Mul(x, y)
	}
}

func BenchmarkPairing(b *testing.B) {
	e := &GT{}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		PairLol(e, &G1{*curveGen}, &G2{*twistGen})
	}
}

func BenchmarkG1BaseMul(b *testing.B) {
	x, _ := rand.Int(rand.Reader, N)
	g := new(G1)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		g.ScalarBaseMult(x)
	}
}

func BenchmarkG2BaseMul(b *testing.B) {
	x, _ := rand.Int(rand.Reader, N)
	g := new(G2)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		g.ScalarBaseMult(x)
	}
}

func BenchmarkGTBaseMul(b *testing.B) {
	x, _ := rand.Int(rand.Reader, N)
	g := new(G2)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		g.ScalarBaseMult(x)
	}
}
