GenericGF.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. namespace FastReport.Barcode.Aztec
  18. {
  19. /// <summary>
  20. /// <p>This class contains utility methods for performing mathematical operations over
  21. /// the Galois Fields. Operations use a given primitive polynomial in calculations.</p>
  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. /// </summary>
  26. /// <author>Sean Owen</author>
  27. internal sealed class GenericGF
  28. {
  29. public static GenericGF AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
  30. public static GenericGF AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
  31. public static GenericGF AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
  32. public static GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
  33. public static GenericGF QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  34. public static GenericGF DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
  35. public static GenericGF AZTEC_DATA_8 = DATA_MATRIX_FIELD_256;
  36. public static GenericGF MAXICODE_FIELD_64 = AZTEC_DATA_6;
  37. private int[] expTable;
  38. private int[] logTable;
  39. private GenericGFPoly zero;
  40. private GenericGFPoly one;
  41. private readonly int size;
  42. private readonly int primitive;
  43. private readonly int generatorBase;
  44. /// <summary>
  45. /// Create a representation of GF(size) using the given primitive polynomial.
  46. /// </summary>
  47. /// <param name="primitive">irreducible polynomial whose coefficients are represented by
  48. /// * the bits of an int, where the least-significant bit represents the constant
  49. /// * coefficient</param>
  50. /// <param name="size">the size of the field</param>
  51. /// <param name="genBase">the factor b in the generator polynomial can be 0- or 1-based
  52. /// * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
  53. /// * In most cases it should be 1, but for QR code it is 0.</param>
  54. public GenericGF(int primitive, int size, int genBase)
  55. {
  56. this.primitive = primitive;
  57. this.size = size;
  58. this.generatorBase = genBase;
  59. expTable = new int[size];
  60. logTable = new int[size];
  61. int x = 1;
  62. for (int i = 0; i < size; i++)
  63. {
  64. expTable[i] = x;
  65. x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
  66. if (x >= size)
  67. {
  68. x ^= primitive;
  69. x &= size - 1;
  70. }
  71. }
  72. for (int i = 0; i < size - 1; i++)
  73. {
  74. logTable[expTable[i]] = i;
  75. }
  76. // logTable[0] == 0 but this should never be used
  77. zero = new GenericGFPoly(this, new int[] { 0 });
  78. one = new GenericGFPoly(this, new int[] { 1 });
  79. }
  80. internal GenericGFPoly Zero
  81. {
  82. get
  83. {
  84. return zero;
  85. }
  86. }
  87. internal GenericGFPoly One
  88. {
  89. get
  90. {
  91. return one;
  92. }
  93. }
  94. /// <summary>
  95. /// Builds the monomial.
  96. /// </summary>
  97. /// <param name="degree">The degree.</param>
  98. /// <param name="coefficient">The coefficient.</param>
  99. /// <returns>the monomial representing coefficient * x^degree</returns>
  100. internal GenericGFPoly buildMonomial(int degree, int coefficient)
  101. {
  102. if (degree < 0)
  103. {
  104. throw new ArgumentException();
  105. }
  106. if (coefficient == 0)
  107. {
  108. return zero;
  109. }
  110. int[] coefficients = new int[degree + 1];
  111. coefficients[0] = coefficient;
  112. return new GenericGFPoly(this, coefficients);
  113. }
  114. /// <summary>
  115. /// Implements both addition and subtraction -- they are the same in GF(size).
  116. /// </summary>
  117. /// <returns>sum/difference of a and b</returns>
  118. static internal int addOrSubtract(int a, int b)
  119. {
  120. return a ^ b;
  121. }
  122. /// <summary>
  123. /// Exps the specified a.
  124. /// </summary>
  125. /// <returns>2 to the power of a in GF(size)</returns>
  126. internal int exp(int a)
  127. {
  128. return expTable[a];
  129. }
  130. /// <summary>
  131. /// Logs the specified a.
  132. /// </summary>
  133. /// <param name="a">A.</param>
  134. /// <returns>base 2 log of a in GF(size)</returns>
  135. internal int log(int a)
  136. {
  137. if (a == 0)
  138. {
  139. throw new ArgumentException();
  140. }
  141. return logTable[a];
  142. }
  143. /// <summary>
  144. /// Inverses the specified a.
  145. /// </summary>
  146. /// <returns>multiplicative inverse of a</returns>
  147. internal int inverse(int a)
  148. {
  149. if (a == 0)
  150. {
  151. throw new ArithmeticException();
  152. }
  153. return expTable[size - logTable[a] - 1];
  154. }
  155. /// <summary>
  156. /// Multiplies the specified a with b.
  157. /// </summary>
  158. /// <param name="a">A.</param>
  159. /// <param name="b">The b.</param>
  160. /// <returns>product of a and b in GF(size)</returns>
  161. internal int multiply(int a, int b)
  162. {
  163. if (a == 0 || b == 0)
  164. {
  165. return 0;
  166. }
  167. return expTable[(logTable[a] + logTable[b]) % (size - 1)];
  168. }
  169. /// <summary>
  170. /// Gets the size.
  171. /// </summary>
  172. public int Size
  173. {
  174. get { return size; }
  175. }
  176. /// <summary>
  177. /// Gets the generator base.
  178. /// </summary>
  179. public int GeneratorBase
  180. {
  181. get { return generatorBase; }
  182. }
  183. /// <summary>
  184. /// Returns a <see cref="System.String"/> that represents this instance.
  185. /// </summary>
  186. /// <returns>
  187. /// A <see cref="System.String"/> that represents this instance.
  188. /// </returns>
  189. override public String ToString()
  190. {
  191. return "GF(0x" + primitive.ToString("X") + ',' + size + ')';
  192. }
  193. }
  194. }