what-the-bank/sources/org/bouncycastle/pqc/math/linearalgebra/GF2nPolynomialElement.java

509 lines
20 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.pqc.math.linearalgebra;
import com.google.android.gms.internal.common.zzd;
import java.math.BigInteger;
import java.util.Random;
import okhttp3.internal.http2.Http2Connection;
import org.bouncycastle.asn1.cmp.PKIFailureInfo;
/* loaded from: classes6.dex */
public class GF2nPolynomialElement extends GF2nElement {
private static final int[] bitMask = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, PKIFailureInfo.badCertTemplate, PKIFailureInfo.badSenderNonce, 4194304, 8388608, Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE, 33554432, zzd.zza, 134217728, 268435456, PKIFailureInfo.duplicateCertReq, 1073741824, PKIFailureInfo.systemUnavail, 0};
private GF2Polynomial polynomial;
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public int trace() {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
for (int i = 1; i < this.mDegree; i++) {
gF2nPolynomialElement.squareThis();
gF2nPolynomialElement.addToThis(this);
}
return gF2nPolynomialElement.isOne() ? 1 : 0;
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public String toString(int i) {
return this.polynomial.toString(i);
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public String toString() {
return this.polynomial.toString(16);
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public BigInteger toFlexiBigInt() {
return this.polynomial.toFlexiBigInt();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public byte[] toByteArray() {
return this.polynomial.toByteArray();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public boolean testRightmostBit() {
return this.polynomial.testBit(0);
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public boolean testBit(int i) {
return this.polynomial.testBit(i);
}
public void squareThisPreCalc() {
this.polynomial.squareThisPreCalc();
reduceThis();
}
public void squareThisMatrix() {
GF2Polynomial gF2Polynomial = new GF2Polynomial(this.mDegree);
for (int i = 0; i < this.mDegree; i++) {
if (this.polynomial.vectorMult(((GF2nPolynomialField) this.mField).squaringMatrix[(this.mDegree - i) - 1])) {
gF2Polynomial.setBit(i);
}
}
this.polynomial = gF2Polynomial;
}
public void squareThisBitwise() {
this.polynomial.squareThisBitwise();
reduceThis();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public void squareThis() {
squareThisPreCalc();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public void squareRootThis() {
this.polynomial.expandN((this.mDegree << 1) + 32);
this.polynomial.reduceN();
for (int i = 0; i < this.mField.getDegree() - 1; i++) {
squareThis();
}
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public GF2nElement squareRoot() {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.squareRootThis();
return gF2nPolynomialElement;
}
public GF2nPolynomialElement squarePreCalc() {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.squareThisPreCalc();
gF2nPolynomialElement.reduceThis();
return gF2nPolynomialElement;
}
public GF2nPolynomialElement squareMatrix() {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.squareThisMatrix();
gF2nPolynomialElement.reduceThis();
return gF2nPolynomialElement;
}
public GF2nPolynomialElement squareBitwise() {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.squareThisBitwise();
gF2nPolynomialElement.reduceThis();
return gF2nPolynomialElement;
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public GF2nElement square() {
return squarePreCalc();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public GF2nElement solveQuadraticEquation() throws RuntimeException {
GF2nPolynomialElement ZERO;
GF2nPolynomialElement gF2nPolynomialElement;
if (isZero()) {
return ZERO((GF2nPolynomialField) this.mField);
}
if ((this.mDegree & 1) == 1) {
return halfTrace();
}
do {
GF2nPolynomialElement gF2nPolynomialElement2 = new GF2nPolynomialElement((GF2nPolynomialField) this.mField, new Random());
ZERO = ZERO((GF2nPolynomialField) this.mField);
gF2nPolynomialElement = (GF2nPolynomialElement) gF2nPolynomialElement2.clone();
for (int i = 1; i < this.mDegree; i++) {
ZERO.squareThis();
gF2nPolynomialElement.squareThis();
ZERO.addToThis(gF2nPolynomialElement.multiply(this));
gF2nPolynomialElement.addToThis(gF2nPolynomialElement2);
}
} while (gF2nPolynomialElement.isZero());
if (equals(ZERO.square().add(ZERO))) {
return ZERO;
}
throw new RuntimeException();
}
public GF2nPolynomialElement power(int i) {
if (i == 1) {
return new GF2nPolynomialElement(this);
}
GF2nPolynomialElement ONE = ONE((GF2nPolynomialField) this.mField);
if (i == 0) {
return ONE;
}
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.polynomial.expandN((gF2nPolynomialElement.mDegree << 1) + 32);
gF2nPolynomialElement.polynomial.reduceN();
for (int i2 = 0; i2 < this.mDegree; i2++) {
if (((1 << i2) & i) != 0) {
ONE.multiplyThisBy(gF2nPolynomialElement);
}
gF2nPolynomialElement.square();
}
return ONE;
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public void multiplyThisBy(GFElement gFElement) throws RuntimeException {
if (!(gFElement instanceof GF2nPolynomialElement)) {
throw new RuntimeException();
}
GF2nPolynomialElement gF2nPolynomialElement = (GF2nPolynomialElement) gFElement;
if (!this.mField.equals(gF2nPolynomialElement.mField)) {
throw new RuntimeException();
}
if (equals(gFElement)) {
squareThis();
} else {
this.polynomial = this.polynomial.multiply(gF2nPolynomialElement.polynomial);
reduceThis();
}
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public GFElement multiply(GFElement gFElement) throws RuntimeException {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.multiplyThisBy(gFElement);
return gF2nPolynomialElement;
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public boolean isZero() {
return this.polynomial.isZero();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public boolean isOne() {
return this.polynomial.isOne();
}
public GF2nPolynomialElement invertSquare() throws ArithmeticException {
if (isZero()) {
throw new ArithmeticException();
}
int degree = this.mField.getDegree() - 1;
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.polynomial.expandN((this.mDegree << 1) + 32);
gF2nPolynomialElement.polynomial.reduceN();
int i = 1;
for (int floorLog = IntegerFunctions.floorLog(degree) - 1; floorLog >= 0; floorLog--) {
GF2nPolynomialElement gF2nPolynomialElement2 = new GF2nPolynomialElement(gF2nPolynomialElement);
for (int i2 = 1; i2 <= i; i2++) {
gF2nPolynomialElement2.squareThisPreCalc();
}
gF2nPolynomialElement.multiplyThisBy(gF2nPolynomialElement2);
i <<= 1;
if ((bitMask[floorLog] & degree) != 0) {
gF2nPolynomialElement.squareThisPreCalc();
gF2nPolynomialElement.multiplyThisBy(this);
i++;
}
}
gF2nPolynomialElement.squareThisPreCalc();
return gF2nPolynomialElement;
}
public GF2nPolynomialElement invertMAIA() throws ArithmeticException {
if (isZero()) {
throw new ArithmeticException();
}
GF2Polynomial gF2Polynomial = new GF2Polynomial(this.mDegree, "ONE");
GF2Polynomial gF2Polynomial2 = new GF2Polynomial(this.mDegree);
GF2Polynomial gF2Polynomial3 = getGF2Polynomial();
GF2Polynomial fieldPolynomial = this.mField.getFieldPolynomial();
while (true) {
if (!gF2Polynomial3.testBit(0)) {
gF2Polynomial3.shiftRightThis();
if (gF2Polynomial.testBit(0)) {
gF2Polynomial.addToThis(this.mField.getFieldPolynomial());
}
gF2Polynomial.shiftRightThis();
} else {
if (gF2Polynomial3.isOne()) {
return new GF2nPolynomialElement((GF2nPolynomialField) this.mField, gF2Polynomial);
}
gF2Polynomial3.reduceN();
fieldPolynomial.reduceN();
if (gF2Polynomial3.getLength() < fieldPolynomial.getLength()) {
GF2Polynomial gF2Polynomial4 = gF2Polynomial2;
gF2Polynomial2 = gF2Polynomial;
gF2Polynomial = gF2Polynomial4;
GF2Polynomial gF2Polynomial5 = fieldPolynomial;
fieldPolynomial = gF2Polynomial3;
gF2Polynomial3 = gF2Polynomial5;
}
gF2Polynomial3.addToThis(fieldPolynomial);
gF2Polynomial.addToThis(gF2Polynomial2);
}
}
}
public GF2nPolynomialElement invertEEA() throws ArithmeticException {
if (isZero()) {
throw new ArithmeticException();
}
GF2Polynomial gF2Polynomial = new GF2Polynomial(this.mDegree + 32, "ONE");
gF2Polynomial.reduceN();
GF2Polynomial gF2Polynomial2 = new GF2Polynomial(this.mDegree + 32);
gF2Polynomial2.reduceN();
GF2Polynomial gF2Polynomial3 = getGF2Polynomial();
GF2Polynomial fieldPolynomial = this.mField.getFieldPolynomial();
gF2Polynomial3.reduceN();
while (!gF2Polynomial3.isOne()) {
gF2Polynomial3.reduceN();
fieldPolynomial.reduceN();
int length = gF2Polynomial3.getLength() - fieldPolynomial.getLength();
if (length < 0) {
length = -length;
gF2Polynomial.reduceN();
GF2Polynomial gF2Polynomial4 = gF2Polynomial2;
gF2Polynomial2 = gF2Polynomial;
gF2Polynomial = gF2Polynomial4;
GF2Polynomial gF2Polynomial5 = fieldPolynomial;
fieldPolynomial = gF2Polynomial3;
gF2Polynomial3 = gF2Polynomial5;
}
gF2Polynomial3.shiftLeftAddThis(fieldPolynomial, length);
gF2Polynomial.shiftLeftAddThis(gF2Polynomial2, length);
}
gF2Polynomial.reduceN();
return new GF2nPolynomialElement((GF2nPolynomialField) this.mField, gF2Polynomial);
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public GFElement invert() throws ArithmeticException {
return invertMAIA();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public void increaseThis() {
this.polynomial.increaseThis();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public GF2nElement increase() {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.increaseThis();
return gF2nPolynomialElement;
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public int hashCode() {
return this.mField.hashCode() + this.polynomial.hashCode();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof GF2nPolynomialElement)) {
return false;
}
GF2nPolynomialElement gF2nPolynomialElement = (GF2nPolynomialElement) obj;
if (this.mField == gF2nPolynomialElement.mField || this.mField.getFieldPolynomial().equals(gF2nPolynomialElement.mField.getFieldPolynomial())) {
return this.polynomial.equals(gF2nPolynomialElement.polynomial);
}
return false;
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement, org.bouncycastle.pqc.math.linearalgebra.GFElement
public Object clone() {
return new GF2nPolynomialElement(this);
}
/* JADX INFO: Access modifiers changed from: package-private */
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
public void assignZero() {
this.polynomial.assignZero();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GF2nElement
void assignOne() {
this.polynomial.assignOne();
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public void addToThis(GFElement gFElement) throws RuntimeException {
if (!(gFElement instanceof GF2nPolynomialElement)) {
throw new RuntimeException();
}
GF2nPolynomialElement gF2nPolynomialElement = (GF2nPolynomialElement) gFElement;
if (!this.mField.equals(gF2nPolynomialElement.mField)) {
throw new RuntimeException();
}
this.polynomial.addToThis(gF2nPolynomialElement.polynomial);
}
@Override // org.bouncycastle.pqc.math.linearalgebra.GFElement
public GFElement add(GFElement gFElement) throws RuntimeException {
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
gF2nPolynomialElement.addToThis(gFElement);
return gF2nPolynomialElement;
}
private void reduceTrinomialBitwise(int i) {
int i2 = this.mDegree;
int length = this.polynomial.getLength();
while (true) {
length--;
if (length < this.mDegree) {
this.polynomial.reduceN();
this.polynomial.expandN(this.mDegree);
return;
} else if (this.polynomial.testBit(length)) {
this.polynomial.xorBit(length);
this.polynomial.xorBit(length - (i2 - i));
this.polynomial.xorBit(length - this.mDegree);
}
}
}
private void reduceThis() {
if (this.polynomial.getLength() <= this.mDegree) {
if (this.polynomial.getLength() < this.mDegree) {
this.polynomial.expandN(this.mDegree);
return;
}
return;
}
if (((GF2nPolynomialField) this.mField).isTrinomial()) {
try {
int tc = ((GF2nPolynomialField) this.mField).getTc();
if (this.mDegree - tc <= 32 || this.polynomial.getLength() > (this.mDegree << 1)) {
reduceTrinomialBitwise(tc);
return;
} else {
this.polynomial.reduceTrinomial(this.mDegree, tc);
return;
}
} catch (RuntimeException unused) {
throw new RuntimeException("GF2nPolynomialElement.reduce: the field polynomial is not a trinomial");
}
}
if (!((GF2nPolynomialField) this.mField).isPentanomial()) {
GF2Polynomial remainder = this.polynomial.remainder(this.mField.getFieldPolynomial());
this.polynomial = remainder;
remainder.expandN(this.mDegree);
return;
}
try {
int[] pc = ((GF2nPolynomialField) this.mField).getPc();
if (this.mDegree - pc[2] <= 32 || this.polynomial.getLength() > (this.mDegree << 1)) {
reducePentanomialBitwise(pc);
} else {
this.polynomial.reducePentanomial(this.mDegree, pc);
}
} catch (RuntimeException unused2) {
throw new RuntimeException("GF2nPolynomialElement.reduce: the field polynomial is not a pentanomial");
}
}
private void reducePentanomialBitwise(int[] iArr) {
int i = this.mDegree;
int i2 = iArr[2];
int i3 = this.mDegree;
int i4 = iArr[1];
int i5 = this.mDegree;
int i6 = iArr[0];
for (int length = this.polynomial.getLength() - 1; length >= this.mDegree; length--) {
if (this.polynomial.testBit(length)) {
this.polynomial.xorBit(length);
this.polynomial.xorBit(length - (i - i2));
this.polynomial.xorBit(length - (i3 - i4));
this.polynomial.xorBit(length - (i5 - i6));
this.polynomial.xorBit(length - this.mDegree);
}
}
this.polynomial.reduceN();
this.polynomial.expandN(this.mDegree);
}
private void randomize(Random random) {
this.polynomial.expandN(this.mDegree);
this.polynomial.randomize(random);
}
private GF2nPolynomialElement halfTrace() throws RuntimeException {
if ((this.mDegree & 1) == 0) {
throw new RuntimeException();
}
GF2nPolynomialElement gF2nPolynomialElement = new GF2nPolynomialElement(this);
for (int i = 1; i <= ((this.mDegree - 1) >> 1); i++) {
gF2nPolynomialElement.squareThis();
gF2nPolynomialElement.squareThis();
gF2nPolynomialElement.addToThis(this);
}
return gF2nPolynomialElement;
}
private GF2Polynomial getGF2Polynomial() {
return new GF2Polynomial(this.polynomial);
}
public static GF2nPolynomialElement ZERO(GF2nPolynomialField gF2nPolynomialField) {
return new GF2nPolynomialElement(gF2nPolynomialField, new GF2Polynomial(gF2nPolynomialField.getDegree()));
}
public static GF2nPolynomialElement ONE(GF2nPolynomialField gF2nPolynomialField) {
return new GF2nPolynomialElement(gF2nPolynomialField, new GF2Polynomial(gF2nPolynomialField.getDegree(), new int[]{1}));
}
public GF2nPolynomialElement(GF2nPolynomialField gF2nPolynomialField, int[] iArr) {
this.mField = gF2nPolynomialField;
this.mDegree = this.mField.getDegree();
GF2Polynomial gF2Polynomial = new GF2Polynomial(this.mDegree, iArr);
this.polynomial = gF2Polynomial;
gF2Polynomial.expandN(gF2nPolynomialField.mDegree);
}
public GF2nPolynomialElement(GF2nPolynomialField gF2nPolynomialField, byte[] bArr) {
this.mField = gF2nPolynomialField;
this.mDegree = this.mField.getDegree();
GF2Polynomial gF2Polynomial = new GF2Polynomial(this.mDegree, bArr);
this.polynomial = gF2Polynomial;
gF2Polynomial.expandN(this.mDegree);
}
public GF2nPolynomialElement(GF2nPolynomialField gF2nPolynomialField, GF2Polynomial gF2Polynomial) {
this.mField = gF2nPolynomialField;
this.mDegree = this.mField.getDegree();
GF2Polynomial gF2Polynomial2 = new GF2Polynomial(gF2Polynomial);
this.polynomial = gF2Polynomial2;
gF2Polynomial2.expandN(this.mDegree);
}
public GF2nPolynomialElement(GF2nPolynomialField gF2nPolynomialField, Random random) {
this.mField = gF2nPolynomialField;
this.mDegree = this.mField.getDegree();
this.polynomial = new GF2Polynomial(this.mDegree);
randomize(random);
}
public GF2nPolynomialElement(GF2nPolynomialElement gF2nPolynomialElement) {
this.mField = gF2nPolynomialElement.mField;
this.mDegree = gF2nPolynomialElement.mDegree;
this.polynomial = new GF2Polynomial(gF2nPolynomialElement.polynomial);
}
}