Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
74bb486
Adding new script error flags included in elements
Christewart Feb 28, 2017
1d0f68b
Preliminary implementation of OP_WPV - no testing done yet
Christewart Mar 2, 2017
878003c
Modifying generators to be more flexible
Christewart Mar 3, 2017
02d8f81
Merge pull request #10 from Christewart/add_merkle_block
Christewart Mar 3, 2017
510e65f
Fixing bug in generator for merkle blocks with specific txs
Christewart Mar 3, 2017
a79ad19
Merge pull request #11 from Christewart/add_merkle_block
Christewart Mar 3, 2017
bf2d9bb
Adding more flexibility to block header generators
Christewart Mar 6, 2017
a74c6d3
Merge pull request #12 from Christewart/add_merkle_block
Christewart Mar 6, 2017
995cf92
Modifying merkle block generator to only include txs that are specfied
Christewart Mar 6, 2017
fe64c0e
Fixing bug
Christewart Mar 6, 2017
f8144d5
Merge pull request #13 from Christewart/add_merkle_block
Christewart Mar 6, 2017
194ec0a
Really rough implementation of OP_WPV, passes unit test but I don't t…
Christewart Mar 6, 2017
1762a74
Fixing bug where we were not checking that we were properly relocking…
Christewart Mar 7, 2017
6429007
Fixing bug in calculating pushops for ScriptNumberOperations
Christewart Mar 8, 2017
55e0cba
Merge pull request #14 from Christewart/fix_calc_pushop_script_num_op
Christewart Mar 8, 2017
a31439c
Fixing bug in pushing constants onto the stack whose op codes are Scr…
Christewart Mar 8, 2017
b7e34d5
Merge pull request #15 from Christewart/fix_calc_pushop_script_num_op
Christewart Mar 9, 2017
73fe54b
Implementing generator for withdrawl tx, implementing property to che…
Christewart Mar 9, 2017
690ea50
Implementing WithdrawScriptSignature & WithdrawScriptPubKey represent…
Christewart Mar 10, 2017
02ff430
Adding properties for WithdrawScriptSignature and WithdrawScriptPubKey
Christewart Mar 13, 2017
612c0e1
All unit tests passing for OP_WPV implementation
Christewart Mar 14, 2017
754b524
Merge pull request #67 from Christewart/fix_calc_pushop_script_num_op
Christewart Mar 14, 2017
c921abc
Adding contract type to represent the contract in a WithdrawScriptSig…
Christewart Mar 15, 2017
30e1976
Fixing nits, adding some comments
Christewart Mar 15, 2017
b4dc2d0
Adding way to automatically generate nonce for a Contract
Christewart Mar 15, 2017
a8c7ea1
Merge pull request #16 from Christewart/op_wpv_bug_fixes
Christewart Mar 16, 2017
979435a
Adding new secp256k1 functionality
Christewart Mar 22, 2017
f5b566b
Merge branch 'op_wpv' into secp256k1_api_enhancement
Christewart Mar 23, 2017
022f347
Merge pull request #17 from Christewart/secp256k1_api_enhancement
Christewart Mar 23, 2017
a67f477
Hopefully finally fixed the P2PKHScriptSignature bug
Christewart Mar 23, 2017
26dfd81
Adding isFullyValid method to the ECPublicKey trait
Christewart Mar 23, 2017
fb5333e
Merge pull request #19 from Christewart/p2pkh_script_sig_bug_rd2
Christewart Mar 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions project/Build.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sbt._
import Keys._
object BitcoinSCoreBuild extends Build {
object BitcoinSidechainsBuild extends Build {

val appName = "bitcoin-s-core"
val appName = "bitcoin-s-sidechains"
val appV = "0.0.1"
val scalaV = "2.11.7"
val organization = "org.bitcoins.core"
Expand Down
78 changes: 52 additions & 26 deletions secp256k1/src/java/org/bitcoin/NativeSecp256k1.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
import java.nio.ByteOrder;

import java.math.BigInteger;
import com.google.common.base.Preconditions;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static org.bitcoin.NativeSecp256k1Util.*;

/**
* <p>This class holds native methods to handle ECDSA verification.</p>
*
* <p>You can find an example library that can be used for this at https://github.com/bitcoin/secp256k1</p>
* <p>You can find an example library that can be used for this at https://github.com/bitcoin-core/secp256k1</p>
*
* <p>To build secp256k1 for use with bitcoinj, run
* `./configure --enable-jni --enable-experimental --enable-module-ecdh`
Expand All @@ -52,8 +51,8 @@ public class NativeSecp256k1 {
* @param pub The public key which did the signing
*/
public static boolean verify(byte[] data, byte[] signature, byte[] pub) throws AssertFailException{
Preconditions.checkArgument(data.length == 32 && signature.length <= 520 && pub.length <= 520);

checkInvariant(data.length == 32 && signature.length <= 520 && pub.length <= 520);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < 520) {
byteBuff = ByteBuffer.allocateDirect(520);
Expand All @@ -65,8 +64,6 @@ public static boolean verify(byte[] data, byte[] signature, byte[] pub) throws A
byteBuff.put(signature);
byteBuff.put(pub);

byte[][] retByteArray;

r.lock();
try {
return secp256k1_ecdsa_verify(byteBuff, Secp256k1Context.getContext(), signature.length, pub.length) == 1;
Expand All @@ -85,7 +82,7 @@ public static boolean verify(byte[] data, byte[] signature, byte[] pub) throws A
* @param sig byte array of signature
*/
public static byte[] sign(byte[] data, byte[] sec) throws AssertFailException{
Preconditions.checkArgument(data.length == 32 && sec.length <= 32);
checkInvariant(data.length == 32 && sec.length <= 32);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < 32 + 32) {
Expand Down Expand Up @@ -121,7 +118,7 @@ public static byte[] sign(byte[] data, byte[] sec) throws AssertFailException{
* @param seckey ECDSA Secret key, 32 bytes
*/
public static boolean secKeyVerify(byte[] seckey) {
Preconditions.checkArgument(seckey.length == 32);
checkInvariant(seckey.length == 32);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < seckey.length) {
Expand Down Expand Up @@ -149,9 +146,8 @@ public static boolean secKeyVerify(byte[] seckey) {
* Return values
* @param pubkey ECDSA Public key, 33 or 65 bytes
*/
//TODO add a 'compressed' arg
public static byte[] computePubkey(byte[] seckey) throws AssertFailException{
Preconditions.checkArgument(seckey.length == 32);
public static byte[] computePubkey(byte[] seckey, boolean fCompressed) throws AssertFailException{
checkInvariant(seckey.length == 32);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < seckey.length) {
Expand All @@ -166,7 +162,7 @@ public static byte[] computePubkey(byte[] seckey) throws AssertFailException{

r.lock();
try {
retByteArray = secp256k1_ec_pubkey_create(byteBuff, Secp256k1Context.getContext());
retByteArray = secp256k1_ec_pubkey_create(byteBuff, Secp256k1Context.getContext(), fCompressed);
} finally {
r.unlock();
}
Expand Down Expand Up @@ -207,7 +203,7 @@ public static long cloneContext() {
* @param seckey 32-byte seckey
*/
public static byte[] privKeyTweakMul(byte[] privkey, byte[] tweak) throws AssertFailException{
Preconditions.checkArgument(privkey.length == 32);
checkInvariant(privkey.length == 32);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < privkey.length + tweak.length) {
Expand Down Expand Up @@ -246,7 +242,7 @@ public static byte[] privKeyTweakMul(byte[] privkey, byte[] tweak) throws Assert
* @param seckey 32-byte seckey
*/
public static byte[] privKeyTweakAdd(byte[] privkey, byte[] tweak) throws AssertFailException{
Preconditions.checkArgument(privkey.length == 32);
checkInvariant(privkey.length == 32);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < privkey.length + tweak.length) {
Expand Down Expand Up @@ -278,14 +274,43 @@ public static byte[] privKeyTweakAdd(byte[] privkey, byte[] tweak) throws Assert
return privArr;
}

/**
* libsecp256k1 checks if a pubkey is valid
* [[https://github.com/bitcoin-core/secp256k1/blob/0f9e69db555ea35b90f49fa48925c366261452ec/src/secp256k1.c#L150]]
* @param pubkey
* @return
*/
public static boolean isValidPubKey(byte[] pubkey) {
if (!(pubkey.length == 33 || pubkey.length == 65)) {
return false;
}
final int expectedLen = pubkey.length;
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkey.length) {
byteBuff = ByteBuffer.allocateDirect(pubkey.length);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(pubkey);

r.lock();
try {
return secp256k1_ec_pubkey_parse(byteBuff,Secp256k1Context.getContext(),expectedLen) == 1;
} finally {
r.unlock();
}
}


/**
* libsecp256k1 PubKey Tweak-Add - Tweak pubkey by adding to it
*
* @param tweak some bytes to tweak with
* @param pubkey 32-byte seckey
*/
public static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak) throws AssertFailException{
Preconditions.checkArgument(pubkey.length == 33 || pubkey.length == 65);
public static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak, boolean fCompressed) throws AssertFailException{
checkInvariant((pubkey.length == 33 && fCompressed) || (pubkey.length == 65 && !fCompressed));

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkey.length + tweak.length) {
Expand All @@ -300,7 +325,7 @@ public static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak) throws AssertFa
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_pubkey_tweak_add(byteBuff,Secp256k1Context.getContext(), pubkey.length);
retByteArray = secp256k1_pubkey_tweak_add(byteBuff,Secp256k1Context.getContext(), pubkey.length, fCompressed);
} finally {
r.unlock();
}
Expand All @@ -323,8 +348,8 @@ public static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak) throws AssertFa
* @param tweak some bytes to tweak with
* @param pubkey 32-byte seckey
*/
public static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak) throws AssertFailException{
Preconditions.checkArgument(pubkey.length == 33 || pubkey.length == 65);
public static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak, boolean fCompressed) throws AssertFailException{
checkInvariant((pubkey.length == 33 && fCompressed) || (pubkey.length == 65 && !fCompressed));

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkey.length + tweak.length) {
Expand All @@ -339,7 +364,7 @@ public static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak) throws AssertFa
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_pubkey_tweak_mul(byteBuff,Secp256k1Context.getContext(), pubkey.length);
retByteArray = secp256k1_pubkey_tweak_mul(byteBuff,Secp256k1Context.getContext(), pubkey.length, fCompressed);
} finally {
r.unlock();
}
Expand All @@ -363,7 +388,7 @@ public static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak) throws AssertFa
* @param pubkey byte array of public key used in exponentiaion
*/
public static byte[] createECDHSecret(byte[] seckey, byte[] pubkey) throws AssertFailException{
Preconditions.checkArgument(seckey.length <= 32 && pubkey.length <= 65);
checkInvariant(seckey.length <= 32 && pubkey.length <= 65);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < 32 + pubkey.length) {
Expand Down Expand Up @@ -398,7 +423,7 @@ public static byte[] createECDHSecret(byte[] seckey, byte[] pubkey) throws Asser
* @param seed 32-byte random seed
*/
public static synchronized boolean randomize(byte[] seed) throws AssertFailException{
Preconditions.checkArgument(seed.length == 32 || seed == null);
checkInvariant(seed.length == 32 || seed == null);

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < seed.length) {
Expand All @@ -425,9 +450,9 @@ public static synchronized boolean randomize(byte[] seed) throws AssertFailExcep

private static native byte[][] secp256k1_privkey_tweak_mul(ByteBuffer byteBuff, long context);

private static native byte[][] secp256k1_pubkey_tweak_add(ByteBuffer byteBuff, long context, int pubLen);
private static native byte[][] secp256k1_pubkey_tweak_add(ByteBuffer byteBuff, long context, int pubLen, boolean fCompressed);

private static native byte[][] secp256k1_pubkey_tweak_mul(ByteBuffer byteBuff, long context, int pubLen);
private static native byte[][] secp256k1_pubkey_tweak_mul(ByteBuffer byteBuff, long context, int pubLen, boolean fCompressed);

private static native void secp256k1_destroy_context(long context);

Expand All @@ -437,10 +462,11 @@ public static synchronized boolean randomize(byte[] seed) throws AssertFailExcep

private static native int secp256k1_ec_seckey_verify(ByteBuffer byteBuff, long context);

private static native byte[][] secp256k1_ec_pubkey_create(ByteBuffer byteBuff, long context);
private static native byte[][] secp256k1_ec_pubkey_create(ByteBuffer byteBuff, long context, boolean fCompressed);

private static native byte[][] secp256k1_ec_pubkey_parse(ByteBuffer byteBuff, long context, int inputLen);
private static native int secp256k1_ec_pubkey_parse(ByteBuffer byteBuff, long context, int inputLen);

private static native byte[][] secp256k1_ecdh(ByteBuffer byteBuff, long context, int inputLen);

}

Loading