forked from dcbuild3r/ptau-deserializer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
phase1.go
193 lines (165 loc) · 5.52 KB
/
phase1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package deserializer
import (
"bufio"
"fmt"
"os"
"github.com/consensys/gnark-crypto/ecc/bn254"
curve "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark/backend/groth16/bn254/mpcsetup"
)
func ConvertPtauToPhase1(ptau Ptau) (phase1 mpcsetup.Phase1, err error) {
tauG1 := make([]curve.G1Affine, len(ptau.PTauPubKey.TauG1))
for i, g1 := range ptau.PTauPubKey.TauG1 {
g1Affine := curve.G1Affine{}
x := bytesToElement(g1[0].Bytes())
g1Affine.X = x
y := bytesToElement(g1[1].Bytes())
g1Affine.Y = y
// fmt.Printf("X: %v \n", g1Affine.X.String())
// fmt.Printf("Y: %v \n", g1Affine.Y.String())
// fmt.Printf("g1Affine: %v \n", g1Affine)
if !g1Affine.IsOnCurve() {
fmt.Printf("tauG1: \n index: %v g1Affine.X: %v \n g1Affine.Y: %v \n", i, g1Affine.X.String(), g1Affine.Y.String())
panic("g1Affine is not on curve")
}
tauG1[i] = g1Affine
}
alphaTauG1 := make([]curve.G1Affine, len(ptau.PTauPubKey.AlphaTauG1))
for i, g1 := range ptau.PTauPubKey.AlphaTauG1 {
g1Affine := curve.G1Affine{}
x := bytesToElement(g1[0].Bytes())
g1Affine.X = x
y := bytesToElement(g1[1].Bytes())
g1Affine.Y = y
if !g1Affine.IsOnCurve() {
fmt.Printf("alphaTauG1: \n index: %v g1Affine.X: %v \n g1Affine.Y: %v \n", i, g1Affine.X.String(), g1Affine.Y.String())
panic("g1Affine is not on curve")
}
alphaTauG1[i] = g1Affine
}
// fmt.Printf("alphaTauG1: %v \n", alphaTauG1)
betaTauG1 := make([]curve.G1Affine, len(ptau.PTauPubKey.BetaTauG1))
for i, g1 := range ptau.PTauPubKey.BetaTauG1 {
g1Affine := curve.G1Affine{}
x := bytesToElement(g1[0].Bytes())
g1Affine.X = x
y := bytesToElement(g1[1].Bytes())
g1Affine.Y = y
if !g1Affine.IsOnCurve() {
fmt.Printf("betaTauG1: \n index: %v, g1Affine.X: %v \n g1Affine.Y: %v \n", i, g1Affine.X.String(), g1Affine.Y.String())
panic("g1Affine is not on curve")
}
betaTauG1[i] = g1Affine
}
// fmt.Printf("betaTauG1: %v \n", betaTauG1)
// fmt.Printf("tauG1: %v \n", tauG1)
tauG2 := make([]curve.G2Affine, len(ptau.PTauPubKey.TauG2))
for i, g2 := range ptau.PTauPubKey.TauG2 {
g2Affine := curve.G2Affine{}
x0 := bytesToElement(g2[0].Bytes())
x1 := bytesToElement(g2[1].Bytes())
g2Affine.X.A0 = x0
g2Affine.X.A1 = x1
y0 := bytesToElement(g2[2].Bytes())
y1 := bytesToElement(g2[3].Bytes())
g2Affine.Y.A0 = y0
g2Affine.Y.A1 = y1
// fmt.Printf("X: %v \n", g2Affine.X.String())
// fmt.Printf("Y: %v \n", g2Affine.Y.String())
// fmt.Printf("g2Affine %v: %v \n", i, g2Affine)
if !g2Affine.IsOnCurve() {
fmt.Printf("tauG2: \n index: %v, g2Affine.X.A0: %v \n g2Affine.X.A1: %v \n g2Affine.Y.A0: %v \n g2Affine.Y.A1 %v \n", i, g2Affine.X.A0.String(), g2Affine.X.A1.String(), g2Affine.Y.A0.String(), g2Affine.Y.A1.String())
panic("g2Affine is not on curve")
}
tauG2[i] = g2Affine
}
// fmt.Printf("tauG2: %v \n", tauG2)
betaG2 := curve.G2Affine{}
{
g2 := ptau.PTauPubKey.BetaG2
x0 := bytesToElement(g2[0].Bytes())
x1 := bytesToElement(g2[1].Bytes())
betaG2.X.A0 = x0
betaG2.X.A1 = x1
y0 := bytesToElement(g2[2].Bytes())
y1 := bytesToElement(g2[3].Bytes())
betaG2.Y.A0 = y0
betaG2.Y.A1 = y1
if !betaG2.IsOnCurve() {
fmt.Printf("g2Affine.X.A0: %v \n g2Affine.X.A1: %v \n g2Affine.Y.A0: %v \n g2Affine.Y.A1 %v \n", betaG2.X.A0.String(), betaG2.X.String(), betaG2.Y.A0.String(), betaG2.Y.A1.String())
panic("g2Affine is not on curve")
}
}
phase1.Parameters.G1.Tau = tauG1
phase1.Parameters.G1.AlphaTau = alphaTauG1
phase1.Parameters.G1.BetaTau = betaTauG1
phase1.Parameters.G2.Tau = tauG2
phase1.Parameters.G2.Beta = betaG2
return phase1, nil
}
func WritePhase1FromPtauFile(ptauFile *PtauFile, outputPath string) error {
outputFile, err := os.Create(outputPath)
if err != nil {
return err
}
defer outputFile.Close()
var header Header
writer := bufio.NewWriter(outputFile)
defer writer.Flush()
N := ptauFile.DomainSize()
fmt.Printf("Power %d supports up to %d constraints\n", ptauFile.Header.Power, N)
header.Power = byte(ptauFile.Header.Power)
// can be extracted from ptau.Contributions (7) but hardcoding for now
// ptau link: https://github.com/iden3/snarkjs/tree/master#7-prepare-phase-2
header.Contributions = 54
// Write the header
err = header.writeTo(outputFile)
if err != nil {
return err
}
// BN254 encoder using compressed representation of points to save storage space
enc := bn254.NewEncoder(writer)
fmt.Println("1. Writing TauG1")
tauG1 := make(chan curve.G1Affine, 10000)
go ptauFile.ReadTauG1(tauG1)
for point := range tauG1 {
if err := enc.Encode(&point); err != nil {
return err
}
}
// Write α[τ⁰]₁, α[τ¹]₁, α[τ²]₁, …, α[τᴺ⁻¹]₁
fmt.Println("2. Writing AlphaTauG1")
alphaTauG1 := make(chan curve.G1Affine, 10000)
go ptauFile.ReadAlphaTauG1(alphaTauG1)
for point := range alphaTauG1 {
if err := enc.Encode(&point); err != nil {
return err
}
}
// Write β[τ⁰]₁, β[τ¹]₁, β[τ²]₁, …, β[τᴺ⁻¹]₁
fmt.Println("3. Writing BetaTauG1")
betaTauG1 := make(chan curve.G1Affine, 10000)
go ptauFile.ReadBetaTauG1(betaTauG1)
for point := range betaTauG1 {
if err := enc.Encode(&point); err != nil {
return err
}
}
// Write {[τ⁰]₂, [τ¹]₂, [τ²]₂, …, [τᴺ⁻¹]₂}
fmt.Println("4. Writing TauG2")
tauG2 := make(chan curve.G2Affine, 10000)
go ptauFile.ReadTauG2(tauG2)
for point := range tauG2 {
if err := enc.Encode(&point); err != nil {
return err
}
}
// Write [β]₂
fmt.Println("5. Writing BetaG2")
betaG2, err := ptauFile.ReadBetaG2()
if err != nil {
return err
}
enc.Encode(&betaG2)
return nil
}