3import static org.bitcoin.NativeSecp256k1Util.*;
12 assert len % 2 == 0 :
"The hex string length should be even !";
13 byte[] byteArray =
new byte[len / 2];
14 for (
int i = 0; i < len; i += 2) {
15 byteArray[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) +
16 Character.digit(s.charAt(i + 1), 16));
22 StringBuilder stringBuilder =
new StringBuilder(byteArray.length * 2);
23 for(
byte b: byteArray) {
24 stringBuilder.append(String.format(
"%02X", b));
26 return stringBuilder.toString();
35 boolean result =
false;
38 byte[] data =
hexStringToByteArray(
"CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90");
39 byte[]
sig =
hexStringToByteArray(
"3044022079BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980220294F14E883B3F525B5367756C2A11EF6CF84B730B36C17CB0C56F0AAB2C98589");
40 byte[] pub =
hexStringToByteArray(
"040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
43 assertEquals(result,
true,
"testVerifyPos");
50 boolean result =
false;
53 byte[] data =
hexStringToByteArray(
"CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A91");
54 byte[]
sig =
hexStringToByteArray(
"3044022079BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980220294F14E883B3F525B5367756C2A11EF6CF84B730B36C17CB0C56F0AAB2C98589");
55 byte[] pub =
hexStringToByteArray(
"040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
58 assertEquals(result,
false,
"testVerifyNeg");
65 boolean result =
false;
67 byte[] sec =
hexStringToByteArray(
"67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
70 assertEquals(result,
true,
"testSecKeyVerifyPos");
77 boolean result =
false;
79 byte[] sec =
hexStringToByteArray(
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
82 assertEquals(result,
false,
"testSecKeyVerifyNeg");
89 byte[] sec =
hexStringToByteArray(
"67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
93 assertEquals(pubkeyString,
"04C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6",
"testPubKeyCreatePos");
100 byte[] sec =
hexStringToByteArray(
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
104 assertEquals(pubkeyString,
"",
"testPubKeyCreateNeg");
112 byte[] data =
hexStringToByteArray(
"CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90");
113 byte[] sec =
hexStringToByteArray(
"67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
117 assertEquals(sigString,
"3045022100F51D069AA46EDB4E2E77773FE364AA2AF6818AF733EA542CFC4D546640A58D8802204F1C442AC9F26F232451A0C3EE99F6875353FC73902C68055C19E31624F687CC",
"testSignPos");
125 byte[] data =
hexStringToByteArray(
"CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90");
126 byte[] sec =
hexStringToByteArray(
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
130 assertEquals(sigString,
"",
"testSignNeg");
138 byte[] data =
hexStringToByteArray(
"3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
139 byte[] sec =
hexStringToByteArray(
"67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
143 assertEquals(sigString,
"A168571E189E6F9A7E2D657A4B53AE99B909F7E712D1C23CED28093CD57C88F3",
"testPrivKeyAdd_1");
151 byte[] data =
hexStringToByteArray(
"3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
152 byte[] sec =
hexStringToByteArray(
"67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
156 assertEquals(sigString,
"97F8184235F101550F3C71C927507651BD3F1CDB4A5A33B8986ACF0DEE20FFFC",
"testPrivKeyMul_1");
164 byte[] data =
hexStringToByteArray(
"3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
165 byte[] pub =
hexStringToByteArray(
"040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
169 assertEquals(sigString,
"0411C6790F4B663CCE607BAAE08C43557EDC1A4D11D88DFCB3D841D0C6A941AF525A268E2A863C148555C48FB5FBA368E88718A46E205FABC3DBA2CCFFAB0796EF",
"testPrivKeyAdd_2");
177 byte[] data =
hexStringToByteArray(
"3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
178 byte[] pub =
hexStringToByteArray(
"040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
182 assertEquals(sigString,
"04E0FE6FE55EBCA626B98A807F6CAF654139E14E5E3698F01A9A658E21DC1D2791EC060D4F412A794D5370F672BC94B722640B5F76914151CFCA6E712CA48CC589",
"testPrivKeyMul_2");
190 byte[] seed =
hexStringToByteArray(
"A441B15FE9A3CF56661190A0B93B9DEC7D04127288CC87250967CF3B52894D11");
193 assertEquals(result,
true,
"testRandomize");
220 "0000000000000000000000000000000000000000000000000000000000000000",
221 "787A848E71043D280C50470E8E1532B2DD5D20EE912A45DBDD2BD1DFBF187EF67031A98831859DC34DFFEEDDA86831842CCD0079E1F92AF177F7F22CC1DCED05",
222 "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
227 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
228 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
229 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
234 "5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C",
235 "00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BE00880371D01766935B92D2AB4CD5C8A2A5837EC57FED7660773A05F0DE142380",
236 "03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B",
241 "4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703",
242 "00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6302A8DC32E64E86A333F20EF56EAC9BA30B7246D6D25E22ADB8C6BE1AEB08D49D",
243 "03DEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34",
248 "0000000000000000000000000000000000000000000000000000000000000000",
249 "52818579ACA59767E3291D91B76B637BEF062083284992F2D95F564CA6CB4E3530B1DA849C8E8304ADC0CFE870660334B3CFC18E825EF1DB34CFAE3DFC5D8187",
250 "031B84C5567B126440995D3ED5AABA0565D71E1834604819FF9C17F5E9D5DD078F",
255 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
256 "570DD4CA83D4E6317B8EE6BAE83467A1BF419D0767122DE409394414B05080DCE9EE5F237CBD108EABAE1E37759AE47F8E4203DA3532EB28DB860F33D62D49BD",
257 "03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B",
262 "4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703",
263 "00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6302A8DC32E64E86A333F20EF56EAC9BA30B7246D6D25E22ADB8C6BE1AEB08D49D",
264 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
266 "public key not on the curve"
269 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
270 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1DFA16AEE06609280A19B67A24E1977E4697712B5FD2943914ECD5F730901B4AB7",
271 "03EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34",
273 "incorrect R residuosity"
276 "5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C",
277 "00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BED092F9D860F1776A1F7412AD8A1EB50DACCC222BC8C0E26B2056DF2F273EFDEC",
278 "03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B",
280 "negated message hash"
283 "0000000000000000000000000000000000000000000000000000000000000000",
284 "787A848E71043D280C50470E8E1532B2DD5D20EE912A45DBDD2BD1DFBF187EF68FCE5677CE7A623CB20011225797CE7A8DE1DC6CCD4F754A47DA6C600E59543C",
285 "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
290 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
291 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
292 "03DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
297 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
298 "00000000000000000000000000000000000000000000000000000000000000009E9D01AF988B5CEDCE47221BFA9B222721F3FA408915444A4B489021DB55775F",
299 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
301 "sG - eP is infinite. Test fails in single verification if jacobi(y(inf)) is defined as 1 and x(inf) as 0"
304 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
305 "0000000000000000000000000000000000000000000000000000000000000001D37DDF0254351836D84B1BD6A795FD5D523048F298C4214D187FE4892947F728",
306 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
308 "sG - eP is infinite. Test fails in single verification if jacobi(y(inf)) is defined as 1 and x(inf) as 1"
311 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
312 "4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
313 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
315 "sig[0:32] is not an X coordinate on the curve"
318 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
319 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC2F1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
320 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
322 "sig[0:32] is equal to field size"
325 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
326 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1DFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
327 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
329 "sig[32:64] is equal to curve order"
334 boolean expected = test.expected;
340 String testMsg = String.join(
" ",
"testSchnorrVerify", String.valueOf(i++), String.valueOf(expected), test.comment);
342 assertEquals(result, expected, testMsg);
351 byte[] data =
hexStringToByteArray(
"5255683DA567900BFD3E786ED8836A4E7763C221BF1AC20ECE2A5171B9199E8A");
352 byte[] sec =
hexStringToByteArray(
"12B004FFF7F4B69EF8650E767F18F11EDE158148B425660723B9F9A66E61F747");
356 assertEquals(sigString,
"2C56731AC2F7A7E7F11518FC7722A166B02438924CA9D8B4D111347B81D0717571846DE67AD3D913A8FDF9D8F3F73161A4C48AE81CB183B214765FEB86E255CE",
"testSchnorrSign");
360 byte[] sec =
hexStringToByteArray(
"67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
361 byte[] pub =
hexStringToByteArray(
"040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
365 assertEquals(ecdhString,
"2A2A67007A926E6594AF3EB564FC74005B37A9C8AEF2033C4552051B5C87F043",
"testCreateECDHSecret");
368 public static void main(String[] args)
throws AssertFailException{
415 System.out.println(
"All tests passed.");
static byte[] createECDHSecret(byte[] seckey, byte[] pubkey)
libsecp256k1 create ECDH secret - constant time ECDH calculation
static boolean schnorrVerify(byte[] data, byte[] signature, byte[] pub)
Verifies the given Schnorr signature in native code.
static byte[] privKeyTweakAdd(byte[] privkey, byte[] tweak)
libsecp256k1 PrivKey Tweak-Add - Tweak privkey by adding to it
static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak)
libsecp256k1 PubKey Tweak-Mul - Tweak pubkey by multiplying to it
static byte[] sign(byte[] data, byte[] sec)
libsecp256k1 Create an ECDSA signature.
static synchronized boolean randomize(byte[] seed)
libsecp256k1 randomize - updates the context randomization
static boolean verify(byte[] data, byte[] signature, byte[] pub)
Verifies the given secp256k1 signature in native code.
static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak)
libsecp256k1 PubKey Tweak-Add - Tweak pubkey by adding to it
static byte[] privKeyTweakMul(byte[] privkey, byte[] tweak)
libsecp256k1 PrivKey Tweak-Mul - Tweak privkey by multiplying to it
static byte[] schnorrSign(byte[] data, byte[] seckey)
libsecp256k1 Create a Schnorr signature.
static byte[] computePubkey(byte[] seckey)
libsecp256k1 Compute Pubkey - computes public key from secret key
static synchronized void cleanup()
libsecp256k1 Cleanup - This destroys the secp256k1 context object This should be called at the end of...
static boolean secKeyVerify(byte[] seckey)
libsecp256k1 Seckey Verify - returns 1 if valid, 0 if invalid
This class holds test cases defined for testing this library.
static void main(String[] args)
static void testPrivKeyTweakAdd_2()
This tests private key tweak-add uncompressed.
static void testVerifyPos()
This tests verify() for a valid signature.
static String byteArrayToHexString(byte[] byteArray)
static byte[] hexStringToByteArray(String s)
static void testSignPos()
This tests sign() for a valid secretkey.
static void testSecKeyVerifyPos()
This tests secret key verify() for a valid secretkey.
static void testSchnorrSign()
This tests signSchnorr() for a valid secretkey.
static void testSchnorrVerify()
This tests schnorrVerify() for a valid signature.
static void testCreateECDHSecret()
static void testPrivKeyTweakAdd_1()
This tests private key tweak-add.
static void testPubKeyCreatePos()
This tests public key create() for a valid secretkey.
static void testPrivKeyTweakMul_2()
This tests private key tweak-mul uncompressed.
static void testSecKeyVerifyNeg()
This tests secret key verify() for an invalid secretkey.
static void testSignNeg()
This tests sign() for a invalid secretkey.
static void testPubKeyCreateNeg()
This tests public key create() for a invalid secretkey.
static void testPrivKeyTweakMul_1()
This tests private key tweak-mul.
static void testVerifyNeg()
This tests verify() for a non-valid signature.
static void testRandomize()
This tests seed randomization.
This class holds the context reference used in native methods to handle ECDSA operations.
static boolean isEnabled()