encode($dataCodewords, 10); echo "EC codewords (10):\n"; echo implode(', ', $ecCodewords) . "\n\n"; // Verify Reed-Solomon encoding // For Reed-Solomon, if we encode data and then decode, we should get the original data back // But we can also verify by checking if the polynomial division was correct echo "=== Reed-Solomon Verification ===\n"; echo "For RS(n, k), where n = total codewords and k = data codewords:\n"; echo " n = 26 (total codewords)\n"; echo " k = 16 (data codewords)\n"; echo " t = 10 (EC codewords)\n"; echo " Can correct up to t/2 = 5 errors\n\n"; // Test: Create a message polynomial and verify encoding echo "Message polynomial (data + zeros for EC):\n"; $messagePoly = array_merge($dataCodewords, array_fill(0, 10, 0)); echo " " . implode(', ', $messagePoly) . "\n\n"; // Get generator polynomial $reflection = new ReflectionClass($reedSolomon); $getGeneratorMethod = $reflection->getMethod('getGeneratorPolynomial'); $getGeneratorMethod->setAccessible(true); $generator = $getGeneratorMethod->invoke($reedSolomon, 10); echo "Generator polynomial (degree 10):\n"; echo " " . implode(', ', $generator) . "\n\n"; // Verify: The generator polynomial should have 11 coefficients (degree 10 + 1) if (count($generator) === 11) { echo "✅ Generator polynomial has correct degree (10)\n"; } else { echo "❌ Generator polynomial has wrong degree: " . (count($generator) - 1) . " (expected: 10)\n"; } // Test GF field operations echo "\n=== GF(256) Field Operations Test ===\n"; $gfMultiplyMethod = $reflection->getMethod('gfMultiply'); $gfMultiplyMethod->setAccessible(true); // Test some multiplications $testCases = [ [0, 5, 0], // 0 * anything = 0 [1, 5, 5], // 1 * x = x [2, 3, 6], // 2 * 3 = 6 [87, 1, 87], // Generator coefficient [251, 1, 251], // Generator coefficient ]; $allCorrect = true; foreach ($testCases as [$a, $b, $expected]) { $result = $gfMultiplyMethod->invoke($reedSolomon, $a, $b); $match = $result === $expected ? '✅' : '❌'; if ($result !== $expected) { $allCorrect = false; } echo " {$a} * {$b} = {$result} (expected: {$expected}) {$match}\n"; } if ($allCorrect) { echo "\n✅ GF multiplication is correct\n"; } else { echo "\n❌ GF multiplication has errors\n"; } // Test generator polynomial coefficients echo "\n=== Generator Polynomial Coefficients ===\n"; echo "Expected for 10 EC codewords (from QR spec):\n"; echo " [0, 251, 67, 46, 61, 118, 70, 64, 94, 32, 45]\n"; echo "Actual:\n"; echo " [" . implode(', ', $generator) . "]\n"; if ($generator === [0, 251, 67, 46, 61, 118, 70, 64, 94, 32, 45]) { echo "\n✅ Generator polynomial matches specification!\n"; } else { echo "\n❌ Generator polynomial doesn't match specification!\n"; echo "Differences:\n"; $expected = [0, 251, 67, 46, 61, 118, 70, 64, 94, 32, 45]; for ($i = 0; $i < min(count($generator), count($expected)); $i++) { if ($generator[$i] !== $expected[$i]) { echo " Coefficient {$i}: got {$generator[$i]}, expected {$expected[$i]}\n"; } } } // Final check: Can we decode the encoded data? echo "\n=== Decode Test ===\n"; echo "Note: Full decode test requires error correction decoder.\n"; echo "For now, we verify that EC codewords are generated correctly.\n"; echo "If EC codewords are wrong, QR scanners will fail the error correction check.\n";