Crypto/Bech32.pm

filter: Statementstmt 100% 154 / 154 Branchbran 94.4% 34 / 36 Conditioncond 85.7% 18 / 21 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 25 / 25 Podpod 25.0% 3 / 12 totaltotal 92.6% 241 / 260 CC 34 SCAR 14.2
CC
34
Cov
98%
split_bech32
10.0
decode_bech32
5.0
decode_segwit
5.0
CRAP 4.2
« Crypto/Script/Tree.pm ↑ Summary Crypto/Script.pm »
1 package Bitcoin::Crypto::Bech32;
2 65 $Bitcoin::Crypto::Bech32::VERSION = '4.004';
3 65 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 65 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 65 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 65 ▶use Types::Common -sigs, -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 65 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 65 ▶use Bitcoin::Crypto::Util::Internal qw(validate_segwit);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 65 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 65 ▶use Bitcoin::Crypto::Constants qw(:bech32);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 65 our @EXPORT_OK = qw(
15 translate_5to8
16 translate_8to5
17 encode_bech32
18 decode_bech32
19 encode_segwit
20 decode_segwit
21 get_hrp
22 );
23  
24 65 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
25  
26 65 my $CHECKSUM_SIZE = 6;
27 65 my $BECH32M_CONSTANT = 0x2bc830a3;
28  
29 65 my @alphabet = qw(
30 q p z r y 9 x 8
31 g f 2 t v d w 0
32 s 3 j n 5 4 k h
33 c e 6 m u a 7 l
34 );
35  
36 65 my %alphabet_mapped = map { $alphabet[$_] => $_ } 0 .. $#alphabet;
37  
38 sub polymod
39 {
40 265 ▶ my ($values) = @_;
Subroutine polymod: called CC=4 SCAR=13.9
Pod undocumented
41 265 my @consts = (0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3);
42 265 my $chk = 1;
43 265 for my $val (@$values) {
44 14555 my $b = ($chk >> 25);
45 14555 $chk = ($chk & 0x1ffffff) << 5 ^ $val;
46 14555 for (0 .. 4) {
47 72775 ▶ $chk ^= ((($b >> $_) & 1) ? $consts[$_] : 0);
Branch: 1 branch
BranchTrueFalseTotal
$b >> $_ & 1 ? : 31890 40885 72775
48 }
49 }
50 265 return $chk;
51 }
52  
53 sub hrp_expand
54 {
55 265 ▶ my @hrp = split //, shift;
Subroutine hrp_expand: called CC=2 SCAR=6.9
Pod undocumented
56 265 my (@part1, @part2);
57 265 for (@hrp) {
58 960 my $val = ord;
59 960 push @part1, $val >> 5;
60 960 push @part2, $val & 31;
61 }
62 265 return [@part1, 0, @part2];
63 }
64  
65 sub to_numarr
66 {
67 265 ▶ my ($string) = @_;
Subroutine to_numarr: called CC=1 SCAR=0.0
Pod undocumented
68  
69 265 return [map { $alphabet_mapped{$_} } split //, $string];
70 }
71  
72 sub create_checksum
73 {
74 52 ▶ my ($hrp, $data) = @_;
Subroutine create_checksum: called CC=2 SCAR=6.9
Pod undocumented
75 52 my $polymod = polymod([@{hrp_expand $hrp}, @{to_numarr $data}, (0) x $CHECKSUM_SIZE]) ^ 1;
76 52 my $checksum;
77 52 for (0 .. $CHECKSUM_SIZE - 1) {
78 312 $checksum .= $alphabet[($polymod >> 5 * (5 - $_)) & 31];
79 }
80 52 return $checksum;
81 }
82  
83 sub create_checksum_bech32m
84 {
85 43 ▶ my ($hrp, $data) = @_;
Subroutine create_checksum_bech32m: called CC=2 SCAR=6.9
Pod undocumented
86 43 my $polymod = polymod([@{hrp_expand $hrp}, @{to_numarr $data}, (0) x $CHECKSUM_SIZE]) ^ $BECH32M_CONSTANT;
87 43 my $checksum;
88 43 for (0 .. $CHECKSUM_SIZE - 1) {
89 258 $checksum .= $alphabet[($polymod >> 5 * (5 - $_)) & 31];
90 }
91 43 return $checksum;
92 }
93  
94 sub verify_checksum
95 {
96 114 ▶ my ($hrp, $data) = @_;
Subroutine verify_checksum: called CC=1 SCAR=0.0
Pod undocumented
97 114 return polymod([@{hrp_expand $hrp}, @{to_numarr $data}]) == 1;
98 }
99  
100 sub verify_checksum_bech32m
101 {
102 56 ▶ my ($hrp, $data) = @_;
Subroutine verify_checksum_bech32m: called CC=1 SCAR=0.0
Pod undocumented
103 56 return polymod([@{hrp_expand $hrp}, @{to_numarr $data}]) == $BECH32M_CONSTANT;
104 }
105  
106 65 signature_for split_bech32 => (
107 positional => [Str],
108 );
109  
110 sub split_bech32
111 {
112 196 ▶ my ($bech32enc) = @_;
Subroutine split_bech32: called CC=10 SCAR=23.0
113  
114 196 ▶ $bech32enc = lc $bech32enc
Branch: 1 branch
BranchTrueFalseTotal
if uc $bech32enc eq $bech32enc 13 183 196
115 if uc $bech32enc eq $bech32enc;
116  
117 196 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $bech32enc > 90 2 194 196
118 'bech32 string too long'
119 ) if length $bech32enc > 90;
120  
121 194 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if lc $bech32enc ne $bech32enc 6 188 194
122 'bech32 string contains mixed case'
123 ) if lc $bech32enc ne $bech32enc;
124  
125 188 my @parts = split /1/, $bech32enc;
126  
127 188 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if @parts < 2 3 185 188
128 'bech32 separator character missing'
129 ) if @parts < 2;
130  
131 185 my $data = pop @parts;
132  
133 185 @parts = (join('1', @parts), $data);
134  
135 185 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $parts[0] < 1 or length $parts[0] > 83 3 182 185
Condition66%2/3
length $parts[0] < 1 or length $parts[0] > 83
l!l&&r!l&&!r
30182
MC/DC50%1/2
length $parts[0] < 1 or length $parts[0] > 83
length $parts[0] < 1 length $parts[0] > 83
Truth table66%2/3
A or B
A= length $parts[0] < 1B= length $parts[0] > 83
ABresult
1X1
011
000
136 'incorrect length of bech32 human readable part'
137 ) if length $parts[0] < 1 || length $parts[0] > 83;
138  
139 182 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[0] =~ /\A[\x21-\x7e]+\z/u 6 176 182
140 'illegal characters in bech32 human readable part'
141 ) if $parts[0] !~ /\A[\x21-\x7e]+\z/;
142  
143 176 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $parts[1] < 6 3 173 176
144 'incorrect length of bech32 data part'
145 ) if length $parts[1] < 6;
146  
147 173 my $chars = join '', @alphabet;
148 173 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[1] =~ /\A[$chars]+\z/u 3 170 173
149 'illegal characters in bech32 data part'
150 ) if $parts[1] !~ /\A[$chars]+\z/;
151  
152 170 return @parts;
153 }
154  
155 65 signature_for translate_5to8 => (
156 positional => [ArrayRef [Int]],
157 );
158  
159 # used during segwit address decoding
160 sub translate_5to8
161 {
162 77 ▶ my ($values_ref) = @_;
Subroutine translate_5to8: called CC=4 SCAR=13.9
163 77 my @enc_values = @{$values_ref};
164  
165 77 my $bits = unpack 'B*', pack 'C*', @enc_values;
166 77 $bits = join '', map { substr $_, 3 } unpack '(a8)*', $bits;
167  
168 77 my $length_padded = length $bits;
169 77 my $padding = $length_padded % 8;
170 77 $bits =~ s/0{$padding}$//;
171  
172 77 ▶ Bitcoin::Crypto::Exception::Bech32InputData->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length($bits) % 8 != 0 or length $bits < $length_padded - 4 3 74 77
Condition100%3/3
length($bits) % 8 != 0 or length $bits < $length_padded - 4
l!l&&r!l&&!r
2174
MC/DC100%2/2
length($bits) % 8 != 0 or length $bits < $length_padded - 4
length($bits) % 8 != 0 length $bits < $length_padded - 4
Truth table100%3/3
A or B
A= length($bits) % 8 != 0B= length $bits < $length_padded - 4
ABresult
1X1
011
000
173 'incorrect padding encoded in bech32'
174 ) if length($bits) % 8 != 0 || length($bits) < $length_padded - 4;
175  
176 74 my @data = unpack '(a8)*', $bits;
177 74 my $result = '';
178 74 for my $bitstr (@data) {
179 1940 $result .= pack 'B8', $bitstr;
180 }
181 74 return $result;
182 }
183  
184 65 signature_for translate_8to5 => (
185 positional => [ByteStr],
186 );
187  
188 # used during segwit address encoding
189 sub translate_8to5
190 {
191 79 ▶ my ($bytes) = @_;
Subroutine translate_8to5: called CC=2 SCAR=6.9
192  
193 79 my @data = unpack '(a5)*', unpack 'B*', $bytes;
194 79 my @result;
195 79 for my $bitstr (@data) {
196 3406 my $pad = 5 - length $bitstr;
197 3406 my $num = unpack 'C', pack 'B*', "000$bitstr" . 0 x $pad;
198 3406 push @result, $num;
199 }
200  
201 79 return \@result;
202 }
203  
204 sub encode_base32
205 {
206 95 ▶ my ($array) = @_;
Subroutine encode_base32: called CC=4 SCAR=14.5
Pod undocumented
207  
208 95 my $result = '';
209 95 for my $num (@{$array}) {
210 3916 ▶ Bitcoin::Crypto::Exception::Bech32InputData->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $num < 0 or $num > 31 0 3916 3916
Condition33%1/3
$num < 0 or $num > 31
l!l&&r!l&&!r
003916
MC/DC0%0/2
$num < 0 or $num > 31
$num < 0 $num > 31
Truth table33%1/3
A or B
A= $num < 0B= $num > 31
ABresult
1X1
011
000
211 'incorrect number to be encoded in bech32: must be between 0 and 31'
212 ) if $num < 0 || $num > 31;
213 3916 $result .= $alphabet[$num];
214 }
215  
216 95 return $result;
217 }
218  
219 sub decode_base32
220 {
221 109 ▶ my ($encoded) = @_;
Subroutine decode_base32: called CC=1 SCAR=0.0
Pod undocumented
222  
223 109 my @enc_values = map { $alphabet_mapped{$_} } split //, $encoded;
224  
225 109 return \@enc_values;
226 }
227  
228 65 signature_for encode_bech32 => (
229 positional => [Str, ArrayRef [Int], Enum [BECH32M, BECH32], {default => BECH32M}],
230 );
231  
232 sub encode_bech32
233 {
234 95 ▶ my ($hrp, $data, $type) = @_;
Subroutine encode_bech32: called CC=3 SCAR=11.0
235  
236 95 my $result = encode_base32($data);
237 95 my $checksum;
238  
239 95 ▶ if ($type eq BECH32) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'bech32') { } 52 43 95
elsif ($type eq 'bech32m') { } 43 0 43
240 52 $checksum = create_checksum($hrp, $result);
241 }
242 elsif ($type eq BECH32M) {
243 43 $checksum = create_checksum_bech32m($hrp, $result);
244 }
245  
246 95 return $hrp . 1 . $result . $checksum;
247 }
248  
249 65 signature_for encode_segwit => (
250 positional => [Str, ByteStr],
251 );
252  
253 sub encode_segwit
254 {
255 79 ▶ my ($hrp, $bytes) = @_;
Subroutine encode_segwit: called CC=2 SCAR=6.9
256  
257 79 my $version = validate_segwit($bytes);
258 79 ▶ return encode_bech32($hrp, [$version, @{translate_8to5(substr $bytes, 1)}], $version == 0 ? BECH32 : BECH32M);
Branch: 1 branch
BranchTrueFalseTotal
$version == 0 ? : 43 36 79
259 }
260  
261 sub decode_bech32
262 {
263 134 ▶ my ($hrp, $data) = split_bech32 @_;
Subroutine decode_bech32: called CC=5 SCAR=16.1
Pod documented
264  
265 114 my $type;
266 114 ▶ $type = BECH32
Branch: 1 branch
BranchTrueFalseTotal
if verify_checksum($hrp, $data) 58 56 114
267 if verify_checksum($hrp, $data);
268 114 ▶ $type = BECH32M
Branch: 1 branch
BranchTrueFalseTotal
if not $type and verify_checksum_bech32m($hrp, $data) 51 63 114
Condition100%3/3
not $type and verify_checksum_bech32m($hrp, $data)
!ll&&!rl&&r
58551
MC/DC100%2/2
not $type and verify_checksum_bech32m($hrp, $data)
not $type verify_checksum_bech32m($hrp, $data)
Truth table100%3/3
not(A) and B
A= not $typeB= verify_checksum_bech32m($hrp, $data)
ABresult
0X0
100
111
269 if !$type && verify_checksum_bech32m($hrp, $data);
270  
271 114 ▶ Bitcoin::Crypto::Exception::Bech32InputChecksum->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $type 5 109 114
272 'incorrect bech32 checksum'
273 ) unless $type;
274  
275 109 return ($hrp, decode_base32(substr $data, 0, -$CHECKSUM_SIZE), $type);
276 }
277  
278 sub decode_segwit
279 {
280 88 ▶ my ($hrp, $data, $type) = decode_bech32 @_;
Subroutine decode_segwit: called CC=5 SCAR=16.1
Pod documented
281 83 my $ver = shift @{$data};
282  
283 83 ▶ Bitcoin::Crypto::Exception::Bech32InputChecksum->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $ver == 0 and $type ne "bech32" or $ver > 0 and $type ne "bech32m" 6 77 83
Condition100%9/9
$ver == 0 and $type ne "bech32" or $ver > 0 and $type ne "bech32m"
l!l&&r!l&&!r
2477
$ver == 0 and $type ne "bech32"
!ll&&!rl&&r
41402
$ver > 0 and $type ne "bech32m"
!ll&&!rl&&r
40374
MC/DC50%2/4
$ver == 0 and $type ne "bech32" or $ver > 0 and $type ne "bech32m"
$ver == 0 $type ne "bech32" $ver > 0 $type ne "bech32m"
Truth table42%3/7
A and B or C and D
A= $ver == 0B= $type ne "bech32"C= $ver > 0D= $type ne "bech32m"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
284 'wrong bech32 checksum calculated for given segwit program'
285 ) if ($ver == 0 && $type ne BECH32)
286 || ($ver > 0 && $type ne BECH32M);
287  
288 77 my $bytes = pack('C', $ver) . translate_5to8 $data;
289 74 validate_segwit($bytes);
290  
291 68 return $bytes;
292 }
293  
294 sub get_hrp
295 {
296 62 ▶ my ($hrp, $data) = split_bech32 @_;
Subroutine get_hrp: called CC=1 SCAR=0.0
Pod documented
297  
298 56 return $hrp;
299 }
300  
301 65 1;
302  
303 __END__
« Crypto/Script/Tree.pm ↑ Summary Crypto/Script.pm »