forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extendeddatasquare.go
343 lines (297 loc) · 8.61 KB
/
extendeddatasquare.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Package rsmt2d implements the two dimensional Reed-Solomon merkle tree data availability scheme.
package rsmt2d
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"golang.org/x/sync/errgroup"
)
// ExtendedDataSquare represents an extended piece of data.
type ExtendedDataSquare struct {
*dataSquare
codec Codec
originalDataWidth uint
}
func (eds *ExtendedDataSquare) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
DataSquare [][]byte `json:"data_square"`
Codec string `json:"codec"`
}{
DataSquare: eds.dataSquare.Flattened(),
Codec: eds.codec.Name(),
})
}
func (eds *ExtendedDataSquare) UnmarshalJSON(b []byte) error {
var aux struct {
DataSquare [][]byte `json:"data_square"`
Codec string `json:"codec"`
}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
importedEds, err := ImportExtendedDataSquare(aux.DataSquare, codecs[aux.Codec], NewDefaultTree)
if err != nil {
return err
}
*eds = *importedEds
return nil
}
// ComputeExtendedDataSquare computes the extended data square for some chunks
// of original data.
func ComputeExtendedDataSquare(
data [][]byte,
codec Codec,
treeCreatorFn TreeConstructorFn,
) (*ExtendedDataSquare, error) {
if len(data) > codec.MaxChunks() {
return nil, errors.New("number of chunks exceeds the maximum")
}
chunkSize := getChunkSize(data)
err := codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err
}
ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
if err != nil {
return nil, err
}
eds := ExtendedDataSquare{dataSquare: ds, codec: codec}
err = eds.erasureExtendSquare(codec)
if err != nil {
return nil, err
}
return &eds, nil
}
// ImportExtendedDataSquare imports an extended data square, represented as flattened chunks of data.
func ImportExtendedDataSquare(
data [][]byte,
codec Codec,
treeCreatorFn TreeConstructorFn,
) (*ExtendedDataSquare, error) {
if len(data) > 4*codec.MaxChunks() {
return nil, errors.New("number of chunks exceeds the maximum")
}
chunkSize := getChunkSize(data)
err := codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err
}
ds, err := newDataSquare(data, treeCreatorFn, uint(chunkSize))
if err != nil {
return nil, err
}
eds := ExtendedDataSquare{dataSquare: ds, codec: codec}
err = validateEdsWidth(eds.width)
if err != nil {
return nil, err
}
eds.originalDataWidth = eds.width / 2
return &eds, nil
}
// NewExtendedDataSquare returns a new extended data square with a width of
// edsWidth. All shares are initialized to nil so that the returned extended
// data square can be populated via subsequent SetCell invocations.
func NewExtendedDataSquare(codec Codec, treeCreatorFn TreeConstructorFn, edsWidth uint, chunkSize uint) (*ExtendedDataSquare, error) {
err := validateEdsWidth(edsWidth)
if err != nil {
return nil, err
}
err = codec.ValidateChunkSize(int(chunkSize))
if err != nil {
return nil, err
}
data := make([][]byte, edsWidth*edsWidth)
dataSquare, err := newDataSquare(data, treeCreatorFn, chunkSize)
if err != nil {
return nil, err
}
originalDataWidth := edsWidth / 2
eds := ExtendedDataSquare{
dataSquare: dataSquare,
codec: codec,
originalDataWidth: originalDataWidth,
}
return &eds, nil
}
func (eds *ExtendedDataSquare) erasureExtendSquare(codec Codec) error {
eds.originalDataWidth = eds.width
// Extend original square with filler chunks. O represents original data. F
// represents filler chunks.
//
// ------- -------
// | | |
// | O | F |
// | | |
// ------- -------
// | | |
// | F | F |
// | | |
// ------- -------
if err := eds.extendSquare(eds.width, bytes.Repeat([]byte{0}, int(eds.chunkSize))); err != nil {
return err
}
errs, _ := errgroup.WithContext(context.Background())
// Populate filler chunks in Q1 and Q2. E represents erasure data.
//
// ------- -------
// | | |
// | O → | E |
// | ↓ | |
// ------- -------
// | | |
// | E | F |
// | | |
// ------- -------
for i := uint(0); i < eds.originalDataWidth; i++ {
i := i
// Encode Q0 and populate Q1 with erasure data
errs.Go(func() error {
return eds.erasureExtendRow(codec, i)
})
// Encode Q0 and populate Q2 with erasure data
errs.Go(func() error {
return eds.erasureExtendCol(codec, i)
})
}
if err := errs.Wait(); err != nil {
return err
}
// Populate filler chunks in Q3. Note that the parity data in `Q3` will be
// identical if it is vertically extended from `Q1` or horizontally extended
// from `Q2`.
//
// ------- -------
// | | |
// | O | E |
// | | |
// ------- -------
// | | |
// | E → | E |
// | | |
// ------- -------
for i := eds.originalDataWidth; i < eds.width; i++ {
i := i
// Encode Q2 and populate Q3 with erasure data
errs.Go(func() error {
return eds.erasureExtendRow(codec, i)
})
}
return errs.Wait()
}
func (eds *ExtendedDataSquare) erasureExtendRow(codec Codec, i uint) error {
parityShares, err := codec.Encode(eds.rowSlice(i, 0, eds.originalDataWidth))
if err != nil {
return err
}
return eds.setRowSlice(i, eds.originalDataWidth, parityShares)
}
func (eds *ExtendedDataSquare) erasureExtendCol(codec Codec, i uint) error {
parityShares, err := codec.Encode(eds.colSlice(0, i, eds.originalDataWidth))
if err != nil {
return err
}
return eds.setColSlice(eds.originalDataWidth, i, parityShares)
}
func (eds *ExtendedDataSquare) deepCopy(codec Codec) (ExtendedDataSquare, error) {
imported, err := ImportExtendedDataSquare(eds.Flattened(), codec, eds.createTreeFn)
return *imported, err
}
// Col returns a column slice.
// This slice is a copy of the internal column slice.
func (eds *ExtendedDataSquare) Col(y uint) [][]byte {
return deepCopy(eds.col(y))
}
// ColRoots returns the Merkle roots of all the columns in the square. Returns
// an error if the EDS is incomplete (i.e. some shares are nil).
func (eds *ExtendedDataSquare) ColRoots() ([][]byte, error) {
colRoots, err := eds.getColRoots()
if err != nil {
return nil, err
}
return deepCopy(colRoots), nil
}
// Row returns a row slice.
// This slice is a copy of the internal row slice.
func (eds *ExtendedDataSquare) Row(x uint) [][]byte {
return deepCopy(eds.row(x))
}
// RowRoots returns the Merkle roots of all the rows in the square. Returns an
// error if the EDS is incomplete (i.e. some shares are nil).
func (eds *ExtendedDataSquare) RowRoots() ([][]byte, error) {
rowRoots, err := eds.getRowRoots()
if err != nil {
return nil, err
}
return deepCopy(rowRoots), nil
}
func deepCopy(original [][]byte) [][]byte {
dest := make([][]byte, len(original))
for i, cell := range original {
dest[i] = make([]byte, len(cell))
copy(dest[i], cell)
}
return dest
}
// Width returns the width of the square.
func (eds *ExtendedDataSquare) Width() uint {
return eds.width
}
// Flattened returns the extended data square as a flattened slice of bytes.
func (eds *ExtendedDataSquare) Flattened() [][]byte {
return eds.dataSquare.Flattened()
}
// FlattenedODS returns the original data square as a flattened slice of bytes.
func (eds *ExtendedDataSquare) FlattenedODS() (flattened [][]byte) {
flattened = make([][]byte, eds.originalDataWidth*eds.originalDataWidth)
for i := uint(0); i < eds.originalDataWidth; i++ {
row := eds.Row(i)
for j := uint(0); j < eds.originalDataWidth; j++ {
flattened[(i*eds.originalDataWidth)+j] = row[j]
}
}
return flattened
}
// Equals returns true if other is equal to eds.
func (eds *ExtendedDataSquare) Equals(other *ExtendedDataSquare) bool {
if eds.originalDataWidth != other.originalDataWidth {
return false
}
if eds.codec.Name() != other.codec.Name() {
return false
}
if eds.chunkSize != other.chunkSize {
return false
}
if eds.width != other.width {
return false
}
for rowIndex := uint(0); rowIndex < eds.Width(); rowIndex++ {
edsRow := eds.Row(rowIndex)
otherRow := other.Row(rowIndex)
for colIndex := 0; colIndex < len(edsRow); colIndex++ {
if !bytes.Equal(edsRow[colIndex], otherRow[colIndex]) {
return false
}
}
}
return true
}
// validateEdsWidth returns an error if edsWidth is not a valid width for an
// extended data square.
func validateEdsWidth(edsWidth uint) error {
if edsWidth%2 != 0 {
return fmt.Errorf("extended data square width %v must be even", edsWidth)
}
return nil
}
// getChunkSize returns the size of the first non-nil chunk in data.
func getChunkSize(data [][]byte) (chunkSize int) {
for _, d := range data {
if d != nil {
return len(d)
}
}
return 0
}