You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericGF.cs 5.3 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ZXing.Common.ReedSolomon
  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. public sealed class GenericGF
  28. {
  29. public static GenericGF QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
  30. private int[] expTable;
  31. private int[] logTable;
  32. private GenericGFPoly zero;
  33. private GenericGFPoly one;
  34. private readonly int size;
  35. private readonly int primitive;
  36. private readonly int generatorBase;
  37. /// <summary>
  38. /// Create a representation of GF(size) using the given primitive polynomial.
  39. /// </summary>
  40. /// <param name="primitive">irreducible polynomial whose coefficients are represented by
  41. /// * the bits of an int, where the least-significant bit represents the constant
  42. /// * coefficient</param>
  43. /// <param name="size">the size of the field</param>
  44. /// <param name="genBase">the factor b in the generator polynomial can be 0- or 1-based
  45. /// * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
  46. /// * In most cases it should be 1, but for QR code it is 0.</param>
  47. public GenericGF(int primitive, int size, int genBase)
  48. {
  49. this.primitive = primitive;
  50. this.size = size;
  51. this.generatorBase = genBase;
  52. expTable = new int[size];
  53. logTable = new int[size];
  54. int x = 1;
  55. for (int i = 0; i < size; i++)
  56. {
  57. expTable[i] = x;
  58. x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
  59. if (x >= size)
  60. {
  61. x ^= primitive;
  62. x &= size - 1;
  63. }
  64. }
  65. for (int i = 0; i < size - 1; i++)
  66. {
  67. logTable[expTable[i]] = i;
  68. }
  69. // logTable[0] == 0 but this should never be used
  70. zero = new GenericGFPoly(this, new int[] { 0 });
  71. one = new GenericGFPoly(this, new int[] { 1 });
  72. }
  73. internal GenericGFPoly Zero
  74. {
  75. get
  76. {
  77. return zero;
  78. }
  79. }
  80. /// <summary>
  81. /// Builds the monomial.
  82. /// </summary>
  83. /// <param name="degree">The degree.</param>
  84. /// <param name="coefficient">The coefficient.</param>
  85. /// <returns>the monomial representing coefficient * x^degree</returns>
  86. internal GenericGFPoly buildMonomial(int degree, int coefficient)
  87. {
  88. if (degree < 0)
  89. {
  90. throw new ArgumentException();
  91. }
  92. if (coefficient == 0)
  93. {
  94. return zero;
  95. }
  96. int[] coefficients = new int[degree + 1];
  97. coefficients[0] = coefficient;
  98. return new GenericGFPoly(this, coefficients);
  99. }
  100. /// <summary>
  101. /// Implements both addition and subtraction -- they are the same in GF(size).
  102. /// </summary>
  103. /// <returns>sum/difference of a and b</returns>
  104. static internal int addOrSubtract(int a, int b)
  105. {
  106. return a ^ b;
  107. }
  108. /// <summary>
  109. /// Exps the specified a.
  110. /// </summary>
  111. /// <returns>2 to the power of a in GF(size)</returns>
  112. internal int exp(int a)
  113. {
  114. return expTable[a];
  115. }
  116. /// <summary>
  117. /// Inverses the specified a.
  118. /// </summary>
  119. /// <returns>multiplicative inverse of a</returns>
  120. internal int inverse(int a)
  121. {
  122. if (a == 0)
  123. {
  124. throw new ArithmeticException();
  125. }
  126. return expTable[size - logTable[a] - 1];
  127. }
  128. /// <summary>
  129. /// Multiplies the specified a with b.
  130. /// </summary>
  131. /// <param name="a">A.</param>
  132. /// <param name="b">The b.</param>
  133. /// <returns>product of a and b in GF(size)</returns>
  134. internal int multiply(int a, int b)
  135. {
  136. if (a == 0 || b == 0)
  137. {
  138. return 0;
  139. }
  140. return expTable[(logTable[a] + logTable[b]) % (size - 1)];
  141. }
  142. /// <summary>
  143. /// Gets the generator base.
  144. /// </summary>
  145. public int GeneratorBase
  146. {
  147. get { return generatorBase; }
  148. }
  149. }
  150. }