GenericGF.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Common\Reedsolomon;
  18. /**
  19. * <p>This class contains utility methods for performing mathematical operations over
  20. * the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
  21. *
  22. * <p>Throughout this package, elements of the GF are represented as an {@code int}
  23. * for convenience and speed (but at the cost of memory).
  24. * </p>
  25. *
  26. * @author Sean Owen
  27. * @author David Olivier
  28. */
  29. final class GenericGF
  30. {
  31. public static $AZTEC_DATA_12;
  32. public static $AZTEC_DATA_10;
  33. public static $AZTEC_DATA_6;
  34. public static $AZTEC_PARAM;
  35. public static $QR_CODE_FIELD_256;
  36. public static $DATA_MATRIX_FIELD_256;
  37. public static $AZTEC_DATA_8;
  38. public static $MAXICODE_FIELD_64;
  39. private array $expTable = [];
  40. private array $logTable = [];
  41. private readonly \Zxing\Common\Reedsolomon\GenericGFPoly $zero;
  42. private readonly \Zxing\Common\Reedsolomon\GenericGFPoly $one;
  43. /**
  44. * Create a representation of GF(size) using the given primitive polynomial.
  45. *
  46. * @param irreducible $primitive polynomial whose coefficients are represented by
  47. * the bits of an int, where the least-significant bit represents the constant
  48. * coefficient
  49. * @param the $size size of the field
  50. * @param the $generatorBase factor b in the generator polynomial can be 0- or 1-based
  51. (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
  52. In most cases it should be 1, but for QR code it is 0.
  53. */
  54. public function __construct(private $primitive, private $size, private $generatorBase)
  55. {
  56. $x = 1;
  57. for ($i = 0; $i < $size; $i++) {
  58. $this->expTable[$i] = $x;
  59. $x *= 2; // we're assuming the generator alpha is 2
  60. if ($x >= $size) {
  61. $x ^= $primitive;
  62. $x &= $size - 1;
  63. }
  64. }
  65. for ($i = 0; $i < $size - 1; $i++) {
  66. $this->logTable[$this->expTable[$i]] = $i;
  67. }
  68. // logTable[0] == 0 but this should never be used
  69. $this->zero = new GenericGFPoly($this, [0]);
  70. $this->one = new GenericGFPoly($this, [1]);
  71. }
  72. public static function Init(): void
  73. {
  74. self::$AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
  75. self::$AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
  76. self::$AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
  77. self::$AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
  78. self::$QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  79. self::$DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
  80. self::$AZTEC_DATA_8 = self::$DATA_MATRIX_FIELD_256;
  81. self::$MAXICODE_FIELD_64 = self::$AZTEC_DATA_6;
  82. }
  83. /**
  84. * Implements both addition and subtraction -- they are the same in GF(size).
  85. *
  86. * @return sum/difference of a and b
  87. */
  88. public static function addOrSubtract($a, $b)
  89. {
  90. return $a ^ $b;
  91. }
  92. public function getZero()
  93. {
  94. return $this->zero;
  95. }
  96. public function getOne()
  97. {
  98. return $this->one;
  99. }
  100. /**
  101. * @return GenericGFPoly the monomial representing coefficient * x^degree
  102. */
  103. public function buildMonomial($degree, $coefficient)
  104. {
  105. if ($degree < 0) {
  106. throw new \InvalidArgumentException();
  107. }
  108. if ($coefficient == 0) {
  109. return $this->zero;
  110. }
  111. $coefficients = fill_array(0, $degree + 1, 0);//new int[degree + 1];
  112. $coefficients[0] = $coefficient;
  113. return new GenericGFPoly($this, $coefficients);
  114. }
  115. /**
  116. * @return 2 to the power of a in GF(size)
  117. */
  118. public function exp($a)
  119. {
  120. return $this->expTable[$a];
  121. }
  122. /**
  123. * @return base 2 log of a in GF(size)
  124. */
  125. public function log($a)
  126. {
  127. if ($a == 0) {
  128. throw new \InvalidArgumentException();
  129. }
  130. return $this->logTable[$a];
  131. }
  132. /**
  133. * @return multiplicative inverse of a
  134. */
  135. public function inverse($a)
  136. {
  137. if ($a == 0) {
  138. throw new \Exception();
  139. }
  140. return $this->expTable[$this->size - $this->logTable[$a] - 1];
  141. }
  142. /**
  143. * @return int product of a and b in GF(size)
  144. */
  145. public function multiply($a, $b)
  146. {
  147. if ($a == 0 || $b == 0) {
  148. return 0;
  149. }
  150. return $this->expTable[($this->logTable[$a] + $this->logTable[$b]) % ($this->size - 1)];
  151. }
  152. public function getSize()
  153. {
  154. return $this->size;
  155. }
  156. public function getGeneratorBase()
  157. {
  158. return $this->generatorBase;
  159. }
  160. // @Override
  161. public function toString()
  162. {
  163. return "GF(0x" . dechex((int)($this->primitive)) . ',' . $this->size . ')';
  164. }
  165. }
  166. GenericGF::Init();