Example usage for java.math BigInteger mod

List of usage examples for java.math BigInteger mod

Introduction

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

Prototype

public BigInteger mod(BigInteger m) 

Source Link

Document

Returns a BigInteger whose value is (this mod m ).

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("-100");
    BigInteger bi2 = new BigInteger("3");

    // perform mod operation on bi1 using bi2
    BigInteger bi3 = bi1.mod(bi2);

    System.out.println(bi3);//from w  w w. j  av a  2  s  . co  m
}

From source file:Main.java

public static void main(String[] args) {
    BigInteger bigInteger = new BigInteger("2000000000000");// uper limit
    BigInteger min = new BigInteger("1000000000");// lower limit
    BigInteger bigInteger1 = bigInteger.subtract(min);
    Random rnd = new Random();
    int maxNumBitLength = bigInteger.bitLength();

    BigInteger aRandomBigInt;

    aRandomBigInt = new BigInteger(maxNumBitLength, rnd);
    if (aRandomBigInt.compareTo(min) < 0)
        aRandomBigInt = aRandomBigInt.add(min);
    if (aRandomBigInt.compareTo(bigInteger) >= 0)
        aRandomBigInt = aRandomBigInt.mod(bigInteger1).add(min);

    System.out.println(aRandomBigInt);
}

From source file:Main.java

public static void main(String[] args) {
    BigInteger numberA = new BigInteger("98765432123456789");
    BigInteger numberB = BigInteger.TEN;

    numberA = numberA.add(numberB);/*from   w w  w  .ja v  a 2  s  .  c o m*/
    System.out.println("numberA = " + numberA);

    numberA = numberA.multiply(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.subtract(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.divide(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.mod(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.pow(2);
    System.out.println("numberA = " + numberA);

    numberA = numberA.negate();
    System.out.println("numberA = " + numberA);
}

From source file:Main.java

public static BigInteger mod(BigInteger a, BigInteger b) {
    BigInteger res = a.mod(b);

    return res;
}

From source file:Main.java

public static BigInteger modNear(BigInteger a, BigInteger b) {
    BigInteger res = a.mod(b);

    if (res.compareTo(b.shiftRight(1)) == 1)
        res = res.subtract(b);/*from  w  w  w  .j  a v  a  2s .  co  m*/

    return res;
}

From source file:Main.java

private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) {
    BigInteger beta = null;/*from   w w  w.j  a  v a  2  s. c o  m*/
    if (p.mod(BigInteger.valueOf(4)).compareTo(BigInteger.valueOf(3)) == 0) {
        BigInteger k = p.shiftRight(2).add(ONE);
        beta = alpha.modPow(k, p);
    } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(5)) == 0) {
        System.out.println("p = 8 mod 5");
        BigInteger k = p.subtract(BigInteger.valueOf(5)).divide(BigInteger.valueOf(8));
        BigInteger gamma = alpha.multiply(BigInteger.valueOf(2)).modPow(k, p);
        BigInteger i = alpha.multiply(BigInteger.valueOf(2)).multiply(gamma.pow(2)).mod(p);
        beta = alpha.multiply(gamma).multiply(i.subtract(ONE)).mod(p);
    } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(1)) == 0) {
        beta = null;
        //TODO
        System.out.println("finding square root not fully implemented yet");
    }
    return beta;
}

From source file:Util.java

public static boolean isDivisible(BigInteger isThisNumberDivisible, BigInteger byThisNumber) {
    return isThisNumberDivisible.mod(byThisNumber).equals(BigIntegerZERO);
}

From source file:Main.java

public static boolean isGoodPrime(byte[] prime, int g) {
    if (!(g >= 2 && g <= 7)) {
        return false;
    }/*from w  w  w.  j a  v  a2s  .com*/

    if (prime.length != 256 || prime[0] >= 0) {
        return false;
    }

    BigInteger dhBI = new BigInteger(1, prime);

    if (g == 2) { // p mod 8 = 7 for g = 2;
        BigInteger res = dhBI.mod(BigInteger.valueOf(8));
        if (res.intValue() != 7) {
            return false;
        }
    } else if (g == 3) { // p mod 3 = 2 for g = 3;
        BigInteger res = dhBI.mod(BigInteger.valueOf(3));
        if (res.intValue() != 2) {
            return false;
        }
    } else if (g == 5) { // p mod 5 = 1 or 4 for g = 5;
        BigInteger res = dhBI.mod(BigInteger.valueOf(5));
        int val = res.intValue();
        if (val != 1 && val != 4) {
            return false;
        }
    } else if (g == 6) { // p mod 24 = 19 or 23 for g = 6;
        BigInteger res = dhBI.mod(BigInteger.valueOf(24));
        int val = res.intValue();
        if (val != 19 && val != 23) {
            return false;
        }
    } else if (g == 7) { // p mod 7 = 3, 5 or 6 for g = 7.
        BigInteger res = dhBI.mod(BigInteger.valueOf(7));
        int val = res.intValue();
        if (val != 3 && val != 5 && val != 6) {
            return false;
        }
    }

    String hex = bytesToHex(prime);
    if (hex.equals(
            "C71CAEB9C6B1C9048E6C522F70F13F73980D40238E3E21C14934D037563D930F48198A0AA7C14058229493D22530F4DBFA336F6E0AC925139543AED44CCE7C3720FD51F69458705AC68CD4FE6B6B13ABDC9746512969328454F18FAF8C595F642477FE96BB2A941D5BCD1D4AC8CC49880708FA9B378E3C4F3A9060BEE67CF9A4A4A695811051907E162753B56B0F6B410DBA74D8A84B2A14B3144E0EF1284754FD17ED950D5965B4B9DD46582DB1178D169C6BC465B0D6FF9CA3928FEF5B9AE4E418FC15E83EBEA0F87FA9FF5EED70050DED2849F47BF959D956850CE929851F0D8115F635B105EE2E4E15D04B2454BF6F4FADF034B10403119CD8E3B92FCC5B")) {
        return true;
    }

    BigInteger dhBI2 = dhBI.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(2));
    return !(!dhBI.isProbablePrime(30) || !dhBI2.isProbablePrime(30));
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

public static BigInteger cartesianProduct(BigInteger a, BigInteger s, BigInteger p, BigInteger n) {
    return s.modPow(p, n).multiply(a).mod(n);
}

From source file:Main.java

public static BigInteger computeUnblindSignature(BigInteger r, BigInteger bs)
        throws UnsupportedEncodingException {
    RSAPublicKey pubKey = (RSAPublicKey) getPublicKey(public_key);
    BigInteger n = pubKey.getModulus();

    BigInteger s = r.modInverse(n).multiply(bs).mod(n);

    return s;/*from  w  ww. ja  va 2 s . c om*/
}