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.

69 lines
1.8 KiB

  1. package zk
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. "github.com/arnaucube/go-snark/bn128"
  7. )
  8. const bits = 512
  9. func GenerateTrustedSetup(bn bn128.Bn128, pollength int) ([][3]*big.Int, [][3][2]*big.Int, error) {
  10. // generate random t value
  11. t, err := rand.Prime(rand.Reader, bits)
  12. if err != nil {
  13. return [][3]*big.Int{}, [][3][2]*big.Int{}, err
  14. }
  15. fmt.Print("trusted t: ")
  16. fmt.Println(t)
  17. // encrypt t values with curve generators
  18. var gt1 [][3]*big.Int
  19. var gt2 [][3][2]*big.Int
  20. for i := 0; i < pollength; i++ {
  21. tPow := bn.Fq1.Exp(t, big.NewInt(int64(i)))
  22. tEncr1 := bn.G1.MulScalar(bn.G1.G, tPow)
  23. gt1 = append(gt1, tEncr1)
  24. tEncr2 := bn.G2.MulScalar(bn.G2.G, tPow)
  25. gt2 = append(gt2, tEncr2)
  26. }
  27. // gt1: g1, g1*t, g1*t^2, g1*t^3, ...
  28. // gt2: g2, g2*t, g2*t^2, ...
  29. return gt1, gt2, nil
  30. }
  31. func GenerateProofs(bn bn128.Bn128, gt1 [][3]*big.Int, gt2 [][3][2]*big.Int, ax, bx, cx, hx, zx []*big.Int) ([3]*big.Int, [3][2]*big.Int, [3]*big.Int, [3]*big.Int, [3][2]*big.Int) {
  32. // multiply g1*A(x), g2*B(x), g1*C(x), g1*H(x)
  33. // g1*A(x)
  34. g1At := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  35. for i := 0; i < len(ax); i++ {
  36. m := bn.G1.MulScalar(gt1[i], ax[i])
  37. g1At = bn.G1.Add(g1At, m)
  38. }
  39. g2Bt := bn.Fq6.Zero()
  40. for i := 0; i < len(bx); i++ {
  41. m := bn.G2.MulScalar(gt2[i], bx[i])
  42. g2Bt = bn.G2.Add(g2Bt, m)
  43. }
  44. g1Ct := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  45. for i := 0; i < len(cx); i++ {
  46. m := bn.G1.MulScalar(gt1[i], cx[i])
  47. g1Ct = bn.G1.Add(g1Ct, m)
  48. }
  49. g1Ht := [3]*big.Int{bn.G1.F.Zero(), bn.G1.F.Zero(), bn.G1.F.Zero()}
  50. for i := 0; i < len(hx); i++ {
  51. m := bn.G1.MulScalar(gt1[i], hx[i])
  52. g1Ht = bn.G1.Add(g1Ht, m)
  53. }
  54. g2Zt := bn.Fq6.Zero()
  55. for i := 0; i < len(bx); i++ {
  56. m := bn.G2.MulScalar(gt2[i], zx[i])
  57. g2Zt = bn.G2.Add(g2Zt, m)
  58. }
  59. return g1At, g2Bt, g1Ct, g1Ht, g2Zt
  60. }