Crypto/Bech32.pm

filter: Statementstmt 100% 154 / 154 Branchbran 94.4% 34 / 36 Conditioncond 85.7% 18 / 21 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 25 / 25 Podpod 25.0% 3 / 12 totaltotal 93.0% 242 / 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/PSBT/Map.pm ↑ Summary Crypto/Script.pm »
1 package Bitcoin::Crypto::Bech32;
2 65 $Bitcoin::Crypto::Bech32::VERSION = '4.005';
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 269 ▶{
Subroutine polymod: called CC=4 SCAR=13.9
Pod undocumented
40 269 my ($values) = @_;
41 269 my @consts = (0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3);
42 269 my $chk = 1;
43 269 for my $val (@$values) {
44 14811 my $b = ($chk >> 25);
45 14811 $chk = ($chk & 0x1ffffff) << 5 ^ $val;
46 14811 for (0 .. 4) {
47 74055 ▶ $chk ^= ((($b >> $_) & 1) ? $consts[$_] : 0);
Branch: 1 branch
BranchTrueFalseTotal
$b >> $_ & 1 ? : 32446 41609 74055
48 }
49 }
50 269 return $chk;
51 }
52  
53 sub hrp_expand
54 269 ▶{
Subroutine hrp_expand: called CC=2 SCAR=6.9
Pod undocumented
55 269 my @hrp = split //, shift;
56 269 my (@part1, @part2);
57 269 for (@hrp) {
58 968 my $val = ord;
59 968 push @part1, $val >> 5;
60 968 push @part2, $val & 31;
61 }
62 269 return [@part1, 0, @part2];
63 }
64  
65 sub to_numarr
66 269 ▶{
Subroutine to_numarr: called CC=1 SCAR=0.0
Pod undocumented
67 269 my ($string) = @_;
68  
69 269 return [map { $alphabet_mapped{$_} } split //, $string];
70 }
71  
72 sub create_checksum
73 52 ▶{
Subroutine create_checksum: called CC=2 SCAR=6.9
Pod undocumented
74 52 my ($hrp, $data) = @_;
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 43 ▶{
Subroutine create_checksum_bech32m: called CC=2 SCAR=6.9
Pod undocumented
85 43 my ($hrp, $data) = @_;
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 116 ▶{
Subroutine verify_checksum: called CC=1 SCAR=0.0
Pod undocumented
96 116 my ($hrp, $data) = @_;
97 116 return polymod([@{hrp_expand $hrp}, @{to_numarr $data}]) == 1;
98 }
99  
100 sub verify_checksum_bech32m
101 58 ▶{
Subroutine verify_checksum_bech32m: called CC=1 SCAR=0.0
Pod undocumented
102 58 my ($hrp, $data) = @_;
103 58 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 200 ▶{
Subroutine split_bech32: called CC=10 SCAR=23.0
112 200 my ($bech32enc) = @_;
113  
114 200 ▶ $bech32enc = lc $bech32enc
Branch: 1 branch
BranchTrueFalseTotal
if uc $bech32enc eq $bech32enc 13 187 200
115 if uc $bech32enc eq $bech32enc;
116  
117 200 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $bech32enc > 90 2 198 200
118 'bech32 string too long'
119 ) if length $bech32enc > 90;
120  
121 198 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if lc $bech32enc ne $bech32enc 6 192 198
122 'bech32 string contains mixed case'
123 ) if lc $bech32enc ne $bech32enc;
124  
125 192 my @parts = split /1/, $bech32enc;
126  
127 192 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if @parts < 2 3 189 192
128 'bech32 separator character missing'
129 ) if @parts < 2;
130  
131 189 my $data = pop @parts;
132  
133 189 @parts = (join('1', @parts), $data);
134  
135 189 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $parts[0] < 1 or length $parts[0] > 83 3 186 189
Condition66%2/3
length $parts[0] < 1 or length $parts[0] > 83
l!l&&r!l&&!r
30186
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 186 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[0] =~ /\A[\x21-\x7e]+\z/u 6 180 186
140 'illegal characters in bech32 human readable part'
141 ) if $parts[0] !~ /\A[\x21-\x7e]+\z/;
142  
143 180 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length $parts[1] < 6 3 177 180
144 'incorrect length of bech32 data part'
145 ) if length $parts[1] < 6;
146  
147 177 my $chars = join '', @alphabet;
148 177 ▶ Bitcoin::Crypto::Exception::Bech32InputFormat->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[1] =~ /\A[$chars]+\z/u 3 174 177
149 'illegal characters in bech32 data part'
150 ) if $parts[1] !~ /\A[$chars]+\z/;
151  
152 174 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 79 ▶{
Subroutine translate_5to8: called CC=4 SCAR=13.9
162 79 my ($values_ref) = @_;
163 79 my @enc_values = @{$values_ref};
164  
165 79 my $bits = unpack 'B*', pack 'C*', @enc_values;
166 79 $bits = join '', map { substr $_, 3 } unpack '(a8)*', $bits;
167  
168 79 my $length_padded = length $bits;
169 79 my $padding = $length_padded % 8;
170 79 $bits =~ s/0{$padding}$//;
171  
172 79 ▶ Bitcoin::Crypto::Exception::Bech32InputData->raise(
Branch: 1 branch
BranchTrueFalseTotal
if length($bits) % 8 != 0 or length $bits < $length_padded - 4 3 76 79
Condition100%3/3
length($bits) % 8 != 0 or length $bits < $length_padded - 4
l!l&&r!l&&!r
2176
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 76 my @data = unpack '(a8)*', $bits;
177 76 my $result = '';
178 76 for my $bitstr (@data) {
179 2004 $result .= pack 'B8', $bitstr;
180 }
181 76 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 79 ▶{
Subroutine translate_8to5: called CC=2 SCAR=6.9
191 79 my ($bytes) = @_;
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 95 ▶{
Subroutine encode_base32: called CC=4 SCAR=14.5
Pod undocumented
206 95 my ($array) = @_;
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 111 ▶{
Subroutine decode_base32: called CC=1 SCAR=0.0
Pod undocumented
221 111 my ($encoded) = @_;
222  
223 111 my @enc_values = map { $alphabet_mapped{$_} } split //, $encoded;
224  
225 111 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 95 ▶{
Subroutine encode_bech32: called CC=3 SCAR=11.0
234 95 my ($hrp, $data, $type) = @_;
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 79 ▶{
Subroutine encode_segwit: called CC=2 SCAR=6.9
255 79 my ($hrp, $bytes) = @_;
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 136 ▶{
Subroutine decode_bech32: called CC=5 SCAR=16.1
Pod documented
263 136 my ($hrp, $data) = split_bech32 @_;
264  
265 116 my $type;
266 116 ▶ $type = BECH32
Branch: 1 branch
BranchTrueFalseTotal
if verify_checksum($hrp, $data) 58 58 116
267 if verify_checksum($hrp, $data);
268 116 ▶ $type = BECH32M
Branch: 1 branch
BranchTrueFalseTotal
if not $type and verify_checksum_bech32m($hrp, $data) 53 63 116
Condition100%3/3
not $type and verify_checksum_bech32m($hrp, $data)
!ll&&!rl&&r
58553
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 116 ▶ Bitcoin::Crypto::Exception::Bech32InputChecksum->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $type 5 111 116
272 'incorrect bech32 checksum'
273 ) unless $type;
274  
275 111 return ($hrp, decode_base32(substr $data, 0, -$CHECKSUM_SIZE), $type);
276 }
277  
278 sub decode_segwit
279 90 ▶{
Subroutine decode_segwit: called CC=5 SCAR=16.1
Pod documented
280 90 my ($hrp, $data, $type) = decode_bech32 @_;
281 85 my $ver = shift @{$data};
282  
283 85 ▶ Bitcoin::Crypto::Exception::Bech32InputChecksum->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $ver == 0 and $type ne "bech32" or $ver > 0 and $type ne "bech32m" 6 79 85
Condition100%9/9
$ver == 0 and $type ne "bech32" or $ver > 0 and $type ne "bech32m"
l!l&&r!l&&!r
2479
$ver == 0 and $type ne "bech32"
!ll&&!rl&&r
43402
$ver > 0 and $type ne "bech32m"
!ll&&!rl&&r
40394
MC/DC75%3/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 table57%4/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 79 my $bytes = pack('C', $ver) . translate_5to8 $data;
289 76 validate_segwit($bytes);
290  
291 70 return $bytes;
292 }
293  
294 sub get_hrp
295 64 ▶{
Subroutine get_hrp: called CC=1 SCAR=0.0
Pod documented
296 64 my ($hrp, $data) = split_bech32 @_;
297  
298 58 return $hrp;
299 }
300  
301 65 1;
302  
303 __END__
« Crypto/PSBT/Map.pm ↑ Summary Crypto/Script.pm »