// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.util;

import org.bouncycastle.util.BigIntegers;
import java.math.BigInteger;

public class RadixConverter
{
    private static final double LOG_LONG_MAX_VALUE;
    private static final int DEFAULT_POWERS_TO_CACHE = 10;
    private final int digitsGroupLength;
    private final BigInteger digitsGroupSpaceSize;
    private final int radix;
    private final BigInteger[] digitsGroupSpacePowers;
    
    public RadixConverter(final int radix, final int n) {
        this.radix = radix;
        this.digitsGroupLength = (int)Math.floor(RadixConverter.LOG_LONG_MAX_VALUE / Math.log(radix));
        this.digitsGroupSpaceSize = BigInteger.valueOf(radix).pow(this.digitsGroupLength);
        this.digitsGroupSpacePowers = this.precomputeDigitsGroupPowers(n, this.digitsGroupSpaceSize);
    }
    
    public RadixConverter(final int n) {
        this(n, 10);
    }
    
    public int getRadix() {
        return this.radix;
    }
    
    public void toEncoding(BigInteger bigInteger, final int n, final short[] array) {
        if (bigInteger.signum() < 0) {
            throw new IllegalArgumentException();
        }
        int i = n - 1;
        do {
            if (bigInteger.equals(BigInteger.ZERO)) {
                array[i--] = 0;
            }
            else {
                final BigInteger[] divideAndRemainder = bigInteger.divideAndRemainder(this.digitsGroupSpaceSize);
                bigInteger = divideAndRemainder[0];
                i = this.toEncoding(divideAndRemainder[1].longValue(), i, array);
            }
        } while (i >= 0);
        if (bigInteger.signum() != 0) {
            throw new IllegalArgumentException();
        }
    }
    
    private int toEncoding(long n, int n2, final short[] array) {
        for (int n3 = 0; n3 < this.digitsGroupLength && n2 >= 0; ++n3) {
            if (n == 0L) {
                array[n2--] = 0;
            }
            else {
                array[n2--] = (short)(n % this.radix);
                n /= this.radix;
            }
        }
        if (n != 0L) {
            throw new IllegalStateException("Failed to convert decimal number");
        }
        return n2;
    }
    
    public BigInteger fromEncoding(final short[] array) {
        BigInteger one = BigIntegers.ONE;
        BigInteger add = null;
        int n = 0;
        final int length = array.length;
        for (int i = length - this.digitsGroupLength; i > -this.digitsGroupLength; i -= this.digitsGroupLength) {
            int digitsGroupLength = this.digitsGroupLength;
            if (i < 0) {
                digitsGroupLength = this.digitsGroupLength + i;
                i = 0;
            }
            final BigInteger value = BigInteger.valueOf(this.fromEncoding(i, Math.min(i + digitsGroupLength, length), array));
            if (n == 0) {
                add = value;
            }
            else {
                one = ((n <= this.digitsGroupSpacePowers.length) ? this.digitsGroupSpacePowers[n - 1] : one.multiply(this.digitsGroupSpaceSize));
                add = add.add(value.multiply(one));
            }
            ++n;
        }
        return add;
    }
    
    public int getDigitsGroupLength() {
        return this.digitsGroupLength;
    }
    
    private long fromEncoding(final int n, final int n2, final short[] array) {
        long n3 = 0L;
        for (int i = n; i < n2; ++i) {
            n3 = n3 * this.radix + (array[i] & 0xFFFF);
        }
        return n3;
    }
    
    private BigInteger[] precomputeDigitsGroupPowers(final int n, final BigInteger val) {
        final BigInteger[] array = new BigInteger[n];
        BigInteger multiply = val;
        for (int i = 0; i < n; ++i) {
            array[i] = multiply;
            multiply = multiply.multiply(val);
        }
        return array;
    }
    
    static {
        LOG_LONG_MAX_VALUE = Math.log(9.223372036854776E18);
    }
}
