-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.go
More file actions
294 lines (242 loc) · 8.37 KB
/
Copy pathserialization.go
File metadata and controls
294 lines (242 loc) · 8.37 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
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
292
293
294
package bulletproofs
import (
"encoding/binary"
"errors"
"fmt"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
)
// Point and scalar sizes derived from gnark-crypto library constants.
const (
compressedG1Size = bn254.SizeOfG1AffineCompressed // 32 bytes
scalarSize = fr.Bytes // 32 bytes
)
// marshalCompressed serializes a G1 affine point to 32-byte compressed form.
func marshalCompressed(p *bn254.G1Affine) []byte {
b := p.Bytes() // gnark-crypto compressed format, 32 bytes
return b[:]
}
// unmarshalCompressed deserializes a 32-byte compressed G1 point.
func unmarshalCompressed(data []byte) (bn254.G1Affine, error) {
var p bn254.G1Affine
_, err := p.SetBytes(data)
return p, err
}
// marshalScalar serializes a field element to 32 bytes (big-endian).
func marshalScalar(s *fr.Element) []byte {
b := s.Bytes() // 32 bytes, big-endian
return b[:]
}
// unmarshalScalar deserializes a 32-byte field element.
func unmarshalScalar(data []byte) (fr.Element, error) {
var s fr.Element
err := s.SetBytesCanonical(data)
return s, err
}
// Marshal serializes an IPProof to bytes.
//
// Layout: numRounds(4 bytes, uint32 big-endian) || L[0](32) || R[0](32) || ... || L[k](32) || R[k](32) || A(32) || B(32)
func (p *IPProof) Marshal() ([]byte, error) {
k := len(p.L)
if k != len(p.R) {
return nil, errors.New("ipproof marshal: L and R must have same length")
}
// Total size: 4 + k*32 + k*32 + 32 + 32 = 4 + 64k + 64
size := 4 + k*2*compressedG1Size + 2*scalarSize
buf := make([]byte, 0, size)
// numRounds
var numRounds [4]byte
binary.BigEndian.PutUint32(numRounds[:], uint32(k))
buf = append(buf, numRounds[:]...)
// L and R interleaved
for i := 0; i < k; i++ {
buf = append(buf, marshalCompressed(&p.L[i])...)
buf = append(buf, marshalCompressed(&p.R[i])...)
}
// A and B scalars
buf = append(buf, marshalScalar(&p.A)...)
buf = append(buf, marshalScalar(&p.B)...)
return buf, nil
}
// maxIPRounds is the maximum number of inner product rounds accepted during
// deserialization. log2(maxGeneratorN) = 20; we use a generous upper bound.
const maxIPRounds = 64
// Unmarshal deserializes an IPProof from bytes.
func (p *IPProof) Unmarshal(data []byte) error {
if len(data) < 4 {
return errors.New("ipproof unmarshal: data too short for header")
}
k := int(binary.BigEndian.Uint32(data[:4]))
if k > maxIPRounds {
return fmt.Errorf("ipproof unmarshal: numRounds %d exceeds maximum %d", k, maxIPRounds)
}
expectedSize := 4 + k*2*compressedG1Size + 2*scalarSize
if len(data) < expectedSize {
return fmt.Errorf("ipproof unmarshal: expected %d bytes, got %d", expectedSize, len(data))
}
offset := 4
p.L = make([]bn254.G1Affine, k)
p.R = make([]bn254.G1Affine, k)
for i := 0; i < k; i++ {
var err error
p.L[i], err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("ipproof unmarshal: L[%d]: %w", i, err)
}
offset += compressedG1Size
p.R[i], err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("ipproof unmarshal: R[%d]: %w", i, err)
}
offset += compressedG1Size
}
var err error
p.A, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("ipproof unmarshal: A: %w", err)
}
offset += scalarSize
p.B, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("ipproof unmarshal: B: %w", err)
}
return nil
}
// Marshal serializes a RangeProof to bytes.
//
// Layout: A(32) || S(32) || T1(32) || T2(32) || Taux(32) || Mu(32) || That(32) || IPProof
func (p *RangeProof) Marshal() ([]byte, error) {
ipBytes, err := p.IP.Marshal()
if err != nil {
return nil, fmt.Errorf("rangeproof marshal: %w", err)
}
// Fixed header: 7 * 32 = 224 bytes
size := 7*compressedG1Size + len(ipBytes)
buf := make([]byte, 0, size)
buf = append(buf, marshalCompressed(&p.A)...)
buf = append(buf, marshalCompressed(&p.S)...)
buf = append(buf, marshalCompressed(&p.T1)...)
buf = append(buf, marshalCompressed(&p.T2)...)
buf = append(buf, marshalScalar(&p.Taux)...)
buf = append(buf, marshalScalar(&p.Mu)...)
buf = append(buf, marshalScalar(&p.That)...)
buf = append(buf, ipBytes...)
return buf, nil
}
// Unmarshal deserializes a RangeProof from bytes.
func (p *RangeProof) Unmarshal(data []byte) error {
headerSize := 7 * compressedG1Size // 4 points + 3 scalars, all 32 bytes
if len(data) < headerSize+4 { // at least header + IP numRounds
return errors.New("rangeproof unmarshal: data too short")
}
offset := 0
var err error
p.A, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: A: %w", err)
}
offset += compressedG1Size
p.S, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: S: %w", err)
}
offset += compressedG1Size
p.T1, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: T1: %w", err)
}
offset += compressedG1Size
p.T2, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: T2: %w", err)
}
offset += compressedG1Size
p.Taux, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: Taux: %w", err)
}
offset += scalarSize
p.Mu, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: Mu: %w", err)
}
offset += scalarSize
p.That, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("rangeproof unmarshal: That: %w", err)
}
offset += scalarSize
return p.IP.Unmarshal(data[offset:])
}
// Marshal serializes an AggregateRangeProof to bytes.
// Same layout as RangeProof (same fields).
func (p *AggregateRangeProof) Marshal() ([]byte, error) {
ipBytes, err := p.IP.Marshal()
if err != nil {
return nil, fmt.Errorf("aggregate rangeproof marshal: %w", err)
}
size := 7*compressedG1Size + len(ipBytes)
buf := make([]byte, 0, size)
buf = append(buf, marshalCompressed(&p.A)...)
buf = append(buf, marshalCompressed(&p.S)...)
buf = append(buf, marshalCompressed(&p.T1)...)
buf = append(buf, marshalCompressed(&p.T2)...)
buf = append(buf, marshalScalar(&p.Taux)...)
buf = append(buf, marshalScalar(&p.Mu)...)
buf = append(buf, marshalScalar(&p.That)...)
buf = append(buf, ipBytes...)
return buf, nil
}
// Unmarshal deserializes an AggregateRangeProof from bytes.
func (p *AggregateRangeProof) Unmarshal(data []byte) error {
headerSize := 7 * compressedG1Size
if len(data) < headerSize+4 {
return errors.New("aggregate rangeproof unmarshal: data too short")
}
offset := 0
var err error
p.A, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: A: %w", err)
}
offset += compressedG1Size
p.S, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: S: %w", err)
}
offset += compressedG1Size
p.T1, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: T1: %w", err)
}
offset += compressedG1Size
p.T2, err = unmarshalCompressed(data[offset : offset+compressedG1Size])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: T2: %w", err)
}
offset += compressedG1Size
p.Taux, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: Taux: %w", err)
}
offset += scalarSize
p.Mu, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: Mu: %w", err)
}
offset += scalarSize
p.That, err = unmarshalScalar(data[offset : offset+scalarSize])
if err != nil {
return fmt.Errorf("aggregate rangeproof unmarshal: That: %w", err)
}
offset += scalarSize
return p.IP.Unmarshal(data[offset:])
}
// Marshal serializes a ThresholdProof to bytes (delegates to inner RangeProof).
func (p *ThresholdProof) Marshal() ([]byte, error) {
return p.Proof.Marshal()
}
// Unmarshal deserializes a ThresholdProof from bytes.
func (p *ThresholdProof) Unmarshal(data []byte) error {
return p.Proof.Unmarshal(data)
}