下面列出了怎么用java.security.spec.ECFieldF2m的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Tests for constructor <code>ECFieldF2m(int)</code><br>
*
* Assertion: constructs new <code>ECFieldF2m</code> object
* using valid parameter m.
*
* Assertion: IllegalArgumentException if m is not positive.
*/
public final void testECFieldF2mint() {
for(int i=0; i<intCtorTestParameters.length; i++) {
ECFieldF2mDomainParams tp = intCtorTestParameters[i];
try {
// perform test
new ECFieldF2m(tp.m);
if (tp.x != null) {
// exception has been expected
fail(getName() + ", set " + i +
" FAILED: expected exception has not been thrown");
}
} catch (Exception e){
if (tp.x == null || !e.getClass().isInstance(tp.x)) {
// exception: failure
// if it has not been expected
// or wrong one has been thrown
fail(getName() + ", set " + i +
" FAILED: unexpected " + e);
}
}
}
}
/**
* Tests for constructor <code>ECFieldF2m(int m, int[] ks)</code><br>
*
* Assertion: constructs new <code>ECFieldF2m</code> object
* using valid parameters m and rp. ks represents trinomial basis.
*
* Assertion: constructs new <code>ECFieldF2m</code> object
* using valid parameters m and ks. ks represents pentanomial basis.
*
* Assertion: IllegalArgumentException if m is not positive.
*
* Assertion: NullPointerException if ks is null.
*
* Assertion: IllegalArgumentException if ks is invalid.
*/
public final void testECFieldF2mintintArray() {
for(int i=0; i<constructorTestParameters.length; i++) {
ECFieldF2mDomainParams tp = constructorTestParameters[i];
try {
// perform test
ECFieldF2m test = new ECFieldF2m(tp.m, tp.ks);
if (tp.x != null) {
// exception has been expected
fail(getName() + ", set " + i +
" FAILED: expected exception has not been thrown");
}
} catch (Exception e){
if (tp.x == null || !e.getClass().isInstance(tp.x)) {
// exception: failure
// if it has not been expected
// or wrong one has been thrown
fail(getName() + ", set " + i +
" FAILED: unexpected " + e);
}
}
}
}
/**
* Tests for constructor <code>ECFieldF2m(int m, BigInteger rp)</code><br>
*
* Assertion: constructs new <code>ECFieldF2m</code> object
* using valid parameters m and rp.
*
* Assertion: constructs new <code>ECFieldF2m</code> object
* using valid parameters m and rp.
*
* Assertion: IllegalArgumentException if m is not positive.
*
* Assertion: NullPointerException if rp is null.
*
* Assertion: IllegalArgumentException if rp is invalid.
*/
public final void testECFieldF2mintBigInteger() {
for(int i=0; i<constructorTestParameters.length; i++) {
ECFieldF2mDomainParams tp = constructorTestParameters[i];
try {
// perform test
new ECFieldF2m(tp.m, tp.rp);
if (tp.x != null) {
// exception has been expected
fail(getName() + ", set " + i +
" FAILED: expected exception has not been thrown");
}
} catch (Exception e){
if (tp.x == null || !e.getClass().isInstance(tp.x)) {
// exception: failure
// if it has not been expected
// or wrong one has been thrown
fail(getName() + ", set " + i +
" FAILED: unexpected " + e);
}
}
}
}
public static ECCurve convertCurve(
EllipticCurve ec)
{
ECField field = ec.getField();
BigInteger a = ec.getA();
BigInteger b = ec.getB();
if (field instanceof ECFieldFp)
{
ECCurve.Fp curve = new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);
if (customCurves.containsKey(curve))
{
return (ECCurve)customCurves.get(curve);
}
return curve;
}
else
{
ECFieldF2m fieldF2m = (ECFieldF2m)field;
int m = fieldF2m.getM();
int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b);
}
}
public static ECCurve convertCurve(
EllipticCurve ec)
{
ECField field = ec.getField();
BigInteger a = ec.getA();
BigInteger b = ec.getB();
if (field instanceof ECFieldFp)
{
ECCurve.Fp curve = new ECCurve.Fp(((ECFieldFp)field).getP(), a, b);
if (customCurves.containsKey(curve))
{
return (ECCurve)customCurves.get(curve);
}
return curve;
}
else
{
ECFieldF2m fieldF2m = (ECFieldF2m)field;
int m = fieldF2m.getM();
int ks[] = ECUtil.convertMidTerms(fieldF2m.getMidTermsOfReductionPolynomial());
return new ECCurve.F2m(m, ks[0], ks[1], ks[2], a, b);
}
}
public static EcFieldDef from(ECField field) {
if (field instanceof ECFieldFp) {
return EcPrimeFieldDef.from((ECFieldFp) field);
} else if (field instanceof ECFieldF2m) {
return EcCharacteristic2FieldDef.from((ECFieldF2m) field);
} else {
throw new IllegalArgumentException(field.toString());
}
}
@Override
public ECFieldF2m toECField() {
if (this.basis != null) {
return new ECFieldF2m(this.size, this.basis);
} else {
return new ECFieldF2m(this.size);
}
}
/**
* Test #5 for <code>EllipticCurve(ECField, BigInteger, BigInteger, byte[])</code>
* constructor<br>
* Assertion: array <code>seed</code> is copied to prevent subsequent modification<br>
* Test preconditions: pass <code>seed</code> to the ctor then modify it<br>
* Expected: getSeed() must return unmodified array
*/
public final void testEllipticCurveECFieldBigIntegerBigIntegerbyteArray05() {
ECFieldF2m f = new ECFieldF2m(5);
BigInteger a = BigInteger.valueOf(0L);
BigInteger b = BigInteger.valueOf(19L);
byte[] seed = new byte[24];
byte[] seedCopy = seed.clone();
EllipticCurve c = new EllipticCurve(f, a, b, seedCopy);
// modify array passed
seedCopy[0] = (byte) 1;
// check that above modification did not changed
// internal state of test object
assertTrue(Arrays.equals(seed, c.getSeed()));
}
protected void setUp() throws Exception {
super.setUp();
ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger
.valueOf(1));
EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger
.valueOf(1), BigInteger.valueOf(1));
w = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
params = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
ecpks = new ECPublicKeySpec(w, params);
}
protected void setUp() throws Exception {
super.setUp();
ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger
.valueOf(1));
EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger
.valueOf(1), BigInteger.valueOf(1));
s = BigInteger.valueOf(1);
ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
ecpks = new ECPrivateKeySpec(s, ecparams);
}
/**
* Test #1 for <code>hashCode()</code> method.<br>
*
* Assertion: must return the same value if invoked
* repeatedly on the same object.
*/
public final void testHashCode01() {
ECFieldF2m f = new ECFieldF2m(2000);
int hc = f.hashCode();
assertTrue(hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode());
}
/**
* Test #2 for <code>hashCode()</code> method.<br>
*
* Assertion: must return the same value if invoked
* repeatedly on the same object.
*/
public final void testHashCode02() {
ECFieldF2m f = new ECFieldF2m(2000, new int[] {981, 2, 1});
int hc = f.hashCode();
assertTrue(hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode() &&
hc == f.hashCode());
}
/**
* Test #5 for <code>hashCode()</code> method.<br>
*
* Assertion: must return the same value if invoked
* on equal (according to the <code>equals(Object)</code> method) objects.
*/
public final void testHashCode05() {
assertTrue(new ECFieldF2m(2000, new int[] {981, 2, 1}).hashCode() ==
new ECFieldF2m(2000, BigInteger.valueOf(0L).
setBit(0).setBit(1).setBit(2).
setBit(981).setBit(2000)).hashCode());
}
/**
* Test #4 for <code>equals()</code> method.<br>
*
* Assertion: pentanomial basis - objects equal if their m, and rp
* are mutually equal.
*/
public final void testEqualsObject04() {
ECFieldF2m f1 = new ECFieldF2m(2000, new int[] {981, 2, 1});
ECFieldF2m f2 = new ECFieldF2m(2000, BigInteger.valueOf(0L).
setBit(0).setBit(1).setBit(2).
setBit(981).setBit(2000));
assertTrue(f1.equals(f2) && f2.equals(f1));
}
/**
* Test #5 for <code>equals()</code> method.<br>
*
* Assertion: objects equal if their m, and rp are mutually equal.
*/
public final void testEqualsObject05() {
ECFieldF2m f1 = new ECFieldF2m(2000);
ECFieldF2m f2 = new ECFieldF2m(2000, BigInteger.valueOf(0L).
setBit(0).setBit(1).setBit(2).
setBit(981).setBit(2000));
assertFalse(f1.equals(f2) || f2.equals(f1));
}
/**
* Test #1 for <code>getMidTermsOfReductionPolynomial()</code> method.<br>
*
* Assertion: returns mid terms of reduction polynomial
*/
public final void testGetMidTermsOfReductionPolynomial01() {
int[] a = new int[] {981,2,1};
int[] b = new ECFieldF2m(2000,
BigInteger.valueOf(0L).setBit(0).setBit(1).
setBit(2).setBit(981).setBit(2000)).
getMidTermsOfReductionPolynomial();
assertTrue(Arrays.equals(a, b));
}
/**
* Tests that object state is preserved against modifications
* through array reference passed to the constructor.
*/
public final void testIsStatePreserved01() {
// reference array
int[] a = new int[] {367};
// reference array copy
int[] aCopy = a.clone();
// create obj using copy
ECFieldF2m f = new ECFieldF2m(1999, aCopy);
// modify copy
aCopy[0] = 5;
// compare reference with returned array
assertTrue(Arrays.equals(a, f.getMidTermsOfReductionPolynomial()));
}
/**
* Tests that object state is preserved against
* modifications through array reference returned by
* <code>getMidTermsOfReductionPolynomial()</code> method.
*/
public final void testIsStatePreserved02() {
// reference array
int[] a = new int[] {981,2,1};
// reference array copy
int[] aCopy = a.clone();
// create obj using copy
ECFieldF2m f = new ECFieldF2m(2000, aCopy);
// get array reference and modify returned array
f.getMidTermsOfReductionPolynomial()[0] = 1532;
// compare reference with returned for the second time array
assertTrue(Arrays.equals(a, f.getMidTermsOfReductionPolynomial()));
}
protected void setUp() throws Exception {
super.setUp();
curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1),
BigInteger.valueOf(1));
ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
ecps = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
}
/**
* Decode a point on this curve which has been encoded using point
* compression (X9.62 s 4.2.1 and 4.2.2) or regular encoding.
*
* @param curve
* The elliptic curve.
* @param encoded
* The encoded point.
* @return the decoded point.
*/
public static ECPoint decodePoint(
EllipticCurve curve,
byte[] encoded)
{
ECCurve c = null;
if (curve.getField() instanceof ECFieldFp)
{
c = new ECCurve.Fp(
((ECFieldFp)curve.getField()).getP(), curve.getA(), curve.getB());
}
else
{
int k[] = ((ECFieldF2m)curve.getField()).getMidTermsOfReductionPolynomial();
if (k.length == 3)
{
c = new ECCurve.F2m(
((ECFieldF2m)curve.getField()).getM(), k[2], k[1], k[0], curve.getA(), curve.getB());
}
else
{
c = new ECCurve.F2m(
((ECFieldF2m)curve.getField()).getM(), k[0], curve.getA(), curve.getB());
}
}
org.ripple.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded);
return new ECPoint(p.getAffineXCoord().toBigInteger(), p.getAffineYCoord().toBigInteger());
}
private static EllipticCurve convertCurve(
ECCurve curve,
byte[] seed)
{
if (ECAlgorithms.isFpCurve(curve))
{
return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
}
else
{
ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
int ks[];
if (curveF2m.isTrinomial())
{
ks = new int[] { curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
}
else
{
ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
}
}
}
public static EllipticCurve convertCurve(
ECCurve curve,
byte[] seed)
{
// TODO: the Sun EC implementation doesn't currently handle the seed properly
// so at the moment it's set to null. Should probably look at making this configurable
if (ECAlgorithms.isFpCurve(curve))
{
return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
else
{
ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
int ks[];
if (curveF2m.isTrinomial())
{
ks = new int[] { curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
else
{
ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
}
}
/**
* Decode a point on this curve which has been encoded using point
* compression (X9.62 s 4.2.1 and 4.2.2) or regular encoding.
*
* @param curve
* The elliptic curve.
* @param encoded
* The encoded point.
* @return the decoded point.
*/
public static ECPoint decodePoint(
EllipticCurve curve,
byte[] encoded)
{
ECCurve c = null;
if (curve.getField() instanceof ECFieldFp)
{
c = new ECCurve.Fp(
((ECFieldFp)curve.getField()).getP(), curve.getA(), curve.getB());
}
else
{
int k[] = ((ECFieldF2m)curve.getField()).getMidTermsOfReductionPolynomial();
if (k.length == 3)
{
c = new ECCurve.F2m(
((ECFieldF2m)curve.getField()).getM(), k[2], k[1], k[0], curve.getA(), curve.getB());
}
else
{
c = new ECCurve.F2m(
((ECFieldF2m)curve.getField()).getM(), k[0], curve.getA(), curve.getB());
}
}
org.ripple.bouncycastle.math.ec.ECPoint p = c.decodePoint(encoded);
return new ECPoint(p.getAffineXCoord().toBigInteger(), p.getAffineYCoord().toBigInteger());
}
private static EllipticCurve convertCurve(
ECCurve curve,
byte[] seed)
{
if (ECAlgorithms.isFpCurve(curve))
{
return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
}
else
{
ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
int ks[];
if (curveF2m.isTrinomial())
{
ks = new int[] { curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
}
else
{
ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
}
}
}
public static EllipticCurve convertCurve(
ECCurve curve,
byte[] seed)
{
// TODO: the Sun EC implementation doesn't currently handle the seed properly
// so at the moment it's set to null. Should probably look at making this configurable
if (ECAlgorithms.isFpCurve(curve))
{
return new EllipticCurve(new ECFieldFp(curve.getField().getCharacteristic()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
else
{
ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
int ks[];
if (curveF2m.isTrinomial())
{
ks = new int[] { curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
else
{
ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), null);
}
}
}
public static EcCharacteristic2FieldDef from(ECFieldF2m field) {
return new EcCharacteristic2FieldDef(field.getM(), field.getReductionPolynomial());
}
/**
* Test #3 for <code>getMidTermsOfReductionPolynomial()</code> method.<br>
*
* Assertion: returns mid terms of reduction polynomial
*/
public final void testGetMidTermsOfReductionPolynomial03() {
int[] a = new int[] {367};
int[] b = new ECFieldF2m(1999, a).getMidTermsOfReductionPolynomial();
assertTrue(Arrays.equals(a, b));
}
/**
* Test #1 for <code>getReductionPolynomial()</code> method.<br>
*
* Assertion: returns reduction polynomial
*/
public final void testGetReductionPolynomial01() {
BigInteger rp = BigInteger.valueOf(0L).setBit(0).setBit(1).setBit(2).
setBit(981).setBit(2000);
assertTrue(new ECFieldF2m(2000, rp).getReductionPolynomial().equals(rp));
}
/**
* Test for <code>getA()</code> method<br>
* Assertion: returns coefficient <code>a</code><br>
* Test preconditions: <code>ECFieldF2m</code> instance
* created using valid parameters<br>
* Expected: must return coefficient <code>a</code> which is equal
* to the one passed to the constructor; (both must refer
* the same object)
*/
public final void testGetA() {
ECFieldF2m f = new ECFieldF2m(5);
BigInteger a = BigInteger.valueOf(5L);
BigInteger b = BigInteger.valueOf(19L);
EllipticCurve c = new EllipticCurve(f, a, b);
assertEquals(a, c.getA());
assertSame(a, c.getA());
}
/**
* Test for <code>getB()</code> method<br>
* Assertion: returns coefficient <code>b</code><br>
* Test preconditions: <code>ECFieldF2m</code> instance
* created using valid parameters<br>
* Expected: must return coefficient <code>b</code> which is equal
* to the one passed to the constructor; (both must refer
* the same object)
*/
public final void testGetB() {
ECFieldF2m f = new ECFieldF2m(5);
BigInteger a = BigInteger.valueOf(5L);
BigInteger b = BigInteger.valueOf(19L);
EllipticCurve c = new EllipticCurve(f, a, b);
assertEquals(b, c.getB());
assertSame(b, c.getB());
}