JNI library: cleanup, removed unimplemented code

This commit is contained in:
GreenAddress
2016-02-01 13:37:33 +01:00
committed by Jerzy Kozera
parent 3093576aa4
commit 86e2d07e4c
4 changed files with 17 additions and 603 deletions

View File

@@ -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);

View File

@@ -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();