Skip to content

Commit

Permalink
JNI library: cleanup, removed unimplemented code
Browse files Browse the repository at this point in the history
  • Loading branch information
greenaddress authored and jkozera committed Feb 1, 2016
1 parent 3093576 commit 86e2d07
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 598 deletions.
298 changes: 1 addition & 297 deletions src/java/org/bitcoin/NativeSecp256k1.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,6 @@ public static boolean verify(byte[] data, byte[] signature, byte[] pub) throws A
}
}

/**
* recover the given secp256k1 pubkey in native code.
*
* @param data The data which was signed, must be exactly 32 bytes
* @param signature The signature
* @param compressed whether to recover a compressed pubkey
* @param pub The public key which did the signing
*/
//TODO recoverCompact()
public static byte[] recoverCompact(byte[] data, byte[] signature,int compressed, int recID) throws AssertFailException{
Preconditions.checkArgument(data.length == 32 && signature.length == 64 && (compressed == 0 || compressed == 1));

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < 32 + 64) {
byteBuff = ByteBuffer.allocateDirect(32 + 64);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(data);
byteBuff.put(signature);

byte[][] retByteArray = null;//secp256k1_ecdsa_recover_compact(byteBuff, Secp256k1Context, compressed, recID);

byte[] pubArr = retByteArray[0];
int pubLen = new BigInteger(new byte[] { retByteArray[1][0] }).intValue();
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();

assertEquals(pubArr.length, pubLen, "Got bad signature length.");

assertEquals(retVal, 1, "Failed return value check.");

return pubArr;
}

/**
* libsecp256k1 Create an ECDSA signature.
*
Expand All @@ -96,7 +61,6 @@ public static byte[] recoverCompact(byte[] data, byte[] signature,int compressed
* Return values
* @param sig byte array of signature
*/

public static byte[] sign(byte[] data, byte[] sec) throws AssertFailException{
Preconditions.checkArgument(data.length == 32 && sec.length <= 32);

Expand Down Expand Up @@ -133,7 +97,6 @@ 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);

Expand All @@ -159,13 +122,11 @@ public static boolean secKeyVerify(byte[] seckey) {
* libsecp256k1 Compute Pubkey - computes public key from secret key
*
* @param seckey ECDSA Secret key, 32 bytes
* @param compressed 1 to return compressed key, 0 for uncompressed
*
* Return values
* @param pubkey ECDSA Public key, 33 or 65 bytes
*/

//TODO support 'compressed' arg
//TODO add a 'compressed' arg
public static byte[] computePubkey(byte[] seckey) throws AssertFailException{
Preconditions.checkArgument(seckey.length == 32);

Expand Down Expand Up @@ -209,82 +170,6 @@ public static synchronized void cleanup() {
}
}

/**
* libsecp256k1 Secret Key Import - Import a secret key in DER format.
*
* @param seckey DER Sec key
* @param compressed Compressed format
*/
public static byte[] secKeyImport(byte[] seckey) throws AssertFailException{

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < seckey.length) {
byteBuff = ByteBuffer.allocateDirect(seckey.length);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(seckey);

byte[][] retByteArray;

r.lock();
try {
retByteArray = secp256k1_ec_privkey_import(byteBuff,Secp256k1Context.getContext(), seckey.length);
} finally {
r.unlock();
}

byte[] privArr = retByteArray[0];

int privLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();

assertEquals(privArr.length, privLen, "Got bad pubkey length.");

assertEquals(retVal, 1, "Failed return value check.");

return privArr;
}

/**
* libsecp256k1 Private Key Export - Export a private key in DER format.
*
* @param seckey ECDSA Sec key, 33 or 65 bytes
* @param compressed Compressed format
*/
public static byte[] privKeyExport(byte[] privkey, int compressed) throws AssertFailException{
Preconditions.checkArgument(privkey.length == 32 && (compressed == 0 || compressed == 1));

ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < privkey.length) {
byteBuff = ByteBuffer.allocateDirect(privkey.length);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(privkey);

byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_ec_privkey_export(byteBuff, Secp256k1Context.getContext(), privkey.length, compressed);
} finally {
r.unlock();
}

byte[] privArr = retByteArray[0];

int privLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();

assertEquals(privArr.length, compressed == 1 ? 214 : 279, "Got bad pubkey length.");

assertEquals(retVal, 1, "Failed return value check.");

return privArr;
}

public static long cloneContext() {
r.lock();
try {
Expand Down Expand Up @@ -448,47 +333,6 @@ public static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak) throws AssertFa
return pubArr;
}

/**
* libsecp256k1 create ECDH secret - constant time ECDH calculation
*
* @param seckey byte array of secret key used in exponentiaion
* @param pubkey byte array of public key used in exponentiaion
*/
//TODO schnorrSign, schnoorVerify, schnorrRecover()
public static byte[] schnoorOps(byte[] pubkey, byte[] msg32) throws AssertFailException{
/*
Preconditions.checkArgument(msg32.length == 32);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkeys.length * 65) {
byteBuff = ByteBuffer.allocateDirect(pubkeys.length * 65);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(pubkey);
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_ecdsa_recover(byteBuff,Secp256k1Context.getContext(), pubkey.length);
} finally {
r.unlock();
}
byte[] pubArr = retByteArray[0];
int pubLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();
assertEquals(pubArr.length, pubLen, "Got bad pubkey length." );
assertEquals(retVal,1, "Failed return value check.");
return pubArr;*/
return new byte[0];
}

/**
* libsecp256k1 create ECDH secret - constant time ECDH calculation
*
Expand Down Expand Up @@ -525,135 +369,6 @@ public static byte[] createECDHSecret(byte[] seckey, byte[] pubkey) throws Asser
return resArr;
}

/**
* libsecp256k1 createRecoverableSig - create recoverable signature
*
* @param seckey byte array of secret key used in signing
* @param msg32 32-byte signed message hash
*/
//TODO createRecoverableSig()
public static byte[] createRecoverableSig(byte[] pubkey, byte[] msg32) throws AssertFailException{
/*
Preconditions.checkArgument(msg32.length == 32);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkeys.length * 65) {
byteBuff = ByteBuffer.allocateDirect(pubkeys.length * 65);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(pubkey);
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_ecdsa_recover(byteBuff,Secp256k1Context.getContext(), pubkey.length);
} finally {
r.unlock();
}
byte[] pubArr = retByteArray[0];
int pubLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();
assertEquals(pubArr.length, pubLen, "Got bad pubkey length." );
assertEquals(retVal,1, "Failed return value check.");
return pubArr;
*/
return new byte[0];
}

/**
* libsecp256k1 Recover pubkey - recover pubkey from signature
*
* @param sig byte array of signature
* @param msg32 32-byte signed message hash
*/
//TODO recoverPubkey()
public static byte[] recoverPubkey(byte[] pubkey, byte[] msg32) throws AssertFailException{


return new byte[0];
/*
Preconditions.checkArgument(msg32.length == 32);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkeys.length * 65) {
byteBuff = ByteBuffer.allocateDirect(pubkeys.length * 65);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(pubkey);
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_ecdsa_recover(byteBuff,Secp256k1Context.getContext(), pubkey.length);
} finally {
r.unlock();
}
byte[] pubArr = retByteArray[0];
int pubLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();
assertEquals(pubArr.length, pubLen, "Got bad pubkey length." );
assertEquals(retVal,1, "Failed return value check.");
return pubArr;
*/
}

/**
* libsecp256k1 PubKey combine - add pubkeys together
*
* @param pubkeys byte array of pubkeys
* @param numKeys number of pubkeys to add
*/
//TODO pubkeyCombine
public static byte[] pubKeyCombine(byte[][] pubkeys, int numKeys) throws AssertFailException{
/*
Preconditions.checkArgument(pubkeys.length > 0);
for(int i = 0; i < pubkeys.length; i++) Preconditions.checkArgument(pubkeys[i].length == 65 || pubkeys[i].length == 33);
ByteBuffer byteBuff = nativeECDSABuffer.get();
if (byteBuff == null || byteBuff.capacity() < pubkeys.length * 65) {
byteBuff = ByteBuffer.allocateDirect(pubkeys.length * 65);
byteBuff.order(ByteOrder.nativeOrder());
nativeECDSABuffer.set(byteBuff);
}
byteBuff.rewind();
byteBuff.put(pubkey);
byte[][] retByteArray;
r.lock();
try {
retByteArray = secp256k1_pubkey_combine(byteBuff,Secp256k1Context.getContext(), pubkey.length);
} finally {
r.unlock();
}
byte[] pubArr = retByteArray[0];
int pubLen = (byte) new BigInteger(new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
int retVal = new BigInteger(new byte[] { retByteArray[1][1] }).intValue();
assertEquals(pubArr.length, pubLen, "Got bad pubkey length." );
assertEquals(retVal,1, "Failed return value check.");
return pubArr;
*/
return new byte[0];
}

/**
* libsecp256k1 randomize - updates the context randomization
*
Expand Down Expand Up @@ -731,19 +446,8 @@ public static byte[] schnorrSign(byte[] data, byte[] sec) throws AssertFailExcep

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

private static native byte[][] secp256k1_ec_privkey_export(ByteBuffer byteBuff, long context, int privLen, int compressed);

private static native byte[][] secp256k1_ec_privkey_import(ByteBuffer byteBuff, long context, int privLen);

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

//TODO sigNormalize()
private static native byte[][] secp256k1_ecdsa_signature_normalize(ByteBuffer byteBuff, long context);

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

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

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

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

0 comments on commit 86e2d07

Please sign in to comment.