<p>Your compressed public key is wrong. It should be <code>034c8478229d9a139803ab0889617ecb6421191f3d891d0817d6a6cfd43bcbaa20</code>. How did you construct it?</p>

CC BY-SA 4.0
2018-06-23T14:19:34.127Z
2018-06-23T14:19:34.127Z
Mike D
<p>I am trying to test out TestNet3 and I have a keypair generated using Java crypto library.</p>

<p>The private key is <code>23F847360E1EB348A212F443C633058080A8EB9F05AD132F8B82D51FE672EADD</code> and the public key is <code>044C8478229D9A139803AB0889617ECB6421191F3D891D0817D6A6CFD43BCBAA206FA9CE781E1C48764D8D1D0CE58DC6DE76C1B5F97CBCB5C9F01960DEAD3E192F</code> with the compressed form as <code>024C8478229D9A139803AB0889617ECB6421191F3D891D0817D6A6CFD43BCBAA20</code>.</p>

<p>I derive a TestNet address resulting in <code>msmVKMjAz7UdJSfkpvv3z4RMbt7pxocqZw</code> and decided to use it to sign a transaction hash of <code>C46A8842187BB3642463B020D63462800FFB45791284404A965EF027D9F3B473</code> using <code>SHA256withECDSA</code> mode.</p>

<p>The following R and S is generated as the Signature:</p>

<pre><code>Component R: 038646375A34E4DA028B44362D1745870D019782245EF094AA788D17FD69F1D3
Component S: 00F29287B9A8913A9D751ECB33B32EEEB5614BAA32CF714811E29AE07F0886D4E7
</code></pre>

<p>My signing code is using BouncyCastle as the Java JCE provider as shown below:</p>

<pre><code>System.out.println("BC Sign Begin");
System.out.println("=============");
X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1");
ECDomainParameters CURVE = new ECDomainParameters(CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(), CURVE_PARAMS.getH());
ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
ECPrivateKeyParameters privKey1 = new ECPrivateKeyParameters(((ECPrivateKey) keys[0]).getS(), CURVE);
System.out.println("Curve A Coefficient (a): " + privKey1.getParameters().getCurve().getA().toBigInteger().toString(16));
System.out.println("Curve B Coefficient (b): " + privKey1.getParameters().getCurve().getB().toBigInteger().toString(16));
System.out.println("Curve G Generator X (Gx): " + privKey1.getParameters().getG().getAffineXCoord().toBigInteger().toString(16));
System.out.println("Curve G Generator Y (Gy): " + privKey1.getParameters().getG().getAffineYCoord().toBigInteger().toString(16));
System.out.println("Curve Order (n): " + privKey1.getParameters().getCurve().getOrder().toString(16));
System.out.println("Curve Cofactor (h): " + privKey1.getParameters().getCurve().getCofactor().toString(16));
System.out.println("Private Key In Use: " + ((ECPrivateKey) keys[0]).getS().toString(16));
signer.init(true, privKey1);
BigInteger[] components = signer.generateSignature(input1);
System.out.println("Component R: " + BinUtils.toHexString(components[0].toByteArray()));
System.out.println("Component S: " + BinUtils.toHexString(components[1].toByteArray()));
String compStrs = BinUtils.toHexString(components[0].toByteArray()) + "+" + BinUtils.toHexString(components[1].toByteArray());
System.out.println("Components String: " + compStrs);
</code></pre>

<p>The output:</p>

<pre><code>BC Sign Begin
=============
Curve A Coefficient (a): 0
Curve B Coefficient (b): 7
Curve G Generator X (Gx): 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
Curve G Generator Y (Gy): 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
Curve Order (n): fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
Curve Cofactor (h): 1
Private Key In Use: 23f847360e1eb348a212f443c633058080a8eb9f05ad132f8b82d51fe672eadd
Component R: 038646375A34E4DA028B44362D1745870D019782245EF094AA788D17FD69F1D3
Component S: 00F29287B9A8913A9D751ECB33B32EEEB5614BAA32CF714811E29AE07F0886D4E7
Components String: 038646375A34E4DA028B44362D1745870D019782245EF094AA788D17FD69F1D3+00F29287B9A8913A9D751ECB33B32EEEB5614BAA32CF714811E29AE07F0886D4E7
=============
</code></pre>

<p>When I use BitcoinJ to transmit my transaction to the TestNet, I am getting the following errors:</p>

<pre><code>org.bitcoinj.core.ScriptException: Script resulted in a non-true stack: []
at org.bitcoinj.script.Script.correctlySpends(Script.java:1606)
at java.lang.Thread.run(Thread.java:744)
Hit an exception when trying to approve: 
java.util.concurrent.ExecutionException: org.bitcoinj.core.RejectedTransactionException: Reject: tx e374f230fe59b91ff88cc81768c10cb0d3b651079383da4dcfa4ef54372dcbd7 for reason 'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)' (16)
at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:299)
at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:286)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
Caused by: org.bitcoinj.core.RejectedTransactionException: Reject: tx e374f230fe59b91ff88cc81768c10cb0d3b651079383da4dcfa4ef54372dcbd7 for reason 'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)' (16)
at org.bitcoinj.core.TransactionBroadcast$2.onPreMessageReceived(TransactionBroadcast.java:102)
at org.bitcoinj.core.Peer.processMessage(Peer.java:461)
at org.bitcoinj.core.PeerSocketHandler.receiveBytes(PeerSocketHandler.java:184)
at org.bitcoinj.net.ConnectionHandler.handleKey(ConnectionHandler.java:223)
at org.bitcoinj.net.NioClientManager.handleKey(NioClientManager.java:86)
at org.bitcoinj.net.NioClientManager.run(NioClientManager.java:122)
at com.google.common.util.concurrent.AbstractExecutionThreadService$1$2.run(AbstractExecutionThreadService.java:60)
at com.google.common.util.concurrent.Callables$3.run(Callables.java:95)
at org.bitcoinj.utils.ContextPropagatingThreadFactory$1.run(ContextPropagatingThreadFactory.java:49)
... 1 more
</code></pre>

<transactions><bitcoinj><java><secp256k1>
Problematic Digital Signature with SECP256K1
thotheolh
# Problematic Digital Signature with SECP256K1

(Asked by: username_0 on 2018-06-23)

I am trying to test out TestNet3 and I have a keypair generated using Java crypto library.

The private key is `23F847360E1EB348A212F443C633058080A8EB9F05AD132F8B82D51FE672EADD` and the public key is `044C8478229D9A139803AB0889617ECB6421191F3D891D0817D6A6CFD43BCBAA206FA9CE781E1C48764D8D1D0CE58DC6DE76C1B5F97CBCB5C9F01960DEAD3E192F` with the compressed form as `024C8478229D9A139803AB0889617ECB6421191F3D891D0817D6A6CFD43BCBAA20`.

I derive a TestNet address resulting in `msmVKMjAz7UdJSfkpvv3z4RMbt7pxocqZw` and decided to use it to sign a transaction hash of `C46A8842187BB3642463B020D63462800FFB45791284404A965EF027D9F3B473` using `SHA256withECDSA` mode.

The following R and S is generated as the Signature:

```
Component R: 038646375A34E4DA028B44362D1745870D019782245EF094AA788D17FD69F1D3
Component S: 00F29287B9A8913A9D751ECB33B32EEEB5614BAA32CF714811E29AE07F0886D4E7

```

My signing code is using BouncyCastle as the Java JCE provider as shown below:

```
System.out.println("BC Sign Begin");
System.out.println("=============");
X9ECParameters CURVE_PARAMS = CustomNamedCurves.getByName("secp256k1");
ECDomainParameters CURVE = new ECDomainParameters(CURVE_PARAMS.getCurve(), CURVE_PARAMS.getG(), CURVE_PARAMS.getN(), CURVE_PARAMS.getH());
ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
ECPrivateKeyParameters privKey1 = new ECPrivateKeyParameters(((ECPrivateKey) keys[0]).getS(), CURVE);
System.out.println("Curve A Coefficient (a): " + privKey1.getParameters().getCurve().getA().toBigInteger().toString(16));
System.out.println("Curve B Coefficient (b): " + privKey1.getParameters().getCurve().getB().toBigInteger().toString(16));
System.out.println("Curve G Generator X (Gx): " + privKey1.getParameters().getG().getAffineXCoord().toBigInteger().toString(16));
System.out.println("Curve G Generator Y (Gy): " + privKey1.getParameters().getG().getAffineYCoord().toBigInteger().toString(16));
System.out.println("Curve Order (n): " + privKey1.getParameters().getCurve().getOrder().toString(16));
System.out.println("Curve Cofactor (h): " + privKey1.getParameters().getCurve().getCofactor().toString(16));
System.out.println("Private Key In Use: " + ((ECPrivateKey) keys[0]).getS().toString(16));
signer.init(true, privKey1);
BigInteger[] components = signer.generateSignature(input1);
System.out.println("Component R: " + BinUtils.toHexString(components[0].toByteArray()));
System.out.println("Component S: " + BinUtils.toHexString(components[1].toByteArray()));
String compStrs = BinUtils.toHexString(components[0].toByteArray()) + "+" + BinUtils.toHexString(components[1].toByteArray());
System.out.println("Components String: " + compStrs);

```

The output:

```
BC Sign Begin
=============
Curve A Coefficient (a): 0
Curve B Coefficient (b): 7
Curve G Generator X (Gx): 79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
Curve G Generator Y (Gy): 483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
Curve Order (n): fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
Curve Cofactor (h): 1
Private Key In Use: 23f847360e1eb348a212f443c633058080a8eb9f05ad132f8b82d51fe672eadd
Component R: 038646375A34E4DA028B44362D1745870D019782245EF094AA788D17FD69F1D3
Component S: 00F29287B9A8913A9D751ECB33B32EEEB5614BAA32CF714811E29AE07F0886D4E7
Components String: 038646375A34E4DA028B44362D1745870D019782245EF094AA788D17FD69F1D3+00F29287B9A8913A9D751ECB33B32EEEB5614BAA32CF714811E29AE07F0886D4E7
=============

```

When I use BitcoinJ to transmit my transaction to the TestNet, I am getting the following errors:

```
org.bitcoinj.core.ScriptException: Script resulted in a non-true stack: []
at org.bitcoinj.script.Script.correctlySpends(Script.java:1606)
at java.lang.Thread.run(Thread.java:744)
Hit an exception when trying to approve: 
java.util.concurrent.ExecutionException: org.bitcoinj.core.RejectedTransactionException: Reject: tx e374f230fe59b91ff88cc81768c10cb0d3b651079383da4dcfa4ef54372dcbd7 for reason 'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)' (16)
at com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:299)
at com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:286)
at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
Caused by: org.bitcoinj.core.RejectedTransactionException: Reject: tx e374f230fe59b91ff88cc81768c10cb0d3b651079383da4dcfa4ef54372dcbd7 for reason 'mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)' (16)
at org.bitcoinj.core.TransactionBroadcast$2.onPreMessageReceived(TransactionBroadcast.java:102)
at org.bitcoinj.core.Peer.processMessage(Peer.java:461)
at org.bitcoinj.core.PeerSocketHandler.receiveBytes(PeerSocketHandler.java:184)
at org.bitcoinj.net.ConnectionHandler.handleKey(ConnectionHandler.java:223)
at org.bitcoinj.net.NioClientManager.handleKey(NioClientManager.java:86)
at org.bitcoinj.net.NioClientManager.run(NioClientManager.java:122)
at com.google.common.util.concurrent.AbstractExecutionThreadService$1$2.run(AbstractExecutionThreadService.java:60)
at com.google.common.util.concurrent.Callables$3.run(Callables.java:95)
at org.bitcoinj.utils.ContextPropagatingThreadFactory$1.run(ContextPropagatingThreadFactory.java:49)
... 1 more

```

**Tags:** transactions, bitcoinj, java, secp256k1


---

## 1 answers

### Answer by username_1 on 2018-06-23. Score: 1 | Accepted answer

Your compressed public key is wrong. It should be `034c8478229d9a139803ab0889617ecb6421191f3d891d0817d6a6cfd43bcbaa20`. How did you construct it?


---

