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.

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