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

143 lines
4.3 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.pqc.math.linearalgebra;
import java.security.SecureRandom;
/* loaded from: classes6.dex */
public class Permutation {
private int[] perm;
public String toString() {
StringBuilder sb = new StringBuilder("[");
sb.append(this.perm[0]);
String obj = sb.toString();
for (int i = 1; i < this.perm.length; i++) {
StringBuilder sb2 = new StringBuilder();
sb2.append(obj);
sb2.append(", ");
sb2.append(this.perm[i]);
obj = sb2.toString();
}
StringBuilder sb3 = new StringBuilder();
sb3.append(obj);
sb3.append("]");
return sb3.toString();
}
public Permutation rightMultiply(Permutation permutation) {
int length = permutation.perm.length;
int[] iArr = this.perm;
if (length != iArr.length) {
throw new IllegalArgumentException("length mismatch");
}
Permutation permutation2 = new Permutation(iArr.length);
for (int length2 = this.perm.length - 1; length2 >= 0; length2--) {
permutation2.perm[length2] = this.perm[permutation.perm[length2]];
}
return permutation2;
}
public int hashCode() {
return this.perm.hashCode();
}
public int[] getVector() {
return IntUtils.clone(this.perm);
}
public byte[] getEncoded() {
int length = this.perm.length;
int ceilLog256 = IntegerFunctions.ceilLog256(length - 1);
byte[] bArr = new byte[(length * ceilLog256) + 4];
LittleEndianConversions.I2OSP(length, bArr, 0);
for (int i = 0; i < length; i++) {
LittleEndianConversions.I2OSP(this.perm[i], bArr, (i * ceilLog256) + 4, ceilLog256);
}
return bArr;
}
public boolean equals(Object obj) {
if (obj instanceof Permutation) {
return IntUtils.equals(this.perm, ((Permutation) obj).perm);
}
return false;
}
public Permutation computeInverse() {
Permutation permutation = new Permutation(this.perm.length);
for (int length = this.perm.length - 1; length >= 0; length--) {
permutation.perm[this.perm[length]] = length;
}
return permutation;
}
private boolean isPermutation(int[] iArr) {
int length = iArr.length;
boolean[] zArr = new boolean[length];
for (int i : iArr) {
if (i < 0 || i >= length || zArr[i]) {
return false;
}
zArr[i] = true;
}
return true;
}
public Permutation(int[] iArr) {
if (!isPermutation(iArr)) {
throw new IllegalArgumentException("array is not a permutation vector");
}
this.perm = IntUtils.clone(iArr);
}
public Permutation(byte[] bArr) {
if (bArr.length <= 4) {
throw new IllegalArgumentException("invalid encoding");
}
int OS2IP = LittleEndianConversions.OS2IP(bArr, 0);
int ceilLog256 = IntegerFunctions.ceilLog256(OS2IP - 1);
if (bArr.length != (OS2IP * ceilLog256) + 4) {
throw new IllegalArgumentException("invalid encoding");
}
this.perm = new int[OS2IP];
for (int i = 0; i < OS2IP; i++) {
this.perm[i] = LittleEndianConversions.OS2IP(bArr, (i * ceilLog256) + 4, ceilLog256);
}
if (!isPermutation(this.perm)) {
throw new IllegalArgumentException("invalid encoding");
}
}
public Permutation(int i, SecureRandom secureRandom) {
if (i <= 0) {
throw new IllegalArgumentException("invalid length");
}
this.perm = new int[i];
int[] iArr = new int[i];
for (int i2 = 0; i2 < i; i2++) {
iArr[i2] = i2;
}
int i3 = i;
for (int i4 = 0; i4 < i; i4++) {
int nextInt = RandUtils.nextInt(secureRandom, i3);
i3--;
this.perm[i4] = iArr[nextInt];
iArr[nextInt] = iArr[i3];
}
}
public Permutation(int i) {
if (i <= 0) {
throw new IllegalArgumentException("invalid length");
}
this.perm = new int[i];
while (true) {
i--;
if (i < 0) {
return;
} else {
this.perm[i] = i;
}
}
}
}