Example usage for java.math BigInteger subtract

List of usage examples for java.math BigInteger subtract

Introduction

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

Prototype

public BigInteger subtract(BigInteger val) 

Source Link

Document

Returns a BigInteger whose value is (this - val) .

Usage

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;//from w  ww  .  j a va2  s  . 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:jp.co.ntts.vhut.util.MacConversionUtil.java

/**
 * ????????.//from  w  w w .j a  v  a 2 s .  c  o m
 * @param macStart ()
 * @param macEnd ()
 * @return 
 */
public static int getCount(String macStart, String macEnd) {
    BigInteger biMacStart = new BigInteger(addrToByte(macStart));
    BigInteger biMacEnd = new BigInteger(addrToByte(macEnd));
    return biMacEnd.subtract(biMacStart).intValue() + 1;
}

From source file:Main.java

static BigInteger inverseMod(BigInteger a, BigInteger b) {
    BigInteger b0 = b, t, q;/*w  ww .j  a va  2 s.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:jp.co.ntts.vhut.util.MacConversionUtil.java

/**
 * ??????./*from ww w . jav a 2s. c  o m*/
 * @param mac 
 * @param macStart 
 * @return ?
 */
public static int getMacAddressOrder(String mac, String macStart) {
    BigInteger biMacTarget = new BigInteger(addrToByte(mac));
    BigInteger biMacStart = new BigInteger(addrToByte(macStart));
    return biMacTarget.subtract(biMacStart).intValue();
}

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   ww  w. j  av  a  2s.  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:com.offbynull.peernetic.common.identification.Id.java

/**
 * Adds two IDs together. The limit of the IDs must match.
 * @param lhs right-hand side/*from ww w  .  j a  v a2  s.  co m*/
 * @param rhs right-hand side
 * @return {@code lhs + rhs}, wrapped if it exceeds the limit
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if the limit from {@code lhs} doesn't match the limit from {@code rhs}
 */
public static Id add(Id lhs, Id rhs) {
    Validate.notNull(lhs);
    Validate.notNull(rhs);
    Validate.isTrue(lhs.limit.equals(rhs.limit));

    BigInteger added = lhs.data.add(rhs.data);
    if (added.compareTo(lhs.limit) > 0) {
        added = added.subtract(lhs.limit).subtract(BigInteger.ONE);
    }

    Id addedId = new Id(added, lhs.limit);

    return addedId;
}

From source file:com.og.elliptic.sdk.main.java

private static void elGamalString(WeierStrassCurve curve) {
    // Droulement de El Gamal
    // Cryptage/*  ww w  .j a  va2  s  .  c  om*/
    PointGMP generateur = new PointGMP(curve.getGx(), curve.getGy(), curve);// le point gnrateur qui servira  calculer notre clef
    // secrte
    Random rnd = new Random();

    BigInteger clefSecrete = BigInteger.probablePrime(7, rnd);
    PointGMP publicKey = generateur.mult(clefSecrete);// on calcule notre clef secrte, qui nous servira  crypter les messages (h)

    BigInteger m = new BigInteger(Base64.decodeBase64("bonjour lesil"));

    BigInteger k = BigInteger.probablePrime(7, rnd);// on choisit un k random

    PointGMP C1 = generateur.mult(k);// on multiplie le gnrateur par k
    PointGMP tmp = publicKey.mult(k);
    BigInteger C2 = m.add(tmp.getX());// on multiplie notre clef secrte par k

    // Dcryptage
    PointGMP temp = C1.mult(clefSecrete);// on multiplie C1 par notre clef secrte
    BigInteger messageTraduit = C2.subtract(temp.getX());// et on obtient le message en ajoutant m2 avec l'oppos de temp
    messageTraduit = messageTraduit.mod(curve.getP());

    if (m == messageTraduit)
        System.out.println("El Gamal fonctionnel");
    else
        System.out.println("El Gamal non fonctionnel");

    System.out.println("Message a retrouver " + m + " " + Base64.encodeBase64String(m.toByteArray()) + "\n");
    System.out.println("Resultat El gamal " + messageTraduit + " "
            + Base64.encodeBase64String(messageTraduit.toByteArray()) + "\n");

}

From source file:Main.java

private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) {
    BigInteger beta = null;//from  w  w  w.ja  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:jp.co.ntts.vhut.util.Ipv4ConversionUtil.java

/**
 * ??IP???.//from  w  w w .  ja  va2  s . c o  m
 * @param startIpaddr (HEX/)
 * @param endIpaddr (HEX/)
 * @return IP?
 */
public static int getHostAddressCount(String startIpaddr, String endIpaddr) {
    BigInteger startBI = new BigInteger(addrTobyte(startIpaddr));
    BigInteger endBI = new BigInteger(addrTobyte(endIpaddr));
    return (int) Math.min(endBI.subtract(startBI).longValue() + 1, Integer.MAX_VALUE);
}

From source file:jp.co.ntts.vhut.util.Ipv4ConversionUtil.java

/**
 * IP(HEX)????.//from w  w w . j  a  va  2s . c  o  m
 * ????int????????????.
 * @param startIpaddr (HEX/)
 * @param endIpaddr (HEX/)
 * @return IP(HEX)??
 */
public static Set<String> getIpAddressSetBetween(String startIpaddr, String endIpaddr) {
    BigInteger startBI = new BigInteger(addrTobyte(startIpaddr));
    BigInteger endBI = new BigInteger(addrTobyte(endIpaddr));
    int length = (int) Math.min(endBI.subtract(startBI).longValue(), Integer.MAX_VALUE);

    Set<String> resultSet = new HashSet<String>();

    BigInteger currentBI = startBI;

    for (int i = 0; i <= length; i++) {
        resultSet.add(byteToAddr(currentBI.toByteArray()));
        currentBI = currentBI.add(BigInteger.ONE);
    }

    return resultSet;
}