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.

166 lines
5.0 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. N1 = 500
  13. N2 = 500
  14. )
  15. func randomBigIntArray(n int) []*big.Int{
  16. var p []*big.Int
  17. for i := 0; i < n; i++ {
  18. pi := randBI()
  19. p = append(p, pi)
  20. }
  21. return p
  22. }
  23. func randomG1Array(n int) []*bn256.G1 {
  24. arrayG1 := make([]*bn256.G1, n)
  25. for i:=0; i<n; i++ {
  26. _, arrayG1[i], _ = bn256.RandomG1(rand.Reader)
  27. }
  28. return arrayG1
  29. }
  30. func randomG2Array(n int) []*bn256.G2 {
  31. arrayG2 := make([]*bn256.G2, n)
  32. for i:=0; i<n; i++ {
  33. _, arrayG2[i], _ = bn256.RandomG2(rand.Reader)
  34. }
  35. return arrayG2
  36. }
  37. func TestTableG1(t *testing.T){
  38. n := N1
  39. // init scalar
  40. var arrayW = randomBigIntArray(n)
  41. // init G1 array
  42. var arrayG1 = randomG1Array(n)
  43. beforeT := time.Now()
  44. Q1 := new(bn256.G1).ScalarBaseMult(new(big.Int))
  45. for i:=0; i < n; i++ {
  46. Q1.Add(Q1, new(bn256.G1).ScalarMult(arrayG1[i], arrayW[i]))
  47. }
  48. fmt.Println("Std. Mult. time elapsed:", time.Since(beforeT))
  49. for gsize:=2; gsize < 10; gsize++ {
  50. ntables := int((n + gsize - 1) / gsize)
  51. table := make([]TableG1, ntables)
  52. for i:=0; i<ntables-1; i++ {
  53. table[i].NewTableG1( arrayG1[i*gsize:(i+1)*gsize], gsize)
  54. }
  55. table[ntables-1].NewTableG1( arrayG1[(ntables-1)*gsize:], gsize)
  56. beforeT = time.Now()
  57. Q2:= new(bn256.G1).ScalarBaseMult(new(big.Int))
  58. for i:=0; i<ntables-1; i++ {
  59. Q2 = table[i].MulTableG1(arrayW[i*gsize:(i+1)*gsize], Q2, gsize)
  60. }
  61. Q2 = table[ntables-1].MulTableG1(arrayW[(ntables-1)*gsize:], Q2, gsize)
  62. fmt.Printf("Gsize : %d, TMult time elapsed: %s\n", gsize,time.Since(beforeT))
  63. beforeT = time.Now()
  64. Q3 := ScalarMultG1(arrayG1, arrayW, nil, gsize)
  65. fmt.Printf("Gsize : %d, TMult time elapsed (inc table comp): %s\n", gsize,time.Since(beforeT))
  66. beforeT = time.Now()
  67. Q4 := MulTableNoDoubleG1(table, arrayW, nil, gsize)
  68. fmt.Printf("Gsize : %d, TMultNoDouble time elapsed: %s\n", gsize,time.Since(beforeT))
  69. beforeT = time.Now()
  70. Q5 := ScalarMultNoDoubleG1(arrayG1, arrayW, nil, gsize)
  71. fmt.Printf("Gsize : %d, TMultNoDouble time elapsed (inc table comp): %s\n", gsize,time.Since(beforeT))
  72. if bytes.Compare(Q1.Marshal(),Q2.Marshal()) != 0 {
  73. t.Error("Error in TMult")
  74. }
  75. if bytes.Compare(Q1.Marshal(),Q3.Marshal()) != 0 {
  76. t.Error("Error in TMult with table comp")
  77. }
  78. if bytes.Compare(Q1.Marshal(),Q4.Marshal()) != 0 {
  79. t.Error("Error in TMultNoDouble")
  80. }
  81. if bytes.Compare(Q1.Marshal(),Q5.Marshal()) != 0 {
  82. t.Error("Error in TMultNoDoublee with table comp")
  83. }
  84. }
  85. }
  86. func TestTableG2(t *testing.T){
  87. n := N2
  88. // init scalar
  89. var arrayW = randomBigIntArray(n)
  90. // init G2 array
  91. var arrayG2 = randomG2Array(n)
  92. beforeT := time.Now()
  93. Q1 := new(bn256.G2).ScalarBaseMult(new(big.Int))
  94. for i:=0; i < n; i++ {
  95. Q1.Add(Q1, new(bn256.G2).ScalarMult(arrayG2[i], arrayW[i]))
  96. }
  97. fmt.Println("Std. Mult. time elapsed:", time.Since(beforeT))
  98. for gsize:=2; gsize < 10; gsize++ {
  99. ntables := int((n + gsize - 1) / gsize)
  100. table := make([]TableG2, ntables)
  101. for i:=0; i<ntables-1; i++ {
  102. table[i].NewTableG2( arrayG2[i*gsize:(i+1)*gsize], gsize)
  103. }
  104. table[ntables-1].NewTableG2( arrayG2[(ntables-1)*gsize:], gsize)
  105. beforeT = time.Now()
  106. Q2:= new(bn256.G2).ScalarBaseMult(new(big.Int))
  107. for i:=0; i<ntables-1; i++ {
  108. Q2 =table[i].MulTableG2(arrayW[i*gsize:(i+1)*gsize], Q2, gsize)
  109. }
  110. Q2 = table[ntables-1].MulTableG2(arrayW[(ntables-1)*gsize:], Q2, gsize)
  111. fmt.Printf("Gsize : %d, TMult time elapsed: %s\n", gsize,time.Since(beforeT))
  112. beforeT = time.Now()
  113. Q3 := ScalarMultG2(arrayG2, arrayW, nil, gsize)
  114. fmt.Printf("Gsize : %d, TMult time elapsed (inc table comp): %s\n", gsize,time.Since(beforeT))
  115. beforeT = time.Now()
  116. Q4 := MulTableNoDoubleG2(table, arrayW, nil, gsize)
  117. fmt.Printf("Gsize : %d, TMultNoDouble time elapsed: %s\n", gsize,time.Since(beforeT))
  118. beforeT = time.Now()
  119. Q5 := ScalarMultNoDoubleG2(arrayG2, arrayW, nil, gsize)
  120. fmt.Printf("Gsize : %d, TMultNoDouble time elapsed (inc table comp): %s\n", gsize,time.Since(beforeT))
  121. if bytes.Compare(Q1.Marshal(),Q2.Marshal()) != 0 {
  122. t.Error("Error in TMult")
  123. }
  124. if bytes.Compare(Q1.Marshal(),Q3.Marshal()) != 0 {
  125. t.Error("Error in TMult with table comp")
  126. }
  127. if bytes.Compare(Q1.Marshal(),Q4.Marshal()) != 0 {
  128. t.Error("Error in TMultNoDouble")
  129. }
  130. if bytes.Compare(Q1.Marshal(),Q5.Marshal()) != 0 {
  131. t.Error("Error in TMultNoDoublee with table comp")
  132. }
  133. }
  134. }