Skip to content

Commit e474411

Browse files
committed
WIP support for low-R grinding
1 parent f240b78 commit e474411

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
@@ -245,7 +245,7 @@ public byte[] ecPubKeySerialize(P256k1PubKey pubKey, int flags) {
245245
// public P256K1Point.Uncompressed ecPointUncompress(P256K1Point.Compressed compressedPoint) {
246246
// return compressedPoint.uncompress();
247247
// }
248-
248+
249249
/* package */ static MemorySegment pubKeySerializeSegment(MemorySegment pubKeySegment, int flags) {
250250
int byteSize = switch(flags) {
251251
case 2 -> 65; // SECP256K1_EC_UNCOMPRESSED())
@@ -289,16 +289,24 @@ private MemorySegment pubKeyParse(P256k1PubKey pubKeyData) {
289289

290290
@Override
291291
public Result<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, P256k1PrivKey seckey) {
292+
return ecdsaSign(msg_hash_data, seckey, secp256k1_h.NULL());
293+
}
294+
295+
public Result<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, P256k1PrivKey seckey, byte[] ndata) {
296+
// TODO: validate ndata is exactly 32-bytes long
297+
return ecdsaSign(msg_hash_data, seckey, arena.allocateFrom(JAVA_BYTE, ndata));
298+
}
299+
300+
private Result<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, P256k1PrivKey seckey, MemorySegment ndataSegment) {
292301
/* Generate an ECDSA signature `noncefp` and `ndata` allows you to pass a
293302
* custom nonce function, passing `NULL` will use the RFC-6979 safe default.
294303
* Signing with a valid context, verified secret key
295304
* and the default nonce function should never fail. */
296305
MemorySegment msg_hash = arena.allocateFrom(JAVA_BYTE, msg_hash_data);
297306
MemorySegment sig = secp256k1_ecdsa_signature.allocate(arena);
298-
MemorySegment nullCallback = secp256k1_h.NULL(); // Double-check this (normally you shouldn't use a NULL pointer for a null callback)
299-
MemorySegment nullPointer = secp256k1_h.NULL();
307+
MemorySegment nonceFpNull = secp256k1_h.NULL(); // Double-check this (normally you shouldn't use a NULL pointer for a null callback)
300308
MemorySegment privKeySeg = arena.allocateFrom(JAVA_BYTE, seckey.getEncoded());
301-
int return_val = secp256k1_h.secp256k1_ecdsa_sign(ctx, sig, msg_hash, privKeySeg, nullCallback, nullPointer);
309+
int return_val = secp256k1_h.secp256k1_ecdsa_sign(ctx, sig, msg_hash, privKeySeg, nonceFpNull, ndataSegment);
302310
privKeySeg.fill((byte) 0x00);
303311
return Result.checked(return_val, () -> EcdsaSignature.of(sig.toArray(JAVA_BYTE)));
304312
}

0 commit comments

Comments
 (0)