/** @tests java.math.BigInteger#remainder(java.math.BigInteger) */
  @TestTargetNew(
      level = TestLevel.PARTIAL,
      notes = "ArithmeticException checked",
      method = "remainder",
      args = {java.math.BigInteger.class})
  public void test_remainderLjava_math_BigInteger() {
    try {
      largePos.remainder(zero);
      fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    }

    try {
      bi1.remainder(zero);
      fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    }

    try {
      bi3.negate().remainder(zero);
      fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    }

    try {
      zero.remainder(zero);
      fail("ArithmeticException expected");
    } catch (ArithmeticException e) {
    }
  }
Esempio n. 2
0
 @Test
 public void testDivide() {
   BigInteger dividend = BigInteger.valueOf(4566746).pow(4);
   BigInteger divisor = dividend.shiftRight(7).subtract(BigInteger.valueOf(245));
   System.out.println(
       dividend
           + " / "
           + divisor
           + " = "
           + dividend.divide(divisor)
           + " rem "
           + dividend.remainder(divisor));
   System.out.println(
       "N "
           + dividend.bitLength()
           + " - D "
           + divisor.bitLength()
           + " = "
           + (dividend.bitLength() - divisor.bitLength()));
   System.out.println("Q => " + dividend.divide(divisor).bitLength());
   System.out.println("R => " + dividend.remainder(divisor).bitLength());
   System.out.println("----");
   int diff = dividend.bitLength() - divisor.bitLength();
   BigInteger n = dividend.shiftRight(divisor.bitLength());
   BigInteger d = divisor.shiftRight(divisor.bitLength() - 1);
   System.out.println(n + " / " + d + " = " + n.divide(d));
 }
 public int Decrypto(BigInteger c) {
   BigInteger nSquare = n.multiply(n);
   BigInteger Ans = c.modPow(lambda, nSquare);
   Ans = Ans.subtract(BigInteger.ONE).divide(n);
   Ans = Ans.remainder(n).multiply(nu).remainder(n);
   return Ans.intValue();
 }
Esempio n. 4
0
  public static void arithmetic(int order) {
    int failCount = 0;

    for (int i = 0; i < size; i++) {
      BigInteger x = fetchNumber(order);
      while (x.compareTo(BigInteger.ZERO) != 1) x = fetchNumber(order);
      BigInteger y = fetchNumber(order / 2);
      while (x.compareTo(y) == -1) y = fetchNumber(order / 2);
      if (y.equals(BigInteger.ZERO)) y = y.add(BigInteger.ONE);

      BigInteger baz = x.divide(y);
      baz = baz.multiply(y);
      baz = baz.add(x.remainder(y));
      baz = baz.subtract(x);
      if (!baz.equals(BigInteger.ZERO)) failCount++;
    }
    report("Arithmetic I for " + order + " bits", failCount);

    failCount = 0;
    for (int i = 0; i < 100; i++) {
      BigInteger x = fetchNumber(order);
      while (x.compareTo(BigInteger.ZERO) != 1) x = fetchNumber(order);
      BigInteger y = fetchNumber(order / 2);
      while (x.compareTo(y) == -1) y = fetchNumber(order / 2);
      if (y.equals(BigInteger.ZERO)) y = y.add(BigInteger.ONE);

      BigInteger baz[] = x.divideAndRemainder(y);
      baz[0] = baz[0].multiply(y);
      baz[0] = baz[0].add(baz[1]);
      baz[0] = baz[0].subtract(x);
      if (!baz[0].equals(BigInteger.ZERO)) failCount++;
    }
    report("Arithmetic II for " + order + " bits", failCount);
  }
Esempio n. 5
0
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    while (input.hasNext()) {

      String S = new String();
      S += input.next();
      while (!S.contains("#")) {
        S += input.next();
      }

      S = S.replace("#", "");
      if (S.compareTo("0") == 0) {
        System.out.println("YES");
        continue;
      }

      BigInteger t = new BigInteger(S, 2);
      BigInteger r = t.remainder(new BigInteger(131071 + ""));

      if (r.compareTo(new BigInteger("0")) == 0) {
        System.out.println("YES");
      } else {
        System.out.println("NO");
      }
    }
  }
Esempio n. 6
0
  @GwtIncompatible // TODO
  @AndroidIncompatible // slow
  public void testDivNonZeroExact() {
    boolean isAndroid = System.getProperties().getProperty("java.runtime.name").contains("Android");
    for (BigInteger p : NONZERO_BIGINTEGER_CANDIDATES) {
      for (BigInteger q : NONZERO_BIGINTEGER_CANDIDATES) {
        if (isAndroid && p.equals(BAD_FOR_ANDROID_P) && q.equals(BAD_FOR_ANDROID_Q)) {
          // https://code.google.com/p/android/issues/detail?id=196555
          continue;
        }
        if (isAndroid && p.equals(BAD_FOR_GINGERBREAD_P) && q.equals(BAD_FOR_GINGERBREAD_Q)) {
          // Works fine under Marshmallow, so I haven't filed a bug.
          continue;
        }

        boolean dividesEvenly = p.remainder(q).equals(ZERO);

        try {
          BigInteger quotient = BigIntegerMath.divide(p, q, UNNECESSARY);
          BigInteger undone = quotient.multiply(q);
          if (!p.equals(undone)) {
            failFormat("expected %s.multiply(%s) = %s; got %s", quotient, q, p, undone);
          }
          assertTrue(dividesEvenly);
        } catch (ArithmeticException e) {
          assertFalse(dividesEvenly);
        }
      }
    }
  }
  private void testDiv(BigInteger i1, BigInteger i2) {
    BigInteger q = i1.divide(i2);
    BigInteger r = i1.remainder(i2);
    BigInteger[] temp = i1.divideAndRemainder(i2);

    assertTrue("divide and divideAndRemainder do not agree", q.equals(temp[0]));
    assertTrue("remainder and divideAndRemainder do not agree", r.equals(temp[1]));
    assertTrue(
        "signum and equals(zero) do not agree on quotient", q.signum() != 0 || q.equals(zero));
    assertTrue(
        "signum and equals(zero) do not agree on remainder", r.signum() != 0 || r.equals(zero));
    assertTrue(
        "wrong sign on quotient", q.signum() == 0 || q.signum() == i1.signum() * i2.signum());
    assertTrue("wrong sign on remainder", r.signum() == 0 || r.signum() == i1.signum());
    assertTrue("remainder out of range", r.abs().compareTo(i2.abs()) < 0);
    assertTrue("quotient too small", q.abs().add(one).multiply(i2.abs()).compareTo(i1.abs()) > 0);
    assertTrue("quotient too large", q.abs().multiply(i2.abs()).compareTo(i1.abs()) <= 0);
    BigInteger p = q.multiply(i2);
    BigInteger a = p.add(r);
    assertTrue("(a/b)*b+(a%b) != a", a.equals(i1));
    try {
      BigInteger mod = i1.mod(i2);
      assertTrue("mod is negative", mod.signum() >= 0);
      assertTrue("mod out of range", mod.abs().compareTo(i2.abs()) < 0);
      assertTrue("positive remainder == mod", r.signum() < 0 || r.equals(mod));
      assertTrue(
          "negative remainder == mod - divisor", r.signum() >= 0 || r.equals(mod.subtract(i2)));
    } catch (ArithmeticException e) {
      assertTrue("mod fails on negative divisor only", i2.signum() <= 0);
    }
  }
 public BigInteger Encrypto(int m, BigInteger n, BigInteger g) {
   int r = new Random().nextInt(10) + 3;
   BigInteger Ans = BigInteger.ZERO;
   BigInteger nSquare = n.multiply(n);
   Ans = g.modPow(BigInteger.valueOf(m), nSquare);
   Ans = Ans.multiply(BigInteger.valueOf(r).modPow(n, nSquare));
   Ans = Ans.remainder(nSquare);
   return Ans;
 }
Esempio n. 9
0
 public HashSet<BigInteger> getFactors(BigInteger n) {
   HashSet<BigInteger> factors = new HashSet<BigInteger>();
   while (true) {
     for (BigInteger i : primes) {
       if (n.remainder(i).equals(ZERO)) {
         n = n.divide(i);
         factors.add(i);
         break;
       }
     }
     if (n.equals(ONE)) return factors;
   }
 }
Esempio n. 10
0
 public KeyPair getKeyPair() throws SshException {
   try {
     PublicKey pub;
     PrivateKey prv;
     String keyAlg = getString();
     if (KeyPairProvider.SSH_RSA.equals(keyAlg)) {
       BigInteger e = getMPInt();
       BigInteger n = getMPInt();
       BigInteger d = getMPInt();
       BigInteger qInv = getMPInt();
       BigInteger q = getMPInt();
       BigInteger p = getMPInt();
       BigInteger dP = d.remainder(p.subtract(BigInteger.valueOf(1)));
       BigInteger dQ = d.remainder(q.subtract(BigInteger.valueOf(1)));
       KeyFactory keyFactory = SecurityUtils.getKeyFactory("RSA");
       pub = keyFactory.generatePublic(new RSAPublicKeySpec(n, e));
       prv = keyFactory.generatePrivate(new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv));
     } else if (KeyPairProvider.SSH_DSS.equals(keyAlg)) {
       BigInteger p = getMPInt();
       BigInteger q = getMPInt();
       BigInteger g = getMPInt();
       BigInteger y = getMPInt();
       BigInteger x = getMPInt();
       KeyFactory keyFactory = SecurityUtils.getKeyFactory("DSA");
       pub = keyFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g));
       prv = keyFactory.generatePrivate(new DSAPrivateKeySpec(x, p, q, g));
     } else {
       throw new IllegalStateException("Unsupported algorithm: " + keyAlg);
     }
     return new KeyPair(pub, prv);
   } catch (InvalidKeySpecException e) {
     throw new SshException(e);
   } catch (NoSuchAlgorithmException e) {
     throw new SshException(e);
   } catch (NoSuchProviderException e) {
     throw new SshException(e);
   }
 }
Esempio n. 11
0
  /**
   * @param d
   * @param p
   * @param q
   */
  public RSASecretBCPGKey(BigInteger d, BigInteger p, BigInteger q) {
    //
    // pgp requires (p < q)
    //
    int cmp = p.compareTo(q);
    if (cmp >= 0) {
      if (cmp == 0) {
        throw new IllegalArgumentException("p and q cannot be equal");
      }

      BigInteger tmp = p;
      p = q;
      q = tmp;
    }

    this.d = new MPInteger(d);
    this.p = new MPInteger(p);
    this.q = new MPInteger(q);
    this.u = new MPInteger(p.modInverse(q));

    expP = d.remainder(p.subtract(BigInteger.valueOf(1)));
    expQ = d.remainder(q.subtract(BigInteger.valueOf(1)));
    crt = q.modInverse(p);
  }
  private static void validateDHPublicKey(BigInteger p, BigInteger g, BigInteger y)
      throws InvalidKeyException {

    // For better interoperability, the interval is limited to [2, p-2].
    BigInteger leftOpen = BigInteger.ONE;
    BigInteger rightOpen = p.subtract(BigInteger.ONE);
    if (y.compareTo(leftOpen) <= 0) {
      throw new InvalidKeyException("Diffie-Hellman public key is too small");
    }
    if (y.compareTo(rightOpen) >= 0) {
      throw new InvalidKeyException("Diffie-Hellman public key is too large");
    }

    // y^q mod p == 1?
    // Unable to perform this check as q is unknown in this circumstance.

    // p is expected to be prime.  However, it is too expensive to check
    // that p is prime.  Instead, in order to mitigate the impact of
    // non-prime values, we check that y is not a factor of p.
    BigInteger r = p.remainder(y);
    if (r.equals(BigInteger.ZERO)) {
      throw new InvalidKeyException("Invalid Diffie-Hellman parameters");
    }
  }
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   BigInteger all = in.nextBigInteger();
   BigInteger remain = all.remainder(new BigInteger("7"));
   System.out.println(remain);
 }
  /**
   * TODO: chunkNumericPK definition.
   *
   * @param table
   * @param columns
   * @param chunkSize
   * @throws ReplicatorException
   * @throws InterruptedException
   */
  private void chunkNumericPK(Table table, String[] columns, long chunkSize)
      throws ReplicatorException, InterruptedException {
    // Retrieve PK range
    MinMax minmax = retrieveMinMaxCountPK(connection, table);

    if (minmax != null) {
      if (logger.isDebugEnabled())
        logger.debug(
            "Min = "
                + minmax.getMin()
                + " -- Max = "
                + minmax.getMax()
                + " -- Count = "
                + minmax.getCount());

      if (minmax.getCount() <= chunkSize)
        // Get the whole table at once
        chunks.put(new NumericChunk(table, columns));
      else {
        // Share the joy among threads,
        // if primary key is evenly distributed
        if (!minmax.isDecimal()) {
          long gap = (Long) minmax.getMax() - (Long) minmax.getMin();
          long blockSize = chunkSize * gap / minmax.getCount();

          long nbBlocks = gap / blockSize;
          if (gap % blockSize > 0) nbBlocks++;

          long start = (Long) minmax.getMin() - 1;
          long end;
          do {
            end = start + blockSize;
            if (end > (Long) minmax.getMax()) end = (Long) minmax.getMax();
            NumericChunk e = new NumericChunk(table, start, end, columns, nbBlocks);
            chunks.put(e);
            start = end;
          } while (start < (Long) minmax.getMax());
        } else {
          BigInteger start =
              ((BigDecimal) minmax.getMin())
                  .setScale(0, RoundingMode.FLOOR)
                  .toBigInteger()
                  .subtract(BigInteger.valueOf(1));

          BigInteger max =
              ((BigDecimal) minmax.getMax()).setScale(0, RoundingMode.CEILING).toBigInteger();

          BigInteger gap = max.subtract(start);

          BigInteger blockSize =
              gap.multiply(BigInteger.valueOf(chunkSize))
                  .divide(BigInteger.valueOf(minmax.getCount()));

          long nbBlocks = gap.divide(blockSize).longValue();

          if (!gap.remainder(blockSize).equals(BigInteger.ZERO)) {
            nbBlocks++;
            blockSize =
                gap.divide(BigInteger.valueOf(nbBlocks))
                    .add(
                        gap.remainder(blockSize).equals(BigInteger.ZERO)
                            ? BigInteger.ZERO
                            : BigInteger.ONE);
          }
          BigInteger end;
          do {
            end = start.add(blockSize);
            if (end.compareTo(
                    (((BigDecimal) minmax.getMax()).setScale(0, RoundingMode.CEILING))
                        .toBigInteger())
                == 1)
              end =
                  (((BigDecimal) minmax.getMax()).setScale(0, RoundingMode.CEILING)).toBigInteger();

            NumericChunk e = new NumericChunk(table, start, end, columns, nbBlocks);
            chunks.put(e);
            start = end;
          } while (start.compareTo(
                  (((BigDecimal) minmax.getMax()).setScale(0, RoundingMode.CEILING)).toBigInteger())
              == -1);
        }
      }
    } else {
      // table is empty or does not have a
      // good candidate as a PK for chunking.
      // Fall back to limit method
      chunks.put(new NumericChunk(table, columns));
    }
  }
Esempio n. 15
0
 public static BigInteger LSRem(BigInteger x, BigInteger y) {
   return x.remainder(y);
 }
Esempio n. 16
0
 public Num remFrom(BigInteger num) {
   return new BigNum(num.remainder(BigInteger.valueOf(v)));
 }
Esempio n. 17
0
 public BigNumber remainder(BigNumber val) {
   return new BigNumber(bigInteger.remainder(val.getInt()));
 }
Esempio n. 18
0
 public static Number remFromLargeInteger_(int receiver, BigInteger argument) {
   return objectFromBigInteger(argument.remainder(BigInteger.valueOf(receiver)));
 }