2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.common.reedsolomon;
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>
23 * <p>Throughout this package, elements of the GF are represented as an {@code int}
24 * for convenience and speed (but at the cost of memory).
28 * @author David Olivier
30 public final class GenericGF {
32 public static final GenericGF AZTEC_DATA_12 = new GenericGF(0x1069, 4096, 1); // x^12 + x^6 + x^5 + x^3 + 1
33 public static final GenericGF AZTEC_DATA_10 = new GenericGF(0x409, 1024, 1); // x^10 + x^3 + 1
34 public static final GenericGF AZTEC_DATA_6 = new GenericGF(0x43, 64, 1); // x^6 + x + 1
35 public static final GenericGF AZTEC_PARAM = new GenericGF(0x13, 16, 1); // x^4 + x + 1
36 public static final GenericGF QR_CODE_FIELD_256 = new GenericGF(0x011D, 256, 0); // x^8 + x^4 + x^3 + x^2 + 1
37 public static final GenericGF DATA_MATRIX_FIELD_256 = new GenericGF(0x012D, 256, 1); // x^8 + x^5 + x^3 + x^2 + 1
38 public static final GenericGF AZTEC_DATA_8 = DATA_MATRIX_FIELD_256;
39 public static final GenericGF MAXICODE_FIELD_64 = AZTEC_DATA_6;
41 private static final int INITIALIZATION_THRESHOLD = 0;
43 private int[] expTable;
44 private int[] logTable;
45 private GenericGFPoly zero;
46 private GenericGFPoly one;
47 private final int size;
48 private final int primitive;
49 private final int generatorBase;
50 private boolean initialized = false;
53 * Create a representation of GF(size) using the given primitive polynomial.
55 * @param primitive irreducible polynomial whose coefficients are represented by
56 * the bits of an int, where the least-significant bit represents the constant
58 * @param size the size of the field
59 * @param b the factor b in the generator polynomial can be 0- or 1-based
60 * (g(x) = (x+a^b)(x+a^(b+1))...(x+a^(b+2t-1))).
61 * In most cases it should be 1, but for QR code it is 0.
63 public GenericGF(int primitive, int size, int b) {
64 this.primitive = primitive;
66 this.generatorBase = b;
68 if (size <= INITIALIZATION_THRESHOLD) {
73 private void initialize() {
74 expTable = new int[size];
75 logTable = new int[size];
77 for (int i = 0; i < size; i++) {
79 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
85 for (int i = 0; i < size-1; i++) {
86 logTable[expTable[i]] = i;
88 // logTable[0] == 0 but this should never be used
89 zero = new GenericGFPoly(this, new int[]{0});
90 one = new GenericGFPoly(this, new int[]{1});
94 private void checkInit() {
100 GenericGFPoly getZero() {
106 GenericGFPoly getOne() {
113 * @return the monomial representing coefficient * x^degree
115 GenericGFPoly buildMonomial(int degree, int coefficient) {
119 throw new IllegalArgumentException();
121 if (coefficient == 0) {
124 int[] coefficients = new int[degree + 1];
125 coefficients[0] = coefficient;
126 return new GenericGFPoly(this, coefficients);
130 * Implements both addition and subtraction -- they are the same in GF(size).
132 * @return sum/difference of a and b
134 static int addOrSubtract(int a, int b) {
139 * @return 2 to the power of a in GF(size)
148 * @return base 2 log of a in GF(size)
154 throw new IllegalArgumentException();
160 * @return multiplicative inverse of a
166 throw new ArithmeticException();
168 return expTable[size - logTable[a] - 1];
172 * @return product of a and b in GF(size)
174 int multiply(int a, int b) {
177 if (a == 0 || b == 0) {
180 return expTable[(logTable[a] + logTable[b]) % (size - 1)];
183 public int getSize() {
187 public int getGeneratorBase() {
188 return generatorBase;
192 public String toString() {
193 return "GF(0x" + Integer.toHexString(primitive) + ',' + size + ')';