forked from worldcoin/semaphore-mtb-setup
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevals.go
More file actions
183 lines (165 loc) · 4.74 KB
/
evals.go
File metadata and controls
183 lines (165 loc) · 4.74 KB
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
package main
import (
"encoding/binary"
"io"
curve "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark/backend/groth16/bn254/mpcsetup"
)
// writePhase2Evaluations serializes Phase2Evaluations to a writer
// This is needed because the new gnark API doesn't provide WriteTo/ReadFrom for Phase2Evaluations
func writePhase2Evaluations(evals *mpcsetup.Phase2Evaluations, w io.Writer) error {
enc := curve.NewEncoder(w)
// Write G1.A
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.G1.A))); err != nil {
return err
}
for i := range evals.G1.A {
if err := enc.Encode(&evals.G1.A[i]); err != nil {
return err
}
}
// Write G1.B
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.G1.B))); err != nil {
return err
}
for i := range evals.G1.B {
if err := enc.Encode(&evals.G1.B[i]); err != nil {
return err
}
}
// Write G1.VKK
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.G1.VKK))); err != nil {
return err
}
for i := range evals.G1.VKK {
if err := enc.Encode(&evals.G1.VKK[i]); err != nil {
return err
}
}
// Write G1.CKK (slice of slices)
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.G1.CKK))); err != nil {
return err
}
for i := range evals.G1.CKK {
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.G1.CKK[i]))); err != nil {
return err
}
for j := range evals.G1.CKK[i] {
if err := enc.Encode(&evals.G1.CKK[i][j]); err != nil {
return err
}
}
}
// Write G2.B
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.G2.B))); err != nil {
return err
}
for i := range evals.G2.B {
if err := enc.Encode(&evals.G2.B[i]); err != nil {
return err
}
}
// Write PublicAndCommitmentCommitted (slice of slices of int)
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.PublicAndCommitmentCommitted))); err != nil {
return err
}
for i := range evals.PublicAndCommitmentCommitted {
if err := binary.Write(w, binary.BigEndian, uint32(len(evals.PublicAndCommitmentCommitted[i]))); err != nil {
return err
}
for j := range evals.PublicAndCommitmentCommitted[i] {
if err := binary.Write(w, binary.BigEndian, int32(evals.PublicAndCommitmentCommitted[i][j])); err != nil {
return err
}
}
}
return nil
}
// readPhase2Evaluations deserializes Phase2Evaluations from a reader
func readPhase2Evaluations(r io.Reader) (*mpcsetup.Phase2Evaluations, error) {
dec := curve.NewDecoder(r)
evals := &mpcsetup.Phase2Evaluations{}
// Read G1.A
var lenA uint32
if err := binary.Read(r, binary.BigEndian, &lenA); err != nil {
return nil, err
}
evals.G1.A = make([]curve.G1Affine, lenA)
for i := range evals.G1.A {
if err := dec.Decode(&evals.G1.A[i]); err != nil {
return nil, err
}
}
// Read G1.B
var lenB uint32
if err := binary.Read(r, binary.BigEndian, &lenB); err != nil {
return nil, err
}
evals.G1.B = make([]curve.G1Affine, lenB)
for i := range evals.G1.B {
if err := dec.Decode(&evals.G1.B[i]); err != nil {
return nil, err
}
}
// Read G1.VKK
var lenVKK uint32
if err := binary.Read(r, binary.BigEndian, &lenVKK); err != nil {
return nil, err
}
evals.G1.VKK = make([]curve.G1Affine, lenVKK)
for i := range evals.G1.VKK {
if err := dec.Decode(&evals.G1.VKK[i]); err != nil {
return nil, err
}
}
// Read G1.CKK (slice of slices)
var lenCKK uint32
if err := binary.Read(r, binary.BigEndian, &lenCKK); err != nil {
return nil, err
}
evals.G1.CKK = make([][]curve.G1Affine, lenCKK)
for i := range evals.G1.CKK {
var innerLen uint32
if err := binary.Read(r, binary.BigEndian, &innerLen); err != nil {
return nil, err
}
evals.G1.CKK[i] = make([]curve.G1Affine, innerLen)
for j := range evals.G1.CKK[i] {
if err := dec.Decode(&evals.G1.CKK[i][j]); err != nil {
return nil, err
}
}
}
// Read G2.B
var lenG2B uint32
if err := binary.Read(r, binary.BigEndian, &lenG2B); err != nil {
return nil, err
}
evals.G2.B = make([]curve.G2Affine, lenG2B)
for i := range evals.G2.B {
if err := dec.Decode(&evals.G2.B[i]); err != nil {
return nil, err
}
}
// Read PublicAndCommitmentCommitted (slice of slices of int)
var lenPACC uint32
if err := binary.Read(r, binary.BigEndian, &lenPACC); err != nil {
return nil, err
}
evals.PublicAndCommitmentCommitted = make([][]int, lenPACC)
for i := range evals.PublicAndCommitmentCommitted {
var innerLen uint32
if err := binary.Read(r, binary.BigEndian, &innerLen); err != nil {
return nil, err
}
evals.PublicAndCommitmentCommitted[i] = make([]int, innerLen)
for j := range evals.PublicAndCommitmentCommitted[i] {
var val int32
if err := binary.Read(r, binary.BigEndian, &val); err != nil {
return nil, err
}
evals.PublicAndCommitmentCommitted[i][j] = int(val)
}
}
return evals, nil
}