KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ECFieldF2m.java 1.3 03/12/19
3  *
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 JavaDoc;
10 import java.util.Arrays JavaDoc;
11
12 /**
13  * This immutable class defines an elliptic curve (EC)
14  * characteristic 2 finite field.
15  *
16  * @see ECField
17  *
18  * @author Valerie Peng
19  * @version 1.3, 12/19/03
20  *
21  * @since 1.5
22  */

23 public class ECFieldF2m implements ECField JavaDoc {
24
25     private int m;
26     private int[] ks;
27     private BigInteger JavaDoc rp;
28
29     /**
30      * Creates an elliptic curve characteristic 2 finite
31      * field which has 2^<code>m</code> elements with normal basis.
32      * @param m with 2^<code>m</code> being the number of elements.
33      * @exception IllegalArgumentException if <code>m</code>
34      * is not positive.
35      */

36     public ECFieldF2m(int m) {
37     if (m <= 0) {
38         throw new IllegalArgumentException JavaDoc("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 finite
47      * field which has 2^<code>m</code> elements with
48      * polynomial basis.
49      * The reduction polynomial for this field is based
50      * on <code>rp</code> whose i-th bit correspondes to
51      * the i-th coefficient of the reduction polynomial.<p>
52      * Note: A valid reduction polynomial is either a
53      * trinomial (X^<code>m</code> + X^<code>k</code> + 1
54      * with <code>m</code> > <code>k</code> >= 1) or a
55      * pentanomial (X^<code>m</code> + X^<code>k3</code>
56      * + X^<code>k2</code> + X^<code>k1</code> + 1 with
57      * <code>m</code> > <code>k3</code> > <code>k2</code>
58      * > <code>k1</code> >= 1).
59      * @param m with 2^<code>m</code> being the number of elements.
60      * @param rp the BigInteger whose i-th bit corresponds to
61      * the i-th coefficient of the reduction polynomial.
62      * @exception NullPointerException if <code>rp</code> is null.
63      * @exception IllegalArgumentException if <code>m</code>
64      * is not positive, or <code>rp</code> does not represent
65      * a valid reduction polynomial.
66      */

67     public ECFieldF2m(int m, BigInteger JavaDoc rp) {
68     // check m and rp
69
this.m = m;
70         this.rp = rp;
71         if (m <= 0) {
72             throw new IllegalArgumentException JavaDoc("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 JavaDoc
78         ("rp does not represent a valid reduction polynomial");
79     }
80     // convert rp into ks
81
BigInteger JavaDoc 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 finite
92      * field which has 2^<code>m</code> elements with
93      * polynomial basis. The reduction polynomial for this
94      * field is based on <code>ks</code> whose content
95      * contains the order of the middle term(s) of the
96      * reduction polynomial.
97      * Note: A valid reduction polynomial is either a
98      * trinomial (X^<code>m</code> + X^<code>k</code> + 1
99      * with <code>m</code> > <code>k</code> >= 1) or a
100      * pentanomial (X^<code>m</code> + X^<code>k3</code>
101      * + X^<code>k2</code> + X^<code>k1</code> + 1 with
102      * <code>m</code> > <code>k3</code> > <code>k2</code>
103      * > <code>k1</code> >= 1), so <code>ks</code> should
104      * have length 1 or 3.
105      * @param m with 2^<code>m</code> being the number of elements.
106      * @param ks the order of the middle term(s) of the
107      * reduction polynomial. Contents of this array are copied
108      * to protect against subsequent modification.
109      * @exception NullPointerException if <code>ks</code> is null.
110      * @exception IllegalArgumentException if<code>m</code>
111      * is not positive, or the length of <code>ks</code>
112      * is neither 1 nor 3, or values in <code>ks</code>
113      * are not between <code>m</code>-1 and 1 (inclusive)
114      * and in descending order.
115      */

116     public ECFieldF2m(int m, int[] ks) {
117     // check m and ks
118
this.m = m;
119         this.ks = (int[]) ks.clone();
120     if (m <= 0) {
121         throw new IllegalArgumentException JavaDoc("m is not positive");
122     }
123     if ((this.ks.length != 1) && (this.ks.length != 3)) {
124         throw new IllegalArgumentException JavaDoc
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 JavaDoc
130             ("ks["+ i + "] is out of range");
131         }
132         if ((i != 0) && (this.ks[i] >= this.ks[i-1])) {
133         throw new IllegalArgumentException JavaDoc
134             ("values in ks are not in descending order");
135         }
136     }
137     // convert ks into rp
138
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 <code>m</code>
147      * 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 <code>m</code> of this characteristic
156      * 2 finite field.
157      * @return <code>m</code> with 2^<code>m</code> 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 polynomial
170      * basis or null for normal basis.
171      */

172     public BigInteger JavaDoc 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 the
195      * specified object.
196      * @param obj the object to be compared.
197      * @return true if <code>obj</code> is an instance
198      * of ECFieldF2m and both <code>m</code> and the reduction
199      * polynomial match, false otherwise.
200      */

201     public boolean equals(Object JavaDoc obj) {
202     if (this == obj) return true;
203     if (obj instanceof ECFieldF2m JavaDoc) {
204         // no need to compare rp here since ks and rp
205
// should be equivalent
206
return ((m == ((ECFieldF2m JavaDoc)obj).m) &&
207             (Arrays.equals(ks, ((ECFieldF2m JavaDoc) 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