diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-26 11:59:04 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-26 11:59:04 +0400 |
commit | 268020d0c4fd44de9fe05b268445711e8bc2a61f (patch) | |
tree | ebcaf3062fcc3b7ff953963f28dd7035d5847276 /core/src/main/java/org/bouncycastle/math | |
parent | de503d688701a3e989b40c48ce7624e58f3b2d2e (diff) |
Various minor cleanups noted during C# port
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
7 files changed, 61 insertions, 89 deletions
diff --git a/core/src/main/java/org/bouncycastle/math/ec/Nat.java b/core/src/main/java/org/bouncycastle/math/ec/Nat.java index fee1317d..c2bfdbfd 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/Nat.java +++ b/core/src/main/java/org/bouncycastle/math/ec/Nat.java @@ -32,18 +32,18 @@ public abstract class Nat return (int)c; } - public static int addDWord(int len, long x, int[] z, int zOff) - { - // assert zOff < (len - 2); - long c = x; - c += (z[zOff + 0] & M); - z[zOff + 0] = (int)c; - c >>>= 32; - c += (z[zOff + 1] & M); - z[zOff + 1] = (int)c; - c >>>= 32; - return c == 0 ? 0 : inc(len, z, zOff + 2); - } +// public static int addDWord(int len, long x, int[] z, int zOff) +// { +// // assert zOff < (len - 2); +// long c = x; +// c += (z[zOff + 0] & M); +// z[zOff + 0] = (int)c; +// c >>>= 32; +// c += (z[zOff + 1] & M); +// z[zOff + 1] = (int)c; +// c >>>= 32; +// return c == 0 ? 0 : inc(len, z, zOff + 2); +// } public static int addExt(int len, int[] xx, int[] yy, int[] zz) { @@ -129,7 +129,11 @@ public abstract class Nat { return x[0] & 1; } - int w = bit >>> 5; + int w = bit >> 5; + if ((w - Integer.MIN_VALUE) > x.length) + { + return 0; + } int b = bit & 31; return (x[w] >>> b) & 1; } @@ -208,11 +212,7 @@ public abstract class Nat public static boolean isZero(int len, int[] x) { - if (x[0] != 0) - { - return false; - } - for (int i = 1; i < len; ++i) + for (int i = 0; i < len; ++i) { if (x[i] != 0) { @@ -224,12 +224,8 @@ public abstract class Nat public static boolean isZeroExt(int len, int[] xx) { - if (xx[0] != 0) - { - return false; - } int extLen = len << 1; - for (int i = 1; i < extLen; ++i) + for (int i = 0; i < extLen; ++i) { if (xx[i] != 0) { @@ -430,17 +426,17 @@ public abstract class Nat return (int)c; } - public static int subDWord(int len, long x, int[] z) - { - long c = -x; - c += (z[0] & M); - z[0] = (int)c; - c >>= 32; - c += (z[1] & M); - z[1] = (int)c; - c >>= 32; - return c == 0 ? 0 : dec(len, z, 2); - } +// public static int subDWord(int len, long x, int[] z) +// { +// long c = -x; +// c += (z[0] & M); +// z[0] = (int)c; +// c >>= 32; +// c += (z[1] & M); +// z[1] = (int)c; +// c >>= 32; +// return c == 0 ? 0 : dec(len, z, 2); +// } public static int subExt(int len, int[] xx, int[] yy, int[] zz) { diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat256.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat256.java index c2de5113..0aa2d217 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat256.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat256.java @@ -68,6 +68,7 @@ public abstract class Nat256 return (int)c; } + // TODO Re-write to allow full range for x? public static int addDWord(long x, int[] z, int zOff) { // assert zOff < 6; @@ -262,11 +263,7 @@ public abstract class Nat256 public static boolean isZero(int[] x) { - if (x[0] != 0) - { - return false; - } - for (int i = 1; i < 8; ++i) + for (int i = 0; i < 8; ++i) { if (x[i] != 0) { @@ -278,11 +275,7 @@ public abstract class Nat256 public static boolean isZeroExt(int[] xx) { - if (xx[0] != 0) - { - return false; - } - for (int i = 1; i < 16; ++i) + for (int i = 0; i < 16; ++i) { if (xx[i] != 0) { @@ -379,21 +372,6 @@ public abstract class Nat256 return (int)c; } - public static int squareWordAddExt(int[] x, int xPos, int[] zz) - { - // assert xPos > 0 && xPos < 8; - long c = 0, xVal = x[xPos] & M; - int i = 0; - do - { - c += xVal * (x[i] & M) + (zz[xPos + i] & M); - zz[xPos + i] = (int)c; - c >>>= 32; - } - while (++i < xPos); - return (int)c; - } - public static int mulWordDwordAdd(int x, long y, int[] z, int zOff) { // assert zOff < 5; @@ -612,17 +590,11 @@ public abstract class Nat256 zz[7] = (int)zz_7; zz_8 += (zz_7 >>> 32) + x_7 * x_1; zz_9 += (zz_8 >>> 32) + x_7 * x_2; - zz_8 &= M; zz_10 += (zz_9 >>> 32) + x_7 * x_3; - zz_9 &= M; zz_11 += (zz_10 >>> 32) + x_7 * x_4; - zz_10 &= M; zz_12 += (zz_11 >>> 32) + x_7 * x_5; - zz_11 &= M; zz_13 += (zz_12 >>> 32) + x_7 * x_6; - zz_12 &= M; zz_14 += zz_13 >>> 32; - zz_13 &= M; } zz[8] = (int)zz_8; @@ -637,6 +609,21 @@ public abstract class Nat256 shiftUpBit(zz, 16, (int)x_0 << 31); } + public static int squareWordAddExt(int[] x, int xPos, int[] zz) + { + // assert xPos > 0 && xPos < 8; + long c = 0, xVal = x[xPos] & M; + int i = 0; + do + { + c += xVal * (x[i] & M) + (zz[xPos + i] & M); + zz[xPos + i] = (int)c; + c >>>= 32; + } + while (++i < xPos); + return (int)c; + } + public static int sub(int[] x, int[] y, int[] z) { long c = 0; @@ -697,16 +684,17 @@ public abstract class Nat256 return (int)c; } + // TODO Re-write to allow full range for x? public static int subDWord(long x, int[] z) { - x = -x; - x += (z[0] & M); - z[0] = (int)x; - x >>= 32; - x += (z[1] & M); - z[1] = (int)x; - x >>= 32; - return x == 0 ? 0 : dec(z, 2); + long c = -x; + c += (z[0] & M); + z[0] = (int)c; + c >>= 32; + c += (z[1] & M); + z[1] = (int)c; + c >>= 32; + return c == 0 ? 0 : dec(z, 2); } public static int subExt(int[] xx, int[] yy, int[] zz) diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Curve.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Curve.java index bcdc70b5..5f5f17cf 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Curve.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Curve.java @@ -13,7 +13,6 @@ public class SecP256K1Curve extends ECCurve { public static final BigInteger q = new BigInteger(1, Hex.decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F")); - public static final BigInteger r = ECConstants.ONE.shiftLeft(256).subtract(q); private static final int SECP256K1_DEFAULT_COORDS = COORD_JACOBIAN; diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1FieldElement.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1FieldElement.java index a415fab6..57828917 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1FieldElement.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1FieldElement.java @@ -9,7 +9,6 @@ import org.bouncycastle.util.Arrays; public class SecP256K1FieldElement extends ECFieldElement { public static final BigInteger Q = SecP256K1Curve.q; - public static final BigInteger Qr = SecP256K1Curve.r; protected int[] x; @@ -23,7 +22,7 @@ public class SecP256K1FieldElement extends ECFieldElement this.x = SecP256K1Field.fromBigInteger(x); } - protected SecP256K1FieldElement() + public SecP256K1FieldElement() { this.x = Nat256.create(); } @@ -63,11 +62,6 @@ public class SecP256K1FieldElement extends ECFieldElement return Q.bitLength(); } - public BigInteger getQ() - { - return Q; - } - public ECFieldElement add(ECFieldElement b) { int[] z = Nat256.create(); diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Point.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Point.java index 15820b8a..f1d07cad 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Point.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Point.java @@ -42,7 +42,7 @@ public class SecP256K1Point extends ECPoint { super(curve, x, y); - if ((x != null && y == null) || (x == null && y != null)) + if ((x == null) != (y == null)) { throw new IllegalArgumentException("Exactly one of the field elements is null"); } diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1FieldElement.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1FieldElement.java index c415e65e..03895cfb 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1FieldElement.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1FieldElement.java @@ -23,7 +23,7 @@ public class SecP256R1FieldElement extends ECFieldElement this.x = SecP256R1Field.fromBigInteger(x); } - protected SecP256R1FieldElement() + public SecP256R1FieldElement() { this.x = Nat256.create(); } @@ -63,11 +63,6 @@ public class SecP256R1FieldElement extends ECFieldElement return Q.bitLength(); } - public BigInteger getQ() - { - return Q; - } - public ECFieldElement add(ECFieldElement b) { int[] z = Nat256.create(); diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Point.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Point.java index ca6ae008..7ce96c45 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Point.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Point.java @@ -42,7 +42,7 @@ public class SecP256R1Point extends ECPoint { super(curve, x, y); - if ((x != null && y == null) || (x == null && y != null)) + if ((x == null) != (y == null)) { throw new IllegalArgumentException("Exactly one of the field elements is null"); } |