| 1 |
package reedsolomon |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
// LowLevel exposes low level functionality. |
| 9 |
type LowLevel struct { |
| 10 |
o *options |
| 11 |
} |
| 12 |
|
| 13 |
// WithOptions resets the options to the default+provided options. |
| 14 |
// Options that don't apply to the called functions will be ignored. |
| 15 |
// This should not be called concurrent with other calls. |
| 16 |
func (l *LowLevel) WithOptions(opts ...Option) { |
| 17 |
o := defaultOptions |
| 18 |
for _, opt := range opts { |
| 19 |
opt(&o) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func (l LowLevel) options() *options { |
| 24 |
return cmp.Or(l.o, &defaultOptions) |
| 25 |
} |
| 26 |
|
| 27 |
// GalMulSlice multiplies the elements of in by c, writing the result to out: out[i] = c * in[i]. |
| 28 |
// out must be at least as long as in. |
| 29 |
func (l LowLevel) GalMulSlice(c byte, in, out []byte) { |
| 30 |
galMulSlice(c, in, out, l.options()) |
| 31 |
} |
| 32 |
|
| 33 |
// GalMulSliceXor multiplies the elements of in by c, and adds the result to out: out[i] ^= c * in[i]. |
| 34 |
// out must be at least as long as in. |
| 35 |
func (l LowLevel) GalMulSliceXor(c byte, in, out []byte) { |
| 36 |
galMulSliceXor(c, in, out, l.options()) |
| 37 |
} |
| 38 |
|
| 39 |
// Inv returns the multiplicative inverse of e in GF(2^8). |
| 40 |
// Should not be called with 0 (returns 0 in this case). |
| 41 |
func Inv(e byte) byte { |
| 42 |
return invTable[e] |
| 43 |
} |
| 44 |
|
| 45 |
// GF16MulSliceXor8 fuses 8 scalar-broadcast GF(2^16) mul-XOR accumulates |
| 46 |
// that share the same input slice `in`: |
| 47 |
// |
| 48 |
// out_k[i] ^= scalars[k] * in[i] for k in [0, 8) |
| 49 |
// |
| 50 |
// All 8 destination slices must share length with in, which must be a |
| 51 |
// multiple of 64 bytes (Leopard's chunk size: 32 low bytes + 32 high bytes |
| 52 |
// per chunk). A zero-length input is valid and returns immediately. |
| 53 |
// A zero scalar contributes nothing; callers do not need to filter them. |
| 54 |
func (l LowLevel) GF16MulSliceXor8(scalars *[8]uint16, in []byte, outs *[8][]byte) { |
| 55 |
if len(in) == 0 { |
| 56 |
return |
| 57 |
} |
| 58 |
if len(in)%64 != 0 { |
| 59 |
panic(fmt.Sprintf("reedsolomon: GF16MulSliceXor8: len(in)=%d must be a multiple of 64", len(in))) |
| 60 |
} |
| 61 |
for k := range outs { |
| 62 |
if len(outs[k]) != len(in) { |
| 63 |
panic(fmt.Sprintf("reedsolomon: GF16MulSliceXor8: outs[%d] has len %d, expected %d", k, len(outs[k]), len(in))) |
| 64 |
} |
| 65 |
} |
| 66 |
initConstants() |
| 67 |
mulgf16Xor8(scalars, in, outs, l.options()) |
| 68 |
} |
| 69 |
|
| 70 |
// GF16MulSliceXor multiplies each GF(2^16) element of in by scalar and |
| 71 |
// XOR-accumulates the result into out: |
| 72 |
// |
| 73 |
// out[i] ^= scalar * in[i] |
| 74 |
// |
| 75 |
// Both slices must have equal length, which must be a multiple of 64 bytes |
| 76 |
// (Leopard's chunk size: 32 low bytes + 32 high bytes per chunk). |
| 77 |
// A zero-length input is valid and returns immediately. |
| 78 |
// A zero scalar is a no-op. |
| 79 |
func (l LowLevel) GF16MulSliceXor(scalar uint16, in []byte, out []byte) { |
| 80 |
if len(in) == 0 { |
| 81 |
return |
| 82 |
} |
| 83 |
if scalar == 0 { |
| 84 |
return |
| 85 |
} |
| 86 |
if len(in)%64 != 0 { |
| 87 |
panic(fmt.Sprintf("reedsolomon: GF16MulSliceXor: len(in)=%d must be a multiple of 64", len(in))) |
| 88 |
} |
| 89 |
if len(out) != len(in) { |
| 90 |
panic(fmt.Sprintf("reedsolomon: GF16MulSliceXor: len(out)=%d, expected %d", len(out), len(in))) |
| 91 |
} |
| 92 |
initConstants() |
| 93 |
o := l.options() |
| 94 |
mulgf16Xor(out, in, logLUT[ffe(scalar)], o) |
| 95 |
} |