From 86e2d07e4c1f45c1a2e594ff5d5e74b04bc8087d Mon Sep 17 00:00:00 2001 From: GreenAddress Date: Mon, 1 Feb 2016 13:37:33 +0100 Subject: [PATCH] JNI library: cleanup, removed unimplemented code --- src/java/org/bitcoin/NativeSecp256k1.java | 298 +----------------- src/java/org/bitcoin/NativeSecp256k1Test.java | 104 ------ src/java/org_bitcoin_NativeSecp256k1.c | 169 +--------- src/java/org_bitcoin_NativeSecp256k1.h | 49 +-- 4 files changed, 17 insertions(+), 603 deletions(-) diff --git a/src/java/org/bitcoin/NativeSecp256k1.java b/src/java/org/bitcoin/NativeSecp256k1.java index 1109893..f4d85c0 100644 --- a/src/java/org/bitcoin/NativeSecp256k1.java +++ b/src/java/org/bitcoin/NativeSecp256k1.java @@ -52,41 +52,6 @@ public class NativeSecp256k1 { } } - /** - * 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. * @@ -96,7 +61,6 @@ public class NativeSecp256k1 { * 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); @@ -133,7 +97,6 @@ public class NativeSecp256k1 { * * @param seckey ECDSA Secret key, 32 bytes */ - public static boolean secKeyVerify(byte[] seckey) { Preconditions.checkArgument(seckey.length == 32); @@ -159,13 +122,11 @@ public class NativeSecp256k1 { * 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); @@ -209,82 +170,6 @@ public class NativeSecp256k1 { } } - /** - * 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 { @@ -448,47 +333,6 @@ public class NativeSecp256k1 { 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 * @@ -525,135 +369,6 @@ public class NativeSecp256k1 { 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 * @@ -731,19 +446,8 @@ public class NativeSecp256k1 { 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); diff --git a/src/java/org/bitcoin/NativeSecp256k1Test.java b/src/java/org/bitcoin/NativeSecp256k1Test.java index b21fa81..f18ce95 100644 --- a/src/java/org/bitcoin/NativeSecp256k1Test.java +++ b/src/java/org/bitcoin/NativeSecp256k1Test.java @@ -110,53 +110,6 @@ public class NativeSecp256k1Test { assertEquals( sigString, "" , "testSignNeg"); } - /** - * This tests private key export - */ - public static void testPrivKeyExportComp() throws AssertFailException{ - byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase()); - - byte[] resultArr = NativeSecp256k1.privKeyExport( sec , 1); - String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "3081D3020101042067E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530A08185308182020101302C06072A8648CE3D0101022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F300604010004010704210279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141020101A12403220002C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D" , "Case 12"); - - } - - /** - * This tests private key export - */ - public static void testPrivKeyExportUncomp() throws AssertFailException{ - byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase()); - - byte[] resultArr = NativeSecp256k1.privKeyExport( sec , 0); - String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "30820113020101042067E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530A081A53081A2020101302C06072A8648CE3D0101022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F300604010004010704410479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141020101A14403420004C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6" , "Case 13"); - - } - - /** - * This tests private key import - */ - public static void testSecKeyImportPos() throws AssertFailException { - byte[] sec = BaseEncoding.base16().lowerCase().decode("3081D3020101042067E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530A08185308182020101302C06072A8648CE3D0101022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F300604010004010704210279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141020101A12403220002C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D".toLowerCase()); - - byte[] resultArr = NativeSecp256k1.secKeyImport( sec ); - String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - - assertEquals( sigString , "67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530" , "testSecKeyImport"); - } - - /** - * This tests private key export - */ - public static void testSecKeyImportPos2() throws AssertFailException { - byte[] sec = BaseEncoding.base16().lowerCase().decode("30820113020101042067E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530A081A53081A2020101302C06072A8648CE3D0101022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F300604010004010704410479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8022100FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141020101A14403420004C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6".toLowerCase()); - - byte[] resultArr = NativeSecp256k1.secKeyImport( sec ); - String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530" , "testSecKeyImport2"); - } - /** * This tests private key tweak-add */ @@ -214,44 +167,6 @@ public class NativeSecp256k1Test { assertEquals( result, true, "testRandomize"); } - public static void testRecover() throws AssertFailException { - - /* TODO update this with functions from include/secp256k1_recovery.h - //Case 17 - data = BaseEncoding.base16().lowerCase().decode("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A91".toLowerCase()); //sha256hash of "testing" - sig = BaseEncoding.base16().lowerCase().decode("A33C093C80B84CA1AFBC8974EE3C42FC1CBC966CAE66612593CD1E44646BABFF00CB69703B98B0103AE22C7F9CCADD8DD98F9505BE7A66B1AE459576E930C4F6".toLowerCase()); - pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase()); - int recid = 1; - - resultArr = NativeSecp256k1.recoverCompact( data , sig , 0, recid ); - sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "04C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6" , "Case 17"); - - resultArr = NativeSecp256k1.recoverCompact( data , sig , 1, recid ); - sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "02C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D" , "Case 18"); - */ - } - - public static void testRecoverCompact() throws AssertFailException { - - /* TODO update this with functions from include/secp256k1_recovery.h - //Case 17 - data = BaseEncoding.base16().lowerCase().decode("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A91".toLowerCase()); //sha256hash of "testing" - sig = BaseEncoding.base16().lowerCase().decode("A33C093C80B84CA1AFBC8974EE3C42FC1CBC966CAE66612593CD1E44646BABFF00CB69703B98B0103AE22C7F9CCADD8DD98F9505BE7A66B1AE459576E930C4F6".toLowerCase()); - pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase()); - int recid = 1; - - resultArr = NativeSecp256k1.recoverCompact( data , sig , 0, recid ); - sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "04C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6" , "Case 17"); - - resultArr = NativeSecp256k1.recoverCompact( data , sig , 1, recid ); - sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr); - assertEquals( sigString , "02C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D" , "Case 18"); - */ - } - /** * This tests signSchnorr() for a valid secretkey */ @@ -301,30 +216,11 @@ public class NativeSecp256k1Test { testSignPos(); testSignNeg(); - //Test privKeyExport() compressed/uncomp - //testPrivKeyExportComp(); //Now in /contrib - //testPrivKeyExportUncomp(); //Now in /contrib - - //Test secKeyImport()/2 - //testSecKeyImportPos(); //Now in /contrib - //testSecKeyImportPos2(); //Now in /contrib - - //Test recovery //TODO - //testRecoverCompact(); - //testRecover(); - //testCreateRecoverable(); - - //Test ECDH //TODO - //testECDHSecretGen(); - //Test Schnorr (partial support) //TODO testSchnorrSign(); //testSchnorrVerify //testSchnorrRecovery - //Test pubkeyCombine //TODO - //test pubkeyCombine - //Test privKeyTweakAdd() 1 testPrivKeyTweakAdd_1(); diff --git a/src/java/org_bitcoin_NativeSecp256k1.c b/src/java/org_bitcoin_NativeSecp256k1.c index c5a4a3a..8224929 100644 --- a/src/java/org_bitcoin_NativeSecp256k1.c +++ b/src/java/org_bitcoin_NativeSecp256k1.c @@ -2,7 +2,9 @@ #include #include "org_bitcoin_NativeSecp256k1.h" #include "include/secp256k1.h" +#include "include/secp256k1_ecdh.h" #include "include/secp256k1_recovery.h" +#include "include/secp256k1_schnorr.h" SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ctx_1clone @@ -60,16 +62,6 @@ SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1ve ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pubdata, publen); } - /*Debug - printf("\nData: "); - int i; - for( i = 0; i < 32; i++) printf("%x", data[i]); - printf("\nSig: "); - for( i = 0; i < 64; i++) printf("%x", sig->data[i]); - printf("\nPub: "); - for( i = 0; i < 64; i++) printf("%x", pub->data[i]); - */ - (void)classObject; result = secp256k1_ecdsa_verify(ctx, &sig, data, &pubkey); @@ -172,161 +164,6 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e } -/* TODO replace with contrib/ code */ -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1privkey_1export - (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint privLen, jint compressed) -{ - secp256k1_context *ctx = (secp256k1_context*)ctx_l; - - const unsigned char* secKey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); - - int i, ret; - jobjectArray retArray; - jbyteArray privkeyArray, intsByteArray; - unsigned char intsarray[2]; - - unsigned char* privkey = (unsigned char*) malloc(sizeof(unsigned char)*279); - - for(i = 0; i < privLen; i++) privkey[i] = secKey[i]; - - ret = secp256k1_ec_privkey_export(ctx, privkey , (size_t*)&privLen, secKey, compressed); - - intsarray[0] = privLen; - intsarray[1] = ret; - - retArray = (*env)->NewObjectArray(env, 2, - (*env)->FindClass(env, "[B"), - (*env)->NewByteArray(env, 1)); - - privkeyArray = (*env)->NewByteArray(env, privLen); - (*env)->SetByteArrayRegion(env, privkeyArray, 0, privLen, (jbyte*)privkey); - (*env)->SetObjectArrayElement(env, retArray, 0, privkeyArray); - - intsByteArray = (*env)->NewByteArray(env, 2); - (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); - (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); - - (void)classObject; - - return retArray; -} - -/* TODO replace with contrib/ code */ -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1privkey_1import - (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint privLen) -{ - secp256k1_context *ctx = (secp256k1_context*)ctx_l; - - const unsigned char* secKey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); - - jobjectArray retArray; - jbyteArray privkeyArray, intsByteArray; - unsigned char intsarray[2]; - - unsigned char privkey[32]; - - int ret = secp256k1_ec_privkey_import(ctx, privkey, secKey, privLen); - - privLen = 32; - - intsarray[0] = privLen; - intsarray[1] = ret; - - retArray = (*env)->NewObjectArray(env, 2, - (*env)->FindClass(env, "[B"), - (*env)->NewByteArray(env, 1)); - - privkeyArray = (*env)->NewByteArray(env, privLen); - (*env)->SetByteArrayRegion(env, privkeyArray, 0, privLen, (jbyte*)privkey); - (*env)->SetObjectArrayElement(env, retArray, 0, privkeyArray); - - intsByteArray = (*env)->NewByteArray(env, 2); - (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); - (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); - - (void)classObject; - - return retArray; -} - - -/* TODO replace with serialize_compact() code */ -/*SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1sign_1compact - (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) -{ - secp256k1_context *ctx = (secp256k1_context*)ctx_l; - const unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); - const unsigned char* secKey = (unsigned char*) (data + 32); - - jobjectArray retArray; - jbyteArray sigArray, intsByteArray; - unsigned char intsarray[2]; - - unsigned char sig[64]; - int siglen = 64; - int recID; - - int ret = secp256k1_ecdsa_sign_compact(ctx, data, sig, secKey, NULL, NULL, &recID ); - - intsarray[0] = recID; - intsarray[1] = ret; - - retArray = (*env)->NewObjectArray(env, 2, - (*env)->FindClass(env, "[B"), - (*env)->NewByteArray(env, 1)); - - sigArray = (*env)->NewByteArray(env, siglen); - (*env)->SetByteArrayRegion(env, sigArray, 0, siglen, (jbyte*)sig); - (*env)->SetObjectArrayElement(env, retArray, 0, sigArray); - - intsByteArray = (*env)->NewByteArray(env, 2); - (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); - (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); - - (void)classObject; - - return retArray; -}*/ - - -/* TODO replace with recover_compact() code */ -/* -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1recover_1compact - (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint compressed, jint recid) -{ - secp256k1_context *ctx = (secp256k1_context*)ctx_l; - const unsigned char* msg = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject); - const unsigned char* sig = (unsigned char*) (msg + 32); - - jobjectArray retArray; - jbyteArray pubArray, intsByteArray; - unsigned char intsarray[2]; - - unsigned char pubkey[64]; - int pubkeylen; - - int ret = secp256k1_ecdsa_recover_compact(ctx, msg, sig, pubkey, &pubkeylen, compressed, recid ); - - intsarray[0] = pubkeylen; - intsarray[1] = ret; - - retArray = (*env)->NewObjectArray(env, 2, - (*env)->FindClass(env, "[B"), - (*env)->NewByteArray(env, 1)); - - pubArray = (*env)->NewByteArray(env, pubkeylen); - (*env)->SetByteArrayRegion(env, pubArray, 0, pubkeylen, (jbyte*)pubkey); - (*env)->SetObjectArrayElement(env, retArray, 0, pubArray); - - intsByteArray = (*env)->NewByteArray(env, 2); - (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray); - (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray); - - (void)classObject; - - return retArray; -}*/ - SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1add (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) { @@ -569,4 +406,4 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e (void)classObject; return retArray; -} \ No newline at end of file +} diff --git a/src/java/org_bitcoin_NativeSecp256k1.h b/src/java/org_bitcoin_NativeSecp256k1.h index e4f8f3d..4125a1f 100644 --- a/src/java/org_bitcoin_NativeSecp256k1.h +++ b/src/java/org_bitcoin_NativeSecp256k1.h @@ -96,38 +96,6 @@ SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1secke SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1create (JNIEnv *, jclass, jobject, jlong); -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ec_privkey_export - * Signature: (Ljava/nio/ByteBuffer;JII)[[B - */ -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1privkey_1export - (JNIEnv *, jclass, jobject, jlong, jint, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ec_privkey_import - * Signature: (Ljava/nio/ByteBuffer;JI)[[B - */ -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1privkey_1import - (JNIEnv *, jclass, jobject, jlong, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_signature_parse_der - * Signature: (Ljava/nio/ByteBuffer;JI)[[B - */ -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1signature_1parse_1der - (JNIEnv *, jclass, jobject, jlong, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_signature_normalize - * Signature: (Ljava/nio/ByteBuffer;J)[[B - */ -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1signature_1normalize - (JNIEnv *, jclass, jobject, jlong); - /* * Class: org_bitcoin_NativeSecp256k1 * Method: secp256k1_ec_pubkey_parse @@ -138,11 +106,20 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e /* * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_pubkey_combine - * Signature: (Ljava/nio/ByteBuffer;JI)J + * Method: secp256k1_schnorr_sign + * Signature: (Ljava/nio/ByteBuffer;JI)[[B */ -SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1pubkey_1combine - (JNIEnv *, jclass, jobject, jlong, jint); +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1schnorr_1sign + (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l); + +/* + * Class: org_bitcoin_NativeSecp256k1 + * Method: secp256k1_ecdh + * Signature: (Ljava/nio/ByteBuffer;JI)[[B + */ +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdh + (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen); + #ifdef __cplusplus }