You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB

  1. package prover
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. "testing"
  6. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  7. "time"
  8. "bytes"
  9. "fmt"
  10. )
  11. const (
  12. N = 50000
  13. )
  14. func randomBigIntArray(n int) []*big.Int{
  15. var p []*big.Int
  16. for i := 0; i < n; i++ {
  17. pi := randBI()
  18. p = append(p, pi)
  19. }
  20. return p
  21. }
  22. func randomG1Array(n int) []*bn256.G1 {
  23. arrayG1 := make([]*bn256.G1, n)
  24. for i:=0; i<n; i++ {
  25. _, arrayG1[i], _ = bn256.RandomG1(rand.Reader)
  26. }
  27. return arrayG1
  28. }
  29. func TestTable(t *testing.T){
  30. n := N
  31. // init scalar
  32. var arrayW = randomBigIntArray(N)
  33. // init G1 array
  34. var arrayG1 = randomG1Array(N)
  35. beforeT := time.Now()
  36. Q1 := new(bn256.G1).ScalarBaseMult(new(big.Int))
  37. for i:=0; i < n; i++ {
  38. Q1.Add(Q1, new(bn256.G1).ScalarMult(arrayG1[i], arrayW[i]))
  39. }
  40. fmt.Println("Std. Mult. time elapsed:", time.Since(beforeT))
  41. for gsize:=2; gsize < 10; gsize++ {
  42. ntables := int((n + gsize - 1) / gsize)
  43. table := make([]TableG1, ntables)
  44. for i:=0; i<ntables-1; i++ {
  45. table[i].NewTableG1( arrayG1[i*gsize:(i+1)*gsize], gsize)
  46. }
  47. table[ntables-1].NewTableG1( arrayG1[(ntables-1)*gsize:], gsize)
  48. beforeT = time.Now()
  49. Q2:= new(bn256.G1).ScalarBaseMult(new(big.Int))
  50. for i:=0; i<ntables-1; i++ {
  51. Q2.Add(Q2,table[i].MulTableG1(arrayW[i*gsize:(i+1)*gsize], gsize))
  52. }
  53. Q2.Add(Q2,table[ntables-1].MulTableG1(arrayW[(ntables-1)*gsize:], gsize))
  54. fmt.Printf("Gsize : %d, TMult time elapsed: %s\n", gsize,time.Since(beforeT))
  55. beforeT = time.Now()
  56. Q3 := ScalarMult(arrayG1, arrayW, gsize)
  57. fmt.Printf("Gsize : %d, TMult time elapsed (inc table comp): %s\n", gsize,time.Since(beforeT))
  58. beforeT = time.Now()
  59. Q4 := MulTableNoDoubleG1(table, arrayW, gsize)
  60. fmt.Printf("Gsize : %d, TMultNoDouble time elapsed: %s\n", gsize,time.Since(beforeT))
  61. beforeT = time.Now()
  62. Q5 := ScalarMultNoDoubleG1(arrayG1, arrayW, gsize)
  63. fmt.Printf("Gsize : %d, TMultNoDouble time elapsed (inc table comp): %s\n", gsize,time.Since(beforeT))
  64. if bytes.Compare(Q1.Marshal(),Q2.Marshal()) != 0 {
  65. t.Error("Error in TMult")
  66. }
  67. if bytes.Compare(Q1.Marshal(),Q3.Marshal()) != 0 {
  68. t.Error("Error in TMult with table comp")
  69. }
  70. if bytes.Compare(Q1.Marshal(),Q4.Marshal()) != 0 {
  71. t.Error("Error in TMultNoDouble")
  72. }
  73. if bytes.Compare(Q1.Marshal(),Q5.Marshal()) != 0 {
  74. t.Error("Error in TMultNoDoublee with table comp")
  75. }
  76. }
  77. }