forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasquare.go
242 lines (195 loc) · 5.77 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
package rsmt2d
import (
"errors"
"math"
)
// 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
width uint
chunkSize uint
rowRoots [][]byte
colRoots [][]byte
createTreeFn TreeConstructorFn
}
func newDataSquare(data [][]byte, treeCreator TreeConstructorFn) (*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")
}
chunkSize := len(data[0])
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 len(squareRow[i][j]) != chunkSize {
return nil, errors.New("all chunks must be of equal size")
}
}
}
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
}
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")
}
}
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")
}
}
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() {
ds.rowRoots = nil
ds.colRoots = nil
}
func (ds *dataSquare) computeRoots() {
rowRoots := make([][]byte, ds.width)
colRoots := make([][]byte, ds.width)
for i := uint(0); i < ds.width; i++ {
rowRoots[i] = ds.getRowRoot(i)
colRoots[i] = ds.getColRoot(i)
}
ds.rowRoots = rowRoots
ds.colRoots = colRoots
}
// getRowRoots returns the Merkle roots of all the rows in the square.
func (ds *dataSquare) getRowRoots() [][]byte {
if ds.rowRoots == nil {
ds.computeRoots()
}
return ds.rowRoots
}
// getRowRoot calculates and returns the root of the selected row. Note: unlike the
// getRowRoots method, getRowRoot uses the built-in cache when available.
func (ds *dataSquare) getRowRoot(x uint) []byte {
if ds.rowRoots != nil {
return ds.rowRoots[x]
}
tree := ds.createTreeFn()
for i, d := range ds.row(x) {
tree.Push(d, SquareIndex{Cell: uint(i), Axis: x})
}
return tree.Root()
}
// getColRoots returns the Merkle roots of all the columns in the square.
func (ds *dataSquare) getColRoots() [][]byte {
if ds.colRoots == nil {
ds.computeRoots()
}
return ds.colRoots
}
// getColRoot calculates and returns the root of the selected row. Note: unlike the
// getColRoots method, getColRoot uses the built-in cache when available.
func (ds *dataSquare) getColRoot(y uint) []byte {
if ds.colRoots != nil {
return ds.colRoots[y]
}
tree := ds.createTreeFn()
for i, d := range ds.col(y) {
tree.Push(d, SquareIndex{Axis: y, Cell: uint(i)})
}
return tree.Root()
}
// getCell returns a single chunk at a specific cell.
func (ds *dataSquare) getCell(x uint, y uint) []byte {
cell := make([]byte, ds.chunkSize)
copy(cell, ds.squareRow[x][y])
return cell
}
func (ds *dataSquare) setCell(x uint, y uint, newChunk []byte) {
ds.squareRow[x][y] = newChunk
ds.squareCol[y][x] = newChunk
ds.resetRoots()
}
func (ds *dataSquare) flattened() [][]byte {
flattened := [][]byte(nil)
for _, data := range ds.squareRow {
flattened = append(flattened, data...)
}
return flattened
}