line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Perl::BigInt; |
2
|
|
|
|
|
|
|
|
3
|
16
|
|
|
16
|
|
626882
|
use strict; |
|
16
|
|
|
|
|
64
|
|
|
16
|
|
|
|
|
395
|
|
4
|
16
|
|
|
16
|
|
78
|
use warnings; |
|
16
|
|
|
|
|
35
|
|
|
16
|
|
|
|
|
431
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#Even though Crypt::Perl intends to be pure Perl, there’s no reason |
7
|
|
|
|
|
|
|
#not to use faster computation methods when they’re available. |
8
|
16
|
|
|
16
|
|
14597
|
use Math::BigInt try => 'GMP,Pari'; |
|
16
|
|
|
|
|
319525
|
|
|
16
|
|
|
|
|
75
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#No FastCalc because of bugs shown in the following test runs: |
11
|
|
|
|
|
|
|
#http://www.cpantesters.org/cpan/report/a03dce70-c698-11e6-a1ce-1a99c671d6e6 |
12
|
|
|
|
|
|
|
#http://www.cpantesters.org/cpan/report/0a3e797e-c693-11e6-8c46-2488c671d6e6 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#To test pure Perl speed, comment out the above and enable: |
15
|
|
|
|
|
|
|
#use Math::BigInt; |
16
|
|
|
|
|
|
|
|
17
|
16
|
|
|
16
|
|
315301
|
use parent -norequire => 'Math::BigInt'; |
|
16
|
|
|
|
|
38
|
|
|
16
|
|
|
|
|
136
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#There has been some trouble getting GMP and Pari to do from_bytes() |
20
|
|
|
|
|
|
|
#and as_bytes(), so let’s check on those here. |
21
|
|
|
|
|
|
|
BEGIN { |
22
|
16
|
50
|
|
16
|
|
1613
|
if ( !eval { __PACKAGE__->fffrom_bytes('1234') } ) { |
|
16
|
|
|
|
|
361
|
|
23
|
16
|
|
|
|
|
97
|
*from_bytes = \&_pp_from_bytes; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
16
|
50
|
|
|
|
48
|
if ( !eval { __PACKAGE__->new(1234)->aaas_bytes() } ) { |
|
16
|
|
|
|
|
99
|
|
27
|
16
|
|
|
|
|
1870
|
*as_bytes = \&_pp_as_bytes; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
16
|
|
|
|
|
396
|
$@ = q<>; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
16
|
|
|
16
|
|
6535
|
use Crypt::Perl::X (); |
|
16
|
|
|
|
|
48
|
|
|
16
|
|
|
|
|
2957
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _pp_from_bytes { |
36
|
2803
|
|
|
2803
|
|
974798
|
my $class = shift; |
37
|
|
|
|
|
|
|
|
38
|
2803
|
|
|
|
|
16050
|
return $class->from_hex( unpack 'H*', $_[0] ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _pp_as_bytes { |
42
|
5090
|
|
|
5090
|
|
23260
|
my ($self) = @_; |
43
|
|
|
|
|
|
|
|
44
|
5090
|
50
|
|
|
|
18191
|
die Crypt::Perl::X::create('Generic', "Negatives ($self) can’t convert to bytes!") if $self < 0; |
45
|
|
|
|
|
|
|
|
46
|
5090
|
|
|
|
|
852243
|
my $hex = $self->as_hex(); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#Ensure that we have an even number of hex digits. |
49
|
5090
|
100
|
|
|
|
10163255
|
if (length($hex) % 2) { |
50
|
2178
|
|
|
|
|
6527
|
substr($hex, 1, 1) = q<>; #just remove the “x” of “0x” |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
2912
|
|
|
|
|
7159
|
substr($hex, 0, 2) = q<>; #remove “0x” |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
5090
|
|
|
|
|
31909
|
return pack 'H*', $hex; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub bit_length { |
60
|
1435
|
|
|
1435
|
0
|
10639
|
my ($self) = @_; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#Probably faster than 1 + $self->copy()->blog(2) … |
63
|
1435
|
|
|
|
|
6258
|
return( length($self->as_bin()) - 2 ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub test_bit { |
67
|
410212
|
|
|
410212
|
0
|
777609
|
my ($self, $bit_from_least) = @_; |
68
|
|
|
|
|
|
|
|
69
|
410212
|
|
|
|
|
957330
|
my $bstr = substr( $self->as_bin(), 2 ); |
70
|
|
|
|
|
|
|
|
71
|
410212
|
100
|
|
|
|
446404534
|
return 0 if $bit_from_least >= length($bstr); |
72
|
|
|
|
|
|
|
|
73
|
409700
|
|
|
|
|
1330012
|
return substr($bstr, -$bit_from_least - 1, 1); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |