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.

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