GenericGF.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 $expTable;
  40. private $logTable;
  41. private $zero;
  42. private $one;
  43. private $size;
  44. private $primitive;
  45. private $generatorBase;
  46. /**
  47. * Create a representation of GF(size) using the given primitive polynomial.
  48. *
  49. * @param primitive irreducible polynomial whose coefficients are represented by
  50. * the bits of an int, where the least-significant bit represents the constant
  51. * coefficient
  52. * @param size the size of the field
  53. * @param b the factor b in the generator polynomial can be 0- or 1-based
  54. * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
  55. * In most cases it should be 1, but for QR code it is 0.
  56. */
  57. public function __construct($primitive, $size, $b)
  58. {
  59. $this->primitive = $primitive;
  60. $this->size = $size;
  61. $this->generatorBase = $b;
  62. $this->expTable = [];
  63. $this->logTable = [];
  64. $x = 1;
  65. for ($i = 0; $i < $size; $i++) {
  66. $this->expTable[$i] = $x;
  67. $x *= 2; // we're assuming the generator alpha is 2
  68. if ($x >= $size) {
  69. $x ^= $primitive;
  70. $x &= $size - 1;
  71. }
  72. }
  73. for ($i = 0; $i < $size - 1; $i++) {
  74. $this->logTable[$this->expTable[$i]] = $i;
  75. }
  76. // logTable[0] == 0 but this should never be used
  77. $this->zero = new GenericGFPoly($this, [0]);
  78. $this->one = new GenericGFPoly($this, [1]);
  79. }
  80. public static function Init()
  81. {
  82. self::$AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
  83. self::$AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
  84. self::$AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
  85. self::$AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
  86. self::$QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  87. self::$DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
  88. self::$AZTEC_DATA_8 = self::$DATA_MATRIX_FIELD_256;
  89. self::$MAXICODE_FIELD_64 = self::$AZTEC_DATA_6;
  90. }
  91. /**
  92. * Implements both addition and subtraction -- they are the same in GF(size).
  93. *
  94. * @return sum/difference of a and b
  95. */
  96. public static function addOrSubtract($a, $b)
  97. {
  98. return $a ^ $b;
  99. }
  100. public function getZero()
  101. {
  102. return $this->zero;
  103. }
  104. public function getOne()
  105. {
  106. return $this->one;
  107. }
  108. /**
  109. * @return the monomial representing coefficient * x^degree
  110. */
  111. public function buildMonomial($degree, $coefficient)
  112. {
  113. if ($degree < 0) {
  114. throw new \InvalidArgumentException();
  115. }
  116. if ($coefficient == 0) {
  117. return $this->zero;
  118. }
  119. $coefficients = fill_array(0, $degree + 1, 0);//new int[degree + 1];
  120. $coefficients[0] = $coefficient;
  121. return new GenericGFPoly($this, $coefficients);
  122. }
  123. /**
  124. * @return 2 to the power of a in GF(size)
  125. */
  126. public function exp($a)
  127. {
  128. return $this->expTable[$a];
  129. }
  130. /**
  131. * @return base 2 log of a in GF(size)
  132. */
  133. public function log($a)
  134. {
  135. if ($a == 0) {
  136. throw new \InvalidArgumentException();
  137. }
  138. return $this->logTable[$a];
  139. }
  140. /**
  141. * @return multiplicative inverse of a
  142. */
  143. public function inverse($a)
  144. {
  145. if ($a == 0) {
  146. throw new \Exception();
  147. }
  148. return $this->expTable[$this->size - $this->logTable[$a] - 1];
  149. }
  150. /**
  151. * @return int product of a and b in GF(size)
  152. */
  153. public function multiply($a, $b)
  154. {
  155. if ($a == 0 || $b == 0) {
  156. return 0;
  157. }
  158. return $this->expTable[($this->logTable[$a] + $this->logTable[$b]) % ($this->size - 1)];
  159. }
  160. public function getSize()
  161. {
  162. return $this->size;
  163. }
  164. public function getGeneratorBase()
  165. {
  166. return $this->generatorBase;
  167. }
  168. // @Override
  169. public function toString()
  170. {
  171. return "GF(0x" . dechex((int)($this->primitive)) . ',' . $this->size . ')';
  172. }
  173. }
  174. GenericGF::Init();