Here are the examples of the java api class java.math.BigInteger taken from open source projects.
1. OverloadedNumberUtilTest#testBigIntegerCoercion()
View licensepublic void testBigIntegerCoercion() { BigInteger bi; cipEqu(BigInteger.ZERO, new OverloadedNumberUtil.BigIntegerOrByte(BigInteger.ZERO)); bi = new BigInteger(String.valueOf(Byte.MAX_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrByte(bi)); bi = new BigInteger(String.valueOf(Byte.MIN_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrByte(bi)); bi = new BigInteger(String.valueOf(Byte.MAX_VALUE + 1)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrShort(bi)); bi = new BigInteger(String.valueOf(Byte.MIN_VALUE - 1)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrShort(bi)); bi = new BigInteger(String.valueOf(Short.MAX_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrShort(bi)); bi = new BigInteger(String.valueOf(Short.MIN_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrShort(bi)); bi = new BigInteger(String.valueOf(Short.MAX_VALUE + 1)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrInteger(bi)); bi = new BigInteger(String.valueOf(Short.MIN_VALUE - 1)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrInteger(bi)); bi = new BigInteger(String.valueOf(Integer.MAX_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrInteger(bi)); bi = new BigInteger(String.valueOf(Integer.MIN_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrInteger(bi)); bi = new BigInteger(String.valueOf(Integer.MAX_VALUE + 1L)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrLong(bi)); bi = new BigInteger(String.valueOf(Integer.MIN_VALUE - 1L)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrLong(bi)); bi = new BigInteger(String.valueOf(Long.MAX_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrLong(bi)); bi = new BigInteger(String.valueOf(Long.MIN_VALUE)); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrLong(bi)); cipEqu(new BigInteger(String.valueOf(Long.MAX_VALUE)).add(BigInteger.ONE)); cipEqu(new BigInteger(String.valueOf(Long.MIN_VALUE)).subtract(BigInteger.ONE)); bi = new BigInteger("0"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.DOUBLE | TypeFlags.FLOAT); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.FLOAT); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.DOUBLE); bi = new BigInteger("16777215"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); bi = new BigInteger("-16777215"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); bi = new BigInteger("16777216"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); bi = new BigInteger("-16777216"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrFloat(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); bi = new BigInteger("16777217"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, TypeFlags.FLOAT); bi = new BigInteger("-16777217"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, TypeFlags.FLOAT); bi = new BigInteger("9007199254740991"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.DOUBLE); bi = new BigInteger("-9007199254740991"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.DOUBLE); bi = new BigInteger("9007199254740992"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.DOUBLE); bi = new BigInteger("-9007199254740992"); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, new OverloadedNumberUtil.BigIntegerOrDouble(bi), TypeFlags.DOUBLE); bi = new BigInteger("9007199254740993"); cipEqu(bi, TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, TypeFlags.DOUBLE | TypeFlags.FLOAT); cipEqu(bi, TypeFlags.FLOAT); cipEqu(bi, TypeFlags.DOUBLE); bi = new BigInteger("-9007199254740993"); cipEqu(bi, TypeFlags.MASK_KNOWN_NONINTEGERS); cipEqu(bi, TypeFlags.DOUBLE | TypeFlags.FLOAT); cipEqu(bi, TypeFlags.FLOAT); cipEqu(bi, TypeFlags.DOUBLE); bi = new BigInteger("9007199254740994"); cipEqu(bi, TypeFlags.MASK_KNOWN_NONINTEGERS); bi = new BigInteger("-9007199254740994"); cipEqu(bi, TypeFlags.MASK_KNOWN_NONINTEGERS); }
2. ECComputer#dbl()
View license/** * Doubles point, does not check for infinity * * @param p * @return * @throws DivisionException * exception occurs if y coordinate is zero */ public Point dbl(Point p) throws DivisionException { BigInteger x = p.getX(); BigInteger y = p.getY(); if (y.equals(BigInteger.ZERO)) { throw new DivisionException("y was equal to zero"); } BigInteger l1 = ((THREE.multiply(x.pow(2))).add(curve.getA())); BigInteger l2 = TWO.multiply(y).modInverse(curve.getP()); BigInteger l = l1.multiply(l2).mod(curve.getP()); BigInteger xr = l.pow(2).subtract(TWO.multiply(x)).mod(curve.getP()); BigInteger yr = l.multiply(x.subtract(xr)).subtract(y).mod(curve.getP()); Point ret = new Point(xr, yr); return ret; }
3. PKCS1EncodedKeySpec#decode()
View license/** * Decode PKCS#1 encoded private key into RSAPrivateCrtKeySpec. * * <p/>The ASN.1 syntax for the private key with CRT is * * <pre> * -- * -- Representation of RSA private key with information for the CRT algorithm. * -- * RSAPrivateKey ::= SEQUENCE { * version Version, * modulus INTEGER, -- n * publicExponent INTEGER, -- e * privateExponent INTEGER, -- d * prime1 INTEGER, -- p * prime2 INTEGER, -- q * exponent1 INTEGER, -- d mod (p-1) * exponent2 INTEGER, -- d mod (q-1) * coefficient INTEGER, -- (inverse of q) mod p * otherPrimeInfos OtherPrimeInfos OPTIONAL * } * </pre> * * @param keyBytes PKCS#1 encoded key * @throws IOException */ private void decode(byte[] keyBytes) throws IOException { DerParser parser = new DerParser(keyBytes); Asn1Object sequence = parser.read(); if (sequence.getType() != DerParser.SEQUENCE) //$NON-NLS-1$ throw new IOException("Invalid DER: not a sequence"); // Parse inside the sequence parser = sequence.getParser(); // Skip version parser.read(); BigInteger modulus = parser.read().getInteger(); BigInteger publicExp = parser.read().getInteger(); BigInteger privateExp = parser.read().getInteger(); BigInteger prime1 = parser.read().getInteger(); BigInteger prime2 = parser.read().getInteger(); BigInteger exp1 = parser.read().getInteger(); BigInteger exp2 = parser.read().getInteger(); BigInteger crtCoef = parser.read().getInteger(); keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); }
4. SM#proofEqualCoords()
View license/** * Proof of knowledge of coordinates with first components being equal * * @throws SMException */ public static BigInteger[] proofEqualCoords(SMState state, BigInteger r, int version) throws SMException { BigInteger r1 = randomExponent(); BigInteger r2 = randomExponent(); /* Compute the value of c, as c = h(g3^r1, g1^r1 g2^r2) */ BigInteger temp1 = state.g1.modPow(r1, MODULUS_S); BigInteger temp2 = state.g2.modPow(r2, MODULUS_S); temp2 = temp1.multiply(temp2).mod(MODULUS_S); temp1 = state.g3.modPow(r1, MODULUS_S); BigInteger c = hash(version, temp1, temp2); /* Compute the d values, as d1 = r1 - r c, d2 = r2 - secret c */ temp1 = r.multiply(c).mod(ORDER_S); BigInteger d1 = r1.subtract(temp1).mod(ORDER_S); temp1 = state.secret.multiply(c).mod(ORDER_S); BigInteger d2 = r2.subtract(temp1).mod(ORDER_S); BigInteger[] ret = new BigInteger[3]; ret[0] = c; ret[1] = d1; ret[2] = d2; return ret; }
5. DSA#generateV()
View licenseprivate BigInteger generateV(BigInteger y, BigInteger p, BigInteger q, BigInteger g, BigInteger w, BigInteger r) throws SignatureException { byte[] s2 = getDigest(); BigInteger temp = new BigInteger(1, s2); temp = temp.multiply(w); BigInteger u1 = temp.remainder(q); BigInteger u2 = (r.multiply(w)).remainder(q); BigInteger t1 = g.modPow(u1, p); BigInteger t2 = y.modPow(u2, p); BigInteger t3 = t1.multiply(t2); BigInteger t5 = t3.remainder(p); return t5.remainder(q); }
6. PrivateKeyReader#getRSAKeySpec()
View license/** * Convert PKCS#1 encoded private key into RSAPrivateCrtKeySpec. * <p/> * <p/>The ASN.1 syntax for the private key with CRT is * <p/> * <pre> * -- * -- Representation of RSA private key with information for the CRT algorithm. * -- * RSAPrivateKey ::= SEQUENCE { * version Version, * modulus INTEGER, -- n * publicExponent INTEGER, -- e * privateExponent INTEGER, -- d * prime1 INTEGER, -- p * prime2 INTEGER, -- q * exponent1 INTEGER, -- d mod (p-1) * exponent2 INTEGER, -- d mod (q-1) * coefficient INTEGER, -- (inverse of q) mod p * otherPrimeInfos OtherPrimeInfos OPTIONAL * } * </pre> * * @param keyBytes PKCS#1 encoded key * @return KeySpec * @throws IOException */ private static RSAPrivateCrtKeySpec getRSAKeySpec(byte[] keyBytes) throws IOException { DerParser parser = new DerParser(keyBytes); Asn1Object sequence = parser.read(); if (sequence.getType() != DerParser.SEQUENCE) { throw new IOException("Invalid DER: not a sequence"); } // Parse inside the sequence parser = sequence.getParser(); // Skip version parser.read(); BigInteger modulus = parser.read().getInteger(); BigInteger publicExp = parser.read().getInteger(); BigInteger privateExp = parser.read().getInteger(); BigInteger prime1 = parser.read().getInteger(); BigInteger prime2 = parser.read().getInteger(); BigInteger exp1 = parser.read().getInteger(); BigInteger exp2 = parser.read().getInteger(); BigInteger crtCoef = parser.read().getInteger(); return new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); }
7. Tnaf#approximateDivisionByN()
View license/** * Approximate division by <code>n</code>. For an integer * <code>k</code>, the value <code>λ = s k / n</code> is * computed to <code>c</code> bits of accuracy. * @param k The parameter <code>k</code>. * @param s The curve parameter <code>s<sub>0</sub></code> or * <code>s<sub>1</sub></code>. * @param vm The Lucas Sequence element <code>V<sub>m</sub></code>. * @param a The parameter <code>a</code> of the elliptic curve. * @param m The bit length of the finite field * <code><b>F</b><sub>m</sub></code>. * @param c The number of bits of accuracy, i.e. the scale of the returned * <code>SimpleBigDecimal</code>. * @return The value <code>λ = s k / n</code> computed to * <code>c</code> bits of accuracy. */ public static SimpleBigDecimal approximateDivisionByN(BigInteger k, BigInteger s, BigInteger vm, byte a, int m, int c) { int _k = (m + 5) / 2 + c; BigInteger ns = k.shiftRight(m - _k - 2 + a); BigInteger gs = s.multiply(ns); BigInteger hs = gs.shiftRight(m); BigInteger js = vm.multiply(hs); BigInteger gsPlusJs = gs.add(js); BigInteger ls = gsPlusJs.shiftRight(_k - c); if (gsPlusJs.testBit(_k - c - 1)) { // round up ls = ls.add(ECConstants.ONE); } return new SimpleBigDecimal(ls, c); }
8. Tnaf#approximateDivisionByN()
View license/** * Approximate division by <code>n</code>. For an integer * <code>k</code>, the value <code>λ = s k / n</code> is * computed to <code>c</code> bits of accuracy. * @param k The parameter <code>k</code>. * @param s The curve parameter <code>s<sub>0</sub></code> or * <code>s<sub>1</sub></code>. * @param vm The Lucas Sequence element <code>V<sub>m</sub></code>. * @param a The parameter <code>a</code> of the elliptic curve. * @param m The bit length of the finite field * <code><b>F</b><sub>m</sub></code>. * @param c The number of bits of accuracy, i.e. the scale of the returned * <code>SimpleBigDecimal</code>. * @return The value <code>λ = s k / n</code> computed to * <code>c</code> bits of accuracy. */ public static SimpleBigDecimal approximateDivisionByN(BigInteger k, BigInteger s, BigInteger vm, byte a, int m, int c) { int _k = (m + 5) / 2 + c; BigInteger ns = k.shiftRight(m - _k - 2 + a); BigInteger gs = s.multiply(ns); BigInteger hs = gs.shiftRight(m); BigInteger js = vm.multiply(hs); BigInteger gsPlusJs = gs.add(js); BigInteger ls = gsPlusJs.shiftRight(_k - c); if (gsPlusJs.testBit(_k - c - 1)) { // round up ls = ls.add(ECConstants.ONE); } return new SimpleBigDecimal(ls, c); }
9. Tnaf#approximateDivisionByN()
View license/** * Approximate division by <code>n</code>. For an integer * <code>k</code>, the value <code>λ = s k / n</code> is * computed to <code>c</code> bits of accuracy. * @param k The parameter <code>k</code>. * @param s The curve parameter <code>s<sub>0</sub></code> or * <code>s<sub>1</sub></code>. * @param vm The Lucas Sequence element <code>V<sub>m</sub></code>. * @param a The parameter <code>a</code> of the elliptic curve. * @param m The bit length of the finite field * <code><b>F</b><sub>m</sub></code>. * @param c The number of bits of accuracy, i.e. the scale of the returned * <code>SimpleBigDecimal</code>. * @return The value <code>λ = s k / n</code> computed to * <code>c</code> bits of accuracy. */ public static SimpleBigDecimal approximateDivisionByN(BigInteger k, BigInteger s, BigInteger vm, byte a, int m, int c) { int _k = (m + 5) / 2 + c; BigInteger ns = k.shiftRight(m - _k - 2 + a); BigInteger gs = s.multiply(ns); BigInteger hs = gs.shiftRight(m); BigInteger js = vm.multiply(hs); BigInteger gsPlusJs = gs.add(js); BigInteger ls = gsPlusJs.shiftRight(_k - c); if (gsPlusJs.testBit(_k - c - 1)) { // round up ls = ls.add(ECConstants.ONE); } return new SimpleBigDecimal(ls, c); }
10. ECComputer#add()
View license/** * Provides point addition, without infinity check * * @param p * @param q * @return * @throws DivisionException * exception thrown if xq=xp, since then we divide with zero */ public Point add(Point p, Point q) throws DivisionException { BigInteger xp = p.getX(); BigInteger yp = p.getY(); BigInteger xq = q.getX(); BigInteger yq = q.getY(); if (xq.subtract(xp).mod(curve.getP()).equals(BigInteger.ZERO)) { throw new DivisionException("xq was equal to xp (mod p)"); } BigInteger l = ((yq.subtract(yp)).multiply((xq.subtract(xp)).modInverse(curve.getP()))).mod(curve.getP()); BigInteger xr = l.pow(2).subtract(xp).subtract(xq).mod(curve.getP()); BigInteger yr = (l.multiply(xp.subtract(xr))).subtract(yp).mod(curve.getP()); Point ret = new Point(xr, yr); return ret; }
11. MathHelper#extendedEuclid()
View license/** * * @param u * @param v * @return (c,r,s) such that c = r u + s v */ public static BigIntegerTripple extendedEuclid(BigInteger u, BigInteger v) { BigInteger r = BigInteger.ONE; BigInteger s = BigInteger.ZERO; BigInteger c = u; BigInteger v1 = BigInteger.ZERO; BigInteger v2 = BigInteger.ONE; BigInteger v3 = v; while (!v3.equals(BigInteger.ZERO)) { BigInteger q = c.divide(v3); BigInteger t1 = r.subtract(q.multiply(v1)); BigInteger t2 = s.subtract(q.multiply(v2)); BigInteger t3 = c.subtract(q.multiply(v3)); r = v1; s = v2; c = v3; v1 = t1; v2 = t2; v3 = t3; } return new BigIntegerTripple(c, r, s); }
12. PrimeTest#checkPrime()
View license/** * Verifies whether the fraction of probable primes detected is at least 1 - * 1/2^certainty. * * @return true if and only if the test succeeds */ private static boolean checkPrime(Set<BigInteger> primes, int certainty, boolean parallel) { long probablePrimes = (parallel ? primes.parallelStream() : primes.stream()).filter( bi -> bi.isProbablePrime(certainty)).count(); // N = certainty / 2 // Success if p/t >= 1 - 1/4^N // or (p/t)*4^N >= 4^N - 1 // or p*4^N >= t*(4^N - 1) BigInteger p = BigInteger.valueOf(probablePrimes); BigInteger t = BigInteger.valueOf(primes.size()); BigInteger fourToTheC = BigInteger.valueOf(4).pow(certainty / 2); BigInteger fourToTheCMinusOne = fourToTheC.subtract(BigInteger.ONE); BigInteger left = p.multiply(fourToTheC); BigInteger right = t.multiply(fourToTheCMinusOne); if (left.compareTo(right) < 0) { System.err.println("Probable prime certainty test failed"); } return left.compareTo(right) >= 0; }
13. SM#proofEqualLogs()
View license/** * Proof of knowledge of logs with exponents being equal * * @throws SMException */ public static BigInteger[] proofEqualLogs(SMState state, int version) throws SMException { BigInteger r = randomExponent(); /* Compute the value of c, as c = h(g1^r, (Qa/Qb)^r) */ BigInteger temp1 = state.g1.modPow(r, MODULUS_S); BigInteger temp2 = state.qab.modPow(r, MODULUS_S); BigInteger c = hash(version, temp1, temp2); /* Compute the d values, as d = r - x3 c */ temp1 = state.x3.multiply(c).mod(ORDER_S); BigInteger d = r.subtract(temp1).mod(ORDER_S); BigInteger[] ret = new BigInteger[2]; ret[0] = c; ret[1] = d; return ret; }
14. StatsUtils#getHistogramBigInt()
View license/** * Same as <code>getHistogram</code> but operates on <code>BigIntegers</code>. */ public static List<Integer> getHistogramBigInt(List<BigInteger> data, int breaks) { if (data.isEmpty()) { return Collections.emptyList(); } List<Integer> ret = new ArrayList<Integer>(breaks); for (int i = 0; i < breaks; i++) { ret.add(0); } BigInteger min = Collections.min(data); BigInteger max = Collections.max(data); BigInteger range = max.subtract(min).add(BigInteger.valueOf(1)); BigInteger step = range.divide(BigInteger.valueOf(breaks)); if (step.equals(BigInteger.ZERO)) { // too small return Collections.emptyList(); } for (BigInteger point : data) { int index = point.subtract(min).divide(step).intValue(); // Math.min necessary because rounding error -> AIOOBE index = Math.min(index, breaks - 1); ret.set(index, ret.get(index) + 1); } return ret; }
15. JPAKEUtil#calculateZeroKnowledgeProof()
View license/** * Calculate a zero knowledge proof of x using Schnorr's signature. * The returned array has two elements {g^v, r = v-x*h} for x. */ public static BigInteger[] calculateZeroKnowledgeProof(BigInteger p, BigInteger q, BigInteger g, BigInteger gx, BigInteger x, String participantId, Digest digest, SecureRandom random) { BigInteger[] zeroKnowledgeProof = new BigInteger[2]; /* Generate a random v, and compute g^v */ BigInteger vMin = ZERO; BigInteger vMax = q.subtract(ONE); BigInteger v = BigIntegers.createRandomInRange(vMin, vMax, random); BigInteger gv = g.modPow(v, p); // h BigInteger h = calculateHashForZeroKnowledgeProof(g, gv, gx, participantId, digest); zeroKnowledgeProof[0] = gv; // r = v-x*h zeroKnowledgeProof[1] = v.subtract(x.multiply(h)).mod(q); return zeroKnowledgeProof; }
16. JPAKEParticipant#createRound2PayloadToSend()
View license/** * Creates and returns the payload to send to the other participant during round 2. * <p> * {@link #validateRound1PayloadReceived(JPAKERound1Payload)} must be called prior to this method. * <p> * After execution, the {@link #getState() state} will be {@link #STATE_ROUND_2_CREATED}. * * @throws IllegalStateException if called prior to {@link #validateRound1PayloadReceived(JPAKERound1Payload)}, or multiple times */ public JPAKERound2Payload createRound2PayloadToSend() { if (this.state >= STATE_ROUND_2_CREATED) { throw new IllegalStateException("Round2 payload already created for " + this.participantId); } if (this.state < STATE_ROUND_1_VALIDATED) { throw new IllegalStateException("Round1 payload must be validated prior to creating Round2 payload for " + this.participantId); } BigInteger gA = JPAKEUtil.calculateGA(p, gx1, gx3, gx4); BigInteger s = JPAKEUtil.calculateS(password); BigInteger x2s = JPAKEUtil.calculateX2s(q, x2, s); BigInteger A = JPAKEUtil.calculateA(p, q, gA, x2s); BigInteger[] knowledgeProofForX2s = JPAKEUtil.calculateZeroKnowledgeProof(p, q, gA, A, x2s, participantId, digest, random); this.state = STATE_ROUND_2_CREATED; return new JPAKERound2Payload(participantId, A, knowledgeProofForX2s); }
17. CramerShoupParametersGenerator#generateParameters()
View license/** * which generates the p and g values from the given parameters, returning * the CramerShoupParameters object. * <p> * Note: can take a while... * </p> */ public CramerShoupParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = ParametersHelper.generateSafePrimes(size, certainty, random); // BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g1 = ParametersHelper.selectGenerator(q, random); BigInteger g2 = ParametersHelper.selectGenerator(q, random); while (g1.equals(g2)) { g2 = ParametersHelper.selectGenerator(q, random); } return new CramerShoupParameters(q, g1, g2, new SHA256Digest()); }
18. AbstractConfig#calcDifficulty()
View license@Override public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) { BigInteger pd = parent.getDifficultyBI(); BigInteger quotient = pd.divide(getConstants().getDIFFICULTY_BOUND_DIVISOR()); BigInteger sign = getCalcDifficultyMultiplier(curBlock, parent); BigInteger fromParent = pd.add(quotient.multiply(sign)); BigInteger difficulty = max(getConstants().getMINIMUM_DIFFICULTY(), fromParent); int periodCount = (int) (curBlock.getNumber() / getConstants().getEXP_DIFFICULTY_PERIOD()); if (periodCount > 1) { difficulty = max(getConstants().getMINIMUM_DIFFICULTY(), difficulty.add(BigInteger.ONE.shiftLeft(periodCount - 2))); } return difficulty; }
19. MemoryRangeCalculator#calculateRequestRange()
View license/** * Calculates the range of memory to request from the debug client. For performance reasons this * range is different from the range specified by the user. * * @param offset The start offset of the range according to the user. * @param size The number of bytes to request according to the user. * @param sectionStart Beginning of the section the offset belongs to. * @param sectionEnd End of the section the offset belongs to. * * @return A pair that contains the real start offset and the real size information of the * request. */ public static Pair<IAddress, Integer> calculateRequestRange(final BigInteger offset, final int size, final IAddress sectionStart, final IAddress sectionEnd) { // To smoothen scrolling we try to load a range that is at max // +- 3 requested ranges. final BigInteger availableBefore = offset.subtract(sectionStart.toBigInteger()); final BigInteger availableAfter = sectionEnd.toBigInteger().subtract(offset).add(BigInteger.ONE); final BigInteger loadBefore = availableBefore.compareTo(BigInteger.valueOf(3L * size)) == -1 ? availableBefore : BigInteger.valueOf(3L * size); final BigInteger loadAfter = availableAfter.compareTo(BigInteger.valueOf(3L * size)) == -1 ? availableAfter : BigInteger.valueOf(3L * size); final BigInteger realOffset = offset.subtract(loadBefore); final int realSize = (int) (loadBefore.add(loadAfter)).longValue(); return new Pair<IAddress, Integer>(new CAddress(realOffset), realSize); }
20. JPAKEParticipant#createRound2PayloadToSend()
View license/** * Creates and returns the payload to send to the other participant during round 2. * <p> * {@link #validateRound1PayloadReceived(JPAKERound1Payload)} must be called prior to this method. * </p> * <p> * After execution, the {@link #getState() state} will be {@link #STATE_ROUND_2_CREATED}. * </p> * @throws IllegalStateException if called prior to {@link #validateRound1PayloadReceived(JPAKERound1Payload)}, or multiple times */ public JPAKERound2Payload createRound2PayloadToSend() { if (this.state >= STATE_ROUND_2_CREATED) { throw new IllegalStateException("Round2 payload already created for " + this.participantId); } if (this.state < STATE_ROUND_1_VALIDATED) { throw new IllegalStateException("Round1 payload must be validated prior to creating Round2 payload for " + this.participantId); } BigInteger gA = JPAKEUtil.calculateGA(p, gx1, gx3, gx4); BigInteger s = JPAKEUtil.calculateS(password); BigInteger x2s = JPAKEUtil.calculateX2s(q, x2, s); BigInteger A = JPAKEUtil.calculateA(p, q, gA, x2s); BigInteger[] knowledgeProofForX2s = JPAKEUtil.calculateZeroKnowledgeProof(p, q, gA, A, x2s, participantId, digest, random); this.state = STATE_ROUND_2_CREATED; return new JPAKERound2Payload(participantId, A, knowledgeProofForX2s); }
21. SM#proofKnowLog()
View license/** * Proof of knowledge of a discrete logarithm * * @throws SMException */ public static BigInteger[] proofKnowLog(BigInteger g, BigInteger x, int version) throws SMException { BigInteger r = randomExponent(); BigInteger temp = g.modPow(r, SM.MODULUS_S); BigInteger c = hash(version, temp, null); temp = x.multiply(c).mod(ORDER_S); BigInteger d = r.subtract(temp).mod(ORDER_S); BigInteger[] ret = new BigInteger[2]; ret[0] = c; ret[1] = d; return ret; }
22. TokenManagerTest#testMultiToken()
View license@Test public void testMultiToken() { int h1 = tokenManager.regionOffset("vijay"); int h2 = tokenManager.regionOffset("vijay2"); BigInteger t1 = tokenManager.initialToken(100, 10, h1); BigInteger t2 = tokenManager.initialToken(100, 10, h2); BigInteger tokendistance = t1.subtract(t2).abs(); int hashDiffrence = h1 - h2; assertEquals(new BigInteger("" + hashDiffrence).abs(), tokendistance); BigInteger t3 = tokenManager.initialToken(100, 99, h1); BigInteger t4 = tokenManager.initialToken(100, 99, h2); tokendistance = t3.subtract(t4).abs(); assertEquals(new BigInteger("" + hashDiffrence).abs(), tokendistance); }
23. MathHelper#CRT()
View license/** * Computes Chinese Reminder Theorem: x == congs[i] mod moduli[i] * * @param congs * @param moduli * @return */ public static BigInteger CRT(BigInteger[] congs, BigInteger[] moduli) { BigInteger prodModuli = BigInteger.ONE; for (BigInteger mod : moduli) { prodModuli = prodModuli.multiply(mod); } BigInteger[] M = new BigInteger[moduli.length]; for (int i = 0; i < moduli.length; i++) { M[i] = prodModuli.divide(moduli[i]); } BigInteger retval = BigInteger.ZERO; for (int i = 0; i < moduli.length; i++) { // get s value from EEA BigInteger tmp = extendedEuclid(moduli[i], M[i]).c; retval = retval.add(congs[i].multiply(tmp).multiply(M[i]).mod(prodModuli)); } return retval.mod(prodModuli); }
24. DSAKeyValueType#convertToPublicKey()
View license/** * Convert to the JDK representation of a DSA Public Key * * @return * * @throws org.keycloak.saml.common.exceptions.ProcessingException */ public DSAPublicKey convertToPublicKey() throws ProcessingException { BigInteger BigY, BigP, BigQ, BigG; BigY = new BigInteger(1, massage(Base64.decode(new String(y)))); BigP = new BigInteger(1, massage(Base64.decode(new String(p)))); BigQ = new BigInteger(1, massage(Base64.decode(new String(q)))); BigG = new BigInteger(1, massage(Base64.decode(new String(g)))); try { KeyFactory dsaKeyFactory = KeyFactory.getInstance("dsa"); DSAPublicKeySpec kspec = new DSAPublicKeySpec(BigY, BigP, BigQ, BigG); return (DSAPublicKey) dsaKeyFactory.generatePublic(kspec); } catch (Exception e) { throw new ProcessingException(e); } }
25. DSAKeyValueType#convertToPrivateKey()
View license/** * Convert to the JDK representation of a DSA Private Key * * @return * * @throws ProcessingException */ public DSAPrivateKey convertToPrivateKey() throws ProcessingException { BigInteger BigY, BigP, BigQ, BigG; BigY = new BigInteger(1, massage(Base64.decode(new String(y)))); BigP = new BigInteger(1, massage(Base64.decode(new String(p)))); BigQ = new BigInteger(1, massage(Base64.decode(new String(q)))); BigG = new BigInteger(1, massage(Base64.decode(new String(g)))); try { KeyFactory dsaKeyFactory = KeyFactory.getInstance("dsa"); DSAPrivateKeySpec kspec = new DSAPrivateKeySpec(BigY, BigP, BigQ, BigG); return (DSAPrivateKey) dsaKeyFactory.generatePrivate(kspec); } catch (Exception e) { throw new ProcessingException(e); } }
26. BigIntegerModPowTest#testModPowNegExp()
View license/** * modPow: negative exponent */ public void testModPowNegExp() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75, 48, -7 }; byte eBytes[] = { 27, -15, 65, 39 }; byte mBytes[] = { -128, 2, 3, 4, 5 }; int aSign = 1; int eSign = -1; int mSign = 1; byte rBytes[] = { 12, 118, 46, 86, 92 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger exp = new BigInteger(eSign, eBytes); BigInteger modulus = new BigInteger(mSign, mBytes); BigInteger result = aNumber.modPow(exp, modulus); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
27. DSA#generateS()
View licenseprivate BigInteger generateS(BigInteger x, BigInteger q, BigInteger r, BigInteger k) throws SignatureException { byte[] s2 = getDigest(); BigInteger temp = new BigInteger(1, s2); BigInteger k1 = k.modInverse(q); BigInteger s = x.multiply(r); s = temp.add(s); s = k1.multiply(s); return s.remainder(q); }
28. BigIntegerModPowTest#testModPowPosExp()
View license/** * modPow: positive exponent */ public void testModPowPosExp() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75, 48, -7 }; byte eBytes[] = { 27, -15, 65, 39 }; byte mBytes[] = { -128, 2, 3, 4, 5 }; int aSign = 1; int eSign = 1; int mSign = 1; byte rBytes[] = { 113, 100, -84, -28, -85 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger exp = new BigInteger(eSign, eBytes); BigInteger modulus = new BigInteger(mSign, mBytes); BigInteger result = aNumber.modPow(exp, modulus); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
29. DHTest#testBounds()
View licenseprivate void testBounds() { BigInteger p1 = new BigInteger("00C8028E9151C6B51BCDB35C1F6B2527986A72D8546AE7A4BF41DC4289FF9837EE01592D36C324A0F066149B8B940C86C87D194206A39038AE3396F8E12435BB74449B70222D117B8A2BB77CB0D67A5D664DDE7B75E0FEC13CE0CAF258DAF3ADA0773F6FF0F2051D1859929AAA53B07809E496B582A89C3D7DA8B6E38305626621", 16); BigInteger g1 = new BigInteger("1F869713181464577FE4026B47102FA0D7675503A4FCDA810881FAEC3524E6DBAEA9B96561EF7F8BEA76466DF11C2F3EB1A90CC5851735BF860606481257EECE6418C0204E61004E85D7131CE54BCBC7AD67E53C79DCB715E7C8D083DCD85D728283EC8F96839B4C9FA7C0727C472BEB94E4613CAFA8D580119C0AF4BF8AF252", 16); int l1 = 1023; BigInteger p2 = new BigInteger("00B333C98720220CC3946F494E25231B3E19F9AD5F6B19F4E7ABF80D8826C491C3224D4F7415A14A7C11D1BE584405FED12C3554F103E56A72D986CA5E325BB9DE07AC37D1EAE5E5AC724D32EF638F0E4462D4C1FC7A45B9FD3A5DF5EC36A1FA4DAA3FBB66AA42B1B71DF416AB547E987513426C7BB8634F5F4D37705514FDC1E1", 16); BigInteger g2 = new BigInteger("2592F5A99FE46313650CCE66C94C15DBED9F4A45BD05C329986CF5D3E12139F0405A47C6385FEA27BFFEDC4CBABC5BB151F3BEE7CC3D51567F1E2B12A975AA9F48A70BDAAE7F5B87E70ADCF902490A3CBEFEDA41EBA8E12E02B56120B5FDEFBED07F5EAD3AE020DF3C8233216F8F0D35E13A7AE4DA5CBCC0D91EADBF20C281C6", 16); int l2 = 1024; DHKeyGenerationParameters params1 = new DHKeyGenerationParameters(new SecureRandom(), new DHParameters(p1, g1, null, l1)); DHKeyGenerationParameters params2 = new DHKeyGenerationParameters(new SecureRandom(), new DHParameters(p2, g2, null, l2)); DHBasicKeyPairGenerator kpGen = new DHBasicKeyPairGenerator(); kpGen.init(params1); kpGen.init(params2); }
30. DiscoverEndomorphisms#findBetaValues()
View licenseprivate static ECFieldElement[] findBetaValues(ECCurve c) { BigInteger q = c.getField().getCharacteristic(); BigInteger e = q.divide(ECConstants.THREE); // Search for a random value that generates a non-trival cube root of 1 SecureRandom random = new SecureRandom(); BigInteger b; do { BigInteger r = BigIntegers.createRandomInRange(ECConstants.TWO, q.subtract(ECConstants.TWO), random); b = r.modPow(e, q); } while (b.equals(ECConstants.ONE)); ECFieldElement beta = c.fromBigInteger(b); return new ECFieldElement[] { beta, beta.square() }; }
31. GLVTypeBEndomorphism#decomposeScalar()
View licensepublic BigInteger[] decomposeScalar(BigInteger k) { int bits = parameters.getBits(); BigInteger b1 = calculateB(k, parameters.getG1(), bits); BigInteger b2 = calculateB(k, parameters.getG2(), bits); GLVTypeBParameters p = parameters; BigInteger a = k.subtract((b1.multiply(p.getV1A())).add(b2.multiply(p.getV2A()))); BigInteger b = (b1.multiply(p.getV1B())).add(b2.multiply(p.getV2B())).negate(); return new BigInteger[] { a, b }; }
32. TimeUtil#deltaMs()
View license/** * Get the approximate delta between two monotonic times. * * This function makes the following assumptions: * 1. We read startMs from the monotonic clock prior to endMs. * 2. The two times are not more than 100 years or so apart. * * With these two assumptions in hand, we can smooth over some of the * unpleasant features of the monotonic clock: * 1. It can return either positive or negative values. * 2. When the number of nanoseconds reaches Long.MAX_VALUE it wraps around * to Long.MIN_VALUE. * 3. On some messed up systems it has been known to jump backwards every * now and then. Oops. CPU core synchronization mumble mumble. * * @param startMs The start time. * @param endMs The end time. * @return The delta between the two times. */ static long deltaMs(long startMs, long endMs) { BigInteger startNs = BigInteger.valueOf(TimeUnit.NANOSECONDS.convert(startMs, TimeUnit.MILLISECONDS)); BigInteger endNs = BigInteger.valueOf(TimeUnit.NANOSECONDS.convert(endMs, TimeUnit.MILLISECONDS)); BigInteger deltaNs = endNs.subtract(startNs); if (deltaNs.signum() >= 0) { return TimeUnit.MILLISECONDS.convert(deltaNs.min(BigInteger.valueOf(Long.MAX_VALUE)).longValue(), TimeUnit.NANOSECONDS); } deltaNs = deltaNs.negate(); if (deltaNs.compareTo(BigInteger.valueOf(Long.MAX_VALUE / 2)) < 0) { // the name). return 0L; } // Handle rollover. BigInteger revDeltaNs = BigInteger.ONE.shiftLeft(64).subtract(deltaNs); return TimeUnit.MILLISECONDS.convert(revDeltaNs.min(BigInteger.valueOf(Long.MAX_VALUE)).longValue(), TimeUnit.NANOSECONDS); }
33. DSASHA1Verify#generateSignature()
View licensepublic static DSASignature generateSignature(byte[] message, DSAPrivateKey pk, SecureRandom rnd) { SHA1 md = new SHA1(); md.update(message); byte[] sha_message = new byte[md.getDigestLength()]; md.digest(sha_message); BigInteger m = new BigInteger(1, sha_message); BigInteger k; int qBitLength = pk.getQ().bitLength(); do { k = new BigInteger(qBitLength, rnd); } while (k.compareTo(pk.getQ()) >= 0); BigInteger r = pk.getG().modPow(k, pk.getP()).mod(pk.getQ()); k = k.modInverse(pk.getQ()).multiply(m.add((pk).getX().multiply(r))); BigInteger s = k.mod(pk.getQ()); return new DSASignature(r, s); }
34. DSASHA1Verify#decodeSSHDSAPublicKey()
View licensepublic static DSAPublicKey decodeSSHDSAPublicKey(byte[] key) throws IOException { TypesReader tr = new TypesReader(key); String key_format = tr.readString(); if (key_format.equals("ssh-dss") == false) throw new IllegalArgumentException("This is not a ssh-dss public key!"); BigInteger p = tr.readMPINT(); BigInteger q = tr.readMPINT(); BigInteger g = tr.readMPINT(); BigInteger y = tr.readMPINT(); if (tr.remain() != 0) throw new IOException("Padding in DSA public key!"); return new DSAPublicKey(p, q, g, y); }
35. DSASHA1Verify#generateSignature()
View licensepublic static DSASignature generateSignature(byte[] message, DSAPrivateKey pk, SecureRandom rnd) { SHA1 md = new SHA1(); md.update(message); byte[] sha_message = new byte[md.getDigestLength()]; md.digest(sha_message); BigInteger m = new BigInteger(1, sha_message); BigInteger k; int qBitLength = pk.getQ().bitLength(); do { k = new BigInteger(qBitLength, rnd); } while (k.compareTo(pk.getQ()) >= 0); BigInteger r = pk.getG().modPow(k, pk.getP()).mod(pk.getQ()); k = k.modInverse(pk.getQ()).multiply(m.add((pk).getX().multiply(r))); BigInteger s = k.mod(pk.getQ()); return new DSASignature(r, s); }
36. DSASHA1Verify#decodeSSHDSAPublicKey()
View licensepublic static DSAPublicKey decodeSSHDSAPublicKey(byte[] key) throws IOException { TypesReader tr = new TypesReader(key); String key_format = tr.readString(); if (key_format.equals("ssh-dss") == false) throw new IllegalArgumentException("This is not a ssh-dss public key!"); BigInteger p = tr.readMPINT(); BigInteger q = tr.readMPINT(); BigInteger g = tr.readMPINT(); BigInteger y = tr.readMPINT(); if (tr.remain() != 0) throw new IOException("Padding in DSA public key!"); return new DSAPublicKey(p, q, g, y); }
37. KEYConverter#buildDSA()
View licensestatic byte[] buildDSA(DSAPublicKey key) { ByteArrayOutputStream out = new ByteArrayOutputStream(); BigInteger q = key.getParams().getQ(); BigInteger p = key.getParams().getP(); BigInteger g = key.getParams().getG(); BigInteger y = key.getY(); int t = (p.toByteArray().length - 64) / 8; out.write(t); writeBigInteger(out, q); writeBigInteger(out, p); writeBigInteger(out, g); writeBigInteger(out, y); return out.toByteArray(); }
38. KEYConverter#parseDSA()
View licensestatic DSAPublicKey parseDSA(DataInputStream in) throws IOException { byte t = in.readByte(); BigInteger q = readBigInteger(in, 20); BigInteger p = readBigInteger(in, 64 + t * 8); BigInteger g = readBigInteger(in, 64 + t * 8); BigInteger y = readBigInteger(in, 64 + t * 8); DSAPublicKey dsa = new DSAPubKey(p, q, g, y); return dsa; }
39. ElGamalParametersGenerator#generateParameters()
View license/** * which generates the p and g values from the given parameters, * returning the ElGamalParameters object. * <p> * Note: can take a while... */ public ElGamalParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random); BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g = DHParametersHelper.selectGenerator(p, q, random); return new ElGamalParameters(p, g); }
40. DHParametersGenerator#generateParameters()
View license/** * which generates the p and g values from the given parameters, * returning the DHParameters object. * <p> * Note: can take a while... */ public DHParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random); BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g = DHParametersHelper.selectGenerator(p, q, random); return new DHParameters(p, g, q, TWO, null); }
41. RSAKeyEncapsulation#encrypt()
View license/** * Generate and encapsulate a random session key. * * @param out the output buffer for the encapsulated key. * @param outOff the offset for the output buffer. * @param keyLen the length of the random session key. * @return the random session key. */ public CipherParameters encrypt(byte[] out, int outOff, int keyLen) throws IllegalArgumentException { if (key.isPrivate()) { throw new IllegalArgumentException("Public key required for encryption"); } BigInteger n = key.getModulus(); BigInteger e = key.getExponent(); // Generate the ephemeral random and encode it BigInteger r = BigIntegers.createRandomInRange(ZERO, n.subtract(ONE), rnd); // Encrypt the random and encode it BigInteger c = r.modPow(e, n); byte[] C = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, c); System.arraycopy(C, 0, out, outOff, C.length); return generateKey(n, r, keyLen); }
42. ElGamalParametersGenerator#generateParameters()
View license/** * which generates the p and g values from the given parameters, * returning the ElGamalParameters object. * <p> * Note: can take a while... */ public ElGamalParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random); BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g = DHParametersHelper.selectGenerator(p, q, random); return new ElGamalParameters(p, g); }
43. DHParametersGenerator#generateParameters()
View license/** * which generates the p and g values from the given parameters, * returning the DHParameters object. * <p> * Note: can take a while... */ public DHParameters generateParameters() { // // find a safe prime p where p = 2*q + 1, where p and q are prime. // BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random); BigInteger p = safePrimes[0]; BigInteger q = safePrimes[1]; BigInteger g = DHParametersHelper.selectGenerator(p, q, random); return new DHParameters(p, g, q, TWO, null); }
44. CramerShoupParametersGenerator#generateParameters()
View licensepublic CramerShoupParameters generateParameters(DHParameters dhParams) { BigInteger p = dhParams.getP(); BigInteger g1 = dhParams.getG(); // now we just need a second generator BigInteger g2 = ParametersHelper.selectGenerator(p, random); while (g1.equals(g2)) { g2 = ParametersHelper.selectGenerator(p, random); } return new CramerShoupParameters(p, g1, g2, new SHA256Digest()); }
45. RangeVisitor#getRanges()
View licenseprivate Iterable<MutableRange> getRanges() { if (threadCount == 1) { return ImmutableList.of(new MutableRange(startRow, endRow, batchSize)); } int length = Math.max(startRow.length, endRow.length); length = Math.max(1, length); byte[] expandedStartRow = Arrays.copyOf(startRow, length); byte[] expandedEndRow = Arrays.copyOf(endRow, length); for (int i = endRow.length; i < length; i++) { expandedEndRow[i] = (byte) 0xff; } BigInteger startNum = new BigInteger(1, expandedStartRow); BigInteger endNum = new BigInteger(1, expandedEndRow); BigInteger step = endNum.subtract(startNum).divide(BigInteger.valueOf(threadCount)); BigInteger curr = startNum.add(step); Collection<MutableRange> ranges = Lists.newArrayListWithCapacity(threadCount); ranges.add(new MutableRange(startRow, toBytes(curr, length), batchSize)); for (int i = 1; i < threadCount - 1; i++) { BigInteger next = curr.add(step); ranges.add(new MutableRange(toBytes(curr, length), toBytes(next, length), batchSize)); curr = next; } ranges.add(new MutableRange(toBytes(curr, length), endRow, batchSize)); return ranges; }
46. BigIntegerModPowTest#testModPowNegExp()
View license/** * modPow: negative exponent */ @Test public void testModPowNegExp() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75, 48, -7 }; byte eBytes[] = { 27, -15, 65, 39 }; byte mBytes[] = { -128, 2, 3, 4, 5 }; int aSign = 1; int eSign = -1; int mSign = 1; byte rBytes[] = { 12, 118, 46, 86, 92 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger exp = new BigInteger(eSign, eBytes); BigInteger modulus = new BigInteger(mSign, mBytes); BigInteger result = aNumber.modPow(exp, modulus); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
47. BigIntegerModPowTest#testModPowPosExp()
View license/** * modPow: positive exponent */ @Test public void testModPowPosExp() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75, 48, -7 }; byte eBytes[] = { 27, -15, 65, 39 }; byte mBytes[] = { -128, 2, 3, 4, 5 }; int aSign = 1; int eSign = 1; int mSign = 1; byte rBytes[] = { 113, 100, -84, -28, -85 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger exp = new BigInteger(eSign, eBytes); BigInteger modulus = new BigInteger(mSign, mBytes); BigInteger result = aNumber.modPow(exp, modulus); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
48. MontgomeryMultiplyTest#montgomeryMultiply()
View license// Montgomery multiplication // Calculate a * b * r^-1 mod n) // // R is a power of the word size // N' = R^-1 mod N // // T := ab // m := (T mod R)N' mod R [so 0 <= m < R] // t := (T + mN)/R // if t >= N then return t - N else return t // BigInteger montgomeryMultiply(BigInteger a, BigInteger b, BigInteger N, int len, BigInteger n_prime) throws Throwable { BigInteger T = a.multiply(b); BigInteger R = BigInteger.ONE.shiftLeft(len * 32); BigInteger mask = R.subtract(BigInteger.ONE); BigInteger m = (T.and(mask)).multiply(n_prime); // i.e. m.mod(R) m = m.and(mask); T = T.add(m.multiply(N)); // i.e. T.divide(R) T = T.shiftRight(len * 32); if (T.compareTo(N) > 0) { T = T.subtract(N); } return T; }
49. DSSPublicKeyEntryDecoder#decodePublicKey()
View license@Override public DSAPublicKey decodePublicKey(String keyType, InputStream keyData) throws IOException, GeneralSecurityException { if (!KeyPairProvider.SSH_DSS.equals(keyType)) { // just in case we were invoked directly throw new InvalidKeySpecException("Unepected key type: " + keyType); } BigInteger p = decodeBigInt(keyData); BigInteger q = decodeBigInt(keyData); BigInteger g = decodeBigInt(keyData); BigInteger y = decodeBigInt(keyData); return generatePublicKey(new DSAPublicKeySpec(y, p, q, g)); }
50. SimHashPlusHammingDistanceTextSimilarity#hash()
View license/** * ??????? * @param word ? * @return ??? */ private BigInteger hash(String word) { if (word == null || word.length() == 0) { return new BigInteger("0"); } char[] charArray = word.toCharArray(); BigInteger x = BigInteger.valueOf(((long) charArray[0]) << 7); BigInteger m = new BigInteger("1000003"); BigInteger mask = new BigInteger("2").pow(hashBitCount).subtract(new BigInteger("1")); long sum = 0; for (char c : charArray) { sum += c; } x = x.multiply(m).xor(BigInteger.valueOf(sum)).and(mask); x = x.xor(new BigInteger(String.valueOf(word.length()))); if (x.equals(new BigInteger("-1"))) { x = new BigInteger("-2"); } return x; }
51. TableScanner#makeRanges()
View licensepublic List<Map.Entry<Long, Long>> makeRanges() { Long[] savedStartTokens = this.readSavepoints(); List<Map.Entry<Long, Long>> ranges = Lists.newArrayList(); BigInteger fullRange = BigInteger.valueOf(maxToken).subtract(BigInteger.valueOf(minToken)).add(BigInteger.ONE); BigInteger rangeLength = fullRange.divide(BigInteger.valueOf(numPartitions)); BigInteger rangeStart = BigInteger.valueOf(minToken); for (int i = 0; i < numPartitions - 1; i++) { BigInteger rangeEnd = rangeStart.add(rangeLength).subtract(BigInteger.ONE); // If we have a savepoint for this partition, use that start token instead of the default if (savedStartTokens != null) { rangeStart = BigInteger.valueOf(savedStartTokens[i]); } AbstractMap.SimpleEntry<Long, Long> range = new AbstractMap.SimpleEntry<Long, Long>(rangeStart.longValue(), rangeEnd.longValue()); ranges.add(range); rangeStart = rangeEnd.add(BigInteger.ONE); } BigInteger rangeEnd = BigInteger.valueOf(maxToken); if (savedStartTokens != null) { rangeStart = BigInteger.valueOf(savedStartTokens[numPartitions - 1]); } AbstractMap.SimpleEntry<Long, Long> range = new AbstractMap.SimpleEntry<Long, Long>(rangeStart.longValue(), rangeEnd.longValue()); ranges.add(range); return ranges; }
52. KettleTwoWayPasswordEncoder#encryptPassword()
View licensepublic static final String encryptPassword(String password) { if (password == null) { return ""; } if (password.length() == 0) { return ""; } BigInteger bi_passwd = new BigInteger(password.getBytes()); BigInteger bi_r0 = new BigInteger(SEED); BigInteger bi_r1 = bi_r0.xor(bi_passwd); return bi_r1.toString(RADIX); }
53. HashProvider#hashCarterWegman()
View license/** * @param value the value to be hashed * @param m integer output range [1,size] * @param k number of hashes to be computed * @return array with <i>hashes</i> integer hash positions in the range <i>[0,size)</i> */ public static int[] hashCarterWegman(byte[] value, int m, int k) { int[] positions = new int[k]; BigInteger prime32 = BigInteger.valueOf(4294967279l); BigInteger prime64 = BigInteger.valueOf(53200200938189l); BigInteger prime128 = new BigInteger("21213943449988109084994671"); Random r = new Random(seed32); //BigInteger.valueOf(hashBytes(value) BigInteger v = new BigInteger(value.length > 0 ? value : new byte[1]); for (int i = 0; i < k; i++) { BigInteger a = BigInteger.valueOf(r.nextLong()); BigInteger b = BigInteger.valueOf(r.nextLong()); positions[i] = a.multiply(v).add(b).mod(prime64).mod(BigInteger.valueOf(m)).intValue(); } return positions; }
54. RSAKeyEncapsulation#decrypt()
View license/** * Decrypt an encapsulated session key. * * @param in the input buffer for the encapsulated key. * @param inOff the offset for the input buffer. * @param inLen the length of the encapsulated key. * @param keyLen the length of the session key. * @return the session key. */ public CipherParameters decrypt(byte[] in, int inOff, int inLen, int keyLen) throws IllegalArgumentException { if (!key.isPrivate()) { throw new IllegalArgumentException("Private key required for decryption"); } BigInteger n = key.getModulus(); BigInteger d = key.getExponent(); // Decode the input byte[] C = new byte[inLen]; System.arraycopy(in, inOff, C, 0, C.length); BigInteger c = new BigInteger(1, C); // Decrypt the ephemeral random and encode it BigInteger r = c.modPow(d, n); return generateKey(n, r, keyLen); }
55. Bug6937964Test#testNewDurationDayTimeBigInteger()
View license@Test public void testNewDurationDayTimeBigInteger() throws DatatypeConfigurationException { DatatypeFactory dtf = DatatypeFactory.newInstance(); BigInteger day = new BigInteger("1"); BigInteger hour = new BigInteger("23"); BigInteger min = new BigInteger("59"); BigInteger sec = new BigInteger("65"); Duration d = dtf.newDurationDayTime(true, day, hour, min, sec); int days = d.getDays(); System.out.println("Days: " + days); Assert.assertTrue(days == 2, "Return value should be normalized"); }
56. MathHelperTest#testCRT()
View license/** * Test of CRT method, of class MathHelper. */ @Test public void testCRT() { BigInteger[] congs = { new BigInteger("3"), new BigInteger("4"), new BigInteger("5") }; BigInteger[] moduli = { new BigInteger("2"), new BigInteger("3"), new BigInteger("2") }; assertEquals(4, MathHelper.CRT(congs, moduli).intValue()); // computes: // x == 2 mod 3 // x == 3 mod 4 // x == 1 mod 5 BigInteger[] congs2 = { new BigInteger("2"), new BigInteger("3"), new BigInteger("1") }; BigInteger[] moduli2 = { new BigInteger("3"), new BigInteger("4"), new BigInteger("5") }; assertEquals(11, MathHelper.CRT(congs2, moduli2).intValue()); }
57. BigIntegerModPowTest#testmodInversePos1()
View license/** * modInverse: positive number */ @Test public void testmodInversePos1() { byte aBytes[] = { 24, 123, 56, -11, -112, -34, -98, 8, 10, 12, 14, 25, 125, -15, 28, -127 }; byte mBytes[] = { 122, 45, 36, 100, 122, 45 }; int aSign = 1; int mSign = 1; byte rBytes[] = { 47, 3, 96, 62, 87, 19 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger modulus = new BigInteger(mSign, mBytes); BigInteger result = aNumber.modInverse(modulus); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
58. BigIntegerModPowTest#testModPowZeroExp()
View license@Test public void testModPowZeroExp() { BigInteger exp = new BigInteger("0"); BigInteger[] base = new BigInteger[] { new BigInteger("-1"), new BigInteger("0"), new BigInteger("1") }; BigInteger[] mod = new BigInteger[] { new BigInteger("2"), new BigInteger("10"), new BigInteger("2147483648") }; for (int i = 0; i < base.length; ++i) { for (int j = 0; j < mod.length; ++j) { assertEquals(base[i] + " modePow(" + exp + ", " + mod[j] + ") should be " + BigInteger.ONE, BigInteger.ONE, base[i].modPow(exp, mod[j])); } } mod = new BigInteger[] { new BigInteger("1") }; for (int i = 0; i < base.length; ++i) { for (int j = 0; j < mod.length; ++j) { assertEquals(base[i] + " modePow(" + exp + ", " + mod[j] + ") should be " + BigInteger.ZERO, BigInteger.ZERO, base[i].modPow(exp, mod[j])); } } }
59. BigIntegerModPowTest#testModPowException()
View license/** * modPow: non-positive modulus */ @Test public void testModPowException() { byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 }; byte eBytes[] = { 1, 2, 3, 4, 5 }; byte mBytes[] = { 1, 2, 3 }; int aSign = 1; int eSign = 1; int mSign = -1; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger exp = new BigInteger(eSign, eBytes); BigInteger modulus = new BigInteger(mSign, mBytes); try { aNumber.modPow(exp, modulus); fail("ArithmeticException has not been caught"); } catch (ArithmeticException e) { assertEquals("Improper exception message", "BigInteger: modulus not positive", e.getMessage()); } try { BigInteger.ZERO.modPow(new BigInteger("-1"), new BigInteger("10")); fail("ArithmeticException has not been caught"); } catch (ArithmeticException e) { } }
60. BigIntegerHashCodeTest#testEqualObjects()
View license/** * Test hash codes for equal objects. */ @Test public void testEqualObjects() { String value1 = "12378246728727834290276457386374882976782849"; String value2 = "12378246728727834290276457386374882976782849"; BigInteger aNumber1 = new BigInteger(value1); BigInteger aNumber2 = new BigInteger(value2); int code1 = aNumber1.hashCode(); int code2 = aNumber2.hashCode(); if (aNumber1.equals(aNumber2)) { assertTrue("hash codes for equal objects are unequal", code1 == code2); } }
61. BigIntegerDivideTest#testCase24()
View license/** * mod when a divisor is negative */ @Test public void testCase24() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 }; byte bBytes[] = { 27, -15, 65, 39, 100 }; int aSign = -1; int bSign = 1; byte rBytes[] = { 15, 5, -9, -17, 73 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.mod(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
62. BigIntegerDivideTest#testCase23()
View license/** * mod when a divisor is positive */ @Test public void testCase23() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 }; byte bBytes[] = { 27, -15, 65, 39, 100 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 12, -21, 73, 56, 27 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.mod(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
63. BigIntegerDivideTest#testRemainderKnuthMultiDigitsByOneDigit()
View license/** * Divide the number of multi digits by the number of one digit */ @Test public void testRemainderKnuthMultiDigitsByOneDigit() { byte aBytes[] = { 113, -83, 123, -5, 18, -34, 67, 39, -29 }; byte bBytes[] = { 2, -3, -4, -50 }; int aSign = 1; int bSign = -1; byte rBytes[] = { 2, -37, -60, 59 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
64. BigIntegerDivideTest#testRemainderKnuthOneDigitByOneDigit()
View license/** * Divide the number of one digit by the number of one digit */ @Test public void testRemainderKnuthOneDigitByOneDigit() { byte aBytes[] = { 113, -83, 123, -5 }; byte bBytes[] = { 2, -3, -4, -50 }; int aSign = 1; int bSign = -1; byte rBytes[] = { 2, -9, -14, 53 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
65. BigIntegerDivideTest#testRemainderKnuth1()
View license/** * Tests the step D6 from the Knuth algorithm */ @Test public void testRemainderKnuth1() { byte aBytes[] = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1 }; byte bBytes[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 1, 2, 3, 4, 5, 6, 7, 7, 18, -89 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
66. BigIntegerDivideTest#testCase20()
View license/** * Remainder of division of two numbers of different signs. * The first is negative. */ @Test public void testCase20() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 }; byte bBytes[] = { 27, -15, 65, 39, 100 }; int aSign = -1; int bSign = 1; byte rBytes[] = { -13, 20, -74, -57, -27 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
67. BigIntegerDivideTest#testCase19()
View license/** * Remainder of division of two numbers of different signs. * The first is positive. */ @Test public void testCase19() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 }; byte bBytes[] = { 27, -15, 65, 39, 100 }; int aSign = 1; int bSign = -1; byte rBytes[] = { 12, -21, 73, 56, 27 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
68. BigIntegerDivideTest#testCase18()
View license/** * Remainder of division of two negative numbers */ @Test public void testCase18() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 }; byte bBytes[] = { 27, -15, 65, 39, 100 }; int aSign = -1; int bSign = -1; byte rBytes[] = { -13, 20, -74, -57, -27 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
69. BigIntegerDivideTest#testCase17()
View license/** * Remainder of division of two positive numbers */ @Test public void testCase17() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 }; byte bBytes[] = { 27, -15, 65, 39, 100 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 12, -21, 73, 56, 27 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
70. BigIntegerDivideTest#testCase16()
View license/** * Remainder of division of equal numbers */ @Test public void testCase16() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 }; byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 0 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.remainder(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 0, result.signum()); }
71. BigIntegerDivideTest#testDivisionKnuthMultiDigitsByOneDigit()
View license/** * Divide the number of multi digits by the number of one digit */ @Test public void testDivisionKnuthMultiDigitsByOneDigit() { byte aBytes[] = { 113, -83, 123, -5, 18, -34, 67, 39, -29 }; byte bBytes[] = { 2, -3, -4, -5 }; int aSign = 1; int bSign = -1; byte rBytes[] = { -38, 2, 7, 30, 109, -43 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
72. BigIntegerDivideTest#testDivisionKnuthOneDigitByOneDigit()
View license/** * Divide the number of one digit by the number of one digit */ @Test public void testDivisionKnuthOneDigitByOneDigit() { byte aBytes[] = { 113, -83, 123, -5 }; byte bBytes[] = { 2, -3, -4, -5 }; int aSign = 1; int bSign = -1; byte rBytes[] = { -37 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
73. BigIntegerDivideTest#testDivisionKnuthFirstDigitsEqual()
View license/** * Verifies the case when the first digits of the dividend * and divisor equal. */ @Test public void testDivisionKnuthFirstDigitsEqual() { byte aBytes[] = { 2, -3, -4, -5, -1, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; byte bBytes[] = { 2, -3, -4, -5, -1, -1, -1, -1 }; int aSign = -1; int bSign = -1; byte rBytes[] = { 0, -1, -1, -1, -1, -2, -88, -60, 41 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
74. BigIntegerDivideTest#testDivisionKnuthIsNormalized()
View license/** * Verifies the case when the divisor is already normalized. */ @Test public void testDivisionKnuthIsNormalized() { byte aBytes[] = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; byte bBytes[] = { -1, -1, -1, -1, -1, -1, -1, -1 }; int aSign = -1; int bSign = -1; byte rBytes[] = { 0, -9, -8, -7, -6, -5, -4, -3 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
75. BigIntegerDivideTest#testDivisionKnuth1()
View license/** * Verifies the case when borrow != 0 in the private divide method. */ @Test public void testDivisionKnuth1() { byte aBytes[] = { -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 }; byte bBytes[] = { -3, -3, -3, -3 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 0, -5, -12, -33, -96, -36, -105, -56, 92, 15, 48, -109 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
76. BigIntegerDivideTest#testCase14()
View license/** * Divide ONE by ONE. */ @Test public void testCase14() { byte rBytes[] = { 1 }; BigInteger aNumber = BigInteger.ONE; BigInteger bNumber = BigInteger.ONE; BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
77. BigIntegerDivideTest#testCase13()
View license/** * Divide a positive number by ONE. */ @Test public void testCase13() { byte aBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; int aSign = 1; byte rBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = BigInteger.ONE; BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
78. BigIntegerDivideTest#testCase12()
View license/** * Divide ZERO by a negative number. */ @Test public void testCase12() { byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; int bSign = -1; byte rBytes[] = { 0 }; BigInteger aNumber = BigInteger.ZERO; BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 0, result.signum()); }
79. BigIntegerDivideTest#testCase11()
View license/** * Divide zero by a negative number. */ @Test public void testCase11() { byte aBytes[] = { 0 }; byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; int aSign = 0; int bSign = -1; byte rBytes[] = { 0 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 0, result.signum()); }
80. BigIntegerDivideTest#testCase10()
View license/** * Divide two negative numbers. */ @Test public void testCase10() { byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9 }; byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; int aSign = -1; int bSign = -1; byte rBytes[] = { 23, 115, 11, 78, 35, -11 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
81. BigIntegerDivideTest#testCase9()
View license/** * Divide a negative number by a positive one. */ @Test public void testCase9() { byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9 }; byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; int aSign = -1; int bSign = 1; byte rBytes[] = { -24, -116, -12, -79, -36, 11 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
82. BigIntegerDivideTest#testCase8()
View license/** * Divide a positive number by a negative one. */ @Test public void testCase8() { byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9 }; byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 }; int aSign = 1; int bSign = -1; byte rBytes[] = { -24, -116, -12, -79, -36, 11 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
83. BigIntegerAndTest#testSpecialCase2()
View license/** * Test for a special case */ @Test public void testSpecialCase2() { byte aBytes[] = { -51 }; byte bBytes[] = { -52, -51, -50, -49, -48 }; int aSign = -1; int bSign = 1; byte rBytes[] = { 0, -52, -51, -50, -49, 16 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
84. BigIntegerAndTest#testPosPosFirstLonger()
View license/** * And for two positive numbers; the first is longer */ @Test public void testPosPosFirstLonger() { byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2, 5, 6, 21 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
85. BigIntegerOrTest#testPosPosFirstLonger()
View license/** * Or for two positive numbers; the first is longer */ public void testPosPosFirstLonger() { byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15, -9, 39, 58, -69, 87, 87, -17, -73 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.or(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
86. BigIntegerAndTest#testPosPosFirstShorter()
View license/** * And for two positive numbers; the first is shorter */ @Test public void testPosPosFirstShorter() { byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2, 5, 6, 21 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
87. BigIntegerAndTest#testNegNegSameLength()
View license/** * And for two negative numbers of the same length */ @Test public void testNegNegSameLength() { byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = -1; int bSign = -1; byte rBytes[] = { -1, 1, 2, 3, 3, 0, 65, -96, -48, -124, -60, 12, -40, -31, 97 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
88. BigIntegerAndTest#testNegNegFirstLonger()
View license/** * And for two negative numbers; the first is longer */ @Test public void testNegNegFirstLonger() { byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = -1; int bSign = -1; byte rBytes[] = { -1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 73 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
89. BigIntegerAndTest#testNegNegFirstShorter()
View license/** * And for two negative numbers; the first is shorter */ @Test public void testNegNegFirstShorter() { byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; int aSign = -1; int bSign = -1; byte rBytes[] = { -1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 73 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
90. BigIntegerAndTest#testPosNegSameLength()
View license/** * And for two numbers of different signs and the same length */ @Test public void testPosNegSameLength() { byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = 1; int bSign = -1; byte rBytes[] = { 0, -6, -80, 72, 8, 75, 2, -79, 34, 16, -119 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
91. BigIntegerAndTest#testNegPosSameLength()
View license/** * And for two numbers of different signs and the same length */ @Test public void testNegPosSameLength() { byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = -1; int bSign = 1; byte rBytes[] = { 0, -2, 125, -60, -104, 1, 10, 6, 2, 32, 56, 2, 4, 4, 21 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
92. BigIntegerAndTest#testNegPosFirstLonger()
View license/** * And for a negative and a positive numbers; the first is longer */ @Test public void testNegPosFirstLonger() { byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = -1; int bSign = 1; byte rBytes[] = { 73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 3 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
93. BigIntegerAndTest#testNegPosFirstShorter()
View license/** * And for a negative and a positive numbers; the first is shorter */ @Test public void testNegPosFirstShorter() { byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; int aSign = -1; int bSign = 1; byte rBytes[] = { 0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -95 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
94. BigIntegerAndTest#testPosNegFirstLonger()
View license/** * And for a positive and a negative numbers; the first is longer */ @Test public void testPosNegFirstLonger() { byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; int aSign = 1; int bSign = -1; byte rBytes[] = { 0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -95 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
95. BigIntegerAndTest#testPosNegFirstShorter()
View license/** * And for a positive and a negative numbers; the first is shorter */ @Test public void testPosNegFirstShorter() { byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23 }; byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75 }; int aSign = 1; int bSign = -1; byte rBytes[] = { 73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 3 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
96. BigIntegerAndTest#testSpecialCase1()
View license/** * Test for a special case */ @Test public void testSpecialCase1() { byte aBytes[] = { -1, -1, -1, -1 }; byte bBytes[] = { 5, -4, -3, -2 }; int aSign = -1; int bSign = -1; byte rBytes[] = { -1, 0, 0, 0, 0 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.and(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
97. BigIntegerCompareTest#testMinPosZero()
View license/** * max(BigInteger val). * min of positive and ZERO. */ @Test public void testMinPosZero() { byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 }; int aSign = 1; byte rBytes[] = { 0 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = BigInteger.ZERO; BigInteger result = aNumber.min(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertTrue("incorrect sign", result.signum() == 0); }
98. BigIntegerDivideTest#testCase3()
View license/** * Divide two equal positive numbers */ @Test public void testCase3() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 }; byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 1 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", 1, result.signum()); }
99. BigIntegerDivideTest#testCase4()
View license/** * Divide two equal in absolute value numbers of different signs. */ @Test public void testCase4() { byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 }; byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 }; int aSign = -1; int bSign = 1; byte rBytes[] = { -1 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.divide(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertEquals("incorrect sign", -1, result.signum()); }
100. BigIntegerCompareTest#testMinEqual()
View license/** * min(BigInteger val). * numbers are equal. */ @Test public void testMinEqual() { byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 }; byte bBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 }; int aSign = 1; int bSign = 1; byte rBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 }; BigInteger aNumber = new BigInteger(aSign, aBytes); BigInteger bNumber = new BigInteger(bSign, bBytes); BigInteger result = aNumber.min(bNumber); byte resBytes[] = new byte[rBytes.length]; resBytes = result.toByteArray(); for (int i = 0; i < resBytes.length; i++) { assertTrue(resBytes[i] == rBytes[i]); } assertTrue("incorrect sign", result.signum() == 1); }