-
Notifications
You must be signed in to change notification settings - Fork 81
/
datasquare_test.go
168 lines (150 loc) · 4.59 KB
/
datasquare_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
package rsmt2d
import (
"fmt"
"reflect"
"testing"
)
func TestNewDataSquare(t *testing.T) {
result, err := newDataSquare([][]byte{{1, 2}}, NewDefaultTree)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(result.squareRow, [][][]byte{{{1, 2}}}) {
t.Errorf("newDataSquare failed for 1x1 square")
}
result, err = newDataSquare([][]byte{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, NewDefaultTree)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(result.squareRow, [][][]byte{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}) {
t.Errorf("newDataSquare failed for 2x2 square")
}
_, err = newDataSquare([][]byte{{1, 2}, {3, 4}, {5, 6}}, NewDefaultTree)
if err == nil {
t.Errorf("newDataSquare failed; inconsistent number of chunks accepted")
}
_, err = newDataSquare([][]byte{{1, 2}, {3, 4}, {5, 6}, {7}}, NewDefaultTree)
if err == nil {
t.Errorf("newDataSquare failed; chunks of unequal size accepted")
}
}
func TestExtendSquare(t *testing.T) {
ds, err := newDataSquare([][]byte{{1, 2}}, NewDefaultTree)
if err != nil {
panic(err)
}
err = ds.extendSquare(1, []byte{0})
if err == nil {
t.Errorf("extendSquare failed; error not returned when filler chunk size does not match data square chunk size")
}
ds, err = newDataSquare([][]byte{{1, 2}}, NewDefaultTree)
if err != nil {
panic(err)
}
err = ds.extendSquare(1, []byte{0, 0})
if err != nil {
panic(err)
}
if !reflect.DeepEqual(ds.squareRow, [][][]byte{{{1, 2}, {0, 0}}, {{0, 0}, {0, 0}}}) {
t.Errorf("extendSquare failed; unexpected result when extending 1x1 square to 2x2 square")
}
}
func TestRoots(t *testing.T) {
result, err := newDataSquare([][]byte{{1, 2}}, NewDefaultTree)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(result.RowRoots(), result.ColumnRoots()) {
t.Errorf("computing roots failed; expecting row and column roots for 1x1 square to be equal")
}
}
func TestLazyRootGeneration(t *testing.T) {
square, err := newDataSquare([][]byte{{1}, {2}, {3}, {4}}, NewDefaultTree)
if err != nil {
panic(err)
}
var rowRoots [][]byte
var colRoots [][]byte
for i := uint(0); i < square.width; i++ {
rowRoots = append(rowRoots, square.RowRoot(i))
colRoots = append(rowRoots, square.ColRoot(i))
}
square.computeRoots()
if !reflect.DeepEqual(square.rowRoots, rowRoots) && !reflect.DeepEqual(square.columnRoots, colRoots) {
t.Error("RowRoot or ColumnRoot did not produce identical roots to computeRoots")
}
}
func TestRootAPI(t *testing.T) {
square, err := newDataSquare([][]byte{{1}, {2}, {3}, {4}}, NewDefaultTree)
if err != nil {
panic(err)
}
for i := uint(0); i < square.width; i++ {
if !reflect.DeepEqual(square.RowRoots()[i], square.RowRoot(i)) {
t.Errorf(
"Row root API results in different roots, expected %v go %v",
square.RowRoots()[i],
square.RowRoot(i),
)
}
if !reflect.DeepEqual(square.ColumnRoots()[i], square.ColRoot(i)) {
t.Errorf(
"Column root API results in different roots, expected %v go %v",
square.ColumnRoots()[i],
square.ColRoot(i),
)
}
}
}
func TestProofs(t *testing.T) {
result, err := newDataSquare([][]byte{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, NewDefaultTree)
if err != nil {
panic(err)
}
_, proof, proofIndex, numLeaves, err := result.computeRowProof(1, 1)
if err != nil {
t.Errorf("Got unexpected error: %v", err)
}
if len(proof) != 2 {
t.Errorf("computing row proof for (1, 1) in 2x2 square failed; expecting proof set of length 2")
}
if proofIndex != 1 {
t.Errorf("computing row proof for (1, 1) in 2x2 square failed; expecting proof index of 1")
}
if numLeaves != 2 {
t.Errorf("computing row proof for (1, 1) in 2x2 square failed; expecting number of leaves to be 2")
}
result, err = newDataSquare([][]byte{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, NewDefaultTree)
if err != nil {
panic(err)
}
_, proof, proofIndex, numLeaves, err = result.computeColumnProof(1, 1)
if err != nil {
t.Errorf("Got unexpected error: %v", err)
}
if len(proof) != 2 {
t.Errorf("computing column proof for (1, 1) in 2x2 square failed; expecting proof set of length 2")
}
if proofIndex != 1 {
t.Errorf("computing column proof for (1, 1) in 2x2 square failed; expecting proof index of 1")
}
if numLeaves != 2 {
t.Errorf("computing column proof for (1, 1) in 2x2 square failed; expecting number of leaves to be 2")
}
}
func BenchmarkRoots(b *testing.B) {
for i := 32; i < 257; i *= 2 {
square, err := newDataSquare(genRandDS(i), NewDefaultTree)
if err != nil {
b.Errorf("Failure to create square of size %d: %s", i, err)
}
b.Run(
fmt.Sprintf("Square Size %dx%d", i, i),
func(b *testing.B) {
for n := 0; n < b.N; n++ {
square.computeRoots()
}
},
)
}
}