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.

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