-
Notifications
You must be signed in to change notification settings - Fork 79
/
extendeddatasquare_test.go
218 lines (194 loc) · 5.03 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
package rsmt2d
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
const SHARD_SIZE = 64
var (
zeros = bytes.Repeat([]byte{0}, SHARD_SIZE)
ones = bytes.Repeat([]byte{1}, SHARD_SIZE)
twos = bytes.Repeat([]byte{2}, SHARD_SIZE)
threes = bytes.Repeat([]byte{3}, SHARD_SIZE)
fours = bytes.Repeat([]byte{4}, SHARD_SIZE)
fives = bytes.Repeat([]byte{5}, SHARD_SIZE)
eights = bytes.Repeat([]byte{8}, SHARD_SIZE)
elevens = bytes.Repeat([]byte{11}, SHARD_SIZE)
thirteens = bytes.Repeat([]byte{13}, SHARD_SIZE)
fifteens = bytes.Repeat([]byte{15}, SHARD_SIZE)
)
func TestComputeExtendedDataSquare(t *testing.T) {
codec := NewLeoRSCodec()
type testCase struct {
name string
data [][]byte
want [][][]byte
}
testCases := []testCase{
{
name: "1x1",
// NOTE: data must contain byte slices that are a multiple of 64
// bytes.
// See https://github.com/catid/leopard/blob/22ddc7804998d31c8f1a2617ee720e063b1fa6cd/README.md?plain=1#L27
// See https://github.com/klauspost/reedsolomon/blob/fd3e6910a7e457563469172968f456ad9b7696b6/README.md?plain=1#L403
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)
})
}
}
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 TestImmutableRoots(t *testing.T) {
codec := NewLeoRSCodec()
result, err := ComputeExtendedDataSquare([][]byte{
ones, twos,
threes, fours,
}, codec, NewDefaultTree)
if err != nil {
panic(err)
}
row := result.RowRoots()
row[0][0]++
if reflect.DeepEqual(row, result.RowRoots()) {
t.Errorf("Exported EDS RowRoots was mutable")
}
col := result.ColRoots()
col[0][0]++
if reflect.DeepEqual(col, result.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")
}
}
// 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)
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)
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) [][]byte {
var ds [][]byte
count := width * width
for i := 0; i < count; i++ {
share := make([]byte, 256)
rand.Read(share)
ds = append(ds, share)
}
return ds
}