1 7 package java.security.spec; 8 9 import java.math.BigInteger ; 10 import java.util.Arrays ; 11 12 23 public class ECFieldF2m implements ECField { 24 25 private int m; 26 private int[] ks; 27 private BigInteger rp; 28 29 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 67 public ECFieldF2m(int m, BigInteger rp) { 68 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 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 116 public ECFieldF2m(int m, int[] ks) { 117 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 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 150 public int getFieldSize() { 151 return m; 152 } 153 154 160 public int getM() { 161 return m; 162 } 163 164 172 public BigInteger getReductionPolynomial() { 173 return rp; 174 } 175 176 185 public int[] getMidTermsOfReductionPolynomial() { 186 if (ks == null) { 187 return null; 188 } else { 189 return (int[]) ks.clone(); 190 } 191 } 192 193 201 public boolean equals(Object obj) { 202 if (this == obj) return true; 203 if (obj instanceof ECFieldF2m ) { 204 return ((m == ((ECFieldF2m )obj).m) && 207 (Arrays.equals(ks, ((ECFieldF2m ) obj).ks))); 208 } 209 return false; 210 } 211 212 217 public int hashCode() { 218 int value = m << 5; 219 value += (rp==null? 0:rp.hashCode()); 220 return value; 223 } 224 } 225 | Popular Tags |