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.

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