forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasquare.go
348 lines (292 loc) · 8.65 KB
/
datasquare.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
344
345
346
347
348
package rsmt2d
import (
"errors"
"fmt"
"math"
"sync"
"golang.org/x/sync/errgroup"
)
// ErrUnevenChunks is thrown when non-nil chunks are not all of equal size.
var ErrUnevenChunks = errors.New("non-nil chunks not all of equal size")
// dataSquare stores all data for an original data square (ODS) or extended
// data square (EDS). Data is duplicated in both row-major and column-major
// order in order to be able to provide zero-allocation column slices.
type dataSquare struct {
squareRow [][][]byte // row-major
squareCol [][][]byte // col-major
dataMutex sync.Mutex
width uint
chunkSize uint
rowRoots [][]byte
colRoots [][]byte
createTreeFn TreeConstructorFn
}
// newDataSquare populates the data square from the supplied data and treeCreator.
// No root calculation is performed.
// data may have nil values.
func newDataSquare(data [][]byte, treeCreator TreeConstructorFn, chunkSize uint) (*dataSquare, error) {
width := int(math.Ceil(math.Sqrt(float64(len(data)))))
if width*width != len(data) {
return nil, errors.New("number of chunks must be a square number")
}
for _, d := range data {
if d != nil && len(d) != int(chunkSize) {
return nil, ErrUnevenChunks
}
}
squareRow := make([][][]byte, width)
for i := 0; i < width; i++ {
squareRow[i] = data[i*width : i*width+width]
for j := 0; j < width; j++ {
if squareRow[i][j] != nil && len(squareRow[i][j]) != int(chunkSize) {
return nil, ErrUnevenChunks
}
}
}
squareCol := make([][][]byte, width)
for j := 0; j < width; j++ {
squareCol[j] = make([][]byte, width)
for i := 0; i < width; i++ {
squareCol[j][i] = data[i*width+j]
}
}
return &dataSquare{
squareRow: squareRow,
squareCol: squareCol,
width: uint(width),
chunkSize: uint(chunkSize),
createTreeFn: treeCreator,
}, nil
}
// extendSquare extends the original data square by extendedWidth and fills
// the extended quadrants with fillerChunk.
func (ds *dataSquare) extendSquare(extendedWidth uint, fillerChunk []byte) error {
if uint(len(fillerChunk)) != ds.chunkSize {
return errors.New("filler chunk size does not match data square chunk size")
}
newWidth := ds.width + extendedWidth
newSquareRow := make([][][]byte, newWidth)
fillerExtendedRow := make([][]byte, extendedWidth)
for i := uint(0); i < extendedWidth; i++ {
fillerExtendedRow[i] = fillerChunk
}
fillerRow := make([][]byte, newWidth)
for i := uint(0); i < newWidth; i++ {
fillerRow[i] = fillerChunk
}
row := make([][]byte, ds.width)
for i := uint(0); i < ds.width; i++ {
copy(row, ds.squareRow[i])
newSquareRow[i] = append(row, fillerExtendedRow...)
}
for i := ds.width; i < newWidth; i++ {
newSquareRow[i] = make([][]byte, newWidth)
copy(newSquareRow[i], fillerRow)
}
ds.squareRow = newSquareRow
newSquareCol := make([][][]byte, newWidth)
for j := uint(0); j < newWidth; j++ {
newSquareCol[j] = make([][]byte, newWidth)
for i := uint(0); i < newWidth; i++ {
newSquareCol[j][i] = newSquareRow[i][j]
}
}
ds.squareCol = newSquareCol
ds.width = newWidth
ds.resetRoots()
return nil
}
func (ds *dataSquare) rowSlice(x uint, y uint, length uint) [][]byte {
return ds.squareRow[x][y : y+length]
}
// row returns a row slice.
// Do not modify this slice directly, instead use SetCell.
func (ds *dataSquare) row(x uint) [][]byte {
return ds.rowSlice(x, 0, ds.width)
}
func (ds *dataSquare) setRowSlice(x uint, y uint, newRow [][]byte) error {
for i := uint(0); i < uint(len(newRow)); i++ {
if len(newRow[i]) != int(ds.chunkSize) {
return errors.New("invalid chunk size")
}
}
if y+uint(len(newRow)) > ds.width {
return fmt.Errorf("cannot set row slice at (%d, %d) of length %d: because it would exceed the data square width %d", x, y, len(newRow), ds.width)
}
ds.dataMutex.Lock()
defer ds.dataMutex.Unlock()
for i := uint(0); i < uint(len(newRow)); i++ {
ds.squareRow[x][y+i] = newRow[i]
ds.squareCol[y+i][x] = newRow[i]
}
ds.resetRoots()
return nil
}
func (ds *dataSquare) colSlice(x uint, y uint, length uint) [][]byte {
return ds.squareCol[y][x : x+length]
}
// col returns a column slice.
// Do not modify this slice directly, instead use SetCell.
func (ds *dataSquare) col(y uint) [][]byte {
return ds.colSlice(0, y, ds.width)
}
func (ds *dataSquare) setColSlice(x uint, y uint, newCol [][]byte) error {
for i := uint(0); i < uint(len(newCol)); i++ {
if len(newCol[i]) != int(ds.chunkSize) {
return errors.New("invalid chunk size")
}
}
if x+uint(len(newCol)) > ds.width {
return fmt.Errorf("cannot set col slice at (%d, %d) of length %d: because it would exceed the data square width %d", x, y, len(newCol), ds.width)
}
ds.dataMutex.Lock()
defer ds.dataMutex.Unlock()
for i := uint(0); i < uint(len(newCol)); i++ {
ds.squareRow[x+i][y] = newCol[i]
ds.squareCol[y][x+i] = newCol[i]
}
ds.resetRoots()
return nil
}
func (ds *dataSquare) resetRoots() {
// don't write nil if it's already nil
// this prevents rewriting nil into shared memory slot
// when resetRoots is used from multiple routines
if ds.rowRoots != nil {
ds.rowRoots = nil
}
if ds.colRoots != nil {
ds.colRoots = nil
}
}
func (ds *dataSquare) computeRoots() error {
var g errgroup.Group
rowRoots := make([][]byte, ds.width)
colRoots := make([][]byte, ds.width)
for i := uint(0); i < ds.width; i++ {
i := i // https://go.dev/doc/faq#closures_and_goroutines
g.Go(func() error {
rowRoot, err := ds.getRowRoot(i)
if err != nil {
return err
}
rowRoots[i] = rowRoot
return nil
})
g.Go(func() error {
colRoot, err := ds.getColRoot(i)
if err != nil {
return err
}
colRoots[i] = colRoot
return nil
})
}
err := g.Wait()
if err != nil {
return err
}
ds.rowRoots = rowRoots
ds.colRoots = colRoots
return nil
}
// getRowRoots returns the Merkle roots of all the rows in the square.
func (ds *dataSquare) getRowRoots() ([][]byte, error) {
if ds.rowRoots == nil {
err := ds.computeRoots()
if err != nil {
return nil, err
}
}
return ds.rowRoots, nil
}
// getRowRoot calculates and returns the root of the selected row. Note: unlike
// the getRowRoots method, getRowRoot does not write to the built-in cache.
// Returns an error if the row is incomplete (i.e. some shares are nil).
func (ds *dataSquare) getRowRoot(x uint) ([]byte, error) {
if ds.rowRoots != nil {
return ds.rowRoots[x], nil
}
tree := ds.createTreeFn(Row, x)
row := ds.row(x)
if !isComplete(row) {
return nil, errors.New("can not compute root of incomplete row")
}
for _, d := range row {
err := tree.Push(d)
if err != nil {
return nil, err
}
}
return tree.Root()
}
// getColRoots returns the Merkle roots of all the columns in the square.
func (ds *dataSquare) getColRoots() ([][]byte, error) {
if ds.colRoots == nil {
err := ds.computeRoots()
if err != nil {
return nil, err
}
}
return ds.colRoots, nil
}
// getColRoot calculates and returns the root of the selected row. Note: unlike
// the getColRoots method, getColRoot does not write to the built-in cache.
// Returns an error if the column is incomplete (i.e. some shares are nil).
func (ds *dataSquare) getColRoot(y uint) ([]byte, error) {
if ds.colRoots != nil {
return ds.colRoots[y], nil
}
tree := ds.createTreeFn(Col, y)
col := ds.col(y)
if !isComplete(col) {
return nil, errors.New("can not compute root of incomplete column")
}
for _, d := range col {
err := tree.Push(d)
if err != nil {
return nil, err
}
}
return tree.Root()
}
// GetCell returns a copy of a specific cell.
func (ds *dataSquare) GetCell(x uint, y uint) []byte {
if ds.squareRow[x][y] == nil {
return nil
}
cell := make([]byte, ds.chunkSize)
copy(cell, ds.squareRow[x][y])
return cell
}
// SetCell sets a specific cell. The cell to set must be `nil`. Returns an error
// if the cell to set is not `nil` or newChunk is not the correct size.
func (ds *dataSquare) SetCell(x uint, y uint, newChunk []byte) error {
if ds.squareRow[x][y] != nil {
return fmt.Errorf("cannot set cell (%d, %d) as it already has a value %x", x, y, ds.squareRow[x][y])
}
if len(newChunk) != int(ds.chunkSize) {
return fmt.Errorf("cannot set cell with chunk size %d because dataSquare chunk size is %d", len(newChunk), ds.chunkSize)
}
ds.squareRow[x][y] = newChunk
ds.squareCol[y][x] = newChunk
ds.resetRoots()
return nil
}
// Flattened returns the concatenated rows of the data square.
func (ds *dataSquare) Flattened() [][]byte {
flattened := [][]byte(nil)
for _, data := range ds.squareRow {
flattened = append(flattened, data...)
}
return flattened
}
// isComplete returns true if all the shares are non-nil.
func isComplete(shares [][]byte) bool {
for _, share := range shares {
if share == nil {
return false
}
}
return true
}