-
Notifications
You must be signed in to change notification settings - Fork 162
/
marshal.go
291 lines (248 loc) · 7.77 KB
/
marshal.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2020 Consensys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package kzg
import (
"github.com/consensys/gnark-crypto/ecc/bls12-381"
"io"
"github.com/consensys/gnark-crypto/utils/unsafe"
)
// WriteTo writes binary encoding of the ProvingKey
func (pk *ProvingKey) WriteTo(w io.Writer) (int64, error) {
return pk.writeTo(w)
}
// WriteRawTo writes binary encoding of ProvingKey to w without point compression
func (pk *ProvingKey) WriteRawTo(w io.Writer) (int64, error) {
return pk.writeTo(w, bls12381.RawEncoding())
}
func (pk *ProvingKey) writeTo(w io.Writer, options ...func(*bls12381.Encoder)) (int64, error) {
// encode the ProvingKey
enc := bls12381.NewEncoder(w, options...)
if err := enc.Encode(pk.G1); err != nil {
return enc.BytesWritten(), err
}
return enc.BytesWritten(), nil
}
// WriteRawTo writes binary encoding of VerifyingKey to w without point compression
func (vk *VerifyingKey) WriteRawTo(w io.Writer) (int64, error) {
return vk.writeTo(w, bls12381.RawEncoding())
}
// WriteTo writes binary encoding of the VerifyingKey
func (vk *VerifyingKey) WriteTo(w io.Writer) (int64, error) {
return vk.writeTo(w)
}
func (vk *VerifyingKey) writeTo(w io.Writer, options ...func(*bls12381.Encoder)) (int64, error) {
// encode the VerifyingKey
enc := bls12381.NewEncoder(w, options...)
nLines := 63
toEncode := make([]interface{}, 0, 4*nLines+3)
toEncode = append(toEncode, &vk.G2[0])
toEncode = append(toEncode, &vk.G2[1])
toEncode = append(toEncode, &vk.G1)
for k := 0; k < 2; k++ {
for j := 0; j < 2; j++ {
for i := nLines - 1; i >= 0; i-- {
toEncode = append(toEncode, &vk.Lines[k][j][i].R0)
toEncode = append(toEncode, &vk.Lines[k][j][i].R1)
}
}
}
for _, v := range toEncode {
if err := enc.Encode(v); err != nil {
return enc.BytesWritten(), err
}
}
return enc.BytesWritten(), nil
}
// WriteDump writes the binary encoding of the entire SRS memory representation
// It is meant to be use to achieve fast serialization/deserialization and
// is not compatible with WriteTo / ReadFrom. It does not do any validation
// and doesn't encode points in a canonical form.
// @unsafe: this is platform dependent and may not be compatible with other platforms
// @unstable: the format may change in the future
// If maxPkPoints is provided, the number of points in the ProvingKey will be limited to maxPkPoints
func (srs *SRS) WriteDump(w io.Writer, maxPkPoints ...int) error {
maxG1 := len(srs.Pk.G1)
if len(maxPkPoints) > 0 && maxPkPoints[0] < maxG1 && maxPkPoints[0] > 0 {
maxG1 = maxPkPoints[0]
}
// first we write the VerifyingKey; it is small so we re-use WriteTo
if _, err := srs.Vk.writeTo(w, bls12381.RawEncoding()); err != nil {
return err
}
// write the marker
if err := unsafe.WriteMarker(w); err != nil {
return err
}
// write the slice
return unsafe.WriteSlice(w, srs.Pk.G1[:maxG1])
}
// ReadDump deserializes the SRS from a reader, as written by WriteDump
func (srs *SRS) ReadDump(r io.Reader, maxPkPoints ...int) error {
// first we read the VerifyingKey; it is small so we re-use ReadFrom
_, err := srs.Vk.ReadFrom(r)
if err != nil {
return err
}
// read the marker
if err := unsafe.ReadMarker(r); err != nil {
return err
}
// read the slice
srs.Pk.G1, _, err = unsafe.ReadSlice[[]bls12381.G1Affine](r, maxPkPoints...)
return err
}
// WriteTo writes binary encoding of the entire SRS
func (srs *SRS) WriteTo(w io.Writer) (int64, error) {
// encode the SRS
var pn, vn int64
var err error
if pn, err = srs.Pk.WriteTo(w); err != nil {
return pn, err
}
vn, err = srs.Vk.WriteTo(w)
return pn + vn, err
}
// WriteRawTo writes binary encoding of the entire SRS without point compression
func (srs *SRS) WriteRawTo(w io.Writer) (int64, error) {
// encode the SRS
var pn, vn int64
var err error
if pn, err = srs.Pk.WriteRawTo(w); err != nil {
return pn, err
}
vn, err = srs.Vk.WriteRawTo(w)
return pn + vn, err
}
// ReadFrom decodes ProvingKey data from reader.
func (pk *ProvingKey) ReadFrom(r io.Reader) (int64, error) {
// decode the ProvingKey
dec := bls12381.NewDecoder(r)
if err := dec.Decode(&pk.G1); err != nil {
return dec.BytesRead(), err
}
return dec.BytesRead(), nil
}
// UnsafeReadFrom decodes ProvingKey data from reader without checking
// that point are in the correct subgroup.
func (pk *ProvingKey) UnsafeReadFrom(r io.Reader) (int64, error) {
// decode the ProvingKey
dec := bls12381.NewDecoder(r, bls12381.NoSubgroupChecks())
if err := dec.Decode(&pk.G1); err != nil {
return dec.BytesRead(), err
}
return dec.BytesRead(), nil
}
// ReadFrom decodes VerifyingKey data from reader.
func (vk *VerifyingKey) ReadFrom(r io.Reader) (int64, error) {
// decode the VerifyingKey
dec := bls12381.NewDecoder(r)
nLines := 63
toDecode := make([]interface{}, 0, 4*nLines+3)
toDecode = append(toDecode, &vk.G2[0])
toDecode = append(toDecode, &vk.G2[1])
toDecode = append(toDecode, &vk.G1)
for k := 0; k < 2; k++ {
for j := 0; j < 2; j++ {
for i := nLines - 1; i >= 0; i-- {
toDecode = append(toDecode, &vk.Lines[k][j][i].R0)
toDecode = append(toDecode, &vk.Lines[k][j][i].R1)
}
}
}
for _, v := range toDecode {
if err := dec.Decode(v); err != nil {
return dec.BytesRead(), err
}
}
return dec.BytesRead(), nil
}
// ReadFrom decodes SRS data from reader.
func (srs *SRS) ReadFrom(r io.Reader) (int64, error) {
// decode the VerifyingKey
var pn, vn int64
var err error
if pn, err = srs.Pk.ReadFrom(r); err != nil {
return pn, err
}
vn, err = srs.Vk.ReadFrom(r)
return pn + vn, err
}
// UnsafeReadFrom decodes SRS data from reader without sub group checks
func (srs *SRS) UnsafeReadFrom(r io.Reader) (int64, error) {
// decode the VerifyingKey
var pn, vn int64
var err error
if pn, err = srs.Pk.UnsafeReadFrom(r); err != nil {
return pn, err
}
vn, err = srs.Vk.ReadFrom(r)
return pn + vn, err
}
// WriteTo writes binary encoding of a OpeningProof
func (proof *OpeningProof) WriteTo(w io.Writer) (int64, error) {
enc := bls12381.NewEncoder(w)
toEncode := []interface{}{
&proof.H,
&proof.ClaimedValue,
}
for _, v := range toEncode {
if err := enc.Encode(v); err != nil {
return enc.BytesWritten(), err
}
}
return enc.BytesWritten(), nil
}
// ReadFrom decodes OpeningProof data from reader.
func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) {
dec := bls12381.NewDecoder(r)
toDecode := []interface{}{
&proof.H,
&proof.ClaimedValue,
}
for _, v := range toDecode {
if err := dec.Decode(v); err != nil {
return dec.BytesRead(), err
}
}
return dec.BytesRead(), nil
}
// WriteTo writes binary encoding of a BatchOpeningProof
func (proof *BatchOpeningProof) WriteTo(w io.Writer) (int64, error) {
enc := bls12381.NewEncoder(w)
toEncode := []interface{}{
&proof.H,
proof.ClaimedValues,
}
for _, v := range toEncode {
if err := enc.Encode(v); err != nil {
return enc.BytesWritten(), err
}
}
return enc.BytesWritten(), nil
}
// ReadFrom decodes BatchOpeningProof data from reader.
func (proof *BatchOpeningProof) ReadFrom(r io.Reader) (int64, error) {
dec := bls12381.NewDecoder(r)
toDecode := []interface{}{
&proof.H,
&proof.ClaimedValues,
}
for _, v := range toDecode {
if err := dec.Decode(v); err != nil {
return dec.BytesRead(), err
}
}
return dec.BytesRead(), nil
}