package org.bouncycastle.crypto.util;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class RadixConverter {
    private static final double e = java.lang.Math.log(9.223372036854776E18d);
    private final int a;
    private final java.math.BigInteger b;
    private final int c;
    private final java.math.BigInteger[] d;

    private long a(int i, int i2, short[] sArr) {
        long j = 0;
        while (i < i2) {
            j = (j * ((long) this.c)) + ((long) (sArr[i] & 65535));
            i++;
        }
        return j;
    }

    private int d(long j, int i, short[] sArr) {
        int i2;
        for (int i3 = 0; i3 < this.a && i >= 0; i3++) {
            if (j == 0) {
                i2 = i - 1;
                sArr[i] = 0;
            } else {
                i2 = i - 1;
                int i4 = this.c;
                sArr[i] = (short) (j % ((long) i4));
                j /= (long) i4;
            }
            i = i2;
        }
        if (j == 0) {
            return i;
        }
        throw new java.lang.IllegalStateException("Failed to convert decimal number");
    }

    public java.math.BigInteger b(short[] sArr) {
        java.math.BigInteger bigIntegerMultiply = org.bouncycastle.util.BigIntegers.b;
        int length = sArr.length;
        int i = length - this.a;
        java.math.BigInteger bigIntegerAdd = null;
        int i2 = 0;
        while (true) {
            int i3 = this.a;
            if (i <= (-i3)) {
                return bigIntegerAdd;
            }
            if (i < 0) {
                i3 += i;
                i = 0;
            }
            java.math.BigInteger bigIntegerValueOf = java.math.BigInteger.valueOf(a(i, java.lang.Math.min(i3 + i, length), sArr));
            if (i2 == 0) {
                bigIntegerAdd = bigIntegerValueOf;
            } else {
                java.math.BigInteger[] bigIntegerArr = this.d;
                bigIntegerMultiply = i2 <= bigIntegerArr.length ? bigIntegerArr[i2 - 1] : bigIntegerMultiply.multiply(this.b);
                bigIntegerAdd = bigIntegerAdd.add(bigIntegerValueOf.multiply(bigIntegerMultiply));
            }
            i2++;
            i -= this.a;
        }
    }

    public int c() {
        return this.c;
    }

    public void e(java.math.BigInteger bigInteger, int i, short[] sArr) {
        if (bigInteger.signum() < 0) {
            throw new java.lang.IllegalArgumentException();
        }
        int iD = i - 1;
        do {
            if (bigInteger.equals(java.math.BigInteger.ZERO)) {
                sArr[iD] = 0;
                iD--;
            } else {
                java.math.BigInteger[] bigIntegerArrDivideAndRemainder = bigInteger.divideAndRemainder(this.b);
                java.math.BigInteger bigInteger2 = bigIntegerArrDivideAndRemainder[0];
                iD = d(bigIntegerArrDivideAndRemainder[1].longValue(), iD, sArr);
                bigInteger = bigInteger2;
            }
        } while (iD >= 0);
        if (bigInteger.signum() != 0) {
            throw new java.lang.IllegalArgumentException();
        }
    }
}
