what-the-bank/sources/org/bouncycastle/math/ec/custom/sec/SecT233Field.java

276 lines
9.7 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.math.ec.custom.sec;
import io.flutter.embedding.android.KeyboardMap;
import java.math.BigInteger;
import org.bouncycastle.crypto.tls.CipherSuite;
import org.bouncycastle.math.raw.Interleave;
import org.bouncycastle.math.raw.Nat256;
/* loaded from: classes6.dex */
public class SecT233Field {
private static final long M41 = 2199023255551L;
private static final long M59 = 576460752303423487L;
public static int trace(long[] jArr) {
return ((int) (jArr[0] ^ (jArr[2] >>> 31))) & 1;
}
public static void squareN(long[] jArr, int i, long[] jArr2) {
long[] createExt64 = Nat256.createExt64();
implSquare(jArr, createExt64);
while (true) {
reduce(createExt64, jArr2);
i--;
if (i <= 0) {
return;
} else {
implSquare(jArr2, createExt64);
}
}
}
public static void squareAddToExt(long[] jArr, long[] jArr2) {
long[] createExt64 = Nat256.createExt64();
implSquare(jArr, createExt64);
addExt(jArr2, createExt64, jArr2);
}
public static void square(long[] jArr, long[] jArr2) {
long[] createExt64 = Nat256.createExt64();
implSquare(jArr, createExt64);
reduce(createExt64, jArr2);
}
public static void sqrt(long[] jArr, long[] jArr2) {
long unshuffle = Interleave.unshuffle(jArr[0]);
long unshuffle2 = Interleave.unshuffle(jArr[1]);
long j = (unshuffle >>> 32) | (unshuffle2 & (-4294967296L));
long unshuffle3 = Interleave.unshuffle(jArr[2]);
long unshuffle4 = Interleave.unshuffle(jArr[3]);
long j2 = (unshuffle3 >>> 32) | ((-4294967296L) & unshuffle4);
long j3 = j2 >>> 27;
long j4 = j2 ^ ((j >>> 27) | (j2 << 37));
long j5 = j ^ (j << 37);
long[] createExt64 = Nat256.createExt64();
int[] iArr = {32, 117, CipherSuite.TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA256};
int i = 3;
int i2 = 0;
while (i2 < i) {
int i3 = iArr[i2];
int i4 = i3 >>> 6;
int i5 = i3 & 63;
createExt64[i4] = createExt64[i4] ^ (j5 << i5);
int i6 = i4 + 1;
int[] iArr2 = iArr;
int i7 = -i5;
createExt64[i6] = createExt64[i6] ^ ((j4 << i5) | (j5 >>> i7));
int i8 = i4 + 2;
createExt64[i8] = createExt64[i8] ^ ((j3 << i5) | (j4 >>> i7));
i = 3;
int i9 = i4 + 3;
createExt64[i9] = createExt64[i9] ^ (j3 >>> i7);
i2++;
iArr = iArr2;
}
reduce(createExt64, jArr2);
jArr2[0] = ((unshuffle & KeyboardMap.kValueMask) | (unshuffle2 << 32)) ^ jArr2[0];
jArr2[1] = jArr2[1] ^ ((unshuffle3 & KeyboardMap.kValueMask) | (unshuffle4 << 32));
}
public static void reduce23(long[] jArr, int i) {
int i2 = i + 3;
long j = jArr[i2];
long j2 = j >>> 41;
jArr[i] = jArr[i] ^ j2;
int i3 = i + 1;
jArr[i3] = (j2 << 10) ^ jArr[i3];
jArr[i2] = j & M41;
}
public static void reduce(long[] jArr, long[] jArr2) {
long j = jArr[0];
long j2 = jArr[1];
long j3 = jArr[2];
long j4 = jArr[3];
long j5 = jArr[4];
long j6 = jArr[5];
long j7 = jArr[6];
long j8 = jArr[7];
long j9 = j6 ^ (j8 >>> 31);
long j10 = (j5 ^ ((j8 >>> 41) ^ (j8 << 33))) ^ (j7 >>> 31);
long j11 = ((j4 ^ (j8 << 23)) ^ ((j7 >>> 41) ^ (j7 << 33))) ^ (j9 >>> 31);
long j12 = j11 >>> 41;
jArr2[0] = (j ^ (j10 << 23)) ^ j12;
jArr2[1] = (j12 << 10) ^ ((j2 ^ (j9 << 23)) ^ ((j10 >>> 41) ^ (j10 << 33)));
jArr2[2] = (((j7 << 23) ^ j3) ^ ((j9 >>> 41) ^ (j9 << 33))) ^ (j10 >>> 31);
jArr2[3] = M41 & j11;
}
public static void multiplyAddToExt(long[] jArr, long[] jArr2, long[] jArr3) {
long[] createExt64 = Nat256.createExt64();
implMultiply(jArr, jArr2, createExt64);
addExt(jArr3, createExt64, jArr3);
}
public static void multiply(long[] jArr, long[] jArr2, long[] jArr3) {
long[] createExt64 = Nat256.createExt64();
implMultiply(jArr, jArr2, createExt64);
reduce(createExt64, jArr3);
}
public static void invert(long[] jArr, long[] jArr2) {
if (Nat256.isZero64(jArr)) {
throw new IllegalStateException();
}
long[] create64 = Nat256.create64();
long[] create642 = Nat256.create64();
square(jArr, create64);
multiply(create64, jArr, create64);
square(create64, create64);
multiply(create64, jArr, create64);
squareN(create64, 3, create642);
multiply(create642, create64, create642);
square(create642, create642);
multiply(create642, jArr, create642);
squareN(create642, 7, create64);
multiply(create64, create642, create64);
squareN(create64, 14, create642);
multiply(create642, create64, create642);
square(create642, create642);
multiply(create642, jArr, create642);
squareN(create642, 29, create64);
multiply(create64, create642, create64);
squareN(create64, 58, create642);
multiply(create642, create64, create642);
squareN(create642, 116, create64);
multiply(create64, create642, create64);
square(create64, jArr2);
}
protected static void implSquare(long[] jArr, long[] jArr2) {
Interleave.expand64To128(jArr[0], jArr2, 0);
Interleave.expand64To128(jArr[1], jArr2, 2);
Interleave.expand64To128(jArr[2], jArr2, 4);
jArr2[6] = Interleave.expand32to64((int) jArr[3]);
jArr2[7] = Interleave.expand16to32((int) (r0 >>> 32)) & KeyboardMap.kValueMask;
}
protected static void implMulwAcc(long j, long j2, long[] jArr, int i) {
long j3 = j2 << 1;
long j4 = j3 ^ j2;
long j5 = j2 << 2;
long j6 = j4 << 1;
long[] jArr2 = {0, j2, j3, j4, j5, j5 ^ j2, j6, j6 ^ j2};
int i2 = (int) j;
long j7 = (jArr2[(i2 >>> 3) & 7] << 3) ^ jArr2[i2 & 7];
long j8 = 0;
int i3 = 54;
do {
int i4 = (int) (j >>> i3);
long j9 = jArr2[i4 & 7] ^ (jArr2[(i4 >>> 3) & 7] << 3);
j7 ^= j9 << i3;
j8 ^= j9 >>> (-i3);
i3 -= 6;
} while (i3 > 0);
jArr[i] = jArr[i] ^ (M59 & j7);
int i5 = i + 1;
jArr[i5] = jArr[i5] ^ ((j7 >>> 59) ^ (j8 << 5));
}
protected static void implMultiply(long[] jArr, long[] jArr2, long[] jArr3) {
long[] jArr4 = new long[4];
long[] jArr5 = new long[4];
implExpand(jArr, jArr4);
implExpand(jArr2, jArr5);
implMulwAcc(jArr4[0], jArr5[0], jArr3, 0);
implMulwAcc(jArr4[1], jArr5[1], jArr3, 1);
implMulwAcc(jArr4[2], jArr5[2], jArr3, 2);
implMulwAcc(jArr4[3], jArr5[3], jArr3, 3);
for (int i = 5; i > 0; i--) {
jArr3[i] = jArr3[i] ^ jArr3[i - 1];
}
implMulwAcc(jArr4[0] ^ jArr4[1], jArr5[0] ^ jArr5[1], jArr3, 1);
implMulwAcc(jArr4[2] ^ jArr4[3], jArr5[2] ^ jArr5[3], jArr3, 3);
for (int i2 = 7; i2 > 1; i2--) {
jArr3[i2] = jArr3[i2] ^ jArr3[i2 - 2];
}
long j = jArr4[0] ^ jArr4[2];
long j2 = jArr4[1] ^ jArr4[3];
long j3 = jArr5[0] ^ jArr5[2];
long j4 = jArr5[1] ^ jArr5[3];
implMulwAcc(j ^ j2, j3 ^ j4, jArr3, 3);
long[] jArr6 = new long[3];
implMulwAcc(j, j3, jArr6, 0);
implMulwAcc(j2, j4, jArr6, 1);
long j5 = jArr6[0];
long j6 = jArr6[1];
long j7 = jArr6[2];
jArr3[2] = jArr3[2] ^ j5;
jArr3[3] = (j5 ^ j6) ^ jArr3[3];
jArr3[4] = jArr3[4] ^ (j7 ^ j6);
jArr3[5] = jArr3[5] ^ j7;
implCompactExt(jArr3);
}
protected static void implExpand(long[] jArr, long[] jArr2) {
long j = jArr[0];
long j2 = jArr[1];
long j3 = jArr[2];
long j4 = jArr[3];
jArr2[0] = j & M59;
jArr2[1] = ((j >>> 59) ^ (j2 << 5)) & M59;
jArr2[2] = ((j2 >>> 54) ^ (j3 << 10)) & M59;
jArr2[3] = (j3 >>> 49) ^ (j4 << 15);
}
protected static void implCompactExt(long[] jArr) {
long j = jArr[0];
long j2 = jArr[1];
long j3 = jArr[2];
long j4 = jArr[3];
long j5 = jArr[4];
long j6 = jArr[5];
long j7 = jArr[6];
long j8 = jArr[7];
jArr[0] = j ^ (j2 << 59);
jArr[1] = (j2 >>> 5) ^ (j3 << 54);
jArr[2] = (j3 >>> 10) ^ (j4 << 49);
jArr[3] = (j4 >>> 15) ^ (j5 << 44);
jArr[4] = (j5 >>> 20) ^ (j6 << 39);
jArr[5] = (j6 >>> 25) ^ (j7 << 34);
jArr[6] = (j7 >>> 30) ^ (j8 << 29);
jArr[7] = j8 >>> 35;
}
public static long[] fromBigInteger(BigInteger bigInteger) {
long[] fromBigInteger64 = Nat256.fromBigInteger64(bigInteger);
reduce23(fromBigInteger64, 0);
return fromBigInteger64;
}
public static void addOne(long[] jArr, long[] jArr2) {
jArr2[0] = jArr[0] ^ 1;
jArr2[1] = jArr[1];
jArr2[2] = jArr[2];
jArr2[3] = jArr[3];
}
public static void addExt(long[] jArr, long[] jArr2, long[] jArr3) {
jArr3[0] = jArr[0] ^ jArr2[0];
jArr3[1] = jArr[1] ^ jArr2[1];
jArr3[2] = jArr[2] ^ jArr2[2];
jArr3[3] = jArr[3] ^ jArr2[3];
jArr3[4] = jArr[4] ^ jArr2[4];
jArr3[5] = jArr[5] ^ jArr2[5];
jArr3[6] = jArr[6] ^ jArr2[6];
jArr3[7] = jArr2[7] ^ jArr[7];
}
public static void add(long[] jArr, long[] jArr2, long[] jArr3) {
jArr3[0] = jArr[0] ^ jArr2[0];
jArr3[1] = jArr[1] ^ jArr2[1];
jArr3[2] = jArr[2] ^ jArr2[2];
jArr3[3] = jArr2[3] ^ jArr[3];
}
}