blib/lib/Quantum/Usrn.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 10 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 95.4% 63 / 66 CC 9 SCAR 11.8
CC
9
Cov
98%
_filter_noise
5.0
_generate_noise
4.0
Usrn
2.0
CRAP 3.3
↑ Summary
1 package Quantum::Usrn;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Crypt::Blowfish;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $Quantum::Usrn::VERSION = '1.00';
6  
7 1 my $key = pack('H*', q{4d61727479205061756c6579203c6d61727479406b617365692e636f6d3e0a4a75737420416e6f74686572205065726c204861636b65720a});
8 1 my $cipher = Crypt::Blowfish->new($key);
9  
10 5 ▶sub _generate_noise {
Subroutine _generate_noise: called CC=4 SCAR=13.9
11 5 my $data = shift;
12 5 my $x = pack('NN', rand(2**32), rand(2**32));
13 5 my $result = $x;
14 5 ▶ if ((~$data & $data) eq 0 and $data==int $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ((~$data & $data) eq 0 and $data == int $data) { } 4 1 5
Condition66%2/3
(~$data & $data) eq 0 and $data == int $data
!ll&&!rl&&r
104
MC/DC50%1/2
(~$data & $data) eq 0 and $data == int $data
(~$data & $data) eq 0 $data == int $data
Truth table66%2/3
A and B
A= (~$data & $data) eq 0B= $data == int $data
ABresult
0X0
100
111
15 4 $result .= $x = $cipher->encrypt($x ^ pack('a4N', 'srn#', int(rand(2**32))));
16 4 $result .= $x = $cipher->encrypt($x ^ pack('NN', $data, int(rand(2**32))));
17 } else {
18 1 $result .= $x = $cipher->encrypt($x ^ pack('a4N', 'srn$', int(rand(2**32))));
19 1 foreach my $four ($data=~/.{1,4}/ogs) {
20 6 $result .= $x = $cipher->encrypt($x ^ pack('a4N', $four, int(rand(2**32))));
21 }
22 }
23 5 return $result;
24 }
25  
26 10 ▶sub _filter_noise {
Subroutine _filter_noise: called CC=5 SCAR=16.1
27 10 my $data = shift;
28 10 my ($x, $b0, @block) = $data=~/.{8}/ogs;
29 10 ▶ return undef unless defined $b0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b0 4 6 10
30 6 my ($type) = substr($x ^ $cipher->decrypt($b0), 0, 4) =~ /^srn([#\$])$/;
31 6 $x = $b0;
32 6 ▶ return undef unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 5 6
33 5 my $result;
34 5 ▶ if ($type eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '#') { } 4 1 5
35 4 $result = (unpack('NN', $x ^ $cipher->decrypt($block[0])))[0];
36 } else {
37 1 foreach my $block (@block) {
38 6 my $txt = $x ^ $cipher->decrypt($block);
39 6 $result .= substr($txt, 0, 4);
40 6 $x = $block;
41 }
42 }
43 5 return $result;
44 }
45  
46 # When we get a 'sensible' value, we want to produce noise;
47 # when we get noise, we want to produce a sensible value.
48 # We produce our noise by encrypting the sensible information and an equal
49 # amount of randomness. Since our key is private, it will look like perfect
50 # noise to anyone outside.
51 # When we get any value, we check it it looks like noise by decrypting it.
52 # If it decrypts, we retrieve the original value and return its compliment;
53 # otherwise, we encrypt it with some randomness and return our noise.
54  
55 10 ▶sub Usrn ($) {
Subroutine Usrn: called CC=2 SCAR=6.9
Pod undocumented
56 10 my $arg = shift;
57 10 my $val = _filter_noise($arg);
58 10 ▶ return defined $val ? ~$val : _generate_noise($arg);
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 5 5 10
59 }
60  
61 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
62 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 *{caller().'::Usrn'} = \&Usrn;
64 1 1;
65 }
66  
67 1 1;
68  
69 =head1 NAME
70  
71 Quantum::Usrn - Square root of not.
72  
73 =head1 SYNOPSIS
74  
75 use Quantum::Usrn;
76  
77 $noise = Usrn($value);
78 $not_value = Usrn($noise);
79  
80 =head1 DESCRIPTION
81  
82 Provide the 'square root of not' function (Usrn), used by weird Quantum
83 Physicists. Applying Usrn to a value will produce noise; applying Usrn to that
84 noise will produce the bitwise negation of the original value.
85  
86 It all sounds a bit stange, and mostly useless.
87  
88 =head1 HISTORY
89  
90 On Monday 26th February 2001 I went to hear Damian Conway give his talk on
91 Quantum::Superpositions at London.pm. During the talk he described the Physics
92 of real quamtum superpositions, and mentioned the 'square root of not' operator.
93 After explaining its properties (see above) he said "it is unlikely that you
94 will see this operator in Perl any time soon". Well, we all know what happens
95 when people say things like that...
96  
97 =head1 SEE ALSO
98  
99 A good physics book or psychiatrist.
100  
101 =head1 AUTHOR
102  
103 Marty Pauley E<lt>marty@kasei.comE<gt>
104  
105 =head1 COPYRIGHT
106  
107 Copyright (C) 2001 Kasei
108  
109 This program is free software; you can redistribute it and/or modify it
110 under the terms of either:
111 a) the GNU General Public License;
112 either version 2 of the License, or (at your option) any later version.
113 b) the Perl Artistic License.
114  
115 This module is distributed in the hope that it will be useful, although I
116 doubt that it will be. There is NO WARRANTY, not even the implied warranty of
117 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE; I can't think of any
118 particular purpose that it would be fit for.
119  
120 =cut
↑ Summary