Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > spec > ECFieldF2m

 `1 /*2  * @(#)ECFieldF2m.java 1.3 03/12/193  *4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.6  */7 package java.security.spec;8 9 import java.math.BigInteger ;10 import java.util.Arrays ;11 12 /**13  * This immutable class defines an elliptic curve (EC)14  * characteristic 2 finite field.15  *16  * @see ECField17  *18  * @author Valerie Peng19  * @version 1.3, 12/19/0320  *21  * @since 1.522  */23 public class ECFieldF2m implements ECField {24 25     private int m;26     private int[] ks;27     private BigInteger rp;28 29     /**30      * Creates an elliptic curve characteristic 2 finite31      * field which has 2^m elements with normal basis.32      * @param m with 2^m being the number of elements.33      * @exception IllegalArgumentException if m34      * is not positive.35      */36     public ECFieldF2m(int m) {37     if (m <= 0) {38         throw new IllegalArgumentException ("m is not positive");39     }40     this.m = m;41     this.ks = null;42     this.rp = null;43     }44 45     /**46      * Creates an elliptic curve characteristic 2 finite47      * field which has 2^m elements with 48      * polynomial basis.49      * The reduction polynomial for this field is based50      * on rp whose i-th bit correspondes to51      * the i-th coefficient of the reduction polynomial.

52      * Note: A valid reduction polynomial is either a 53      * trinomial (X^m + X^k + 154      * with m > k >= 1) or a55      * pentanomial (X^m + X^k3 56      * + X^k2 + X^k1 + 1 with57      * m > k3 > k2 58      * > k1 >= 1). 59      * @param m with 2^m being the number of elements.60      * @param rp the BigInteger whose i-th bit corresponds to61      * the i-th coefficient of the reduction polynomial. 62      * @exception NullPointerException if rp is null.63      * @exception IllegalArgumentException if m 64      * is not positive, or rp does not represent 65      * a valid reduction polynomial. 66      */67     public ECFieldF2m(int m, BigInteger rp) {68     // check m and rp69 this.m = m;70         this.rp = rp;71         if (m <= 0) {72             throw new IllegalArgumentException ("m is not positive");73         }74     int bitCount = this.rp.bitCount();75     if (!this.rp.testBit(0) || !this.rp.testBit(m) ||76         ((bitCount != 3) && (bitCount != 5))) {77         throw new IllegalArgumentException 78         ("rp does not represent a valid reduction polynomial");79     }80     // convert rp into ks81 BigInteger temp = this.rp.clearBit(0).clearBit(m);82     this.ks = new int[bitCount-2];83     for (int i = this.ks.length-1; i >= 0; i--) {84         int index = temp.getLowestSetBit();85         this.ks[i] = index;86         temp = temp.clearBit(index);87     }88     }89 90     /**91      * Creates an elliptic curve characteristic 2 finite92      * field which has 2^m elements with93      * polynomial basis. The reduction polynomial for this94      * field is based on ks whose content95      * contains the order of the middle term(s) of the 96      * reduction polynomial. 97      * Note: A valid reduction polynomial is either a98      * trinomial (X^m + X^k + 199      * with m > k >= 1) or a100      * pentanomial (X^m + X^k3101      * + X^k2 + X^k1 + 1 with102      * m > k3 > k2103      * > k1 >= 1), so ks should104      * have length 1 or 3.105      * @param m with 2^m being the number of elements. 106      * @param ks the order of the middle term(s) of the107      * reduction polynomial. Contents of this array are copied 108      * to protect against subsequent modification.109      * @exception NullPointerException if ks is null.110      * @exception IllegalArgumentException ifm 111      * is not positive, or the length of ks 112      * is neither 1 nor 3, or values in ks 113      * are not between m-1 and 1 (inclusive) 114      * and in descending order. 115      */116     public ECFieldF2m(int m, int[] ks) {117     // check m and ks118 this.m = m;119         this.ks = (int[]) ks.clone();120     if (m <= 0) {121         throw new IllegalArgumentException ("m is not positive");122     }123     if ((this.ks.length != 1) && (this.ks.length != 3)) {124         throw new IllegalArgumentException 125         ("length of ks is neither 1 nor 3");126     }127     for (int i = 0; i < this.ks.length; i++) {128         if ((this.ks[i] < 1) || (this.ks[i] > m-1)) {129         throw new IllegalArgumentException 130             ("ks["+ i + "] is out of range");131         }132         if ((i != 0) && (this.ks[i] >= this.ks[i-1])) {133         throw new IllegalArgumentException 134             ("values in ks are not in descending order");135         }136     }137     // convert ks into rp138 this.rp = BigInteger.ONE;139     this.rp = rp.setBit(m);140     for (int j = 0; j < this.ks.length; j++) {141         rp = rp.setBit(this.ks[j]);142     }143     }144  145     /**146      * Returns the field size in bits which is m147      * for this characteristic 2 finite field.148      * @return the field size in bits.149      */150     public int getFieldSize() {151     return m;152     }153 154     /**155      * Returns the value m of this characteristic156      * 2 finite field.157      * @return m with 2^m being the 158      * number of elements.159      */160     public int getM() {161     return m;162     }163  164     /**165      * Returns a BigInteger whose i-th bit corresponds to the 166      * i-th coefficient of the reduction polynomial for polynomial 167      * basis or null for normal basis. 168      * @return a BigInteger whose i-th bit corresponds to the 169      * i-th coefficient of the reduction polynomial for polynomial170      * basis or null for normal basis.171      */172     public BigInteger getReductionPolynomial() {173     return rp;174     }175  176     /**177      * Returns an integer array which contains the order of the 178      * middle term(s) of the reduction polynomial for polynomial 179      * basis or null for normal basis.180      * @return an integer array which contains the order of the 181      * middle term(s) of the reduction polynomial for polynomial 182      * basis or null for normal basis. A new array is returned 183      * each time this method is called.184      */185     public int[] getMidTermsOfReductionPolynomial() {186     if (ks == null) { 187         return null; 188     } else {189         return (int[]) ks.clone();190     }191     }192  193     /**194      * Compares this finite field for equality with the195      * specified object. 196      * @param obj the object to be compared.197      * @return true if obj is an instance198      * of ECFieldF2m and both m and the reduction 199      * polynomial match, false otherwise.200      */201     public boolean equals(Object obj) {202     if (this == obj) return true;203     if (obj instanceof ECFieldF2m ) {204         // no need to compare rp here since ks and rp 205 // should be equivalent206 return ((m == ((ECFieldF2m )obj).m) &&207             (Arrays.equals(ks, ((ECFieldF2m ) obj).ks)));208     }209     return false;210     }211  212     /**213      * Returns a hash code value for this characteristic 2 214      * finite field.215      * @return a hash code value.216      */217     public int hashCode() {218     int value = m << 5;219     value += (rp==null? 0:rp.hashCode());220     // no need to involve ks here since ks and rp 221 // should be equivalent.222 return value;223     }224 }225 ` Popular Tags