Debian Codegraph @

galois_amd64_test.go

raw
1 //go:build !noasm && !appengine && !gccgo && !nopshufb
2
3 // Copyright 2015, Klaus Post, see LICENSE for details.
4
5 package reedsolomon
6
7 import (
8 "bytes"
9 "encoding/hex"
10 "fmt"
11 "maps"
12 "slices"
13 "testing"
14
15 "github.com/klauspost/cpuid/v2"
16 )
17
18 func TestGenGalois(t *testing.T) {
19 if defaultOptions.useAVX2 {
20 testGenGaloisUpto10x10(t, galMulSlicesAvx2, galMulSlicesAvx2Xor, 32)
21 }
22 }
23
24 func swapBytes64(x uint64) uint64 {
25 return ((x & 0xFF) << 56) |
26 ((x & 0xFF00) << 40) |
27 ((x & 0xFF0000) << 24) |
28 ((x & 0xFF000000) << 8) |
29 ((x & 0xFF00000000) >> 8) |
30 ((x & 0xFF0000000000) >> 24) |
31 ((x & 0xFF000000000000) >> 40) |
32 ((x & 0xFF00000000000000) >> 56)
33 }
34
35 func TestGenerateFinalMatrices(t *testing.T) {
36 // Initialize tables
37 t.Skip("Enable to generate GFNI matrices")
38 enc, err := New(4, 2, WithLeopardGF(true))
39 if err != nil {
40 t.Fatal(err)
41 }
42 _ = enc
43
44 // Generate first few matrices to verify they work
45 t.Logf("Generating and testing first 8 matrices...")
46
47 for logValue := range 8 {
48 var matrix uint64
49
50 // Use the proven working method
51 for inputBit := range 8 {
52 testInput := ffe8(1 << inputBit)
53 result := mulLog8(testInput, ffe8(logValue))
54 for outputBit := range 8 {
55 if (result>>outputBit)&1 == 1 {
56 matrixBitPos := outputBit*8 + inputBit
57 matrix |= 1 << matrixBitPos
58 }
59 }
60 }
61 matrix = swapBytes64(matrix)
62 }
63
64 t.Logf("All tested matrices work! Now generating full array...")
65
66 // Generate the complete array
67 fmt.Printf("\n// Complete corrected gf2p811dMulMatricesLeo8 array:\n")
68 fmt.Printf("var gf2p811dMulMatricesLeo8 = [256]uint64{\n")
69
70 for logValue := range 256 {
71 var matrix uint64
72
73 for inputBit := range 8 {
74 testInput := ffe8(1 << inputBit)
75 result := mulLog8(testInput, ffe8(logValue))
76 for outputBit := range 8 {
77 if (result>>outputBit)&1 == 1 {
78 matrixBitPos := outputBit*8 + inputBit
79 matrix |= 1 << matrixBitPos
80 }
81 }
82 }
83 matrix = swapBytes64(matrix)
84
85 if logValue%4 == 0 {
86 fmt.Printf("\t")
87 }
88
89 fmt.Printf("0x%016x", matrix)
90 if logValue < 255 {
91 fmt.Printf(", ")
92 }
93 if (logValue+1)%4 == 0 {
94 fmt.Printf("\n")
95 }
96 }
97
98 fmt.Printf("}\n")
99 }
100
101 func TestCompareIfftDIT48GFNIvsAVX2(t *testing.T) {
102 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX512VL) {
103 t.Skip("GFNI not supported")
104 }
105
106 // Create a simple encoder to ensure tables are initialized
107 enc, err := New(4, 2, WithLeopardGF(true))
108 if err != nil {
109 t.Fatal(err)
110 }
111 _ = enc
112
113 // Create test shards - 4 work slices of 64 bytes each (leopard8 requirement)
114 work := make([][]byte, 4)
115 for i := range work {
116 work[i] = make([]byte, 128)
117 for j := range work[i] {
118 work[i][j] = byte(i*16 + j + 1) // Simple test pattern
119 }
120 }
121
122 // Test each variant
123 results := make(map[int]string)
124 for variant := range 8 {
125 // Create copies for GFNI and AVX2
126 gfniWork := make([][]byte, len(work))
127 avx2Work := make([][]byte, len(work))
128 for i := range work {
129 gfniWork[i] = make([]byte, len(work[i]))
130 avx2Work[i] = make([]byte, len(work[i]))
131 copy(gfniWork[i], work[i])
132 copy(avx2Work[i], work[i])
133 }
134
135 // Call assembly functions directly
136 log_m01, log_m23, log_m02 := ffe8(10), ffe8(200), ffe8(30)
137
138 // Call GFNI variant directly
139 t01 := gf2p811dMulMatricesLeo8[log_m01]
140 t23 := gf2p811dMulMatricesLeo8[log_m23]
141 t02 := gf2p811dMulMatricesLeo8[log_m02]
142
143 switch variant {
144 case 0:
145 ifftDIT48_gfni_0(gfniWork, 24, t01, t23, t02)
146 case 1:
147 ifftDIT48_gfni_1(gfniWork, 24, t01, t23, t02)
148 case 2:
149 ifftDIT48_gfni_2(gfniWork, 24, t01, t23, t02)
150 case 3:
151 ifftDIT48_gfni_3(gfniWork, 24, t01, t23, t02)
152 case 4:
153 ifftDIT48_gfni_4(gfniWork, 24, t01, t23, t02)
154 case 5:
155 ifftDIT48_gfni_5(gfniWork, 24, t01, t23, t02)
156 case 6:
157 ifftDIT48_gfni_6(gfniWork, 24, t01, t23, t02)
158 case 7:
159 ifftDIT48_gfni_7(gfniWork, 24, t01, t23, t02)
160 }
161
162 // Call AVX2 variant directly
163 at01 := &multiply256LUT8[log_m01]
164 at23 := &multiply256LUT8[log_m23]
165 at02 := &multiply256LUT8[log_m02]
166 if false {
167 switch variant {
168 case 0:
169 ifftDIT48_avx2_0(avx2Work, 24, at01, at23, at02)
170 case 1:
171 ifftDIT48_avx2_1(avx2Work, 24, at01, at23, at02)
172 case 2:
173 ifftDIT48_avx2_2(avx2Work, 24, at01, at23, at02)
174 case 3:
175 ifftDIT48_avx2_3(avx2Work, 24, at01, at23, at02)
176 case 4:
177 ifftDIT48_avx2_4(avx2Work, 24, at01, at23, at02)
178 case 5:
179 ifftDIT48_avx2_5(avx2Work, 24, at01, at23, at02)
180 case 6:
181 ifftDIT48_avx2_6(avx2Work, 24, at01, at23, at02)
182 case 7:
183 ifftDIT48_avx2_7(avx2Work, 24, at01, at23, at02)
184 }
185 } else if true {
186 switch variant {
187 case 0:
188 ifftDIT4Ref8(avx2Work, 1, log_m01, log_m23, log_m02, &options{})
189 case 1:
190 ifftDIT4Ref8(avx2Work, 1, 255, log_m23, log_m02, &options{})
191 case 2:
192 ifftDIT4Ref8(avx2Work, 1, log_m01, 255, log_m02, &options{})
193 case 3:
194 ifftDIT4Ref8(avx2Work, 1, 255, 255, log_m02, &options{})
195 case 4:
196 ifftDIT4Ref8(avx2Work, 1, log_m01, log_m23, 255, &options{})
197 case 5:
198 ifftDIT4Ref8(avx2Work, 1, 255, log_m23, 255, &options{})
199 case 6:
200 ifftDIT4Ref8(avx2Work, 1, log_m01, 255, 255, &options{})
201 case 7:
202 ifftDIT4Ref8(avx2Work, 1, 255, 255, 255, &options{})
203 }
204 }
205
206 // Compare results
207 match := make(map[int]bool, len(gfniWork))
208 for i := range gfniWork {
209 if !bytes.Equal(gfniWork[i], avx2Work[i]) {
210 match[i] = false
211 }
212 }
213
214 if len(match) == 0 {
215 results[variant] = "✅ MATCH"
216 t.Logf("ifftDIT48_%d: MATCH ✅", variant)
217 } else {
218 results[variant] = "❌ MISMATCH"
219 t.Logf("ifftDIT48_%d: MISMATCH ❌", variant)
220 for _, i := range slices.Sorted(maps.Keys(match)) {
221 t.Logf(" Work idx %d: avx2: %s, gfni: %s", i, hex.EncodeToString(avx2Work[i]), hex.EncodeToString(gfniWork[i]))
222 }
223 }
224 }
225
226 // Summary
227 t.Logf("\n=== ifftDIT48 SUMMARY ===")
228 for variant := range 8 {
229 t.Logf("Variant %d: %s", variant, results[variant])
230 }
231 }
232
233 func TestCompareFftDIT48GFNIvsAVX2(t *testing.T) {
234 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX512VL) {
235 t.Skip("GFNI not supported")
236 }
237
238 // Create a simple encoder to ensure tables are initialized
239 enc, err := New(4, 2, WithLeopardGF(true))
240 if err != nil {
241 t.Fatal(err)
242 }
243 _ = enc
244
245 // Create test shards - 4 work slices of 64 bytes each (leopard8 requirement)
246 work := make([][]byte, 4)
247 for i := range work {
248 work[i] = make([]byte, 128)
249 for j := range work[i] {
250 work[i][j] = byte(i*16 + j + 1) // Simple test pattern
251 }
252 }
253
254 // Test each variant
255 results := make(map[int]string)
256 for variant := range 8 {
257 // Create copies for GFNI and AVX2
258 gfniWork := make([][]byte, len(work))
259 avx2Work := make([][]byte, len(work))
260 for i := range work {
261 gfniWork[i] = make([]byte, len(work[i]))
262 avx2Work[i] = make([]byte, len(work[i]))
263 copy(gfniWork[i], work[i])
264 copy(avx2Work[i], work[i])
265 }
266
267 // Call assembly functions directly
268 log_m01, log_m23, log_m02 := ffe8(1), ffe8(2), ffe8(3)
269
270 // Call GFNI variant directly
271 t01 := gf2p811dMulMatricesLeo8[log_m01]
272 t23 := gf2p811dMulMatricesLeo8[log_m23]
273 t02 := gf2p811dMulMatricesLeo8[log_m02]
274
275 switch variant {
276 case 0:
277 fftDIT48_gfni_0(gfniWork, 24, t01, t23, t02)
278 case 1:
279 fftDIT48_gfni_1(gfniWork, 24, t01, t23, t02)
280 case 2:
281 fftDIT48_gfni_2(gfniWork, 24, t01, t23, t02)
282 case 3:
283 fftDIT48_gfni_3(gfniWork, 24, t01, t23, t02)
284 case 4:
285 fftDIT48_gfni_4(gfniWork, 24, t01, t23, t02)
286 case 5:
287 fftDIT48_gfni_5(gfniWork, 24, t01, t23, t02)
288 case 6:
289 fftDIT48_gfni_6(gfniWork, 24, t01, t23, t02)
290 case 7:
291 fftDIT48_gfni_7(gfniWork, 24, t01, t23, t02)
292 }
293
294 // Call AVX2 variant directly
295 at01 := &multiply256LUT8[log_m01]
296 at23 := &multiply256LUT8[log_m23]
297 at02 := &multiply256LUT8[log_m02]
298
299 switch variant {
300 case 0:
301 fftDIT48_avx2_0(avx2Work, 24, at01, at23, at02)
302 case 1:
303 fftDIT48_avx2_1(avx2Work, 24, at01, at23, at02)
304 case 2:
305 fftDIT48_avx2_2(avx2Work, 24, at01, at23, at02)
306 case 3:
307 fftDIT48_avx2_3(avx2Work, 24, at01, at23, at02)
308 case 4:
309 fftDIT48_avx2_4(avx2Work, 24, at01, at23, at02)
310 case 5:
311 fftDIT48_avx2_5(avx2Work, 24, at01, at23, at02)
312 case 6:
313 fftDIT48_avx2_6(avx2Work, 24, at01, at23, at02)
314 case 7:
315 fftDIT48_avx2_7(avx2Work, 24, at01, at23, at02)
316 }
317
318 // Compare results
319 match := true
320 for i := range gfniWork {
321 if !bytes.Equal(gfniWork[i], avx2Work[i]) {
322 match = false
323 break
324 }
325 }
326
327 if match {
328 results[variant] = "✅ MATCH"
329 t.Logf("fftDIT48_%d: MATCH ✅", variant)
330 } else {
331 results[variant] = "❌ MISMATCH"
332 t.Logf("fftDIT48_%d: MISMATCH ❌", variant)
333 }
334 }
335
336 // Summary
337 t.Logf("\n=== fftDIT48 SUMMARY ===")
338 for variant := range 8 {
339 t.Logf("Variant %d: %s", variant, results[variant])
340 }
341 }
342
343 func TestDebugGFNI(t *testing.T) {
344 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX512VL) {
345 t.Skip("GFNI not supported")
346 }
347
348 // Create a simple encoder to ensure tables are initialized
349 enc, err := New(4, 2, WithLeopardGF(true))
350 if err != nil {
351 t.Fatal(err)
352 }
353 _ = enc
354
355 // Create test shards - 4 work slices of 64 bytes each
356 work := make([][]byte, 4)
357 for i := range work {
358 work[i] = make([]byte, 64)
359 for j := range work[i] {
360 work[i][j] = byte(i*16 + j + 1) // Simple test pattern
361 }
362 }
363
364 // Test variant 0 (no skips) - this should do all multiplications
365 log_m01, log_m23, log_m02 := ffe8(1), ffe8(2), ffe8(3)
366
367 // Create copies for GFNI, reference, and AVX2
368 gfniWork := make([][]byte, len(work))
369 refWork := make([][]byte, len(work))
370 avx2Work := make([][]byte, len(work))
371 for i := range work {
372 gfniWork[i] = make([]byte, len(work[i]))
373 refWork[i] = make([]byte, len(work[i]))
374 avx2Work[i] = make([]byte, len(work[i]))
375 copy(gfniWork[i], work[i])
376 copy(refWork[i], work[i])
377 copy(avx2Work[i], work[i])
378 }
379
380 // Call reference implementation
381 o := &options{}
382 ifftDIT4Ref8(refWork, 1, log_m01, log_m23, log_m02, o)
383
384 // Call GFNI directly
385 t01 := gf2p811dMulMatricesLeo8[log_m01]
386 t23 := gf2p811dMulMatricesLeo8[log_m23]
387 t02 := gf2p811dMulMatricesLeo8[log_m02]
388 ifftDIT48_gfni_0(gfniWork, 24, t01, t23, t02)
389
390 // Call AVX2 directly
391 at01 := &multiply256LUT8[log_m01]
392 at23 := &multiply256LUT8[log_m23]
393 at02 := &multiply256LUT8[log_m02]
394 ifftDIT48_avx2_0(avx2Work, 24, at01, at23, at02)
395
396 // Compare byte by byte
397 t.Logf("Comparing GFNI vs Reference vs AVX2:")
398 for i := range work {
399 t.Logf("\n=== Work[%d] ===", i)
400
401 // Find first difference
402 firstDiff := -1
403 for j := range work[i] {
404 if gfniWork[i][j] != refWork[i][j] || avx2Work[i][j] != refWork[i][j] {
405 firstDiff = j
406 break
407 }
408 }
409
410 if firstDiff >= 0 {
411 // Show a few bytes around the first difference
412 start := firstDiff
413 if start > 2 {
414 start = firstDiff - 2
415 }
416 end := min(firstDiff+5, len(work[i]))
417
418 t.Logf("First difference at byte %d:", firstDiff)
419 t.Logf(" Original: %v", work[i][start:end])
420 t.Logf(" Ref: %v", refWork[i][start:end])
421 t.Logf(" AVX2: %v", avx2Work[i][start:end])
422 t.Logf(" GFNI: %v", gfniWork[i][start:end])
423
424 // Show the specific byte difference
425 if firstDiff < len(work[i]) {
426 t.Logf(" At byte %d: Orig=%02x, Ref=%02x, AVX2=%02x, GFNI=%02x",
427 firstDiff, work[i][firstDiff], refWork[i][firstDiff],
428 avx2Work[i][firstDiff], gfniWork[i][firstDiff])
429 }
430 } else if bytes.Equal(gfniWork[i], refWork[i]) && bytes.Equal(avx2Work[i], refWork[i]) {
431 t.Logf(" ✅ All match!")
432 }
433 }
434
435 // Check if AVX2 matches reference
436 avx2MatchesRef := true
437 for i := range work {
438 if !bytes.Equal(avx2Work[i], refWork[i]) {
439 avx2MatchesRef = false
440 break
441 }
442 }
443
444 // Check if GFNI matches reference
445 gfniMatchesRef := true
446 for i := range work {
447 if !bytes.Equal(gfniWork[i], refWork[i]) {
448 gfniMatchesRef = false
449 break
450 }
451 }
452
453 t.Logf("\n=== Summary ===")
454 t.Logf("AVX2 matches Reference: %v", avx2MatchesRef)
455 t.Logf("GFNI matches Reference: %v", gfniMatchesRef)
456
457 // If GFNI doesn't match but AVX2 does, let's debug the matrix multiplication
458 if !gfniMatchesRef && avx2MatchesRef {
459 t.Logf("\n=== Debugging Matrix Multiplication ===")
460 // Test a single multiplication with log_m01 (which is 1)
461 testInput := byte(0x01)
462
463 // Reference using mulLog8
464 refResult := mulLog8(ffe8(testInput), ffe8(log_m01))
465
466 // AVX2 table lookup
467 avx2Table := &multiply256LUT8[log_m01]
468 lo := testInput & 0x0f
469 hi := (testInput >> 4) & 0x0f
470 avx2Result := (*avx2Table)[lo] ^ (*avx2Table)[16+hi]
471
472 // GFNI matrix
473 matrix := gf2p811dMulMatricesLeo8[log_m01]
474 gfniResult := applyGFNIMatrixDebug(testInput, matrix)
475
476 t.Logf("Single byte test with log_m01=%d:", log_m01)
477 t.Logf(" Input: 0x%02x", testInput)
478 t.Logf(" Ref: 0x%02x (using mulLog8)", refResult)
479 t.Logf(" AVX2: 0x%02x (using table lookup)", avx2Result)
480 t.Logf(" GFNI: 0x%02x (using matrix 0x%016x)", gfniResult, matrix)
481
482 // Check what multiply256LUT8 actually contains
483 t.Logf("\n multiply256LUT8[%d] first 16 bytes (low nibble): %v", log_m01, (*avx2Table)[:16])
484 t.Logf(" multiply256LUT8[%d] next 16 bytes (high nibble): %v", log_m01, (*avx2Table)[16:32])
485 }
486 }
487
488 func applyGFNIMatrixDebug(input byte, matrix uint64) byte {
489 result := byte(0)
490 // Apply 8x8 matrix transformation according to VGF2P8AFFINEQB spec
491 for resultBit := range 8 {
492 // Get the matrix row for this result bit (byte 0 controls bit 7, etc.)
493 row := byte((matrix >> (8 * (7 - resultBit))) & 0xff)
494 // Compute dot product of input with this row
495 dotProduct := byte(0)
496 for inputBit := range 8 {
497 if (input>>inputBit)&1 == 1 && (row>>inputBit)&1 == 1 {
498 dotProduct ^= 1
499 }
500 }
501 result |= dotProduct << resultBit
502 }
503 // VGF2P8AFFINEQB with immediate 0x00 - no XOR needed
504 return result
505 }
506
507 func TestCompareIfftDIT2GF16GFNIvsAVX2(t *testing.T) {
508 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX) {
509 t.Skip("AVX+GFNI not supported")
510 }
511
512 // Create a simple encoder to ensure tables are initialized
513 enc, err := New(300, 100, WithLeopardGF16(true))
514 if err != nil {
515 t.Fatal(err)
516 }
517 _ = enc
518
519 if gf2p811dMulMatrices16 == nil {
520 t.Skip("GFNI tables not initialized")
521 }
522
523 // Test with various log_m values
524 testCases := []ffe{0, 1, 100, 1000, 10000, 30000, 65535}
525
526 for _, logM := range testCases {
527 t.Run(fmt.Sprintf("logM=%d", logM), func(t *testing.T) {
528 // Create test data - 128 bytes (64 16-bit elements)
529 x := make([]byte, 128)
530 y := make([]byte, 128)
531 for i := range x {
532 x[i] = byte(i + 1)
533 y[i] = byte(i*2 + 3)
534 }
535
536 // Make copies for both implementations
537 xGFNI := make([]byte, len(x))
538 yGFNI := make([]byte, len(y))
539 copy(xGFNI, x)
540 copy(yGFNI, y)
541
542 xAVX2 := make([]byte, len(x))
543 yAVX2 := make([]byte, len(y))
544 copy(xAVX2, x)
545 copy(yAVX2, y)
546
547 // Compute Go reference (correct result)
548 // Data layout is SPLIT: [0:32] = lo bytes, [32:64] = hi bytes of 32 elements
549 // Then [64:96] = lo bytes, [96:128] = hi bytes of next 32 elements
550 xRef := make([]byte, len(x))
551 yRef := make([]byte, len(y))
552 copy(xRef, x)
553 copy(yRef, y)
554 // Process 64 bytes at a time (32 elements)
555 for chunk := range 2 {
556 base := chunk * 64
557 for i := range 32 {
558 xElem := ffe(x[base+i]) | (ffe(x[base+32+i]) << 8)
559 yElem := ffe(y[base+i]) | (ffe(y[base+32+i]) << 8)
560 yNew := xElem ^ yElem
561 xNew := xElem ^ mulLog(yNew, logM)
562 xRef[base+i] = byte(xNew)
563 xRef[base+32+i] = byte(xNew >> 8)
564 yRef[base+i] = byte(yNew)
565 yRef[base+32+i] = byte(yNew >> 8)
566 }
567 }
568
569 // Run GFNI version
570 gfniTable := &gf2p811dMulMatrices16[logM]
571 ifftDIT2_gfni(xGFNI, yGFNI, gfniTable)
572
573 // Run AVX2 version
574 avx2Table := &multiply256LUT[logM]
575 ifftDIT2_avx2(xAVX2, yAVX2, avx2Table)
576
577 // Compare against reference
578 gfniOK := bytes.Equal(xGFNI, xRef) && bytes.Equal(yGFNI, yRef)
579 avx2OK := bytes.Equal(xAVX2, xRef) && bytes.Equal(yAVX2, yRef)
580
581 if !gfniOK || !avx2OK {
582 t.Errorf("logM=%d: GFNI correct=%v, AVX2 correct=%v", logM, gfniOK, avx2OK)
583 for i := range 8 {
584 t.Logf(" x[%d]: Ref=0x%02x, GFNI=0x%02x, AVX2=0x%02x", i*2, xRef[i*2], xGFNI[i*2], xAVX2[i*2])
585 }
586 }
587 })
588 }
589 }
590
591 func TestCompareFftDIT2GF16GFNIvsAVX2(t *testing.T) {
592 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX) {
593 t.Skip("AVX+GFNI not supported")
594 }
595
596 // Create a simple encoder to ensure tables are initialized
597 enc, err := New(300, 100, WithLeopardGF16(true))
598 if err != nil {
599 t.Fatal(err)
600 }
601 _ = enc
602
603 if gf2p811dMulMatrices16 == nil {
604 t.Skip("GFNI tables not initialized")
605 }
606
607 // Test with various log_m values
608 testCases := []ffe{0, 1, 100, 1000, 10000, 30000, 65535}
609
610 for _, logM := range testCases {
611 t.Run(fmt.Sprintf("logM=%d", logM), func(t *testing.T) {
612 // Create test data - 128 bytes (64 16-bit elements)
613 x := make([]byte, 128)
614 y := make([]byte, 128)
615 for i := range x {
616 x[i] = byte(i + 1)
617 y[i] = byte(i*2 + 3)
618 }
619
620 // Make copies for both implementations
621 xGFNI := make([]byte, len(x))
622 yGFNI := make([]byte, len(y))
623 copy(xGFNI, x)
624 copy(yGFNI, y)
625
626 xAVX2 := make([]byte, len(x))
627 yAVX2 := make([]byte, len(y))
628 copy(xAVX2, x)
629 copy(yAVX2, y)
630
631 // Compute Go reference (fftDIT2: x_new = x ^ mul(y, logM), y_new = x_new ^ y)
632 xRef := make([]byte, len(x))
633 yRef := make([]byte, len(y))
634 copy(xRef, x)
635 copy(yRef, y)
636 for chunk := range 2 {
637 base := chunk * 64
638 for i := range 32 {
639 xElem := ffe(x[base+i]) | (ffe(x[base+32+i]) << 8)
640 yElem := ffe(y[base+i]) | (ffe(y[base+32+i]) << 8)
641 xNew := xElem ^ mulLog(yElem, logM)
642 yNew := xNew ^ yElem
643 xRef[base+i] = byte(xNew)
644 xRef[base+32+i] = byte(xNew >> 8)
645 yRef[base+i] = byte(yNew)
646 yRef[base+32+i] = byte(yNew >> 8)
647 }
648 }
649
650 // Run GFNI version
651 gfniTable := &gf2p811dMulMatrices16[logM]
652 fftDIT2_gfni(xGFNI, yGFNI, gfniTable)
653
654 // Run AVX2 version
655 avx2Table := &multiply256LUT[logM]
656 fftDIT2_avx2(xAVX2, yAVX2, avx2Table)
657
658 // Compare against reference
659 xGfniOK := bytes.Equal(xGFNI, xRef)
660 yGfniOK := bytes.Equal(yGFNI, yRef)
661 xAvx2OK := bytes.Equal(xAVX2, xRef)
662 yAvx2OK := bytes.Equal(yAVX2, yRef)
663
664 if !xGfniOK || !yGfniOK || !xAvx2OK || !yAvx2OK {
665 t.Errorf("logM=%d: GFNI x=%v y=%v, AVX2 x=%v y=%v", logM, xGfniOK, yGfniOK, xAvx2OK, yAvx2OK)
666 if !yGfniOK || !yAvx2OK {
667 for i := range 8 {
668 t.Logf(" y[%d]: Ref=0x%02x, GFNI=0x%02x, AVX2=0x%02x", i, yRef[i], yGFNI[i], yAVX2[i])
669 }
670 }
671 }
672 })
673 }
674 }
675
676 func TestCompareIfftDIT4GF16GFNIvsAVX2(t *testing.T) {
677 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX) {
678 t.Skip("AVX+GFNI not supported")
679 }
680
681 enc, err := New(300, 100, WithLeopardGF16(true))
682 if err != nil {
683 t.Fatal(err)
684 }
685 _ = enc
686
687 if gf2p811dMulMatrices16 == nil {
688 t.Skip("GFNI tables not initialized")
689 }
690
691 // Test all 8 variants (skipMask 0-7)
692 results := make([]bool, 8)
693 for variant := range 8 {
694 t.Run(fmt.Sprintf("variant=%d", variant), func(t *testing.T) {
695 // Create work slices - 4 slices of 64 bytes each (32 x 16-bit elements)
696 work := make([][]byte, 4)
697 for i := range work {
698 work[i] = make([]byte, 64)
699 for j := range work[i] {
700 work[i][j] = byte(i*17 + j + 1)
701 }
702 }
703
704 // Make copies
705 gfniWork := make([][]byte, 4)
706 avx2Work := make([][]byte, 4)
707 for i := range work {
708 gfniWork[i] = make([]byte, len(work[i]))
709 avx2Work[i] = make([]byte, len(work[i]))
710 copy(gfniWork[i], work[i])
711 copy(avx2Work[i], work[i])
712 }
713
714 // Set log_m values based on variant
715 log_m01, log_m23, log_m02 := ffe(10), ffe(200), ffe(3000)
716 if variant&1 != 0 {
717 log_m01 = modulus
718 }
719 if variant&2 != 0 {
720 log_m23 = modulus
721 }
722 if variant&4 != 0 {
723 log_m02 = modulus
724 }
725
726 // Call GFNI version
727 g01 := &gf2p811dMulMatrices16[log_m01]
728 g23 := &gf2p811dMulMatrices16[log_m23]
729 g02 := &gf2p811dMulMatrices16[log_m02]
730 switch variant {
731 case 0:
732 ifftDIT4_gfni_0(gfniWork, 24, g01, g23, g02)
733 case 1:
734 ifftDIT4_gfni_1(gfniWork, 24, g01, g23, g02)
735 case 2:
736 ifftDIT4_gfni_2(gfniWork, 24, g01, g23, g02)
737 case 3:
738 ifftDIT4_gfni_3(gfniWork, 24, g01, g23, g02)
739 case 4:
740 ifftDIT4_gfni_4(gfniWork, 24, g01, g23, g02)
741 case 5:
742 ifftDIT4_gfni_5(gfniWork, 24, g01, g23, g02)
743 case 6:
744 ifftDIT4_gfni_6(gfniWork, 24, g01, g23, g02)
745 case 7:
746 ifftDIT4_gfni_7(gfniWork, 24, g01, g23, g02)
747 }
748
749 // Call AVX2 version
750 t01 := &multiply256LUT[log_m01]
751 t23 := &multiply256LUT[log_m23]
752 t02 := &multiply256LUT[log_m02]
753 switch variant {
754 case 0:
755 ifftDIT4_avx2_0(avx2Work, 24, t01, t23, t02)
756 case 1:
757 ifftDIT4_avx2_1(avx2Work, 24, t01, t23, t02)
758 case 2:
759 ifftDIT4_avx2_2(avx2Work, 24, t01, t23, t02)
760 case 3:
761 ifftDIT4_avx2_3(avx2Work, 24, t01, t23, t02)
762 case 4:
763 ifftDIT4_avx2_4(avx2Work, 24, t01, t23, t02)
764 case 5:
765 ifftDIT4_avx2_5(avx2Work, 24, t01, t23, t02)
766 case 6:
767 ifftDIT4_avx2_6(avx2Work, 24, t01, t23, t02)
768 case 7:
769 ifftDIT4_avx2_7(avx2Work, 24, t01, t23, t02)
770 }
771
772 // Compare results
773 match := true
774 for i := range work {
775 if !bytes.Equal(gfniWork[i], avx2Work[i]) {
776 match = false
777 t.Errorf("work[%d] mismatch for variant %d", i, variant)
778 for j := 0; j < len(gfniWork[i]) && j < 16; j++ {
779 if gfniWork[i][j] != avx2Work[i][j] {
780 t.Logf(" [%d][%d]: GFNI=0x%02x, AVX2=0x%02x", i, j, gfniWork[i][j], avx2Work[i][j])
781 }
782 }
783 }
784 }
785 results[variant] = match
786 if match {
787 t.Logf("ifftDIT4_gfni_%d: MATCH", variant)
788 }
789 })
790 }
791 }
792
793 func TestCompareFftDIT4GF16GFNIvsAVX2(t *testing.T) {
794 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX) {
795 t.Skip("AVX+GFNI not supported")
796 }
797
798 enc, err := New(300, 100, WithLeopardGF16(true))
799 if err != nil {
800 t.Fatal(err)
801 }
802 _ = enc
803
804 if gf2p811dMulMatrices16 == nil {
805 t.Skip("GFNI tables not initialized")
806 }
807
808 // Test all 8 variants (skipMask 0-7)
809 results := make([]bool, 8)
810 for variant := range 8 {
811 t.Run(fmt.Sprintf("variant=%d", variant), func(t *testing.T) {
812 // Create work slices - 4 slices of 64 bytes each (32 x 16-bit elements)
813 work := make([][]byte, 4)
814 for i := range work {
815 work[i] = make([]byte, 64)
816 for j := range work[i] {
817 work[i][j] = byte(i*17 + j + 1)
818 }
819 }
820
821 // Make copies
822 gfniWork := make([][]byte, 4)
823 avx2Work := make([][]byte, 4)
824 for i := range work {
825 gfniWork[i] = make([]byte, len(work[i]))
826 avx2Work[i] = make([]byte, len(work[i]))
827 copy(gfniWork[i], work[i])
828 copy(avx2Work[i], work[i])
829 }
830
831 // Set log_m values based on variant - fftDIT4 has different bit mapping
832 log_m01, log_m23, log_m02 := ffe(10), ffe(200), ffe(3000)
833 if variant&2 != 0 {
834 log_m01 = modulus
835 }
836 if variant&4 != 0 {
837 log_m23 = modulus
838 }
839 if variant&1 != 0 {
840 log_m02 = modulus
841 }
842
843 // Call GFNI version
844 g01 := &gf2p811dMulMatrices16[log_m01]
845 g23 := &gf2p811dMulMatrices16[log_m23]
846 g02 := &gf2p811dMulMatrices16[log_m02]
847 switch variant {
848 case 0:
849 fftDIT4_gfni_0(gfniWork, 24, g01, g23, g02)
850 case 1:
851 fftDIT4_gfni_1(gfniWork, 24, g01, g23, g02)
852 case 2:
853 fftDIT4_gfni_2(gfniWork, 24, g01, g23, g02)
854 case 3:
855 fftDIT4_gfni_3(gfniWork, 24, g01, g23, g02)
856 case 4:
857 fftDIT4_gfni_4(gfniWork, 24, g01, g23, g02)
858 case 5:
859 fftDIT4_gfni_5(gfniWork, 24, g01, g23, g02)
860 case 6:
861 fftDIT4_gfni_6(gfniWork, 24, g01, g23, g02)
862 case 7:
863 fftDIT4_gfni_7(gfniWork, 24, g01, g23, g02)
864 }
865
866 // Call AVX2 version
867 t01 := &multiply256LUT[log_m01]
868 t23 := &multiply256LUT[log_m23]
869 t02 := &multiply256LUT[log_m02]
870 switch variant {
871 case 0:
872 fftDIT4_avx2_0(avx2Work, 24, t01, t23, t02)
873 case 1:
874 fftDIT4_avx2_1(avx2Work, 24, t01, t23, t02)
875 case 2:
876 fftDIT4_avx2_2(avx2Work, 24, t01, t23, t02)
877 case 3:
878 fftDIT4_avx2_3(avx2Work, 24, t01, t23, t02)
879 case 4:
880 fftDIT4_avx2_4(avx2Work, 24, t01, t23, t02)
881 case 5:
882 fftDIT4_avx2_5(avx2Work, 24, t01, t23, t02)
883 case 6:
884 fftDIT4_avx2_6(avx2Work, 24, t01, t23, t02)
885 case 7:
886 fftDIT4_avx2_7(avx2Work, 24, t01, t23, t02)
887 }
888
889 // Compare results
890 match := true
891 for i := range work {
892 if !bytes.Equal(gfniWork[i], avx2Work[i]) {
893 match = false
894 t.Errorf("work[%d] mismatch for variant %d", i, variant)
895 for j := 0; j < len(gfniWork[i]) && j < 16; j++ {
896 if gfniWork[i][j] != avx2Work[i][j] {
897 t.Logf(" [%d][%d]: GFNI=0x%02x, AVX2=0x%02x", i, j, gfniWork[i][j], avx2Work[i][j])
898 }
899 }
900 }
901 }
902 results[variant] = match
903 if match {
904 t.Logf("fftDIT4_gfni_%d: MATCH", variant)
905 }
906 })
907 }
908 }
909
910 func TestCompareIfftDIT4GF16GFNIAvx512vsAVX2(t *testing.T) {
911 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX512VL) {
912 t.Skip("AVX512+GFNI not supported")
913 }
914
915 enc, err := New(300, 100, WithLeopardGF16(true))
916 if err != nil {
917 t.Fatal(err)
918 }
919 _ = enc
920
921 if gf2p811dMulMatrices16 == nil {
922 t.Skip("GFNI tables not initialized")
923 }
924
925 // Test variant 0 (all multiplications)
926 for variant := range 8 {
927 t.Run(fmt.Sprintf("variant=%d", variant), func(t *testing.T) {
928 // Create work slices - 4 slices of 128 bytes each (64 x 16-bit elements)
929 work := make([][]byte, 4)
930 for i := range work {
931 work[i] = make([]byte, 128)
932 for j := range work[i] {
933 work[i][j] = byte(i*17 + j + 1)
934 }
935 }
936
937 // Make copies
938 avx512Work := make([][]byte, 4)
939 avx2Work := make([][]byte, 4)
940 for i := range work {
941 avx512Work[i] = make([]byte, len(work[i]))
942 avx2Work[i] = make([]byte, len(work[i]))
943 copy(avx512Work[i], work[i])
944 copy(avx2Work[i], work[i])
945 }
946
947 log_m01, log_m23, log_m02 := ffe(10), ffe(200), ffe(3000)
948 if variant&1 != 0 {
949 log_m01 = modulus
950 }
951 if variant&2 != 0 {
952 log_m23 = modulus
953 }
954 if variant&4 != 0 {
955 log_m02 = modulus
956 }
957
958 g01 := &gf2p811dMulMatrices16[log_m01]
959 g23 := &gf2p811dMulMatrices16[log_m23]
960 g02 := &gf2p811dMulMatrices16[log_m02]
961
962 // Call AVX-512 GFNI version
963 switch variant {
964 case 0:
965 ifftDIT4_gfni_avx512_0(avx512Work, 24, g01, g23, g02)
966 case 1:
967 ifftDIT4_gfni_avx512_1(avx512Work, 24, g01, g23, g02)
968 case 2:
969 ifftDIT4_gfni_avx512_2(avx512Work, 24, g01, g23, g02)
970 case 3:
971 ifftDIT4_gfni_avx512_3(avx512Work, 24, g01, g23, g02)
972 case 4:
973 ifftDIT4_gfni_avx512_4(avx512Work, 24, g01, g23, g02)
974 case 5:
975 ifftDIT4_gfni_avx512_5(avx512Work, 24, g01, g23, g02)
976 case 6:
977 ifftDIT4_gfni_avx512_6(avx512Work, 24, g01, g23, g02)
978 case 7:
979 ifftDIT4_gfni_avx512_7(avx512Work, 24, g01, g23, g02)
980 }
981
982 // Call AVX2 GFNI version twice (processes 64 bytes each)
983 switch variant {
984 case 0:
985 ifftDIT4_gfni_0(avx2Work, 24, g01, g23, g02)
986 case 1:
987 ifftDIT4_gfni_1(avx2Work, 24, g01, g23, g02)
988 case 2:
989 ifftDIT4_gfni_2(avx2Work, 24, g01, g23, g02)
990 case 3:
991 ifftDIT4_gfni_3(avx2Work, 24, g01, g23, g02)
992 case 4:
993 ifftDIT4_gfni_4(avx2Work, 24, g01, g23, g02)
994 case 5:
995 ifftDIT4_gfni_5(avx2Work, 24, g01, g23, g02)
996 case 6:
997 ifftDIT4_gfni_6(avx2Work, 24, g01, g23, g02)
998 case 7:
999 ifftDIT4_gfni_7(avx2Work, 24, g01, g23, g02)
1000 }
1001
1002 // Compare results
1003 match := true
1004 for i := range work {
1005 if !bytes.Equal(avx512Work[i], avx2Work[i]) {
1006 match = false
1007 t.Errorf("work[%d] mismatch for variant %d", i, variant)
1008 for j := 0; j < len(avx512Work[i]) && j < 32; j++ {
1009 if avx512Work[i][j] != avx2Work[i][j] {
1010 t.Logf(" [%d][%d]: AVX512=0x%02x, AVX2=0x%02x", i, j, avx512Work[i][j], avx2Work[i][j])
1011 }
1012 }
1013 }
1014 }
1015 if match {
1016 t.Logf("ifftDIT4_gfni_avx512_%d: MATCH", variant)
1017 }
1018 })
1019 }
1020 }
1021
1022 func TestCompareFftDIT4GF16GFNIAvx512vsAVX2(t *testing.T) {
1023 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX512VL) {
1024 t.Skip("AVX512+GFNI not supported")
1025 }
1026
1027 enc, err := New(300, 100, WithLeopardGF16(true))
1028 if err != nil {
1029 t.Fatal(err)
1030 }
1031 _ = enc
1032
1033 if gf2p811dMulMatrices16 == nil {
1034 t.Skip("GFNI tables not initialized")
1035 }
1036
1037 for variant := range 8 {
1038 t.Run(fmt.Sprintf("variant=%d", variant), func(t *testing.T) {
1039 work := make([][]byte, 4)
1040 for i := range work {
1041 work[i] = make([]byte, 128)
1042 for j := range work[i] {
1043 work[i][j] = byte(i*17 + j + 1)
1044 }
1045 }
1046
1047 avx512Work := make([][]byte, 4)
1048 avx2Work := make([][]byte, 4)
1049 for i := range work {
1050 avx512Work[i] = make([]byte, len(work[i]))
1051 avx2Work[i] = make([]byte, len(work[i]))
1052 copy(avx512Work[i], work[i])
1053 copy(avx2Work[i], work[i])
1054 }
1055
1056 log_m01, log_m23, log_m02 := ffe(10), ffe(200), ffe(3000)
1057 if variant&1 != 0 {
1058 log_m02 = modulus
1059 }
1060 if variant&2 != 0 {
1061 log_m01 = modulus
1062 }
1063 if variant&4 != 0 {
1064 log_m23 = modulus
1065 }
1066
1067 g01 := &gf2p811dMulMatrices16[log_m01]
1068 g23 := &gf2p811dMulMatrices16[log_m23]
1069 g02 := &gf2p811dMulMatrices16[log_m02]
1070
1071 switch variant {
1072 case 0:
1073 fftDIT4_gfni_avx512_0(avx512Work, 24, g01, g23, g02)
1074 fftDIT4_gfni_0(avx2Work, 24, g01, g23, g02)
1075 case 1:
1076 fftDIT4_gfni_avx512_1(avx512Work, 24, g01, g23, g02)
1077 fftDIT4_gfni_1(avx2Work, 24, g01, g23, g02)
1078 case 2:
1079 fftDIT4_gfni_avx512_2(avx512Work, 24, g01, g23, g02)
1080 fftDIT4_gfni_2(avx2Work, 24, g01, g23, g02)
1081 case 3:
1082 fftDIT4_gfni_avx512_3(avx512Work, 24, g01, g23, g02)
1083 fftDIT4_gfni_3(avx2Work, 24, g01, g23, g02)
1084 case 4:
1085 fftDIT4_gfni_avx512_4(avx512Work, 24, g01, g23, g02)
1086 fftDIT4_gfni_4(avx2Work, 24, g01, g23, g02)
1087 case 5:
1088 fftDIT4_gfni_avx512_5(avx512Work, 24, g01, g23, g02)
1089 fftDIT4_gfni_5(avx2Work, 24, g01, g23, g02)
1090 case 6:
1091 fftDIT4_gfni_avx512_6(avx512Work, 24, g01, g23, g02)
1092 fftDIT4_gfni_6(avx2Work, 24, g01, g23, g02)
1093 case 7:
1094 fftDIT4_gfni_avx512_7(avx512Work, 24, g01, g23, g02)
1095 fftDIT4_gfni_7(avx2Work, 24, g01, g23, g02)
1096 }
1097
1098 match := true
1099 for i := range 4 {
1100 if !bytes.Equal(avx512Work[i], avx2Work[i]) {
1101 match = false
1102 t.Errorf("fftDIT4_gfni_avx512_%d: work[%d] mismatch", variant, i)
1103 for j := 0; j < len(avx512Work[i]) && j < 32; j++ {
1104 if avx512Work[i][j] != avx2Work[i][j] {
1105 t.Logf(" [%d][%d]: AVX512=0x%02x, AVX2=0x%02x", i, j, avx512Work[i][j], avx2Work[i][j])
1106 }
1107 }
1108 }
1109 }
1110 if match {
1111 t.Logf("fftDIT4_gfni_avx512_%d: MATCH", variant)
1112 }
1113 })
1114 }
1115 }
1116
1117 // TestGF16MulSimple tests a simple case of GFNI multiplication to debug
1118 func TestGF16MulSimple(t *testing.T) {
1119 if !cpuid.CPU.Has(cpuid.GFNI) {
1120 t.Skip("GFNI not supported")
1121 }
1122
1123 enc, err := New(300, 100, WithLeopardGF16(true))
1124 if err != nil {
1125 t.Fatal(err)
1126 }
1127 _ = enc
1128
1129 if gf2p811dMulMatrices16 == nil {
1130 t.Skip("GFNI tables not initialized")
1131 }
1132
1133 // Test with identity (logM=0)
1134 // For identity: mul(x, 0) = x (since exp(log(x) + 0) = x)
1135 logM := ffe(0)
1136
1137 // Simple test: process 64 bytes (32 GF16 elements)
1138 // Split layout: [0:32] = lo bytes, [32:64] = hi bytes
1139 y := make([]byte, 64)
1140 for i := range 32 {
1141 y[i] = byte(i + 1) // lo bytes in [0:32]
1142 y[32+i] = byte(0) // hi bytes in [32:64]
1143 }
1144 x := make([]byte, 64)
1145
1146 // Expected: y = x XOR y = y (since x=0)
1147 // Expected: x = x XOR mul(y, 0) = mul(y, 0) = y
1148 xGFNI := make([]byte, 64)
1149 yGFNI := make([]byte, 64)
1150 copy(xGFNI, x)
1151 copy(yGFNI, y)
1152
1153 gfniTable := &gf2p811dMulMatrices16[logM]
1154 ifftDIT2_gfni(xGFNI, yGFNI, gfniTable)
1155
1156 // Check yGFNI should equal original y (since x was 0)
1157 for i := range 64 {
1158 if yGFNI[i] != y[i] {
1159 t.Errorf("y[%d]: got 0x%02x, expected 0x%02x", i, yGFNI[i], y[i])
1160 }
1161 }
1162
1163 // Check xGFNI should equal original y (x = 0 XOR mul(y, identity) = y)
1164 for i := range 64 {
1165 if xGFNI[i] != y[i] {
1166 t.Errorf("x[%d]: got 0x%02x, expected 0x%02x (y[%d])", i, xGFNI[i], y[i], i)
1167 }
1168 }
1169 }
1170
1171 // TestGF16MulNonIdentity tests GFNI assembly for non-identity logM using split layout
1172 func TestGF16MulNonIdentity(t *testing.T) {
1173 if !cpuid.CPU.Has(cpuid.GFNI) {
1174 t.Skip("GFNI not supported")
1175 }
1176
1177 enc, err := New(300, 100, WithLeopardGF16(true))
1178 if err != nil {
1179 t.Fatal(err)
1180 }
1181 _ = enc
1182
1183 if gf2p811dMulMatrices16 == nil {
1184 t.Skip("GFNI tables not initialized")
1185 }
1186
1187 logM := ffe(100)
1188
1189 // Data layout is SPLIT: [0:32] = lo bytes, [32:64] = hi bytes of 32 elements
1190 // Set y to element 1 in first position (split layout)
1191 y := make([]byte, 64)
1192 y[0] = 1 // lo byte of element 0
1193 y[32] = 0 // hi byte of element 0
1194
1195 // Set x to zeros
1196 x := make([]byte, 64)
1197
1198 // Expected: after ifftDIT2:
1199 // y_new = x XOR y = y (since x=0)
1200 // x_new = x XOR mul(y, logM) = mul(y, logM) = mul(element1, logM)
1201 expectedMul := mulLog(ffe(1), logM)
1202 t.Logf("mulLog(1, %d) = 0x%04x", logM, expectedMul)
1203
1204 xGFNI := make([]byte, 64)
1205 yGFNI := make([]byte, 64)
1206 copy(xGFNI, x)
1207 copy(yGFNI, y)
1208
1209 gfniTable := &gf2p811dMulMatrices16[logM]
1210 t.Logf("Matrices: A=0x%016x B=0x%016x C=0x%016x D=0x%016x",
1211 gfniTable[0], gfniTable[1], gfniTable[2], gfniTable[3])
1212
1213 ifftDIT2_gfni(xGFNI, yGFNI, gfniTable)
1214
1215 // x should have mul(1, logM) at element 0 (split layout)
1216 gotResult := ffe(xGFNI[0]) | (ffe(xGFNI[32]) << 8)
1217 t.Logf("GFNI result: 0x%04x (expected 0x%04x)", gotResult, expectedMul)
1218 t.Logf("xGFNI lo bytes: %02x, hi bytes: %02x", xGFNI[0], xGFNI[32])
1219
1220 if gotResult != expectedMul {
1221 t.Errorf("GFNI mul mismatch: got 0x%04x, expected 0x%04x", gotResult, expectedMul)
1222 }
1223
1224 // Test all 32 elements with split layout
1225 t.Log("Testing all 32 elements (split layout)...")
1226 for i := range y {
1227 y[i] = 0
1228 }
1229 // Set elements 0-31 to 1, 2, 3, ..., 32 in split layout
1230 for i := range 32 {
1231 y[i] = byte(i + 1) // lo bytes
1232 y[32+i] = 0 // hi bytes
1233 }
1234
1235 for i := range x {
1236 x[i] = 0
1237 }
1238 copy(xGFNI, x)
1239 copy(yGFNI, y)
1240 ifftDIT2_gfni(xGFNI, yGFNI, gfniTable)
1241
1242 // Check each element (split layout)
1243 errors := 0
1244 for i := range 32 {
1245 elem := ffe(i + 1)
1246 expected := mulLog(elem, logM)
1247 got := ffe(xGFNI[i]) | (ffe(xGFNI[32+i]) << 8)
1248 if got != expected {
1249 t.Errorf("Element %d: mul(%d, %d) got 0x%04x, expected 0x%04x", i, elem, logM, got, expected)
1250 errors++
1251 }
1252 if errors > 10 {
1253 break
1254 }
1255 }
1256 }
1257
1258 // TestGF16MatrixGeneration tests that the GFNI matrices produce correct results
1259 // when applied in pure Go (without assembly).
1260 func TestGF16MatrixGeneration(t *testing.T) {
1261 if !cpuid.CPU.Has(cpuid.GFNI) {
1262 t.Skip("GFNI not supported")
1263 }
1264
1265 // Initialize tables
1266 enc, err := New(300, 100, WithLeopardGF16(true))
1267 if err != nil {
1268 t.Fatal(err)
1269 }
1270 _ = enc
1271
1272 if gf2p811dMulMatrices16 == nil {
1273 t.Skip("GFNI tables not initialized")
1274 }
1275
1276 // applyGFNIMatrix applies one 8x8 GFNI matrix to a byte
1277 // VGF2P8AFFINEQB: for output bit j, uses matrix byte (7-j), XORs bits where matrix[k] AND input[k]
1278 applyMatrix := func(matrix uint64, input byte) byte {
1279 var result byte
1280 for outBit := range 8 {
1281 parity := byte(0)
1282 for inBit := range 8 {
1283 // VGF2P8AFFINEQB uses byte (7-outBit), bit inBit
1284 matrixBit := (matrix >> ((7-outBit)*8 + inBit)) & 1
1285 inputBit := (input >> inBit) & 1
1286 parity ^= byte(matrixBit & uint64(inputBit))
1287 }
1288 result |= parity << outBit
1289 }
1290 return result
1291 }
1292
1293 // Test a few log values
1294 testLogMs := []int{0, 1, 100, 1000, 10000, 30000, 65535}
1295 for _, logM := range testLogMs {
1296 t.Run(fmt.Sprintf("logM=%d", logM), func(t *testing.T) {
1297 matrices := gf2p811dMulMatrices16[logM]
1298 A, B, C, D := matrices[0], matrices[1], matrices[2], matrices[3]
1299
1300 // Test sample 16-bit inputs
1301 testInputs := []ffe{0, 1, 2, 255, 256, 0x1234, 0xFFFF}
1302 for _, input := range testInputs {
1303 // Expected result from mulLog
1304 expected := mulLog(input, ffe(logM))
1305
1306 // Apply GFNI matrices
1307 inLo := byte(input)
1308 inHi := byte(input >> 8)
1309 outLo := applyMatrix(A, inLo) ^ applyMatrix(B, inHi)
1310 outHi := applyMatrix(C, inLo) ^ applyMatrix(D, inHi)
1311 gfniResult := ffe(outLo) | (ffe(outHi) << 8)
1312
1313 if gfniResult != expected {
1314 t.Errorf("input=0x%04x logM=%d: GFNI=0x%04x expected=0x%04x",
1315 input, logM, gfniResult, expected)
1316 }
1317 }
1318 })
1319 }
1320 }
1321
1322 func TestCompareMulgf16GFNIvsAVX2(t *testing.T) {
1323 if !cpuid.CPU.Supports(cpuid.GFNI, cpuid.AVX) {
1324 t.Skip("AVX+GFNI not supported")
1325 }
1326
1327 enc, err := New(300, 100, WithLeopardGF16(true))
1328 if err != nil {
1329 t.Fatal(err)
1330 }
1331 _ = enc
1332
1333 if gf2p811dMulMatrices16 == nil {
1334 t.Skip("GFNI tables not initialized")
1335 }
1336
1337 testCases := []ffe{0, 1, 100, 1000, 10000, 30000, 65535}
1338
1339 for _, logM := range testCases {
1340 t.Run(fmt.Sprintf("logM=%d", logM), func(t *testing.T) {
1341 y := make([]byte, 256)
1342 for i := range y {
1343 y[i] = byte(i*3 + 7)
1344 }
1345
1346 xAVX2 := make([]byte, len(y))
1347 xGFNI := make([]byte, len(y))
1348
1349 avx2Table := &multiply256LUT[logM]
1350 gfniTable := &gf2p811dMulMatrices16[logM]
1351
1352 mulgf16_avx2(xAVX2, y, avx2Table)
1353 mulgf16_gfni(xGFNI, y, gfniTable)
1354
1355 if !bytes.Equal(xGFNI, xAVX2) {
1356 t.Errorf("mulgf16_gfni mismatch for logM=%d", logM)
1357 for i := range xAVX2 {
1358 if xGFNI[i] != xAVX2[i] {
1359 t.Errorf(" byte %d: GFNI=0x%02x AVX2=0x%02x", i, xGFNI[i], xAVX2[i])
1360 if i > 10 {
1361 break
1362 }
1363 }
1364 }
1365 }
1366 })
1367 }
1368 }