From f4730f71471be716bdc925c98474421b06aa68aa Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Sun, 25 Apr 2021 13:25:04 +0200 Subject: [PATCH] add message sign test for low r grinding (verified signatures against electrum 4.1.2) --- .../drongo/crypto/ECKeyTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/test/java/com/sparrowwallet/drongo/crypto/ECKeyTest.java diff --git a/src/test/java/com/sparrowwallet/drongo/crypto/ECKeyTest.java b/src/test/java/com/sparrowwallet/drongo/crypto/ECKeyTest.java new file mode 100644 index 0000000..86860ab --- /dev/null +++ b/src/test/java/com/sparrowwallet/drongo/crypto/ECKeyTest.java @@ -0,0 +1,37 @@ +package com.sparrowwallet.drongo.crypto; + +import com.sparrowwallet.drongo.KeyPurpose; +import com.sparrowwallet.drongo.address.Address; +import com.sparrowwallet.drongo.policy.Policy; +import com.sparrowwallet.drongo.policy.PolicyType; +import com.sparrowwallet.drongo.protocol.ScriptType; +import com.sparrowwallet.drongo.wallet.*; +import org.junit.Assert; +import org.junit.Test; + +public class ECKeyTest { + @Test + public void testGrindLowR() throws MnemonicException { + String words = "absent essay fox snake vast pumpkin height crouch silent bulb excuse razor"; + DeterministicSeed seed = new DeterministicSeed(words, "", 0, DeterministicSeed.Type.BIP39); + Wallet wallet = new Wallet(); + wallet.setPolicyType(PolicyType.SINGLE); + wallet.setScriptType(ScriptType.P2PKH); + Keystore keystore = Keystore.fromSeed(seed, wallet.getScriptType().getDefaultDerivation()); + wallet.getKeystores().add(keystore); + wallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2PKH, wallet.getKeystores(), 1)); + + WalletNode firstReceive = wallet.getNode(KeyPurpose.RECEIVE).getChildren().iterator().next(); + Address address = wallet.getAddress(firstReceive); + Assert.assertEquals("14JmU9a7SzieZNEtBnsZo688rt3mGrw6hr", address.toString()); + ECKey privKey = keystore.getKey(firstReceive); + + //2 attempts required for low R + String signature = privKey.signMessage("Test", ScriptType.P2PKH, null); + Assert.assertEquals("IDgMx1ljPhLHlKUOwnO/jBIgK+K8n8mvDUDROzTgU8gOaPDMs+eYXJpNXXINUx5WpeV605p5uO6B3TzBVcvs478=", signature); + + //3 attempts required for low R + String signature2 = privKey.signMessage("Test1", ScriptType.P2PKH, null); + Assert.assertEquals("IEt/v9K95YVFuRtRtWaabPVwWOFv1FSA/e874I8ABgYMbRyVvHhSwLFz0RZuO87ukxDd4TOsRdofQwMEA90LCgI=", signature2); + } +}