blib/lib/Math/BigInt/LTM.pm

filter: Statementstmt 52.0% 249 / 478 Branchbran 38.8% 104 / 268 Conditioncond 50.0% 9 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 40.7% 22 / 54 Podpod n/a totaltotal 46.5% 386 / 830 CC 142 SCAR 28.5
CC
142
Cov
47%
_lucas
56.0
_from_base_num
42.0
_to_base_num
42.0
CRAP 17.4
« blib/lib/Crypt/ASN1.pm ↑ Summary blib/lib/Crypt/PK/DH.pm »
1 package Math::BigInt::LTM;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 our $VERSION = '0.091';
6  
7 14 ▶use CryptX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶sub CLONE_SKIP { 1 } # prevent cloning
Subroutine CLONE_SKIP: not called CC=1 SCAR=6.9
11  
12 sub api_version() { 2 } # compatible with Math::BigInt v1.83+
13  
14 4 ▶sub import { }
Subroutine import: called CC=1 SCAR=0.0
15  
16 ### the following functions are implemented in XS
17 # _1ex()
18 # _acmp()
19 # _add()
20 # _alen()
21 # _alen()
22 # _and()
23 # _as_bytes()
24 # _copy()
25 # _dec()
26 # _div()
27 # _from_base()
28 # _from_bin()
29 # _from_bytes()
30 # _from_hex()
31 # _from_oct()
32 # _gcd()
33 # _inc()
34 # _is_even()
35 # _is_odd()
36 # _is_one()
37 # _is_ten()
38 # _is_two()
39 # _is_zero()
40 # _lcm()
41 # _len()
42 # _lsft()
43 # _mod()
44 # _modinv()
45 # _modpow()
46 # _mul()
47 # _new()
48 # _one()
49 # _or()
50 # _pow()
51 # _root()
52 # _rsft()
53 # _set()
54 # _sqrt()
55 # _str()
56 # _sub()
57 # _ten()
58 # _to_base()
59 # _to_bin()
60 # _to_bytes()
61 # _to_hex()
62 # _to_oct()
63 # _two()
64 # _xor()
65 # _zero()
66 # _zeros()
67  
68  
69 ### same as overloading in Math::BigInt::Lib
70 use overload
71  
72 # overload key: with_assign
73  
74 14 ▶ '+' => sub {
Subroutine BEGIN: called CC=1 SCAR=6.7
Subroutine __ANON__: not called CC=2 SCAR=17.9
75 0 my $class = ref $_[0];
76 0 my $x = $class -> _copy($_[0]);
77 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
78 0 return $class -> _add($x, $y);
79 },
80  
81 0 ▶ '-' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
82 0 my $class = ref $_[0];
83 0 my ($x, $y);
84 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
85 0 $y = $_[0];
86 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
87 } else {
88 0 $x = $class -> _copy($_[0]);
89 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
90 }
91 0 return $class -> _sub($x, $y);
92 },
93  
94 0 ▶ '*' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
95 0 my $class = ref $_[0];
96 0 my $x = $class -> _copy($_[0]);
97 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
98 0 return $class -> _mul($x, $y);
99 },
100  
101 0 ▶ '/' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
102 0 my $class = ref $_[0];
103 0 my ($x, $y);
104 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
105 0 $y = $_[0];
106 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
107 } else {
108 0 $x = $class -> _copy($_[0]);
109 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
110 }
111 0 return $class -> _div($x, $y);
112 },
113  
114 0 ▶ '%' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
115 0 my $class = ref $_[0];
116 0 my ($x, $y);
117 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
118 0 $y = $_[0];
119 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
120 } else {
121 0 $x = $class -> _copy($_[0]);
122 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
123 }
124 0 return $class -> _mod($x, $y);
125 },
126  
127 0 ▶ '**' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
128 0 my $class = ref $_[0];
129 0 my ($x, $y);
130 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
131 0 $y = $_[0];
132 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
133 } else {
134 0 $x = $class -> _copy($_[0]);
135 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
136 }
137 0 return $class -> _pow($x, $y);
138 },
139  
140 0 ▶ '<<' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
141 0 my $class = ref $_[0];
142 0 my ($x, $y);
143 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
144 0 $y = $class -> _num($_[0]);
145 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
146 } else {
147 0 $x = $_[0];
148 0 ▶ $y = ref($_[1]) ? $class -> _num($_[1]) : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
149 }
150 0 return $class -> _lsft($x, $y);
151 },
152  
153 0 ▶ '>>' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
154 0 my $class = ref $_[0];
155 0 my ($x, $y);
156 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
157 0 $y = $_[0];
158 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
159 } else {
160 0 $x = $class -> _copy($_[0]);
161 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
162 }
163 0 return $class -> _rsft($x, $y);
164 },
165  
166 # overload key: num_comparison
167  
168 0 ▶ '<' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
169 0 my $class = ref $_[0];
170 0 my ($x, $y);
171 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
172 0 $y = $_[0];
173 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
174 } else {
175 0 $x = $class -> _copy($_[0]);
176 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
177 }
178 0 return $class -> _acmp($x, $y) < 0;
179 },
180  
181 0 ▶ '<=' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
182 0 my $class = ref $_[0];
183 0 my ($x, $y);
184 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
185 0 $y = $_[0];
186 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
187 } else {
188 0 $x = $class -> _copy($_[0]);
189 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
190 }
191 0 return $class -> _acmp($x, $y) <= 0;
192 },
193  
194 0 ▶ '>' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
195 0 my $class = ref $_[0];
196 0 my ($x, $y);
197 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
198 0 $y = $_[0];
199 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
200 } else {
201 0 $x = $class -> _copy($_[0]);
202 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
203 }
204 0 return $class -> _acmp($x, $y) > 0;
205 },
206  
207 0 ▶ '>=' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
208 0 my $class = ref $_[0];
209 0 my ($x, $y);
210 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
211 0 $y = $_[0];
212 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
213 } else {
214 0 $x = $class -> _copy($_[0]);
215 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
216 }
217 0 return $class -> _acmp($x, $y) >= 0;
218 },
219  
220 0 ▶ '==' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
221 0 my $class = ref $_[0];
222 0 my $x = $class -> _copy($_[0]);
223 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
224 0 return $class -> _acmp($x, $y) == 0;
225 },
226  
227 0 ▶ '!=' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
228 0 my $class = ref $_[0];
229 0 my $x = $class -> _copy($_[0]);
230 0 ▶ my $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
231 0 return $class -> _acmp($x, $y) != 0;
232 },
233  
234 # overload key: 3way_comparison
235  
236 0 ▶ '<=>' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
237 0 my $class = ref $_[0];
238 0 my ($x, $y);
239 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
240 0 $y = $_[0];
241 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
242 } else {
243 0 $x = $class -> _copy($_[0]);
244 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
245 }
246 0 return $class -> _acmp($x, $y);
247 },
248  
249 # overload key: binary
250  
251 0 ▶ '&' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
252 0 my $class = ref $_[0];
253 0 my ($x, $y);
254 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
255 0 $y = $_[0];
256 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
257 } else {
258 0 $x = $class -> _copy($_[0]);
259 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
260 }
261 0 return $class -> _and($x, $y);
262 },
263  
264 0 ▶ '|' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
265 0 my $class = ref $_[0];
266 0 my ($x, $y);
267 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
268 0 $y = $_[0];
269 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
270 } else {
271 0 $x = $class -> _copy($_[0]);
272 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
273 }
274 0 return $class -> _or($x, $y);
275 },
276  
277 0 ▶ '^' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
278 0 my $class = ref $_[0];
279 0 my ($x, $y);
280 0 ▶ if ($_[2]) { # if swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]) { } 0 0 0
281 0 $y = $_[0];
282 0 ▶ $x = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
283 } else {
284 0 $x = $class -> _copy($_[0]);
285 0 ▶ $y = ref($_[1]) ? $_[1] : $class -> _new($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 0 0
286 }
287 0 return $class -> _xor($x, $y);
288 },
289  
290 # overload key: func
291  
292 0 ▶ 'abs' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
293  
294 0 ▶ 'sqrt' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 my $class = ref $_[0];
296 0 return $class -> _sqrt($class -> _copy($_[0]));
297 },
298  
299 0 ▶ 'int' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
300  
301 # overload key: conversion
302  
303 0 ▶ 'bool' => sub { ref($_[0]) -> _is_zero($_[0]) ? '' : 1; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref($_[0])->_is_zero($_[0]) ? : 0 0 0
304  
305 4 ▶ '""' => sub { ref($_[0]) -> _str($_[0]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
306  
307 0 ▶ '0+' => sub { ref($_[0]) -> _num($_[0]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
308  
309 0 ▶ '=' => sub { ref($_[0]) -> _copy($_[0]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
310  
311 14 ;
312  
313 ### same as _from_base_num() in Math::BigInt::Lib
314 0 ▶sub _from_base_num {
Subroutine _from_base_num: not called CC=6 SCAR=37.4
315 # Convert an array in the given base to a number.
316 0 my ($class, $in, $base) = @_;
317  
318 # Make sure the base is an object and >= 2.
319 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
320 0 my $two = $class -> _two();
321 0 ▶ croak "base must be >= 2" unless $class -> _acmp($base, $two) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_acmp($base, $two) >= 0 0 0 0
322  
323 # @$in = map { ref($_) ? $_ : $class -> _new($_) } @$in;
324  
325 0 my $ele = $in -> [0];
326  
327 0 ▶ $ele = $class -> _new($ele) unless ref($ele);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ele 0 0 0
328 0 my $x = $class -> _copy($ele);
329  
330 0 for my $i (1 .. $#$in) {
331 0 $x = $class -> _mul($x, $base);
332 0 $ele = $in -> [$i];
333 0 ▶ $ele = $class -> _new($ele) unless ref($ele);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ele 0 0 0
334 0 $x = $class -> _add($x, $ele);
335 }
336  
337 0 return $x;
338 }
339  
340 ### same as _to_base_num() in Math::BigInt::Lib
341 0 ▶sub _to_base_num {
Subroutine _to_base_num: not called CC=6 SCAR=37.4
342 # Convert the number to an array of integers in any base.
343 0 my ($class, $x, $base) = @_;
344  
345 # Make sure the base is an object and >= 2.
346 0 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
347 0 my $two = $class -> _two();
348 0 ▶ croak "base must be >= 2" unless $class -> _acmp($base, $two) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_acmp($base, $two) >= 0 0 0 0
349  
350 0 my $out = [];
351 0 my $xcopy = $class -> _copy($x);
352 0 my $rem;
353  
354 # Do all except the last (most significant) element.
355 0 ▶ until ($class -> _acmp($xcopy, $base) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_acmp($xcopy, $base) < 0) 0 0 0
356 0 ($xcopy, $rem) = $class -> _div($xcopy, $base);
357 0 unshift @$out, $rem;
358 }
359  
360 # Do the last (most significant element).
361 0 ▶ unless ($class -> _is_zero($xcopy)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->_is_zero($xcopy)) 0 0 0
362 0 unshift @$out, $xcopy;
363 }
364  
365 # $out is empty if $x is zero.
366 0 ▶ unshift @$out, $class -> _zero() unless @$out;
Branch: 1 branch
BranchTrueFalseTotal
unless @$out 0 0 0
367  
368 0 return $out;
369 }
370  
371 ### same as _check() in Math::BigInt::Lib
372 1541 ▶sub _check {
Subroutine _check: called CC=3 SCAR=11.0
373 # used by the test suite
374 1541 my ($class, $x) = @_;
375 1541 ▶ return "Input is undefined" unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 1541 1541
376 1541 ▶ return "$x is not a reference" unless ref($x);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 1 1540 1541
377 1540 return 0;
378 }
379  
380 ### same as _digit() in Math::BigInt::Lib
381 27 ▶sub _digit {
Subroutine _digit: called CC=1 SCAR=0.0
382 27 my ($class, $x, $n) = @_;
383 27 substr($class ->_str($x), -($n+1), 1);
384 }
385  
386 ### same as _digitsum() in Math::BigInt::Lib
387 0 ▶sub _digitsum {
Subroutine _digitsum: not called CC=2 SCAR=17.9
388 0 my ($class, $x) = @_;
389  
390 0 my $len = $class -> _len($x);
391 0 my $sum = $class -> _zero();
392 0 ▶ for (my $i = 0 ; $i < $len ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 0 0 0
393 0 my $digit = $class -> _digit($x, $i);
394 0 $digit = $class -> _new($digit);
395 0 $sum = $class -> _add($sum, $digit);
396 }
397  
398 0 return $sum;
399 }
400  
401 ### same as _num() in Math::BigInt::Lib
402 6621 ▶sub _num {
Subroutine _num: called CC=1 SCAR=0.0
403 6621 my ($class, $x) = @_;
404 6621 0 + $class -> _str($x);
405 }
406  
407 ### PATCHED/OLDER _fac() from Math::BigInt::Lib
408 37 ▶sub _fac {
Subroutine _fac: called CC=3 SCAR=11.0
409 # factorial
410 37 my ($class, $x) = @_;
411  
412 37 my $two = $class -> _two();
413  
414 37 ▶ if ($class -> _acmp($x, $two) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($x, $two) < 0) 2 35 37
415 ###HACK: needed for MBI 1.999715 compatibility
416 ###return $class -> _one();
417 2 $class->_set($x, 1); return $x
418 }
419  
420 35 my $i = $class -> _copy($x);
421 35 ▶ while ($class -> _acmp($i, $two) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($i, $two) > 0) 449 35 484
422 449 $i = $class -> _dec($i);
423 449 $x = $class -> _mul($x, $i);
424 }
425  
426 35 return $x;
427 }
428  
429 ### PATCHED _dfac() from Math::BigInt::Lib
430 0 ▶sub _dfac {
Subroutine _dfac: not called CC=3 SCAR=24.8
431 # double factorial
432 0 my ($class, $x) = @_;
433  
434 0 my $two = $class -> _two();
435  
436 0 ▶ if ($class -> _acmp($x, $two) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($x, $two) < 0) 0 0 0
437 ###HACK: needed for MBI 1.999715 compatibility
438 ###return $class -> _one();
439 0 $class->_set($x, 1); return $x
440 }
441  
442 0 my $i = $class -> _copy($x);
443 0 ▶ while ($class -> _acmp($i, $two) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($i, $two) > 0) 0 0 0
444 0 $i = $class -> _sub($i, $two);
445 0 $x = $class -> _mul($x, $i);
446 }
447  
448 0 return $x;
449 }
450  
451 ### same as _nok() in Math::BigInt::Lib
452 16 ▶sub _nok {
Subroutine _nok: called CC=4 SCAR=13.9
453 # Return binomial coefficient (n over k).
454 16 my ($class, $n, $k) = @_;
455  
456 # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as
457 # nok(n, n-k), to minimize the number if iterations in the loop.
458  
459 {
460 16 my $twok = $class -> _mul($class -> _two(), $class -> _copy($k));
461 16 ▶ if ($class -> _acmp($twok, $n) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($twok, $n) > 0) 0 16 16
462 0 $k = $class -> _sub($class -> _copy($n), $k);
463 }
464 }
465  
466 # Example:
467 #
468 # / 7 \ 7! 1*2*3*4 * 5*6*7 5 * 6 * 7
469 # | | = --------- = --------------- = --------- = ((5 * 6) / 2 * 7) / 3
470 # \ 3 / (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3
471 #
472 # Equivalently, _nok(11, 5) is computed as
473 #
474 # (((((((7 * 8) / 2) * 9) / 3) * 10) / 4) * 11) / 5
475  
476 16 ▶ if ($class -> _is_zero($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_zero($k)) 6 10 16
477 6 return $class -> _one();
478 }
479  
480 # Make a copy of the original n, in case the subclass modifies n in-place.
481  
482 10 my $n_orig = $class -> _copy($n);
483  
484 # n = 5, f = 6, d = 2 (cf. example above)
485  
486 10 $n = $class -> _sub($n, $k);
487 10 $n = $class -> _inc($n);
488  
489 10 my $f = $class -> _copy($n);
490 10 $f = $class -> _inc($f);
491  
492 10 my $d = $class -> _two();
493  
494 # while f <= n (the original n, that is) ...
495  
496 10 ▶ while ($class -> _acmp($f, $n_orig) <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_acmp($f, $n_orig) <= 0) 30 10 40
497 30 $n = $class -> _mul($n, $f);
498 30 $n = $class -> _div($n, $d);
499 30 $f = $class -> _inc($f);
500 30 $d = $class -> _inc($d);
501 }
502  
503 10 return $n;
504 }
505  
506 ### same as _sadd() in Math::BigInt::Lib
507 # Signed addition. If the flag is false, $xa might be modified, but not $ya. If
508 # the false is true, $ya might be modified, but not $xa.
509 19236 ▶sub _sadd {
Subroutine _sadd: called CC=6 SCAR=17.9
510 19236 my $class = shift;
511 19236 my ($xa, $xs, $ya, $ys, $flag) = @_;
512 19236 my ($za, $zs);
513  
514 # If the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8)
515  
516 19236 ▶ if ($xs eq $ys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xs eq $ys) 10789 8447 19236
517 10789 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 10789 10789
518 0 $za = $class -> _add($ya, $xa);
519 } else {
520 10789 $za = $class -> _add($xa, $ya);
521 }
522 10789 ▶ $zs = $class -> _is_zero($za) ? '+' : $xs;
Branch: 1 branch
BranchTrueFalseTotal
$class->_is_zero($za) ? : 851 9938 10789
523 10789 return $za, $zs;
524 }
525  
526 8447 my $acmp = $class -> _acmp($xa, $ya); # abs(x) = abs(y)
527  
528 8447 ▶ if ($acmp == 0) { # x = -y or -x = y
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp == 0) 2121 6326 8447
529 2121 $za = $class -> _zero();
530 2121 $zs = '+';
531 2121 return $za, $zs;
532 }
533  
534 6326 ▶ if ($acmp > 0) { # abs(x) > abs(y)
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) { } 4795 1531 6326
535 4795 $za = $class -> _sub($xa, $ya, $flag);
536 4795 $zs = $xs;
537 } else { # abs(x) < abs(y)
538 1531 $za = $class -> _sub($ya, $xa, !$flag);
539 1531 $zs = $ys;
540 }
541 6326 return $za, $zs;
542 }
543  
544 ### same as _ssub() in Math::BigInt::Lib
545 # Signed subtraction. If the flag is false, $xa might be modified, but not $ya.
546 # If the false is true, $ya might be modified, but not $xa.
547 9995 ▶sub _ssub {
Subroutine _ssub: called CC=2 SCAR=6.9
548 9995 my $class = shift;
549 9995 my ($xa, $xs, $ya, $ys, $flag) = @_;
550  
551 # Swap sign of second operand and let _sadd() do the job.
552 9995 ▶ $ys = $ys eq '+' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$ys eq '+' ? : 6329 3666 9995
553 9995 $class -> _sadd($xa, $xs, $ya, $ys, $flag);
554 }
555  
556 ### same as _log_int() in Math::BigInt::Lib
557 36 ▶sub _log_int {
Subroutine _log_int: called CC=13 SCAR=26.1
558 # calculate integer log of $x to base $base
559 # ref to array, ref to array - return ref to array
560 36 my ($class, $x, $base) = @_;
561  
562 # X == 0 => NaN
563 36 ▶ return if $class -> _is_zero($x);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) 0 36 36
564  
565 36 ▶ $base = $class -> _new(2) unless defined($base);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base 0 36 36
566 36 ▶ $base = $class -> _new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 1 35 36
567  
568 # BASE 0 or 1 => NaN
569 36 ▶ return if $class -> _is_zero($base) || $class -> _is_one($base);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($base) or $class->_is_one($base) 0 36 36
Condition33%1/3
$class->_is_zero($base) or $class->_is_one($base)
l!l&&r!l&&!r
0036
MC/DC0%0/2
$class->_is_zero($base) or $class->_is_one($base)
$class->_is_zero($base) $class->_is_one($base)
Truth table33%1/3
A or B
A= $class->_is_zero($base)B= $class->_is_one($base)
ABresult
1X1
011
000
570  
571 # X == 1 => 0 (is exact)
572 36 ▶ if ($class -> _is_one($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_is_one($x)) 0 36 36
573 0 return $class -> _zero(), 1;
574 }
575  
576 36 my $cmp = $class -> _acmp($x, $base);
577  
578 # X == BASE => 1 (is exact)
579 36 ▶ if ($cmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp == 0) 0 36 36
580 0 return $class -> _one(), 1;
581 }
582  
583 # 1 < X < BASE => 0 (is truncated)
584 36 ▶ if ($cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp < 0) 1 35 36
585 1 return $class -> _zero(), 0;
586 }
587  
588 35 my $y;
589  
590 # log(x) / log(b) = log(xm * 10^xe) / log(bm * 10^be)
591 # = (log(xm) + xe*(log(10))) / (log(bm) + be*log(10))
592  
593 {
594 35 my $x_str = $class -> _str($x);
595 35 my $b_str = $class -> _str($base);
596 35 my $xm = "." . $x_str;
597 35 my $bm = "." . $b_str;
598 35 my $xe = length($x_str);
599 35 my $be = length($b_str);
600 35 my $log10 = log(10);
601 35 my $guess = int((log($xm) + $xe * $log10) / (log($bm) + $be * $log10));
602 35 $y = $class -> _new($guess);
603 }
604  
605 35 my $trial = $class -> _pow($class -> _copy($base), $y);
606 35 my $acmp = $class -> _acmp($trial, $x);
607  
608 # Did we get the exact result?
609  
610 35 ▶ return $y, 1 if $acmp == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $acmp == 0 10 25 35
611  
612 # Too small?
613  
614 25 ▶ while ($acmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp < 0) 25 25 50
615 25 $trial = $class -> _mul($trial, $base);
616 25 $y = $class -> _inc($y);
617 25 $acmp = $class -> _acmp($trial, $x);
618 }
619  
620 # Too big?
621  
622 25 ▶ while ($acmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acmp > 0) 7 25 32
623 7 $trial = $class -> _div($trial, $base);
624 7 $y = $class -> _dec($y);
625 7 $acmp = $class -> _acmp($trial, $x);
626 }
627  
628 25 ▶ return $y, 1 if $acmp == 0; # result is exact
Branch: 1 branch
BranchTrueFalseTotal
if $acmp == 0 18 7 25
629 7 return $y, 0; # result is too small
630 }
631  
632 ### same as _lucas() in Math::BigInt::Lib
633 0 ▶sub _lucas {
Subroutine _lucas: not called CC=7 SCAR=40.3
634 0 my ($class, $n) = @_;
635  
636 0 ▶ $n = $class -> _num($n) if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 0 0
637  
638 # In list context, use lucas(n) = lucas(n-1) + lucas(n-2)
639  
640 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
641 0 my @y;
642  
643 0 push @y, $class -> _two();
644 0 ▶ return @y if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
645  
646 0 push @y, $class -> _one();
647 0 ▶ return @y if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
648  
649 0 ▶ for (my $i = 2 ; $i <= $n ; ++ $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 0 0 0
650 0 $y[$i] = $class -> _add($class -> _copy($y[$i - 1]), $y[$i - 2]);
651 }
652  
653 0 return @y;
654 }
655  
656 # In scalar context use that lucas(n) = fib(n-1) + fib(n+1).
657 #
658 # Remember that _fib() behaves differently in scalar context and list
659 # context, so we must add scalar() to get the desired behaviour.
660  
661 0 ▶ return $class -> _two() if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
662  
663 0 return $class -> _add(scalar($class -> _fib($n - 1)),
664 scalar($class -> _fib($n + 1)));
665 }
666  
667 ### same as _fib() in Math::BigInt::Lib
668 0 ▶sub _fib {
Subroutine _fib: not called CC=6 SCAR=37.4
669 0 my ($class, $n) = @_;
670  
671 0 ▶ $n = $class -> _num($n) if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 0 0
672  
673 # In list context, use fib(n) = fib(n-1) + fib(n-2)
674  
675 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
676 0 my @y;
677  
678 0 push @y, $class -> _zero();
679 0 ▶ return @y if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
680  
681 0 push @y, $class -> _one();
682 0 ▶ return @y if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
683  
684 0 ▶ for (my $i = 2 ; $i <= $n ; ++ $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 0 0 0
685 0 $y[$i] = $class -> _add($class -> _copy($y[$i - 1]), $y[$i - 2]);
686 }
687  
688 0 return @y;
689 }
690  
691 # In scalar context use a fast algorithm that is much faster than the
692 # recursive algorith used in list context.
693  
694 0 my $cache = {};
695 0 my $two = $class -> _two();
696 0 my $fib;
697  
698 0 ▶ $fib = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
699 0 my $n = shift;
700 0 ▶ return $class -> _zero() if $n <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 0 0 0 0
701 0 ▶ return $class -> _one() if $n <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 2 0 0 0
702 0 ▶ return $cache -> {$n} if exists $cache -> {$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache->{$n} 0 0 0
703  
704 0 my $k = int($n / 2);
705 0 my $a = $fib -> ($k + 1);
706 0 my $b = $fib -> ($k);
707 0 my $y;
708  
709 0 ▶ if ($n % 2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2 == 1) { } 0 0 0
710 # a*a + b*b
711 0 $y = $class -> _add($class -> _mul($class -> _copy($a), $a),
712 $class -> _mul($class -> _copy($b), $b));
713 } else {
714 # (2*a - b)*b
715 0 $y = $class -> _mul($class -> _sub($class -> _mul(
716 $class -> _copy($two), $a), $b), $b);
717 }
718  
719 0 $cache -> {$n} = $y;
720 0 return $y;
721 0 };
722  
723 0 return $fib -> ($n);
724 }
725  
726 ### same as _sand() in Math::BigInt::Lib
727 9 ▶sub _sand {
Subroutine _sand: called CC=14 SCAR=26.4
728 9 my ($class, $x, $sx, $y, $sy) = @_;
729  
730 9 ▶ return ($class -> _zero(), '+')
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) or $class->_is_zero($y) 0 9 9
Condition33%1/3
$class->_is_zero($x) or $class->_is_zero($y)
l!l&&r!l&&!r
009
MC/DC0%0/2
$class->_is_zero($x) or $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A or B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
1X1
011
000
731 if $class -> _is_zero($x) || $class -> _is_zero($y);
732  
733 9 ▶ my $sign = $sx eq '-' && $sy eq '-' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$sx eq '-' && $sy eq '-' ? : 4 5 9
Condition100%3/3
$sx eq "-" and $sy eq "-"
!ll&&!rl&&r
324
MC/DC100%2/2
$sx eq "-" and $sy eq "-"
$sx eq "-" $sy eq "-"
Truth table100%3/3
A and B
A= $sx eq "-"B= $sy eq "-"
ABresult
0X0
100
111
734  
735 9 my ($bx, $by);
736  
737 9 ▶ if ($sx eq '-') { # if x is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sx eq '-') { } 6 3 9
738 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
739 6 $bx = $class -> _copy($x);
740 6 $bx = $class -> _dec($bx);
741 6 $bx = $class -> _as_hex($bx);
742 6 $bx =~ s/^-?0x//;
743 6 $bx =~ tr<0123456789abcdef>
744 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
745 } else { # if x is positive
746 3 $bx = $class -> _as_hex($x); # get binary representation
747 3 $bx =~ s/^-?0x//;
748 3 $bx =~ tr<fedcba9876543210>
749 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
750 }
751  
752 9 ▶ if ($sy eq '-') { # if y is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sy eq '-') { } 7 2 9
753 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
754 7 $by = $class -> _copy($y);
755 7 $by = $class -> _dec($by);
756 7 $by = $class -> _as_hex($by);
757 7 $by =~ s/^-?0x//;
758 7 $by =~ tr<0123456789abcdef>
759 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
760 } else {
761 2 $by = $class -> _as_hex($y); # get binary representation
762 2 $by =~ s/^-?0x//;
763 2 $by =~ tr<fedcba9876543210>
764 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
765 }
766  
767 # now we have bit-strings from X and Y, reverse them for padding
768 9 $bx = reverse $bx;
769 9 $by = reverse $by;
770  
771 # padd the shorter string
772 9 ▶ my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sx eq "-" 6 3 9
773 9 ▶ my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sy eq "-" 7 2 9
774 9 my $diff = CORE::length($bx) - CORE::length($by);
775 9 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 3 6 9
elsif ($diff < 0) { } 0 6 6
776 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
777 3 $by .= $yy x $diff;
778 } elsif ($diff < 0) {
779 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
780 0 $bx .= $xx x abs($diff);
781 }
782  
783 # and the strings together
784 9 my $r = $bx & $by;
785  
786 # and reverse the result again
787 9 $bx = reverse $r;
788  
789 # One of $bx or $by was negative, so need to flip bits in the result. In both
790 # cases (one or two of them negative, or both positive) we need to get the
791 # characters back.
792 9 ▶ if ($sign eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 4 5 9
793 4 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
794 <0123456789abcdef>;
795 } else {
796 5 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
797 <fedcba9876543210>;
798 }
799  
800 # leading zeros will be stripped by _from_hex()
801 9 $bx = '0x' . $bx;
802 9 $bx = $class -> _from_hex($bx);
803  
804 9 ▶ $bx = $class -> _inc($bx) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 4 5 9
805  
806 # avoid negative zero
807 9 ▶ $sign = '+' if $class -> _is_zero($bx);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($bx) 1 8 9
808  
809 9 return $bx, $sign;
810 }
811  
812 ### same as _sxor() in Math::BigInt::Lib
813 11 ▶sub _sxor {
Subroutine _sxor: called CC=13 SCAR=25.7
814 11 my ($class, $x, $sx, $y, $sy) = @_;
815  
816 11 ▶ return ($class -> _zero(), '+')
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) and $class->_is_zero($y) 0 11 11
Condition33%1/3
$class->_is_zero($x) and $class->_is_zero($y)
!ll&&!rl&&r
1100
MC/DC0%0/2
$class->_is_zero($x) and $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A and B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
0X0
100
111
817 if $class -> _is_zero($x) && $class -> _is_zero($y);
818  
819 11 ▶ my $sign = $sx ne $sy ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$sx ne $sy ? : 7 4 11
820  
821 11 my ($bx, $by);
822  
823 11 ▶ if ($sx eq '-') { # if x is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sx eq '-') { } 7 4 11
824 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
825 7 $bx = $class -> _copy($x);
826 7 $bx = $class -> _dec($bx);
827 7 $bx = $class -> _as_hex($bx);
828 7 $bx =~ s/^-?0x//;
829 7 $bx =~ tr<0123456789abcdef>
830 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
831 } else { # if x is positive
832 4 $bx = $class -> _as_hex($x); # get binary representation
833 4 $bx =~ s/^-?0x//;
834 4 $bx =~ tr<fedcba9876543210>
835 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
836 }
837  
838 11 ▶ if ($sy eq '-') { # if y is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sy eq '-') { } 8 3 11
839 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
840 8 $by = $class -> _copy($y);
841 8 $by = $class -> _dec($by);
842 8 $by = $class -> _as_hex($by);
843 8 $by =~ s/^-?0x//;
844 8 $by =~ tr<0123456789abcdef>
845 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
846 } else {
847 3 $by = $class -> _as_hex($y); # get binary representation
848 3 $by =~ s/^-?0x//;
849 3 $by =~ tr<fedcba9876543210>
850 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
851 }
852  
853 # now we have bit-strings from X and Y, reverse them for padding
854 11 $bx = reverse $bx;
855 11 $by = reverse $by;
856  
857 # padd the shorter string
858 11 ▶ my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sx eq "-" 7 4 11
859 11 ▶ my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sy eq "-" 8 3 11
860 11 my $diff = CORE::length($bx) - CORE::length($by);
861 11 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 3 8 11
elsif ($diff < 0) { } 1 7 8
862 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
863 3 $by .= $yy x $diff;
864 } elsif ($diff < 0) {
865 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
866 1 $bx .= $xx x abs($diff);
867 }
868  
869 # xor the strings together
870 11 my $r = $bx ^ $by;
871  
872 # and reverse the result again
873 11 $bx = reverse $r;
874  
875 # One of $bx or $by was negative, so need to flip bits in the result. In both
876 # cases (one or two of them negative, or both positive) we need to get the
877 # characters back.
878 11 ▶ if ($sign eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 7 4 11
879 7 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
880 <0123456789abcdef>;
881 } else {
882 4 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
883 <fedcba9876543210>;
884 }
885  
886 # leading zeros will be stripped by _from_hex()
887 11 $bx = '0x' . $bx;
888 11 $bx = $class -> _from_hex($bx);
889  
890 11 ▶ $bx = $class -> _inc($bx) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 7 4 11
891  
892 # avoid negative zero
893 11 ▶ $sign = '+' if $class -> _is_zero($bx);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($bx) 2 9 11
894  
895 11 return $bx, $sign;
896 }
897  
898 ### same as _sor() in Math::BigInt::Lib
899 10 ▶sub _sor {
Subroutine _sor: called CC=14 SCAR=26.7
900 10 my ($class, $x, $sx, $y, $sy) = @_;
901  
902 10 ▶ return ($class -> _zero(), '+')
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($x) and $class->_is_zero($y) 0 10 10
Condition33%1/3
$class->_is_zero($x) and $class->_is_zero($y)
!ll&&!rl&&r
1000
MC/DC0%0/2
$class->_is_zero($x) and $class->_is_zero($y)
$class->_is_zero($x) $class->_is_zero($y)
Truth table33%1/3
A and B
A= $class->_is_zero($x)B= $class->_is_zero($y)
ABresult
0X0
100
111
903 if $class -> _is_zero($x) && $class -> _is_zero($y);
904  
905 10 ▶ my $sign = $sx eq '-' || $sy eq '-' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$sx eq '-' || $sy eq '-' ? : 10 0 10
Condition66%2/3
$sx eq "-" or $sy eq "-"
l!l&&r!l&&!r
640
MC/DC0%0/2
$sx eq "-" or $sy eq "-"
$sx eq "-" $sy eq "-"
Truth table66%2/3
A or B
A= $sx eq "-"B= $sy eq "-"
ABresult
1X1
011
000
906  
907 10 my ($bx, $by);
908  
909 10 ▶ if ($sx eq '-') { # if x is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sx eq '-') { } 6 4 10
910 # two's complement: inc (dec unsigned value) and flip all "bits" in $bx
911 6 $bx = $class -> _copy($x);
912 6 $bx = $class -> _dec($bx);
913 6 $bx = $class -> _as_hex($bx);
914 6 $bx =~ s/^-?0x//;
915 6 $bx =~ tr<0123456789abcdef>
916 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
917 } else { # if x is positive
918 4 $bx = $class -> _as_hex($x); # get binary representation
919 4 $bx =~ s/^-?0x//;
920 4 $bx =~ tr<fedcba9876543210>
921 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
922 }
923  
924 10 ▶ if ($sy eq '-') { # if y is negative
Branch: 1 branch
BranchTrueFalseTotal
if ($sy eq '-') { } 7 3 10
925 # two's complement: inc (dec unsigned value) and flip all "bits" in $by
926 7 $by = $class -> _copy($y);
927 7 $by = $class -> _dec($by);
928 7 $by = $class -> _as_hex($by);
929 7 $by =~ s/^-?0x//;
930 7 $by =~ tr<0123456789abcdef>
931 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
932 } else {
933 3 $by = $class -> _as_hex($y); # get binary representation
934 3 $by =~ s/^-?0x//;
935 3 $by =~ tr<fedcba9876543210>
936 <\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>;
937 }
938  
939 # now we have bit-strings from X and Y, reverse them for padding
940 10 $bx = reverse $bx;
941 10 $by = reverse $by;
942  
943 # padd the shorter string
944 10 ▶ my $xx = "\x00"; $xx = "\x0f" if $sx eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sx eq "-" 6 4 10
945 10 ▶ my $yy = "\x00"; $yy = "\x0f" if $sy eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sy eq "-" 7 3 10
946 10 my $diff = CORE::length($bx) - CORE::length($by);
947 10 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 4 6 10
elsif ($diff < 0) { } 1 5 6
948 # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by
949 4 $by .= $yy x $diff;
950 } elsif ($diff < 0) {
951 # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx
952 1 $bx .= $xx x abs($diff);
953 }
954  
955 # or the strings together
956 10 my $r = $bx | $by;
957  
958 # and reverse the result again
959 10 $bx = reverse $r;
960  
961 # One of $bx or $by was negative, so need to flip bits in the result. In both
962 # cases (one or two of them negative, or both positive) we need to get the
963 # characters back.
964 10 ▶ if ($sign eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign eq '-') { } 10 0 10
965 10 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
966 <0123456789abcdef>;
967 } else {
968 0 $bx =~ tr<\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00>
969 <fedcba9876543210>;
970 }
971  
972 # leading zeros will be stripped by _from_hex()
973 10 $bx = '0x' . $bx;
974 10 $bx = $class -> _from_hex($bx);
975  
976 10 ▶ $bx = $class -> _inc($bx) if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 10 0 10
977  
978 # avoid negative zero
979 10 ▶ $sign = '+' if $class -> _is_zero($bx);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_is_zero($bx) 0 10 10
980  
981 10 return $bx, $sign;
982 }
983  
984 ### same as _as_bin() in Math::BigInt::Lib
985 13 ▶sub _as_bin {
Subroutine _as_bin: called CC=1 SCAR=0.0
986 # convert the number to a string of binary digits with prefix
987 13 my ($class, $x) = @_;
988 13 return '0b' . $class -> _to_bin($x);
989 }
990  
991 ### same as _as_oct() in Math::BigInt::Lib
992 10 ▶sub _as_oct {
Subroutine _as_oct: called CC=1 SCAR=0.0
993 # convert the number to a string of octal digits with prefix
994 10 my ($class, $x) = @_;
995 10 return '0' . $class -> _to_oct($x); # yes, 0 becomes "00"
996 }
997  
998 ### same as _as_hex() in Math::BigInt::Lib
999 72 ▶sub _as_hex {
Subroutine _as_hex: called CC=1 SCAR=0.0
1000 # convert the number to a string of hexadecimal digits with prefix
1001 72 my ($class, $x) = @_;
1002 72 return '0x' . $class -> _to_hex($x);
1003 }
1004  
1005 117 ▶sub _scmp {
Subroutine _scmp: called CC=4 SCAR=13.9
1006 # Compare two signed values. Return -1, 0, or 1.
1007 117 my ($class, $xa, $xs, $ya, $ys) = @_;
1008 117 ▶ if ($xs eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xs eq '+') { } 97 20 117
1009 97 ▶ if ($ys eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ys eq '+') { } 92 5 97
1010 92 return $class -> _acmp($xa, $ya);
1011 } else {
1012 5 return 1;
1013 }
1014 } else {
1015 20 ▶ if ($ys eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ys eq '+') { } 8 12 20
1016 8 return -1;
1017 } else {
1018 12 return $class -> _acmp($ya, $xa);
1019 }
1020 }
1021 }
1022  
1023 14 1;
1024  
1025 =pod
1026  
1027 =head1 NAME
1028  
1029 Math::BigInt::LTM - Use the libtommath library for Math::BigInt routines
1030  
1031 =head1 SYNOPSIS
1032  
1033 use Math::BigInt lib => 'LTM';
1034  
1035 ## See Math::BigInt docs for usage.
1036  
1037 =head1 DESCRIPTION
1038  
1039 I<Since: CryptX-0.029>
1040  
1041 Provides big integer calculations using the libtommath C library.
1042  
1043 =head1 METHODS
1044  
1045 =head2 api_version
1046  
1047 Returns the backend API version expected by L<Math::BigInt>.
1048  
1049 This method exists for L<Math::BigInt> backend integration; normal application code
1050 should not need to call it directly.
1051  
1052 =head1 SEE ALSO
1053  
1054 L<Math::BigInt>, L<https://github.com/libtom/libtommath>
1055  
1056 =cut
« blib/lib/Crypt/ASN1.pm ↑ Summary blib/lib/Crypt/PK/DH.pm »