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.

186 lines
5.2 KiB

  1. package kzgceremony
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. bls12381 "github.com/kilic/bls12-381"
  8. )
  9. func (s *State) UnmarshalJSON(b []byte) error {
  10. var sStr stateStr
  11. if err := json.Unmarshal(b, &sStr); err != nil {
  12. return err
  13. }
  14. var err error
  15. s.ParticipantIDs = sStr.ParticipantIDs
  16. s.ParticipantECDSASignatures = sStr.ParticipantECDSASignatures
  17. s.Transcripts = make([]Transcript, len(sStr.Transcripts))
  18. for i := 0; i < len(sStr.Transcripts); i++ {
  19. if sStr.Transcripts[i].NumG1Powers != uint64(len(sStr.Transcripts[i].PowersOfTau.G1Powers)) {
  20. return fmt.Errorf("wrong NumG1Powers")
  21. }
  22. if sStr.Transcripts[i].NumG2Powers != uint64(len(sStr.Transcripts[i].PowersOfTau.G2Powers)) {
  23. return fmt.Errorf("wrong NumG2Powers")
  24. }
  25. s.Transcripts[i].NumG1Powers = sStr.Transcripts[i].NumG1Powers
  26. s.Transcripts[i].NumG2Powers = sStr.Transcripts[i].NumG2Powers
  27. s.Transcripts[i].PowersOfTau = &SRS{}
  28. s.Transcripts[i].PowersOfTau.G1Powers, err =
  29. stringsToPointsG1(sStr.Transcripts[i].PowersOfTau.G1Powers)
  30. if err != nil {
  31. return err
  32. }
  33. s.Transcripts[i].PowersOfTau.G2Powers, err =
  34. stringsToPointsG2(sStr.Transcripts[i].PowersOfTau.G2Powers)
  35. if err != nil {
  36. return err
  37. }
  38. s.Transcripts[i].Witness = &Witness{}
  39. s.Transcripts[i].Witness.RunningProducts, err =
  40. stringsToPointsG1(sStr.Transcripts[i].Witness.RunningProducts)
  41. if err != nil {
  42. return err
  43. }
  44. s.Transcripts[i].Witness.PotPubKeys, err =
  45. stringsToPointsG2(sStr.Transcripts[i].Witness.PotPubKeys)
  46. if err != nil {
  47. return err
  48. }
  49. s.Transcripts[i].Witness.BLSSignatures, err =
  50. stringsToPointsG1(sStr.Transcripts[i].Witness.BLSSignatures)
  51. if err != nil {
  52. return err
  53. }
  54. }
  55. return err
  56. }
  57. func (s State) MarshalJSON() ([]byte, error) {
  58. var sStr stateStr
  59. sStr.ParticipantIDs = s.ParticipantIDs
  60. sStr.ParticipantECDSASignatures = s.ParticipantECDSASignatures
  61. sStr.Transcripts = make([]transcriptStr, len(s.Transcripts))
  62. for i := 0; i < len(s.Transcripts); i++ {
  63. if s.Transcripts[i].NumG1Powers != uint64(len(s.Transcripts[i].PowersOfTau.G1Powers)) {
  64. return nil, fmt.Errorf("wrong NumG1Powers")
  65. }
  66. if s.Transcripts[i].NumG2Powers != uint64(len(s.Transcripts[i].PowersOfTau.G2Powers)) {
  67. return nil, fmt.Errorf("wrong NumG2Powers")
  68. }
  69. sStr.Transcripts[i].NumG1Powers = s.Transcripts[i].NumG1Powers
  70. sStr.Transcripts[i].NumG2Powers = s.Transcripts[i].NumG2Powers
  71. sStr.Transcripts[i].PowersOfTau = powersOfTauStr{}
  72. sStr.Transcripts[i].PowersOfTau.G1Powers =
  73. g1PointsToStrings(s.Transcripts[i].PowersOfTau.G1Powers)
  74. sStr.Transcripts[i].PowersOfTau.G2Powers =
  75. g2PointsToStrings(s.Transcripts[i].PowersOfTau.G2Powers)
  76. sStr.Transcripts[i].Witness = witnessStr{}
  77. sStr.Transcripts[i].Witness.RunningProducts =
  78. g1PointsToStrings(s.Transcripts[i].Witness.RunningProducts)
  79. sStr.Transcripts[i].Witness.PotPubKeys =
  80. g2PointsToStrings(s.Transcripts[i].Witness.PotPubKeys)
  81. sStr.Transcripts[i].Witness.BLSSignatures =
  82. g1PointsToStrings(s.Transcripts[i].Witness.BLSSignatures)
  83. }
  84. return json.Marshal(sStr)
  85. }
  86. type powersOfTauStr struct {
  87. G1Powers []string `json:"G1Powers"`
  88. G2Powers []string `json:"G2Powers"`
  89. }
  90. type witnessStr struct {
  91. RunningProducts []string `json:"runningProducts"`
  92. PotPubKeys []string `json:"potPubkeys"`
  93. BLSSignatures []string `json:"blsSignatures"`
  94. }
  95. type transcriptStr struct {
  96. NumG1Powers uint64 `json:"numG1Powers"`
  97. NumG2Powers uint64 `json:"numG2Powers"`
  98. PowersOfTau powersOfTauStr `json:"powersOfTau"`
  99. Witness witnessStr `json:"witness"`
  100. }
  101. type stateStr struct {
  102. Transcripts []transcriptStr `json:"transcripts"`
  103. ParticipantIDs []string `json:"participantIds"`
  104. ParticipantECDSASignatures []string `json:"participantEcdsaSignatures"`
  105. }
  106. func g1PointsToStrings(points []*bls12381.PointG1) []string {
  107. g1 := bls12381.NewG1() // TODO unify g1 instantiation (& g2)
  108. n := len(points)
  109. g1s := make([]string, n)
  110. for i := 0; i < n; i++ {
  111. if points[i] == nil {
  112. g1s[i] = ""
  113. continue
  114. }
  115. g1s[i] = "0x" + hex.EncodeToString(g1.ToCompressed(points[i]))
  116. }
  117. return g1s
  118. }
  119. func g2PointsToStrings(points []*bls12381.PointG2) []string {
  120. g2 := bls12381.NewG2()
  121. n := len(points)
  122. g2s := make([]string, n)
  123. for i := 0; i < n; i++ {
  124. if points[i] == nil {
  125. g2s[i] = ""
  126. continue
  127. }
  128. g2s[i] = "0x" + hex.EncodeToString(g2.ToCompressed(points[i]))
  129. }
  130. return g2s
  131. }
  132. func stringsToPointsG1(s []string) ([]*bls12381.PointG1, error) {
  133. g1 := bls12381.NewG1() // TODO unify g1 instantiation (& g2)
  134. n := len(s)
  135. g1s := make([]*bls12381.PointG1, n)
  136. for i := 0; i < n; i++ {
  137. if s[i] == "" {
  138. continue
  139. }
  140. g1sBytes, err := hex.DecodeString(strings.TrimPrefix(s[i], "0x"))
  141. if err != nil {
  142. return nil, err
  143. }
  144. g1s_i, err := g1.FromCompressed(g1sBytes)
  145. if err != nil {
  146. return nil, err
  147. }
  148. g1s[i] = g1s_i
  149. }
  150. return g1s, nil
  151. }
  152. func stringsToPointsG2(s []string) ([]*bls12381.PointG2, error) {
  153. g2 := bls12381.NewG2()
  154. n := len(s)
  155. g2s := make([]*bls12381.PointG2, n)
  156. for i := 0; i < n; i++ {
  157. if s[i] == "" {
  158. continue
  159. }
  160. g2sBytes, err := hex.DecodeString(strings.TrimPrefix(s[i], "0x"))
  161. if err != nil {
  162. return nil, err
  163. }
  164. g2s_i, err := g2.FromCompressed(g2sBytes)
  165. if err != nil {
  166. return nil, err
  167. }
  168. g2s[i] = g2s_i
  169. }
  170. return g2s, nil
  171. }