/* * GF.java - Implements Finite Field GF(p^m). * * ------------------------------------------------------------------ * * @begin[license] * Copyright (C) 2003 Kai Chen, Caltech * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * @author: Kai Chen * @email{kchen@cs.caltech.edu} * @end[license] * */ public class GF { /* * default values for EE127 final project: * GF(2^5), poly(x) = x^5 + x^2 + 1. */ private static final int DEFAULT_P = 2; private static final int DEFAULT_M = 5; private static final int DEFAULT_POLY[] = {1, 0, 1, 0, 0, 1}; /* field related data */ protected int p, m, n; protected int poly[]; /* power and log tables */ protected int powerTable[], logTable[]; /* * post: construct default GF(2^5) */ public GF() { this(DEFAULT_P, DEFAULT_M, DEFAULT_POLY); } /* * pre: p is prime, poly is irreducible with degree m * post: construct GF(p^m) */ public GF(int p, int m, int poly[]) { this.p = p; this.m = m; this.poly = poly; /* compute n = p^m */ n = 1; for(int i=0; i= n) powerTable[i] = add(powerTable[i]%n, powerTable[m]); /* build the logTable */ logTable = new int[n]; for(int i=0; i=0; i--) x = x*p + vector[i]; return x; } /* * pre: x, y are two values in GF(p^m) * post: returns a value (x+y) in GF(p^m) */ public int add(int x, int y) { /* optimization for p=2 case */ if(p==2) return x^y; int xVector[] = toVector(x); int yVector[] = toVector(y); int resultVector[] = new int[m]; /* do 'entry-wise' addition */ for(int i=0; i