Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
org_bitcoin_NativeSecp256k1.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
5#include "include/secp256k1.h"
9
22
25{
27
28 const unsigned char* seed = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
29
31
32 return secp256k1_context_randomize(ctx, seed);
33
34}
35
45
48{
50
51 unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
52 const unsigned char* sigdata = { (unsigned char*) (data + 32) };
53 const unsigned char* pubdata = { (unsigned char*) (data + siglen + 32) };
54
56 secp256k1_pubkey pubkey;
57
58 int ret = secp256k1_ecdsa_signature_parse_der(ctx, &sig, sigdata, siglen);
59
60 if( ret ) {
62
63 if( ret ) {
64 ret = secp256k1_ecdsa_verify(ctx, &sig, data, &pubkey);
65 }
66 }
67
69
70 return ret;
71}
72
75{
77 unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
78 unsigned char* secKey = (unsigned char*) (data + 32);
79
82 unsigned char intsarray[2];
83
85
86 int ret = secp256k1_ecdsa_sign(ctx, &sig, data, secKey, NULL, NULL);
87
88 unsigned char outputSer[72];
89 size_t outputLen = 72;
90
91 if( ret ) {
93 }
94
96 intsarray[1] = ret;
97
98 retArray = (*env)->NewObjectArray(env, 2,
99 (*env)->FindClass(env, "[B"),
100 (*env)->NewByteArray(env, 1));
101
102 sigArray = (*env)->NewByteArray(env, outputLen);
103 (*env)->SetByteArrayRegion(env, sigArray, 0, outputLen, (jbyte*)outputSer);
104 (*env)->SetObjectArrayElement(env, retArray, 0, sigArray);
105
106 intsByteArray = (*env)->NewByteArray(env, 2);
107 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray);
108 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
109
111
112 return retArray;
113}
114
125
128{
130 const unsigned char* secKey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
131
132 secp256k1_pubkey pubkey;
133
136 unsigned char intsarray[2];
137
138 int ret = secp256k1_ec_pubkey_create(ctx, &pubkey, secKey);
139
140 unsigned char outputSer[65];
141 size_t outputLen = 65;
142
143 if( ret ) {
145 }
146
147 intsarray[0] = outputLen;
148 intsarray[1] = ret;
149
150 retArray = (*env)->NewObjectArray(env, 2,
151 (*env)->FindClass(env, "[B"),
152 (*env)->NewByteArray(env, 1));
153
154 pubkeyArray = (*env)->NewByteArray(env, outputLen);
155 (*env)->SetByteArrayRegion(env, pubkeyArray, 0, outputLen, (jbyte*)outputSer);
156 (*env)->SetObjectArrayElement(env, retArray, 0, pubkeyArray);
157
158 intsByteArray = (*env)->NewByteArray(env, 2);
159 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray);
160 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
161
163
164 return retArray;
165
166}
167
170{
172 unsigned char* privkey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
173 const unsigned char* tweak = (unsigned char*) (privkey + 32);
174
177 unsigned char intsarray[2];
178
179 int privkeylen = 32;
180
182
184 intsarray[1] = ret;
185
186 retArray = (*env)->NewObjectArray(env, 2,
187 (*env)->FindClass(env, "[B"),
188 (*env)->NewByteArray(env, 1));
189
190 privArray = (*env)->NewByteArray(env, privkeylen);
191 (*env)->SetByteArrayRegion(env, privArray, 0, privkeylen, (jbyte*)privkey);
192 (*env)->SetObjectArrayElement(env, retArray, 0, privArray);
193
194 intsByteArray = (*env)->NewByteArray(env, 2);
195 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray);
196 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
197
199
200 return retArray;
201}
202
205{
207 unsigned char* privkey = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
208 const unsigned char* tweak = (unsigned char*) (privkey + 32);
209
212 unsigned char intsarray[2];
213
214 int privkeylen = 32;
215
217
219 intsarray[1] = ret;
220
221 retArray = (*env)->NewObjectArray(env, 2,
222 (*env)->FindClass(env, "[B"),
223 (*env)->NewByteArray(env, 1));
224
225 privArray = (*env)->NewByteArray(env, privkeylen);
226 (*env)->SetByteArrayRegion(env, privArray, 0, privkeylen, (jbyte*)privkey);
227 (*env)->SetObjectArrayElement(env, retArray, 0, privArray);
228
229 intsByteArray = (*env)->NewByteArray(env, 2);
230 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray);
231 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
232
234
235 return retArray;
236}
237
240{
242/* secp256k1_pubkey* pubkey = (secp256k1_pubkey*) (*env)->GetDirectBufferAddress(env, byteBufferObject);*/
243 unsigned char* pkey = (*env)->GetDirectBufferAddress(env, byteBufferObject);
244 const unsigned char* tweak = (unsigned char*) (pkey + publen);
245
248 unsigned char intsarray[2];
249 unsigned char outputSer[65];
250 size_t outputLen = 65;
251
252 secp256k1_pubkey pubkey;
253 int ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pkey, publen);
254
255 if( ret ) {
257 }
258
259 if( ret ) {
261 }
262
263 intsarray[0] = outputLen;
264 intsarray[1] = ret;
265
266 retArray = (*env)->NewObjectArray(env, 2,
267 (*env)->FindClass(env, "[B"),
268 (*env)->NewByteArray(env, 1));
269
270 pubArray = (*env)->NewByteArray(env, outputLen);
271 (*env)->SetByteArrayRegion(env, pubArray, 0, outputLen, (jbyte*)outputSer);
272 (*env)->SetObjectArrayElement(env, retArray, 0, pubArray);
273
274 intsByteArray = (*env)->NewByteArray(env, 2);
275 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray);
276 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
277
279
280 return retArray;
281}
282
285{
287 unsigned char* pkey = (*env)->GetDirectBufferAddress(env, byteBufferObject);
288 const unsigned char* tweak = (unsigned char*) (pkey + publen);
289
292 unsigned char intsarray[2];
293 unsigned char outputSer[65];
294 size_t outputLen = 65;
295
296 secp256k1_pubkey pubkey;
297 int ret = secp256k1_ec_pubkey_parse(ctx, &pubkey, pkey, publen);
298
299 if ( ret ) {
301 }
302
303 if( ret ) {
305 }
306
307 intsarray[0] = outputLen;
308 intsarray[1] = ret;
309
310 retArray = (*env)->NewObjectArray(env, 2,
311 (*env)->FindClass(env, "[B"),
312 (*env)->NewByteArray(env, 1));
313
314 pubArray = (*env)->NewByteArray(env, outputLen);
315 (*env)->SetByteArrayRegion(env, pubArray, 0, outputLen, (jbyte*)outputSer);
316 (*env)->SetObjectArrayElement(env, retArray, 0, pubArray);
317
318 intsByteArray = (*env)->NewByteArray(env, 2);
319 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 2, (jbyte*)intsarray);
320 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
321
323
324 return retArray;
325}
326
334
337{
339
340 unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
341 const unsigned char* sigdata = { (unsigned char*) (data + 32) };
342 const unsigned char* pubdata = { (unsigned char*) (data + 32 + 64) };
343
344 secp256k1_pubkey pubkey;
346
347 if( ret ) {
348 ret = secp256k1_schnorr_verify(ctx, sigdata, data, &pubkey);
349 }
350
352
353 return ret;
354}
355
358{
360 unsigned char* data = (unsigned char*) (*env)->GetDirectBufferAddress(env, byteBufferObject);
361 unsigned char* secKey = (unsigned char*) (data + 32);
362
365 unsigned char intsarray[1];
366 unsigned char sig[64];
367
369
370 retArray = (*env)->NewObjectArray(env, 2,
371 (*env)->FindClass(env, "[B"),
372 (*env)->NewByteArray(env, 1));
373
374 sigArray = (*env)->NewByteArray(env, 64);
375 (*env)->SetByteArrayRegion(env, sigArray, 0, 64, (jbyte*)sig);
376 (*env)->SetObjectArrayElement(env, retArray, 0, sigArray);
377
378 intsByteArray = (*env)->NewByteArray(env, 1);
379 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 1, (jbyte*)intsarray);
380 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
381
383
384 return retArray;
385}
386
389{
391 const unsigned char* secdata = (*env)->GetDirectBufferAddress(env, byteBufferObject);
392 const unsigned char* pubdata = (const unsigned char*) (secdata + 32);
393
396 unsigned char intsarray[1];
397 secp256k1_pubkey pubkey;
398 unsigned char nonce_res[32];
399 size_t outputLen = 32;
400
402
403 if (ret) {
405 ctx,
406 nonce_res,
407 &pubkey,
408 secdata,
409 NULL,
410 NULL
411 );
412 }
413
414 intsarray[0] = ret;
415
416 retArray = (*env)->NewObjectArray(env, 2,
417 (*env)->FindClass(env, "[B"),
418 (*env)->NewByteArray(env, 1));
419
420 outArray = (*env)->NewByteArray(env, outputLen);
421 (*env)->SetByteArrayRegion(env, outArray, 0, 32, (jbyte*)nonce_res);
422 (*env)->SetObjectArrayElement(env, retArray, 0, outArray);
423
424 intsByteArray = (*env)->NewByteArray(env, 1);
425 (*env)->SetByteArrayRegion(env, intsByteArray, 0, 1, (jbyte*)intsarray);
426 (*env)->SetObjectArrayElement(env, retArray, 1, intsByteArray);
427
429
430 return retArray;
431}
secp256k1_context * ctx
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1sign(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1verify(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint siglen, jint publen)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1mul(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1create(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1add(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1mul(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen)
SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1schnorr_1verify(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdh(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1schnorr_1sign(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1pubkey_1combine(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint numkeys)
SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1seckey_1verify(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ctx_1clone(JNIEnv *env, jclass classObject, jlong ctx_l)
SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1context_1randomize(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l)
SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1add(JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen)
SECP256K1_API void JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1destroy_1context(JNIEnv *env, jclass classObject, jlong ctx_l)
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
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
Definition secp256k1.c:730
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition secp256k1.c:296
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
Definition secp256k1.c:550
#define SECP256K1_API
Definition secp256k1.h:140
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
Definition secp256k1.c:535
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition secp256k1.c:278
SECP256K1_API int secp256k1_ecdsa_signature_parse_der(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a DER ECDSA signature.
Definition secp256k1.c:343
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition secp256k1.c:573
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify an ECDSA signature.
Definition secp256k1.c:424
SECP256K1_API secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1) SECP256K1_WARN_UNUSED_RESULT
Copy a secp256k1 context object (into dynamically allocated memory).
Definition secp256k1.c:177
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a public key by adding tweak times the generator to it.
Definition secp256k1.c:664
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Same as secp256k1_ec_seckey_tweak_add, but DEPRECATED.
Definition secp256k1.c:653
#define SECP256K1_EC_UNCOMPRESSED
Definition secp256k1.h:177
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Same as secp256k1_ec_seckey_tweak_mul, but DEPRECATED.
Definition secp256k1.c:702
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
Definition secp256k1.c:380
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a public key by multiplying it by a tweak value.
Definition secp256k1.c:706
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition secp256k1.c:196
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *output, const secp256k1_pubkey *pubkey, const unsigned char *seckey, secp256k1_ecdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time.
Definition main_impl.h:29
SECP256K1_API int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a signature using a custom EC-Schnorr-SHA256 construction.
Definition main_impl.h:33
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorr_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify a signature created by secp256k1_schnorr_sign.
Definition main_impl.h:13
Opaque data structured that holds a parsed ECDSA signature.
Definition secp256k1.h:80
Opaque data structure that holds a parsed and valid public key.
Definition secp256k1.h:67