int nlen = n.bitLength(); BigInteger nm1 = n.subtract(BigInteger.ONE); BigInteger r, s; do { s = new BigInteger(nlen + 100, rnd); r = s.mod(n); } while (s.subtract(r).add(nm1).bitLength() >= nlen + 100); // result is in 'r'
BigInteger val = new BigInteger("1254789363254125"); public boolean odd(BigInteger val) { if(!val.mod(new BigInteger("2")).equals(BigInteger.ZERO)) return true; return false; }
public static void main(String... args) { BigInteger fact = fact(100); System.out.println("fact(100) = " + fact); System.out.println("fact(100).longValue() = " + fact.longValue()); System.out.println("fact(100).intValue() = " + fact.intValue()); int powerOfTwoCount = 0; BigInteger two = BigInteger.valueOf(2); while (fact.compareTo(BigInteger.ZERO) > 0 && fact.mod(two).equals(BigInteger.ZERO)) { powerOfTwoCount++; fact = fact.divide(two); } System.out.println("fact(100) powers of two = " + powerOfTwoCount); } private static BigInteger fact(long n) { BigInteger result = BigInteger.ONE; for (long i = 2; i <= n; i++) result = result.multiply(BigInteger.valueOf(i)); return result; }
protected byte[] engineSign() throws SignatureException { BigInteger elgp = key.getParams().getP(); BigInteger pm1 = elgp.subtract(BigInteger.ONE); BigInteger elgg = key.getParams().getG(); BigInteger x = ((ElGamalPrivateKey) key).getX(); boolean ok; do { k = new BigInteger(2048, RandomSource.getInstance()); ok = k.compareTo(pm1) == -1; ok = ok && k.compareTo(BigInteger.ONE) == 1; ok = ok && k.gcd(pm1).equals(BigInteger.ONE); } while (!ok); BigInteger r = elgg.modPow(k, elgp); BigInteger kinv = k.modInverse(pm1); BigInteger h = new NativeBigInteger(1, data); BigInteger s = (kinv.multiply(h.subtract(x.multiply(r)))).mod(pm1);
BigInteger base = new BigInteger("" + DICTIONARY.length); int exponent = 1; BigInteger remaining = new BigInteger(value.toString()); while (true) { BigInteger b = remaining.mod(a); // 119 % 16 = 7 | 112 % 256 = 112 BigInteger c = base.pow(exponent - 1); BigInteger d = b.divide(c); remaining = remaining.subtract(b); // 119 - 7 = 112 | 112 - 112 = 0 if (remaining.equals(BigInteger.ZERO)) { break;
byte[] chainCode = Arrays.copyOfRange(i, 32, 64); Arrays.fill(i, (byte) 0); BigInteger ilInt = new BigInteger(1, il); Arrays.fill(il, (byte) 0); ECPoint ki = Sign.publicPointFromPrivate(ilInt).add(getPublicKeyPoint()); byte[] chainCode = Arrays.copyOfRange(i, 32, 64); Arrays.fill(i, (byte) 0); BigInteger ilInt = new BigInteger(1, il); Arrays.fill(il, (byte) 0); BigInteger privateKey = getPrivateKey().add(ilInt).mod(Sign.CURVE.getN());
private static String calculateIbanChecksum(String countryCode, String basicBankAccountNumber) { String basis = basicBankAccountNumber + countryCode + "00"; StringBuilder sb = new StringBuilder(); char[] characters = basis.toLowerCase().toCharArray(); for (char c : characters) { if (Character.isLetter(c)) { sb.append(String.valueOf((c - 'a') + 10)); } else { sb.append(c); } } int mod97 = new BigInteger(sb.toString()).mod(BigInteger.valueOf(97L)).intValue(); return StringUtils.leftPad(String.valueOf(98 - mod97), 2, '0'); }
public AuthenticationHelper(String userPoolName) { do { a = new BigInteger(EPHEMERAL_KEY_LENGTH, SECURE_RANDOM).mod(N); A = GG.modPow(a, N); } while (A.mod(N).equals(BigInteger.ZERO)); if (userPoolName.contains("_")) { poolName = userPoolName.split("_", 2)[1]; } else { poolName = userPoolName; } }
/** * Returns a {@code BigInteger} whose value is {@code * pow(this, exponent) mod modulus}. The modulus must be positive. The * result is guaranteed to be in the interval {@code [0, modulus)}. * If the exponent is negative, then * {@code pow(this.modInverse(modulus), -exponent) mod modulus} is computed. * The inverse of this only exists if {@code this} is relatively prime to the modulus, * otherwise an exception is thrown. * * @throws NullPointerException if {@code modulus == null} or {@code exponent == null}. * @throws ArithmeticException if {@code modulus < 0} or if {@code exponent < 0} and * not relatively prime to {@code modulus}. */ public BigInteger modPow(BigInteger exponent, BigInteger modulus) { if (modulus.signum() <= 0) { throw new ArithmeticException("modulus.signum() <= 0"); } int exponentSignum = exponent.signum(); if (exponentSignum == 0) { // OpenSSL gets this case wrong; http://b/8574367. return ONE.mod(modulus); } BigInteger base = exponentSignum < 0 ? modInverse(modulus) : this; return new BigInteger(BigInt.modExp(base.getBigInt(), exponent.getBigInt(), modulus.getBigInt())); }
@Test public void squareAndDoubleReturnsCorrectResult() { for (int i=0; i<1000; i++) { // Arrange: final FieldElement f1 = getRandomFieldElement(); final BigInteger b1 = toBigInteger(f1); // Act: final FieldElement f2 = f1.squareAndDouble(); final BigInteger b2 = toBigInteger(f2).mod(getQ()); // Assert: Assert.assertThat(b2, IsEqual.equalTo(b1.multiply(b1).multiply(new BigInteger("2")).mod(getQ()))); } }
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final BigInteger value = data.getInstance().getNode().bigIntegerValue(); /* * We use a plain list to store failed divisors: remember that the * digested form was built with divisors in order, we therefore * only need insertion order, and a plain ArrayList guarantees that. */ final List<BigInteger> failed = Lists.newArrayList(); for (final BigInteger divisor: divisors) if (!value.mod(divisor).equals(BigInteger.ZERO)) failed.add(divisor); if (failed.isEmpty()) return; /* * There are missed divisors: report. */ report.error(newMsg(data, bundle, "missingDivisors") .put("divisors", divisors).put("failed", failed)); }
BigInteger bigInt = new BigInteger(String.valueOf(value)); bigInt = bigInt.add(BIG_2_64); while (bigInt.compareTo(BigInteger.ZERO) != 0) { int digit = bigInt.mod(BIG_TEN).intValue();
BigInteger val = new BigInteger("1254789363254125"); public boolean odd(BigInteger val) { return !val.mod(new BigInteger("2")).equals(BigInteger.ZERO)); }
void testAddMod(String v1, String v2, String v3) { DataWord dv1 = DataWord.of(Hex.decode(v1)); DataWord dv2 = DataWord.of(Hex.decode(v2)); DataWord dv3 = DataWord.of(Hex.decode(v3)); BigInteger bv1 = new BigInteger(v1, 16); BigInteger bv2 = new BigInteger(v2, 16); BigInteger bv3 = new BigInteger(v3, 16); DataWord actual = dv1.addmod(dv2, dv3); BigInteger br = bv1.add(bv2).mod(bv3); assertEquals(actual.value(), br); }