package org.bouncycastle.crypto.util;

import java.math.BigInteger;
import org.bouncycastle.crypto.hpke.HPKE;
import org.bouncycastle.util.BigIntegers;

/* JADX INFO: loaded from: classes.dex */
public class RadixConverter {
    private static final int DEFAULT_POWERS_TO_CACHE = 10;
    private static final double LOG_LONG_MAX_VALUE = Math.log(9.223372036854776E18d);
    private final int digitsGroupLength;
    private final BigInteger[] digitsGroupSpacePowers;
    private final BigInteger digitsGroupSpaceSize;
    private final int radix;

    public RadixConverter(int i) {
        this(i, 10);
    }

    public RadixConverter(int i, int i2) {
        this.radix = i;
        this.digitsGroupLength = (int) Math.floor(LOG_LONG_MAX_VALUE / Math.log(i));
        this.digitsGroupSpaceSize = BigInteger.valueOf(i).pow(this.digitsGroupLength);
        this.digitsGroupSpacePowers = precomputeDigitsGroupPowers(i2, this.digitsGroupSpaceSize);
    }

    private long fromEncoding(int i, int i2, short[] sArr) {
        long j = 0;
        while (i < i2) {
            j = (j * ((long) this.radix)) + ((long) (sArr[i] & HPKE.aead_EXPORT_ONLY));
            i++;
        }
        return j;
    }

    private BigInteger[] precomputeDigitsGroupPowers(int i, BigInteger bigInteger) {
        BigInteger[] bigIntegerArr = new BigInteger[i];
        BigInteger bigIntegerMultiply = bigInteger;
        for (int i2 = 0; i2 < i; i2++) {
            bigIntegerArr[i2] = bigIntegerMultiply;
            bigIntegerMultiply = bigIntegerMultiply.multiply(bigInteger);
        }
        return bigIntegerArr;
    }

    private int toEncoding(long j, int i, short[] sArr) {
        int i2;
        for (int i3 = 0; i3 < this.digitsGroupLength && i >= 0; i3++) {
            if (j == 0) {
                i2 = i - 1;
                sArr[i] = 0;
            } else {
                i2 = i - 1;
                sArr[i] = (short) (j % ((long) this.radix));
                j /= (long) this.radix;
            }
            i = i2;
        }
        if (j == 0) {
            return i;
        }
        throw new IllegalStateException("Failed to convert decimal number");
    }

    public BigInteger fromEncoding(short[] sArr) {
        BigInteger bigIntegerMultiply = BigIntegers.ONE;
        int length = sArr.length;
        int i = length - this.digitsGroupLength;
        BigInteger bigIntegerAdd = null;
        int i2 = 0;
        while (i > (-this.digitsGroupLength)) {
            int i3 = this.digitsGroupLength;
            if (i < 0) {
                i3 = this.digitsGroupLength + i;
                i = 0;
            }
            BigInteger bigIntegerValueOf = BigInteger.valueOf(fromEncoding(i, Math.min(i3 + i, length), sArr));
            if (i2 == 0) {
                bigIntegerAdd = bigIntegerValueOf;
            } else {
                bigIntegerMultiply = i2 <= this.digitsGroupSpacePowers.length ? this.digitsGroupSpacePowers[i2 - 1] : bigIntegerMultiply.multiply(this.digitsGroupSpaceSize);
                bigIntegerAdd = bigIntegerAdd.add(bigIntegerValueOf.multiply(bigIntegerMultiply));
            }
            i2++;
            i -= this.digitsGroupLength;
        }
        return bigIntegerAdd;
    }

    public int getDigitsGroupLength() {
        return this.digitsGroupLength;
    }

    public int getRadix() {
        return this.radix;
    }

    public void toEncoding(BigInteger bigInteger, int i, short[] sArr) {
        if (bigInteger.signum() < 0) {
            throw new IllegalArgumentException();
        }
        int encoding = i - 1;
        do {
            if (bigInteger.equals(BigInteger.ZERO)) {
                sArr[encoding] = 0;
                encoding--;
            } else {
                BigInteger[] bigIntegerArrDivideAndRemainder = bigInteger.divideAndRemainder(this.digitsGroupSpaceSize);
                BigInteger bigInteger2 = bigIntegerArrDivideAndRemainder[0];
                encoding = toEncoding(bigIntegerArrDivideAndRemainder[1].longValue(), encoding, sArr);
                bigInteger = bigInteger2;
            }
        } while (encoding >= 0);
        if (bigInteger.signum() != 0) {
            throw new IllegalArgumentException();
        }
    }
}
