eth-kzg-ceremony-alt/powersoftau.go
powersoftau.goRaw
1package kzgceremony
2
3import (
4 "math/big"
5
6 "github.com/ethereum/go-ethereum/crypto/bls12381"
7)
8
9type Contribution struct {
10 SRS *SRS
11}
12
13type SRS struct {
14 G1s []*bls12381.PointG1
15 G2s []*bls12381.PointG2
16}
17
18type toxicWaste struct {
19 tau *big.Int
20 TauG2 *bls12381.PointG2
21}
22
23// newEmptySRS creates an empty SRS
24func newEmptySRS(nG1, nG2 int) *SRS {
25 g1s := make([]*bls12381.PointG1, nG1)
26 g2s := make([]*bls12381.PointG2, nG2)
27 g1 := bls12381.NewG1()
28 g2 := bls12381.NewG2()
29 // one_G1 := g1.One()
30 // one_G2 := g2.One()
31 for i := 0; i < nG1; i++ {
32 g1s[i] = g1.One()
33 // g1.MulScalar(g1s[i], one_G1, big.NewInt(int64(i)))
34 }
35 for i := 0; i < nG2; i++ {
36 g2s[i] = g2.One()
37 // g2.MulScalar(g2s[i], one_G2, big.NewInt(int64(i)))
38 }
39 return &SRS{g1s, g2s}
40}
41
42func tau(randomness []byte) *toxicWaste {
43 g2 := bls12381.NewG2()
44 tau := new(big.Int).Mod(
45 new(big.Int).SetBytes(randomness),
46 g2.Q())
47 TauG2 := g2.New()
48 g2.MulScalar(TauG2, g2.One(), tau)
49
50 return &toxicWaste{tau, TauG2}
51}
52
53func computeContribution(t *toxicWaste, prevSRS *SRS) *SRS {
54 srs := newEmptySRS(len(prevSRS.G1s), len(prevSRS.G2s))
55 g1 := bls12381.NewG1()
56 g2 := bls12381.NewG2()
57 Q := g1.Q() // Q = |G1| == |G2|
58
59 for i := 0; i < len(prevSRS.G1s); i++ {
60 tau_i := new(big.Int).Exp(t.tau, big.NewInt(int64(i)), Q)
61 g1.MulScalar(srs.G1s[i], prevSRS.G1s[i], tau_i)
62 }
63 for i := 0; i < len(prevSRS.G2s); i++ {
64 tau_i := new(big.Int).Exp(t.tau, big.NewInt(int64(i)), Q)
65 g2.MulScalar(srs.G2s[i], prevSRS.G2s[i], tau_i)
66 }
67
68 return srs
69}
70
71// Contribute
72func Contribute(prevSRS *SRS, randomness []byte) (Contribution, error) {
73 // set tau from randomness
74 tw := tau(randomness)
75
76 newSRS := computeContribution(tw, prevSRS)
77
78 return Contribution{SRS: newSRS}, nil
79}