package org.bouncycastle.math.ec; import java.math.BigInteger; import java.util.Hashtable; import java.util.Random; import org.bouncycastle.math.ec.ECFieldElement; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.math.ec.endo.ECEndomorphism; import org.bouncycastle.math.ec.endo.GLVEndomorphism; import org.bouncycastle.math.field.FiniteField; import org.bouncycastle.math.field.FiniteFields; import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.Integers; /* loaded from: classes6.dex */ public abstract class ECCurve { public static final int COORD_AFFINE = 0; public static final int COORD_HOMOGENEOUS = 1; public static final int COORD_JACOBIAN = 2; public static final int COORD_JACOBIAN_CHUDNOVSKY = 3; public static final int COORD_JACOBIAN_MODIFIED = 4; public static final int COORD_LAMBDA_AFFINE = 5; public static final int COORD_LAMBDA_PROJECTIVE = 6; public static final int COORD_SKEWED = 7; public ECFieldElement a; public ECFieldElement b; public BigInteger cofactor; protected FiniteField field; public BigInteger order; public int coord = 0; protected ECEndomorphism endomorphism = null; protected ECMultiplier multiplier = null; protected abstract ECCurve cloneCurve(); /* JADX INFO: Access modifiers changed from: protected */ public abstract ECPoint createRawPoint(ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, boolean z); /* JADX INFO: Access modifiers changed from: protected */ public abstract ECPoint createRawPoint(ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, ECFieldElement[] eCFieldElementArr, boolean z); protected abstract ECPoint decompressPoint(int i, BigInteger bigInteger); public abstract ECFieldElement fromBigInteger(BigInteger bigInteger); public abstract int getFieldSize(); public abstract ECPoint getInfinity(); public abstract boolean isValidFieldElement(BigInteger bigInteger); public boolean supportsCoordinateSystem(int i) { return i == 0; } public ECPoint validatePoint(BigInteger bigInteger, BigInteger bigInteger2, boolean z) { ECPoint createPoint = createPoint(bigInteger, bigInteger2, z); if (createPoint.isValid()) { return createPoint; } throw new IllegalArgumentException("Invalid point coordinates"); } public ECPoint validatePoint(BigInteger bigInteger, BigInteger bigInteger2) { ECPoint createPoint = createPoint(bigInteger, bigInteger2); if (createPoint.isValid()) { return createPoint; } throw new IllegalArgumentException("Invalid point coordinates"); } public void setPreCompInfo(ECPoint eCPoint, String str, PreCompInfo preCompInfo) { checkPoint(eCPoint); synchronized (eCPoint) { Hashtable hashtable = eCPoint.preCompTable; if (hashtable == null) { hashtable = new Hashtable(4); eCPoint.preCompTable = hashtable; } hashtable.put(str, preCompInfo); } } public void normalizeAll(ECPoint[] eCPointArr, int i, int i2, ECFieldElement eCFieldElement) { checkPoints(eCPointArr, i, i2); int coordinateSystem = getCoordinateSystem(); if (coordinateSystem == 0 || coordinateSystem == 5) { if (eCFieldElement != null) { throw new IllegalArgumentException("'iso' not valid for affine coordinates"); } return; } ECFieldElement[] eCFieldElementArr = new ECFieldElement[i2]; int[] iArr = new int[i2]; int i3 = 0; for (int i4 = 0; i4 < i2; i4++) { int i5 = i + i4; ECPoint eCPoint = eCPointArr[i5]; if (eCPoint != null && (eCFieldElement != null || !eCPoint.isNormalized())) { eCFieldElementArr[i3] = eCPoint.getZCoord(0); iArr[i3] = i5; i3++; } } if (i3 == 0) { return; } ECAlgorithms.montgomeryTrick(eCFieldElementArr, 0, i3, eCFieldElement); for (int i6 = 0; i6 < i3; i6++) { int i7 = iArr[i6]; eCPointArr[i7] = eCPointArr[i7].normalize(eCFieldElementArr[i6]); } } public void normalizeAll(ECPoint[] eCPointArr) { normalizeAll(eCPointArr, 0, eCPointArr.length, null); } public ECPoint importPoint(ECPoint eCPoint) { if (this == eCPoint.getCurve()) { return eCPoint; } if (eCPoint.isInfinity()) { return getInfinity(); } ECPoint normalize = eCPoint.normalize(); return validatePoint(normalize.getXCoord().toBigInteger(), normalize.getYCoord().toBigInteger(), normalize.withCompression); } public int hashCode() { return (getField().hashCode() ^ Integers.rotateLeft(getA().toBigInteger().hashCode(), 8)) ^ Integers.rotateLeft(getB().toBigInteger().hashCode(), 16); } public PreCompInfo getPreCompInfo(ECPoint eCPoint, String str) { PreCompInfo preCompInfo; checkPoint(eCPoint); synchronized (eCPoint) { Hashtable hashtable = eCPoint.preCompTable; preCompInfo = hashtable == null ? null : (PreCompInfo) hashtable.get(str); } return preCompInfo; } /* loaded from: classes6.dex */ public static class F2m extends AbstractF2m { private static final int F2M_DEFAULT_COORDS = 6; private ECPoint.F2m infinity; private int k1; private int k2; private int k3; private int m; @Override // org.bouncycastle.math.ec.ECCurve public boolean supportsCoordinateSystem(int i) { return i == 0 || i == 1 || i == 6; } public boolean isTrinomial() { return this.k2 == 0 && this.k3 == 0; } public BigInteger getN() { return this.order; } public int getM() { return this.m; } public int getK3() { return this.k3; } public int getK2() { return this.k2; } public int getK1() { return this.k1; } @Override // org.bouncycastle.math.ec.ECCurve public ECPoint getInfinity() { return this.infinity; } public BigInteger getH() { return this.cofactor; } @Override // org.bouncycastle.math.ec.ECCurve public int getFieldSize() { return this.m; } @Override // org.bouncycastle.math.ec.ECCurve public ECFieldElement fromBigInteger(BigInteger bigInteger) { return new ECFieldElement.F2m(this.m, this.k1, this.k2, this.k3, bigInteger); } @Override // org.bouncycastle.math.ec.ECCurve protected ECPoint createRawPoint(ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, ECFieldElement[] eCFieldElementArr, boolean z) { return new ECPoint.F2m(this, eCFieldElement, eCFieldElement2, eCFieldElementArr, z); } @Override // org.bouncycastle.math.ec.ECCurve protected ECPoint createRawPoint(ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, boolean z) { return new ECPoint.F2m(this, eCFieldElement, eCFieldElement2, z); } @Override // org.bouncycastle.math.ec.ECCurve protected ECMultiplier createDefaultMultiplier() { return isKoblitz() ? new WTauNafMultiplier() : super.createDefaultMultiplier(); } @Override // org.bouncycastle.math.ec.ECCurve protected ECCurve cloneCurve() { return new F2m(this.m, this.k1, this.k2, this.k3, this.a, this.b, this.order, this.cofactor); } public F2m(int i, int i2, BigInteger bigInteger, BigInteger bigInteger2, BigInteger bigInteger3, BigInteger bigInteger4) { this(i, i2, 0, 0, bigInteger, bigInteger2, bigInteger3, bigInteger4); } public F2m(int i, int i2, BigInteger bigInteger, BigInteger bigInteger2) { this(i, i2, 0, 0, bigInteger, bigInteger2, (BigInteger) null, (BigInteger) null); } protected F2m(int i, int i2, int i3, int i4, ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, BigInteger bigInteger, BigInteger bigInteger2) { super(i, i2, i3, i4); this.m = i; this.k1 = i2; this.k2 = i3; this.k3 = i4; this.order = bigInteger; this.cofactor = bigInteger2; this.infinity = new ECPoint.F2m(this, null, null); this.a = eCFieldElement; this.b = eCFieldElement2; this.coord = 6; } public F2m(int i, int i2, int i3, int i4, BigInteger bigInteger, BigInteger bigInteger2, BigInteger bigInteger3, BigInteger bigInteger4) { super(i, i2, i3, i4); this.m = i; this.k1 = i2; this.k2 = i3; this.k3 = i4; this.order = bigInteger3; this.cofactor = bigInteger4; this.infinity = new ECPoint.F2m(this, null, null); this.a = fromBigInteger(bigInteger); this.b = fromBigInteger(bigInteger2); this.coord = 6; } public F2m(int i, int i2, int i3, int i4, BigInteger bigInteger, BigInteger bigInteger2) { this(i, i2, i3, i4, bigInteger, bigInteger2, (BigInteger) null, (BigInteger) null); } } public BigInteger getOrder() { return this.order; } public ECMultiplier getMultiplier() { ECMultiplier eCMultiplier; synchronized (this) { if (this.multiplier == null) { this.multiplier = createDefaultMultiplier(); } eCMultiplier = this.multiplier; } return eCMultiplier; } public FiniteField getField() { return this.field; } public ECEndomorphism getEndomorphism() { return this.endomorphism; } public int getCoordinateSystem() { return this.coord; } public BigInteger getCofactor() { return this.cofactor; } public ECFieldElement getB() { return this.b; } /* loaded from: classes6.dex */ public static class Fp extends AbstractFp { private static final int FP_DEFAULT_COORDS = 4; ECPoint.Fp infinity; BigInteger q; BigInteger r; @Override // org.bouncycastle.math.ec.ECCurve public boolean supportsCoordinateSystem(int i) { return i == 0 || i == 1 || i == 2 || i == 4; } @Override // org.bouncycastle.math.ec.ECCurve public ECPoint importPoint(ECPoint eCPoint) { int coordinateSystem; return (this == eCPoint.getCurve() || getCoordinateSystem() != 2 || eCPoint.isInfinity() || !((coordinateSystem = eCPoint.getCurve().getCoordinateSystem()) == 2 || coordinateSystem == 3 || coordinateSystem == 4)) ? super.importPoint(eCPoint) : new ECPoint.Fp(this, fromBigInteger(eCPoint.x.toBigInteger()), fromBigInteger(eCPoint.y.toBigInteger()), new ECFieldElement[]{fromBigInteger(eCPoint.zs[0].toBigInteger())}, eCPoint.withCompression); } public BigInteger getQ() { return this.q; } @Override // org.bouncycastle.math.ec.ECCurve public ECPoint getInfinity() { return this.infinity; } @Override // org.bouncycastle.math.ec.ECCurve public int getFieldSize() { return this.q.bitLength(); } @Override // org.bouncycastle.math.ec.ECCurve public ECFieldElement fromBigInteger(BigInteger bigInteger) { return new ECFieldElement.Fp(this.q, this.r, bigInteger); } @Override // org.bouncycastle.math.ec.ECCurve protected ECPoint createRawPoint(ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, ECFieldElement[] eCFieldElementArr, boolean z) { return new ECPoint.Fp(this, eCFieldElement, eCFieldElement2, eCFieldElementArr, z); } @Override // org.bouncycastle.math.ec.ECCurve protected ECPoint createRawPoint(ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, boolean z) { return new ECPoint.Fp(this, eCFieldElement, eCFieldElement2, z); } @Override // org.bouncycastle.math.ec.ECCurve protected ECCurve cloneCurve() { return new Fp(this.q, this.r, this.a, this.b, this.order, this.cofactor); } protected Fp(BigInteger bigInteger, BigInteger bigInteger2, ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2, BigInteger bigInteger3, BigInteger bigInteger4) { super(bigInteger); this.q = bigInteger; this.r = bigInteger2; this.infinity = new ECPoint.Fp(this, null, null); this.a = eCFieldElement; this.b = eCFieldElement2; this.order = bigInteger3; this.cofactor = bigInteger4; this.coord = 4; } protected Fp(BigInteger bigInteger, BigInteger bigInteger2, ECFieldElement eCFieldElement, ECFieldElement eCFieldElement2) { this(bigInteger, bigInteger2, eCFieldElement, eCFieldElement2, null, null); } public Fp(BigInteger bigInteger, BigInteger bigInteger2, BigInteger bigInteger3, BigInteger bigInteger4, BigInteger bigInteger5) { super(bigInteger); this.q = bigInteger; this.r = ECFieldElement.Fp.calculateResidue(bigInteger); this.infinity = new ECPoint.Fp(this, null, null); this.a = fromBigInteger(bigInteger2); this.b = fromBigInteger(bigInteger3); this.order = bigInteger4; this.cofactor = bigInteger5; this.coord = 4; } public Fp(BigInteger bigInteger, BigInteger bigInteger2, BigInteger bigInteger3) { this(bigInteger, bigInteger2, bigInteger3, null, null); } } public ECFieldElement getA() { return this.a; } public boolean equals(ECCurve eCCurve) { return this == eCCurve || (eCCurve != null && getField().equals(eCCurve.getField()) && getA().toBigInteger().equals(eCCurve.getA().toBigInteger()) && getB().toBigInteger().equals(eCCurve.getB().toBigInteger())); } public boolean equals(Object obj) { return this == obj || ((obj instanceof ECCurve) && equals((ECCurve) obj)); } /* loaded from: classes6.dex */ public static abstract class AbstractF2m extends ECCurve { private BigInteger[] si; @Override // org.bouncycastle.math.ec.ECCurve public boolean isValidFieldElement(BigInteger bigInteger) { return bigInteger != null && bigInteger.signum() >= 0 && bigInteger.bitLength() <= getFieldSize(); } public boolean isKoblitz() { return this.order != null && this.cofactor != null && this.b.isOne() && (this.a.isZero() || this.a.isOne()); } /* JADX INFO: Access modifiers changed from: package-private */ public BigInteger[] getSi() { BigInteger[] bigIntegerArr; synchronized (this) { if (this.si == null) { this.si = Tnaf.getSi(this); } bigIntegerArr = this.si; } return bigIntegerArr; } @Override // org.bouncycastle.math.ec.ECCurve protected ECPoint decompressPoint(int i, BigInteger bigInteger) { ECFieldElement eCFieldElement; ECFieldElement fromBigInteger = fromBigInteger(bigInteger); if (fromBigInteger.isZero()) { eCFieldElement = getB().sqrt(); } else { ECFieldElement solveQuadraticEquation = solveQuadraticEquation(fromBigInteger.square().invert().multiply(getB()).add(getA()).add(fromBigInteger)); if (solveQuadraticEquation != null) { if (solveQuadraticEquation.testBitZero() != (i == 1)) { solveQuadraticEquation = solveQuadraticEquation.addOne(); } int coordinateSystem = getCoordinateSystem(); eCFieldElement = (coordinateSystem == 5 || coordinateSystem == 6) ? solveQuadraticEquation.add(fromBigInteger) : solveQuadraticEquation.multiply(fromBigInteger); } else { eCFieldElement = null; } } if (eCFieldElement != null) { return createRawPoint(fromBigInteger, eCFieldElement, true); } throw new IllegalArgumentException("Invalid point compression"); } @Override // org.bouncycastle.math.ec.ECCurve public ECPoint createPoint(BigInteger bigInteger, BigInteger bigInteger2, boolean z) { ECFieldElement fromBigInteger = fromBigInteger(bigInteger); ECFieldElement fromBigInteger2 = fromBigInteger(bigInteger2); int coordinateSystem = getCoordinateSystem(); if (coordinateSystem == 5 || coordinateSystem == 6) { if (!fromBigInteger.isZero()) { fromBigInteger2 = fromBigInteger2.divide(fromBigInteger).add(fromBigInteger); } else if (!fromBigInteger2.square().equals(getB())) { throw new IllegalArgumentException(); } } return createRawPoint(fromBigInteger, fromBigInteger2, z); } private ECFieldElement solveQuadraticEquation(ECFieldElement eCFieldElement) { ECFieldElement eCFieldElement2; if (eCFieldElement.isZero()) { return eCFieldElement; } ECFieldElement fromBigInteger = fromBigInteger(ECConstants.ZERO); int fieldSize = getFieldSize(); Random random = new Random(); do { ECFieldElement fromBigInteger2 = fromBigInteger(new BigInteger(fieldSize, random)); ECFieldElement eCFieldElement3 = eCFieldElement; eCFieldElement2 = fromBigInteger; for (int i = 1; i < fieldSize; i++) { ECFieldElement square = eCFieldElement3.square(); eCFieldElement2 = eCFieldElement2.square().add(square.multiply(fromBigInteger2)); eCFieldElement3 = square.add(eCFieldElement); } if (!eCFieldElement3.isZero()) { return null; } } while (eCFieldElement2.square().add(eCFieldElement2).isZero()); return eCFieldElement2; } public static BigInteger inverse(int i, int[] iArr, BigInteger bigInteger) { return new LongArray(bigInteger).modInverse(i, iArr).toBigInteger(); } private static FiniteField buildField(int i, int i2, int i3, int i4) { if (i2 == 0) { throw new IllegalArgumentException("k1 must be > 0"); } if (i3 == 0) { if (i4 == 0) { return FiniteFields.getBinaryExtensionField(new int[]{0, i2, i}); } throw new IllegalArgumentException("k3 must be 0 if k2 == 0"); } if (i3 <= i2) { throw new IllegalArgumentException("k2 must be > k1"); } if (i4 > i3) { return FiniteFields.getBinaryExtensionField(new int[]{0, i2, i3, i4, i}); } throw new IllegalArgumentException("k3 must be > k2"); } public AbstractF2m(int i, int i2, int i3, int i4) { super(buildField(i, i2, i3, i4)); this.si = null; } } public ECPoint decodePoint(byte[] bArr) { ECPoint infinity; int fieldSize = (getFieldSize() + 7) / 8; byte b = bArr[0]; if (b != 0) { if (b == 2 || b == 3) { if (bArr.length != fieldSize + 1) { throw new IllegalArgumentException("Incorrect length for compressed encoding"); } infinity = decompressPoint(b & 1, BigIntegers.fromUnsignedByteArray(bArr, 1, fieldSize)); if (!infinity.satisfiesCofactor()) { throw new IllegalArgumentException("Invalid point"); } } else if (b != 4) { if (b != 6 && b != 7) { StringBuilder sb = new StringBuilder("Invalid point encoding 0x"); sb.append(Integer.toString(b, 16)); throw new IllegalArgumentException(sb.toString()); } if (bArr.length != (fieldSize << 1) + 1) { throw new IllegalArgumentException("Incorrect length for hybrid encoding"); } BigInteger fromUnsignedByteArray = BigIntegers.fromUnsignedByteArray(bArr, 1, fieldSize); BigInteger fromUnsignedByteArray2 = BigIntegers.fromUnsignedByteArray(bArr, fieldSize + 1, fieldSize); if (fromUnsignedByteArray2.testBit(0) != (b == 7)) { throw new IllegalArgumentException("Inconsistent Y coordinate in hybrid encoding"); } infinity = validatePoint(fromUnsignedByteArray, fromUnsignedByteArray2); } else { if (bArr.length != (fieldSize << 1) + 1) { throw new IllegalArgumentException("Incorrect length for uncompressed encoding"); } infinity = validatePoint(BigIntegers.fromUnsignedByteArray(bArr, 1, fieldSize), BigIntegers.fromUnsignedByteArray(bArr, fieldSize + 1, fieldSize)); } } else { if (bArr.length != 1) { throw new IllegalArgumentException("Incorrect length for infinity encoding"); } infinity = getInfinity(); } if (b == 0 || !infinity.isInfinity()) { return infinity; } throw new IllegalArgumentException("Invalid infinity encoding"); } public ECPoint createPoint(BigInteger bigInteger, BigInteger bigInteger2, boolean z) { return createRawPoint(fromBigInteger(bigInteger), fromBigInteger(bigInteger2), z); } public ECPoint createPoint(BigInteger bigInteger, BigInteger bigInteger2) { return createPoint(bigInteger, bigInteger2, false); } protected ECMultiplier createDefaultMultiplier() { ECEndomorphism eCEndomorphism = this.endomorphism; return eCEndomorphism instanceof GLVEndomorphism ? new GLVMultiplier(this, (GLVEndomorphism) eCEndomorphism) : new WNafL2RMultiplier(); } /* loaded from: classes6.dex */ public class Config { protected int coord; protected ECEndomorphism endomorphism; protected ECMultiplier multiplier; final ECCurve this$0; public Config setMultiplier(ECMultiplier eCMultiplier) { this.multiplier = eCMultiplier; return this; } public Config setEndomorphism(ECEndomorphism eCEndomorphism) { this.endomorphism = eCEndomorphism; return this; } public Config setCoordinateSystem(int i) { this.coord = i; return this; } public ECCurve create() { if (!this.this$0.supportsCoordinateSystem(this.coord)) { throw new IllegalStateException("unsupported coordinate system"); } ECCurve cloneCurve = this.this$0.cloneCurve(); if (cloneCurve == this.this$0) { throw new IllegalStateException("implementation returned current curve"); } synchronized (cloneCurve) { cloneCurve.coord = this.coord; cloneCurve.endomorphism = this.endomorphism; cloneCurve.multiplier = this.multiplier; } return cloneCurve; } Config(ECCurve eCCurve, int i, ECEndomorphism eCEndomorphism, ECMultiplier eCMultiplier) { this.this$0 = eCCurve; this.coord = i; this.endomorphism = eCEndomorphism; this.multiplier = eCMultiplier; } } public Config configure() { Config config; synchronized (this) { config = new Config(this, this.coord, this.endomorphism, this.multiplier); } return config; } protected void checkPoints(ECPoint[] eCPointArr, int i, int i2) { if (eCPointArr == null) { throw new IllegalArgumentException("'points' cannot be null"); } if (i < 0 || i2 < 0 || i > eCPointArr.length - i2) { throw new IllegalArgumentException("invalid range specified for 'points'"); } for (int i3 = 0; i3 < i2; i3++) { ECPoint eCPoint = eCPointArr[i + i3]; if (eCPoint != null && this != eCPoint.getCurve()) { throw new IllegalArgumentException("'points' entries must be null or on this curve"); } } } /* loaded from: classes6.dex */ public static abstract class AbstractFp extends ECCurve { @Override // org.bouncycastle.math.ec.ECCurve public boolean isValidFieldElement(BigInteger bigInteger) { return bigInteger != null && bigInteger.signum() >= 0 && bigInteger.compareTo(getField().getCharacteristic()) < 0; } @Override // org.bouncycastle.math.ec.ECCurve protected ECPoint decompressPoint(int i, BigInteger bigInteger) { ECFieldElement fromBigInteger = fromBigInteger(bigInteger); ECFieldElement sqrt = fromBigInteger.square().add(this.a).multiply(fromBigInteger).add(this.b).sqrt(); if (sqrt == null) { throw new IllegalArgumentException("Invalid point compression"); } if (sqrt.testBitZero() != (i == 1)) { sqrt = sqrt.negate(); } return createRawPoint(fromBigInteger, sqrt, true); } public AbstractFp(BigInteger bigInteger) { super(FiniteFields.getPrimeField(bigInteger)); } } protected void checkPoints(ECPoint[] eCPointArr) { checkPoints(eCPointArr, 0, eCPointArr.length); } protected void checkPoint(ECPoint eCPoint) { if (eCPoint == null || this != eCPoint.getCurve()) { throw new IllegalArgumentException("'point' must be non-null and on this curve"); } } public static int[] getAllCoordinateSystems() { return new int[]{0, 1, 2, 3, 4, 5, 6, 7}; } protected ECCurve(FiniteField finiteField) { this.field = finiteField; } }