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.

99 lines
2.1 KiB

  1. package types
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "math/big"
  6. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  7. )
  8. var Q, _ = new(big.Int).SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10)
  9. // R is the mod of the finite field
  10. var R, _ = new(big.Int).SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10)
  11. // Proof is the data structure of the Groth16 zkSNARK proof
  12. type Proof struct {
  13. A *bn256.G1
  14. B *bn256.G2
  15. C *bn256.G1
  16. }
  17. type proofAux struct {
  18. A string `json:"pi_a"`
  19. B string `json:"pi_b"`
  20. C string `json:"pi_c"`
  21. }
  22. func (p Proof) MarshalJSON() ([]byte, error) {
  23. var pa proofAux
  24. pa.A = hex.EncodeToString(p.A.Marshal())
  25. pa.B = hex.EncodeToString(p.B.Marshal())
  26. pa.C = hex.EncodeToString(p.C.Marshal())
  27. return json.Marshal(pa)
  28. }
  29. func (p *Proof) UnmarshalJSON(data []byte) error {
  30. var pa proofAux
  31. if err := json.Unmarshal(data, &pa); err != nil {
  32. return err
  33. }
  34. aBytes, err := hex.DecodeString(pa.A)
  35. if err != nil {
  36. return err
  37. }
  38. p.A = new(bn256.G1)
  39. if _, err := p.A.Unmarshal(aBytes); err != nil {
  40. return err
  41. }
  42. bBytes, err := hex.DecodeString(pa.B)
  43. if err != nil {
  44. return err
  45. }
  46. p.B = new(bn256.G2)
  47. if _, err := p.B.Unmarshal(bBytes); err != nil {
  48. return err
  49. }
  50. cBytes, err := hex.DecodeString(pa.C)
  51. if err != nil {
  52. return err
  53. }
  54. p.C = new(bn256.G1)
  55. if _, err := p.C.Unmarshal(cBytes); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. // Pk holds the data structure of the ProvingKey
  61. type Pk struct {
  62. A []*bn256.G1
  63. B2 []*bn256.G2
  64. B1 []*bn256.G1
  65. C []*bn256.G1
  66. NVars int
  67. NPublic int
  68. VkAlpha1 *bn256.G1
  69. VkDelta1 *bn256.G1
  70. VkBeta1 *bn256.G1
  71. VkBeta2 *bn256.G2
  72. VkDelta2 *bn256.G2
  73. HExps []*bn256.G1
  74. DomainSize int
  75. PolsA []map[int]*big.Int
  76. PolsB []map[int]*big.Int
  77. PolsC []map[int]*big.Int
  78. }
  79. // Witness contains the witness
  80. type Witness []*big.Int
  81. // Vk is the Verification Key data structure
  82. type Vk struct {
  83. Alpha *bn256.G1
  84. Beta *bn256.G2
  85. Gamma *bn256.G2
  86. Delta *bn256.G2
  87. IC []*bn256.G1
  88. }