diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-26 20:16:31 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-02-26 20:16:31 +0400 |
commit | 20c87e4c916c3d2d996d2221668ce60724a4b731 (patch) | |
tree | bac74506689fbb1dfd207252bdea6bd6f7cb459b /core/src/main/java/org | |
parent | 2c7db5b33dbd68a503684d66949e1d236150b3f5 (diff) |
Round out the Nat192 methods
Use Nat*.Copy methods in fields
Diffstat (limited to 'core/src/main/java/org')
7 files changed, 275 insertions, 20 deletions
diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat192.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat192.java index 03f2814d..802f20b4 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat192.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/Nat192.java @@ -82,27 +82,56 @@ public abstract class Nat192 return (int)c; } - public static int addToExt(int[] x, int xOff, int[] zz, int zzOff) + public static int addTo(int[] x, int xOff, int[] z, int zOff, int cIn) + { + long c = cIn & M; + c += (x[xOff + 0] & M) + (z[zOff + 0] & M); + z[zOff + 0] = (int)c; + c >>>= 32; + c += (x[xOff + 1] & M) + (z[zOff + 1] & M); + z[zOff + 1] = (int)c; + c >>>= 32; + c += (x[xOff + 2] & M) + (z[zOff + 2] & M); + z[zOff + 2] = (int)c; + c >>>= 32; + c += (x[xOff + 3] & M) + (z[zOff + 3] & M); + z[zOff + 3] = (int)c; + c >>>= 32; + c += (x[xOff + 4] & M) + (z[zOff + 4] & M); + z[zOff + 4] = (int)c; + c >>>= 32; + c += (x[xOff + 5] & M) + (z[zOff + 5] & M); + z[zOff + 5] = (int)c; + c >>>= 32; + return (int)c; + } + + public static int addToEachOther(int[] u, int uOff, int[] v, int vOff) { - // assert zzOff <= 6; long c = 0; - c += (x[xOff + 0] & M) + (zz[zzOff + 0] & M); - zz[zzOff + 0] = (int)c; + c += (u[uOff + 0] & M) + (v[vOff + 0] & M); + u[uOff + 0] = (int)c; + v[vOff + 0] = (int)c; c >>>= 32; - c += (x[xOff + 1] & M) + (zz[zzOff + 1] & M); - zz[zzOff + 1] = (int)c; + c += (u[uOff + 1] & M) + (v[vOff + 1] & M); + u[uOff + 1] = (int)c; + v[vOff + 1] = (int)c; c >>>= 32; - c += (x[xOff + 2] & M) + (zz[zzOff + 2] & M); - zz[zzOff + 2] = (int)c; + c += (u[uOff + 2] & M) + (v[vOff + 2] & M); + u[uOff + 2] = (int)c; + v[vOff + 2] = (int)c; c >>>= 32; - c += (x[xOff + 3] & M) + (zz[zzOff + 3] & M); - zz[zzOff + 3] = (int)c; + c += (u[uOff + 3] & M) + (v[vOff + 3] & M); + u[uOff + 3] = (int)c; + v[vOff + 3] = (int)c; c >>>= 32; - c += (x[xOff + 4] & M) + (zz[zzOff + 4] & M); - zz[zzOff + 4] = (int)c; + c += (u[uOff + 4] & M) + (v[vOff + 4] & M); + u[uOff + 4] = (int)c; + v[vOff + 4] = (int)c; c >>>= 32; - c += (x[xOff + 5] & M) + (zz[zzOff + 5] & M); - zz[zzOff + 5] = (int)c; + c += (u[uOff + 5] & M) + (v[vOff + 5] & M); + u[uOff + 5] = (int)c; + v[vOff + 5] = (int)c; c >>>= 32; return (int)c; } @@ -125,6 +154,16 @@ public abstract class Nat192 return c == 0 ? 0 : incExt(zz, zzOff + 1); } + public static void copy(int[] x, int[] z) + { + z[0] = x[0]; + z[1] = x[1]; + z[2] = x[2]; + z[3] = x[3]; + z[4] = x[4]; + z[5] = x[5]; + } + public static int[] create() { return new int[6]; @@ -161,6 +200,20 @@ public abstract class Nat192 return -1; } + public static boolean diff(int[] x, int xOff, int[] y, int yOff, int[] z, int zOff) + { + boolean pos = gte(x, xOff, y, yOff); + if (pos) + { + sub(x, xOff, y, yOff, z, zOff); + } + else + { + sub(y, yOff, x, xOff, z, zOff); + } + return pos; + } + public static int[] fromBigInteger(BigInteger x) { if (x.signum() < 0 || x.bitLength() > 192) @@ -207,6 +260,20 @@ public abstract class Nat192 return true; } + public static boolean gte(int[] x, int xOff, int[] y, int yOff) + { + for (int i = 5; i >= 0; --i) + { + int x_i = x[xOff + i] ^ Integer.MIN_VALUE; + int y_i = y[yOff + i] ^ Integer.MIN_VALUE; + if (x_i < y_i) + return false; + if (x_i > y_i) + return true; + } + return true; + } + public static boolean gteExt(int[] xx, int[] yy) { for (int i = 11; i >= 0; --i) @@ -344,6 +411,64 @@ public abstract class Nat192 } } + public static void mul(int[] x, int xOff, int[] y, int yOff, int[] zz, int zzOff) + { + long y_0 = y[yOff + 0] & M; + long y_1 = y[yOff + 1] & M; + long y_2 = y[yOff + 2] & M; + long y_3 = y[yOff + 3] & M; + long y_4 = y[yOff + 4] & M; + long y_5 = y[yOff + 5] & M; + + { + long c = 0, x_0 = x[xOff + 0] & M; + c += x_0 * y_0; + zz[zzOff + 0] = (int)c; + c >>>= 32; + c += x_0 * y_1; + zz[zzOff + 1] = (int)c; + c >>>= 32; + c += x_0 * y_2; + zz[zzOff + 2] = (int)c; + c >>>= 32; + c += x_0 * y_3; + zz[zzOff + 3] = (int)c; + c >>>= 32; + c += x_0 * y_4; + zz[zzOff + 4] = (int)c; + c >>>= 32; + c += x_0 * y_5; + zz[zzOff + 5] = (int)c; + c >>>= 32; + zz[zzOff + 6] = (int)c; + } + + for (int i = 1; i < 6; ++i) + { + ++zzOff; + long c = 0, x_i = x[xOff + i] & M; + c += x_i * y_0 + (zz[zzOff + 0] & M); + zz[zzOff + 0] = (int)c; + c >>>= 32; + c += x_i * y_1 + (zz[zzOff + 1] & M); + zz[zzOff + 1] = (int)c; + c >>>= 32; + c += x_i * y_2 + (zz[zzOff + 2] & M); + zz[zzOff + 2] = (int)c; + c >>>= 32; + c += x_i * y_3 + (zz[zzOff + 3] & M); + zz[zzOff + 3] = (int)c; + c >>>= 32; + c += x_i * y_4 + (zz[zzOff + 4] & M); + zz[zzOff + 4] = (int)c; + c >>>= 32; + c += x_i * y_5 + (zz[zzOff + 5] & M); + zz[zzOff + 5] = (int)c; + c >>>= 32; + zz[zzOff + 6] = (int)c; + } + } + public static long mul33Add(int w, int[] x, int xOff, int[] y, int yOff, int[] z, int zOff) { // assert w >>> 31 == 0; @@ -534,6 +659,17 @@ public abstract class Nat192 return c >>> 31; } + public static int shiftUpBit(int[] x, int xOff, int xLen, int c) + { + for (int i = 0; i < xLen; ++i) + { + int next = x[xOff + i]; + x[xOff + i] = (next << 1) | (c >>> 31); + c = next; + } + return c >>> 31; + } + public static int shiftUpBit(int[] x, int c, int[] z) { for (int i = 0; i < 6; ++i) @@ -640,6 +776,101 @@ public abstract class Nat192 shiftUpBit(zz, 12, (int)x_0 << 31); } + public static void square(int[] x, int xOff, int[] zz, int zzOff) + { + long x_0 = x[xOff + 0] & M; + long zz_1; + + { + int c = 0, i = 5, j = 12; + do + { + long xVal = (x[xOff + i--] & M); + long p = xVal * xVal; + zz[zzOff + --j] = (c << 31) | (int)(p >>> 33); + zz[zzOff + --j] = (int)(p >>> 1); + c = (int)p; + } + while (i > 0); + + { + long p = x_0 * x_0; + zz_1 = ((c << 31) & M) | (p >>> 33); + zz[zzOff + 0] = (int)(p >>> 1); + } + } + + long x_1 = x[xOff + 1] & M; + long zz_2 = zz[zzOff + 2] & M; + + { + zz_1 += x_1 * x_0; + zz[zzOff + 1] = (int)zz_1; + zz_2 += zz_1 >>> 32; + } + + long x_2 = x[xOff + 2] & M; + long zz_3 = zz[zzOff + 3] & M; + long zz_4 = zz[zzOff + 4] & M; + { + zz_2 += x_2 * x_0; + zz[zzOff + 2] = (int)zz_2; + zz_3 += (zz_2 >>> 32) + x_2 * x_1; + zz_4 += zz_3 >>> 32; + zz_3 &= M; + } + + long x_3 = x[xOff + 3] & M; + long zz_5 = zz[zzOff + 5] & M; + long zz_6 = zz[zzOff + 6] & M; + { + zz_3 += x_3 * x_0; + zz[zzOff + 3] = (int)zz_3; + zz_4 += (zz_3 >>> 32) + x_3 * x_1; + zz_5 += (zz_4 >>> 32) + x_3 * x_2; + zz_4 &= M; + zz_6 += zz_5 >>> 32; + zz_5 &= M; + } + + long x_4 = x[xOff + 4] & M; + long zz_7 = zz[zzOff + 7] & M; + long zz_8 = zz[zzOff + 8] & M; + { + zz_4 += x_4 * x_0; + zz[zzOff + 4] = (int)zz_4; + zz_5 += (zz_4 >>> 32) + x_4 * x_1; + zz_6 += (zz_5 >>> 32) + x_4 * x_2; + zz_5 &= M; + zz_7 += (zz_6 >>> 32) + x_4 * x_3; + zz_6 &= M; + zz_8 += zz_7 >>> 32; + zz_7 &= M; + } + + long x_5 = x[xOff + 5] & M; + long zz_9 = zz[zzOff + 9] & M; + long zz_10 = zz[zzOff + 10] & M; + { + zz_5 += x_5 * x_0; + zz[zzOff + 5] = (int)zz_5; + zz_6 += (zz_5 >>> 32) + x_5 * x_1; + zz_7 += (zz_6 >>> 32) + x_5 * x_2; + zz_8 += (zz_7 >>> 32) + x_5 * x_3; + zz_9 += (zz_8 >>> 32) + x_5 * x_4; + zz_10 += zz_9 >>> 32; + } + + zz[zzOff + 6] = (int)zz_6; + zz[zzOff + 7] = (int)zz_7; + zz[zzOff + 8] = (int)zz_8; + zz[zzOff + 9] = (int)zz_9; + zz[zzOff + 10] = (int)zz_10; + zz[zzOff + 11] += (int)(zz_10 >>> 32); + + shiftUpBit(zz, zzOff, 12, (int)x_0 << 31); + } + public static int sub(int[] x, int[] y, int[] z) { long c = 0; @@ -664,6 +895,30 @@ public abstract class Nat192 return (int)c; } + public static int sub(int[] x, int xOff, int[] y, int yOff, int[] z, int zOff) + { + long c = 0; + c += (x[xOff + 0] & M) - (y[yOff + 0] & M); + z[zOff + 0] = (int)c; + c >>= 32; + c += (x[xOff + 1] & M) - (y[yOff + 1] & M); + z[zOff + 1] = (int)c; + c >>= 32; + c += (x[xOff + 2] & M) - (y[yOff + 2] & M); + z[zOff + 2] = (int)c; + c >>= 32; + c += (x[xOff + 3] & M) - (y[yOff + 3] & M); + z[zOff + 3] = (int)c; + c >>= 32; + c += (x[xOff + 4] & M) - (y[yOff + 4] & M); + z[zOff + 4] = (int)c; + c >>= 32; + c += (x[xOff + 5] & M) - (y[yOff + 5] & M); + z[zOff + 5] = (int)c; + c >>= 32; + return (int)c; + } + public static int subBothFrom(int[] x, int[] y, int[] z) { long c = 0; diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192K1Field.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192K1Field.java index f4f60d58..dcb8e0fb 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192K1Field.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192K1Field.java @@ -33,7 +33,7 @@ public class SecP192K1Field public static void addOne(int[] x, int[] z) { - System.arraycopy(x, 0, z, 0, 6); + Nat192.copy(x, z); int c = Nat192.inc(z, 0); if (c != 0 || (z[5] == P5 && Nat192.gte(z, P))) { diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192R1Field.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192R1Field.java index 6b5db1d9..b7e11471 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192R1Field.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP192R1Field.java @@ -33,7 +33,7 @@ public class SecP192R1Field public static void addOne(int[] x, int[] z) { - System.arraycopy(x, 0, z, 0, 6); + Nat192.copy(x, z); int c = Nat192.inc(z, 0); if (c != 0 || (z[5] == P5 && Nat192.gte(z, P))) { diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224K1Field.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224K1Field.java index d5cd4072..70895d51 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224K1Field.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224K1Field.java @@ -34,7 +34,7 @@ public class SecP224K1Field public static void addOne(int[] x, int[] z) { - System.arraycopy(x, 0, z, 0, 8); + Nat224.copy(x, z); int c = Nat224.inc(z, 0); if (c != 0 || (z[6] == P6 && Nat224.gte(z, P))) { diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224R1Field.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224R1Field.java index d828b71c..5bdece44 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224R1Field.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP224R1Field.java @@ -33,7 +33,7 @@ public class SecP224R1Field public static void addOne(int[] x, int[] z) { - System.arraycopy(x, 0, z, 0, 7); + Nat224.copy(x, z); int c = Nat224.inc(z, 0); if (c != 0 || (z[6] == P6 && Nat224.gte(z, P))) { diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Field.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Field.java index bf9ec919..4f16c5c6 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Field.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256K1Field.java @@ -35,7 +35,7 @@ public class SecP256K1Field public static void addOne(int[] x, int[] z) { - System.arraycopy(x, 0, z, 0, 8); + Nat256.copy(x, z); int c = Nat256.inc(z, 0); if (c != 0 || (z[7] == P7 && Nat256.gte(z, P))) { diff --git a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Field.java b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Field.java index 9fee3adf..3fd9e305 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Field.java +++ b/core/src/main/java/org/bouncycastle/math/ec/custom/sec/SecP256R1Field.java @@ -34,7 +34,7 @@ public class SecP256R1Field public static void addOne(int[] x, int[] z) { - System.arraycopy(x, 0, z, 0, 8); + Nat256.copy(x, z); int c = Nat256.inc(z, 0); if (c != 0 || (z[7] == P7 && Nat256.gte(z, P))) { |