mirror of
https://github.com/dogecoin/dogecoin.git
synced 2026-02-18 11:18:39 +00:00
b19c000063 Merge #607: Use size_t shifts when computing a size_t 4d01bc2d9c Merge #606: travis: Remove unused sudo:false e6d01e9347 Use size_t shifts when computing a size_t 7667532bd7 travis: Remove unused sudo:false ee99f12f3d Merge #599: Switch x86_64 asm to use "i" instead of "n" for immediate values. d58bc93f2c Switch x86_64 asm to use "i" instead of "n" for immediate values. 05362ee042 Merge #597: Add $(COMMON_LIB) to exhaustive tests to fix ARM asm build 83483869ac Add $(COMMON_LIB) to exhaustive tests to fix ARM asm build aa15154a48 Merge #568: Fix integer overflow in ecmult_multi_var when n is large 2277af5ff0 Fix integer overflow in ecmult_multi_var when n is large 85d0e1bcce Merge #591: Make bench_internal obey secp256k1_fe_sqrt's contract wrt aliasing. 14196379ec Merge #580: Add trivial ecmult_multi algorithm which does not require a scratch space a697d82da9 Add trivial ecmult_multi to the benchmark tool bade617417 Add trivial ecmult_multi algorithm. It is selected when no scratch space is given and just multiplies and adds the points. 5545e13dea Merge #584: configure: Use CFLAGS_FOR_BUILD when checking native compiler 20c5869df2 Merge #516: improvements to random seed in src/tests.c b76e45d5d6 Make bench_internal obey secp256k1_fe_sqrt's contract wrt aliasing. 870a977644 Merge #562: Make use of TAG_PUBKEY constants in secp256k1_eckey_pubkey_parse be40c4d0b5 Fixup for C90 mixed declarations. c71dd2c08f Merge #509: Fix algorithm selection in bench_ecmult 6492bf88cc Merge #518: Summarize build options after running configure 0e9ada1941 Merge #567: Correct order of libs returned on pkg-config --libs --static libsecp2… e96901a4b9 Merge #587: Make randomization of a non-signing context a noop 58df8d03ad Merge #511: Portability fix for the configure scripts generated 2ebdad772a Merge #552: Make constants static: 1c131affd3 Merge #551: secp256k1_fe_sqrt: Verify that the arguments don't alias. ba698f883b Merge #539: Assorted minor corrections 949e85b009 Merge #550: Optimize secp256k1_fe_normalize_weak calls. a34bcaadf1 Actually pass CFLAGS_FOR_BUILD and LDFLAGS_FOR_BUILD to linker 2d5f4cebdc configure: Use CFLAGS_FOR_BUILD when checking native compiler b408c6a8b2 Merge #579: Use __GNUC_PREREQ for detecting __builtin_expect 6198375218 Make randomization of a non-signing context a noop c663397f46 Use __GNUC_PREREQ for detecting __builtin_expect e34ceb333b Merge #557: Eliminate scratch memory used when generating contexts b3bf5f99a3 ecmult_impl: expand comment to explain how effective affine interacts with everything efa783f8f0 Store z-ratios in the 'x' coord they'll recover ffd3b346fe add `secp256k1_ge_set_all_gej_var` test which deals with many infinite points 84740acd2a ecmult_impl: save one fe_inv_var 47045270fa ecmult_impl: eliminate scratch memory used when generating context 7f7a2ed3a8 ecmult_gen_impl: eliminate scratch memory used when generating context 314a61d724 Merge #553: add static context object which has no capabilities 89a20a8945 Correct order of libs returned on pkg-config --libs --static libsecp256k1 call. 1086fda4c1 Merge #354: [ECDH API change] Support custom hash function d3cb1f95eb Make use of TAG_PUBKEY constants in secp256k1_eckey_pubkey_parse 40fde611bd prevent attempts to modify `secp256k1_context_no_precomp` ed7c08417a add static context object which has no capabilities 496c5b43b8 Make constants static: static const secp256k1_ge secp256k1_ge_const_g; static const int CURVE_B; bf8b86cc07 secp256k1_fe_sqrt: Verify that the arguments don't alias. 9bd89c836b Optimize secp256k1_fe_normalize_weak calls. Move secp256k1_fe_normalize_weak calls out of ECMULT_TABLE_GET_GE and ECMULT_TABLE_GET_GE_STORAGE and into secp256k1_ge_globalz_set_table_gej instead. 52ab96fedb clean dependendies in field_*_impl.h deff5edd42 Correct math typos in field_*.h 4efb3f8dd1 Add check that restrict pointers don't alias with all parameters. 1e6f1f5ad5 Merge #529: fix tests.c in the count == 0 case c8fbc3c397 [ECDH API change] Allow pass arbitrary data to hash function b00be65056 [ECDH API change] Support custom hash function 95e99f196f fix tests.c in the count == 0 case 452d8e4d2a Merge #523: scratch: add stack frame support 6fe50439ae scratch: add stack frame support 9bc2e26502 Merge #522: parameterize ecmult_const over input size 7c1b91ba4b parameterize ecmult_const over input size dbc3ddd5e2 Merge #513: Increase sparsity of pippenger fixed window naf representation 3965027c81 Summarize build options in configure script 0f0517369c Fix algorithm selection in bench_ecmult fb9271dcf0 Merge #510: add a couple missing `const`s to ecmult_pippenger_wnaf cd5f6028e5 Merge #515: Fix typo 09146ae854 Merge #512: secp256k1_ec_privkey_negate - fix documentation ec0a7b3ae3 Don't touch leading zeros in wnaf_fixed. 9e36d1bfe2 Fix bug in wnaf_fixed where the wnaf array is not completely zeroed when given a 0 scalar. 96f68a0afc Don't invert scalar in wnaf_fixed when it is even because a caller might intentionally give a scalar with many leading zeros. 8b3841c91d fix bug in fread() failure check cddef0c0be tests: add warning message when /dev/urandom fails 9b7c47a21e Fix typo 6dbb007869 Increase sparsity of pippenger fixed window naf representation 1646ace4d5 secp256k1_ec_privkey_negate - fix documentation 270f6c80db Portability fix for the configure scripts generated 9b3ff0309d add a couple missing `const`s to ecmult_pippenger_wnaf cd329dbc3e Merge #460: [build] Update ax_jni_include_dir.m4 macro 7f9c1a1565 Merge #498: tests: Avoid calling fclose(...) with an invalid argument f99aa8d4d3 Merge #499: tests: Make sure we get the requested number of bytes from /dev/urandom b549d3d5f7 Merge #472: [build] Set --enable-jni to no by default instead of auto. d333521516 Merge #494: Support OpenSSL versions >= 1.1 for ENABLE_OPENSSL_TESTS 2ef8ea5d21 Merge #495: Add bench_ecmult to .gitignore 82a96e4587 tests: Make sure we get the requested number of bytes from /dev/urandom 5aae5b5bb2 Avoid calling fclose(...) with an invalid argument cb32940df3 Add bench_ecmult to .gitignore 31abd3ab8d Support OpenSSL versions >= 1.1 for ENABLE_OPENSSL_TESTS c95f6f1360 Merge #487: fix tests typo, s/changed/unchanged fb46c83881 Merge #463: Reduce usage of hardcoded size constants 02f5001dfc Merge #490: Disambiguate bench functions and types 1f46d6089e Disambiguate bench functions and types f54c6c5083 Merge #480: Enable benchmark building by default c77fc08597 Merge #486: Add pippenger_wnaf for multi-multiplication d2f9c6b5dc Use more precise pippenger bucket windows 4c950bbeaf Save some additions per window in _pippenger_wnaf a58f543f5a Add flags for choosing algorithm in ecmult_multi benchmark 36b22c9337 Use scratch space dependent batching in ecmult_multi 355a38f113 Add pippenger_wnaf ecmult_multi bc65aa794e Add bench_ecmult dba5471b69 Add ecmult_multi tests 8c1c831bdb Generalize Strauss to support multiple points 548de42ecf add resizeable scratch space API 0e96cdc6b6 fix typo, s/changed/unchanged c7680e570f Reduce usage of hardcoded size constants 6ad5cdb42a Merge #479: Get rid of reserved _t in type names 7a78f60598 Print whether we're building benchmarks 4afec9f1ae Build benchmarks by default d1dc9dfc0a Get rid of reserved _t in type names 57752d28b3 [build] Set --enable-jni to no by default instead of auto. e7daa9b3c2 [build] Tweak JNI macro to warn instead of error for JNI not found. 5b22977922 [build] Update ax_jni_include_dir.m4 macro to deal with recent versions of macOS git-subtree-dir: src/secp256k1 git-subtree-split: b19c000063be11018b4d1a6b0a85871ab9d0bdcf
227 lines
11 KiB
Java
227 lines
11 KiB
Java
package org.bitcoin;
|
|
|
|
import com.google.common.io.BaseEncoding;
|
|
import java.util.Arrays;
|
|
import java.math.BigInteger;
|
|
import javax.xml.bind.DatatypeConverter;
|
|
import static org.bitcoin.NativeSecp256k1Util.*;
|
|
|
|
/**
|
|
* This class holds test cases defined for testing this library.
|
|
*/
|
|
public class NativeSecp256k1Test {
|
|
|
|
//TODO improve comments/add more tests
|
|
/**
|
|
* This tests verify() for a valid signature
|
|
*/
|
|
public static void testVerifyPos() throws AssertFailException{
|
|
boolean result = false;
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90".toLowerCase()); //sha256hash of "testing"
|
|
byte[] sig = BaseEncoding.base16().lowerCase().decode("3044022079BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980220294F14E883B3F525B5367756C2A11EF6CF84B730B36C17CB0C56F0AAB2C98589".toLowerCase());
|
|
byte[] pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase());
|
|
|
|
result = NativeSecp256k1.verify( data, sig, pub);
|
|
assertEquals( result, true , "testVerifyPos");
|
|
}
|
|
|
|
/**
|
|
* This tests verify() for a non-valid signature
|
|
*/
|
|
public static void testVerifyNeg() throws AssertFailException{
|
|
boolean result = false;
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A91".toLowerCase()); //sha256hash of "testing"
|
|
byte[] sig = BaseEncoding.base16().lowerCase().decode("3044022079BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980220294F14E883B3F525B5367756C2A11EF6CF84B730B36C17CB0C56F0AAB2C98589".toLowerCase());
|
|
byte[] pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase());
|
|
|
|
result = NativeSecp256k1.verify( data, sig, pub);
|
|
//System.out.println(" TEST " + new BigInteger(1, resultbytes).toString(16));
|
|
assertEquals( result, false , "testVerifyNeg");
|
|
}
|
|
|
|
/**
|
|
* This tests secret key verify() for a valid secretkey
|
|
*/
|
|
public static void testSecKeyVerifyPos() throws AssertFailException{
|
|
boolean result = false;
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase());
|
|
|
|
result = NativeSecp256k1.secKeyVerify( sec );
|
|
//System.out.println(" TEST " + new BigInteger(1, resultbytes).toString(16));
|
|
assertEquals( result, true , "testSecKeyVerifyPos");
|
|
}
|
|
|
|
/**
|
|
* This tests secret key verify() for an invalid secretkey
|
|
*/
|
|
public static void testSecKeyVerifyNeg() throws AssertFailException{
|
|
boolean result = false;
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".toLowerCase());
|
|
|
|
result = NativeSecp256k1.secKeyVerify( sec );
|
|
//System.out.println(" TEST " + new BigInteger(1, resultbytes).toString(16));
|
|
assertEquals( result, false , "testSecKeyVerifyNeg");
|
|
}
|
|
|
|
/**
|
|
* This tests public key create() for a valid secretkey
|
|
*/
|
|
public static void testPubKeyCreatePos() throws AssertFailException{
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase());
|
|
|
|
byte[] resultArr = NativeSecp256k1.computePubkey( sec);
|
|
String pubkeyString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( pubkeyString , "04C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6" , "testPubKeyCreatePos");
|
|
}
|
|
|
|
/**
|
|
* This tests public key create() for a invalid secretkey
|
|
*/
|
|
public static void testPubKeyCreateNeg() throws AssertFailException{
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".toLowerCase());
|
|
|
|
byte[] resultArr = NativeSecp256k1.computePubkey( sec);
|
|
String pubkeyString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( pubkeyString, "" , "testPubKeyCreateNeg");
|
|
}
|
|
|
|
/**
|
|
* This tests sign() for a valid secretkey
|
|
*/
|
|
public static void testSignPos() throws AssertFailException{
|
|
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90".toLowerCase()); //sha256hash of "testing"
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase());
|
|
|
|
byte[] resultArr = NativeSecp256k1.sign(data, sec);
|
|
String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( sigString, "30440220182A108E1448DC8F1FB467D06A0F3BB8EA0533584CB954EF8DA112F1D60E39A202201C66F36DA211C087F3AF88B50EDF4F9BDAA6CF5FD6817E74DCA34DB12390C6E9" , "testSignPos");
|
|
}
|
|
|
|
/**
|
|
* This tests sign() for a invalid secretkey
|
|
*/
|
|
public static void testSignNeg() throws AssertFailException{
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90".toLowerCase()); //sha256hash of "testing"
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF".toLowerCase());
|
|
|
|
byte[] resultArr = NativeSecp256k1.sign(data, sec);
|
|
String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( sigString, "" , "testSignNeg");
|
|
}
|
|
|
|
/**
|
|
* This tests private key tweak-add
|
|
*/
|
|
public static void testPrivKeyTweakAdd_1() throws AssertFailException {
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase());
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3".toLowerCase()); //sha256hash of "tweak"
|
|
|
|
byte[] resultArr = NativeSecp256k1.privKeyTweakAdd( sec , data );
|
|
String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( sigString , "A168571E189E6F9A7E2D657A4B53AE99B909F7E712D1C23CED28093CD57C88F3" , "testPrivKeyAdd_1");
|
|
}
|
|
|
|
/**
|
|
* This tests private key tweak-mul
|
|
*/
|
|
public static void testPrivKeyTweakMul_1() throws AssertFailException {
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase());
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3".toLowerCase()); //sha256hash of "tweak"
|
|
|
|
byte[] resultArr = NativeSecp256k1.privKeyTweakMul( sec , data );
|
|
String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( sigString , "97F8184235F101550F3C71C927507651BD3F1CDB4A5A33B8986ACF0DEE20FFFC" , "testPrivKeyMul_1");
|
|
}
|
|
|
|
/**
|
|
* This tests private key tweak-add uncompressed
|
|
*/
|
|
public static void testPrivKeyTweakAdd_2() throws AssertFailException {
|
|
byte[] pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase());
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3".toLowerCase()); //sha256hash of "tweak"
|
|
|
|
byte[] resultArr = NativeSecp256k1.pubKeyTweakAdd( pub , data );
|
|
String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( sigString , "0411C6790F4B663CCE607BAAE08C43557EDC1A4D11D88DFCB3D841D0C6A941AF525A268E2A863C148555C48FB5FBA368E88718A46E205FABC3DBA2CCFFAB0796EF" , "testPrivKeyAdd_2");
|
|
}
|
|
|
|
/**
|
|
* This tests private key tweak-mul uncompressed
|
|
*/
|
|
public static void testPrivKeyTweakMul_2() throws AssertFailException {
|
|
byte[] pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase());
|
|
byte[] data = BaseEncoding.base16().lowerCase().decode("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3".toLowerCase()); //sha256hash of "tweak"
|
|
|
|
byte[] resultArr = NativeSecp256k1.pubKeyTweakMul( pub , data );
|
|
String sigString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( sigString , "04E0FE6FE55EBCA626B98A807F6CAF654139E14E5E3698F01A9A658E21DC1D2791EC060D4F412A794D5370F672BC94B722640B5F76914151CFCA6E712CA48CC589" , "testPrivKeyMul_2");
|
|
}
|
|
|
|
/**
|
|
* This tests seed randomization
|
|
*/
|
|
public static void testRandomize() throws AssertFailException {
|
|
byte[] seed = BaseEncoding.base16().lowerCase().decode("A441B15FE9A3CF56661190A0B93B9DEC7D04127288CC87250967CF3B52894D11".toLowerCase()); //sha256hash of "random"
|
|
boolean result = NativeSecp256k1.randomize(seed);
|
|
assertEquals( result, true, "testRandomize");
|
|
}
|
|
|
|
public static void testCreateECDHSecret() throws AssertFailException{
|
|
|
|
byte[] sec = BaseEncoding.base16().lowerCase().decode("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".toLowerCase());
|
|
byte[] pub = BaseEncoding.base16().lowerCase().decode("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40".toLowerCase());
|
|
|
|
byte[] resultArr = NativeSecp256k1.createECDHSecret(sec, pub);
|
|
String ecdhString = javax.xml.bind.DatatypeConverter.printHexBinary(resultArr);
|
|
assertEquals( ecdhString, "2A2A67007A926E6594AF3EB564FC74005B37A9C8AEF2033C4552051B5C87F043" , "testCreateECDHSecret");
|
|
}
|
|
|
|
public static void main(String[] args) throws AssertFailException{
|
|
|
|
|
|
System.out.println("\n libsecp256k1 enabled: " + Secp256k1Context.isEnabled() + "\n");
|
|
|
|
assertEquals( Secp256k1Context.isEnabled(), true, "isEnabled" );
|
|
|
|
//Test verify() success/fail
|
|
testVerifyPos();
|
|
testVerifyNeg();
|
|
|
|
//Test secKeyVerify() success/fail
|
|
testSecKeyVerifyPos();
|
|
testSecKeyVerifyNeg();
|
|
|
|
//Test computePubkey() success/fail
|
|
testPubKeyCreatePos();
|
|
testPubKeyCreateNeg();
|
|
|
|
//Test sign() success/fail
|
|
testSignPos();
|
|
testSignNeg();
|
|
|
|
//Test privKeyTweakAdd() 1
|
|
testPrivKeyTweakAdd_1();
|
|
|
|
//Test privKeyTweakMul() 2
|
|
testPrivKeyTweakMul_1();
|
|
|
|
//Test privKeyTweakAdd() 3
|
|
testPrivKeyTweakAdd_2();
|
|
|
|
//Test privKeyTweakMul() 4
|
|
testPrivKeyTweakMul_2();
|
|
|
|
//Test randomize()
|
|
testRandomize();
|
|
|
|
//Test ECDH
|
|
testCreateECDHSecret();
|
|
|
|
NativeSecp256k1.cleanup();
|
|
|
|
System.out.println(" All tests passed." );
|
|
|
|
}
|
|
}
|