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.

356 lines
12 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
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 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. // Setup is the data structure holding the Trusted Setup data. The Setup.Toxic sub struct must be destroyed after the GenerateTrustedSetup function is completed
  12. type Setup struct {
  13. Toxic struct {
  14. T *big.Int // trusted setup secret
  15. Ka *big.Int // prover
  16. Kb *big.Int // prover
  17. Kc *big.Int // prover
  18. Kbeta *big.Int
  19. Kgamma *big.Int
  20. RhoA *big.Int
  21. RhoB *big.Int
  22. RhoC *big.Int
  23. }
  24. // public
  25. G1T [][3]*big.Int // t encrypted in G1 curve, G1T == Pk.H
  26. G2T [][3][2]*big.Int // t encrypted in G2 curve
  27. Pk struct { // Proving Key pk:=(pkA, pkB, pkC, pkH)
  28. A [][3]*big.Int
  29. B [][3][2]*big.Int
  30. C [][3]*big.Int
  31. Kp [][3]*big.Int
  32. Ap [][3]*big.Int
  33. Bp [][3]*big.Int
  34. Cp [][3]*big.Int
  35. Z []*big.Int
  36. }
  37. Vk struct {
  38. Vka [3][2]*big.Int
  39. Vkb [3]*big.Int
  40. Vkc [3][2]*big.Int
  41. IC [][3]*big.Int
  42. G1Kbg [3]*big.Int // g1 * Kbeta * Kgamma
  43. G2Kbg [3][2]*big.Int // g2 * Kbeta * Kgamma
  44. G2Kg [3][2]*big.Int // g2 * Kgamma
  45. Vkz [3][2]*big.Int
  46. }
  47. }
  48. // Proof contains the parameters to proof the zkSNARK
  49. type Proof struct {
  50. PiA [3]*big.Int
  51. PiAp [3]*big.Int
  52. PiB [3][2]*big.Int
  53. PiBp [3]*big.Int
  54. PiC [3]*big.Int
  55. PiCp [3]*big.Int
  56. PiH [3]*big.Int
  57. PiKp [3]*big.Int
  58. // PublicSignals []*big.Int
  59. }
  60. type utils struct {
  61. Bn bn128.Bn128
  62. FqR fields.Fq
  63. PF r1csqap.PolynomialField
  64. }
  65. // Utils is the data structure holding the BN128, FqR Finite Field over R, PolynomialField, that will be used inside the snarks operations
  66. var Utils = prepareUtils()
  67. func prepareUtils() utils {
  68. bn, err := bn128.NewBn128()
  69. if err != nil {
  70. panic(err)
  71. }
  72. // new Finite Field
  73. fqR := fields.NewFq(bn.R)
  74. // new Polynomial Field
  75. pf := r1csqap.NewPolynomialField(fqR)
  76. return utils{
  77. Bn: bn,
  78. FqR: fqR,
  79. PF: pf,
  80. }
  81. }
  82. // GenerateTrustedSetup generates the Trusted Setup from a compiled Circuit. The Setup.Toxic sub data structure must be destroyed
  83. func GenerateTrustedSetup(witnessLength int, circuit circuitcompiler.Circuit, alphas, betas, gammas [][]*big.Int) (Setup, error) {
  84. var setup Setup
  85. var err error
  86. // input soundness
  87. // for i := 0; i < len(alphas); i++ {
  88. // for j := 0; j < len(alphas[i]); j++ {
  89. // if j <= circuit.NPublic {
  90. // if bytes.Equal(alphas[i][j].Bytes(), Utils.FqR.Zero().Bytes()) {
  91. // alphas[i][j] = Utils.FqR.One()
  92. // }
  93. // }
  94. // }
  95. // }
  96. // generate random t value
  97. setup.Toxic.T, err = Utils.FqR.Rand()
  98. if err != nil {
  99. return Setup{}, err
  100. }
  101. // k for calculating pi' and Vk
  102. setup.Toxic.Ka, err = Utils.FqR.Rand()
  103. if err != nil {
  104. return Setup{}, err
  105. }
  106. setup.Toxic.Kb, err = Utils.FqR.Rand()
  107. if err != nil {
  108. return Setup{}, err
  109. }
  110. setup.Toxic.Kc, err = Utils.FqR.Rand()
  111. if err != nil {
  112. return Setup{}, err
  113. }
  114. // generate Kβ (Kbeta) and Kγ (Kgamma)
  115. setup.Toxic.Kbeta, err = Utils.FqR.Rand()
  116. if err != nil {
  117. return Setup{}, err
  118. }
  119. setup.Toxic.Kgamma, err = Utils.FqR.Rand()
  120. if err != nil {
  121. return Setup{}, err
  122. }
  123. // generate ρ (Rho): ρA, ρB, ρC
  124. setup.Toxic.RhoA, err = Utils.FqR.Rand()
  125. if err != nil {
  126. return Setup{}, err
  127. }
  128. setup.Toxic.RhoB, err = Utils.FqR.Rand()
  129. if err != nil {
  130. return Setup{}, err
  131. }
  132. setup.Toxic.RhoC = Utils.FqR.Mul(setup.Toxic.RhoA, setup.Toxic.RhoB)
  133. // calculated more down
  134. // for i := 0; i < witnessLength; i++ {
  135. // tPow := Utils.FqR.Exp(setup.Toxic.T, big.NewInt(int64(i)))
  136. // tEncr1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, tPow)
  137. // gt1 = append(gt1, tEncr1)
  138. // tEncr2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, tPow)
  139. // gt2 = append(gt2, tEncr2)
  140. // }
  141. // gt1: g1, g1*t, g1*t^2, g1*t^3, ...
  142. // gt2: g2, g2*t, g2*t^2, ...
  143. setup.Vk.Vka = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Ka)
  144. setup.Vk.Vkb = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, setup.Toxic.Kb)
  145. setup.Vk.Vkc = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kc)
  146. /*
  147. Verification keys:
  148. - Vk_betagamma1: setup.G1Kbg = g1 * Kbeta*Kgamma
  149. - Vk_betagamma2: setup.G2Kbg = g2 * Kbeta*Kgamma
  150. - Vk_gamma: setup.G2Kg = g2 * Kgamma
  151. */
  152. kbg := Utils.FqR.Mul(setup.Toxic.Kbeta, setup.Toxic.Kgamma)
  153. setup.Vk.G1Kbg = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kbg)
  154. setup.Vk.G2Kbg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, kbg)
  155. setup.Vk.G2Kg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kgamma)
  156. // for i := 0; i < circuit.NVars; i++ {
  157. for i := 0; i < len(circuit.Signals); i++ {
  158. at := Utils.PF.Eval(alphas[i], setup.Toxic.T)
  159. // rhoAat := Utils.Bn.Fq1.Mul(setup.Toxic.RhoA, at)
  160. rhoAat := Utils.FqR.Mul(setup.Toxic.RhoA, at)
  161. a := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoAat)
  162. setup.Pk.A = append(setup.Pk.A, a)
  163. if i <= circuit.NPublic {
  164. setup.Vk.IC = append(setup.Vk.IC, a)
  165. }
  166. bt := Utils.PF.Eval(betas[i], setup.Toxic.T)
  167. // rhoBbt := Utils.Bn.Fq1.Mul(setup.Toxic.RhoB, bt)
  168. rhoBbt := Utils.FqR.Mul(setup.Toxic.RhoB, bt)
  169. bg1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoBbt)
  170. bg2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoBbt)
  171. setup.Pk.B = append(setup.Pk.B, bg2)
  172. ct := Utils.PF.Eval(gammas[i], setup.Toxic.T)
  173. // rhoCct := Utils.Bn.Fq1.Mul(setup.Toxic.RhoC, ct)
  174. rhoCct := Utils.FqR.Mul(setup.Toxic.RhoC, ct)
  175. c := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoCct)
  176. setup.Pk.C = append(setup.Pk.C, c)
  177. kt := Utils.FqR.Add(Utils.FqR.Add(rhoAat, rhoBbt), rhoCct)
  178. k := Utils.Bn.G1.Affine(Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt))
  179. ktest := Utils.Bn.G1.Affine(Utils.Bn.G1.Add(Utils.Bn.G1.Add(a, bg1), c))
  180. if !Utils.Bn.Fq2.Equal(k, ktest) {
  181. os.Exit(1)
  182. return setup, err
  183. }
  184. setup.Pk.Ap = append(setup.Pk.Ap, Utils.Bn.G1.MulScalar(a, setup.Toxic.Ka))
  185. setup.Pk.Bp = append(setup.Pk.Bp, Utils.Bn.G1.MulScalar(bg1, setup.Toxic.Kb))
  186. setup.Pk.Cp = append(setup.Pk.Cp, Utils.Bn.G1.MulScalar(c, setup.Toxic.Kc))
  187. k_ := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt)
  188. setup.Pk.Kp = append(setup.Pk.Kp, Utils.Bn.G1.MulScalar(k_, setup.Toxic.Kbeta))
  189. }
  190. // z pol
  191. zpol := []*big.Int{big.NewInt(int64(1))}
  192. // for i := 0; i < len(circuit.Constraints); i++ {
  193. for i := 1; i < len(alphas)-1; i++ {
  194. zpol = Utils.PF.Mul(
  195. zpol,
  196. []*big.Int{
  197. Utils.FqR.Neg( // neg over R
  198. big.NewInt(int64(i))),
  199. big.NewInt(int64(1)),
  200. })
  201. }
  202. setup.Pk.Z = zpol
  203. zt := Utils.PF.Eval(zpol, setup.Toxic.T)
  204. // rhoCzt := Utils.Bn.Fq1.Mul(setup.Toxic.RhoC, zt)
  205. rhoCzt := Utils.FqR.Mul(setup.Toxic.RhoC, zt)
  206. setup.Vk.Vkz = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoCzt)
  207. // encrypt t values with curve generators
  208. var gt1 [][3]*big.Int
  209. gt1 = append(gt1, Utils.Bn.G1.G) // the first is t**0 * G1 = 1 * G1 = G1
  210. tEncr := setup.Toxic.T
  211. for i := 1; i < len(zpol); i++ { //should be G1T = pkH = (tau**i * G1) from i=0 to d, where d is degree of pol Z(x)
  212. gt1 = append(gt1, Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, tEncr))
  213. // tEncr = Utils.Bn.Fq1.Mul(tEncr, setup.Toxic.T)
  214. tEncr = Utils.FqR.Mul(tEncr, setup.Toxic.T)
  215. }
  216. setup.G1T = gt1
  217. return setup, nil
  218. }
  219. // GenerateProofs generates all the parameters to proof the zkSNARK from the Circuit, Setup and the Witness
  220. func GenerateProofs(circuit circuitcompiler.Circuit, setup Setup, w []*big.Int, px []*big.Int) (Proof, error) {
  221. var proof Proof
  222. proof.PiA = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  223. proof.PiAp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  224. proof.PiB = Utils.Bn.Fq6.Zero()
  225. proof.PiBp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  226. proof.PiC = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  227. proof.PiCp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  228. proof.PiH = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  229. proof.PiKp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  230. for i := circuit.NPublic + 1; i < circuit.NVars; i++ {
  231. proof.PiA = Utils.Bn.G1.Add(proof.PiA, Utils.Bn.G1.MulScalar(setup.Pk.A[i], w[i]))
  232. proof.PiAp = Utils.Bn.G1.Add(proof.PiAp, Utils.Bn.G1.MulScalar(setup.Pk.Ap[i], w[i]))
  233. }
  234. for i := 0; i < circuit.NVars; i++ {
  235. proof.PiB = Utils.Bn.G2.Add(proof.PiB, Utils.Bn.G2.MulScalar(setup.Pk.B[i], w[i]))
  236. proof.PiBp = Utils.Bn.G1.Add(proof.PiBp, Utils.Bn.G1.MulScalar(setup.Pk.Bp[i], w[i]))
  237. proof.PiC = Utils.Bn.G1.Add(proof.PiC, Utils.Bn.G1.MulScalar(setup.Pk.C[i], w[i]))
  238. proof.PiCp = Utils.Bn.G1.Add(proof.PiCp, Utils.Bn.G1.MulScalar(setup.Pk.Cp[i], w[i]))
  239. proof.PiKp = Utils.Bn.G1.Add(proof.PiKp, Utils.Bn.G1.MulScalar(setup.Pk.Kp[i], w[i]))
  240. }
  241. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z) // maybe move this calculation to a previous step
  242. // piH = pkH,0 + sum ( hi * pk H,i ), where pkH = G1T, hi=hx
  243. // proof.PiH = Utils.Bn.G1.Add(proof.PiH, setup.G1T[0])
  244. for i := 0; i < len(hx); i++ {
  245. proof.PiH = Utils.Bn.G1.Add(proof.PiH, Utils.Bn.G1.MulScalar(setup.G1T[i], hx[i]))
  246. }
  247. return proof, nil
  248. }
  249. // VerifyProof verifies over the BN128 the Pairings of the Proof
  250. func VerifyProof(circuit circuitcompiler.Circuit, setup Setup, proof Proof, publicSignals []*big.Int, debug bool) bool {
  251. // e(piA, Va) == e(piA', g2)
  252. pairingPiaVa := Utils.Bn.Pairing(proof.PiA, setup.Vk.Vka)
  253. pairingPiapG2 := Utils.Bn.Pairing(proof.PiAp, Utils.Bn.G2.G)
  254. if !Utils.Bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  255. fmt.Println("❌ e(piA, Va) == e(piA', g2), valid knowledge commitment for A")
  256. return false
  257. }
  258. if debug {
  259. fmt.Println("✓ e(piA, Va) == e(piA', g2), valid knowledge commitment for A")
  260. }
  261. // e(Vb, piB) == e(piB', g2)
  262. pairingVbPib := Utils.Bn.Pairing(setup.Vk.Vkb, proof.PiB)
  263. pairingPibpG2 := Utils.Bn.Pairing(proof.PiBp, Utils.Bn.G2.G)
  264. if !Utils.Bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  265. fmt.Println("❌ e(Vb, piB) == e(piB', g2), valid knowledge commitment for B")
  266. return false
  267. }
  268. if debug {
  269. fmt.Println("✓ e(Vb, piB) == e(piB', g2), valid knowledge commitment for B")
  270. }
  271. // e(piC, Vc) == e(piC', g2)
  272. pairingPicVc := Utils.Bn.Pairing(proof.PiC, setup.Vk.Vkc)
  273. pairingPicpG2 := Utils.Bn.Pairing(proof.PiCp, Utils.Bn.G2.G)
  274. if !Utils.Bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  275. fmt.Println("❌ e(piC, Vc) == e(piC', g2), valid knowledge commitment for C")
  276. return false
  277. }
  278. if debug {
  279. fmt.Println("✓ e(piC, Vc) == e(piC', g2), valid knowledge commitment for C")
  280. }
  281. // Vkx, to then calculate Vkx+piA
  282. vkxpia := setup.Vk.IC[0]
  283. for i := 0; i < len(publicSignals); i++ {
  284. vkxpia = Utils.Bn.G1.Add(vkxpia, Utils.Bn.G1.MulScalar(setup.Vk.IC[i+1], publicSignals[i]))
  285. }
  286. // e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2)
  287. if !Utils.Bn.Fq12.Equal(
  288. Utils.Bn.Pairing(Utils.Bn.G1.Add(vkxpia, proof.PiA), proof.PiB), // TODO Add(vkxpia, proof.PiA) can go outside in order to save computation, as is reused later
  289. Utils.Bn.Fq12.Mul(
  290. Utils.Bn.Pairing(proof.PiH, setup.Vk.Vkz),
  291. Utils.Bn.Pairing(proof.PiC, Utils.Bn.G2.G))) {
  292. fmt.Println("❌ e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2), QAP disibility checked")
  293. return false
  294. }
  295. if debug {
  296. fmt.Println("✓ e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2), QAP disibility checked")
  297. }
  298. // e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB)
  299. // == e(piK, g2Kgamma)
  300. piApiC := Utils.Bn.G1.Add(Utils.Bn.G1.Add(vkxpia, proof.PiA), proof.PiC)
  301. pairingPiACG2Kbg := Utils.Bn.Pairing(piApiC, setup.Vk.G2Kbg)
  302. pairingG1KbgPiB := Utils.Bn.Pairing(setup.Vk.G1Kbg, proof.PiB)
  303. pairingL := Utils.Bn.Fq12.Mul(pairingPiACG2Kbg, pairingG1KbgPiB)
  304. pairingR := Utils.Bn.Pairing(proof.PiKp, setup.Vk.G2Kg)
  305. if !Utils.Bn.Fq12.Equal(pairingL, pairingR) {
  306. fmt.Println("❌ e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB) == e(piK, g2Kgamma)")
  307. return false
  308. }
  309. if debug {
  310. fmt.Println("✓ e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB) == e(piK, g2Kgamma)")
  311. }
  312. return true
  313. }