KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)EllipticCurve.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
8 package java.security.spec;
9
10 import java.math.BigInteger JavaDoc;
11 import java.util.Arrays JavaDoc;
12
13 /**
14  * This immutable class holds the necessary values needed to represent
15  * an elliptic curve.
16  *
17  * @see ECField
18  * @see ECFieldFp
19  * @see ECFieldF2m
20  *
21  * @author Valerie Peng
22  * @version 1.3, 12/19/03
23  *
24  * @since 1.5
25  */

26 public class EllipticCurve {
27     
28     private final ECField JavaDoc field;
29     private final BigInteger JavaDoc a;
30     private final BigInteger JavaDoc b;
31     private final byte[] seed;
32
33     // Check coefficient c is a valid element in ECField field.
34
private static void checkValidity(ECField JavaDoc field, BigInteger JavaDoc c,
35     String JavaDoc cName) {
36     // can only perform check if field is ECFieldFp or ECFieldF2m.
37
if (field instanceof ECFieldFp JavaDoc) {
38         BigInteger JavaDoc p = ((ECFieldFp JavaDoc)field).getP();
39         if (p.compareTo(c) != 1) {
40         throw new IllegalArgumentException JavaDoc(cName + " is too large");
41         } else if (c.signum() != 1) {
42         throw new IllegalArgumentException JavaDoc(cName + " is negative");
43         }
44     } else if (field instanceof ECFieldF2m JavaDoc) {
45         int m = ((ECFieldF2m JavaDoc)field).getM();
46         if (c.bitLength() > m) {
47         throw new IllegalArgumentException JavaDoc(cName + " is too large");
48         }
49     }
50     }
51
52     /**
53      * Creates an elliptic curve with the specified elliptic field
54      * <code>field</code> and the coefficients <code>a</code> and
55      * <code>b</code>.
56      * @param field the finite field that this elliptic curve is over.
57      * @param a the first coefficient of this elliptic curve.
58      * @param b the second coefficient of this elliptic curve.
59      * @exception NullPointerException if <code>field</code>,
60      * <code>a</code>, or <code>b</code> is null.
61      * @exception IllegalArgumentException if <code>a</code>
62      * or <code>b</code> is not null and not in <code>field</code>.
63      */

64     public EllipticCurve(ECField JavaDoc field, BigInteger JavaDoc a,
65              BigInteger JavaDoc b) {
66     this(field, a, b, null);
67     }
68
69     /**
70      * Creates an elliptic curve with the specified elliptic field
71      * <code>field</code>, the coefficients <code>a</code> and
72      * <code>b</code>, and the <code>seed</code> used for curve generation.
73      * @param field the finite field that this elliptic curve is over.
74      * @param a the first coefficient of this elliptic curve.
75      * @param b the second coefficient of this elliptic curve.
76      * @param seed the bytes used during curve generation for later
77      * validation. Contents of this array are copied to protect against
78      * subsequent modification.
79      * @exception NullPointerException if <code>field</code>,
80      * <code>a</code>, or <code>b</code> is null.
81      * @exception IllegalArgumentException if <code>a</code>
82      * or <code>b</code> is not null and not in <code>field</code>.
83      */

84     public EllipticCurve(ECField JavaDoc field, BigInteger JavaDoc a,
85              BigInteger JavaDoc b, byte[] seed) {
86         if (field == null) {
87             throw new NullPointerException JavaDoc("field is null");
88         }
89         if (a == null) {
90         throw new NullPointerException JavaDoc("first coefficient is null");
91     }
92         if (b == null) {
93             throw new NullPointerException JavaDoc("second coefficient is null");
94         }
95         checkValidity(field, a, "first coefficient");
96     checkValidity(field, b, "second coefficient");
97         this.field = field;
98         this.a = a;
99         this.b = b;
100     if (seed != null) {
101         this.seed = (byte[]) seed.clone();
102     } else {
103         this.seed = null;
104     }
105     }
106  
107     /**
108      * Returns the finite field <code>field</code> that this
109      * elliptic curve is over.
110      * @return the field <code>field</code> that this curve
111      * is over.
112      */

113     public ECField JavaDoc getField() {
114     return field;
115     }
116
117     /**
118      * Returns the first coefficient <code>a</code> of the
119      * elliptic curve.
120      * @return the first coefficient <code>a</code>.
121      */

122     public BigInteger JavaDoc getA() {
123     return a;
124     }
125
126     /**
127      * Returns the second coefficient <code>b</code> of the
128      * elliptic curve.
129      * @return the second coefficient <code>b</code>.
130      */

131     public BigInteger JavaDoc getB() {
132     return b;
133     }
134
135     /**
136      * Returns the seeding bytes <code>seed</code> used
137      * during curve generation. May be null if not specified.
138      * @return the seeding bytes <code>seed</code>. A new
139      * array is returned each time this method is called.
140      */

141     public byte[] getSeed() {
142     if (seed == null) return null;
143     else return (byte[]) seed.clone();
144     }
145
146     /**
147      * Compares this elliptic curve for equality with the
148      * specified object.
149      * @param obj the object to be compared.
150      * @return true if <code>obj</code> is an instance of
151      * EllipticCurve and the field, A, B, and seeding bytes
152      * match, false otherwise.
153      */

154     public boolean equals(Object JavaDoc obj) {
155     if (this == obj) return true;
156     if (obj instanceof EllipticCurve JavaDoc) {
157         EllipticCurve JavaDoc curve = (EllipticCurve JavaDoc) obj;
158         if ((field.equals(curve.field)) &&
159         (a.equals(curve.a)) &&
160         (b.equals(curve.b)) &&
161         (Arrays.equals(seed, curve.seed))) {
162         return true;
163         }
164     }
165     return false;
166     }
167
168     /**
169      * Returns a hash code value for this elliptic curve.
170      * @return a hash code value.
171      */

172     public int hashCode() {
173     return (field.hashCode() << 6 +
174         (a.hashCode() << 4) +
175         (b.hashCode() << 2) +
176         (seed==null? 0:seed.length));
177     }
178 }
179
Popular Tags