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.

249 lines
6.2 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
  1. package snark
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. "github.com/arnaucube/go-snark/bn128"
  6. "github.com/arnaucube/go-snark/fields"
  7. )
  8. type Setup struct {
  9. Toxic struct {
  10. T *big.Int // trusted setup secret
  11. Ka *big.Int // prover
  12. Kb *big.Int // prover
  13. Kc *big.Int // prover
  14. Kbeta *big.Int
  15. Kgamma *big.Int
  16. RhoA *big.Int
  17. RhoB *big.Int
  18. RhoC *big.Int
  19. }
  20. // public
  21. G1T [][3]*big.Int // t encrypted in G1 curve
  22. G2T [][3][2]*big.Int // t encrypted in G2 curve
  23. G1Kbg [3]*big.Int // g1 * Kbeta * Kgamma
  24. G2Kbg [3][2]*big.Int // g2 * Kbeta * Kgamma
  25. G2Kg [3][2]*big.Int // g2 * Kgamma
  26. }
  27. type Proof struct {
  28. PiA [3]*big.Int
  29. PiAp [3]*big.Int
  30. PiB [3][2]*big.Int
  31. PiBp [3]*big.Int
  32. PiC [3]*big.Int
  33. PiCp [3]*big.Int
  34. PiH [3]*big.Int
  35. PiK [3]*big.Int
  36. Va [3][2]*big.Int
  37. Vb [3]*big.Int
  38. Vc [3][2]*big.Int
  39. Vz [3][2]*big.Int
  40. }
  41. const bits = 512
  42. func GenerateTrustedSetup(bn bn128.Bn128, polLength int) (Setup, error) {
  43. var setup Setup
  44. var err error
  45. // generate random t value
  46. setup.Toxic.T, err = rand.Prime(rand.Reader, bits)
  47. if err != nil {
  48. return Setup{}, err
  49. }
  50. // k for calculating pi' and Vk
  51. setup.Toxic.Ka, err = rand.Prime(rand.Reader, bits)
  52. if err != nil {
  53. return Setup{}, err
  54. }
  55. setup.Toxic.Kb, err = rand.Prime(rand.Reader, bits)
  56. if err != nil {
  57. return Setup{}, err
  58. }
  59. setup.Toxic.Kc, err = rand.Prime(rand.Reader, bits)
  60. if err != nil {
  61. return Setup{}, err
  62. }
  63. // generate Kβ (Kbeta) and Kγ (Kgamma)
  64. setup.Toxic.Kbeta, err = rand.Prime(rand.Reader, bits)
  65. if err != nil {
  66. return Setup{}, err
  67. }
  68. setup.Toxic.Kgamma, err = rand.Prime(rand.Reader, bits)
  69. if err != nil {
  70. return Setup{}, err
  71. }
  72. // generate ρ (Rho): ρA, ρB, ρC
  73. setup.Toxic.RhoA, err = rand.Prime(rand.Reader, bits)
  74. if err != nil {
  75. return Setup{}, err
  76. }
  77. setup.Toxic.RhoB, err = rand.Prime(rand.Reader, bits)
  78. if err != nil {
  79. return Setup{}, err
  80. }
  81. setup.Toxic.RhoC = bn.Fq1.Mul(setup.Toxic.RhoA, setup.Toxic.RhoB)
  82. // encrypt t values with curve generators
  83. var gt1 [][3]*big.Int
  84. var gt2 [][3][2]*big.Int
  85. for i := 0; i < polLength; i++ {
  86. tPow := bn.Fq1.Exp(setup.Toxic.T, big.NewInt(int64(i)))
  87. tEncr1 := bn.G1.MulScalar(bn.G1.G, tPow)
  88. gt1 = append(gt1, tEncr1)
  89. tEncr2 := bn.G2.MulScalar(bn.G2.G, tPow)
  90. gt2 = append(gt2, tEncr2)
  91. }
  92. // gt1: g1, g1*t, g1*t^2, g1*t^3, ...
  93. // gt2: g2, g2*t, g2*t^2, ...
  94. setup.G1T = gt1
  95. setup.G2T = gt2
  96. /*
  97. Verification keys:
  98. - Vk_betagamma1: setup.G1Kbg = g1 * Kbeta*Kgamma
  99. - Vk_betagamma2: setup.G2Kbg = g2 * Kbeta*Kgamma
  100. - Vk_gamma: setup.G2Kg = g2 * Kgamma
  101. */
  102. kbg := bn.Fq1.Mul(setup.Toxic.Kbeta, setup.Toxic.Kgamma)
  103. setup.G1Kbg = bn.G1.MulScalar(bn.G1.G, kbg)
  104. setup.G2Kbg = bn.G2.MulScalar(bn.G2.G, kbg)
  105. setup.G2Kg = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Kgamma)
  106. return setup, nil
  107. }
  108. func GenerateProofs(bn bn128.Bn128, f fields.Fq, setup Setup, w, ax, bx, cx, hx, zx []*big.Int) (Proof, error) {
  109. var proof Proof
  110. // g1*A(t)
  111. proof.PiA = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  112. for i := 0; i < len(ax); i++ {
  113. m := bn.G1.MulScalar(setup.G1T[i], ax[i])
  114. proof.PiA = bn.G1.Add(proof.PiA, m)
  115. }
  116. proof.PiAp = bn.G1.MulScalar(proof.PiA, setup.Toxic.Ka) // move this in the setup step
  117. // g2*B(t)
  118. proof.PiB = bn.Fq6.Zero()
  119. // g1*B(t)
  120. pib1 := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  121. for i := 0; i < len(bx); i++ {
  122. m := bn.G2.MulScalar(setup.G2T[i], bx[i])
  123. proof.PiB = bn.G2.Add(proof.PiB, m)
  124. m1 := bn.G1.MulScalar(setup.G1T[i], bx[i])
  125. pib1 = bn.G1.Add(pib1, m1)
  126. }
  127. proof.PiBp = bn.G1.MulScalar(pib1, setup.Toxic.Kb) // this in the setup step
  128. // g1*C(t)
  129. proof.PiC = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  130. for i := 0; i < len(cx); i++ {
  131. m := bn.G1.MulScalar(setup.G1T[i], cx[i])
  132. proof.PiC = bn.G1.Add(proof.PiC, m)
  133. }
  134. proof.PiCp = bn.G1.MulScalar(proof.PiC, setup.Toxic.Kc) // this in the setup step
  135. // g1*H(t)
  136. proof.PiH = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  137. for i := 0; i < len(hx); i++ {
  138. m := bn.G1.MulScalar(setup.G1T[i], hx[i])
  139. proof.PiH = bn.G1.Add(proof.PiH, m)
  140. }
  141. proof.Vz = bn.Fq6.Zero()
  142. for i := 0; i < len(bx); i++ {
  143. m := bn.G2.MulScalar(setup.G2T[i], zx[i])
  144. proof.Vz = bn.G2.Add(proof.Vz, m)
  145. }
  146. // proof.Vz = g2Zt
  147. proof.Va = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Ka) // this in the setup step
  148. proof.Vb = bn.G1.MulScalar(bn.G1.G, setup.Toxic.Kb) // this in the setup step
  149. proof.Vc = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Kc) // this in the setup step
  150. return proof, nil
  151. }
  152. func VerifyProof(bn bn128.Bn128, setup Setup, proof Proof) bool {
  153. // e(piA, Va) == e(piA', g2)
  154. pairingPiaVa, err := bn.Pairing(proof.PiA, proof.Va)
  155. if err != nil {
  156. return false
  157. }
  158. pairingPiapG2, err := bn.Pairing(proof.PiAp, bn.G2.G)
  159. if err != nil {
  160. return false
  161. }
  162. if !bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  163. return false
  164. }
  165. // e(Vb, piB) == e(piB', g2)
  166. pairingVbPib, err := bn.Pairing(proof.Vb, proof.PiB)
  167. if err != nil {
  168. return false
  169. }
  170. pairingPibpG2, err := bn.Pairing(proof.PiBp, bn.G2.G)
  171. if err != nil {
  172. return false
  173. }
  174. if !bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  175. return false
  176. }
  177. // e(piC, Vc) == e(piC', g2)
  178. pairingPicVc, err := bn.Pairing(proof.PiC, proof.Vc)
  179. if err != nil {
  180. return false
  181. }
  182. pairingPicpG2, err := bn.Pairing(proof.PiCp, bn.G2.G)
  183. if err != nil {
  184. return false
  185. }
  186. if !bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  187. return false
  188. }
  189. // e(piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB)
  190. // == e(piK, g2Kgamma)
  191. // piApiC := bn.G1.Add(proof.PiA, proof.PiC)
  192. // pairingPiACG2Kbg, err := bn.Pairing(piApiC, setup.G2Kbg)
  193. // if err != nil {
  194. // return false
  195. // }
  196. // pairingG1KbgPiB, err := bn.Pairing(setup.G1Kbg, proof.PiB)
  197. // if err != nil {
  198. // return false
  199. // }
  200. // pairing1 := bn.Fq12.Mul(pairingPiACG2Kbg, pairingG1KbgPiB)
  201. //
  202. // e(piA, piB) == e(piH, Vz) * e(piC, g2)
  203. // pairingPiaPib, err := bn.Pairing(proof.PiA, proof.PiB)
  204. // if err != nil {
  205. // return false
  206. // }
  207. // pairingPihVz, err := bn.Pairing(proof.PiH, proof.Vz)
  208. // if err != nil {
  209. // return false
  210. // }
  211. // pairingPicG2, err := bn.Pairing(proof.PiC, bn.G2.G)
  212. // if err != nil {
  213. // return false
  214. // }
  215. // if !bn.Fq12.Equal(pairingPiaPib, bn.Fq12.Mul(pairingPihVz, pairingPicG2)) {
  216. // return false
  217. // }
  218. return true
  219. }