| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package gf256 |
| 7 |
|
| 8 |
import "strconv" |
| 9 |
|
| 10 |
|
| 11 |
type Field struct { |
| 12 |
log [256]byte |
| 13 |
exp [510]byte |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
s |
| 18 |
|
| 19 |
|
| 20 |
. |
| 21 |
func NewField(poly, α int) *Field { |
| 22 |
if poly < 0x100 || poly >= 0x200 || reducible(poly) { |
| 23 |
panic("gf256: invalid polynomial: " + strconv.Itoa(poly)) |
| 24 |
} |
| 25 |
|
| 26 |
var f Field |
| 27 |
x := 1 |
| 28 |
for i := 0; i < 255; i++ { |
| 29 |
if x == 1 && i != 0 { |
| 30 |
panic("gf256: invalid generator " + strconv.Itoa(α) + |
| 31 |
" for polynomial " + strconv.Itoa(poly)) |
| 32 |
} |
| 33 |
f.exp[i] = byte(x) |
| 34 |
f.exp[i+255] = byte(x) |
| 35 |
f.log[x] = byte(i) |
| 36 |
x = mul(x, α, poly) |
| 37 |
} |
| 38 |
f.log[0] = 255 |
| 39 |
for i := 0; i < 255; i++ { |
| 40 |
if f.log[f.exp[i]] != byte(i) { |
| 41 |
panic("bad log") |
| 42 |
} |
| 43 |
if f.log[f.exp[i+255]] != byte(i) { |
| 44 |
panic("bad log") |
| 45 |
} |
| 46 |
} |
| 47 |
for i := 1; i < 256; i++ { |
| 48 |
if f.exp[f.log[i]] != byte(i) { |
| 49 |
panic("bad log") |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return &f |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func nbit(p int) uint { |
| 58 |
n := uint(0) |
| 59 |
for ; p > 0; p >>= 1 { |
| 60 |
n++ |
| 61 |
} |
| 62 |
return n |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func polyDiv(p, q int) int { |
| 67 |
np := nbit(p) |
| 68 |
nq := nbit(q) |
| 69 |
for ; np >= nq; np-- { |
| 70 |
if p&(1<<(np-1)) != 0 { |
| 71 |
p ^= q << (np - nq) |
| 72 |
} |
| 73 |
} |
| 74 |
return p |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func mul(x, y, poly int) int { |
| 79 |
z := 0 |
| 80 |
for x > 0 { |
| 81 |
if x&1 != 0 { |
| 82 |
z ^= y |
| 83 |
} |
| 84 |
x >>= 1 |
| 85 |
y <<= 1 |
| 86 |
if y&0x100 != 0 { |
| 87 |
y ^= poly |
| 88 |
} |
| 89 |
} |
| 90 |
return z |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
func reducible(p int) bool { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
np := nbit(p) |
| 99 |
for q := 2; q < int(1<<(np/2+1)); q++ { |
| 100 |
if polyDiv(p, q) == 0 { |
| 101 |
return true |
| 102 |
} |
| 103 |
} |
| 104 |
return false |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func (f *Field) Add(x, y byte) byte { |
| 109 |
return x ^ y |
| 110 |
} |
| 111 |
|
| 112 |
. |
| 113 |
|
| 114 |
func (f *Field) Exp(e int) byte { |
| 115 |
if e < 0 { |
| 116 |
return 0 |
| 117 |
} |
| 118 |
return f.exp[e%255] |
| 119 |
} |
| 120 |
|
| 121 |
. |
| 122 |
|
| 123 |
func (f *Field) Log(x byte) int { |
| 124 |
if x == 0 { |
| 125 |
return -1 |
| 126 |
} |
| 127 |
return int(f.log[x]) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
func (f *Field) Inv(x byte) byte { |
| 133 |
if x == 0 { |
| 134 |
return 0 |
| 135 |
} |
| 136 |
return f.exp[255-f.log[x]] |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
func (f *Field) Mul(x, y byte) byte { |
| 141 |
if x == 0 || y == 0 { |
| 142 |
return 0 |
| 143 |
} |
| 144 |
return f.exp[int(f.log[x])+int(f.log[y])] |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
type RSEncoder struct { |
| 150 |
f *Field |
| 151 |
c int |
| 152 |
gen []byte |
| 153 |
lgen []byte |
| 154 |
p []byte |
| 155 |
} |
| 156 |
|
| 157 |
func (f *Field) gen(e int) (gen, lgen []byte) { |
| 158 |
|
| 159 |
p := make([]byte, e+1) |
| 160 |
p[e] = 1 |
| 161 |
|
| 162 |
for i := 0; i < e; i++ { |
| 163 |
|
| 164 |
|
| 165 |
c := f.Exp(i) |
| 166 |
for j := 0; j < e; j++ { |
| 167 |
p[j] = f.Mul(p[j], c) ^ p[j+1] |
| 168 |
} |
| 169 |
p[e] = f.Mul(p[e], c) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
lp := make([]byte, e+1) |
| 174 |
for i, c := range p { |
| 175 |
if c == 0 { |
| 176 |
lp[i] = 255 |
| 177 |
} else { |
| 178 |
lp[i] = byte(f.Log(c)) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return p, lp |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
func NewRSEncoder(f *Field, c int) *RSEncoder { |
| 188 |
gen, lgen := f.gen(c) |
| 189 |
return &RSEncoder{f: f, c: c, gen: gen, lgen: lgen} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
func (rs *RSEncoder) ECC(data []byte, check []byte) { |
| 195 |
if len(check) < rs.c { |
| 196 |
panic("gf256: invalid check byte length") |
| 197 |
} |
| 198 |
if rs.c == 0 { |
| 199 |
return |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
var p []byte |
| 207 |
n := len(data) + rs.c |
| 208 |
if len(rs.p) >= n { |
| 209 |
p = rs.p |
| 210 |
} else { |
| 211 |
p = make([]byte, n) |
| 212 |
} |
| 213 |
copy(p, data) |
| 214 |
for i := len(data); i < len(p); i++ { |
| 215 |
p[i] = 0 |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
f := rs.f |
| 225 |
lgen := rs.lgen[1:] |
| 226 |
for i := 0; i < len(data); i++ { |
| 227 |
c := p[i] |
| 228 |
if c == 0 { |
| 229 |
continue |
| 230 |
} |
| 231 |
q := p[i+1:] |
| 232 |
exp := f.exp[f.log[c]:] |
| 233 |
for j, lg := range lgen { |
| 234 |
if lg != 255 { |
| 235 |
q[j] ^= exp[lg] |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
copy(check, p[len(data):]) |
| 240 |
rs.p = p |
| 241 |
} |