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