1 7 8 package java.security.spec; 9 10 import java.math.BigInteger ; 11 import java.util.Arrays ; 12 13 26 public class EllipticCurve { 27 28 private final ECField field; 29 private final BigInteger a; 30 private final BigInteger b; 31 private final byte[] seed; 32 33 private static void checkValidity(ECField field, BigInteger c, 35 String cName) { 36 if (field instanceof ECFieldFp ) { 38 BigInteger p = ((ECFieldFp )field).getP(); 39 if (p.compareTo(c) != 1) { 40 throw new IllegalArgumentException (cName + " is too large"); 41 } else if (c.signum() != 1) { 42 throw new IllegalArgumentException (cName + " is negative"); 43 } 44 } else if (field instanceof ECFieldF2m ) { 45 int m = ((ECFieldF2m )field).getM(); 46 if (c.bitLength() > m) { 47 throw new IllegalArgumentException (cName + " is too large"); 48 } 49 } 50 } 51 52 64 public EllipticCurve(ECField field, BigInteger a, 65 BigInteger b) { 66 this(field, a, b, null); 67 } 68 69 84 public EllipticCurve(ECField field, BigInteger a, 85 BigInteger b, byte[] seed) { 86 if (field == null) { 87 throw new NullPointerException ("field is null"); 88 } 89 if (a == null) { 90 throw new NullPointerException ("first coefficient is null"); 91 } 92 if (b == null) { 93 throw new NullPointerException ("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 113 public ECField getField() { 114 return field; 115 } 116 117 122 public BigInteger getA() { 123 return a; 124 } 125 126 131 public BigInteger getB() { 132 return b; 133 } 134 135 141 public byte[] getSeed() { 142 if (seed == null) return null; 143 else return (byte[]) seed.clone(); 144 } 145 146 154 public boolean equals(Object obj) { 155 if (this == obj) return true; 156 if (obj instanceof EllipticCurve ) { 157 EllipticCurve curve = (EllipticCurve ) 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 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 |