Skip to content

Commit 871a527

Browse files
committed
WIP support for low-R grinding
1 parent 1ac1f3e commit 871a527

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

secp-ffm/src/main/java/org/bitcoinj/secp/ffm/Secp256k1Foreign.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public byte[] ecPubKeySerialize(SecpPubKey pubKey, int flags) {
259259
// public SecpPoint.Uncompressed ecPointUncompress(SecpPoint.Compressed compressedPoint) {
260260
// return compressedPoint.uncompress();
261261
// }
262-
262+
263263
/* package */ static MemorySegment pubKeySerializeSegment(MemorySegment pubKeySegment, int flags) {
264264
int byteSize = switch(flags) {
265265
case 2 -> 65; // SECP256K1_EC_UNCOMPRESSED())
@@ -303,16 +303,24 @@ private MemorySegment pubKeyParse(SecpPubKey pubKeyData) {
303303

304304
@Override
305305
public SecpResult<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, SecpPrivKey seckey) {
306+
return ecdsaSign(msg_hash_data, seckey, secp256k1_h.NULL());
307+
}
308+
309+
public SecpResult<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, SecpPrivKey seckey, byte[] ndata) {
310+
// TODO: validate ndata is exactly 32-bytes long
311+
return ecdsaSign(msg_hash_data, seckey, arena.allocateFrom(JAVA_BYTE, ndata));
312+
}
313+
314+
public SecpResult<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, SecpPrivKey seckey, MemorySegment ndataSegment) {
306315
/* Generate an ECDSA signature `noncefp` and `ndata` allows you to pass a
307316
* custom nonce function, passing `NULL` will use the RFC-6979 safe default.
308317
* Signing with a valid context, verified secret key
309318
* and the default nonce function should never fail. */
310319
MemorySegment msg_hash = arena.allocateFrom(JAVA_BYTE, msg_hash_data);
311320
MemorySegment sig = secp256k1_ecdsa_signature.allocate(arena);
312-
MemorySegment nullCallback = secp256k1_h.NULL(); // Double-check this (normally you shouldn't use a NULL pointer for a null callback)
313-
MemorySegment nullPointer = secp256k1_h.NULL();
321+
MemorySegment nonceFpNull = secp256k1_h.NULL(); // Double-check this (normally you shouldn't use a NULL pointer for a null callback)
314322
MemorySegment privKeySeg = arena.allocateFrom(JAVA_BYTE, seckey.getEncoded());
315-
int return_val = secp256k1_h.secp256k1_ecdsa_sign(ctx, sig, msg_hash, privKeySeg, nullCallback, nullPointer);
323+
int return_val = secp256k1_h.secp256k1_ecdsa_sign(ctx, sig, msg_hash, privKeySeg, nonceFpNull, ndataSegment);
316324
privKeySeg.fill((byte) 0x00);
317325
return SecpResult.checked(return_val, () -> EcdsaSignature.of(sig.toArray(JAVA_BYTE)));
318326
}

0 commit comments

Comments
 (0)