Debian Codegraph @

galois_test.go

raw
1 /**
2 * Unit tests for Galois
3 *
4 * Copyright 2015, Klaus Post
5 * Copyright 2015, Backblaze, Inc.
6 */
7
8 package reedsolomon
9
10 import (
11 "bytes"
12 "crypto/rand"
13 "encoding/hex"
14 "fmt"
15 mathrand "math/rand"
16 "testing"
17 )
18
19 func TestAssociativity(t *testing.T) {
20 for i := range 256 {
21 a := byte(i)
22 for j := range 256 {
23 b := byte(j)
24 for k := range 256 {
25 c := byte(k)
26 x := galAdd(a, galAdd(b, c))
27 y := galAdd(galAdd(a, b), c)
28 if x != y {
29 t.Fatal("add does not match:", x, "!=", y)
30 }
31 x = galMultiply(a, galMultiply(b, c))
32 y = galMultiply(galMultiply(a, b), c)
33 if x != y {
34 t.Fatal("multiply does not match:", x, "!=", y)
35 }
36 }
37 }
38 }
39 }
40
41 func TestIdentity(t *testing.T) {
42 for i := range 256 {
43 a := byte(i)
44 b := galAdd(a, 0)
45 if a != b {
46 t.Fatal("Add zero should yield same result", a, "!=", b)
47 }
48 b = galMultiply(a, 1)
49 if a != b {
50 t.Fatal("Mul by one should yield same result", a, "!=", b)
51 }
52 }
53 }
54
55 func TestInverse(t *testing.T) {
56 for i := range 256 {
57 a := byte(i)
58 b := galAdd(0, a)
59 c := galAdd(a, b)
60 if c != 0 {
61 t.Fatal("inverse sub/add", c, "!=", 0)
62 }
63 if a != 0 {
64 b = galDivide(1, a)
65 c = galMultiply(a, b)
66 if c != 1 {
67 t.Fatal("inverse div/mul", c, "!=", 1)
68 }
69 b2 := galOneOver(a)
70 if b != b2 {
71 t.Fatal("inverse div/mul", b, "!=", b2)
72 }
73 }
74 }
75 }
76
77 func TestCommutativity(t *testing.T) {
78 for i := range 256 {
79 a := byte(i)
80 for j := range 256 {
81 b := byte(j)
82 x := galAdd(a, b)
83 y := galAdd(b, a)
84 if x != y {
85 t.Fatal(x, "!= ", y)
86 }
87 x = galMultiply(a, b)
88 y = galMultiply(b, a)
89 if x != y {
90 t.Fatal(x, "!= ", y)
91 }
92 }
93 }
94 }
95
96 func TestDistributivity(t *testing.T) {
97 for i := range 256 {
98 a := byte(i)
99 for j := range 256 {
100 b := byte(j)
101 for k := range 256 {
102 c := byte(k)
103 x := galMultiply(a, galAdd(b, c))
104 y := galAdd(galMultiply(a, b), galMultiply(a, c))
105 if x != y {
106 t.Fatal(x, "!= ", y)
107 }
108 }
109 }
110 }
111 }
112
113 func TestExp(t *testing.T) {
114 for i := range 256 {
115 a := byte(i)
116 power := byte(1)
117 for j := range 256 {
118 x := galExp(a, j)
119 if x != power {
120 t.Fatal(x, "!=", power)
121 }
122 power = galMultiply(power, a)
123 }
124 }
125 }
126
127 func testGalois(t *testing.T, o *options) {
128 // These values were copied output of the Python code.
129 if galMultiply(3, 4) != 12 {
130 t.Fatal("galMultiply(3, 4) != 12")
131 }
132 if galMultiply(7, 7) != 21 {
133 t.Fatal("galMultiply(7, 7) != 21")
134 }
135 if galMultiply(23, 45) != 41 {
136 t.Fatal("galMultiply(23, 45) != 41")
137 }
138
139 // Test slices (>32 entries to test assembler -- AVX2 & NEON)
140 in := []byte{0, 1, 2, 3, 4, 5, 6, 10, 50, 100, 150, 174, 201, 255, 99, 32, 67, 85, 200, 199, 198, 197, 196, 195, 194, 193, 192, 191, 190, 189, 188, 187, 186, 185}
141 out := make([]byte, len(in))
142 galMulSlice(25, in, out, o)
143 expect := []byte{0x0, 0x19, 0x32, 0x2b, 0x64, 0x7d, 0x56, 0xfa, 0xb8, 0x6d, 0xc7, 0x85, 0xc3, 0x1f, 0x22, 0x7, 0x25, 0xfe, 0xda, 0x5d, 0x44, 0x6f, 0x76, 0x39, 0x20, 0xb, 0x12, 0x11, 0x8, 0x23, 0x3a, 0x75, 0x6c, 0x47}
144 if 0 != bytes.Compare(out, expect) {
145 t.Errorf("got %#v, expected %#v", out, expect)
146 }
147 expectXor := []byte{0x0, 0x2d, 0x5a, 0x77, 0xb4, 0x99, 0xee, 0x2f, 0x79, 0xf2, 0x7, 0x51, 0xd4, 0x19, 0x31, 0xc9, 0xf8, 0xfc, 0xf9, 0x4f, 0x62, 0x15, 0x38, 0xfb, 0xd6, 0xa1, 0x8c, 0x96, 0xbb, 0xcc, 0xe1, 0x22, 0xf, 0x78}
148 galMulSliceXor(52, in, out, o)
149 if 0 != bytes.Compare(out, expectXor) {
150 t.Errorf("got %#v, expected %#v", out, expectXor)
151 }
152
153 galMulSlice(177, in, out, o)
154 expect = []byte{0x0, 0xb1, 0x7f, 0xce, 0xfe, 0x4f, 0x81, 0x9e, 0x3, 0x6, 0xe8, 0x75, 0xbd, 0x40, 0x36, 0xa3, 0x95, 0xcb, 0xc, 0xdd, 0x6c, 0xa2, 0x13, 0x23, 0x92, 0x5c, 0xed, 0x1b, 0xaa, 0x64, 0xd5, 0xe5, 0x54, 0x9a}
155 if 0 != bytes.Compare(out, expect) {
156 t.Errorf("got %#v, expected %#v", out, expect)
157 }
158
159 expectXor = []byte{0x0, 0xc4, 0x95, 0x51, 0x37, 0xf3, 0xa2, 0xfb, 0xec, 0xc5, 0xd0, 0xc7, 0x53, 0x88, 0xa3, 0xa5, 0x6, 0x78, 0x97, 0x9f, 0x5b, 0xa, 0xce, 0xa8, 0x6c, 0x3d, 0xf9, 0xdf, 0x1b, 0x4a, 0x8e, 0xe8, 0x2c, 0x7d}
160 galMulSliceXor(117, in, out, o)
161 if 0 != bytes.Compare(out, expectXor) {
162 t.Errorf("got %#v, expected %#v", out, expectXor)
163 }
164
165 if galExp(2, 2) != 4 {
166 t.Fatal("galExp(2, 2) != 4")
167 }
168 if galExp(5, 20) != 235 {
169 t.Fatal("galExp(5, 20) != 235")
170 }
171 if galExp(13, 7) != 43 {
172 t.Fatal("galExp(13, 7) != 43")
173 }
174 }
175
176 func TestGalois(t *testing.T) {
177 // invoke with all combinations of asm instructions
178 o := options{}
179 o.useSSSE3, o.useAVX2 = false, false
180 testGalois(t, &o)
181 o.useSSSE3, o.useAVX2 = true, false
182 testGalois(t, &o)
183 if defaultOptions.useAVX2 {
184 o.useSSSE3, o.useAVX2 = false, true
185 testGalois(t, &o)
186 }
187 }
188
189 func TestSliceGalAdd(t *testing.T) {
190
191 lengthList := []int{16, 32, 34}
192 for _, length := range lengthList {
193 in := make([]byte, length)
194 fillRandom(in)
195 out := make([]byte, length)
196 fillRandom(out)
197 expect := make([]byte, length)
198 for i := range expect {
199 expect[i] = in[i] ^ out[i]
200 }
201 noSSE2 := defaultOptions
202 noSSE2.useSSE2 = false
203 sliceXor(in, out, &noSSE2)
204 if 0 != bytes.Compare(out, expect) {
205 t.Errorf("got %#v, expected %#v", out, expect)
206 }
207 fillRandom(out)
208 for i := range expect {
209 expect[i] = in[i] ^ out[i]
210 }
211 sliceXor(in, out, &defaultOptions)
212 if 0 != bytes.Compare(out, expect) {
213 t.Errorf("got %#v, expected %#v", out, expect)
214 }
215 }
216
217 for i := range 256 {
218 a := byte(i)
219 for j := range 256 {
220 b := byte(j)
221 for k := range 256 {
222 c := byte(k)
223 x := galAdd(a, galAdd(b, c))
224 y := galAdd(galAdd(a, b), c)
225 if x != y {
226 t.Fatal("add does not match:", x, "!=", y)
227 }
228 x = galMultiply(a, galMultiply(b, c))
229 y = galMultiply(galMultiply(a, b), c)
230 if x != y {
231 t.Fatal("multiply does not match:", x, "!=", y)
232 }
233 }
234 }
235 }
236 }
237
238 func testGenGalois(t *testing.T, matrixRows [][]byte, size, start, stop int, f func(matrix []byte, in, out [][]byte, start, stop int) int, vectorLength int) {
239
240 // reference versions
241 galMulSliceRef := func(c byte, in, out []byte) {
242 out = out[:len(in)]
243 mt := mulTable[c][:256]
244 for n, input := range in {
245 out[n] = mt[input]
246 }
247 }
248 galMulSliceXorRef := func(c byte, in, out []byte) {
249 out = out[:len(in)]
250 mt := mulTable[c][:256]
251 for n, input := range in {
252 out[n] ^= mt[input]
253 }
254 }
255
256 outputs := make([][]byte, len(matrixRows))
257 for i := range outputs {
258 outputs[i] = make([]byte, size)
259 if _, err := rand.Read(outputs[i]); err != nil {
260 t.Fatalf("error: %v", err)
261 return
262 }
263 }
264 inputs := make([][]byte, len(matrixRows[0]))
265 for i := range inputs {
266 inputs[i] = make([]byte, size)
267 if _, err := rand.Read(inputs[i]); err != nil {
268 t.Fatalf("error: %v", err)
269 return
270 }
271 }
272
273 m := genCodeGenMatrix(matrixRows, len(inputs), 0, len(outputs), vectorLength, nil)
274
275 end := start + f(m, inputs, outputs, start, stop)
276 if end != stop {
277 t.Errorf("got %#v, expected %#v", end, stop)
278 }
279
280 wanteds := make([][]byte, len(outputs))
281 for i := range wanteds {
282 wanteds[i] = make([]byte, size)
283 galMulSliceRef(matrixRows[i][0], inputs[0], wanteds[i])
284 for j := 1; j < len(matrixRows[i]); j++ {
285 galMulSliceXorRef(matrixRows[i][j], inputs[j], wanteds[i])
286 }
287 }
288
289 for i := range outputs {
290 if !bytes.Equal(outputs[i][start:stop], wanteds[i][start:stop]) {
291 t.Errorf("testGenGalois(%dx%d): got %#v, expected %#v", len(inputs), len(outputs), outputs[i][start:stop], wanteds[i][start:stop])
292 fmt.Printf("output[%d]\n", i)
293 fmt.Print(hex.Dump(outputs[i][start:stop]))
294 fmt.Printf("wanted[%d]\n", i)
295 fmt.Print(hex.Dump(wanteds[i][start:stop]))
296 }
297 }
298 }
299
300 func testGenGaloisXor(t *testing.T, matrixRows [][]byte, size, start, stop int, f func(matrix []byte, in, out [][]byte, start, stop int) int, vectorLength int) {
301
302 // reference version
303 galMulSliceXorRef := func(c byte, in, out []byte) {
304 out = out[:len(in)]
305 mt := mulTable[c][:256]
306 for n, input := range in {
307 out[n] ^= mt[input]
308 }
309 }
310
311 outputs := make([][]byte, len(matrixRows))
312 wanteds := make([][]byte, len(outputs))
313 for i := range outputs {
314 outputs[i] = make([]byte, size)
315 wanteds[i] = make([]byte, size)
316
317 // For Xor tests, prefill both outputs and wanted with identical values
318 copy(outputs[i], bytes.Repeat([]byte{byte(i)}, size))
319 copy(wanteds[i], outputs[i])
320 }
321 inputs := make([][]byte, len(matrixRows[0]))
322 for i := range inputs {
323 inputs[i] = make([]byte, size)
324 if _, err := rand.Read(inputs[i]); err != nil {
325 t.Fatalf("error: %v", err)
326 return
327 }
328 }
329
330 m := genCodeGenMatrix(matrixRows, len(inputs), 0, len(outputs), vectorLength, nil)
331
332 end := start + f(m, inputs, outputs, start, stop)
333 if end != stop {
334 t.Errorf("got %#v, expected %#v", end, stop)
335 }
336
337 for i := range wanteds {
338 for j := 0; j < len(matrixRows[i]); j++ {
339 galMulSliceXorRef(matrixRows[i][j], inputs[j], wanteds[i])
340 }
341 }
342
343 for i := range outputs {
344 if !bytes.Equal(outputs[i][start:stop], wanteds[i][start:stop]) {
345 t.Errorf("testGenGaloisXor(%dx%d): got %#v, expected %#v", len(inputs), len(outputs), outputs[i][start:stop], wanteds[i][start:stop])
346 fmt.Printf("output[%d]\n", i)
347 fmt.Print(hex.Dump(outputs[i][start:stop]))
348 fmt.Printf("wanted[%d]\n", i)
349 fmt.Print(hex.Dump(wanteds[i][start:stop]))
350 }
351 }
352 }
353
354 // Test early abort for galMulARCH_?x?_* routines
355 func testGenGaloisEarlyAbort(t *testing.T, matrixRows [][]byte, size int, f func(matrix []byte, in, out [][]byte, start, stop int) int) {
356 outputs := make([][]byte, len(matrixRows))
357 inputs := make([][]byte, len(matrixRows[0]))
358
359 start := 0
360 start += f(nil, inputs, outputs, 0, size)
361 if start != 0 {
362 t.Errorf("got %#v, expected %#v", start, 0)
363 }
364 }
365
366 func testGenGaloisUpto10x10(t *testing.T, f, fXor func(matrix []byte, in, out [][]byte, start, stop int) int, vectorLength int) {
367
368 for output := 1; output <= codeGenMaxOutputs; output++ {
369 for input := 1; input <= codeGenMaxInputs; input++ {
370 matrixRows := make([][]byte, output)
371 for i := range matrixRows {
372 matrixRows[i] = make([]byte, input)
373 for j := range matrixRows[i] {
374 matrixRows[i][j] = byte(mathrand.Intn(16))
375 }
376 }
377
378 size, stepsize := 32, 32
379 if output <= 3 {
380 size, stepsize = 64, 64 // ?x3 and below are all _64 versions
381 }
382
383 // test early abort
384 testGenGaloisEarlyAbort(t, matrixRows, size-1, f)
385 testGenGaloisEarlyAbort(t, matrixRows, size-1, fXor)
386 const limit = 1024
387 for ; size < limit; size += stepsize {
388 // test full range
389 testGenGalois(t, matrixRows, size, 0, size, f, vectorLength)
390 testGenGaloisXor(t, matrixRows, size, 0, size, fXor, vectorLength)
391
392 if size >= stepsize*2 && size < limit-stepsize*2 {
393 start := stepsize
394 stop := size - start
395 // test partial range
396 testGenGalois(t, matrixRows, size, start, stop, f, vectorLength)
397 testGenGaloisXor(t, matrixRows, size, start, stop, fXor, vectorLength)
398 }
399 }
400 }
401 }
402 }
403
404 // testGenGalois10xN tests ARM64 assembly which only has 10xN variants.
405 func testGenGalois10xN(t *testing.T, f, fXor func(matrix []byte, in, out [][]byte, start, stop int) int, vectorLength int) {
406 for output := 1; output <= codeGenMaxOutputs; output++ {
407 for input := 1; input <= codeGenMaxInputs; input++ {
408 matrixRows := make([][]byte, output)
409 for i := range matrixRows {
410 matrixRows[i] = make([]byte, input)
411 for j := range matrixRows[i] {
412 matrixRows[i][j] = byte(mathrand.Intn(16))
413 }
414 }
415 size, stepsize := 32, 32
416 if output <= 3 {
417 size, stepsize = 64, 64
418 }
419 testGenGaloisEarlyAbort(t, matrixRows, size-1, f)
420 testGenGaloisEarlyAbort(t, matrixRows, size-1, fXor)
421 const limit = 1024
422 for ; size < limit; size += stepsize {
423 testGenGalois(t, matrixRows, size, 0, size, f, vectorLength)
424 testGenGaloisXor(t, matrixRows, size, 0, size, fXor, vectorLength)
425 if size >= stepsize*2 && size < limit-stepsize*2 {
426 start := stepsize
427 stop := size - start
428 testGenGalois(t, matrixRows, size, start, stop, f, vectorLength)
429 testGenGaloisXor(t, matrixRows, size, start, stop, fXor, vectorLength)
430 }
431 }
432 }
433 }
434 }
435
436 func benchmarkGalois(b *testing.B, size int) {
437 in := make([]byte, size)
438 out := make([]byte, size)
439
440 o := options{}
441 o.useSSSE3, o.useAVX2 = !*noSSSE3, !*noAVX2
442
443 b.SetBytes(int64(size))
444 b.ResetTimer()
445 for i := 0; i < b.N; i++ {
446 galMulSlice(25, in[:], out[:], &o)
447 }
448 }
449
450 func BenchmarkGalois128K(b *testing.B) {
451 benchmarkGalois(b, 128*1024)
452 }
453
454 func BenchmarkGalois1M(b *testing.B) {
455 benchmarkGalois(b, 1024*1024)
456 }
457
458 func benchmarkGaloisXor(b *testing.B, size int) {
459 in := make([]byte, size)
460 out := make([]byte, size)
461
462 o := options{}
463 o.useSSSE3, o.useAVX2 = !*noSSSE3, !*noAVX2
464
465 b.SetBytes(int64(size))
466 b.ResetTimer()
467 for i := 0; i < b.N; i++ {
468 galMulSliceXor(177, in[:], out[:], &o)
469 }
470 }
471
472 func BenchmarkGaloisXor128K(b *testing.B) {
473 benchmarkGaloisXor(b, 128*1024)
474 }
475
476 func BenchmarkGaloisXor1M(b *testing.B) {
477 benchmarkGaloisXor(b, 1024*1024)
478 }