Secp256k1.xs

filter: Statementstmt 83.9% 403 / 480 Branchbran 68.1% 165 / 242 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.6% 568 / 722 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Bitcoin/Secp256k1.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #define SECP256K1_STATIC
7 #include <secp256k1.h>
8 #include <secp256k1_extrakeys.h>
9 #include <secp256k1_schnorrsig.h>
10 #include <secp256k1_recovery.h>
11  
12 #define CURVE_SIZE 32
13 #define SCHNORR_SIGNATURE_SIZE 64
14 #define RECOVERABLE_SIGNATURE_SIZE 64
15  
16 typedef struct {
17 void **items;
18 unsigned int item_count;
19 unsigned int capacity;
20 } secp256k1_perl_multifield;
21  
22 6 secp256k1_perl_multifield* secp256k1_perl_multifield_create()
23 {
24 6 secp256k1_perl_multifield *result = malloc (sizeof *result);
25 6 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 6 6
26 0 return NULL;
27 }
28  
29 6 result->capacity = 1;
30 6 result->item_count = 0;
31 6 result->items = malloc(sizeof *result->items * result->capacity);
32 6 ▶ if (!result->items) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result->items) { 0 6 6
33 0 free(result);
34 0 return NULL;
35 }
36  
37 6 return result;
38 }
39  
40 60 void secp256k1_perl_multifield_clear(secp256k1_perl_multifield *multifield)
41 {
42 int i;
43 115 ▶ for (i = 0; i < multifield->item_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < multifield->item_count; ++i) { 55 60 115
44 55 free(multifield->items[i]);
45 }
46  
47 60 multifield->item_count = 0;
48 60 }
49  
50 /* IMPORTANT: clear must be called first explicitly (to avoid memory leak) */
51 6 void secp256k1_perl_multifield_destroy(secp256k1_perl_multifield *multifield)
52 {
53 6 free(multifield->items);
54 6 free(multifield);
55 6 }
56  
57 55 int secp256k1_perl_multifield_push(secp256k1_perl_multifield *multifield, void *value)
58 {
59 55 ▶ if (multifield->item_count == multifield->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (multifield->item_count == multifield->capacity) { 4 51 55
60 4 multifield->capacity *= 2;
61 4 multifield->items = realloc(multifield->items, sizeof *multifield->items * multifield->capacity);
62 4 ▶ if (!multifield->items) {
Branch: 1 branch
BranchTrueFalseTotal
if (!multifield->items) { 0 4 4
63 0 return 0;
64 }
65 }
66  
67 55 multifield->items[multifield->item_count] = value;
68 55 multifield->item_count += 1;
69 55 return 1;
70 }
71  
72 typedef struct {
73 secp256k1_context *ctx;
74 secp256k1_perl_multifield *pubkeys;
75 secp256k1_xonly_pubkey *xonly_pubkey;
76 secp256k1_ecdsa_signature *signature;
77 unsigned char *schnorr_signature;
78 secp256k1_ecdsa_recoverable_signature *recoverable_signature;
79 } secp256k1_perl;
80  
81 int secp256k1_perl_replace_pubkey(secp256k1_perl *perl_ctx, secp256k1_pubkey *new_pubkey);
82 void secp256k1_perl_replace_xonly_pubkey(secp256k1_perl *perl_ctx, secp256k1_xonly_pubkey *new_pubkey);
83 void secp256k1_perl_replace_signature(secp256k1_perl *perl_ctx, secp256k1_ecdsa_signature *new_signature);
84 void secp256k1_perl_replace_schnorr_signature(secp256k1_perl *perl_ctx, unsigned char *new_signature);
85 void secp256k1_perl_replace_recoverable_signature(secp256k1_perl *perl_ctx, secp256k1_ecdsa_recoverable_signature *new_signature);
86  
87 6 secp256k1_perl* secp256k1_perl_create()
88 {
89 6 secp256k1_perl *perl_ctx = malloc(sizeof *perl_ctx);
90 6 ▶ if (!perl_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!perl_ctx) { 0 6 6
91 0 return NULL;
92 }
93  
94 6 perl_ctx->pubkeys = secp256k1_perl_multifield_create();
95 6 ▶ if (!perl_ctx->pubkeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (!perl_ctx->pubkeys) { 0 6 6
96 0 free(perl_ctx);
97 0 return NULL;
98 }
99  
100 6 perl_ctx->ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
101 6 ▶ if (!perl_ctx->ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!perl_ctx->ctx) { 0 6 6
102 0 free(perl_ctx->pubkeys);
103 0 free(perl_ctx);
104 0 return NULL;
105 }
106  
107 6 perl_ctx->xonly_pubkey = NULL;
108 6 perl_ctx->signature = NULL;
109 6 perl_ctx->schnorr_signature = NULL;
110 6 perl_ctx->recoverable_signature = NULL;
111  
112 6 return perl_ctx;
113 }
114  
115 8 void secp256k1_perl_clear(secp256k1_perl *perl_ctx)
116 {
117 8 secp256k1_perl_replace_xonly_pubkey(perl_ctx, NULL);
118 8 secp256k1_perl_replace_signature(perl_ctx, NULL);
119 8 secp256k1_perl_replace_schnorr_signature(perl_ctx, NULL);
120 8 secp256k1_perl_replace_recoverable_signature(perl_ctx, NULL);
121  
122 8 secp256k1_perl_multifield_clear(perl_ctx->pubkeys);
123 8 }
124  
125 6 void secp256k1_perl_destroy(secp256k1_perl *perl_ctx)
126 {
127 6 secp256k1_perl_clear(perl_ctx);
128 6 secp256k1_perl_multifield_destroy(perl_ctx->pubkeys);
129 6 secp256k1_context_destroy(perl_ctx->ctx);
130 6 free(perl_ctx);
131 6 }
132  
133 52 int secp256k1_perl_replace_pubkey(secp256k1_perl *perl_ctx, secp256k1_pubkey *new_pubkey)
134 {
135 52 secp256k1_perl_multifield_clear(perl_ctx->pubkeys);
136 52 ▶ if (new_pubkey != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_pubkey != NULL) { 47 5 52
137 47 ▶ if (!secp256k1_perl_multifield_push(perl_ctx->pubkeys, new_pubkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_multifield_push(perl_ctx->pubkeys, new_pubkey)) { 0 47 47
138 0 return 0;
139 }
140 }
141  
142 52 return 1;
143 }
144  
145 18 void secp256k1_perl_replace_xonly_pubkey(secp256k1_perl *perl_ctx, secp256k1_xonly_pubkey *new_pubkey)
146 {
147 18 ▶ if (perl_ctx->xonly_pubkey != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (perl_ctx->xonly_pubkey != NULL) { 9 9 18
148 9 free(perl_ctx->xonly_pubkey);
149 }
150  
151 18 perl_ctx->xonly_pubkey = new_pubkey;
152 18 }
153  
154 36 void secp256k1_perl_replace_signature(secp256k1_perl *perl_ctx, secp256k1_ecdsa_signature *new_signature)
155 {
156 36 ▶ if (perl_ctx->signature != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (perl_ctx->signature != NULL) { 27 9 36
157 27 free(perl_ctx->signature);
158 }
159  
160 36 perl_ctx->signature = new_signature;
161 36 }
162  
163 21 void secp256k1_perl_replace_schnorr_signature(secp256k1_perl *perl_ctx, unsigned char *new_signature)
164 {
165 21 ▶ if (perl_ctx->schnorr_signature != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (perl_ctx->schnorr_signature != NULL) { 12 9 21
166 12 free(perl_ctx->schnorr_signature);
167 }
168  
169 21 perl_ctx->schnorr_signature = new_signature;
170 21 }
171  
172 27 void secp256k1_perl_replace_recoverable_signature(secp256k1_perl *perl_ctx, secp256k1_ecdsa_recoverable_signature *new_signature)
173 {
174 27 ▶ if (perl_ctx->recoverable_signature != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (perl_ctx->recoverable_signature != NULL) { 19 8 27
175 19 free(perl_ctx->recoverable_signature);
176 }
177  
178 27 perl_ctx->recoverable_signature = new_signature;
179 27 }
180  
181 63 secp256k1_pubkey* secp256k1_perl_first_pubkey(secp256k1_perl *perl_ctx)
182 {
183 63 ▶ if (perl_ctx->pubkeys->item_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (perl_ctx->pubkeys->item_count == 0) { 0 63 63
184 0 croak("missing public key");
185 }
186  
187 63 return perl_ctx->pubkeys->items[0];
188 }
189  
190 /* HELPERS */
191  
192 227 secp256k1_perl* ctx_from_sv(SV *self)
193 {
194 227 ▶ if (!(sv_isobject(self) && sv_derived_from(self, "Bitcoin::Secp256k1"))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(sv_isobject(self) && sv_derived_from(self, "Bitcoin::Secp256k1"))) { 225 2 227
if (!(sv_isobject(self) && sv_derived_from(self, "Bitcoin::Secp256k1"))) { 0 225 225
195 2 croak("calling Bitcoin::Secp256k1 methods is only valid in object context");
196 }
197  
198 225 return (secp256k1_perl*) SvIV(SvRV(self));
199 }
200  
201 162 unsigned char* bytestr_from_sv(SV *perlval, STRLEN *size)
202 {
203 162 return (unsigned char*) SvPVbyte(perlval, *size);
204 }
205  
206 103 unsigned char* size_bytestr_from_sv(SV *perlval, size_t wanted_size, char *argname)
207 {
208 STRLEN size;
209 103 unsigned char *bytestr = bytestr_from_sv(perlval, &size);
210  
211 103 ▶ if (size != wanted_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size != wanted_size) { 5 98 103
212 5 croak("%s must be a bytestring of length %lu", argname, (unsigned long) wanted_size);
213 }
214  
215 98 return bytestr;
216 }
217  
218 42 secp256k1_pubkey* pubkey_from_sv(secp256k1_perl *ctx, SV *data)
219 {
220 42 ▶ if (!SvOK(data) || SvROK(data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(data) || SvROK(data)) { 42 0 42
if (!SvOK(data) || SvROK(data)) { 0 42 42
221 0 croak("public key must be defined and not a reference");
222 }
223  
224 size_t key_size;
225 42 unsigned char *key = bytestr_from_sv(data, &key_size);
226  
227 42 secp256k1_pubkey *result_pubkey = malloc(sizeof *result_pubkey);
228 42 ▶ if (!result_pubkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_pubkey) { 0 42 42
229 0 croak("failed to allocate pubkey memory");
230 }
231  
232 42 int result = secp256k1_ec_pubkey_parse(
233 42 ctx->ctx,
234 result_pubkey,
235 key,
236 key_size
237 );
238  
239 42 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 4 38 42
240 4 free(result_pubkey);
241 4 croak("the input does not appear to be a valid public key");
242 }
243  
244 38 return result_pubkey;
245 }
246  
247 9 secp256k1_xonly_pubkey* xonly_pubkey_from_sv(secp256k1_perl *ctx, SV *data)
248 {
249 9 ▶ if (!SvOK(data) || SvROK(data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(data) || SvROK(data)) { 9 0 9
if (!SvOK(data) || SvROK(data)) { 0 9 9
250 0 croak("xonly public key must be defined and not a reference");
251 }
252  
253 9 unsigned char *key = size_bytestr_from_sv(data, CURVE_SIZE, "xonly pubkey");
254  
255 8 secp256k1_xonly_pubkey *result_pubkey = malloc(sizeof *result_pubkey);
256 8 ▶ if (!result_pubkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_pubkey) { 0 8 8
257 0 croak("failed to allocate pubkey memory");
258 }
259  
260 8 int result = secp256k1_xonly_pubkey_parse(
261 8 ctx->ctx,
262 result_pubkey,
263 key
264 );
265  
266 8 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 8 8
267 0 free(result_pubkey);
268 0 croak("the input does not appear to be a valid xonly public key");
269 }
270  
271 8 return result_pubkey;
272 }
273  
274 5 void copy_bytestr(unsigned char *to, unsigned char *from, size_t size)
275 {
276 int i;
277 165 ▶ for (i = 0; i < size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; ++i) { 160 5 165
278 160 to[i] = from[i];
279 }
280 5 }
281  
282 9 void clean_secret(unsigned char *secret, size_t size)
283 {
284 int i;
285 553 ▶ for (i = 0; i < size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < size; ++i) { 544 9 553
286 544 secret[i] = 0;
287 }
288 9 }
289  
290 /* XS code below */
291  
292 MODULE = Bitcoin::Secp256k1 PACKAGE = Bitcoin::Secp256k1
293  
294 PROTOTYPES: DISABLE
295  
296 SV*
297 new(classname)
298 SV *classname
299 CODE:
300 const char *classname_str;
301 6 ▶ if (sv_isobject(classname)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(classname)) { 1 5 6
302 1 classname_str = sv_reftype(SvRV(classname), 1);
303 }
304 else {
305 5 classname_str = SvPVbyte_nolen(classname);
306 }
307  
308 /* Calling perl function to randomize context */
309 6 dSP;
310 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
311  
312 6 SV *tmp = newSViv(CURVE_SIZE);
313  
314 6 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 6 6
315 6 PUSHs(tmp);
316 6 PUTBACK;
317  
318 6 size_t count = call_pv("Bitcoin::Secp256k1::_random_bytes", G_SCALAR);
319 6 SvREFCNT_dec(tmp);
320  
321 6 SPAGAIN;
322  
323 6 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 6 6
324 0 croak("fetching randomness went wrong in Bitcoin::Secp256k1::new");
325 }
326  
327 6 tmp = POPs;
328 6 PUTBACK;
329  
330 12 unsigned char *randomize = SvOK(tmp)
331 0 ? size_bytestr_from_sv(tmp, CURVE_SIZE, "random data")
332 6 ▶ : NULL
Branch: 1 branch
BranchTrueFalseTotal
: NULL 0 6 6
333 ;
334  
335 /* Randomness dump */
336 /* for (int i = 0; i < len; ++i) { warn("%d: %d", i, randomize[i]); } */
337  
338 6 secp256k1_perl* ctx = secp256k1_perl_create();
339 6 ▶ if (!ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) { 0 6 6
340 0 croak("Failed to create secp256k1 perl context");
341 }
342  
343 6 ▶ if (randomize != NULL && !secp256k1_context_randomize(ctx->ctx, randomize)) {
Branch: 2 branches
BranchTrueFalseTotal
if (randomize != NULL && !secp256k1_context_randomize(ctx->ctx, randomize)) { 0 6 6
if (randomize != NULL && !secp256k1_context_randomize(ctx->ctx, randomize)) { 0 0 0
344 0 secp256k1_perl_destroy(ctx);
345 0 croak("Failed to randomize secp256k1 context");
346 }
347  
348 /* Blessing the object */
349 6 SV *secp_sv = newSViv(0);
350 6 RETVAL = sv_setref_iv(secp_sv, classname_str, (uintptr_t) ctx);
351 6 SvREADONLY_on(SvRV(secp_sv));
352 OUTPUT:
353 RETVAL
354  
355 # Clears public key and signature from the object
356 void
357 _clear(self)
358 SV *self
359 CODE:
360 2 secp256k1_perl *ctx = ctx_from_sv(self);
361 2 secp256k1_perl_clear(ctx);
362  
363  
364 # Getter / setter for the public key
365 SV*
366 _pubkey(self, ...)
367 SV *self
368 CODE:
369 62 secp256k1_perl *ctx = ctx_from_sv(self);
370 91 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 38 23 61
if (items > 1 && SvOK(ST(1))) { 33 5 38
371 33 SV *pubkey_data = ST(1);
372 33 secp256k1_pubkey *new_pubkey = pubkey_from_sv(ctx, pubkey_data);
373 30 ▶ if (!secp256k1_perl_replace_pubkey(ctx, new_pubkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_replace_pubkey(ctx, new_pubkey)) { 0 30 30
374 0 croak("could not replace a public key");
375 }
376 }
377 28 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 5 23 28
378 5 ▶ if (!secp256k1_perl_replace_pubkey(ctx, NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_replace_pubkey(ctx, NULL)) { 0 5 5
379 0 croak("could not replace a public key");
380 }
381 }
382  
383 58 unsigned int compression = SECP256K1_EC_COMPRESSED;
384  
385 58 ▶ if (items > 2 && !SvTRUE(ST(2))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 2 && !SvTRUE(ST(2))) { 11 47 58
if (items > 2 && !SvTRUE(ST(2))) { 3 8 11
386 3 compression = SECP256K1_EC_UNCOMPRESSED;
387 }
388  
389 58 ▶ if (ctx->pubkeys->item_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pubkeys->item_count > 0) { 51 7 58
390 unsigned char key_output[65];
391 51 size_t key_size = 65;
392 51 secp256k1_ec_pubkey_serialize(
393 51 ctx->ctx,
394 key_output,
395 &key_size,
396 51 secp256k1_perl_first_pubkey(ctx),
397 compression
398 );
399  
400 51 RETVAL = newSVpv((char*) key_output, key_size);
401 }
402 else {
403 7 RETVAL = &PL_sv_undef;
404 }
405 OUTPUT:
406 RETVAL
407  
408 # Getter / setter for the xonly public key
409 SV*
410 _xonly_pubkey(self, ...)
411 SV *self
412 CODE:
413 14 secp256k1_perl *ctx = ctx_from_sv(self);
414 22 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 10 4 14
if (items > 1 && SvOK(ST(1))) { 9 1 10
415 9 SV *pubkey_data = ST(1);
416 9 secp256k1_xonly_pubkey *new_pubkey = xonly_pubkey_from_sv(ctx, pubkey_data);
417 8 secp256k1_perl_replace_xonly_pubkey(ctx, new_pubkey);
418 }
419 5 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 1 4 5
420 1 secp256k1_perl_replace_xonly_pubkey(ctx, NULL);
421 }
422  
423 13 ▶ if (ctx->xonly_pubkey != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->xonly_pubkey != NULL) { 10 3 13
424 unsigned char key_output[CURVE_SIZE];
425 10 secp256k1_xonly_pubkey_serialize(
426 10 ctx->ctx,
427 key_output,
428 10 ctx->xonly_pubkey
429 );
430  
431 10 RETVAL = newSVpv((char*) key_output, CURVE_SIZE);
432 }
433 else {
434 3 RETVAL = &PL_sv_undef;
435 }
436 OUTPUT:
437 RETVAL
438  
439 # Getter / setter for the signature
440 SV*
441 _signature(self, ...)
442 SV *self
443 CODE:
444 23 secp256k1_perl *ctx = ctx_from_sv(self);
445 36 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 15 8 23
if (items > 1 && SvOK(ST(1))) { 14 1 15
446 14 SV *new_signature = ST(1);
447 14 ▶ if (SvROK(new_signature)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(new_signature)) { 0 14 14
448 0 croak("signature must not be a reference");
449 }
450  
451 size_t signature_size;
452 14 unsigned char *signature = bytestr_from_sv(new_signature, &signature_size);
453  
454 14 secp256k1_ecdsa_signature *result_signature = malloc(sizeof *result_signature);
455 14 ▶ if (!result_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_signature) { 0 14 14
456 0 croak("failed to allocate signature memory");
457 }
458  
459 14 int result = secp256k1_ecdsa_signature_parse_der(
460 14 ctx->ctx,
461 result_signature,
462 signature,
463 signature_size
464 );
465  
466 14 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 1 13 14
467 1 free(result_signature);
468 1 croak("the input does not appear to be a valid signature");
469 }
470  
471 13 secp256k1_perl_replace_signature(ctx, result_signature);
472 }
473 9 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 1 8 9
474 1 secp256k1_perl_replace_signature(ctx, NULL);
475 }
476  
477 22 ▶ if (ctx->signature != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->signature != NULL) { 19 3 22
478 unsigned char signature_output[72];
479 19 size_t signature_size = 72;
480 19 secp256k1_ecdsa_signature_serialize_der(
481 19 ctx->ctx,
482 signature_output,
483 &signature_size,
484 19 ctx->signature
485 );
486  
487 19 RETVAL = newSVpv((char*) signature_output, signature_size);
488 }
489 else {
490 3 RETVAL = &PL_sv_undef;
491 }
492 OUTPUT:
493 RETVAL
494  
495 # Getter / setter for the schnorr signature
496 SV*
497 _signature_schnorr(self, ...)
498 SV *self
499 CODE:
500 16 secp256k1_perl *ctx = ctx_from_sv(self);
501 24 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 9 7 16
if (items > 1 && SvOK(ST(1))) { 8 1 9
502 8 SV *new_signature = ST(1);
503 8 ▶ if (SvROK(new_signature)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(new_signature)) { 0 8 8
504 0 croak("signature must not be a reference");
505 }
506  
507 8 unsigned char *signature = size_bytestr_from_sv(new_signature, SCHNORR_SIGNATURE_SIZE, "Schnorr signature");
508  
509 8 unsigned char *result_signature = malloc(sizeof *result_signature * SCHNORR_SIGNATURE_SIZE);
510 8 ▶ if (!result_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_signature) { 0 8 8
511 0 croak("failed to allocate signature memory");
512 }
513  
514 8 memcpy(result_signature, signature, sizeof *result_signature * SCHNORR_SIGNATURE_SIZE);
515 8 secp256k1_perl_replace_schnorr_signature(ctx, result_signature);
516 }
517 8 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 1 7 8
518 1 secp256k1_perl_replace_schnorr_signature(ctx, NULL);
519 }
520  
521 16 ▶ if (ctx->schnorr_signature != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->schnorr_signature != NULL) { 13 3 16
522 13 RETVAL = newSVpv((char*) ctx->schnorr_signature, SCHNORR_SIGNATURE_SIZE);
523 }
524 else {
525 3 RETVAL = &PL_sv_undef;
526 }
527 OUTPUT:
528 RETVAL
529  
530 # Getter / setter for the recoverable signature
531 SV*
532 _signature_recoverable(self, ...)
533 SV *self
534 CODE:
535 21 secp256k1_perl *ctx = ctx_from_sv(self);
536 32 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 13 8 21
if (items > 1 && SvOK(ST(1))) { 13 0 13
537 13 SV *new_signature = ST(1);
538  
539 /* Expect hash ref with signature and recovery_id */
540 13 ▶ if (!SvROK(new_signature) || SvTYPE(SvRV(new_signature)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(new_signature) || SvTYPE(SvRV(new_signature)) != SVt_PVHV) { 13 0 13
if (!SvROK(new_signature) || SvTYPE(SvRV(new_signature)) != SVt_PVHV) { 0 13 13
541 0 croak("recoverable signature must be a hash reference with 'signature' and 'recovery_id' keys");
542 }
543  
544 13 HV *sig_hash = (HV*) SvRV(new_signature);
545  
546 13 SV **sig_sv = hv_fetchs(sig_hash, "signature", 0);
547 13 SV **recovery_id_sv = hv_fetchs(sig_hash, "recovery_id", 0);
548  
549 13 ▶ if (!sig_sv || !recovery_id_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sig_sv || !recovery_id_sv) { 13 0 13
if (!sig_sv || !recovery_id_sv) { 0 13 13
550 0 croak("recoverable signature must contain 'signature' and 'recovery_id' keys");
551 }
552  
553 13 unsigned char *sig_data = size_bytestr_from_sv(*sig_sv, RECOVERABLE_SIGNATURE_SIZE, "signature data");
554  
555 13 int recovery_id = -1;
556  
557 /* validate the recovery_id to be an integer */
558 13 ▶ if (SvOK(*recovery_id_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(*recovery_id_sv)) { 12 1 13
559 STRLEN recovery_id_len;
560 12 const char *recovery_id_str = SvPV(*recovery_id_sv, recovery_id_len);
561 12 int recovery_id_flags = grok_number(recovery_id_str, recovery_id_len, NULL);
562 12 ▶ if (recovery_id_flags && !(recovery_id_flags & IS_NUMBER_NOT_INT)) {
Branch: 2 branches
BranchTrueFalseTotal
if (recovery_id_flags && !(recovery_id_flags & IS_NUMBER_NOT_INT)) { 11 1 12
if (recovery_id_flags && !(recovery_id_flags & IS_NUMBER_NOT_INT)) { 11 0 11
563 11 recovery_id = SvIV(*recovery_id_sv);
564 }
565 }
566  
567 13 ▶ if (recovery_id < 0 || recovery_id > 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (recovery_id < 0 || recovery_id > 3) { 11 2 13
if (recovery_id < 0 || recovery_id > 3) { 0 11 11
568 2 croak("recovery_id must be 0, 1, 2, or 3");
569 }
570  
571 11 secp256k1_ecdsa_recoverable_signature *parsed_signature = malloc(sizeof *parsed_signature);
572 11 ▶ if (!parsed_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!parsed_signature) { 0 11 11
573 0 croak("failed to allocate signature memory");
574 }
575  
576 11 int result = secp256k1_ecdsa_recoverable_signature_parse_compact(
577 11 ctx->ctx,
578 parsed_signature,
579 sig_data,
580 recovery_id
581 );
582  
583 11 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 11 11
584 0 free(parsed_signature);
585 0 croak("failed to parse compact recoverable signature");
586 }
587  
588 11 secp256k1_perl_replace_recoverable_signature(ctx, parsed_signature);
589 }
590 8 ▶ else if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items > 1) { 0 8 8
591 0 secp256k1_perl_replace_recoverable_signature(ctx, NULL);
592 }
593  
594 19 ▶ if (ctx->recoverable_signature != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->recoverable_signature != NULL) { 19 0 19
595 unsigned char signature_data[RECOVERABLE_SIGNATURE_SIZE];
596 int recovery_id;
597  
598 19 int result = secp256k1_ecdsa_recoverable_signature_serialize_compact(
599 19 ctx->ctx,
600 signature_data,
601 &recovery_id,
602 19 ctx->recoverable_signature
603 );
604  
605 19 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 19 19
606 0 croak("failed to serialize recoverable signature");
607 }
608  
609 19 HV *return_hash = newHV();
610 19 hv_stores(return_hash, "signature", newSVpvn((char*) signature_data, RECOVERABLE_SIGNATURE_SIZE));
611 19 hv_stores(return_hash, "recovery_id", newSViv(recovery_id));
612 19 RETVAL = newRV_noinc((SV*) return_hash);
613 }
614 else {
615 0 RETVAL = &PL_sv_undef;
616 }
617 OUTPUT:
618 RETVAL
619  
620 void
621 _push_pubkey(self, ...)
622 SV *self
623 CODE:
624 9 secp256k1_perl *ctx = ctx_from_sv(self);
625  
626 17 ▶ if (items > 1 && SvOK(ST(1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 1 && SvOK(ST(1))) { 9 0 9
if (items > 1 && SvOK(ST(1))) { 9 0 9
627 9 SV *pubkey_data = ST(1);
628 9 ▶ if (!secp256k1_perl_multifield_push(ctx->pubkeys, pubkey_from_sv(ctx, pubkey_data))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_multifield_push(ctx->pubkeys, pubkey_from_sv(ctx, pubkey_data))) { 0 8 8
629 0 croak("failed to push public key");
630 }
631 }
632 else {
633 0 croak("missing public key to push");
634 }
635  
636 # Creates a public key from a private key
637 void
638 _create_pubkey(self, privkey)
639 SV *self
640 SV *privkey
641 CODE:
642 7 secp256k1_perl *ctx = ctx_from_sv(self);
643 6 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private key");
644  
645 3 secp256k1_pubkey *result_pubkey = malloc(sizeof *result_pubkey);
646 3 ▶ if (!result_pubkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_pubkey) { 0 3 3
647 0 croak("failed to allocate pubkey memory");
648 }
649  
650 3 int result = secp256k1_ec_pubkey_create(
651 3 ctx->ctx,
652 result_pubkey,
653 seckey_str
654 );
655  
656 3 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 3 3
657 0 free(result_pubkey);
658 0 croak("creating pubkey failed (invalid private key?)");
659 }
660  
661 3 ▶ if (!secp256k1_perl_replace_pubkey(ctx, result_pubkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_replace_pubkey(ctx, result_pubkey)) { 0 3 3
662 0 croak("could not replace a public key");
663 }
664  
665 # Convert pubkey to xonly pubkey
666 void
667 _convert_pubkey_xonly(self)
668 SV *self
669 CODE:
670 1 secp256k1_perl *ctx = ctx_from_sv(self);
671  
672 1 secp256k1_pubkey *first_key = secp256k1_perl_first_pubkey(ctx);
673 1 secp256k1_xonly_pubkey *xonly_pubkey = malloc(sizeof *xonly_pubkey);
674 1 ▶ if (!xonly_pubkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!xonly_pubkey) { 0 1 1
675 0 croak("failed to allocate pubkey memory");
676 }
677  
678 1 int result = secp256k1_xonly_pubkey_from_pubkey(
679 1 ctx->ctx,
680 xonly_pubkey,
681 NULL,
682 first_key
683 );
684  
685 1 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 1 1
686 0 free(xonly_pubkey);
687 0 croak("converting pubkey failed");
688 }
689  
690 1 secp256k1_perl_replace_xonly_pubkey(ctx, xonly_pubkey);
691  
692 # Normalizes a signature. Returns false value if signature was already normalized
693 SV*
694 _normalize(self)
695 SV *self
696 CODE:
697 12 secp256k1_perl *ctx = ctx_from_sv(self);
698 12 ▶ if (ctx->signature == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->signature == NULL) { 0 12 12
699 0 croak("normalization requires a signature");
700 }
701  
702 12 secp256k1_ecdsa_signature *result_signature = malloc(sizeof *result_signature);
703 12 ▶ if (!result_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_signature) { 0 12 12
704 0 croak("failed to allocate signature memory");
705 }
706  
707 12 int result = secp256k1_ecdsa_signature_normalize(
708 12 ctx->ctx,
709 result_signature,
710 12 ctx->signature
711 );
712  
713 12 secp256k1_perl_replace_signature(ctx, result_signature);
714 12 ▶ RETVAL = result ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result ? &PL_sv_yes : &PL_sv_no; 4 8 12
715 OUTPUT:
716 RETVAL
717  
718 # Verifies a signature
719 SV*
720 _verify(self, message)
721 SV *self
722 SV *message
723 CODE:
724 8 secp256k1_perl *ctx = ctx_from_sv(self);
725 8 ▶ if (ctx->signature == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->signature == NULL) { 0 8 8
726 0 croak("verification requires a signature");
727 }
728  
729 8 unsigned char *message_str = size_bytestr_from_sv(message, CURVE_SIZE, "digest");
730  
731 7 int result = secp256k1_ecdsa_verify(
732 7 ctx->ctx,
733 7 ctx->signature,
734 message_str,
735 7 secp256k1_perl_first_pubkey(ctx)
736 );
737  
738 7 ▶ RETVAL = result ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result ? &PL_sv_yes : &PL_sv_no; 5 2 7
739 OUTPUT:
740 RETVAL
741  
742 # Verifies a Schnorr signature
743 SV*
744 _verify_schnorr(self, message)
745 SV *self
746 SV *message
747 CODE:
748 6 secp256k1_perl *ctx = ctx_from_sv(self);
749 6 ▶ if (ctx->xonly_pubkey == NULL || ctx->schnorr_signature == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->xonly_pubkey == NULL || ctx->schnorr_signature == NULL) { 6 0 6
if (ctx->xonly_pubkey == NULL || ctx->schnorr_signature == NULL) { 0 6 6
750 0 croak("verification requires both xonly pubkey and schnorr signature");
751 }
752  
753 6 unsigned char *message_str = size_bytestr_from_sv(message, CURVE_SIZE, "digest");
754  
755 6 int result = secp256k1_schnorrsig_verify(
756 6 ctx->ctx,
757 6 ctx->schnorr_signature,
758 message_str,
759 CURVE_SIZE,
760 6 ctx->xonly_pubkey
761 );
762  
763 6 ▶ RETVAL = result ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result ? &PL_sv_yes : &PL_sv_no; 4 2 6
764 OUTPUT:
765 RETVAL
766  
767 # Signs a digest
768 void
769 _sign(self, privkey, message)
770 SV* self
771 SV* privkey
772 SV* message
773 CODE:
774 2 secp256k1_perl *ctx = ctx_from_sv(self);
775  
776 2 unsigned char *message_str = size_bytestr_from_sv(message, CURVE_SIZE, "digest");
777 2 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private key");
778  
779 2 secp256k1_ecdsa_signature *result_signature = malloc(sizeof *result_signature);
780 2 ▶ if (!result_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_signature) { 0 2 2
781 0 croak("failed to allocate signature memory");
782 }
783  
784 2 int result = secp256k1_ecdsa_sign(
785 2 ctx->ctx,
786 result_signature,
787 message_str,
788 seckey_str,
789 NULL,
790 NULL
791 );
792  
793 2 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 2 2
794 0 free(result_signature);
795 0 croak("signing failed (nonce generation problem?)");
796 }
797  
798 2 secp256k1_perl_replace_signature(ctx, result_signature);
799  
800 # Signs a digest with Schnorr
801 void
802 _sign_schnorr(self, privkey, message)
803 SV* self
804 SV* privkey
805 SV* message
806 CODE:
807 4 secp256k1_perl *ctx = ctx_from_sv(self);
808  
809 /* Calling perl function to get auxiliary randomness */
810 4 dSP;
811 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
812  
813 4 SV *tmp = newSViv(CURVE_SIZE);
814  
815 4 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 4 4
816 4 PUSHs(tmp);
817 4 PUTBACK;
818  
819 4 size_t count = call_pv("Bitcoin::Secp256k1::_schnorr_aux_random", G_SCALAR);
820 4 SvREFCNT_dec(tmp);
821  
822 4 SPAGAIN;
823  
824 4 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 4 4
825 0 croak("fetching randomness went wrong in Bitcoin::Secp256k1::_sign_schnorr");
826 }
827  
828 4 tmp = POPs;
829 4 PUTBACK;
830  
831 8 unsigned char *randomize = SvOK(tmp)
832 3 ? size_bytestr_from_sv(tmp, CURVE_SIZE, "auxiliary randomness")
833 4 ▶ : NULL
Branch: 1 branch
BranchTrueFalseTotal
: NULL 3 1 4
834 ;
835  
836 4 unsigned char *message_str = size_bytestr_from_sv(message, CURVE_SIZE, "digest");
837 4 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private key");
838  
839 secp256k1_keypair keypair;
840 4 int result = secp256k1_keypair_create(
841 4 ctx->ctx,
842 &keypair,
843 seckey_str
844 );
845  
846 4 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 4 4
847 0 croak("creating a keypair failed");
848 }
849  
850 4 unsigned char *result_signature = malloc(sizeof *result_signature * SCHNORR_SIGNATURE_SIZE);
851 4 ▶ if (!result_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_signature) { 0 4 4
852 0 croak("failed to allocate signature memory");
853 }
854  
855 4 result = secp256k1_schnorrsig_sign32(
856 4 ctx->ctx,
857 result_signature,
858 message_str,
859 &keypair,
860 randomize
861 );
862  
863 /* keypair is guaranteed by docs to contain 96 bytes */
864 4 clean_secret(keypair.data, 96);
865  
866 4 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 4 4
867 0 free(result_signature);
868 0 croak("Schnorr signing failed");
869 }
870  
871 4 secp256k1_perl_replace_schnorr_signature(ctx, result_signature);
872  
873 # Signs a recoverable digest
874 void
875 _sign_recoverable(self, privkey, message)
876 SV* self
877 SV* privkey
878 SV* message
879 CODE:
880 8 secp256k1_perl *ctx = ctx_from_sv(self);
881  
882 8 unsigned char *message_str = size_bytestr_from_sv(message, CURVE_SIZE, "digest");
883 8 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private key");
884  
885 8 secp256k1_ecdsa_recoverable_signature *signature = malloc(sizeof *signature);
886 8 ▶ if (!signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (!signature) { 0 8 8
887 0 croak("failed to allocate signature memory");
888 }
889  
890 8 int result = secp256k1_ecdsa_sign_recoverable(
891 8 ctx->ctx,
892 signature,
893 message_str,
894 seckey_str,
895 NULL,
896 NULL
897 );
898  
899 8 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 8 8
900 0 free(signature);
901 0 croak("signing failed (nonce generation problem?)");
902 }
903  
904 8 secp256k1_perl_replace_recoverable_signature(ctx, signature);
905  
906 # Recover an ECDSA public key from a recoverable signature and message hash
907 void
908 _recover_pubkey_recoverable(self, message_hash)
909 SV* self
910 SV* message_hash
911 CODE:
912 11 secp256k1_perl *ctx = ctx_from_sv(self);
913  
914 11 ▶ if (ctx->recoverable_signature == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->recoverable_signature == NULL) { 0 11 11
915 0 croak("no recoverable signature available - sign first or set via _signature_recoverable");
916 }
917  
918 11 unsigned char *message_str = size_bytestr_from_sv(message_hash, CURVE_SIZE, "message hash");
919  
920 11 secp256k1_pubkey *result_pubkey = malloc(sizeof *result_pubkey);
921 11 ▶ if (!result_pubkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_pubkey) { 0 11 11
922 0 croak("failed to allocate pubkey memory");
923 }
924  
925 11 int result = secp256k1_ecdsa_recover(
926 11 ctx->ctx,
927 result_pubkey,
928 11 ctx->recoverable_signature,
929 message_str
930 );
931  
932 11 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 11 11
933 0 free(result_pubkey);
934 0 croak("failed to recover public key from signature");
935 }
936  
937 11 ▶ if (!secp256k1_perl_replace_pubkey(ctx, result_pubkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_replace_pubkey(ctx, result_pubkey)) { 0 11 11
938 0 croak("could not replace a public key");
939 }
940  
941 # Checks whether a private key is valid
942 SV*
943 _verify_privkey(self, privkey)
944 SV* self
945 SV* privkey
946 CODE:
947 3 secp256k1_perl *ctx = ctx_from_sv(self);
948 STRLEN seckey_size;
949 3 unsigned char *seckey_str = bytestr_from_sv(privkey, &seckey_size);
950  
951 3 int result = (
952 3 seckey_size == CURVE_SIZE
953 3 ▶ && secp256k1_ec_seckey_verify(ctx->ctx, seckey_str)
Branch: 2 branches
BranchTrueFalseTotal
&& secp256k1_ec_seckey_verify(ctx->ctx, seckey_str) 2 1 3
&& secp256k1_ec_seckey_verify(ctx->ctx, seckey_str) 1 1 2
954 );
955  
956 3 ▶ RETVAL = result ? &PL_sv_yes : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = result ? &PL_sv_yes : &PL_sv_no; 1 2 3
957 OUTPUT:
958 RETVAL
959  
960 # Negates a private key
961 SV*
962 _privkey_negate(self, privkey)
963 SV *self
964 SV *privkey
965 CODE:
966 2 secp256k1_perl *ctx = ctx_from_sv(self);
967 2 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private key");
968  
969 unsigned char new_seckey[CURVE_SIZE];
970 2 copy_bytestr(new_seckey, seckey_str, CURVE_SIZE);
971  
972 2 int result = secp256k1_ec_seckey_negate(
973 2 ctx->ctx,
974 new_seckey
975 );
976  
977 2 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 2 2
978 0 clean_secret(new_seckey, CURVE_SIZE);
979 0 croak("resulting negated privkey is not valid");
980 }
981  
982 2 RETVAL = newSVpv((char*) new_seckey, CURVE_SIZE);
983 2 clean_secret(new_seckey, CURVE_SIZE);
984 OUTPUT:
985 RETVAL
986  
987 # Negates a public key
988 void
989 _pubkey_negate(self)
990 SV *self
991 CODE:
992 1 secp256k1_perl *ctx = ctx_from_sv(self);
993  
994 1 int result = secp256k1_ec_pubkey_negate(
995 1 ctx->ctx,
996 secp256k1_perl_first_pubkey(ctx)
997 );
998  
999 /* NOTE: result is always 1 */
1000 1 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 1 1
1001 0 croak("something went wrong while negating a pubkey");
1002 }
1003  
1004 # Adds a tweak to private key
1005 SV*
1006 _privkey_add(self, privkey, tweak)
1007 SV *self
1008 SV *privkey
1009 SV *tweak
1010 CODE:
1011 2 secp256k1_perl *ctx = ctx_from_sv(self);
1012 2 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private key");
1013 2 unsigned char *tweak_str = size_bytestr_from_sv(tweak, CURVE_SIZE, "tweak");
1014  
1015 unsigned char new_seckey[CURVE_SIZE];
1016 2 copy_bytestr(new_seckey, seckey_str, CURVE_SIZE);
1017  
1018 2 int result = secp256k1_ec_seckey_tweak_add(
1019 2 ctx->ctx,
1020 new_seckey,
1021 tweak_str
1022 );
1023  
1024 2 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 1 1 2
1025 1 clean_secret(new_seckey, CURVE_SIZE);
1026 1 croak("resulting added privkey is not valid");
1027 }
1028  
1029 1 RETVAL = newSVpv((char*) new_seckey, CURVE_SIZE);
1030 1 clean_secret(new_seckey, CURVE_SIZE);
1031 OUTPUT:
1032 RETVAL
1033  
1034 # Adds a tweak to public key
1035 void
1036 _pubkey_add(self, tweak)
1037 SV *self
1038 SV *tweak
1039 CODE:
1040 1 secp256k1_perl *ctx = ctx_from_sv(self);
1041 1 unsigned char *tweak_str = size_bytestr_from_sv(tweak, CURVE_SIZE, "tweak");
1042  
1043 1 int result = secp256k1_ec_pubkey_tweak_add(
1044 1 ctx->ctx,
1045 secp256k1_perl_first_pubkey(ctx),
1046 tweak_str
1047 );
1048  
1049 1 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 1 1
1050 0 croak("resulting added pubkey is not valid");
1051 }
1052  
1053 # Multiplies private key by a tweak
1054 SV*
1055 _privkey_mul(self, privkey, tweak)
1056 SV *self
1057 SV *privkey
1058 SV *tweak
1059 CODE:
1060 1 secp256k1_perl *ctx = ctx_from_sv(self);
1061 1 unsigned char *seckey_str = size_bytestr_from_sv(privkey, CURVE_SIZE, "private_key");
1062 1 unsigned char *tweak_str = size_bytestr_from_sv(tweak, CURVE_SIZE, "tweak");
1063  
1064 unsigned char new_seckey[CURVE_SIZE];
1065 1 copy_bytestr(new_seckey, seckey_str, CURVE_SIZE);
1066  
1067 1 int result = secp256k1_ec_seckey_tweak_mul(
1068 1 ctx->ctx,
1069 new_seckey,
1070 tweak_str
1071 );
1072  
1073 1 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 1 1
1074 0 clean_secret(new_seckey, CURVE_SIZE);
1075 0 croak("multiplication arguments are not valid");
1076 }
1077  
1078 1 RETVAL = newSVpv((char*) new_seckey, CURVE_SIZE);
1079 1 clean_secret(new_seckey, CURVE_SIZE);
1080 OUTPUT:
1081 RETVAL
1082  
1083 # Multiplies public key by a tweak
1084 void
1085 _pubkey_mul(self, tweak)
1086 SV *self
1087 SV *tweak
1088 CODE:
1089 2 secp256k1_perl *ctx = ctx_from_sv(self);
1090 2 unsigned char *tweak_str = size_bytestr_from_sv(tweak, CURVE_SIZE, "tweak");
1091  
1092 2 int result = secp256k1_ec_pubkey_tweak_mul(
1093 2 ctx->ctx,
1094 secp256k1_perl_first_pubkey(ctx),
1095 tweak_str
1096 );
1097  
1098 2 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 1 1 2
1099 1 croak("multiplication arguments are not valid");
1100 }
1101  
1102 # Combines public keys together
1103 void
1104 _pubkey_combine(self)
1105 SV *self
1106 CODE:
1107 3 secp256k1_perl *ctx = ctx_from_sv(self);
1108  
1109 3 ▶ if (ctx->pubkeys->item_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->pubkeys->item_count == 0) { 0 3 3
1110 0 croak("there are no public keys to combine");
1111 }
1112  
1113 3 secp256k1_pubkey *result_pubkey = malloc(sizeof *result_pubkey);
1114 3 ▶ if (!result_pubkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result_pubkey) { 0 3 3
1115 0 croak("failed to allocate pubkey memory");
1116 }
1117  
1118 3 int result = secp256k1_ec_pubkey_combine(
1119 3 ctx->ctx,
1120 result_pubkey,
1121 3 (const secp256k1_pubkey * const *) ctx->pubkeys->items,
1122 3 ctx->pubkeys->item_count
1123 );
1124  
1125 3 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 3 3
1126 0 free(result_pubkey);
1127 0 croak("resulting sum of pubkeys is not valid");
1128 }
1129  
1130 3 ▶ if (!secp256k1_perl_replace_pubkey(ctx, result_pubkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!secp256k1_perl_replace_pubkey(ctx, result_pubkey)) { 0 3 3
1131 0 croak("could not replace a public key");
1132 }
1133  
1134  
1135 # Destructor
1136 void
1137 DESTROY(self)
1138 SV *self
1139 CODE:
1140 6 secp256k1_perl_destroy(ctx_from_sv(self));
1141  
1142 # Do a selftest on module load
1143 BOOT:
1144 3 secp256k1_selftest();
1145  
« blib/lib/Bitcoin/Secp256k1.pm ↑ Summary