Example usage for java.math BigInteger ONE

List of usage examples for java.math BigInteger ONE

Introduction

In this page you can find the example usage for java.math BigInteger ONE.

Prototype

BigInteger ONE

To view the source code for java.math BigInteger ONE.

Click Source Link

Document

The BigInteger constant one.

Usage

From source file:Main.java

public static void main(String[] args) {
    BigInteger numberB = BigInteger.ONE;

}

From source file:Main.java

public static BigInteger pow(BigInteger base, BigInteger exponent) {
    BigInteger result = BigInteger.ONE;
    while (exponent.signum() > 0) {
        if (exponent.testBit(0))
            result = result.multiply(base);
        base = base.multiply(base);//from  ww w .ja  v  a 2s  .c  o  m
        exponent = exponent.shiftRight(1);
    }
    return result;
}

From source file:Main.java

public static BigInteger productOf(Collection<Integer> numbers) {
    BigInteger sum = BigInteger.ONE;
    for (Integer n : numbers) {
        sum = sum.multiply(BigInteger.valueOf(n));
    }//from   w ww .j  av a 2 s . co m
    return sum;
}

From source file:Main.java

public static BigInteger sqrt(final BigInteger val) {
    final BigInteger two = BigInteger.valueOf(2);
    BigInteger a = BigInteger.ONE.shiftLeft(val.bitLength() / 2);
    BigInteger b;/*  w  w  w .ja va 2s.c o m*/
    do {
        b = val.divide(a);
        a = (a.add(b)).divide(two);
    } while (a.subtract(b).abs().compareTo(two) >= 0);
    return a;
}

From source file:Main.java

/**
 * Symmetric encryption of private key x
 * @param u//  w w w  .j a v a 2s  . co  m
 * @param k
 * @param p
 * @param x
 * @return
 */
public static BigInteger getE(BigInteger u, BigInteger k, BigInteger p, BigInteger x) {
    return u.multiply(x).subtract(k).mod(p.subtract(BigInteger.ONE));
}

From source file:Main.java

static BigInteger inverseMod(BigInteger a, BigInteger b) {
    BigInteger b0 = b, t, q;//  ww  w .j a v a 2s .  c o m
    BigInteger x0 = BigInteger.ZERO, x1 = BigInteger.ONE;
    if (b.equals(BigInteger.ONE))
        return BigInteger.ONE;
    while (a.subtract(BigInteger.ONE).signum() > 0) {
        q = a.divide(b);
        t = b;
        b = a.mod(b);
        a = t;
        t = x0;
        x0 = x1.subtract(q.multiply(x0));
        x1 = t;
    }
    if (x1.signum() < 0)
        x1 = x1.add(b0);
    return x1;
}

From source file:Main.java

static BigInteger mulmod(BigInteger a, BigInteger b, BigInteger p) {
    BigInteger r = BigInteger.ZERO;
    while (b.compareTo(BigInteger.ZERO) > 0) {
        if (!b.and(BigInteger.ONE).equals(BigInteger.ZERO)) {
            r = addmod(r, a, p);/*from w ww .  ja v  a2  s. c o  m*/
        }
        b = b.shiftRight(1);
        a = addmod(a, a, p);
    }
    return r;
}

From source file:Main.java

@SuppressLint("NewApi")
public static KeyPair generateRsaPairWithGenerator(Context ctx, String alais)
        throws InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException {
    Calendar notBefore = Calendar.getInstance();
    Calendar notAfter = Calendar.getInstance();
    notAfter.add(1, Calendar.YEAR);
    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx).setAlias(alais)
            .setSubject(new X500Principal(String.format("CN=%s, OU=%s", alais, ctx.getPackageName())))
            .setSerialNumber(BigInteger.ONE).setStartDate(notBefore.getTime()).setEndDate(notAfter.getTime())
            .build();/*from ww w  . j a  v a  2  s .  com*/

    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
    kpGenerator.initialize(spec);
    KeyPair kp = kpGenerator.generateKeyPair();

    return kp;
}

From source file:Main.java

private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) {
    final BigInteger ONE = BigInteger.ONE;
    final BigInteger TWO = BigInteger.valueOf(2);
    // Find a and m such that m is odd and this == 1 + 2**a * m
    BigInteger thisMinusOne = us.subtract(ONE);
    BigInteger m = thisMinusOne;/*from   w  ww  .  j a v a2 s.c o m*/
    int a = m.getLowestSetBit();
    m = m.shiftRight(a);

    // Do the tests
    if (rnd == null) {
        rnd = new SecureRandom();
    }
    for (int i = 0; i < iterations; i++) {
        // Generate a uniform random on (1, this)
        BigInteger b;
        do {
            b = new BigInteger(us.bitLength(), rnd);
        } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0);

        int j = 0;
        BigInteger z = b.modPow(m, us);
        while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
            if (j > 0 && z.equals(ONE) || ++j == a)
                return false;
            z = z.modPow(TWO, us);
        }
    }
    return true;
}

From source file:RSA.java

/** Create an instance that can both encrypt and decrypt. */
public RSA(int bits) {
    bitlen = bits;//ww  w. j a  va  2  s  .c  om
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
        e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
}