PHP code example of guillaumetissier / galois-fields
1. Go to this page and download the library: Download guillaumetissier/galois-fields library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
guillaumetissier / galois-fields example snippets
use Guillaumetissier\GaloisFields\GaloisField;
// Create GF(256) - used in QR codes
$gf256 = new GaloisField(256);
// Basic operations
$sum = $gf256->add(123, 45); // Addition (XOR for binary fields)
$product = $gf256->multiply(53, 78); // Multiplication
$quotient = $gf256->divide(200, 17); // Division
$inverse = $gf256->inverse(123); // Multiplicative inverse
// Check field properties
echo $gf256->getOrder(); // 256
echo $gf256->getCharacteristic(); // 2
echo $gf256->getDegree(); // 8
// Create GF(7)
$gf7 = new GaloisField(7);
// All operations work modulo 7
echo $gf7->add(5, 3); // 1 (8 mod 7)
echo $gf7->multiply(4, 2); // 1 (8 mod 7)
echo $gf7->power(3, 2); // 2 (9 mod 7)
// Every non-zero element has an inverse
echo $gf7->multiply(3, $gf7->inverse(3)); // 1
$gf = new GaloisField(256);
// Reed-Solomon error correction uses GF(256)
// Polynomial evaluation in GF(256)
function evaluatePolynomial(GaloisField $gf, array $coefficients, int $x): int {
$result = 0;
foreach ($coefficients as $coeff) {
$result = $gf->add($gf->multiply($result, $x), $coeff);
}
return $result;
}
$polynomial = [1, 2, 3]; // 1*x^2 + 2*x + 3
$value = evaluatePolynomial($gf, $polynomial, 5);
// Small fields
$gf4 = new GaloisField(4); // GF(2^2)
$gf8 = new GaloisField(8); // GF(2^3)
$gf16 = new GaloisField(16); // GF(2^4)
// Medium fields
$gf256 = new GaloisField(256); // GF(2^8) - QR codes
$gf65536 = new GaloisField(65536); // GF(2^16) -
$gf = new GaloisField(256);
// Get detailed information
$info = $gf->getInfo();
print_r($info);
// Output:
// [
// 'order' => 256,
// 'characteristic' => 2,
// 'degree' => 8,
// 'notation' => 'GF(2^8)'
// ]
// Validate elements
$gf->isValidElement(123); // true
$gf->isValidElement(256); // false (out of range)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.