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.

253 lines
7.4 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. // TODO validate data (G1 & G2 subgroup checks, etc)
  56. return err
  57. }
  58. func (s State) MarshalJSON() ([]byte, error) {
  59. var sStr stateStr
  60. sStr.ParticipantIDs = s.ParticipantIDs
  61. sStr.ParticipantECDSASignatures = s.ParticipantECDSASignatures
  62. sStr.Transcripts = make([]transcriptStr, len(s.Transcripts))
  63. for i := 0; i < len(s.Transcripts); i++ {
  64. if s.Transcripts[i].NumG1Powers != uint64(len(s.Transcripts[i].PowersOfTau.G1Powers)) {
  65. return nil, fmt.Errorf("wrong NumG1Powers")
  66. }
  67. if s.Transcripts[i].NumG2Powers != uint64(len(s.Transcripts[i].PowersOfTau.G2Powers)) {
  68. return nil, fmt.Errorf("wrong NumG2Powers")
  69. }
  70. sStr.Transcripts[i].NumG1Powers = s.Transcripts[i].NumG1Powers
  71. sStr.Transcripts[i].NumG2Powers = s.Transcripts[i].NumG2Powers
  72. sStr.Transcripts[i].PowersOfTau = powersOfTauStr{}
  73. sStr.Transcripts[i].PowersOfTau.G1Powers =
  74. g1PointsToStrings(s.Transcripts[i].PowersOfTau.G1Powers)
  75. sStr.Transcripts[i].PowersOfTau.G2Powers =
  76. g2PointsToStrings(s.Transcripts[i].PowersOfTau.G2Powers)
  77. sStr.Transcripts[i].Witness = witnessStr{}
  78. sStr.Transcripts[i].Witness.RunningProducts =
  79. g1PointsToStrings(s.Transcripts[i].Witness.RunningProducts)
  80. sStr.Transcripts[i].Witness.PotPubKeys =
  81. g2PointsToStrings(s.Transcripts[i].Witness.PotPubKeys)
  82. sStr.Transcripts[i].Witness.BLSSignatures =
  83. g1PointsToStrings(s.Transcripts[i].Witness.BLSSignatures)
  84. }
  85. return json.Marshal(sStr)
  86. }
  87. func (c *BatchContribution) UnmarshalJSON(b []byte) error {
  88. var cStr batchContributionStr
  89. if err := json.Unmarshal(b, &cStr); err != nil {
  90. return err
  91. }
  92. var err error
  93. g2 := bls12381.NewG2()
  94. c.Contributions = make([]Contribution, len(cStr.Contributions))
  95. for i := 0; i < len(cStr.Contributions); i++ {
  96. c.Contributions[i].NumG1Powers = cStr.Contributions[i].NumG1Powers
  97. c.Contributions[i].NumG2Powers = cStr.Contributions[i].NumG2Powers
  98. c.Contributions[i].PowersOfTau = &SRS{}
  99. c.Contributions[i].PowersOfTau.G1Powers, err =
  100. stringsToPointsG1(cStr.Contributions[i].PowersOfTau.G1Powers)
  101. if err != nil {
  102. return err
  103. }
  104. c.Contributions[i].PowersOfTau.G2Powers, err =
  105. stringsToPointsG2(cStr.Contributions[i].PowersOfTau.G2Powers)
  106. if err != nil {
  107. return err
  108. }
  109. g2sBytes, err := hex.DecodeString(strings.TrimPrefix(cStr.Contributions[i].PotPubKey, "0x"))
  110. if err != nil {
  111. return err
  112. }
  113. c.Contributions[i].PotPubKey, err = g2.FromCompressed(g2sBytes)
  114. if err != nil {
  115. return err
  116. }
  117. }
  118. return err
  119. }
  120. func (c BatchContribution) MarshalJSON() ([]byte, error) {
  121. var cStr batchContributionStr
  122. g2 := bls12381.NewG2()
  123. cStr.Contributions = make([]contributionStr, len(c.Contributions))
  124. for i := 0; i < len(c.Contributions); i++ {
  125. cStr.Contributions[i].NumG1Powers = c.Contributions[i].NumG1Powers
  126. cStr.Contributions[i].NumG2Powers = c.Contributions[i].NumG2Powers
  127. cStr.Contributions[i].PowersOfTau = powersOfTauStr{}
  128. cStr.Contributions[i].PowersOfTau.G1Powers =
  129. g1PointsToStrings(c.Contributions[i].PowersOfTau.G1Powers)
  130. cStr.Contributions[i].PowersOfTau.G2Powers =
  131. g2PointsToStrings(c.Contributions[i].PowersOfTau.G2Powers)
  132. cStr.Contributions[i].PotPubKey = "0x" + hex.EncodeToString(g2.ToCompressed(c.Contributions[i].PotPubKey))
  133. }
  134. return json.Marshal(cStr)
  135. }
  136. type powersOfTauStr struct {
  137. G1Powers []string `json:"G1Powers"`
  138. G2Powers []string `json:"G2Powers"`
  139. }
  140. type witnessStr struct {
  141. RunningProducts []string `json:"runningProducts"`
  142. PotPubKeys []string `json:"potPubkeys"`
  143. BLSSignatures []string `json:"blsSignatures"`
  144. }
  145. type transcriptStr struct {
  146. NumG1Powers uint64 `json:"numG1Powers"`
  147. NumG2Powers uint64 `json:"numG2Powers"`
  148. PowersOfTau powersOfTauStr `json:"powersOfTau"`
  149. Witness witnessStr `json:"witness"`
  150. }
  151. type contributionStr struct {
  152. NumG1Powers uint64 `json:"numG1Powers"`
  153. NumG2Powers uint64 `json:"numG2Powers"`
  154. PowersOfTau powersOfTauStr `json:"powersOfTau"`
  155. PotPubKey string `json:"potPubkey"`
  156. }
  157. type batchContributionStr struct {
  158. Contributions []contributionStr `json:"contributions"`
  159. }
  160. type stateStr struct {
  161. Transcripts []transcriptStr `json:"transcripts"`
  162. ParticipantIDs []string `json:"participantIds"`
  163. ParticipantECDSASignatures []string `json:"participantEcdsaSignatures"`
  164. }
  165. func g1PointsToStrings(points []*bls12381.PointG1) []string {
  166. g1 := bls12381.NewG1() // TODO unify g1 instantiation (& g2)
  167. n := len(points)
  168. g1s := make([]string, n)
  169. for i := 0; i < n; i++ {
  170. if points[i] == nil {
  171. g1s[i] = ""
  172. continue
  173. }
  174. g1s[i] = "0x" + hex.EncodeToString(g1.ToCompressed(points[i]))
  175. }
  176. return g1s
  177. }
  178. func g2PointsToStrings(points []*bls12381.PointG2) []string {
  179. g2 := bls12381.NewG2()
  180. n := len(points)
  181. g2s := make([]string, n)
  182. for i := 0; i < n; i++ {
  183. if points[i] == nil {
  184. g2s[i] = ""
  185. continue
  186. }
  187. g2s[i] = "0x" + hex.EncodeToString(g2.ToCompressed(points[i]))
  188. }
  189. return g2s
  190. }
  191. func stringsToPointsG1(s []string) ([]*bls12381.PointG1, error) {
  192. g1 := bls12381.NewG1() // TODO unify g1 instantiation (& g2)
  193. n := len(s)
  194. g1s := make([]*bls12381.PointG1, n)
  195. for i := 0; i < n; i++ {
  196. if s[i] == "" {
  197. continue
  198. }
  199. g1sBytes, err := hex.DecodeString(strings.TrimPrefix(s[i], "0x"))
  200. if err != nil {
  201. return nil, err
  202. }
  203. g1s_i, err := g1.FromCompressed(g1sBytes)
  204. if err != nil {
  205. return nil, err
  206. }
  207. g1s[i] = g1s_i
  208. }
  209. return g1s, nil
  210. }
  211. func stringsToPointsG2(s []string) ([]*bls12381.PointG2, error) {
  212. g2 := bls12381.NewG2()
  213. n := len(s)
  214. g2s := make([]*bls12381.PointG2, n)
  215. for i := 0; i < n; i++ {
  216. if s[i] == "" {
  217. continue
  218. }
  219. g2sBytes, err := hex.DecodeString(strings.TrimPrefix(s[i], "0x"))
  220. if err != nil {
  221. return nil, err
  222. }
  223. g2s_i, err := g2.FromCompressed(g2sBytes)
  224. if err != nil {
  225. return nil, err
  226. }
  227. g2s[i] = g2s_i
  228. }
  229. return g2s, nil
  230. }