line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Perl::BigInt; |
2
|
|
|
|
|
|
|
|
3
|
18
|
|
|
18
|
|
640808
|
use strict; |
|
18
|
|
|
|
|
64
|
|
|
18
|
|
|
|
|
500
|
|
4
|
18
|
|
|
18
|
|
85
|
use warnings; |
|
18
|
|
|
|
|
31
|
|
|
18
|
|
|
|
|
510
|
|
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
|
18
|
|
|
18
|
|
17335
|
use Math::BigInt try => 'GMP,Pari'; |
|
18
|
|
|
|
|
390462
|
|
|
18
|
|
|
|
|
88
|
|
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
|
18
|
|
|
18
|
|
376970
|
use parent -norequire => 'Math::BigInt'; |
|
18
|
|
|
|
|
47
|
|
|
18
|
|
|
|
|
163
|
|
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
|
18
|
50
|
|
18
|
|
1933
|
if ( !eval { __PACKAGE__->fffrom_bytes('1234') } ) { |
|
18
|
|
|
|
|
412
|
|
23
|
18
|
|
|
|
|
87
|
*from_bytes = \&_pp_from_bytes; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
18
|
50
|
|
|
|
49
|
if ( !eval { __PACKAGE__->new(1234)->aaas_bytes() } ) { |
|
18
|
|
|
|
|
164
|
|
27
|
18
|
|
|
|
|
2166
|
*as_bytes = \&_pp_as_bytes; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
18
|
|
|
|
|
470
|
$@ = q<>; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
18
|
|
|
18
|
|
6607
|
use Crypt::Perl::X (); |
|
18
|
|
|
|
|
56
|
|
|
18
|
|
|
|
|
3562
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _pp_from_bytes { |
36
|
2805
|
|
|
2805
|
|
1080212
|
my $class = shift; |
37
|
|
|
|
|
|
|
|
38
|
2805
|
|
|
|
|
16334
|
return $class->from_hex( unpack 'H*', $_[0] ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _pp_as_bytes { |
42
|
6507
|
|
|
6507
|
|
28765
|
my ($self) = @_; |
43
|
|
|
|
|
|
|
|
44
|
6507
|
50
|
|
|
|
26309
|
die Crypt::Perl::X::create('Generic', "Negatives ($self) can’t convert to bytes!") if $self < 0; |
45
|
|
|
|
|
|
|
|
46
|
6507
|
|
|
|
|
1235014
|
my $hex = $self->as_hex(); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#Ensure that we have an even number of hex digits. |
49
|
6507
|
100
|
|
|
|
12819295
|
if (length($hex) % 2) { |
50
|
2325
|
|
|
|
|
7488
|
substr($hex, 1, 1) = q<>; #just remove the “x” of “0x” |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
4182
|
|
|
|
|
10336
|
substr($hex, 0, 2) = q<>; #remove “0x” |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
6507
|
|
|
|
|
47363
|
return pack 'H*', $hex; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub bit_length { |
60
|
1435
|
|
|
1435
|
0
|
11915
|
my ($self) = @_; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#Probably faster than 1 + $self->copy()->blog(2) … |
63
|
1435
|
|
|
|
|
5799
|
return( length($self->as_bin()) - 2 ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub test_bit { |
67
|
410354
|
|
|
410354
|
0
|
890304
|
my ($self, $bit_from_least) = @_; |
68
|
|
|
|
|
|
|
|
69
|
410354
|
|
|
|
|
1016072
|
my $bstr = substr( $self->as_bin(), 2 ); |
70
|
|
|
|
|
|
|
|
71
|
410354
|
100
|
|
|
|
498851355
|
return 0 if $bit_from_least >= length($bstr); |
72
|
|
|
|
|
|
|
|
73
|
409838
|
|
|
|
|
1320489
|
return substr($bstr, -$bit_from_least - 1, 1); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
1; |