| 1 |
package reedsolomon |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/klauspost/cpuid/v2" |
| 8 |
) |
| 9 |
|
| 10 |
func TestAddMod8(t *testing.T) { |
| 11 |
type testCase struct { |
| 12 |
x ffe8 |
| 13 |
y ffe8 |
| 14 |
expected ffe8 |
| 15 |
} |
| 16 |
testCases := []testCase{ |
| 17 |
{x: ffe8(1), y: ffe8(2), expected: ffe8(3)}, |
| 18 |
{x: ffe8(253), y: ffe8(1), expected: ffe8(254)}, |
| 19 |
{x: ffe8(254), y: ffe8(2), expected: ffe8(1)}, |
| 20 |
{x: ffe8(255), y: ffe8(1), expected: ffe8(1)}, |
| 21 |
// it is expected that the following tests cases return modulus and that |
| 22 |
// callers of addMod will convert it to 0. |
| 23 |
{x: ffe8(254), y: ffe8(1), expected: ffe8(255)}, |
| 24 |
{x: ffe8(255), y: ffe8(0), expected: ffe8(255)}, |
| 25 |
{x: ffe8(255), y: ffe8(255), expected: ffe8(255)}, |
| 26 |
} |
| 27 |
for _, tc := range testCases { |
| 28 |
got := addMod8(tc.x, tc.y) |
| 29 |
if tc.expected != got { |
| 30 |
t.Errorf("expected %v, got %v", tc.expected, got) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestGFNIMultiplication(t *testing.T) { |
| 36 |
if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX512VL) { |
| 37 |
t.Skip("GFNI not supported") |
| 38 |
} |
| 39 |
|
| 40 |
// Create a simple encoder to ensure tables are initialized |
| 41 |
enc, err := New(4, 2, WithLeopardGF(true)) |
| 42 |
if err != nil { |
| 43 |
t.Fatal(err) |
| 44 |
} |
| 45 |
_ = enc |
| 46 |
|
| 47 |
// Test simple multiplication: multiply a single byte using both methods |
| 48 |
testValue := byte(0x42) |
| 49 |
multiplier := ffe8(5) // Use a non-trivial multiplier |
| 50 |
|
| 51 |
// Create test data - single byte repeated 64 times for SIMD alignment |
| 52 |
testData := make([]byte, 64) |
| 53 |
for i := range testData { |
| 54 |
testData[i] = testValue |
| 55 |
} |
| 56 |
|
| 57 |
// Test with AVX2 lookup table method |
| 58 |
avx2Result := make([]byte, 64) |
| 59 |
copy(avx2Result, testData) |
| 60 |
avx2Table := &multiply256LUT8[multiplier] |
| 61 |
|
| 62 |
// We need a simple way to call the multiplication - let's use the direct table lookup |
| 63 |
for i := range avx2Result { |
| 64 |
lo := avx2Result[i] & 0x0f |
| 65 |
hi := (avx2Result[i] >> 4) & 0x0f |
| 66 |
// First 16 bytes are for low nibble, second 16 bytes for high nibble |
| 67 |
avx2Result[i] = (*avx2Table)[lo] ^ (*avx2Table)[16+hi] |
| 68 |
} |
| 69 |
|
| 70 |
// Test with GFNI matrix method |
| 71 |
gfniResult := make([]byte, 64) |
| 72 |
copy(gfniResult, testData) |
| 73 |
gfniMatrix := gf2p811dMulMatricesLeo8[multiplier] |
| 74 |
|
| 75 |
// Manually compute what VGF2P8AFFINEQB should produce |
| 76 |
for i := range gfniResult { |
| 77 |
input := gfniResult[i] |
| 78 |
result := byte(0) |
| 79 |
|
| 80 |
// Apply 8x8 matrix transformation according to VGF2P8AFFINEQB spec |
| 81 |
// The matrix is stored with byte 0 controlling bit 7, byte 1 controlling bit 6, etc. |
| 82 |
matrix := gfniMatrix |
| 83 |
for resultBit := range 8 { |
| 84 |
// Get the matrix row for this result bit (note: bit 7-i indexing) |
| 85 |
row := byte((matrix >> (8 * (7 - resultBit))) & 0xff) |
| 86 |
// Compute dot product of input with this row |
| 87 |
dotProduct := byte(0) |
| 88 |
for inputBit := range 8 { |
| 89 |
if (input>>inputBit)&1 == 1 && (row>>inputBit)&1 == 1 { |
| 90 |
dotProduct ^= 1 |
| 91 |
} |
| 92 |
} |
| 93 |
result |= dotProduct << resultBit |
| 94 |
} |
| 95 |
gfniResult[i] = result |
| 96 |
} |
| 97 |
|
| 98 |
// Verify which result is correct using the reference implementation |
| 99 |
correctResult := mulLog8(ffe8(testValue), multiplier) |
| 100 |
|
| 101 |
// Compare results |
| 102 |
if !bytes.Equal(avx2Result, gfniResult) { |
| 103 |
t.Errorf("GFNI multiplication doesn't match AVX2") |
| 104 |
t.Logf("Input: %02x, Multiplier: %d", testValue, multiplier) |
| 105 |
t.Logf("AVX2 result: %02x", avx2Result[0]) |
| 106 |
t.Logf("GFNI result: %02x", gfniResult[0]) |
| 107 |
t.Logf("Correct result: %02x", correctResult) |
| 108 |
|
| 109 |
if avx2Result[0] == byte(correctResult) { |
| 110 |
t.Logf("AVX2 is correct, GFNI is wrong") |
| 111 |
} else if gfniResult[0] == byte(correctResult) { |
| 112 |
t.Logf("GFNI is correct, AVX2 is wrong") |
| 113 |
} else { |
| 114 |
t.Logf("Both are wrong!") |
| 115 |
} |
| 116 |
|
| 117 |
// Show first few bytes for debugging |
| 118 |
for i := range 8 { |
| 119 |
t.Logf("Byte %d: AVX2=%02x GFNI=%02x", i, avx2Result[i], gfniResult[i]) |
| 120 |
} |
| 121 |
|
| 122 |
// Debug the GFNI matrix |
| 123 |
t.Logf("GFNI matrix for multiplier %d: %016x", multiplier, gfniMatrix) |
| 124 |
} |
| 125 |
|
| 126 |
// NOTE: This test demonstrates the issue - it should be used to verify the fix |
| 127 |
t.Logf("This test shows GFNI vs AVX2 differences - used for debugging VGF2P8AFFINEQB immediate values") |
| 128 |
} |