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.

61 lines
1.4 KiB

  1. package kzgceremony
  2. import (
  3. "encoding/hex"
  4. "strings"
  5. bls12381 "github.com/kilic/bls12-381"
  6. )
  7. // WIP
  8. // ParseEthJSON parses the eth-kzg-ceremony SRS json format
  9. func ParseEthJSON(g1sStr, g2sStr []string) (*SRS, error) {
  10. g1 := bls12381.NewG1()
  11. g2 := bls12381.NewG2()
  12. nG1s := len(g1sStr)
  13. nG2s := len(g2sStr)
  14. g1s := make([]*bls12381.PointG1, nG1s)
  15. g2s := make([]*bls12381.PointG2, nG2s)
  16. for i := 0; i < nG1s; i++ {
  17. g1sBytes, err := hex.DecodeString(strings.TrimPrefix(g1sStr[i], "0x"))
  18. if err != nil {
  19. return nil, err
  20. }
  21. g1s_i, err := g1.FromCompressed(g1sBytes)
  22. if err != nil {
  23. return nil, err
  24. }
  25. g1s[i] = g1s_i
  26. }
  27. for i := 0; i < nG2s; i++ {
  28. g2sBytes, err := hex.DecodeString(strings.TrimPrefix(g2sStr[i], "0x"))
  29. if err != nil {
  30. return nil, err
  31. }
  32. g2s_i, err := g2.FromCompressed(g2sBytes)
  33. if err != nil {
  34. return nil, err
  35. }
  36. g2s[i] = g2s_i
  37. }
  38. return &SRS{G1s: g1s, G2s: g2s}, nil
  39. }
  40. // ToEthJSON outputs the SRS into the eth-kzg-ceremony SRS json format
  41. func (srs *SRS) ToEthJSON() ([]string, []string, error) {
  42. g1 := bls12381.NewG1()
  43. g2 := bls12381.NewG2()
  44. nG1s := len(srs.G1s)
  45. nG2s := len(srs.G2s)
  46. g1s := make([]string, nG1s)
  47. g2s := make([]string, nG2s)
  48. for i := 0; i < nG1s; i++ {
  49. g1s[i] = "0x" + hex.EncodeToString(g1.ToCompressed(srs.G1s[i]))
  50. }
  51. for i := 0; i < nG2s; i++ {
  52. g2s[i] = "0x" + hex.EncodeToString(g2.ToCompressed(srs.G2s[i]))
  53. }
  54. return g1s, g2s, nil
  55. }