what-the-bank/sources/org/bouncycastle/math/ec/tools/TraceOptimizer.java

96 lines
3.7 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.math.ec.tools;
import java.io.PrintStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.TreeSet;
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.util.Integers;
/* loaded from: classes6.dex */
public class TraceOptimizer {
private static final BigInteger ONE = BigInteger.valueOf(1);
private static final SecureRandom R = new SecureRandom();
public static void printNonZeroTraceBits(X9ECParameters x9ECParameters) {
if (!ECAlgorithms.isF2mCurve(x9ECParameters.getCurve())) {
throw new IllegalArgumentException("Trace only defined over characteristic-2 fields");
}
implPrintNonZeroTraceBits(x9ECParameters);
}
public static void main(String[] strArr) {
TreeSet<String> treeSet = new TreeSet(enumToList(ECNamedCurveTable.getNames()));
treeSet.addAll(enumToList(CustomNamedCurves.getNames()));
for (String str : treeSet) {
X9ECParameters byName = CustomNamedCurves.getByName(str);
if (byName == null) {
byName = ECNamedCurveTable.getByName(str);
}
if (byName != null && ECAlgorithms.isF2mCurve(byName.getCurve())) {
PrintStream printStream = System.out;
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.append(":");
printStream.print(sb.toString());
implPrintNonZeroTraceBits(byName);
}
}
}
public static void implPrintNonZeroTraceBits(X9ECParameters x9ECParameters) {
ECCurve curve = x9ECParameters.getCurve();
int fieldSize = curve.getFieldSize();
ArrayList arrayList = new ArrayList();
for (int i = 0; i < fieldSize; i++) {
if (calculateTrace(curve.fromBigInteger(ONE.shiftLeft(i))) != 0) {
arrayList.add(Integers.valueOf(i));
System.out.print(" ".concat(String.valueOf(i)));
}
}
System.out.println();
for (int i2 = 0; i2 < 1000; i2++) {
BigInteger bigInteger = new BigInteger(fieldSize, R);
int calculateTrace = calculateTrace(curve.fromBigInteger(bigInteger));
int i3 = 0;
for (int i4 = 0; i4 < arrayList.size(); i4++) {
if (bigInteger.testBit(((Integer) arrayList.get(i4)).intValue())) {
i3 ^= 1;
}
}
if (calculateTrace != i3) {
throw new IllegalStateException("Optimized-trace sanity check failed");
}
}
}
private static ArrayList enumToList(Enumeration enumeration) {
ArrayList arrayList = new ArrayList();
while (enumeration.hasMoreElements()) {
arrayList.add(enumeration.nextElement());
}
return arrayList;
}
private static int calculateTrace(ECFieldElement eCFieldElement) {
int fieldSize = eCFieldElement.getFieldSize();
ECFieldElement eCFieldElement2 = eCFieldElement;
for (int i = 1; i < fieldSize; i++) {
eCFieldElement2 = eCFieldElement2.square();
eCFieldElement = eCFieldElement.add(eCFieldElement2);
}
BigInteger bigInteger = eCFieldElement.toBigInteger();
if (bigInteger.bitLength() <= 1) {
return bigInteger.intValue();
}
throw new IllegalStateException();
}
}