829N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
829N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
829N/A *
829N/A * This code is free software; you can redistribute it and/or modify it
829N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
829N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
829N/A *
829N/A * This code is distributed in the hope that it will be useful, but WITHOUT
829N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
829N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
829N/A * version 2 for more details (a copy is included in the LICENSE file that
829N/A * accompanied this code).
829N/A *
829N/A * You should have received a copy of the GNU General Public License version
829N/A * 2 along with this work; if not, write to the Free Software Foundation,
829N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
829N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
829N/A */
829N/Apackage java.security.spec;
829N/A
829N/Aimport java.math.BigInteger;
829N/Aimport java.util.Arrays;
829N/A
829N/A/**
829N/A * This immutable class defines an elliptic curve (EC)
829N/A * characteristic 2 finite field.
829N/A *
829N/A * @see ECField
829N/A *
829N/A * @author Valerie Peng
829N/A *
829N/A * @since 1.5
829N/A */
829N/Apublic class ECFieldF2m implements ECField {
829N/A
829N/A private int m;
829N/A private int[] ks;
829N/A private BigInteger rp;
829N/A
829N/A /**
829N/A * Creates an elliptic curve characteristic 2 finite
829N/A * field which has 2^<code>m</code> elements with normal basis.
829N/A * @param m with 2^<code>m</code> being the number of elements.
829N/A * @exception IllegalArgumentException if <code>m</code>
829N/A * is not positive.
829N/A */
829N/A public ECFieldF2m(int m) {
829N/A if (m <= 0) {
829N/A throw new IllegalArgumentException("m is not positive");
829N/A }
829N/A this.m = m;
829N/A this.ks = null;
829N/A this.rp = null;
829N/A }
829N/A
829N/A /**
829N/A * Creates an elliptic curve characteristic 2 finite
829N/A * field which has 2^<code>m</code> elements with
829N/A * polynomial basis.
829N/A * The reduction polynomial for this field is based
829N/A * on <code>rp</code> whose i-th bit correspondes to
829N/A * the i-th coefficient of the reduction polynomial.<p>
829N/A * Note: A valid reduction polynomial is either a
829N/A * trinomial (X^<code>m</code> + X^<code>k</code> + 1
829N/A * with <code>m</code> > <code>k</code> >= 1) or a
829N/A * pentanomial (X^<code>m</code> + X^<code>k3</code>
829N/A * + X^<code>k2</code> + X^<code>k1</code> + 1 with
829N/A * <code>m</code> > <code>k3</code> > <code>k2</code>
829N/A * > <code>k1</code> >= 1).
829N/A * @param m with 2^<code>m</code> being the number of elements.
829N/A * @param rp the BigInteger whose i-th bit corresponds to
829N/A * the i-th coefficient of the reduction polynomial.
829N/A * @exception NullPointerException if <code>rp</code> is null.
829N/A * @exception IllegalArgumentException if <code>m</code>
829N/A * is not positive, or <code>rp</code> does not represent
829N/A * a valid reduction polynomial.
829N/A */
829N/A public ECFieldF2m(int m, BigInteger rp) {
829N/A // check m and rp
829N/A this.m = m;
829N/A this.rp = rp;
829N/A if (m <= 0) {
829N/A throw new IllegalArgumentException("m is not positive");
829N/A }
829N/A int bitCount = this.rp.bitCount();
829N/A if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
829N/A ((bitCount != 3) && (bitCount != 5))) {
829N/A throw new IllegalArgumentException
829N/A ("rp does not represent a valid reduction polynomial");
829N/A }
829N/A // convert rp into ks
829N/A BigInteger temp = this.rp.clearBit(0).clearBit(m);
829N/A this.ks = new int[bitCount-2];
829N/A for (int i = this.ks.length-1; i >= 0; i--) {
829N/A int index = temp.getLowestSetBit();
829N/A this.ks[i] = index;
829N/A temp = temp.clearBit(index);
829N/A }
829N/A }
829N/A
829N/A /**
829N/A * Creates an elliptic curve characteristic 2 finite
829N/A * field which has 2^<code>m</code> elements with
829N/A * polynomial basis. The reduction polynomial for this
829N/A * field is based on <code>ks</code> whose content
829N/A * contains the order of the middle term(s) of the
829N/A * reduction polynomial.
829N/A * Note: A valid reduction polynomial is either a
829N/A * trinomial (X^<code>m</code> + X^<code>k</code> + 1
829N/A * with <code>m</code> > <code>k</code> >= 1) or a
829N/A * pentanomial (X^<code>m</code> + X^<code>k3</code>
829N/A * + X^<code>k2</code> + X^<code>k1</code> + 1 with
829N/A * <code>m</code> > <code>k3</code> > <code>k2</code>
* > <code>k1</code> >= 1), so <code>ks</code> should
* have length 1 or 3.
* @param m with 2^<code>m</code> being the number of elements.
* @param ks the order of the middle term(s) of the
* reduction polynomial. Contents of this array are copied
* to protect against subsequent modification.
* @exception NullPointerException if <code>ks</code> is null.
* @exception IllegalArgumentException if<code>m</code>
* is not positive, or the length of <code>ks</code>
* is neither 1 nor 3, or values in <code>ks</code>
* are not between <code>m</code>-1 and 1 (inclusive)
* and in descending order.
*/
public ECFieldF2m(int m, int[] ks) {
// check m and ks
this.m = m;
this.ks = ks.clone();
if (m <= 0) {
throw new IllegalArgumentException("m is not positive");
}
if ((this.ks.length != 1) && (this.ks.length != 3)) {
throw new IllegalArgumentException
("length of ks is neither 1 nor 3");
}
for (int i = 0; i < this.ks.length; i++) {
if ((this.ks[i] < 1) || (this.ks[i] > m-1)) {
throw new IllegalArgumentException
("ks["+ i + "] is out of range");
}
if ((i != 0) && (this.ks[i] >= this.ks[i-1])) {
throw new IllegalArgumentException
("values in ks are not in descending order");
}
}
// convert ks into rp
this.rp = BigInteger.ONE;
this.rp = rp.setBit(m);
for (int j = 0; j < this.ks.length; j++) {
rp = rp.setBit(this.ks[j]);
}
}
/**
* Returns the field size in bits which is <code>m</code>
* for this characteristic 2 finite field.
* @return the field size in bits.
*/
public int getFieldSize() {
return m;
}
/**
* Returns the value <code>m</code> of this characteristic
* 2 finite field.
* @return <code>m</code> with 2^<code>m</code> being the
* number of elements.
*/
public int getM() {
return m;
}
/**
* Returns a BigInteger whose i-th bit corresponds to the
* i-th coefficient of the reduction polynomial for polynomial
* basis or null for normal basis.
* @return a BigInteger whose i-th bit corresponds to the
* i-th coefficient of the reduction polynomial for polynomial
* basis or null for normal basis.
*/
public BigInteger getReductionPolynomial() {
return rp;
}
/**
* Returns an integer array which contains the order of the
* middle term(s) of the reduction polynomial for polynomial
* basis or null for normal basis.
* @return an integer array which contains the order of the
* middle term(s) of the reduction polynomial for polynomial
* basis or null for normal basis. A new array is returned
* each time this method is called.
*/
public int[] getMidTermsOfReductionPolynomial() {
if (ks == null) {
return null;
} else {
return ks.clone();
}
}
/**
* Compares this finite field for equality with the
* specified object.
* @param obj the object to be compared.
* @return true if <code>obj</code> is an instance
* of ECFieldF2m and both <code>m</code> and the reduction
* polynomial match, false otherwise.
*/
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj instanceof ECFieldF2m) {
// no need to compare rp here since ks and rp
// should be equivalent
return ((m == ((ECFieldF2m)obj).m) &&
(Arrays.equals(ks, ((ECFieldF2m) obj).ks)));
}
return false;
}
/**
* Returns a hash code value for this characteristic 2
* finite field.
* @return a hash code value.
*/
public int hashCode() {
int value = m << 5;
value += (rp==null? 0:rp.hashCode());
// no need to involve ks here since ks and rp
// should be equivalent.
return value;
}
}