java.math.BigInteger.modPow()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中java.math.BigInteger.modPow()方法的一些代码示例,展示了BigInteger.modPow()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.modPow()方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:modPow

BigInteger.modPow介绍

[英]Returns a BigInteger whose value is pow(this, exponent) mod modulus. The modulus must be positive. The result is guaranteed to be in the interval [0, modulus). If the exponent is negative, then pow(this.modInverse(modulus), -exponent) mod modulus is computed. The inverse of this only exists if this is relatively prime to the modulus, otherwise an exception is thrown.
[中]返回一个BigInteger,其值为pow(此,指数)模。模量必须为正。结果保证在区间[0,模]内。如果指数为负,则计算pow(this.modInverse(module),-exponent)mod module。仅当此值与模相对素数时,此值的倒数才存在,否则会引发异常。

代码示例

代码示例来源:origin: robovm/robovm

r = new BigInteger(bytes);
s = new BigInteger(bytes);
u1 = (new BigInteger(1, digest)).multiply(w).mod(q);
u2 = r.multiply(w).mod(q);
v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q);

代码示例来源:origin: aws-amplify/aws-sdk-android

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;
  }
}

代码示例来源:origin: i2p/i2p.i2p

/**
 *  @param sigBytes ASN.1 R,S
 */
@Override
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
  BigInteger elgp = key.getParams().getP();
  BigInteger pm1 = elgp.subtract(BigInteger.ONE);
  BigInteger elgg = key.getParams().getG();
  BigInteger y = ((ElGamalPublicKey) key).getY();
  if (!(y instanceof NativeBigInteger))
    y = new NativeBigInteger(y);
  byte[] data = digest.digest();
  try {
    BigInteger[] rs = SigUtil.aSN1ToBigInteger(sigBytes, 256);
    BigInteger r = rs[0];
    BigInteger s = rs[1];
    if (r.signum() != 1 || s.signum() != 1 ||
      r.compareTo(elgp) != -1 || s.compareTo(pm1) != -1)
      return false;
    NativeBigInteger h = new NativeBigInteger(1, data);
    BigInteger modvalr = r.modPow(s, elgp);
    BigInteger modvaly = y.modPow(r, elgp);
    BigInteger modmulval = modvalr.multiply(modvaly).mod(elgp);
    BigInteger v = elgg.modPow(h, elgp);
    boolean ok = v.compareTo(modmulval) == 0;
    return ok;
  } catch (RuntimeException e) {
    throw new SignatureException("verify", e);
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

/**
 * Generates the SRP verifier.
 * @param salt REQUIRED: The random salt created by the service.
 * @param userIdHash REQIURED: Username hash.
 * @return verifier as a BigInteger.
 */
private static BigInteger calcVerifier(BigInteger salt, byte[] userIdHash) {
  begin();
  update(salt);
  update(userIdHash);
  final byte[] digest = end();
  final BigInteger x = new BigInteger(1, digest);
  return GG.modPow(x, N);
}

代码示例来源:origin: i2p/i2p.i2p

byte[] samplePrime = CryptoConstants.elgp.toByteArray();
BigInteger jg = new BigInteger(sampleGenerator);
NativeBigInteger ng = CryptoConstants.elgg;
BigInteger jp = new BigInteger(samplePrime);
    bi = new BigInteger(16, rand);
  } while (bi.signum() == 0);
  if (mode == 1)
    jg.modPow(bi, jp);
  else if (mode == 2)
    ng.modPowCT(bi, jp);
      jval = jg.modPow(bi, jp);
    else
      jval = bi.modInverse(jp);

代码示例来源:origin: com.madgag.spongycastle/core

private CramerShoupPublicKeyParameters calculatePublicKey(CramerShoupParameters csParams, CramerShoupPrivateKeyParameters sk) {
    BigInteger g1 = csParams.getG1();
    BigInteger g2 = csParams.getG2();
    BigInteger p = csParams.getP();
    
    BigInteger c = g1.modPow(sk.getX1(), p).multiply(g2.modPow(sk.getX2(), p));
    BigInteger d = g1.modPow(sk.getY1(), p).multiply(g2.modPow(sk.getY2(), p));
    BigInteger h = g1.modPow(sk.getZ(), p);
    
    return new CramerShoupPublicKeyParameters(csParams, c, d, h);
  }
}

代码示例来源:origin: stackoverflow.com

BigInteger tenDigits = BigInteger.valueOf(10).pow(10);
BigInteger sum = BigInteger.ZERO;
for (int i= 1; i <= 1000; i++) {
  BigInteger bi = BigInteger.valueOf(i);
  sum = sum.add(bi.modPow(bi, tenDigits));
}
sum = sum.mod(tenDigits);

代码示例来源:origin: stackoverflow.com

BigInteger bi1;
for (String sbigint : input.split(" ")) {
  BigInteger b1 = new BigInteger(sbigint);
  bi1 = b1.modPow(exp, mod);
}

代码示例来源:origin: robovm/robovm

digestBI = new BigInteger(1, msgDigest.digest());
  k = new BigInteger(1, randomBytes);
  if (k.compareTo(q) != -1) {
    continue;
  r = g.modPow(k, p).mod(q);
  if (r.signum() == 0) {
    continue;
  s = k.modInverse(q).multiply(digestBI.add(x.multiply(r)).mod(q))
      .mod(q);

代码示例来源:origin: i2p/i2p.i2p

@Test
public void pow22523ReturnsCorrectResult() {
  for (int i=0; i<1000; i++) {
    // Arrange:
    final FieldElement f1 = getRandomFieldElement();
    final BigInteger b1 = toBigInteger(f1);
    // Act:
    final FieldElement f2 = f1.pow22523();
    final BigInteger b2 = toBigInteger(f2).mod(getQ());
    // Assert:
    Assert.assertThat(b2, IsEqual.equalTo(b1.modPow(BigInteger.ONE.shiftLeft(252).subtract(new BigInteger("3")), getQ())));
  }
}

代码示例来源:origin: i2p/i2p.i2p

BigInteger d = aalpha.modPow(k, CryptoConstants.elgp);
d = d.multiply(m);
d = d.mod(CryptoConstants.elgp);

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private CramerShoupPublicKeyParameters calculatePublicKey(CramerShoupParameters csParams, CramerShoupPrivateKeyParameters sk) {
    BigInteger g1 = csParams.getG1();
    BigInteger g2 = csParams.getG2();
    BigInteger p = csParams.getP();
    
    BigInteger c = g1.modPow(sk.getX1(), p).multiply(g2.modPow(sk.getX2(), p));
    BigInteger d = g1.modPow(sk.getY1(), p).multiply(g2.modPow(sk.getY2(), p));
    BigInteger h = g1.modPow(sk.getZ(), p);
    
    return new CramerShoupPublicKeyParameters(csParams, c, d, h);
  }
}

代码示例来源:origin: stackoverflow.com

public BigInteger[] step1() {
  x2 = getRandomExponent();
  x3 = getRandomExponent();

  g2 = new BigInteger(gen + "").modPow(x2, mod);
  g3 = new BigInteger(gen + "").modPow(x3, mod);

  BigInteger[] logProof1 = createLogProof("1", g2);
  BigInteger[] logProof2 = createLogProof("2", g3);

  BigInteger c1 = logProof1[0];
  BigInteger d1 = logProof1[1];
  BigInteger c2 = logProof2[0];
  BigInteger d2 = logProof2[1];

  return new BigInteger[] { g2, g3, c1, d1, c2, d2 };
}

代码示例来源:origin: aws-amplify/aws-sdk-android

messageDigest.reset();
messageDigest.update(A.toByteArray());
final BigInteger u = new BigInteger(1, messageDigest.digest(B.toByteArray()));
if (u.equals(BigInteger.ZERO)) {
  throw new CognitoInternalErrorException("Hash of A and B cannot be zero");
final BigInteger x = new BigInteger(1, messageDigest.digest(userIdHash));
final BigInteger s = (B.subtract(KK.multiply(GG.modPow(x, N)))
    .modPow(a.add(u.multiply(x)), N)).mod(N);

代码示例来源:origin: com.amazonaws/aws-android-sdk-cognitoidentityprovider

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;
  }
}

代码示例来源:origin: BiglySoftware/BiglyBT

private BigInteger calculateS()
  {
    return v.modPow(u, N).multiply(A).mod(N).modPow(b, N);
  }
}

代码示例来源:origin: redfish64/TinyTravelTracker

private CramerShoupPublicKeyParameters calculatePublicKey(CramerShoupParameters csParams, CramerShoupPrivateKeyParameters sk) {
    BigInteger g1 = csParams.getG1();
    BigInteger g2 = csParams.getG2();
    BigInteger p = csParams.getP();
    
    BigInteger c = g1.modPow(sk.getX1(), p).multiply(g2.modPow(sk.getX2(), p));
    BigInteger d = g1.modPow(sk.getY1(), p).multiply(g2.modPow(sk.getY2(), p));
    BigInteger h = g1.modPow(sk.getZ(), p);
    
    return new CramerShoupPublicKeyParameters(csParams, c, d, h);
  }
}

代码示例来源:origin: org.jvnet.hudson/ganymed-ssh-2

public void init(SecureRandom rnd)
{
  k = null;
  x = new BigInteger(p.bitLength() - 1, rnd);
  e = g.modPow(x, p);
}

代码示例来源:origin: i2p/i2p.i2p

private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) {
  if (r.equals(s))
    return doublePoint(r, curve);
  else if (r.equals(ECPoint.POINT_INFINITY))
    return s;
  else if (s.equals(ECPoint.POINT_INFINITY))
    return r;
  BigInteger prime = ((ECFieldFp) curve.getField()).getP();
  // use NBI modInverse();
  BigInteger tmp = r.getAffineX().subtract(s.getAffineX());
  tmp = new NativeBigInteger(tmp);
  BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(tmp.modInverse(prime)).mod(prime);
  slope = new NativeBigInteger(slope);
  BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime);
  BigInteger yOut = s.getAffineY().negate().mod(prime);
  yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime);
  ECPoint out = new ECPoint(xOut, yOut);
  return out;
}

代码示例来源:origin: stackoverflow.com

private static boolean isPrime(int n) {
  BigInteger N = new BigInteger(String.valueOf(n));
  BigInteger N_MINUS_1 = N.subtract(BigInteger.ONE); 

  BigInteger sum = BigInteger.ZERO;
  for (int k = 1;  k < n; k++)
    sum = sum.add(new BigInteger(String.valueOf(k)).modPow(N_MINUS_1,N)).mod(N);
  return sum.equals(N_MINUS_1);
}

相关文章