forked from celestiaorg/rsmt2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extendeddatasquare_test.go
442 lines (382 loc) · 11.1 KB
/
extendeddatasquare_test.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package rsmt2d
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
zeros = bytes.Repeat([]byte{0}, shareSize)
ones = bytes.Repeat([]byte{1}, shareSize)
twos = bytes.Repeat([]byte{2}, shareSize)
threes = bytes.Repeat([]byte{3}, shareSize)
fours = bytes.Repeat([]byte{4}, shareSize)
fives = bytes.Repeat([]byte{5}, shareSize)
eights = bytes.Repeat([]byte{8}, shareSize)
elevens = bytes.Repeat([]byte{11}, shareSize)
thirteens = bytes.Repeat([]byte{13}, shareSize)
fifteens = bytes.Repeat([]byte{15}, shareSize)
)
func TestComputeExtendedDataSquare(t *testing.T) {
codec := NewLeoRSCodec()
type testCase struct {
name string
data [][]byte
want [][][]byte
}
testCases := []testCase{
{
name: "1x1",
data: [][]byte{ones},
want: [][][]byte{
{ones, ones},
{ones, ones},
},
},
{
name: "2x2",
data: [][]byte{
ones, twos,
threes, fours,
},
want: [][][]byte{
{ones, twos, zeros, threes},
{threes, fours, eights, fifteens},
{twos, elevens, thirteens, fours},
{zeros, thirteens, fives, eights},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := ComputeExtendedDataSquare(tc.data, codec, NewDefaultTree)
assert.NoError(t, err)
assert.Equal(t, tc.want, result.squareRow)
})
}
t.Run("returns an error if chunkSize is not a multiple of 64", func(t *testing.T) {
chunk := bytes.Repeat([]byte{1}, 65)
_, err := ComputeExtendedDataSquare([][]byte{chunk}, NewLeoRSCodec(), NewDefaultTree)
assert.Error(t, err)
})
}
func TestImportExtendedDataSquare(t *testing.T) {
t.Run("is able to import an EDS", func(t *testing.T) {
eds := createExampleEds(t, shareSize)
got, err := ImportExtendedDataSquare(eds.Flattened(), NewLeoRSCodec(), NewDefaultTree)
assert.NoError(t, err)
assert.Equal(t, eds.Flattened(), got.Flattened())
})
t.Run("returns an error if chunkSize is not a multiple of 64", func(t *testing.T) {
chunk := bytes.Repeat([]byte{1}, 65)
_, err := ImportExtendedDataSquare([][]byte{chunk}, NewLeoRSCodec(), NewDefaultTree)
assert.Error(t, err)
})
}
func TestMarshalJSON(t *testing.T) {
codec := NewLeoRSCodec()
result, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
if err != nil {
panic(err)
}
edsBytes, err := json.Marshal(result)
if err != nil {
t.Errorf("failed to marshal EDS: %v", err)
}
var eds ExtendedDataSquare
err = json.Unmarshal(edsBytes, &eds)
if err != nil {
t.Errorf("failed to marshal EDS: %v", err)
}
if !reflect.DeepEqual(result.squareRow, eds.squareRow) {
t.Errorf("eds not equal after json marshal/unmarshal")
}
}
func TestNewExtendedDataSquare(t *testing.T) {
t.Run("returns an error if edsWidth is not even", func(t *testing.T) {
edsWidth := uint(1)
_, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.Error(t, err)
})
t.Run("returns an error if chunkSize is not a multiple of 64", func(t *testing.T) {
edsWidth := uint(1)
chunkSize := uint(65)
_, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, chunkSize)
assert.Error(t, err)
})
t.Run("returns a 4x4 EDS", func(t *testing.T) {
edsWidth := uint(4)
got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.NoError(t, err)
assert.Equal(t, edsWidth, got.width)
assert.Equal(t, uint(shareSize), got.chunkSize)
})
t.Run("returns a 4x4 EDS that can be populated via SetCell", func(t *testing.T) {
edsWidth := uint(4)
got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.NoError(t, err)
chunk := bytes.Repeat([]byte{1}, int(shareSize))
err = got.SetCell(0, 0, chunk)
assert.NoError(t, err)
assert.Equal(t, chunk, got.squareRow[0][0])
})
t.Run("returns an error when SetCell is invoked on an EDS with a chunk that is not the correct size", func(t *testing.T) {
edsWidth := uint(4)
incorrectChunkSize := shareSize + 1
got, err := NewExtendedDataSquare(NewLeoRSCodec(), NewDefaultTree, edsWidth, shareSize)
assert.NoError(t, err)
chunk := bytes.Repeat([]byte{1}, int(incorrectChunkSize))
err = got.SetCell(0, 0, chunk)
assert.Error(t, err)
})
}
func TestImmutableRoots(t *testing.T) {
codec := NewLeoRSCodec()
result, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
if err != nil {
panic(err)
}
mutatedRowRoots, err := result.RowRoots()
assert.NoError(t, err)
mutatedRowRoots[0][0]++ // mutate
rowRoots, err := result.RowRoots()
assert.NoError(t, err)
if reflect.DeepEqual(mutatedRowRoots, rowRoots) {
t.Errorf("Exported EDS RowRoots was mutable")
}
mutatedColRoots, err := result.ColRoots()
assert.NoError(t, err)
mutatedColRoots[0][0]++ // mutate
colRoots, err := result.ColRoots()
assert.NoError(t, err)
if reflect.DeepEqual(mutatedColRoots, colRoots) {
t.Errorf("Exported EDS ColRoots was mutable")
}
}
func TestEDSRowColImmutable(t *testing.T) {
codec := NewLeoRSCodec()
result, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
if err != nil {
panic(err)
}
row := result.Row(0)
row[0][0]++
if reflect.DeepEqual(row, result.Row(0)) {
t.Errorf("Exported EDS Row was mutable")
}
col := result.Col(0)
col[0][0]++
if reflect.DeepEqual(col, result.Col(0)) {
t.Errorf("Exported EDS Col was mutable")
}
}
func TestRowRoots(t *testing.T) {
t.Run("returns row roots for a 4x4 EDS", func(t *testing.T) {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)
rowRoots, err := eds.RowRoots()
assert.NoError(t, err)
assert.Len(t, rowRoots, 4)
})
t.Run("returns an error for an incomplete EDS", func(t *testing.T) {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)
// set a cell to nil to make the EDS incomplete
eds.setCell(0, 0, nil)
_, err = eds.RowRoots()
assert.Error(t, err)
})
}
func TestColRoots(t *testing.T) {
t.Run("returns col roots for a 4x4 EDS", func(t *testing.T) {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)
colRoots, err := eds.ColRoots()
assert.NoError(t, err)
assert.Len(t, colRoots, 4)
})
t.Run("returns an error for an incomplete EDS", func(t *testing.T) {
eds, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)
// set a cell to nil to make the EDS incomplete
eds.setCell(0, 0, nil)
_, err = eds.ColRoots()
assert.Error(t, err)
})
}
// dump acts as a data dump for the benchmarks to stop the compiler from making
// unrealistic optimizations
var dump *ExtendedDataSquare
// BenchmarkExtension benchmarks extending datasquares sizes 4-128 using all
// supported codecs (encoding only)
func BenchmarkExtensionEncoding(b *testing.B) {
for i := 4; i < 513; i *= 2 {
for codecName, codec := range codecs {
if codec.MaxChunks() < i*i {
// Only test codecs that support this many chunks
continue
}
square := genRandDS(i, shareSize)
b.Run(
fmt.Sprintf("%s %dx%dx%d ODS", codecName, i, i, len(square[0])),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
eds, err := ComputeExtendedDataSquare(square, codec, NewDefaultTree)
if err != nil {
b.Error(err)
}
dump = eds
}
},
)
}
}
}
// BenchmarkExtension benchmarks extending datasquares sizes 4-128 using all
// supported codecs (both encoding and root computation)
func BenchmarkExtensionWithRoots(b *testing.B) {
for i := 4; i < 513; i *= 2 {
for codecName, codec := range codecs {
if codec.MaxChunks() < i*i {
// Only test codecs that support this many chunks
continue
}
square := genRandDS(i, shareSize)
b.Run(
fmt.Sprintf("%s %dx%dx%d ODS", codecName, i, i, len(square[0])),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
eds, err := ComputeExtendedDataSquare(square, codec, NewDefaultTree)
if err != nil {
b.Error(err)
}
_, _ = eds.RowRoots()
_, _ = eds.ColRoots()
dump = eds
}
},
)
}
}
}
// genRandDS make a datasquare of random data, with width describing the number
// of shares on a single side of the ds
func genRandDS(width int, chunkSize int) [][]byte {
var ds [][]byte
count := width * width
for i := 0; i < count; i++ {
share := make([]byte, chunkSize)
_, err := rand.Read(share)
if err != nil {
panic(err)
}
ds = append(ds, share)
}
return ds
}
// TestFlattened_EDS tests that eds.Flattened() returns all the shares in the
// EDS. This function has the `_EDS` suffix to avoid a name collision with the
// TestFlattened.
func TestFlattened_EDS(t *testing.T) {
example := createExampleEds(t, shareSize)
want := [][]byte{
ones, twos, zeros, threes,
threes, fours, eights, fifteens,
twos, elevens, thirteens, fours,
zeros, thirteens, fives, eights,
}
got := example.Flattened()
assert.Equal(t, want, got)
}
func TestFlattenedODS(t *testing.T) {
example := createExampleEds(t, shareSize)
want := [][]byte{
ones, twos,
threes, fours,
}
got := example.FlattenedODS()
assert.Equal(t, want, got)
}
func TestEquals(t *testing.T) {
t.Run("returns true for two equal EDS", func(t *testing.T) {
a := createExampleEds(t, shareSize)
b := createExampleEds(t, shareSize)
assert.True(t, a.Equals(b))
})
t.Run("returns false for two unequal EDS", func(t *testing.T) {
a := createExampleEds(t, shareSize)
type testCase struct {
name string
other *ExtendedDataSquare
}
unequalOriginalDataWidth := createExampleEds(t, shareSize)
unequalOriginalDataWidth.originalDataWidth = 1
unequalCodecs := createExampleEds(t, shareSize)
unequalCodecs.codec = newTestCodec()
unequalChunkSize := createExampleEds(t, shareSize*2)
unequalEds, err := ComputeExtendedDataSquare([][]byte{ones}, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)
testCases := []testCase{
{
name: "unequal original data width",
other: unequalOriginalDataWidth,
},
{
name: "unequal codecs",
other: unequalCodecs,
},
{
name: "unequal chunkSize",
other: unequalChunkSize,
},
{
name: "unequalEds",
other: unequalEds,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.False(t, a.Equals(tc.other))
assert.False(t, reflect.DeepEqual(a, tc.other))
})
}
})
}
func createExampleEds(t *testing.T, chunkSize int) (eds *ExtendedDataSquare) {
ones := bytes.Repeat([]byte{1}, chunkSize)
twos := bytes.Repeat([]byte{2}, chunkSize)
threes := bytes.Repeat([]byte{3}, chunkSize)
fours := bytes.Repeat([]byte{4}, chunkSize)
ods := [][]byte{
ones, twos,
threes, fours,
}
eds, err := ComputeExtendedDataSquare(ods, NewLeoRSCodec(), NewDefaultTree)
require.NoError(t, err)
return eds
}