Example usage for com.google.common.math BigIntegerMath log2

List of usage examples for com.google.common.math BigIntegerMath log2

Introduction

In this page you can find the example usage for com.google.common.math BigIntegerMath log2.

Prototype

@SuppressWarnings("fallthrough")

public static int log2(BigInteger x, RoundingMode mode) 

Source Link

Document

Returns the base-2 logarithm of x , rounded according to the specified rounding mode.

Usage

From source file:us.eharning.atomun.mnemonic.spi.electrum.v2.MnemonicBuilderSpiImpl.java

/**
 * Generate the mnemonic sequence given the input parameters.
 *
 * @param parameters//from  w w w.ja v  a2  s  . c o m
 *         builder parameters to drive the process.
 *
 * @return the generated mnemonic sequence.
 */
@Nonnull
@Override
public String generateMnemonic(BuilderParameter... parameters) {
    int entropyLengthBytes = -1;
    String wordListIdentifier = null;
    Map<String, Object> extensions = null;
    for (BuilderParameter parameter : parameters) {
        if (null == parameter) {
            continue;
        }
        if (parameter instanceof EntropyBuilderParameter) {
            EntropyBuilderParameter entropyBuilder = (EntropyBuilderParameter) parameter;
            entropyLengthBytes = entropyBuilder.getEntropyLength();
        } else if (parameter instanceof WordListBuilderParameter) {
            wordListIdentifier = ((WordListBuilderParameter) parameter).getWordListIdentifier();
        } else if (parameter instanceof ExtensionBuilderParameter) {
            extensions = ((ExtensionBuilderParameter) parameter).getExtensions();
        } else {
            throw new IllegalArgumentException("Unsupported parameter type: " + parameter);
        }
    }
    if (entropyLengthBytes < 0) {
        entropyLengthBytes = DEFAULT_ENTROPY_PARAMETER.getEntropyLength();
    }
    if (null == wordListIdentifier) {
        wordListIdentifier = DEFAULT_WORDLIST_PARAMETER.getWordListIdentifier();
    }
    if (null == extensions) {
        extensions = Collections.emptyMap();
    }
    /* DUMMY */
    Verify.verifyNotNull(extensions);
    BidirectionalDictionary dictionary = MnemonicUtility.getDictionary(wordListIdentifier);

    BigInteger customEntropy = BigInteger.ONE;
    /* Based on make_seed algorithm */
    int customEntropyBits = BigIntegerMath.log2(customEntropy, RoundingMode.CEILING);
    /* Prefix is a sequence of nibbles, technically we can construct partial-byte
     * prefixes by using a nice mask. */
    byte[] prefix = { 0x01 };
    byte[] prefixMask = { (byte) 0xFF };
    int prefixLength = prefix.length * 8;
    int entropyLengthBits = entropyLengthBytes * 8;
    int randomEntropy = Math.max(16, prefixLength + entropyLengthBits - customEntropyBits);
    BigInteger generatedEntropy = new BigInteger(randomEntropy, new SecureRandom());
    /* Algorithm:
     * {
     *      nonce = 1
     *      i = custom_entropy * (generated entropy + nonce)
     *      if (not valid) nonce += 1; retry
     * }
     * {
     *      nonce = 1
     *      i = custom_entropy * generated entropy + custom_entropy * nonce
     *      if (not valid) nonce += 1; retry
     * }
     * {
     *      nonce = custom_entropy
     *      i = custom_entropy * generated entropy + nonce
     *      if (not valid) nonce += custom_entropy; retry
     * }
     * {
     *      customGeneratedEntropy = custom_entropy * generated_entropy
     *      nonce = custom_entropy
     *      i = customGeneratedEntropy + nonce
     *      if (not valid) nonce += custom_entropy; retry
     * }
     */
    /* Start this off with nonce=1 and post-increment */
    BigInteger nonce = customEntropy;
    BigInteger customGeneratedEntropy = customEntropy.multiply(generatedEntropy);
    while (true) {
        BigInteger value = customGeneratedEntropy.add(nonce);
        String seed = encodeSeed(dictionary, value);
        if (MnemonicUtility.isValidGeneratedSeed(seed, prefix, prefixMask)) {
            return seed;
        }
        nonce = nonce.add(customEntropy);
    }
}

From source file:org.pshdl.model.types.builtIn.HDLBuiltInFunctions.java

public static BigInteger log2ceil(BigInteger le) {
    return BigInteger.valueOf(BigIntegerMath.log2(le, RoundingMode.CEILING));
}

From source file:org.pshdl.model.types.builtIn.HDLBuiltInFunctions.java

public static BigInteger log2floor(BigInteger le) {
    return BigInteger.valueOf(BigIntegerMath.log2(le, RoundingMode.FLOOR));
}