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