Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
NativeSecp256k1Test.java
Go to the documentation of this file.
1package org.bitcoin;
2
3import static org.bitcoin.NativeSecp256k1Util.*;
4
8public class NativeSecp256k1Test {
9
10 private static byte[] hexStringToByteArray(String s) {
11 int len = s.length();
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));
17 }
18 return byteArray;
19 }
20
21 private static String byteArrayToHexString(byte[] byteArray) {
23 for(byte b: byteArray) {
24 stringBuilder.append(String.format("%02X", b));
25 }
26 return stringBuilder.toString();
27 }
28
29 //TODO improve comments/add more tests
30
34 public static void testVerifyPos() throws AssertFailException{
35 boolean result = false;
36
37 // sha256hash of "testing"
38 byte[] data = hexStringToByteArray("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90");
39 byte[] sig = hexStringToByteArray("3044022079BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980220294F14E883B3F525B5367756C2A11EF6CF84B730B36C17CB0C56F0AAB2C98589");
40 byte[] pub = hexStringToByteArray("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
41
42 result = NativeSecp256k1.verify(data, sig, pub);
43 assertEquals(result, true, "testVerifyPos");
44 }
45
49 public static void testVerifyNeg() throws AssertFailException{
50 boolean result = false;
51
52 // sha256hash of "testing"
53 byte[] data = hexStringToByteArray("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A91");
54 byte[] sig = hexStringToByteArray("3044022079BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F817980220294F14E883B3F525B5367756C2A11EF6CF84B730B36C17CB0C56F0AAB2C98589");
55 byte[] pub = hexStringToByteArray("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
56
57 result = NativeSecp256k1.verify(data, sig, pub);
58 assertEquals(result, false, "testVerifyNeg");
59 }
60
64 public static void testSecKeyVerifyPos() throws AssertFailException{
65 boolean result = false;
66
67 byte[] sec = hexStringToByteArray("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
68
70 assertEquals(result, true, "testSecKeyVerifyPos");
71 }
72
76 public static void testSecKeyVerifyNeg() throws AssertFailException{
77 boolean result = false;
78
79 byte[] sec = hexStringToByteArray("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
80
82 assertEquals(result, false, "testSecKeyVerifyNeg");
83 }
84
88 public static void testPubKeyCreatePos() throws AssertFailException{
89 byte[] sec = hexStringToByteArray("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
90
93 assertEquals(pubkeyString, "04C591A8FF19AC9C4E4E5793673B83123437E975285E7B442F4EE2654DFFCA5E2D2103ED494718C697AC9AEBCFD19612E224DB46661011863ED2FC54E71861E2A6", "testPubKeyCreatePos");
94 }
95
99 public static void testPubKeyCreateNeg() throws AssertFailException{
100 byte[] sec = hexStringToByteArray("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
101
104 assertEquals(pubkeyString, "", "testPubKeyCreateNeg");
105 }
106
110 public static void testSignPos() throws AssertFailException{
111 // sha256hash of "testing"
112 byte[] data = hexStringToByteArray("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90");
113 byte[] sec = hexStringToByteArray("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
114
115 byte[] resultArr = NativeSecp256k1.sign(data, sec);
117 assertEquals(sigString, "3045022100F51D069AA46EDB4E2E77773FE364AA2AF6818AF733EA542CFC4D546640A58D8802204F1C442AC9F26F232451A0C3EE99F6875353FC73902C68055C19E31624F687CC", "testSignPos");
118 }
119
123 public static void testSignNeg() throws AssertFailException{
124 // sha256hash of "testing"
125 byte[] data = hexStringToByteArray("CF80CD8AED482D5D1527D7DC72FCEFF84E6326592848447D2DC0B0E87DFC9A90");
126 byte[] sec = hexStringToByteArray("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
127
128 byte[] resultArr = NativeSecp256k1.sign(data, sec);
130 assertEquals(sigString, "", "testSignNeg");
131 }
132
136 public static void testPrivKeyTweakAdd_1() throws AssertFailException {
137 // sha256hash of "tweak"
138 byte[] data = hexStringToByteArray("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
139 byte[] sec = hexStringToByteArray("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
140
143 assertEquals(sigString, "A168571E189E6F9A7E2D657A4B53AE99B909F7E712D1C23CED28093CD57C88F3", "testPrivKeyAdd_1");
144 }
145
149 public static void testPrivKeyTweakMul_1() throws AssertFailException {
150 // sha256hash of "tweak"
151 byte[] data = hexStringToByteArray("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
152 byte[] sec = hexStringToByteArray("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
153
156 assertEquals(sigString, "97F8184235F101550F3C71C927507651BD3F1CDB4A5A33B8986ACF0DEE20FFFC", "testPrivKeyMul_1");
157 }
158
162 public static void testPrivKeyTweakAdd_2() throws AssertFailException {
163 // sha256hash of "tweak"
164 byte[] data = hexStringToByteArray("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
165 byte[] pub = hexStringToByteArray("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
166
169 assertEquals(sigString, "0411C6790F4B663CCE607BAAE08C43557EDC1A4D11D88DFCB3D841D0C6A941AF525A268E2A863C148555C48FB5FBA368E88718A46E205FABC3DBA2CCFFAB0796EF", "testPrivKeyAdd_2");
170 }
171
175 public static void testPrivKeyTweakMul_2() throws AssertFailException {
176 // sha256hash of "tweak"
177 byte[] data = hexStringToByteArray("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3");
178 byte[] pub = hexStringToByteArray("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
179
182 assertEquals(sigString, "04E0FE6FE55EBCA626B98A807F6CAF654139E14E5E3698F01A9A658E21DC1D2791EC060D4F412A794D5370F672BC94B722640B5F76914151CFCA6E712CA48CC589", "testPrivKeyMul_2");
183 }
184
188 public static void testRandomize() throws AssertFailException {
189 // sha256hash of "random"
190 byte[] seed = hexStringToByteArray("A441B15FE9A3CF56661190A0B93B9DEC7D04127288CC87250967CF3B52894D11");
191
192 boolean result = NativeSecp256k1.randomize(seed);
193 assertEquals(result, true, "testRandomize");
194 }
195
196 private static class SchnorrTestVector {
197 String data;
198 String sig;
199 String pubKey;
200 boolean expected;
201 String comment;
202
203 SchnorrTestVector(String d, String s, String p, boolean e, String c) {
204 data = d;
205 sig = s;
206 pubKey = p;
207 expected = e;
208 comment = c;
209 }
210 }
211
217 public static void testSchnorrVerify() throws AssertFailException{
220 "0000000000000000000000000000000000000000000000000000000000000000",
221 "787A848E71043D280C50470E8E1532B2DD5D20EE912A45DBDD2BD1DFBF187EF67031A98831859DC34DFFEEDDA86831842CCD0079E1F92AF177F7F22CC1DCED05",
222 "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
223 true,
224 "success"
225 ),
227 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
228 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
229 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
230 true,
231 "success"
232 ),
234 "5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C",
235 "00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BE00880371D01766935B92D2AB4CD5C8A2A5837EC57FED7660773A05F0DE142380",
236 "03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B",
237 true,
238 "success"
239 ),
241 "4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703",
242 "00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6302A8DC32E64E86A333F20EF56EAC9BA30B7246D6D25E22ADB8C6BE1AEB08D49D",
243 "03DEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34",
244 true,
245 "success"
246 ),
248 "0000000000000000000000000000000000000000000000000000000000000000",
249 "52818579ACA59767E3291D91B76B637BEF062083284992F2D95F564CA6CB4E3530B1DA849C8E8304ADC0CFE870660334B3CFC18E825EF1DB34CFAE3DFC5D8187",
250 "031B84C5567B126440995D3ED5AABA0565D71E1834604819FF9C17F5E9D5DD078F",
251 true,
252 "success"
253 ),
255 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
256 "570DD4CA83D4E6317B8EE6BAE83467A1BF419D0767122DE409394414B05080DCE9EE5F237CBD108EABAE1E37759AE47F8E4203DA3532EB28DB860F33D62D49BD",
257 "03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B",
258 true,
259 "success"
260 ),
262 "4DF3C3F68FCC83B27E9D42C90431A72499F17875C81A599B566C9889B9696703",
263 "00000000000000000000003B78CE563F89A0ED9414F5AA28AD0D96D6795F9C6302A8DC32E64E86A333F20EF56EAC9BA30B7246D6D25E22ADB8C6BE1AEB08D49D",
264 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
265 false,
266 "public key not on the curve"
267 ),
269 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
270 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1DFA16AEE06609280A19B67A24E1977E4697712B5FD2943914ECD5F730901B4AB7",
271 "03EEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34",
272 false,
273 "incorrect R residuosity"
274 ),
276 "5E2D58D8B3BCDF1ABADEC7829054F90DDA9805AAB56C77333024B9D0A508B75C",
277 "00DA9B08172A9B6F0466A2DEFD817F2D7AB437E0D253CB5395A963866B3574BED092F9D860F1776A1F7412AD8A1EB50DACCC222BC8C0E26B2056DF2F273EFDEC",
278 "03FAC2114C2FBB091527EB7C64ECB11F8021CB45E8E7809D3C0938E4B8C0E5F84B",
279 false,
280 "negated message hash"
281 ),
283 "0000000000000000000000000000000000000000000000000000000000000000",
284 "787A848E71043D280C50470E8E1532B2DD5D20EE912A45DBDD2BD1DFBF187EF68FCE5677CE7A623CB20011225797CE7A8DE1DC6CCD4F754A47DA6C600E59543C",
285 "0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
286 false,
287 "negated s value"
288 ),
290 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
291 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
292 "03DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
293 false,
294 "negated public key"
295 ),
297 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
298 "00000000000000000000000000000000000000000000000000000000000000009E9D01AF988B5CEDCE47221BFA9B222721F3FA408915444A4B489021DB55775F",
299 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
300 false,
301 "sG - eP is infinite. Test fails in single verification if jacobi(y(inf)) is defined as 1 and x(inf) as 0"
302 ),
304 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
305 "0000000000000000000000000000000000000000000000000000000000000001D37DDF0254351836D84B1BD6A795FD5D523048F298C4214D187FE4892947F728",
306 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
307 false,
308 "sG - eP is infinite. Test fails in single verification if jacobi(y(inf)) is defined as 1 and x(inf) as 1"
309 ),
311 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
312 "4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
313 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
314 false,
315 "sig[0:32] is not an X coordinate on the curve"
316 ),
318 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
319 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC2F1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD",
320 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
321 false,
322 "sig[0:32] is equal to field size"
323 ),
325 "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89",
326 "2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1DFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
327 "02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659",
328 false,
329 "sig[32:64] is equal to curve order"
330 )
331 };
332 int i = 0;
334 boolean expected = test.expected;
335 byte[] data = hexStringToByteArray(test.data);
336 byte[] sig = hexStringToByteArray(test.sig);
337 byte[] pub = hexStringToByteArray(test.pubKey);
338 boolean result = NativeSecp256k1.schnorrVerify(data, sig, pub);
339
340 String testMsg = String.join(" ", "testSchnorrVerify", String.valueOf(i++), String.valueOf(expected), test.comment);
341
342 assertEquals(result, expected, testMsg);
343 }
344 }
345
349 public static void testSchnorrSign() throws AssertFailException{
350 // sha256(sha256("Very deterministic message"))
351 byte[] data = hexStringToByteArray("5255683DA567900BFD3E786ED8836A4E7763C221BF1AC20ECE2A5171B9199E8A");
352 byte[] sec = hexStringToByteArray("12B004FFF7F4B69EF8650E767F18F11EDE158148B425660723B9F9A66E61F747");
353
356 assertEquals(sigString, "2C56731AC2F7A7E7F11518FC7722A166B02438924CA9D8B4D111347B81D0717571846DE67AD3D913A8FDF9D8F3F73161A4C48AE81CB183B214765FEB86E255CE", "testSchnorrSign");
357 }
358
359 public static void testCreateECDHSecret() throws AssertFailException{
360 byte[] sec = hexStringToByteArray("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530");
361 byte[] pub = hexStringToByteArray("040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE595DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40");
362
365 assertEquals(ecdhString, "2A2A67007A926E6594AF3EB564FC74005B37A9C8AEF2033C4552051B5C87F043", "testCreateECDHSecret");
366 }
367
368 public static void main(String[] args) throws AssertFailException{
369 System.out.println("\nlibsecp256k1 enabled: " + Secp256k1Context.isEnabled() + "\n");
370
371 assertEquals(Secp256k1Context.isEnabled(), true, "isEnabled");
372
373 // Test verify() success/fail
376
377 // Test secKeyVerify() success/fail
380
381 // Test computePubkey() success/fail
384
385 // Test sign() success/fail
386 testSignPos();
387 testSignNeg();
388
389 // Test privKeyTweakAdd() 1
391
392 // Test privKeyTweakMul() 2
394
395 // Test privKeyTweakAdd() 3
397
398 // Test privKeyTweakMul() 4
400
401 // Test randomize()
403
404 // Test verifySchnorr() success/fail
406
407 // Test schnorrSign()
409
410 // Test ECDH
412
414
415 System.out.println("All tests passed.");
416 }
417}
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 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 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.
SchnorrSig sig
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
assert(!tx.IsCoinBase())