inc/CryptX_BigInt_LTM.xs.inc

filter: Statementstmt 89.0% 196 / 220 Branchbran 70.9% 95 / 134 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.2% 291 / 354 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« inc/CryptX_ASN1.xs.inc ↑ Summary inc/CryptX_Mac_HMAC.xs.inc »
1 MODULE = CryptX PACKAGE = Math::BigInt::LTM
2  
3 PROTOTYPES: DISABLE
4  
5 ##############################################################################
6 # _new()
7  
8 Math::BigInt::LTM
9 _new(Class, SV *x)
10 PREINIT:
11 mp_err merr;
12 CODE:
13 32764 Newz(0, RETVAL, 1, mp_int);
14 32764 merr = mp_init(RETVAL);
15 #if IVSIZE == 8
16 32764 ▶ if (SvUOK(x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUOK(x)) { 0 32764 32764
17 0 mp_set_u64(RETVAL, (unsigned long long)SvUV(x));
18 }
19 32764 ▶ else if (SvIOK(x)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(x)) { 17381 15383 32764
20 17381 mp_set_i64(RETVAL, (long long)SvIV(x));
21 }
22 #else
23 if (SvUOK(x)) {
24 mp_set_u32(RETVAL, (unsigned int)SvUV(x));
25 }
26 else if (SvIOK(x)) {
27 mp_set_i32(RETVAL, (int)SvIV(x));
28 }
29 #endif
30 else {
31 /* fallback - read the decimal number from string */
32 15383 merr = mp_read_radix(RETVAL, SvPV_nolen(x), 10);
33 }
34 PERL_UNUSED_VAR(merr);
35 OUTPUT:
36 RETVAL
37  
38 ##############################################################################
39 # _from_bin()
40  
41 Math::BigInt::LTM
42 _from_bin(Class, SV *x)
43 PREINIT:
44 mp_err merr;
45 char *str, *start;
46 CODE:
47 64 Newz(0, RETVAL, 1, mp_int);
48 64 merr = mp_init(RETVAL);
49 64 str = SvPV_nolen(x);
50 64 ▶ start = (strlen(str)>2 && str[0] == '0' && str[1] == 'b') ? str+2 : str;
Branch: 3 branches
BranchTrueFalseTotal
start = (strlen(str)>2 && str[0] == '0' && str[1] == 'b') ? str+2 : str; 64 0 64
start = (strlen(str)>2 && str[0] == '0' && str[1] == 'b') ? str+2 : str; 64 0 64
start = (strlen(str)>2 && str[0] == '0' && str[1] == 'b') ? str+2 : str; 64 0 64
51 64 merr = mp_read_radix(RETVAL, start, 2);
52 PERL_UNUSED_VAR(merr);
53 OUTPUT:
54 RETVAL
55  
56 ##############################################################################
57 # _from_hex()
58  
59 Math::BigInt::LTM
60 _from_hex(Class, SV *x)
61 PREINIT:
62 mp_err merr;
63 char *str, *start;
64 CODE:
65 276 Newz(0, RETVAL, 1, mp_int);
66 276 merr = mp_init(RETVAL);
67 276 str = SvPV_nolen(x);
68 276 ▶ start = (strlen(str)>2 && str[0] == '0' && str[1] == 'x') ? str+2 : str;
Branch: 3 branches
BranchTrueFalseTotal
start = (strlen(str)>2 && str[0] == '0' && str[1] == 'x') ? str+2 : str; 276 0 276
start = (strlen(str)>2 && str[0] == '0' && str[1] == 'x') ? str+2 : str; 276 0 276
start = (strlen(str)>2 && str[0] == '0' && str[1] == 'x') ? str+2 : str; 276 0 276
69 276 merr = mp_read_radix(RETVAL, start, 16);
70 PERL_UNUSED_VAR(merr);
71 OUTPUT:
72 RETVAL
73  
74 ##############################################################################
75 # _from_oct()
76  
77 Math::BigInt::LTM
78 _from_oct(Class, SV *x)
79 PREINIT:
80 mp_err merr;
81 CODE:
82 6 Newz(0, RETVAL, 1, mp_int);
83 6 merr = mp_init(RETVAL);
84 6 merr = mp_read_radix(RETVAL, SvPV_nolen(x), 8);
85 PERL_UNUSED_VAR(merr);
86 OUTPUT:
87 RETVAL
88  
89 ##############################################################################
90 # _from_base()
91  
92 Math::BigInt::LTM
93 _from_base(Class, SV *x, int base)
94 PREINIT:
95 mp_err merr;
96 CODE:
97 0 Newz(0, RETVAL, 1, mp_int);
98 0 merr = mp_init(RETVAL);
99 0 merr = mp_read_radix(RETVAL, SvPV_nolen(x), base);
100 PERL_UNUSED_VAR(merr);
101 OUTPUT:
102 RETVAL
103  
104 ##############################################################################
105 # _from_bytes()
106  
107 Math::BigInt::LTM
108 _from_bytes(Class, SV *x)
109 PREINIT:
110 mp_err merr;
111 STRLEN buf_len;
112 unsigned char *buf_ptr;
113 CODE:
114 0 Newz(0, RETVAL, 1, mp_int);
115 0 merr = mp_init(RETVAL);
116 0 buf_ptr = (unsigned char *)SvPVbyte(x, buf_len);
117 0 merr = mp_from_ubin(RETVAL, buf_ptr, buf_len);
118 PERL_UNUSED_VAR(merr);
119 OUTPUT:
120 RETVAL
121  
122 ##############################################################################
123 # _set() - set an already existing object to the given scalar value
124  
125 void
126 _set(Class, Math::BigInt::LTM n, SV *x)
127 PREINIT:
128 mp_err merr;
129 CODE:
130 #if IVSIZE == 8
131 3 ▶ if (SvUOK(x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUOK(x)) { 0 3 3
132 0 mp_set_u64(n, (unsigned long long)SvUV(x));
133 }
134 3 ▶ else if (SvIOK(x)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(x)) { 3 0 3
135 3 mp_set_i64(n, (long long)SvIV(x));
136 }
137 #else
138 if (SvUOK(x)) {
139 mp_set_u32(n, (unsigned int)SvUV(x));
140 }
141 else if (SvIOK(x)) {
142 mp_set_i32(n, (int)SvIV(x));
143 }
144 #endif
145 else {
146 /* fallback - read the decimal number from string */
147 0 merr = mp_read_radix(n, SvPV_nolen(x), 10);
148 PERL_UNUSED_VAR(merr);
149 }
150  
151 ##############################################################################
152 # _zero()
153  
154 Math::BigInt::LTM
155 _zero(Class)
156 PREINIT:
157 mp_err merr;
158 CODE:
159 7748 Newz(0, RETVAL, 1, mp_int);
160 7748 merr = mp_init(RETVAL);
161 7748 mp_zero(RETVAL);
162 PERL_UNUSED_VAR(merr);
163 OUTPUT:
164 RETVAL
165  
166 ##############################################################################
167 # _one()
168  
169 Math::BigInt::LTM
170 _one(Class)
171 PREINIT:
172 mp_err merr;
173 CODE:
174 342 Newz(0, RETVAL, 1, mp_int);
175 342 merr = mp_init(RETVAL);
176 342 mp_set_u32(RETVAL, 1);
177 PERL_UNUSED_VAR(merr);
178 OUTPUT:
179 RETVAL
180  
181 ##############################################################################
182 # _two()
183  
184 Math::BigInt::LTM
185 _two(Class)
186 PREINIT:
187 mp_err merr;
188 CODE:
189 142 Newz(0, RETVAL, 1, mp_int);
190 142 merr = mp_init(RETVAL);
191 142 mp_set_u32(RETVAL, 2);
192 PERL_UNUSED_VAR(merr);
193 OUTPUT:
194 RETVAL
195  
196 ##############################################################################
197 # _ten()
198  
199 Math::BigInt::LTM
200 _ten(Class)
201 PREINIT:
202 mp_err merr;
203 CODE:
204 4 Newz(0, RETVAL, 1, mp_int);
205 4 merr = mp_init(RETVAL);
206 4 mp_set_u32(RETVAL, 10);
207 PERL_UNUSED_VAR(merr);
208 OUTPUT:
209 RETVAL
210  
211 ##############################################################################
212 # _1ex()
213  
214 Math::BigInt::LTM
215 _1ex(Class, int x)
216 PREINIT:
217 mp_err merr;
218 CODE:
219 5 Newz(0, RETVAL, 1, mp_int);
220 5 merr = mp_init(RETVAL);
221 5 mp_set_u32(RETVAL, 10);
222 5 merr = mp_expt_n(RETVAL, x, RETVAL);
223 PERL_UNUSED_VAR(merr);
224 OUTPUT:
225 RETVAL
226  
227 ##############################################################################
228 # DESTROY() - free memory of a GMP number
229  
230 void
231 DESTROY(Math::BigInt::LTM n)
232 PPCODE:
233 65124 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 65124 0 65124
234 65124 mp_clear(n);
235 65124 Safefree(n);
236 }
237  
238 ##############################################################################
239 # _str() - return string so that atof() and atoi() can use it
240  
241 SV *
242 _str(Class, Math::BigInt::LTM n)
243 PREINIT:
244 mp_err merr;
245 int len;
246 char *buf;
247 CODE:
248 18328 ▶ if (mp_iszero(n) == MP_YES) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_iszero(n) == MP_YES) { 1352 16976 18328
249 1352 RETVAL = newSVpv("0", 0);
250 }
251 else {
252 16976 len = mp_count_bits(n) / 3 + 3; /* decimal_size ~ (binary_size/3 + 1) +1 for sign +1 for NUL-byte */
253 16976 Newz(0, buf, len, char);
254 16976 merr = mp_to_radix(n, buf, len, NULL, 10);
255 16976 RETVAL = newSVpv(buf, 0);
256 16976 Safefree(buf);
257 }
258 PERL_UNUSED_VAR(merr);
259 OUTPUT:
260 RETVAL
261  
262 ##############################################################################
263 # _len() - return the length of the number in base 10 (costly)
264  
265 int
266 _len(Class, Math::BigInt::LTM n)
267 PREINIT:
268 mp_err merr;
269 int len;
270 char *buf;
271 CODE:
272 31826 ▶ if (mp_iszero(n) == MP_YES) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_iszero(n) == MP_YES) { 9 31817 31826
273 9 RETVAL = 1;
274 }
275 else {
276 31817 len = mp_count_bits(n) / 3 + 3; /* decimal_size ~ (binary_size/3 + 1) +1 for sign +1 for NUL-byte */
277 31817 Newz(0, buf, len, char);
278 31817 merr = mp_to_radix(n, buf, len, NULL, 10);
279 31817 RETVAL = (int)strlen(buf);
280 31817 Safefree(buf);
281 }
282 PERL_UNUSED_VAR(merr);
283 OUTPUT:
284 RETVAL
285  
286 ##############################################################################
287 # _alen() - return the approx. length of the number in base 10 (fast)
288 # _alen() might underestimate, but never overestimate the true value
289  
290 int
291 _alen(Class, Math::BigInt::LTM n)
292 PREINIT:
293 int bits;
294 CODE:
295 36 bits = mp_count_bits(n);
296 /* alen = round(bits * log(2) / log(10)) */
297 36 ▶ RETVAL = (bits < 5) ? 1 : (int)(bits * 0.301029995663 + 0.499999999999);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (bits < 5) ? 1 : (int)(bits * 0.301029995663 + 0.499999999999); 28 8 36
RETVAL = (bits < 5) ? 1 : (int)(bits * 0.301029995663 + 0.499999999999); 35 1 36
298 /* less accurate approximation, but without floating-point calculations
299 RETVAL = (bits < 5) ? 1 : bits / 4 + bits / 32 + bits / 64 + bits / 256;
300 RETVAL = (bits < 5) ? 1 : bits / 4;
301 */
302 OUTPUT:
303 RETVAL
304  
305 ##############################################################################
306 # _zeros() - return number of trailing zeros (in decimal form)
307  
308 int
309 _zeros(Class, Math::BigInt::LTM n)
310 PREINIT:
311 mp_err merr;
312 int len;
313 char *buf;
314 CODE:
315 23287 ▶ if (mp_iszero(n) == MP_YES) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_iszero(n) == MP_YES) { 450 22837 23287
316 450 RETVAL = 0; /* '0' has no trailing zeros! */
317 }
318 else {
319 22837 len = mp_count_bits(n) / 3 + 3; /* decimal_size ~ (binary_size/3 + 1) +1 for sign +1 for NUL-byte */
320 22837 Newz(0, buf, len, char);
321 22837 merr = mp_to_radix(n, buf, len, NULL, 10);
322 22837 len = (int)strlen(buf);
323 22837 RETVAL = 0;
324 242329 ▶ while (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len > 0) { 242329 0 242329
325 242329 ▶ if (buf[len-1] != '0') break;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[len-1] != '0') break; 22837 219492 242329
326 219492 RETVAL++;
327 219492 len--;
328 }
329 22837 Safefree(buf);
330 }
331 PERL_UNUSED_VAR(merr);
332 OUTPUT:
333 RETVAL
334  
335 ##############################################################################
336 # _to_hex() - return ref to hexadecimal string (no prefix)
337  
338 SV *
339 _to_hex(Class, Math::BigInt::LTM n)
340 PREINIT:
341 mp_err merr;
342 size_t i, len;
343 char *buf;
344 CODE:
345 72 ▶ len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 2 + 1; /* incl. NUL-byte */
Branch: 1 branch
BranchTrueFalseTotal
len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 2 + 1; /* incl. NUL-byte */ 59 13 72
346 72 RETVAL = newSV(len);
347 72 SvPOK_on(RETVAL);
348 72 buf = SvPVX(RETVAL);
349 72 merr = mp_to_radix(n, buf, len, NULL, 16); /* to hex */
350 206 ▶ for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]);
Branch: 3 branches
BranchTrueFalseTotal
for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]); 15 119 134
for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]); 206 0 206
for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]); 134 72 206
351 72 SvCUR_set(RETVAL, strlen(buf));
352 PERL_UNUSED_VAR(merr);
353 OUTPUT:
354 RETVAL
355  
356 ##############################################################################
357 # _to_bin() - return ref to binary string (no prefix)
358  
359 SV *
360 _to_bin(Class, Math::BigInt::LTM n)
361 PREINIT:
362 mp_err merr;
363 size_t len;
364 char *buf;
365 CODE:
366 31 ▶ len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 8 + 1; /* incl. NUL-byte */
Branch: 1 branch
BranchTrueFalseTotal
len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 8 + 1; /* incl. NUL-byte */ 27 4 31
367 31 RETVAL = newSV(len);
368 31 SvPOK_on(RETVAL);
369 31 buf = SvPVX(RETVAL);
370 31 merr = mp_to_radix(n, buf, len, NULL, 2); /* to binary */
371 31 SvCUR_set(RETVAL, strlen(buf));
372 PERL_UNUSED_VAR(merr);
373 OUTPUT:
374 RETVAL
375  
376 ##############################################################################
377 # _to_oct() - return ref to octal string (no prefix)
378  
379 SV *
380 _to_oct(Class, Math::BigInt::LTM n)
381 PREINIT:
382 mp_err merr;
383 size_t len;
384 char *buf;
385 CODE:
386 10 ▶ len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 3 + 1; /* incl. NUL-byte */
Branch: 1 branch
BranchTrueFalseTotal
len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 3 + 1; /* incl. NUL-byte */ 7 3 10
387 10 RETVAL = newSV(len);
388 10 SvPOK_on(RETVAL);
389 10 buf = SvPVX(RETVAL);
390 10 merr = mp_to_radix(n, buf, len, NULL, 8); /* to octal */
391 10 SvCUR_set(RETVAL, strlen(buf));
392 PERL_UNUSED_VAR(merr);
393 OUTPUT:
394 RETVAL
395  
396 ##############################################################################
397 # _to_base() - raw bytes
398  
399 SV *
400 _to_base(Class, Math::BigInt::LTM n, int base)
401 PREINIT:
402 mp_err merr;
403 size_t len;
404 char *buf;
405 CODE:
406 0 ▶ len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 8 + 1; /* the worst case for base == 2 */
Branch: 1 branch
BranchTrueFalseTotal
len = mp_iszero(n) ? 2 : mp_ubin_size(n) * 8 + 1; /* the worst case for base == 2 */ 0 0 0
407 0 RETVAL = newSV(len);
408 0 SvPOK_on(RETVAL);
409 0 buf = SvPVX(RETVAL);
410 0 merr = mp_to_radix(n, buf, len, NULL, base);
411 0 SvCUR_set(RETVAL, strlen(buf));
412 PERL_UNUSED_VAR(merr);
413 OUTPUT:
414 RETVAL
415  
416 ##############################################################################
417 # _to_bytes() - raw bytes
418 # _as_bytes() - raw bytes
419  
420 SV *
421 _to_bytes(Class, Math::BigInt::LTM n)
422 ALIAS:
423 _as_bytes = 1
424 PREINIT:
425 mp_err merr;
426 size_t len;
427 unsigned char *buf;
428 CODE:
429 PERL_UNUSED_VAR(ix);
430 10 len = mp_ubin_size(n);
431 10 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 10 0 10
432 10 RETVAL = newSV(len);
433 10 SvPOK_on(RETVAL);
434 10 buf = (unsigned char*)SvPVX(RETVAL);
435 10 merr = mp_to_ubin(n, buf, len, NULL);
436 10 SvCUR_set(RETVAL, len);
437 10 *SvEND(RETVAL) = '\0';
438 }
439 else {
440 0 RETVAL = newSV(1);
441 0 SvPOK_on(RETVAL);
442 0 buf = (unsigned char*)SvPVX(RETVAL);
443 0 buf[0] = 0;
444 0 SvCUR_set(RETVAL, 1);
445 0 *SvEND(RETVAL) = '\0';
446 }
447 PERL_UNUSED_VAR(merr);
448 OUTPUT:
449 RETVAL
450  
451 ##############################################################################
452 # _modpow() - ($n ** $exp) % $mod
453  
454 Math::BigInt::LTM
455 _modpow(Class, Math::BigInt::LTM n, Math::BigInt::LTM exp, Math::BigInt::LTM mod)
456 PREINIT:
457 mp_err merr;
458 CODE:
459 144 Newz(0, RETVAL, 1, mp_int);
460 144 merr = mp_init(RETVAL);
461 144 ▶ if (mp_cmp_d(mod, 1) == MP_EQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_cmp_d(mod, 1) == MP_EQ) { 50 94 144
462 50 mp_zero(RETVAL);
463 }
464 else {
465 94 merr = mp_exptmod(n, exp, mod, RETVAL);
466 }
467 PERL_UNUSED_VAR(merr);
468 OUTPUT:
469 RETVAL
470  
471 ##############################################################################
472 # _modinv() - compute the inverse of x % y
473  
474 void
475 _modinv(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
476 PREINIT:
477 mp_err merr;
478 int rc;
479 SV* s;
480 mp_int* RETVAL;
481 PPCODE:
482 42 Newz(0, RETVAL, 1, mp_int);
483 42 merr = mp_init(RETVAL);
484 42 rc = mp_invmod(x, y, RETVAL);
485 42 ▶ EXTEND(SP, 2); /* we return two values */
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); /* we return two values */ 0 42 42
486 42 ▶ if (rc != MP_OKAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != MP_OKAY) { 7 35 42
487 /* Inverse doesn't exist. Return both values undefined. */
488 7 PUSHs(&PL_sv_undef);
489 7 PUSHs(&PL_sv_undef);
490 }
491 else {
492 /* Inverse exists. When the modulus to mp_invert() is positive,
493 * the returned value is also positive. */
494 35 PUSHs(sv_2mortal(sv_from_mpi(RETVAL)));
495 35 s = sv_newmortal();
496 35 sv_setpvn(s, "+", 1);
497 35 PUSHs(s);
498 }
499 PERL_UNUSED_VAR(merr);
500  
501 ##############################################################################
502 # _add() - add $y to $x in place
503  
504 void
505 _add(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
506 PREINIT:
507 mp_err merr;
508 PPCODE:
509 15560 merr = mp_add(x, y, x);
510 PERL_UNUSED_VAR(merr);
511 15560 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 15560 15560
512  
513 ##############################################################################
514 # _inc() - modify x inline by doing x++
515  
516 void
517 _inc(Class, Math::BigInt::LTM x)
518 PREINIT:
519 mp_err merr;
520 PPCODE:
521 1427 merr = mp_add_d(x, 1, x);
522 PERL_UNUSED_VAR(merr);
523 1427 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 1427 1427
524  
525 ##############################################################################
526 # _dec() - modify x inline by doing x--
527  
528 void
529 _dec(Class, Math::BigInt::LTM x)
530 PREINIT:
531 mp_err merr;
532 PPCODE:
533 558 merr = mp_sub_d(x, 1, x);
534 PERL_UNUSED_VAR(merr);
535 558 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 558 558
536  
537 ##############################################################################
538 # _sub() - $x - $y
539 # $x is always larger than $y! So overflow/underflow can not happen here.
540  
541 void
542 _sub(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, ...)
543 PREINIT:
544 mp_err merr;
545 PPCODE:
546 15776 ▶ if ( items == 4 && SvTRUE(ST(3)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( items == 4 && SvTRUE(ST(3)) ) { 6364 9412 15776
if ( items == 4 && SvTRUE(ST(3)) ) { 1569 4795 6364
547 /* y -= x */
548 1569 merr = mp_sub(x, y, y);
549 1569 ▶ XPUSHs(ST(2)); /* y */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(2)); /* y */ 0 1569 1569
550 }
551 else {
552 /* x -= y */
553 14207 merr = mp_sub(x, y, x);
554 14207 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 14207 14207
555 }
556 PERL_UNUSED_VAR(merr);
557  
558 ##############################################################################
559 # _rsft()
560  
561 void
562 _rsft(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, unsigned long base_int)
563 PREINIT:
564 mp_err merr;
565 mp_int* BASE;
566 PPCODE:
567 9473 Newz(0, BASE, 1, mp_int);
568 9473 merr = mp_init(BASE);
569 9473 mp_set_ul(BASE, base_int);
570 9473 merr = mp_expt_n(BASE, mp_get_l(y), BASE);
571 9473 merr = mp_div(x, BASE, x, NULL);
572 PERL_UNUSED_VAR(merr);
573 9473 mp_clear(BASE);
574 9473 Safefree(BASE);
575 9473 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 9473 9473
576  
577 ##############################################################################
578 # _lsft()
579  
580 void
581 _lsft(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, unsigned long base_int)
582 PREINIT:
583 mp_err merr;
584 mp_int* BASE;
585 PPCODE:
586 6051 Newz(0, BASE, 1, mp_int);
587 6051 merr = mp_init(BASE);
588 6051 mp_set_ul(BASE, base_int);
589 6051 merr = mp_expt_n(BASE, mp_get_l(y), BASE);
590 6051 merr = mp_mul(x, BASE, x);
591 PERL_UNUSED_VAR(merr);
592 6051 mp_clear(BASE);
593 6051 Safefree(BASE);
594 6051 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 6051 6051
595  
596 ##############################################################################
597 # _mul()
598  
599 void
600 _mul(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
601 PREINIT:
602 mp_err merr;
603 PPCODE:
604 6924 merr = mp_mul(x, y, x);
605 PERL_UNUSED_VAR(merr);
606 6924 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 6924 6924
607  
608 ##############################################################################
609 # _div(): x /= y or (x,rem) = x / y
610  
611 void
612 _div(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
613 PREINIT:
614 mp_err merr;
615 mp_int * rem;
616 PPCODE:
617 2884 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 146 2738 2884
618 146 Newz(0, rem, 1, mp_int);
619 146 merr = mp_init(rem);
620 146 merr = mp_div(x, y, x, rem);
621 146 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 146 146
622 146 PUSHs(ST(1)); /* x */
623 146 PUSHs(sv_2mortal(sv_from_mpi(rem)));
624 }
625 else {
626 2738 merr = mp_div(x, y, x, NULL);
627 2738 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 2738 2738
628 }
629 PERL_UNUSED_VAR(merr);
630  
631 ##############################################################################
632 # _mod() - x %= y
633  
634 void
635 _mod(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
636 PREINIT:
637 mp_err merr;
638 PPCODE:
639 256 merr = mp_mod(x, y, x);
640 PERL_UNUSED_VAR(merr);
641 256 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 256 256
642  
643 ##############################################################################
644 # _acmp() - cmp two numbers
645  
646 int
647 _acmp(Class, Math::BigInt::LTM m, Math::BigInt::LTM n)
648 CODE:
649 20217 RETVAL = mp_cmp(m, n);
650 20217 ▶ if ( RETVAL < 0) RETVAL = -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( RETVAL < 0) RETVAL = -1; 1884 18333 20217
651 20217 ▶ if ( RETVAL > 0) RETVAL = 1;
Branch: 1 branch
BranchTrueFalseTotal
if ( RETVAL > 0) RETVAL = 1; 15648 4569 20217
652 OUTPUT:
653 RETVAL
654  
655 ##############################################################################
656 # _is_zero()
657  
658 int
659 _is_zero(Class, Math::BigInt::LTM x)
660 CODE:
661 74168 ▶ RETVAL = (mp_iszero(x) == MP_YES) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (mp_iszero(x) == MP_YES) ? 1 : 0; 74055 113 74168
662 OUTPUT:
663 RETVAL
664  
665 ##############################################################################
666 # _is_one()
667  
668 int
669 _is_one(Class, Math::BigInt::LTM x)
670 CODE:
671 936 ▶ RETVAL = (mp_cmp_d(x, 1) == MP_EQ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (mp_cmp_d(x, 1) == MP_EQ) ? 1 : 0; 899 37 936
672 OUTPUT:
673 RETVAL
674  
675 ##############################################################################
676 # _is_two()
677  
678 int
679 _is_two(Class, Math::BigInt::LTM x)
680 CODE:
681 45 ▶ RETVAL = (mp_cmp_d(x, 2) == MP_EQ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (mp_cmp_d(x, 2) == MP_EQ) ? 1 : 0; 37 8 45
682 OUTPUT:
683 RETVAL
684  
685 ##############################################################################
686 # _is_ten()
687  
688 int
689 _is_ten(Class, Math::BigInt::LTM x)
690 CODE:
691 2 ▶ RETVAL = (mp_cmp_d(x, 10) == MP_EQ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (mp_cmp_d(x, 10) == MP_EQ) ? 1 : 0; 0 2 2
692 OUTPUT:
693 RETVAL
694  
695 ##############################################################################
696 # _pow() - x **= y
697  
698 void
699 _pow(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
700 PREINIT:
701 mp_err merr;
702 PPCODE:
703 200 merr = mp_expt_n(x, mp_get_l(y), x);
704 PERL_UNUSED_VAR(merr);
705 200 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 200 200
706  
707 ##############################################################################
708 # _gcd() - gcd(m,n)
709  
710 Math::BigInt::LTM
711 _gcd(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
712 PREINIT:
713 mp_err merr;
714 CODE:
715 18 Newz(0, RETVAL, 1, mp_int);
716 18 merr = mp_init(RETVAL);
717 18 merr = mp_gcd(x, y, RETVAL);
718 PERL_UNUSED_VAR(merr);
719 OUTPUT:
720 RETVAL
721  
722 ##############################################################################
723 # _and() - m &= n
724  
725 void
726 _and(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
727 PREINIT:
728 mp_err merr;
729 PPCODE:
730 26 merr = mp_and(x, y, x);
731 PERL_UNUSED_VAR(merr);
732 26 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 26 26
733  
734 ##############################################################################
735 # _xor() - m =^ n
736  
737 void
738 _xor(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
739 PREINIT:
740 mp_err merr;
741 PPCODE:
742 39 merr = mp_xor(x, y, x);
743 PERL_UNUSED_VAR(merr);
744 39 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 39 39
745  
746 ##############################################################################
747 # _or() - m =| n
748  
749 void
750 _or(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
751 PREINIT:
752 mp_err merr;
753 PPCODE:
754 41 merr = mp_or(x, y, x);
755 PERL_UNUSED_VAR(merr);
756 41 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 41 41
757  
758 ##############################################################################
759 # _copy()
760  
761 Math::BigInt::LTM
762 _copy(Class, Math::BigInt::LTM m)
763 PREINIT:
764 mp_err merr;
765 CODE:
766 23429 Newz(0, RETVAL, 1, mp_int);
767 23429 merr = mp_init(RETVAL);
768 23429 merr = mp_copy(m, RETVAL);
769 PERL_UNUSED_VAR(merr);
770 OUTPUT:
771 RETVAL
772  
773 ##############################################################################
774 # _is_odd() - test for number being odd
775  
776 int
777 _is_odd(Class, Math::BigInt::LTM n)
778 CODE:
779 171 ▶ RETVAL = (mp_isodd(n) == MP_YES) ? 1 : 0;
Branch: 3 branches
BranchTrueFalseTotal
RETVAL = (mp_isodd(n) == MP_YES) ? 1 : 0; 146 25 171
RETVAL = (mp_isodd(n) == MP_YES) ? 1 : 0; 74 72 146
RETVAL = (mp_isodd(n) == MP_YES) ? 1 : 0; 163 8 171
780 OUTPUT:
781 RETVAL
782  
783 ##############################################################################
784 # _is_even() - test for number being even
785  
786 int
787 _is_even(Class, Math::BigInt::LTM n)
788 CODE:
789 23 ▶ RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0;
Branch: 5 branches
BranchTrueFalseTotal
RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; 20 3 23
RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; 8 12 20
RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; 12 11 23
RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; 0 12 12
RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; 19 4 23
790 OUTPUT:
791 RETVAL
792  
793 ##############################################################################
794 # _sqrt() - square root
795  
796 void
797 _sqrt(Class, Math::BigInt::LTM x)
798 PREINIT:
799 mp_err merr;
800 PPCODE:
801 119 merr = mp_sqrt(x, x);
802 PERL_UNUSED_VAR(merr);
803 119 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 119 119
804  
805 ##############################################################################
806 # _root() - integer roots
807  
808 void
809 _root(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
810 PREINIT:
811 mp_err merr;
812 PPCODE:
813 50 merr = mp_root_n(x, mp_get_l(y), x);
814 PERL_UNUSED_VAR(merr);
815 50 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 50 50
816  
817 ##############################################################################
818 # _lcm() - least common multiple
819 void
820 _lcm(Class, Math::BigInt::LTM x, Math::BigInt::LTM y)
821 PREINIT:
822 mp_err merr;
823 PPCODE:
824 2 merr = mp_lcm(x, y, x) ;
825 PERL_UNUSED_VAR(merr);
826 2 ▶ XPUSHs(ST(1)); /* x */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(1)); /* x */ 0 2 2
827  
828 ##############################################################################
829 # Storable hooks
830  
831 void
832 STORABLE_thaw(blank_obj, cloning, serialized, ...)
833 SV *blank_obj
834 SV *cloning = NO_INIT
835 SV *serialized
836 PREINIT:
837 mp_err merr;
838 SV *target;
839 mp_int *mpi;
840 PPCODE:
841 PERL_UNUSED_VAR(cloning);
842 1 ▶ if (SvROK(blank_obj) && sv_isa(blank_obj, "Math::BigInt::LTM")) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(blank_obj) && sv_isa(blank_obj, "Math::BigInt::LTM")) { 1 0 1
if (SvROK(blank_obj) && sv_isa(blank_obj, "Math::BigInt::LTM")) { 1 0 1
843 1 Newz(0, mpi, 1, mp_int);
844 1 merr = mp_init(mpi);
845 1 merr = mp_read_radix(mpi, SvPV_nolen(serialized), 10);
846 PERL_UNUSED_VAR(merr);
847 1 target = SvRV(blank_obj);
848 1 SvIV_set(target, PTR2IV(mpi));
849 1 SvIOK_on(target);
850 1 PUSHs(target);
851 1 XSRETURN(1);
852 }
853 else
854 0 croak("Bad object for Math::BigInt::LTM::STORABLE_thaw call");
855  
856 SV *
857 STORABLE_freeze(self, cloning = NULL)
858 Math::BigInt::LTM self
859 SV *cloning = NO_INIT
860 PREINIT:
861 mp_err merr;
862 unsigned long len;
863 char *buf;
864 CODE:
865 PERL_UNUSED_VAR(cloning);
866 1 ▶ if (mp_iszero(self) == MP_YES) {
Branch: 1 branch
BranchTrueFalseTotal
if (mp_iszero(self) == MP_YES) { 0 1 1
867 0 RETVAL = newSVpv("0", 0);
868 }
869 else {
870 1 len = mp_count_bits(self) / 3 + 3; /* decimal_size ~ (binary_size/3 + 1) +1 for sign +1 for NUL-byte */
871 1 Newz(0, buf, len, char);
872 1 merr = mp_to_radix(self, buf, len, NULL, 10);
873 PERL_UNUSED_VAR(merr);
874 1 RETVAL = newSVpv(buf, 0);
875 1 Safefree(buf);
876 }
877 OUTPUT:
878 RETVAL
« inc/CryptX_ASN1.xs.inc ↑ Summary inc/CryptX_Mac_HMAC.xs.inc »