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.

194 lines
4.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package zk
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. "github.com/arnaucube/go-snark/bn128"
  7. "github.com/arnaucube/go-snark/fields"
  8. )
  9. type Setup struct {
  10. Toxic struct {
  11. T *big.Int // trusted setup secret
  12. Ka *big.Int // prover
  13. Kb *big.Int // prover
  14. Kc *big.Int // prover
  15. }
  16. // public
  17. G1T [][3]*big.Int // t encrypted in G1 curve
  18. G2T [][3][2]*big.Int // t encrypted in G2 curve
  19. }
  20. type Proof struct {
  21. PiA [3]*big.Int
  22. PiAp [3]*big.Int
  23. PiB [3][2]*big.Int
  24. PiBp [3]*big.Int
  25. PiC [3]*big.Int
  26. PiCp [3]*big.Int
  27. PiH [3]*big.Int
  28. Va [3][2]*big.Int
  29. Vb [3]*big.Int
  30. Vc [3][2]*big.Int
  31. Vz [3][2]*big.Int
  32. }
  33. const bits = 512
  34. func GenerateTrustedSetup(bn bn128.Bn128, pollength int) (Setup, error) {
  35. var setup Setup
  36. var err error
  37. // generate random t value
  38. setup.Toxic.T, err = rand.Prime(rand.Reader, bits)
  39. if err != nil {
  40. return Setup{}, err
  41. }
  42. fmt.Print("trusted t: ")
  43. fmt.Println(setup.Toxic.T)
  44. // encrypt t values with curve generators
  45. var gt1 [][3]*big.Int
  46. var gt2 [][3][2]*big.Int
  47. for i := 0; i < pollength; i++ {
  48. tPow := bn.Fq1.Exp(setup.Toxic.T, big.NewInt(int64(i)))
  49. tEncr1 := bn.G1.MulScalar(bn.G1.G, tPow)
  50. gt1 = append(gt1, tEncr1)
  51. tEncr2 := bn.G2.MulScalar(bn.G2.G, tPow)
  52. gt2 = append(gt2, tEncr2)
  53. }
  54. // gt1: g1, g1*t, g1*t^2, g1*t^3, ...
  55. // gt2: g2, g2*t, g2*t^2, ...
  56. setup.G1T = gt1
  57. setup.G2T = gt2
  58. return setup, nil
  59. }
  60. func GenerateProofs(bn bn128.Bn128, f fields.Fq, setup Setup, ax, bx, cx, hx, zx []*big.Int) (Proof, error) {
  61. var proof Proof
  62. var err error
  63. // k for calculating pi' and Vk
  64. setup.Toxic.Ka, err = rand.Prime(rand.Reader, bits)
  65. if err != nil {
  66. return Proof{}, err
  67. }
  68. setup.Toxic.Kb, err = rand.Prime(rand.Reader, bits)
  69. if err != nil {
  70. return Proof{}, err
  71. }
  72. setup.Toxic.Kc, err = rand.Prime(rand.Reader, bits)
  73. if err != nil {
  74. return Proof{}, err
  75. }
  76. // g1*A(t)
  77. proof.PiA = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  78. for i := 0; i < len(ax); i++ {
  79. m := bn.G1.MulScalar(setup.G1T[i], ax[i])
  80. proof.PiA = bn.G1.Add(proof.PiA, m)
  81. }
  82. proof.PiAp = bn.G1.MulScalar(proof.PiA, setup.Toxic.Ka)
  83. // g2*B(t)
  84. proof.PiB = bn.Fq6.Zero()
  85. // g1*B(t)
  86. pib1 := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  87. for i := 0; i < len(bx); i++ {
  88. m := bn.G2.MulScalar(setup.G2T[i], bx[i])
  89. proof.PiB = bn.G2.Add(proof.PiB, m)
  90. m1 := bn.G1.MulScalar(setup.G1T[i], bx[i])
  91. pib1 = bn.G1.Add(pib1, m1)
  92. }
  93. proof.PiBp = bn.G1.MulScalar(pib1, setup.Toxic.Kb)
  94. // g1*C(t)
  95. proof.PiC = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  96. for i := 0; i < len(cx); i++ {
  97. m := bn.G1.MulScalar(setup.G1T[i], cx[i])
  98. proof.PiC = bn.G1.Add(proof.PiC, m)
  99. }
  100. proof.PiCp = bn.G1.MulScalar(proof.PiC, setup.Toxic.Kc)
  101. // g1*H(t)
  102. proof.PiH = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  103. for i := 0; i < len(hx); i++ {
  104. m := bn.G1.MulScalar(setup.G1T[i], hx[i])
  105. proof.PiH = bn.G1.Add(proof.PiH, m)
  106. }
  107. g2Zt := bn.Fq6.Zero()
  108. for i := 0; i < len(bx); i++ {
  109. m := bn.G2.MulScalar(setup.G2T[i], zx[i])
  110. g2Zt = bn.G2.Add(g2Zt, m)
  111. }
  112. proof.Vz = g2Zt
  113. proof.Va = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Ka)
  114. proof.Vb = bn.G1.MulScalar(bn.G1.G, setup.Toxic.Kb)
  115. proof.Vc = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Kc)
  116. return proof, nil
  117. }
  118. func VerifyProof(bn bn128.Bn128, setup Setup, proof Proof) bool {
  119. // e(piA, Va) == e(piA', g2)
  120. pairingPiaVa, err := bn.Pairing(proof.PiA, proof.Va)
  121. if err != nil {
  122. return false
  123. }
  124. pairingPiapG2, err := bn.Pairing(proof.PiAp, bn.G2.G)
  125. if err != nil {
  126. return false
  127. }
  128. if !bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  129. return false
  130. }
  131. // e(Vb, piB) == e(piB', g2)
  132. pairingVbPib, err := bn.Pairing(proof.Vb, proof.PiB)
  133. if err != nil {
  134. return false
  135. }
  136. pairingPibpG2, err := bn.Pairing(proof.PiBp, bn.G2.G)
  137. if err != nil {
  138. return false
  139. }
  140. if !bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  141. return false
  142. }
  143. // e(piC, Vc) == e(piC', g2)
  144. pairingPicVc, err := bn.Pairing(proof.PiC, proof.Vc)
  145. if err != nil {
  146. return false
  147. }
  148. pairingPicpG2, err := bn.Pairing(proof.PiCp, bn.G2.G)
  149. if err != nil {
  150. return false
  151. }
  152. if !bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  153. return false
  154. }
  155. // e(piA, piB) == e(piH, Vz) * e(piC, g2)
  156. // pairingPiaPib, err := bn.Pairing(proof.PiA, proof.PiB)
  157. // if err != nil {
  158. // return false
  159. // }
  160. // pairingPihVz, err := bn.Pairing(proof.PiH, proof.Vz)
  161. // if err != nil {
  162. // return false
  163. // }
  164. // pairingPicG2, err := bn.Pairing(proof.PiC, bn.G2.G)
  165. // if err != nil {
  166. // return false
  167. // }
  168. // if !bn.Fq12.Equal(pairingPiaPib, bn.Fq12.Mul(pairingPihVz, pairingPicG2)) {
  169. // return false
  170. // }
  171. return true
  172. }