ProvablePrime/Rand.pm

filter: Statementstmt 96.0% 24 / 25 Branchbran 100% 10 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 1 totaltotal 86.9% 40 / 46 CC 7 SCAR 15.9
CC
7
Cov
95%
int
7.0
_irand
2.0
BEGIN
1.0
CRAP 4.9
« ProvablePrime.pm ↑ Summary
1 package Math::ProvablePrime::Rand;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Bytes::Random::Secure::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 my $rng;
9  
10 #Return a random integer N such that $lower <= N <= $upper.
11 #cf. Python random.randint()
12 #TODO: test this
13 14250 ▶sub int {
Subroutine int: called CC=7 SCAR=19.5
Pod undocumented
14 14250 my ($lower, $upper) = @_;
15  
16 14250 ▶ $rng ||= Bytes::Random::Secure::Tiny->new();
Condition66%2/3
$rng ||= "Bytes::Random::Secure::Tiny"->new
l!l&&r!l&&!r
1424910
MC/DC0%0/2
$rng ||= "Bytes::Random::Secure::Tiny"->new
$rng "Bytes::Random::Secure::Tiny"->new
Truth table66%2/3
A ||= B
A= $rngB= "Bytes::Random::Secure::Tiny"->new
ABresult
1X1
011
000
17  
18 14250 my $is_bigint = ref $upper;
19  
20 14250 ▶ my $diff = $is_bigint ? $upper->copy()->bsub($lower) : ($upper - $lower);
Branch: 1 branch
BranchTrueFalseTotal
$is_bigint ? : 3035 11215 14250
21  
22 14250 ▶ my $diff_bin = $is_bigint ? substr( $diff->as_bin(), 2 ) : sprintf('%b', $diff);
Branch: 1 branch
BranchTrueFalseTotal
$is_bigint ? : 3035 11215 14250
23  
24 14250 my $bit_length = length $diff_bin;
25  
26 14250 my $ct_of_chunks_of_32_bits = int( $bit_length / 32 );
27 14250 my $ct_of_leftover_bits = $bit_length % 32;
28  
29 14250 ▶ my $diff_hex = $is_bigint ? substr( $diff->as_hex(), 2 ) : sprintf('%x', $diff);
Branch: 1 branch
BranchTrueFalseTotal
$is_bigint ? : 3035 11215 14250
30  
31 #my $hex;
32 14250 my $rand;
33  
34 14250 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$rand->bgt($diff) and do { do { $rand = "Math::BigInt"->from_bin($rng->string_from("01", length $diff_bin)) } } 6410 14250 20660
35 20660 $rand = Math::BigInt->from_bin( $rng->string_from('01', length $diff_bin) );
36  
37 #$hex = join( q<>, map { sprintf '%08x', _irand() } 1 .. $ct_of_chunks_of_32_bits );
38  
39 #Now the remaining bits
40 #if ($ct_of_leftover_bits) {
41 # substr( $hex, 0, 0) = sprintf '%x', int rand( (2 ** $ct_of_leftover_bits) );
42 #}
43 #$rand->badd($lower);
44  
45 #} while (length($hex) == length($diff_hex)) && ($hex gt $diff_hex);
46 } while $rand->bgt($diff);
47  
48 #TODO: Allow this to return scalars.
49 #return $is_bigint ? Math::BigInt->from_hex($hex)->badd($lower) : ($lower + hex $hex);
50 14250 ▶ return $is_bigint ? $rand->badd($lower) : ($rand->numify() + $lower);
Branch: 1 branch
BranchTrueFalseTotal
$is_bigint ? : 3035 11215 14250
51 }
52  
53 #cf. Bytes::Random::Secure::Tiny … but NOT cryptographic-strength
54 0 ▶sub _irand {
Subroutine _irand: not called CC=1 SCAR=6.9
55 0 return CORE::int(0.5 + rand) + CORE::int(rand 0xffffffff);
56 }
57  
58 2 1;
« ProvablePrime.pm ↑ Summary