@Test void testCannotRecoverPublicKeyFromSignature() { SECP256K1.Signature signature = new Signature((byte) 0, SECP256K1.Parameters.CURVE_ORDER.subtract(BigInteger.ONE), BigInteger.valueOf(10)); assertNull(SECP256K1.PublicKey.recoverFromSignature(Bytes.of("Random data".getBytes(UTF_8)), signature)); }
@Nullable private static BigInteger recoverFromSignature(int v, BigInteger r, BigInteger s, Bytes32 messageHash) { assert (v == 0 || v == 1); assert (r.signum() >= 0); assert (s.signum() >= 0); assert (messageHash != null); ECPoint R = decompressKey(r, (v & 1) == 1); if (R == null || !R.multiply(Parameters.CURVE_ORDER).isInfinity()) { return null; } BigInteger e = messageHash.toUnsignedBigInteger(); BigInteger eInv = BigInteger.ZERO.subtract(e).mod(Parameters.CURVE_ORDER); BigInteger rInv = r.modInverse(Parameters.CURVE_ORDER); BigInteger srInv = rInv.multiply(s).mod(Parameters.CURVE_ORDER); BigInteger eInvrInv = rInv.multiply(eInv).mod(Parameters.CURVE_ORDER); ECPoint q = ECAlgorithms.sumOfTwoMultiplies(Parameters.CURVE.getG(), eInvrInv, R, srInv); if (q.isInfinity()) { return null; } byte[] qBytes = q.getEncoded(false); return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); }
SECP256K1 { @Nullable private static BigInteger recoverFromSignature(int v, BigInteger r, BigInteger s, Bytes32 messageHash) { assert (v == 0 || v == 1); assert (r.signum() >= 0); assert (s.signum() >= 0); assert (messageHash != null); ECPoint R = decompressKey(r, (v & 1) == 1); if (R == null || !R.multiply(Parameters.CURVE_ORDER).isInfinity()) { return null; } BigInteger e = messageHash.toUnsignedBigInteger(); BigInteger eInv = BigInteger.ZERO.subtract(e).mod(Parameters.CURVE_ORDER); BigInteger rInv = r.modInverse(Parameters.CURVE_ORDER); BigInteger srInv = rInv.multiply(s).mod(Parameters.CURVE_ORDER); BigInteger eInvrInv = rInv.multiply(eInv).mod(Parameters.CURVE_ORDER); ECPoint q = ECAlgorithms.sumOfTwoMultiplies(Parameters.CURVE.getG(), eInvrInv, R, srInv); if (q.isInfinity()) { return null; } byte[] qBytes = q.getEncoded(false); return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); } }
SECP256K1 { @Nullable private static BigInteger recoverFromSignature(int v, BigInteger r, BigInteger s, Bytes32 messageHash) { assert (v == 0 || v == 1); assert (r.signum() >= 0); assert (s.signum() >= 0); assert (messageHash != null); ECPoint R = decompressKey(r, (v & 1) == 1); if (R == null || !R.multiply(Parameters.CURVE_ORDER).isInfinity()) { return null; } BigInteger e = messageHash.toUnsignedBigInteger(); BigInteger eInv = BigInteger.ZERO.subtract(e).mod(Parameters.CURVE_ORDER); BigInteger rInv = r.modInverse(Parameters.CURVE_ORDER); BigInteger srInv = rInv.multiply(s).mod(Parameters.CURVE_ORDER); BigInteger eInvrInv = rInv.multiply(eInv).mod(Parameters.CURVE_ORDER); ECPoint q = ECAlgorithms.sumOfTwoMultiplies(Parameters.CURVE.getG(), eInvrInv, R, srInv); if (q.isInfinity()) { return null; } byte[] qBytes = q.getEncoded(false); return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); } private  SECP256K1(); }
SECP256K1 { @Nullable private static BigInteger recoverFromSignature(int v, BigInteger r, BigInteger s, Bytes32 messageHash) { assert (v == 0 || v == 1); assert (r.signum() >= 0); assert (s.signum() >= 0); assert (messageHash != null); ECPoint R = decompressKey(r, (v & 1) == 1); if (R == null || !R.multiply(Parameters.CURVE_ORDER).isInfinity()) { return null; } BigInteger e = messageHash.toUnsignedBigInteger(); BigInteger eInv = BigInteger.ZERO.subtract(e).mod(Parameters.CURVE_ORDER); BigInteger rInv = r.modInverse(Parameters.CURVE_ORDER); BigInteger srInv = rInv.multiply(s).mod(Parameters.CURVE_ORDER); BigInteger eInvrInv = rInv.multiply(eInv).mod(Parameters.CURVE_ORDER); ECPoint q = ECAlgorithms.sumOfTwoMultiplies(Parameters.CURVE.getG(), eInvrInv, R, srInv); if (q.isInfinity()) { return null; } byte[] qBytes = q.getEncoded(false); return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); } private  SECP256K1(); static Signature sign(byte[] data, KeyPair keyPair); static Signature sign(Bytes data, KeyPair keyPair); static Signature signHashed(byte[] hash, KeyPair keyPair); static Signature signHashed(Bytes32 hash, KeyPair keyPair); static boolean verify(byte[] data, Signature signature, PublicKey publicKey); static boolean verify(Bytes data, Signature signature, PublicKey publicKey); static boolean verifyHashed(Bytes32 hash, Signature signature, PublicKey publicKey); static boolean verifyHashed(byte[] hash, Signature signature, PublicKey publicKey); static Bytes32 calculateKeyAgreement(SecretKey privKey, PublicKey theirPubKey); }
SECP256K1 { @Nullable private static BigInteger recoverFromSignature(int v, BigInteger r, BigInteger s, Bytes32 messageHash) { assert (v == 0 || v == 1); assert (r.signum() >= 0); assert (s.signum() >= 0); assert (messageHash != null); ECPoint R = decompressKey(r, (v & 1) == 1); if (R == null || !R.multiply(Parameters.CURVE_ORDER).isInfinity()) { return null; } BigInteger e = messageHash.toUnsignedBigInteger(); BigInteger eInv = BigInteger.ZERO.subtract(e).mod(Parameters.CURVE_ORDER); BigInteger rInv = r.modInverse(Parameters.CURVE_ORDER); BigInteger srInv = rInv.multiply(s).mod(Parameters.CURVE_ORDER); BigInteger eInvrInv = rInv.multiply(eInv).mod(Parameters.CURVE_ORDER); ECPoint q = ECAlgorithms.sumOfTwoMultiplies(Parameters.CURVE.getG(), eInvrInv, R, srInv); if (q.isInfinity()) { return null; } byte[] qBytes = q.getEncoded(false); return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length)); } private  SECP256K1(); static Signature sign(byte[] data, KeyPair keyPair); static Signature sign(Bytes data, KeyPair keyPair); static Signature signHashed(byte[] hash, KeyPair keyPair); static Signature signHashed(Bytes32 hash, KeyPair keyPair); static boolean verify(byte[] data, Signature signature, PublicKey publicKey); static boolean verify(Bytes data, Signature signature, PublicKey publicKey); static boolean verifyHashed(Bytes32 hash, Signature signature, PublicKey publicKey); static boolean verifyHashed(byte[] hash, Signature signature, PublicKey publicKey); static Bytes32 calculateKeyAgreement(SecretKey privKey, PublicKey theirPubKey);  }