forked from templexxx/reedsolomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_test.go
342 lines (315 loc) · 6.94 KB
/
matrix_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
// Copyright (c) 2017 Temple3x (temple3x@gmail.com)
//
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
package reedsolomon
import (
"bytes"
"fmt"
"math/bits"
"math/rand"
"testing"
"time"
)
func TestMakeEncodeMatrix(t *testing.T) {
act := makeEncodeMatrix(4, 4)
exp := []byte{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
71, 167, 122, 186,
167, 71, 186, 122,
122, 186, 71, 167,
186, 122, 167, 71}
if !bytes.Equal(act, exp) {
t.Fatal("mismatch")
}
}
func TestMatrixSwap(t *testing.T) {
n := 7
m := make([]byte, n*n)
rand.Seed(time.Now().UnixNano())
fillRandom(m)
exp := make([]byte, n*n)
copy(exp, m)
matrix(exp).swap(0, 1, n)
matrix(exp).swap(0, 1, n)
if !bytes.Equal(exp, m) {
t.Fatalf("swap mismatch")
}
}
func TestMatrixInvert(t *testing.T) {
testCases := []struct {
matrixData []byte
n int
expect []byte
ok bool
expectedErr error
}{
{
[]byte{
56, 23, 98,
3, 100, 200,
45, 201, 123},
3,
[]byte{
175, 133, 33,
130, 13, 245,
112, 35, 126},
true,
nil,
},
{
[]byte{
0, 23, 98,
3, 100, 200,
45, 201, 123},
3,
[]byte{
245, 128, 152,
188, 64, 135,
231, 81, 239},
true,
nil,
},
{
[]byte{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1,
7, 7, 6, 6, 1},
5,
[]byte{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
123, 123, 1, 122, 122,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0},
true,
nil,
},
{
[]byte{
4, 2,
12, 6},
2,
nil,
false,
ErrSingularMatrix,
},
{
[]byte{7, 8, 9},
2,
nil,
false,
ErrNotSquare,
},
}
for i, c := range testCases {
m := matrix(c.matrixData)
actual, actualErr := m.invert(c.n)
if actualErr != nil && c.ok {
t.Errorf("case.%d, expected to pass, but failed with: <ERROR> %s", i+1, actualErr.Error())
}
if actualErr == nil && !c.ok {
t.Errorf("case.%d, expected to fail with <ERROR> \"%s\", but passed", i+1, c.expectedErr)
}
if actualErr != nil && !c.ok {
if c.expectedErr != actualErr {
t.Errorf("case.%d, expected to fail with error \"%s\", but instead failed with error \"%s\"", i+1, c.expectedErr, actualErr)
}
}
if actualErr == nil && c.ok {
if !bytes.Equal(c.expect, actual) {
t.Errorf("case.%d, mismatch", i+1)
}
}
}
}
func TestMakeEncMatrixForReconst(t *testing.T) {
d, p := 4, 4
em := makeEncodeMatrix(d, p)
dpHas := makeHasRandom(d+p, p)
emr, err := em.makeEncMatrixForReconst(dpHas)
if err != nil {
t.Fatal(err)
}
hasM := make([]byte, d*d)
for i, h := range dpHas {
copy(hasM[i*d:i*d+d], em[h*d:h*d+d])
}
if !mul(emr, hasM, d).isIdentity(d) {
t.Fatal("make wrong encoding matrix for reconstruction")
}
}
// Check all sub matrices when there is a lost.
// Warn:
// Don't set too big numbers,
// it may have too many combinations, the test will never finish.
func TestEncMatrixInvertibleAll(t *testing.T) {
testEncMatrixInvertible(t, 10, 4)
testEncMatrixInvertible(t, 15, 4)
}
func testEncMatrixInvertible(t *testing.T, d, p int) {
encMatrix := makeEncodeMatrix(d, p)
var bitmap uint64
cnt := 0
// Lost more, bitmap bigger.
var min uint64 = (1 << (d + 1)) - 1 ^ (1 << (d - 1)) // Min value when lost one data row vector.
var max uint64 = ((1 << d) - 1) << p // Max value when lost when lost parity-num data row vectors.
for bitmap = min; bitmap <= max; bitmap++ {
if bits.OnesCount64(bitmap) != d {
continue
}
cnt++
v := bitmap
dpHas := make([]int, d)
c := 0
for i := 0; i < d+p; i++ {
var j uint64 = 1 << i
if j&v == j {
dpHas[c] = i
c++
}
}
m := make([]byte, d*d)
for i := 0; i < d; i++ {
copy(m[i*d:i*d+d], encMatrix[dpHas[i]*d:dpHas[i]*d+d])
}
im, err := matrix(m).invert(d)
if err != nil {
t.Fatalf("encode matrix is singular, d:%d, p:%d, dpHas:%#v", d, p, dpHas)
}
// Check A * A' = I or not,
// ensure nothing wrong in the invert process.
if !mul(im, m, d).isIdentity(d) {
t.Fatalf("matrix invert wrong, d:%d, p:%d, dpHas:%#v", d, p, dpHas)
}
}
t.Logf("%d+%d pass invertible test, total submatrix(with lost): %d", d, p, cnt)
}
// Check Encoding Matrices' submatrices are invertible.
// Randomly pick up submatrix every data+parity pair.
//
// This test may cost about 100s, unless modify codes about
// galois field or matrix, there is no need to run it every time,
// so skip the test by default, avoiding waste time in develop process.
func TestEncMatrixInvertibleRandom(t *testing.T) {
if testing.Short() {
t.Skip("skip the test, because it may cost too much time")
}
for d := 1; d < 256; d++ {
for p := 1; p < 256; p++ {
if d+p > 256 {
continue
}
encMatrix := makeEncodeMatrix(d, p)
h := makeHasRandom(d+p, p)
m := make([]byte, d*d)
for i := 0; i < d; i++ {
copy(m[i*d:i*d+d], encMatrix[h[i]*d:h[i]*d+d])
}
im, err := matrix(m).invert(d)
if err != nil {
t.Fatalf("encode matrix is singular, d:%d, p:%d, dpHas:%#v", d, p, h)
}
// Check A * A' = I or not,
// ensure nothing wrong in the invert process.
if !mul(im, m, d).isIdentity(d) {
t.Fatalf("matrix invert wrong, d:%d, p:%d, dpHas:%#v", d, p, h)
}
}
}
}
// square matrix a * square matrix b = out
func mul(a, b matrix, n int) (out matrix) {
out = make([]byte, n*n)
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
d := byte(0)
for k := 0; k < n; k++ {
d ^= gfmul(a[n*i+k], b[n*k+j])
}
out[i*n+j] = d
}
}
return
}
func (m matrix) isIdentity(n int) bool {
im := make([]byte, n*n)
for i := 0; i < n; i++ {
im[i*n+i] = 1
}
return bytes.Equal(m, im)
}
func makeHasRandom(n, lostN int) []int {
l := makeLostRandom(n, lostN)
s := make([]int, n-lostN)
c := 0
for i := 0; i < n; i++ {
if !isIn(i, l) {
s[c] = i
c++
}
}
return s
}
func makeLostRandom(n, lostN int) []int {
l := make([]int, lostN)
rand.Seed(time.Now().UnixNano())
c := 0
for {
if c == lostN {
break
}
v := rand.Intn(n)
if !isIn(v, l) {
l[c] = v
c++
}
}
return l
}
func BenchmarkMatrixInvert(b *testing.B) {
ns := []int{5, 10, 15}
b.Run("", benchMatrixInvertRun(benchInvert, ns))
}
func benchMatrixInvertRun(f func(*testing.B, int), ns []int) func(*testing.B) {
return func(b *testing.B) {
for _, n := range ns {
b.Run(fmt.Sprintf("(%dx%d)", n, n), func(b *testing.B) {
f(b, n)
})
}
}
}
func benchInvert(b *testing.B, n int) {
m := makeCauchyMatrix(n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := m.invert(n)
if err != nil {
b.Fatal(err)
}
}
}
// Cauchy Matrix must be invertible,
// and it's complex enough for test invert performance.
//
// In Reed-Solomon Codes reconstruction process,
// because the major part of the matrix is from
// the identity matrix, the speed will be faster than
// this benchmark test.
func makeCauchyMatrix(n int) matrix {
m := make([]byte, n*n)
off := 0
for i := n; i < n*2; i++ {
for j := 0; j < n; j++ {
m[off] = inverseTbl[i^j]
off++
}
}
return m
}