1d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootpackage org.bouncycastle.math.ec.custom.sec; 2d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 3d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootimport java.math.BigInteger; 4d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 5d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootimport org.bouncycastle.math.raw.Nat; 6d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootimport org.bouncycastle.math.raw.Nat192; 7d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 8d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Rootpublic class SecP192K1Field 9d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root{ 10d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root // 2^192 - 2^32 - 2^12 - 2^8 - 2^7 - 2^6 - 2^3 - 1 11d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root static final int[] P = new int[]{ 0xFFFFEE37, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; 12d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root static final int[] PExt = new int[]{ 0x013C4FD1, 0x00002392, 0x00000001, 0x00000000, 0x00000000, 13d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 0x00000000, 0xFFFFDC6E, 0xFFFFFFFD, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; 14d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root private static final int[] PExtInv = new int[]{ 0xFEC3B02F, 0xFFFFDC6D, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 15d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 0xFFFFFFFF, 0x00002391, 0x00000002 }; 16d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root private static final int P5 = 0xFFFFFFFF; 17d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root private static final int PExt11 = 0xFFFFFFFF; 18d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root private static final int PInv33 = 0x11C9; 19d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 20d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void add(int[] x, int[] y, int[] z) 21d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 22d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat192.add(x, y, z); 23d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0 || (z[5] == P5 && Nat192.gte(z, P))) 24d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 25d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.add33To(6, PInv33, z); 26d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 27d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 28d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 29d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void addExt(int[] xx, int[] yy, int[] zz) 30d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 31d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat.add(12, xx, yy, zz); 32d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0 || (zz[11] == PExt11 && Nat.gte(12, zz, PExt))) 33d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 34d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (Nat.addTo(PExtInv.length, PExtInv, zz) != 0) 35d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 36d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.incAt(12, zz, PExtInv.length); 37d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 38d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 39d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 40d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 41d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void addOne(int[] x, int[] z) 42d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 43d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat.inc(6, x, z); 44d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0 || (z[5] == P5 && Nat192.gte(z, P))) 45d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 46d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.add33To(6, PInv33, z); 47d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 48d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 49d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 50d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static int[] fromBigInteger(BigInteger x) 51d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 52d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int[] z = Nat192.fromBigInteger(x); 53d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (z[5] == P5 && Nat192.gte(z, P)) 54d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 55d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.subFrom(P, z); 56d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 57d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root return z; 58d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 59d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 60d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void half(int[] x, int[] z) 61d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 62d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if ((x[0] & 1) == 0) 63d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 64d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.shiftDownBit(6, x, 0, z); 65d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 66d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root else 67d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 68d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat192.add(x, P, z); 69d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.shiftDownBit(6, z, c); 70d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 71d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 72d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 73d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void multiply(int[] x, int[] y, int[] z) 74d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 75d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int[] tt = Nat192.createExt(); 76d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.mul(x, y, tt); 77d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root reduce(tt, z); 78d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 79d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 80d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void multiplyAddToExt(int[] x, int[] y, int[] zz) 81d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 82d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat192.mulAddTo(x, y, zz); 83d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0 || (zz[11] == PExt11 && Nat.gte(12, zz, PExt))) 84d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 85d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (Nat.addTo(PExtInv.length, PExtInv, zz) != 0) 86d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 87d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.incAt(12, zz, PExtInv.length); 88d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 89d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 90d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 91d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 92d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void negate(int[] x, int[] z) 93d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 94d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (Nat192.isZero(x)) 95d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 96d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.zero(z); 97d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 98d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root else 99d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 100d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.sub(P, x, z); 101d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 102d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 103d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 104d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void reduce(int[] xx, int[] z) 105d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 106d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root long cc = Nat192.mul33Add(PInv33, xx, 6, xx, 0, z, 0); 107d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat192.mul33DWordAdd(PInv33, cc, z, 0); 108d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 109d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root // assert c == 0L || c == 1L; 110d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 111d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0 || (z[5] == P5 && Nat192.gte(z, P))) 112d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 113d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.add33To(6, PInv33, z); 114d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 115d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 116d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 117d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void reduce32(int x, int[] z) 118d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 119d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if ((x != 0 && Nat192.mul33WordAdd(PInv33, x, z, 0) != 0) 120d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root || (z[5] == P5 && Nat192.gte(z, P))) 121d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 122d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.add33To(6, PInv33, z); 123d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 124d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 125d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 126d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void square(int[] x, int[] z) 127d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 128d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int[] tt = Nat192.createExt(); 129d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.square(x, tt); 130d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root reduce(tt, z); 131d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 132d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 133d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void squareN(int[] x, int n, int[] z) 134d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 135d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root// assert n > 0; 136d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 137d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int[] tt = Nat192.createExt(); 138d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.square(x, tt); 139d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root reduce(tt, z); 140d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 141d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root while (--n > 0) 142d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 143d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat192.square(z, tt); 144d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root reduce(tt, z); 145d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 146d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 147d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 148d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void subtract(int[] x, int[] y, int[] z) 149d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 150d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat192.sub(x, y, z); 151d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0) 152d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 153d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.sub33From(6, PInv33, z); 154d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 155d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 156d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 157d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void subtractExt(int[] xx, int[] yy, int[] zz) 158d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 159d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat.sub(12, xx, yy, zz); 160d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0) 161d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 162d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (Nat.subFrom(PExtInv.length, PExtInv, zz) != 0) 163d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 164d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.decAt(12, zz, PExtInv.length); 165d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 166d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 167d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 168d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root 169d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root public static void twice(int[] x, int[] z) 170d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 171d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root int c = Nat.shiftUpBit(6, x, 0, z); 172d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root if (c != 0 || (z[5] == P5 && Nat192.gte(z, P))) 173d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root { 174d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root Nat.add33To(6, PInv33, z); 175d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 176d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root } 177d001700a15b8bd733ae344c1fc315b97c43c6590Kenny Root} 178