if (this.isZero() || this.isOne()) // earlier JDK compatibility return checkSqrt(new Fp(q, r, x.modPow(e, q))); BigInteger t2 = modMult(t1, x); BigInteger t3 = modMult(t2, t1); return checkSqrt(new Fp(q, r, t2)); BigInteger y = modMult(t2, t4); return checkSqrt(new Fp(q, r, y)); BigInteger fourX = modDouble(modDouble(X)); || !modReduce(P.multiply(P).subtract(fourX)).modPow(legendreExponent, q).equals(qMinusOne)); BigInteger[] result = lucasSequence(P, X, k); U = result[0]; V = result[1]; if (modMult(V, V).equals(fourX)) return new ECFieldElement.Fp(q, r, modHalfAbs(V));
if (this.isZero() || this.isOne()) // earlier JDK compatibility return checkSqrt(new Fp(q, r, x.modPow(e, q))); BigInteger t2 = modMult(t1, x); BigInteger t3 = modMult(t2, t1); return checkSqrt(new Fp(q, r, t2)); BigInteger y = modMult(t2, t4); return checkSqrt(new Fp(q, r, y)); BigInteger fourX = modDouble(modDouble(X)); || !modReduce(P.multiply(P).subtract(fourX)).modPow(legendreExponent, q).equals(qMinusOne)); BigInteger[] result = lucasSequence(P, X, k); U = result[0]; V = result[1]; if (modMult(V, V).equals(fourX)) return new ECFieldElement.Fp(q, r, modHalfAbs(V));
public SM2() { this.ecc_p = new BigInteger(ecc_param[0], 16); this.ecc_a = new BigInteger(ecc_param[1], 16); this.ecc_b = new BigInteger(ecc_param[2], 16); this.ecc_n = new BigInteger(ecc_param[3], 16); this.ecc_gx = new BigInteger(ecc_param[4], 16); this.ecc_gy = new BigInteger(ecc_param[5], 16); this.ecc_gx_fieldelement = new Fp(this.ecc_p, this.ecc_gx); this.ecc_gy_fieldelement = new Fp(this.ecc_p, this.ecc_gy); this.ecc_curve = new ECCurve.Fp(this.ecc_p, this.ecc_a, this.ecc_b); this.ecc_point_g = new ECPoint.Fp(this.ecc_curve, this.ecc_gx_fieldelement, this.ecc_gy_fieldelement); this.ecc_bc_spec = new ECDomainParameters(this.ecc_curve, this.ecc_point_g, this.ecc_n); ECKeyGenerationParameters ecc_ecgenparam; ecc_ecgenparam = new ECKeyGenerationParameters(this.ecc_bc_spec, new SecureRandom()); this.ecc_key_pair_generator = new ECKeyPairGenerator(); this.ecc_key_pair_generator.init(ecc_ecgenparam); }
private static BigInteger[] solveQuadraticEquation(BigInteger n, BigInteger r, BigInteger s) { BigInteger det = r.multiply(r).subtract(s.shiftLeft(2)).mod(n); BigInteger root1 = new ECFieldElement.Fp(n, det).sqrt().toBigInteger(), root2 = n.subtract(root1); if (root1.testBit(0)) { root2 = root2.add(n); } else { root1 = root1.add(n); } // assert root1.testBit(0); // assert root2.testBit(0); // NOTE: implicit -1 of the low-bits return new BigInteger[]{ root1.shiftRight(1), root2.shiftRight(1) }; }
public SM2() { this.ecc_p = new BigInteger(ECC_PARAM[0], 16); this.ecc_a = new BigInteger(ECC_PARAM[1], 16); this.ecc_b = new BigInteger(ECC_PARAM[2], 16); this.ecc_n = new BigInteger(ECC_PARAM[3], 16); this.ecc_gx = new BigInteger(ECC_PARAM[4], 16); this.ecc_gy = new BigInteger(ECC_PARAM[5], 16); this.ecc_gx_fieldelement = new Fp(this.ecc_p, this.ecc_gx); this.ecc_gy_fieldelement = new Fp(this.ecc_p, this.ecc_gy); this.ecc_curve = new ECCurve.Fp(this.ecc_p, this.ecc_a, this.ecc_b); this.ecc_point_g = new ECPoint.Fp(this.ecc_curve, this.ecc_gx_fieldelement, this.ecc_gy_fieldelement); this.ecc_bc_spec = new ECDomainParameters(this.ecc_curve, this.ecc_point_g, this.ecc_n); ECKeyGenerationParameters ecc_ecgenparam; ecc_ecgenparam = new ECKeyGenerationParameters(this.ecc_bc_spec, new SecureRandom()); this.ecc_key_pair_generator = new ECKeyPairGenerator(); this.ecc_key_pair_generator.init(ecc_ecgenparam); }
public ECFieldElement negate() { return x.signum() == 0 ? this : new Fp(q, r, q.subtract(x)); }
public ECFieldElement negate() { return x.signum() == 0 ? this : new Fp(q, r, q.subtract(x)); }
public ECFieldElement subtract(ECFieldElement b) { return new Fp(q, r, modSubtract(x, b.toBigInteger())); }
public ECFieldElement fromBigInteger(BigInteger x) { return new ECFieldElement.Fp(this.q, this.r, x); }
protected BigInteger modInverse(BigInteger x) { int bits = getFieldSize(); int len = (bits + 31) >> 5; int[] p = Nat.fromBigInteger(bits, q); int[] n = Nat.fromBigInteger(bits, x); int[] z = Nat.create(len); Mod.invert(p, n, z); return Nat.toBigInteger(len, z); }
public Fp(BigInteger q, BigInteger a, BigInteger b, BigInteger order, BigInteger cofactor) { super(q); this.q = q; this.r = ECFieldElement.Fp.calculateResidue(q); this.infinity = new ECPoint.Fp(this, null, null); this.a = fromBigInteger(a); this.b = fromBigInteger(b); this.order = order; this.cofactor = cofactor; this.coord = FP_DEFAULT_COORDS; }
public ECFieldElement invert() { // TODO Modular inversion can be faster for a (Generalized) Mersenne Prime. return new Fp(q, r, modInverse(x)); }
public ECFieldElement addOne() { BigInteger x2 = x.add(ECConstants.ONE); if (x2.compareTo(q) == 0) { x2 = ECConstants.ZERO; } return new Fp(q, r, x2); }
/** * @deprecated Will be removed */ public X9FieldElement(BigInteger p, ASN1OctetString s) { this(new ECFieldElement.Fp(p, new BigInteger(1, s.getOctets()))); }