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.

269 lines
7.9 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
  1. package snark
  2. import (
  3. "fmt"
  4. "math/big"
  5. "os"
  6. "github.com/arnaucube/go-snark/bn128"
  7. "github.com/arnaucube/go-snark/circuitcompiler"
  8. "github.com/arnaucube/go-snark/fields"
  9. "github.com/arnaucube/go-snark/r1csqap"
  10. )
  11. type Setup struct {
  12. Toxic struct {
  13. T *big.Int // trusted setup secret
  14. Ka *big.Int // prover
  15. Kb *big.Int // prover
  16. Kc *big.Int // prover
  17. Kbeta *big.Int
  18. Kgamma *big.Int
  19. RhoA *big.Int
  20. RhoB *big.Int
  21. RhoC *big.Int
  22. }
  23. // public
  24. G1T [][3]*big.Int // t encrypted in G1 curve
  25. G2T [][3][2]*big.Int // t encrypted in G2 curve
  26. Pk struct { // Proving Key pk:=(pkA, pkB, pkC, pkH)
  27. A [][3]*big.Int
  28. B [][3][2]*big.Int
  29. C [][3]*big.Int
  30. Kp [][3]*big.Int
  31. Ap [][3]*big.Int
  32. Bp [][3]*big.Int
  33. Cp [][3]*big.Int
  34. }
  35. Vk struct {
  36. Vka [3][2]*big.Int
  37. Vkb [3]*big.Int
  38. Vkc [3][2]*big.Int
  39. A [][3]*big.Int
  40. G1Kbg [3]*big.Int // g1 * Kbeta * Kgamma
  41. G2Kbg [3][2]*big.Int // g2 * Kbeta * Kgamma
  42. G2Kg [3][2]*big.Int // g2 * Kgamma
  43. Vkz [3][2]*big.Int
  44. }
  45. }
  46. type Proof struct {
  47. PiA [3]*big.Int
  48. PiAp [3]*big.Int
  49. PiB [3][2]*big.Int
  50. PiBp [3]*big.Int
  51. PiC [3]*big.Int
  52. PiCp [3]*big.Int
  53. PiH [3]*big.Int
  54. PiKp [3]*big.Int
  55. PublicSignals []*big.Int
  56. }
  57. func GenerateTrustedSetup(bn bn128.Bn128, fqR fields.Fq, pf r1csqap.PolynomialField, witnessLength int, circuit circuitcompiler.Circuit, alphas, betas, gammas [][]*big.Int, zx []*big.Int) (Setup, error) {
  58. var setup Setup
  59. var err error
  60. // generate random t value
  61. setup.Toxic.T, err = fqR.Rand()
  62. if err != nil {
  63. return Setup{}, err
  64. }
  65. // k for calculating pi' and Vk
  66. setup.Toxic.Ka, err = fqR.Rand()
  67. if err != nil {
  68. return Setup{}, err
  69. }
  70. setup.Toxic.Kb, err = fqR.Rand()
  71. if err != nil {
  72. return Setup{}, err
  73. }
  74. setup.Toxic.Kc, err = fqR.Rand()
  75. if err != nil {
  76. return Setup{}, err
  77. }
  78. // generate Kβ (Kbeta) and Kγ (Kgamma)
  79. setup.Toxic.Kbeta, err = fqR.Rand()
  80. if err != nil {
  81. return Setup{}, err
  82. }
  83. setup.Toxic.Kgamma, err = fqR.Rand()
  84. if err != nil {
  85. return Setup{}, err
  86. }
  87. // generate ρ (Rho): ρA, ρB, ρC
  88. setup.Toxic.RhoA, err = fqR.Rand()
  89. if err != nil {
  90. return Setup{}, err
  91. }
  92. setup.Toxic.RhoB, err = fqR.Rand()
  93. if err != nil {
  94. return Setup{}, err
  95. }
  96. setup.Toxic.RhoC = fqR.Mul(setup.Toxic.RhoA, setup.Toxic.RhoB)
  97. // encrypt t values with curve generators
  98. var gt1 [][3]*big.Int
  99. var gt2 [][3][2]*big.Int
  100. for i := 0; i < witnessLength; i++ {
  101. tPow := fqR.Exp(setup.Toxic.T, big.NewInt(int64(i)))
  102. tEncr1 := bn.G1.MulScalar(bn.G1.G, tPow)
  103. gt1 = append(gt1, tEncr1)
  104. tEncr2 := bn.G2.MulScalar(bn.G2.G, tPow)
  105. gt2 = append(gt2, tEncr2)
  106. }
  107. // gt1: g1, g1*t, g1*t^2, g1*t^3, ...
  108. // gt2: g2, g2*t, g2*t^2, ...
  109. setup.G1T = gt1
  110. setup.G2T = gt2
  111. setup.Vk.Vka = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Ka)
  112. setup.Vk.Vkb = bn.G1.MulScalar(bn.G1.G, setup.Toxic.Kb)
  113. setup.Vk.Vkc = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Kc)
  114. /*
  115. Verification keys:
  116. - Vk_betagamma1: setup.G1Kbg = g1 * Kbeta*Kgamma
  117. - Vk_betagamma2: setup.G2Kbg = g2 * Kbeta*Kgamma
  118. - Vk_gamma: setup.G2Kg = g2 * Kgamma
  119. */
  120. kbg := fqR.Mul(setup.Toxic.Kbeta, setup.Toxic.Kgamma)
  121. setup.Vk.G1Kbg = bn.G1.MulScalar(bn.G1.G, kbg)
  122. setup.Vk.G2Kbg = bn.G2.MulScalar(bn.G2.G, kbg)
  123. setup.Vk.G2Kg = bn.G2.MulScalar(bn.G2.G, setup.Toxic.Kgamma)
  124. // for i := 0; i < circuit.NSignals; i++ {
  125. for i := 0; i < circuit.NVars; i++ {
  126. at := pf.Eval(alphas[i], setup.Toxic.T)
  127. a := bn.G1.MulScalar(bn.G1.G, at)
  128. setup.Pk.A = append(setup.Pk.A, a)
  129. if i <= circuit.NPublic {
  130. setup.Vk.A = append(setup.Vk.A, a)
  131. }
  132. bt := pf.Eval(betas[i], setup.Toxic.T)
  133. bg1 := bn.G1.MulScalar(bn.G1.G, bt)
  134. bg2 := bn.G2.MulScalar(bn.G2.G, bt)
  135. setup.Pk.B = append(setup.Pk.B, bg2)
  136. ct := pf.Eval(gammas[i], setup.Toxic.T)
  137. c := bn.G1.MulScalar(bn.G1.G, ct)
  138. setup.Pk.C = append(setup.Pk.C, c)
  139. kt := fqR.Add(fqR.Add(at, bt), ct)
  140. k := bn.G1.Affine(bn.G1.MulScalar(bn.G1.G, kt))
  141. ktest := bn.G1.Affine(bn.G1.Add(bn.G1.Add(a, bg1), c))
  142. if !bn.Fq2.Equal(k, ktest) {
  143. os.Exit(1)
  144. return setup, err
  145. }
  146. setup.Pk.Ap = append(setup.Pk.Ap, bn.G1.MulScalar(a, setup.Toxic.Ka))
  147. setup.Pk.Bp = append(setup.Pk.Bp, bn.G1.MulScalar(bg1, setup.Toxic.Kb))
  148. setup.Pk.Cp = append(setup.Pk.Cp, bn.G1.MulScalar(c, setup.Toxic.Kc))
  149. k_ := bn.G1.MulScalar(bn.G1.G, kt)
  150. setup.Pk.Kp = append(setup.Pk.Kp, bn.G1.MulScalar(k_, setup.Toxic.Kbeta))
  151. }
  152. setup.Vk.Vkz = bn.G2.MulScalar(bn.G2.G, pf.Eval(zx, setup.Toxic.T))
  153. return setup, nil
  154. }
  155. func GenerateProofs(bn bn128.Bn128, f fields.Fq, circuit circuitcompiler.Circuit, setup Setup, hx []*big.Int, w []*big.Int) (Proof, error) {
  156. var proof Proof
  157. proof.PiA = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  158. proof.PiAp = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  159. proof.PiB = bn.Fq6.Zero()
  160. proof.PiBp = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  161. proof.PiC = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  162. proof.PiCp = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  163. proof.PiH = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  164. proof.PiKp = [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  165. for i := circuit.NPublic + 1; i < circuit.NVars; i++ {
  166. proof.PiA = bn.G1.Add(proof.PiA, bn.G1.MulScalar(setup.Pk.A[i], w[i]))
  167. proof.PiAp = bn.G1.Add(proof.PiAp, bn.G1.MulScalar(setup.Pk.Ap[i], w[i]))
  168. }
  169. for i := 0; i < circuit.NVars; i++ {
  170. proof.PiB = bn.G2.Add(proof.PiB, bn.G2.MulScalar(setup.Pk.B[i], w[i]))
  171. proof.PiBp = bn.G1.Add(proof.PiBp, bn.G1.MulScalar(setup.Pk.Bp[i], w[i]))
  172. proof.PiC = bn.G1.Add(proof.PiC, bn.G1.MulScalar(setup.Pk.C[i], w[i]))
  173. proof.PiCp = bn.G1.Add(proof.PiCp, bn.G1.MulScalar(setup.Pk.Cp[i], w[i]))
  174. proof.PiKp = bn.G1.Add(proof.PiKp, bn.G1.MulScalar(setup.Pk.Kp[i], w[i]))
  175. }
  176. for i := 0; i < len(hx); i++ {
  177. proof.PiH = bn.G1.Add(proof.PiH, bn.G1.MulScalar(setup.G1T[i], hx[i]))
  178. }
  179. proof.PublicSignals = w[1 : circuit.NPublic+1]
  180. return proof, nil
  181. }
  182. func VerifyProof(bn bn128.Bn128, circuit circuitcompiler.Circuit, setup Setup, proof Proof) bool {
  183. // e(piA, Va) == e(piA', g2)
  184. pairingPiaVa := bn.Pairing(proof.PiA, setup.Vk.Vka)
  185. pairingPiapG2 := bn.Pairing(proof.PiAp, bn.G2.G)
  186. if !bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  187. return false
  188. } else {
  189. fmt.Println("✓ e(piA, Va) == e(piA', g2), valid knowledge commitment for A")
  190. }
  191. // e(Vb, piB) == e(piB', g2)
  192. pairingVbPib := bn.Pairing(setup.Vk.Vkb, proof.PiB)
  193. pairingPibpG2 := bn.Pairing(proof.PiBp, bn.G2.G)
  194. if !bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  195. return false
  196. } else {
  197. fmt.Println("✓ e(Vb, piB) == e(piB', g2), valid knowledge commitment for B")
  198. }
  199. // e(piC, Vc) == e(piC', g2)
  200. pairingPicVc := bn.Pairing(proof.PiC, setup.Vk.Vkc)
  201. pairingPicpG2 := bn.Pairing(proof.PiCp, bn.G2.G)
  202. if !bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  203. return false
  204. } else {
  205. fmt.Println("✓ e(piC, Vc) == e(piC', g2), valid knowledge commitment for C")
  206. }
  207. // Vkx, to then calculate Vkx+piA
  208. vkxpia := setup.Vk.A[0]
  209. for i := 0; i < circuit.NPublic; i++ {
  210. vkxpia = bn.G1.Add(vkxpia, bn.G1.MulScalar(setup.Vk.A[i+1], proof.PublicSignals[i]))
  211. }
  212. // e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2)
  213. if !bn.Fq12.Equal(
  214. bn.Pairing(bn.G1.Add(vkxpia, proof.PiA), proof.PiB),
  215. bn.Fq12.Mul(
  216. bn.Pairing(proof.PiH, setup.Vk.Vkz),
  217. bn.Pairing(proof.PiC, bn.G2.G))) {
  218. return false
  219. } else {
  220. fmt.Println("✓ e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2), QAP disibility checked")
  221. }
  222. // e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB)
  223. // == e(piK, g2Kgamma)
  224. piApiC := bn.G1.Add(bn.G1.Add(vkxpia, proof.PiA), proof.PiC)
  225. pairingPiACG2Kbg := bn.Pairing(piApiC, setup.Vk.G2Kbg)
  226. pairingG1KbgPiB := bn.Pairing(setup.Vk.G1Kbg, proof.PiB)
  227. pairingL := bn.Fq12.Mul(pairingPiACG2Kbg, pairingG1KbgPiB)
  228. pairingR := bn.Pairing(proof.PiKp, setup.Vk.G2Kg)
  229. if !bn.Fq12.Equal(pairingL, pairingR) {
  230. return false
  231. } else {
  232. fmt.Println("✓ e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB) == e(piK, g2Kgamma)")
  233. }
  234. return true
  235. }