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. fmt.Println("alphas[1]", alphas[1])
  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. rhoAat := Utils.FqR.Mul(setup.Toxic.RhoA, at)
  162. a := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoAat)
  163. setup.Pk.A = append(setup.Pk.A, a)
  164. if i <= circuit.NPublic {
  165. setup.Vk.IC = append(setup.Vk.IC, a)
  166. }
  167. bt := Utils.PF.Eval(betas[i], setup.Toxic.T)
  168. // rhoBbt := Utils.Bn.Fq1.Mul(setup.Toxic.RhoB, bt)
  169. rhoBbt := Utils.FqR.Mul(setup.Toxic.RhoB, bt)
  170. bg1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoBbt)
  171. bg2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoBbt)
  172. setup.Pk.B = append(setup.Pk.B, bg2)
  173. ct := Utils.PF.Eval(gammas[i], setup.Toxic.T)
  174. // rhoCct := Utils.Bn.Fq1.Mul(setup.Toxic.RhoC, ct)
  175. rhoCct := Utils.FqR.Mul(setup.Toxic.RhoC, ct)
  176. c := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoCct)
  177. setup.Pk.C = append(setup.Pk.C, c)
  178. kt := Utils.FqR.Add(Utils.FqR.Add(rhoAat, rhoBbt), rhoCct)
  179. k := Utils.Bn.G1.Affine(Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt))
  180. ktest := Utils.Bn.G1.Affine(Utils.Bn.G1.Add(Utils.Bn.G1.Add(a, bg1), c))
  181. if !Utils.Bn.Fq2.Equal(k, ktest) {
  182. os.Exit(1)
  183. return setup, err
  184. }
  185. setup.Pk.Ap = append(setup.Pk.Ap, Utils.Bn.G1.MulScalar(a, setup.Toxic.Ka))
  186. setup.Pk.Bp = append(setup.Pk.Bp, Utils.Bn.G1.MulScalar(bg1, setup.Toxic.Kb))
  187. setup.Pk.Cp = append(setup.Pk.Cp, Utils.Bn.G1.MulScalar(c, setup.Toxic.Kc))
  188. k_ := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt)
  189. setup.Pk.Kp = append(setup.Pk.Kp, Utils.Bn.G1.MulScalar(k_, setup.Toxic.Kbeta))
  190. }
  191. // z pol
  192. zpol := []*big.Int{big.NewInt(int64(1))}
  193. // for i := 0; i < len(circuit.Constraints); i++ {
  194. for i := 1; i < len(alphas)-1; i++ {
  195. zpol = Utils.PF.Mul(
  196. zpol,
  197. []*big.Int{
  198. Utils.FqR.Neg( // neg over R
  199. big.NewInt(int64(i))),
  200. big.NewInt(int64(1)),
  201. })
  202. }
  203. fmt.Println("zpol", zpol)
  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. fmt.Println("len(G1T)", len(gt1))
  219. setup.G1T = gt1
  220. return setup, nil
  221. }
  222. // GenerateProofs generates all the parameters to proof the zkSNARK from the Circuit, Setup and the Witness
  223. func GenerateProofs(circuit circuitcompiler.Circuit, setup Setup, w []*big.Int, px []*big.Int) (Proof, error) {
  224. var proof Proof
  225. proof.PiA = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  226. proof.PiAp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  227. proof.PiB = Utils.Bn.Fq6.Zero()
  228. proof.PiBp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  229. proof.PiC = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  230. proof.PiCp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  231. proof.PiH = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  232. proof.PiKp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  233. for i := circuit.NPublic + 1; i < circuit.NVars; i++ {
  234. proof.PiA = Utils.Bn.G1.Add(proof.PiA, Utils.Bn.G1.MulScalar(setup.Pk.A[i], w[i]))
  235. proof.PiAp = Utils.Bn.G1.Add(proof.PiAp, Utils.Bn.G1.MulScalar(setup.Pk.Ap[i], w[i]))
  236. }
  237. for i := 0; i < circuit.NVars; i++ {
  238. proof.PiB = Utils.Bn.G2.Add(proof.PiB, Utils.Bn.G2.MulScalar(setup.Pk.B[i], w[i]))
  239. proof.PiBp = Utils.Bn.G1.Add(proof.PiBp, Utils.Bn.G1.MulScalar(setup.Pk.Bp[i], w[i]))
  240. proof.PiC = Utils.Bn.G1.Add(proof.PiC, Utils.Bn.G1.MulScalar(setup.Pk.C[i], w[i]))
  241. proof.PiCp = Utils.Bn.G1.Add(proof.PiCp, Utils.Bn.G1.MulScalar(setup.Pk.Cp[i], w[i]))
  242. proof.PiKp = Utils.Bn.G1.Add(proof.PiKp, Utils.Bn.G1.MulScalar(setup.Pk.Kp[i], w[i]))
  243. }
  244. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z) // maybe move this calculation to a previous step
  245. // piH = pkH,0 + sum ( hi * pk H,i ), where pkH = G1T, hi=hx
  246. // proof.PiH = Utils.Bn.G1.Add(proof.PiH, setup.G1T[0])
  247. for i := 0; i < len(hx); i++ {
  248. proof.PiH = Utils.Bn.G1.Add(proof.PiH, Utils.Bn.G1.MulScalar(setup.G1T[i], hx[i]))
  249. }
  250. return proof, nil
  251. }
  252. // VerifyProof verifies over the BN128 the Pairings of the Proof
  253. func VerifyProof(circuit circuitcompiler.Circuit, setup Setup, proof Proof, publicSignals []*big.Int, debug bool) bool {
  254. // e(piA, Va) == e(piA', g2)
  255. pairingPiaVa := Utils.Bn.Pairing(proof.PiA, setup.Vk.Vka)
  256. pairingPiapG2 := Utils.Bn.Pairing(proof.PiAp, Utils.Bn.G2.G)
  257. if !Utils.Bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  258. return false
  259. }
  260. if debug {
  261. fmt.Println("✓ e(piA, Va) == e(piA', g2), valid knowledge commitment for A")
  262. }
  263. // e(Vb, piB) == e(piB', g2)
  264. pairingVbPib := Utils.Bn.Pairing(setup.Vk.Vkb, proof.PiB)
  265. pairingPibpG2 := Utils.Bn.Pairing(proof.PiBp, Utils.Bn.G2.G)
  266. if !Utils.Bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  267. return false
  268. }
  269. if debug {
  270. fmt.Println("✓ e(Vb, piB) == e(piB', g2), valid knowledge commitment for B")
  271. }
  272. // e(piC, Vc) == e(piC', g2)
  273. pairingPicVc := Utils.Bn.Pairing(proof.PiC, setup.Vk.Vkc)
  274. pairingPicpG2 := Utils.Bn.Pairing(proof.PiCp, Utils.Bn.G2.G)
  275. if !Utils.Bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  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. fmt.Println("pub sig", publicSignals[i])
  285. vkxpia = Utils.Bn.G1.Add(vkxpia, Utils.Bn.G1.MulScalar(setup.Vk.IC[i+1], publicSignals[i]))
  286. }
  287. // e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2)
  288. if !Utils.Bn.Fq12.Equal(
  289. 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
  290. Utils.Bn.Fq12.Mul(
  291. Utils.Bn.Pairing(proof.PiH, setup.Vk.Vkz),
  292. Utils.Bn.Pairing(proof.PiC, Utils.Bn.G2.G))) {
  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. 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. }