Debian Codegraph @

gf256/gf256_test.go

raw symbols SCIP
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package gf256
6
7 import (
8 "bytes"
9 "fmt"
10 "testing"
11 )
12
13 var f = NewField(0x11d, 2) // x^8 + x^4 + x^3 + x^2 + 1
14
15 func TestBasic(t *testing.T) {
16 if f.Exp(0) != 1 || f.Exp(1) != 2 || f.Exp(255) != 1 {
17 panic("bad Exp")
18 }
19 }
20
21 func TestECC(t *testing.T) {
22 data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
23 check := []byte{0xa5, 0x24, 0xd4, 0xc1, 0xed, 0x36, 0xc7, 0x87, 0x2c, 0x55}
24 out := make([]byte, len(check))
25 rs := NewRSEncoder(f, len(check))
26 rs.ECC(data, out)
27 if !bytes.Equal(out, check) {
28 t.Errorf("have %x want %x", out, check)
29 }
30 }
31
32 func TestLinear(t *testing.T) {
33 d1 := []byte{0x00, 0x00}
34 c1 := []byte{0x00, 0x00}
35 out := make([]byte, len(c1))
36 rs := NewRSEncoder(f, len(c1))
37 if rs.ECC(d1, out); !bytes.Equal(out, c1) {
38 t.Errorf("ECBytes(%x, %d) = %x, want 0", d1, len(c1), out)
39 }
40 d2 := []byte{0x00, 0x01}
41 c2 := make([]byte, 2)
42 rs.ECC(d2, c2)
43 d3 := []byte{0x00, 0x02}
44 c3 := make([]byte, 2)
45 rs.ECC(d3, c3)
46 cx := make([]byte, 2)
47 for i := range cx {
48 cx[i] = c2[i] ^ c3[i]
49 }
50 d4 := []byte{0x00, 0x03}
51 c4 := make([]byte, 2)
52 rs.ECC(d4, c4)
53 if !bytes.Equal(cx, c4) {
54 t.Errorf("ECBytes(%x, 2) = %x\nECBytes(%x, 2) = %x\nxor = %x\nECBytes(%x, 2) = %x",
55 d2, c2, d3, c3, cx, d4, c4)
56 }
57 }
58
59 func TestGaussJordan(t *testing.T) {
60 rs := NewRSEncoder(f, 2)
61 m := make([][]byte, 16)
62 for i := range m {
63 m[i] = make([]byte, 4)
64 m[i][i/8] = 1 << uint(i%8)
65 rs.ECC(m[i][:2], m[i][2:])
66 }
67 if false {
68 fmt.Printf("---\n")
69 for _, row := range m {
70 fmt.Printf("%x\n", row)
71 }
72 }
73 b := []uint{0, 1, 2, 3, 12, 13, 14, 15, 20, 21, 22, 23, 24, 25, 26, 27}
74 for i := 0; i < 16; i++ {
75 bi := b[i]
76 if m[i][bi/8]&(1<<(7-bi%8)) == 0 {
77 for j := i + 1; ; j++ {
78 if j >= len(m) {
79 t.Errorf("lost track for %d", bi)
80 break
81 }
82 if m[j][bi/8]&(1<<(7-bi%8)) != 0 {
83 m[i], m[j] = m[j], m[i]
84 break
85 }
86 }
87 }
88 for j := i + 1; j < len(m); j++ {
89 if m[j][bi/8]&(1<<(7-bi%8)) != 0 {
90 for k := range m[j] {
91 m[j][k] ^= m[i][k]
92 }
93 }
94 }
95 }
96 if false {
97 fmt.Printf("---\n")
98 for _, row := range m {
99 fmt.Printf("%x\n", row)
100 }
101 }
102 for i := 15; i >= 0; i-- {
103 bi := b[i]
104 for j := i - 1; j >= 0; j-- {
105 if m[j][bi/8]&(1<<(7-bi%8)) != 0 {
106 for k := range m[j] {
107 m[j][k] ^= m[i][k]
108 }
109 }
110 }
111 }
112 if false {
113 fmt.Printf("---\n")
114 for _, row := range m {
115 fmt.Printf("%x", row)
116 out := make([]byte, 2)
117 if rs.ECC(row[:2], out); !bytes.Equal(out, row[2:]) {
118 fmt.Printf(" - want %x", out)
119 }
120 fmt.Printf("\n")
121 }
122 }
123 }
124
125 func BenchmarkECC(b *testing.B) {
126 data := []byte{0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0x10, 0x20, 0x0c, 0x56, 0x61, 0x80, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11, 0xec, 0x11}
127 check := []byte{0x29, 0x41, 0xb3, 0x93, 0x8, 0xe8, 0xa3, 0xe7, 0x63, 0x8f}
128 out := make([]byte, len(check))
129 rs := NewRSEncoder(f, len(check))
130 for i := 0; i < b.N; i++ {
131 rs.ECC(data, out)
132 }
133 b.SetBytes(int64(len(data)))
134 if !bytes.Equal(out, check) {
135 fmt.Printf("have %#v want %#v\n", out, check)
136 }
137 }
138
139 func TestGen(t *testing.T) {
140 for i := 0; i < 256; i++ {
141 _, lg := f.gen(i)
142 if lg[0] != 0 {
143 t.Errorf("#%d: %x", i, lg)
144 }
145 }
146 }
147
148 func TestReducible(t *testing.T) {
149 var count = []int{1, 2, 3, 6, 9, 18, 30, 56, 99, 186} // oeis.org/A1037
150 for i, want := range count {
151 n := 0
152 for p := 1 << uint(i+2); p < 1<<uint(i+3); p++ {
153 if !reducible(p) {
154 n++
155 }
156 }
157 if n != want {
158 t.Errorf("#reducible(%d-bit) = %d, want %d", i+2, n, want)
159 }
160 }
161 }
162
163 func TestExhaustive(t *testing.T) {
164 for poly := 0x100; poly < 0x200; poly++ {
165 if reducible(poly) {
166 continue
167 }
168 α := 2
169 for !generates(α, poly) {
170 α++
171 }
172 f := NewField(poly, α)
173 for p := 0; p < 256; p++ {
174 for q := 0; q < 256; q++ {
175 fm := int(f.Mul(byte(p), byte(q)))
176 pm := mul(p, q, poly)
177 if fm != pm {
178 t.Errorf("NewField(%#x).Mul(%#x, %#x) = %#x, want %#x", poly, p, q, fm, pm)
179 }
180 }
181 }
182 }
183 }
184
185 func generates(α, poly int) bool {
186 x := α
187 for i := 0; i < 254; i++ {
188 if x == 1 {
189 return false
190 }
191 x = mul(x, α, poly)
192 }
193 return true
194 }