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.

268 lines
8.1 KiB

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
5 years ago
5 years ago
6 years ago
5 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 Pinocchio system setup structure
  9. type PinocchioSetup struct {
  10. Toxic struct {
  11. T *big.Int // trusted setup secret
  12. Ka *big.Int // prover
  13. Kb *big.Int // prover
  14. Kc *big.Int // prover
  15. Kbeta *big.Int
  16. Kgamma *big.Int
  17. RhoA *big.Int
  18. RhoB *big.Int
  19. RhoC *big.Int
  20. } `json:"-"`
  21. // public
  22. G1T [][3]*big.Int // t encrypted in G1 curve, G1T == Pk.H
  23. G2T [][3][2]*big.Int // t encrypted in G2 curve
  24. Pk struct { // Proving Key pk:=(pkA, pkB, pkC, pkH)
  25. A [][3]*big.Int
  26. B [][3][2]*big.Int
  27. C [][3]*big.Int
  28. Kp [][3]*big.Int
  29. Ap [][3]*big.Int
  30. Bp [][3]*big.Int
  31. Cp [][3]*big.Int
  32. Z []*big.Int
  33. }
  34. Vk struct {
  35. Vka [3][2]*big.Int
  36. Vkb [3]*big.Int
  37. Vkc [3][2]*big.Int
  38. IC [][3]*big.Int
  39. G1Kbg [3]*big.Int // g1 * Kbeta * Kgamma
  40. G2Kbg [3][2]*big.Int // g2 * Kbeta * Kgamma
  41. G2Kg [3][2]*big.Int // g2 * Kgamma
  42. Vkz [3][2]*big.Int
  43. }
  44. }
  45. // PinocchioProof is Pinocchio proof structure
  46. type PinocchioProof struct {
  47. PiA [3]*big.Int
  48. PiAp [3]*big.Int
  49. PiB [3][2]*big.Int
  50. PiBp [3]*big.Int
  51. PiC [3]*big.Int
  52. PiCp [3]*big.Int
  53. PiH [3]*big.Int
  54. PiKp [3]*big.Int
  55. }
  56. // Z is ...
  57. func (setup *PinocchioSetup) Z() []*big.Int {
  58. return setup.Pk.Z
  59. }
  60. // Init setups the trusted setup from a compiled circuit
  61. func (setup *PinocchioSetup) Init(cir *circuit.Circuit, alphas, betas, gammas [][]*big.Int) error {
  62. var err error
  63. setup.Toxic.T, err = Utils.FqR.Rand()
  64. if err != nil {
  65. return err
  66. }
  67. setup.Toxic.Ka, err = Utils.FqR.Rand()
  68. if err != nil {
  69. return err
  70. }
  71. setup.Toxic.Kb, err = Utils.FqR.Rand()
  72. if err != nil {
  73. return err
  74. }
  75. setup.Toxic.Kc, err = Utils.FqR.Rand()
  76. if err != nil {
  77. return err
  78. }
  79. setup.Toxic.Kbeta, err = Utils.FqR.Rand()
  80. if err != nil {
  81. return err
  82. }
  83. setup.Toxic.Kgamma, err = Utils.FqR.Rand()
  84. if err != nil {
  85. return err
  86. }
  87. kbg := Utils.FqR.Mul(setup.Toxic.Kbeta, setup.Toxic.Kgamma)
  88. setup.Toxic.RhoA, err = Utils.FqR.Rand()
  89. if err != nil {
  90. return err
  91. }
  92. setup.Toxic.RhoB, err = Utils.FqR.Rand()
  93. if err != nil {
  94. return err
  95. }
  96. setup.Toxic.RhoC = Utils.FqR.Mul(setup.Toxic.RhoA, setup.Toxic.RhoB)
  97. setup.Vk.Vka = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Ka)
  98. setup.Vk.Vkb = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, setup.Toxic.Kb)
  99. setup.Vk.Vkc = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kc)
  100. setup.Vk.G1Kbg = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kbg)
  101. setup.Vk.G2Kbg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, kbg)
  102. setup.Vk.G2Kg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kgamma)
  103. for i := 0; i < len(cir.Signals); i++ {
  104. at := Utils.PF.Eval(alphas[i], setup.Toxic.T)
  105. rhoAat := Utils.FqR.Mul(setup.Toxic.RhoA, at)
  106. a := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoAat)
  107. setup.Pk.A = append(setup.Pk.A, a)
  108. if i <= cir.NPublic {
  109. setup.Vk.IC = append(setup.Vk.IC, a)
  110. }
  111. bt := Utils.PF.Eval(betas[i], setup.Toxic.T)
  112. rhoBbt := Utils.FqR.Mul(setup.Toxic.RhoB, bt)
  113. bg1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoBbt)
  114. bg2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoBbt)
  115. setup.Pk.B = append(setup.Pk.B, bg2)
  116. ct := Utils.PF.Eval(gammas[i], setup.Toxic.T)
  117. rhoCct := Utils.FqR.Mul(setup.Toxic.RhoC, ct)
  118. c := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoCct)
  119. setup.Pk.C = append(setup.Pk.C, c)
  120. kt := Utils.FqR.Add(Utils.FqR.Add(rhoAat, rhoBbt), rhoCct)
  121. k := Utils.Bn.G1.Affine(Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt))
  122. ktest := Utils.Bn.G1.Affine(Utils.Bn.G1.Add(Utils.Bn.G1.Add(a, bg1), c))
  123. if !Utils.Bn.Fq2.Equal(k, ktest) {
  124. return err
  125. }
  126. setup.Pk.Ap = append(setup.Pk.Ap, Utils.Bn.G1.MulScalar(a, setup.Toxic.Ka))
  127. setup.Pk.Bp = append(setup.Pk.Bp, Utils.Bn.G1.MulScalar(bg1, setup.Toxic.Kb))
  128. setup.Pk.Cp = append(setup.Pk.Cp, Utils.Bn.G1.MulScalar(c, setup.Toxic.Kc))
  129. kk := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt)
  130. setup.Pk.Kp = append(setup.Pk.Kp, Utils.Bn.G1.MulScalar(kk, setup.Toxic.Kbeta))
  131. }
  132. zpol := []*big.Int{big.NewInt(int64(1))}
  133. for i := 1; i < len(alphas)-1; i++ {
  134. zpol = Utils.PF.Mul(
  135. zpol,
  136. []*big.Int{
  137. Utils.FqR.Neg(big.NewInt(int64(i))),
  138. big.NewInt(int64(1)),
  139. })
  140. }
  141. setup.Pk.Z = zpol
  142. zt := Utils.PF.Eval(zpol, setup.Toxic.T)
  143. rhoCzt := Utils.FqR.Mul(setup.Toxic.RhoC, zt)
  144. setup.Vk.Vkz = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoCzt)
  145. var gt1 [][3]*big.Int
  146. gt1 = append(gt1, Utils.Bn.G1.G)
  147. tEncr := setup.Toxic.T
  148. for i := 1; i < len(zpol); i++ {
  149. gt1 = append(gt1, Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, tEncr))
  150. tEncr = Utils.FqR.Mul(tEncr, setup.Toxic.T)
  151. }
  152. setup.G1T = gt1
  153. return nil
  154. }
  155. // Generate generates Pinocchio proof
  156. func (setup *PinocchioSetup) Generate(cir *circuit.Circuit, w []*big.Int, px []*big.Int) (Proof, error) {
  157. proof := &PinocchioProof{}
  158. proof.PiA = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  159. proof.PiAp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  160. proof.PiB = Utils.Bn.Fq6.Zero()
  161. proof.PiBp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  162. proof.PiC = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  163. proof.PiCp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  164. proof.PiH = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  165. proof.PiKp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
  166. for i := cir.NPublic + 1; i < cir.NVars; i++ {
  167. proof.PiA = Utils.Bn.G1.Add(proof.PiA, Utils.Bn.G1.MulScalar(setup.Pk.A[i], w[i]))
  168. proof.PiAp = Utils.Bn.G1.Add(proof.PiAp, Utils.Bn.G1.MulScalar(setup.Pk.Ap[i], w[i]))
  169. }
  170. for i := 0; i < cir.NVars; i++ {
  171. proof.PiB = Utils.Bn.G2.Add(proof.PiB, Utils.Bn.G2.MulScalar(setup.Pk.B[i], w[i]))
  172. proof.PiBp = Utils.Bn.G1.Add(proof.PiBp, Utils.Bn.G1.MulScalar(setup.Pk.Bp[i], w[i]))
  173. proof.PiC = Utils.Bn.G1.Add(proof.PiC, Utils.Bn.G1.MulScalar(setup.Pk.C[i], w[i]))
  174. proof.PiCp = Utils.Bn.G1.Add(proof.PiCp, Utils.Bn.G1.MulScalar(setup.Pk.Cp[i], w[i]))
  175. proof.PiKp = Utils.Bn.G1.Add(proof.PiKp, Utils.Bn.G1.MulScalar(setup.Pk.Kp[i], w[i]))
  176. }
  177. hx := Utils.PF.DivisorPolynomial(px, setup.Pk.Z)
  178. for i := 0; i < len(hx); i++ {
  179. proof.PiH = Utils.Bn.G1.Add(proof.PiH, Utils.Bn.G1.MulScalar(setup.G1T[i], hx[i]))
  180. }
  181. return proof, nil
  182. }
  183. // Verify verifies over the BN128 the Pairings of the Proof
  184. func (setup *PinocchioSetup) Verify(proof Proof, publicSignals []*big.Int) (bool, error) {
  185. pproof, ok := proof.(*PinocchioProof)
  186. if !ok {
  187. return false, fmt.Errorf("bad proof type")
  188. }
  189. pairingPiaVa := Utils.Bn.Pairing(pproof.PiA, setup.Vk.Vka)
  190. pairingPiapG2 := Utils.Bn.Pairing(pproof.PiAp, Utils.Bn.G2.G)
  191. if !Utils.Bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
  192. return false, nil
  193. }
  194. // e(Vb, piB) == e(piB', g2)
  195. pairingVbPib := Utils.Bn.Pairing(setup.Vk.Vkb, pproof.PiB)
  196. pairingPibpG2 := Utils.Bn.Pairing(pproof.PiBp, Utils.Bn.G2.G)
  197. if !Utils.Bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
  198. return false, nil
  199. }
  200. // e(piC, Vc) == e(piC', g2)
  201. pairingPicVc := Utils.Bn.Pairing(pproof.PiC, setup.Vk.Vkc)
  202. pairingPicpG2 := Utils.Bn.Pairing(pproof.PiCp, Utils.Bn.G2.G)
  203. if !Utils.Bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
  204. return false, nil
  205. }
  206. // Vkx+piA
  207. vkxpia := setup.Vk.IC[0]
  208. for i := 0; i < len(publicSignals); i++ {
  209. vkxpia = Utils.Bn.G1.Add(vkxpia, Utils.Bn.G1.MulScalar(setup.Vk.IC[i+1], publicSignals[i]))
  210. }
  211. // e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2)
  212. if !Utils.Bn.Fq12.Equal(
  213. Utils.Bn.Pairing(Utils.Bn.G1.Add(vkxpia, pproof.PiA), pproof.PiB),
  214. Utils.Bn.Fq12.Mul(
  215. Utils.Bn.Pairing(pproof.PiH, setup.Vk.Vkz),
  216. Utils.Bn.Pairing(pproof.PiC, Utils.Bn.G2.G))) {
  217. return false, nil
  218. }
  219. // e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB) == e(piK, g2Kgamma)
  220. piapic := Utils.Bn.G1.Add(Utils.Bn.G1.Add(vkxpia, pproof.PiA), pproof.PiC)
  221. pairingPiACG2Kbg := Utils.Bn.Pairing(piapic, setup.Vk.G2Kbg)
  222. pairingG1KbgPiB := Utils.Bn.Pairing(setup.Vk.G1Kbg, pproof.PiB)
  223. pairingL := Utils.Bn.Fq12.Mul(pairingPiACG2Kbg, pairingG1KbgPiB)
  224. pairingR := Utils.Bn.Pairing(pproof.PiKp, setup.Vk.G2Kg)
  225. if !Utils.Bn.Fq12.Equal(pairingL, pairingR) {
  226. return false, nil
  227. }
  228. return true, nil
  229. }