Crypto/Util/Internal.pm

filter: Statementstmt 98.0% 198 / 202 Branchbran 89.8% 88 / 98 Conditioncond 74.1% 43 / 58 MC/DCmcdc 52.9% 18 / 34 Subroutinesub 100% 41 / 41 Podpod 0.0% 0 / 22 totaltotal 85.2% 388 / 455 CC 72 SCAR 20.7
CC
72
Cov
92%
get_address_type
22.0
validate_segwit
11.2
unpack_compactsize
10.0
CRAP 8.0
« Crypto/Role/PSBT/Finalizer.pm ↑ Summary Crypto/PSBT.pm »
1 package Bitcoin::Crypto::Util::Internal;
2 71 $Bitcoin::Crypto::Util::Internal::VERSION = '4.006';
3 71 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 71 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 71 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶use Unicode::Normalize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 71 ▶use Crypt::KeyDerivation qw(pbkdf2);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 71 ▶use Crypt::Digest::RIPEMD160 qw(ripemd160);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 71 ▶use Crypt::Digest::SHA256 qw(sha256);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 71 ▶use Bitcoin::BIP39 qw(gen_bip39_mnemonic entropy_to_bip39_mnemonic);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 71 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 71 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 71 ▶use Types::Common -types, -sigs;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 71 ▶use Bitcoin::Crypto::Helpers qw(parse_formatdesc ecc);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 71 ▶use Bitcoin::Crypto::Constants qw(:key :witness);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 71 ▶use Bitcoin::Crypto::Types -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 71 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 71 our @EXPORT_OK = qw(
23 validate_wif
24 validate_segwit
25 get_address_type
26 get_key_type
27 get_public_key_compressed
28 generate_mnemonic
29 mnemonic_from_entropy
30 mnemonic_to_seed
31 get_path_info
32 from_format
33 to_format
34 pack_compactsize
35 unpack_compactsize
36 pack_array
37 unpack_array
38 hash160
39 hash256
40 merkle_root
41 tagged_hash
42 lift_x
43 has_even_y
44 get_taproot_ext
45 );
46  
47 71 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
48  
49 sub validate_wif
50 22 ▶{
Subroutine validate_wif: called CC=2 SCAR=6.9
Pod undocumented
51 22 my ($wif) = @_;
52  
53 22 require Bitcoin::Crypto::Base58;
54 22 my $byte_wif = Bitcoin::Crypto::Base58::decode_base58check($wif);
55  
56 21 my $last_byte = substr $byte_wif, -1;
57 21 ▶ if (length $byte_wif == KEY_MAX_LENGTH + 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $byte_wif == 34) { } 9 12 21
58 9 return $last_byte eq WIF_COMPRESSED_BYTE;
59 }
60 else {
61 12 return length $byte_wif == KEY_MAX_LENGTH + 1;
62 }
63 }
64  
65 sub validate_segwit
66 289 ▶{
Subroutine validate_segwit: called CC=11 SCAR=24.2
Pod undocumented
67 289 my ($program) = @_;
68  
69 289 my $version = unpack 'C', $program;
70 289 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and $version >= 0 and $version <= 16 2 287 289
Condition50%4/8
defined $version and $version >= 0 and $version <= 16
!ll&&!rl&&r
02287
defined $version and $version >= 0
!ll&&!rl&&r
00289
$version // "[null]"
l!l
20
MC/DC33%1/3
defined $version and $version >= 0 and $version <= 16
defined $version $version >= 0 $version <= 16
MC/DC0%0/1
$version // "[null]"
$version
Truth table50%2/4
A and B and C
A= defined $versionB= $version >= 0C= $version <= 16
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $version
Aresult
11
00
71 'incorrect witness program version ' . ($version // '[null]')
72 ) unless defined $version && $version >= 0 && $version <= MAX_WITNESS_VERSION;
73  
74 287 $program = substr $program, 1;
75  
76 # common validator
77 287 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless length $program >= 2 and length $program <= 40 6 281 287
Condition100%3/3
length $program >= 2 and length $program <= 40
!ll&&!rl&&r
51281
MC/DC100%2/2
length $program >= 2 and length $program <= 40
length $program >= 2 length $program <= 40
Truth table100%3/3
A and B
A= length $program >= 2B= length $program <= 40
ABresult
0X0
100
111
78 'incorrect witness program length'
79 ) unless length $program >= 2 && length $program <= 40;
80  
81 281 ▶ if ($version == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($version == 0) { } 132 149 281
elsif ($version == 1) { } 143 6 149
82  
83 # SegWit validator
84 132 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless length $program == 20 or length $program == 32 2 130 132
Condition100%3/3
length $program == 20 or length $program == 32
l!l&&r!l&&!r
102282
MC/DC100%2/2
length $program == 20 or length $program == 32
length $program == 20 length $program == 32
Truth table100%3/3
A or B
A= length $program == 20B= length $program == 32
ABresult
1X1
011
000
85 'incorrect witness program length (segwit)'
86 ) unless length $program == 20 || length $program == 32;
87 }
88 elsif ($version == 1) {
89  
90 # Taproot validator
91  
92 # taproot outputs are 32 bytes, but other lengths "remain unencumbered"
93 # do not throw this exception to make bip350 test suite pass (10-Bech32.t)
94  
95 # Bitcoin::Crypto::Exception::SegwitProgram->raise(
96 # 'incorrect witness program length (taproot)'
97 # ) unless length $program == 32;
98 }
99  
100 279 return $version;
101 }
102  
103 sub get_address_type
104 80 ▶{
Subroutine get_address_type: called CC=19 SCAR=30.9
Pod undocumented
105 80 my ($address, $network_id) = @_;
106  
107 80 require Bitcoin::Crypto::Base58;
108 80 require Bitcoin::Crypto::Bech32;
109 80 require Bitcoin::Crypto::Network;
110  
111 80 ▶ my $network = Bitcoin::Crypto::Network->get($network_id // ());
Condition66%2/3
$network_id // ()
l!l&&r!l&&!r
1790
MC/DC0%0/2
$network_id // ()
$network_id ()
Truth table66%2/3
A // B
A= $network_idB= ()
ABresult
1X1
011
000
112 80 my $type;
113  
114 # first, try segwit
115 80 ▶ if ($network->supports_segwit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($network->supports_segwit) 80 0 80
116 80 try {
117 80 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless Bitcoin::Crypto::Bech32::get_hrp($address) eq $network->segwit_hrp 1 65 66
118 'invalid human readable part in address'
119 ) unless Bitcoin::Crypto::Bech32::get_hrp($address) eq $network->segwit_hrp;
120  
121 65 my $data = Bitcoin::Crypto::Bech32::decode_segwit($address);
122 65 my $version = ord substr $data, 0, 1, '';
123  
124 65 ▶ $type = 'P2TR'
Branch: 1 branch
BranchTrueFalseTotal
if $version == 1 and length $data == 32 36 29 65
Condition66%2/3
$version == 1 and length $data == 32
!ll&&!rl&&r
29036
MC/DC50%1/2
$version == 1 and length $data == 32
$version == 1 length $data == 32
Truth table66%2/3
A and B
A= $version == 1B= length $data == 32
ABresult
0X0
100
111
125 if $version == TAPROOT_WITNESS_VERSION
126 && length $data == 32;
127  
128 65 ▶ return $type if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 36 29 65
129  
130 29 ▶ Bitcoin::Crypto::Exception::SegwitProgram->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $version == 0 0 29 29
131 "invalid segwit address of version $version"
132 ) unless $version == SEGWIT_WITNESS_VERSION;
133  
134 29 ▶ $type = 'P2WPKH' if length $data == 20;
Branch: 1 branch
BranchTrueFalseTotal
if length $data == 20 27 2 29
135 29 ▶ $type = 'P2WSH' if length $data == 32;
Branch: 1 branch
BranchTrueFalseTotal
if length $data == 32 2 27 29
136  
137 29 ▶ return $type if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 29 0 29
138  
139 0 Bitcoin::Crypto::Exception::Address->raise(
140 'invalid segwit address'
141 );
142 }
143 catch ($ex) {
144 15 ▶ die $ex unless blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::Bech32InputFormat');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat") 1 14 15
Condition66%2/3
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
!ll&&!rl&&r
0114
MC/DC50%1/2
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
builtin::blessed($ex) $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
Truth table66%2/3
A and B
A= builtin::blessed($ex)B= $ex->isa("Bitcoin::Crypto::Exception::Bech32InputFormat")
ABresult
0X0
100
111
145 }
146 }
147  
148 # then, try legacy
149 14 try {
150 14 my $data = Bitcoin::Crypto::Base58::decode_base58check($address);
151 12 my $byte = substr $data, 0, 1, '';
152  
153 12 ▶ $type = 'P2PKH' if $byte eq $network->p2pkh_byte;
Branch: 1 branch
BranchTrueFalseTotal
if $byte eq $network->p2pkh_byte 4 8 12
154 12 ▶ $type = 'P2SH' if $byte eq $network->p2sh_byte;
Branch: 1 branch
BranchTrueFalseTotal
if $byte eq $network->p2sh_byte 8 4 12
155  
156 12 ▶ Bitcoin::Crypto::Exception::Address->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless length $data == 20 0 12 12
157 'invalid legacy address'
158 ) unless length $data == 20;
159  
160 12 ▶ return $type if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 12 0 12
161  
162 0 Bitcoin::Crypto::Exception::Address->raise(
163 'invalid first byte in address'
164 );
165 }
166 catch ($ex) {
167 2 ▶ die $ex unless blessed $ex && $ex->isa('Bitcoin::Crypto::Exception::Base58InputFormat');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat") 2 0 2
Condition33%1/3
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
!ll&&!rl&&r
020
MC/DC0%0/2
builtin::blessed($ex) and $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
builtin::blessed($ex) $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
Truth table33%1/3
A and B
A= builtin::blessed($ex)B= $ex->isa("Bitcoin::Crypto::Exception::Base58InputFormat")
ABresult
0X0
100
111
168 };
169  
170 0 Bitcoin::Crypto::Exception::Address->raise(
171 "not an address: $address"
172 );
173 }
174  
175 sub get_key_type
176 1587 ▶{
Subroutine get_key_type: called CC=4 SCAR=13.9
Pod undocumented
177 1587 my $entropy = shift;
178  
179 1587 ▶ return undef unless length $entropy;
Branch: 1 branch
BranchTrueFalseTotal
unless length $entropy 2 1585 1587
180 1585 ▶ return 0 if defined get_public_key_compressed($entropy);
Branch: 1 branch
BranchTrueFalseTotal
if defined get_public_key_compressed($entropy) 1046 539 1585
181 539 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if length $entropy <= 32 536 3 539
182 if length $entropy <= KEY_MAX_LENGTH;
183 3 return undef;
184 }
185  
186 sub get_public_key_compressed
187 2123 ▶{
Subroutine get_public_key_compressed: called CC=9 SCAR=22.0
Pod undocumented
188 2123 my $entropy = shift;
189 2123 my $octet = unpack 'C', $entropy;
190  
191 2123 ▶ return undef unless defined $octet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $octet 0 2123 2123
192  
193 2123 ▶ if ($octet == 0x02 || $octet == 0x03) {
Branch: 2 branches
BranchTrueFalseTotal
if ($octet == 2 or $octet == 3) { } 1552 571 2123
elsif ($octet == 4 or $octet == 6 or $octet == 7) { } 50 521 571
Condition100%3/3
$octet == 2 or $octet == 3
l!l&&r!l&&!r
911641571
MC/DC100%2/2
$octet == 2 or $octet == 3
$octet == 2 $octet == 3
Truth table100%3/3
A or B
A= $octet == 2B= $octet == 3
ABresult
1X1
011
000
194 1552 ▶ return 1 if length $entropy == KEY_MAX_LENGTH + 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $entropy == 33 1539 13 1552
195 }
196 ▶ elsif ($octet == 0x04 || $octet == 0x06 || $octet == 0x07) {
Condition100%6/6
$octet == 4 or $octet == 6 or $octet == 7
l!l&&r!l&&!r
491521
$octet == 4 or $octet == 6
l!l&&r!l&&!r
472522
MC/DC100%3/3
$octet == 4 or $octet == 6 or $octet == 7
$octet == 4 $octet == 6 $octet == 7
Truth table100%4/4
A or B or C
A= $octet == 4B= $octet == 6C= $octet == 7
ABCresult
1XX1
01X1
0011
0000
197 50 ▶ return 0 if length $entropy == 2 * KEY_MAX_LENGTH + 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $entropy == 65 45 5 50
198 }
199  
200 539 return undef;
201 }
202  
203 sub mnemonic_to_seed
204 37 ▶{
Subroutine mnemonic_to_seed: called CC=2 SCAR=6.9
Pod undocumented
205 37 my ($mnemonic, $password) = @_;
206  
207 37 $mnemonic = encode('UTF-8', NFKD($mnemonic));
208 37 ▶ $password = encode('UTF-8', NFKD('mnemonic' . ($password // '')));
Condition100%2/2
$password // ""
l!l
334
MC/DC100%1/1
$password // ""
$password
Truth table100%2/2
A
A= $password
Aresult
11
00
209  
210 37 return pbkdf2($mnemonic, $password, 2048, 'SHA512', 64);
211 }
212  
213 sub generate_mnemonic
214 8 ▶{
Subroutine generate_mnemonic: called CC=4 SCAR=14.2
Pod undocumented
215 8 my ($len, $lang) = @_;
216 8 my ($min_len, $len_div, $max_len) = (128, 32, 256);
217  
218 # bip39 specification values
219 8 ▶ Bitcoin::Crypto::Exception::MnemonicGenerate->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $len < $min_len or $len > $max_len or $len % $len_div != 0 1 7 8
Condition50%3/6
$len < $min_len or $len > $max_len or $len % $len_div != 0
l!l&&r!l&&!r
017
$len < $min_len or $len > $max_len
l!l&&r!l&&!r
008
MC/DC33%1/3
$len < $min_len or $len > $max_len or $len % $len_div != 0
$len < $min_len $len > $max_len $len % $len_div != 0
Truth table50%2/4
A or B or C
A= $len < $min_lenB= $len > $max_lenC= $len % $len_div != 0
ABCresult
1XX1
01X1
0011
0000
220 "required entropy of between $min_len and $max_len bits, divisible by $len_div"
221 ) if $len < $min_len || $len > $max_len || $len % $len_div != 0;
222  
223 return Bitcoin::Crypto::Exception::MnemonicGenerate->trap_into(
224 7 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
225 7 my $ret = gen_bip39_mnemonic(bits => $len, language => $lang);
226 7 $ret->{mnemonic};
227 }
228 7 );
229 }
230  
231 sub mnemonic_from_entropy
232 12 ▶{
Subroutine mnemonic_from_entropy: called CC=1 SCAR=0.0
Pod undocumented
233 12 my ($entropy, $lang) = @_;
234  
235 return Bitcoin::Crypto::Exception::MnemonicGenerate->trap_into(
236 12 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
237 12 entropy_to_bip39_mnemonic(
238 entropy => $entropy,
239 language => $lang
240 );
241 }
242 12 );
243 }
244  
245 sub get_path_info
246 9 ▶{
Subroutine get_path_info: called CC=1 SCAR=0.0
Pod undocumented
247 9 my ($path) = @_;
248  
249 # NOTE: ->coerce may still throw because of exceptions in from_string of DerivationPath
250 9 try {
251 9 return scalar DerivationPath->assert_coerce($path);
252 }
253 catch ($e) {
254 4 return undef;
255 }
256 }
257  
258 sub from_format ($)
259 8 ▶{
Subroutine from_format: called CC=1 SCAR=0.0
Pod undocumented
260 8 state $sig = signature(positional => [Tuple [FormatStr, Str]]);
261 8 my ($format, $data) = @{($sig->(@_))[0]};
262  
263 8 return parse_formatdesc($format, $data);
264 }
265  
266 sub to_format ($)
267 1191 ▶{
Subroutine to_format: called CC=1 SCAR=0.0
Pod undocumented
268 1191 state $sig = signature(positional => [Tuple [FormatStr, ByteStr]]);
269 1191 my ($format, $data) = @{($sig->(@_))[0]};
270  
271 1191 return parse_formatdesc($format, $data, 1);
272 }
273  
274 sub pack_compactsize
275 6688 ▶{
Subroutine pack_compactsize: called CC=4 SCAR=13.9
Pod undocumented
276 6688 my $value = shift;
277  
278 6688 ▶ if ($value <= 0xfc) {
Branch: 3 branches
BranchTrueFalseTotal
if ($value <= 252) { } 6674 14 6688
elsif ($value <= 65535) { } 12 2 14
elsif ($value <= 4294967295) { } 1 1 2
279 6674 return pack 'C', $value;
280 }
281 elsif ($value <= 0xffff) {
282 12 return pack 'Cv', 0xfd, $value;
283 }
284 elsif ($value <= 0xffffffff) {
285 1 return pack 'CV', 0xfe, $value;
286 }
287 else {
288 # 32 bit archs should not reach this
289 1 return pack 'CVV', 0xff, $value & 0xffffffff, $value >> 32;
290 }
291 }
292  
293 sub unpack_compactsize
294 6041 ▶{
Subroutine unpack_compactsize: called CC=10 SCAR=23.0
Pod undocumented
295 6041 my ($stream, $pos_ref) = @_;
296 6041 my $partial = !!$pos_ref;
297 6041 ▶ my $pos = $partial ? $$pos_ref : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 5270 771 6041
298  
299 6041 ▶ Bitcoin::Crypto::Exception->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $stream 1 6040 6041
300 "cannot unpack CompactSize: no data in stream"
301 ) unless $pos < length $stream;
302  
303 # if the first byte is 0xfd, 0xfe or 0xff, then CompactSize contains 2, 4 or 8
304 # bytes respectively
305 6040 my $value = ord substr $stream, $pos++, 1;
306 6040 ▶ if ($value > 0xfc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > 252) 18 6022 6040
307 18 my $length = 1 << ($value - 0xfc);
308  
309 18 ▶ Bitcoin::Crypto::Exception->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $pos + $length > length $stream 2 16 18
310 "cannot unpack CompactSize: not enough data in stream"
311 ) if $pos + $length > length $stream;
312  
313 16 ▶ if ($length == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($length == 2) { } 12 4 16
elsif ($length == 4) { } 3 1 4
314 12 $value = unpack "\@$pos v", $stream;
315 }
316 elsif ($length == 4) {
317 3 $value = unpack "\@$pos V", $stream;
318 }
319 else {
320 1 Bitcoin::Crypto::Exception->raise(
321 "cannot unpack CompactSize: no 64 bit support"
322 ) if !Bitcoin::Crypto::Constants::is_64bit;
323  
324 1 my ($lower, $higher) = unpack "\@$pos VV", $stream;
325 1 $value = ($higher << 32) + $lower;
326 }
327  
328 16 $pos += $length;
329 }
330  
331 Bitcoin::Crypto::Exception->raise(
332 6038 ▶ "cannot unpack CompactSize: leftover data in stream"
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $stream 1 6037 6038
Condition100%3/3
not $partial and $pos != length $stream
!ll&&!rl&&r
52697681
MC/DC100%2/2
not $partial and $pos != length $stream
not $partial $pos != length $stream
Truth table100%3/3
not(A) and B
A= not $partialB= $pos != length $stream
ABresult
0X0
100
111
333 ) if !$partial && $pos != length $stream;
334  
335 6037 ▶ $$pos_ref = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 5269 768 6037
336 if $partial;
337  
338 6037 return $value;
339 }
340  
341 sub pack_array
342 239 ▶{
Subroutine pack_array: called CC=1 SCAR=0.0
Pod undocumented
343 239 my ($array) = @_;
344  
345 return join '',
346 239 pack_compactsize(scalar @{$array}),
347 239 (map { pack_compactsize(length $_) . $_ } @{$array}),
348 ;
349 }
350  
351 sub unpack_array
352 178 ▶{
Subroutine unpack_array: called CC=6 SCAR=18.0
Pod undocumented
353 178 my ($serialized, $pos_ref) = @_;
354  
355 178 my $partial = !!$pos_ref;
356 178 ▶ my $pos = $partial ? $$pos_ref : 0;
Branch: 1 branch
BranchTrueFalseTotal
$partial ? : 143 35 178
357  
358 178 my $count = unpack_compactsize $serialized, \$pos;
359 178 my @array;
360 178 for (1 .. $count) {
361 312 my $size = unpack_compactsize $serialized, \$pos;
362  
363 312 push @array, substr $serialized, $pos, $size;
364 312 $pos += $size;
365 }
366  
367 Bitcoin::Crypto::Exception->raise(
368 178 ▶ 'array data is corruputed'
Branch: 1 branch
BranchTrueFalseTotal
if not $partial and $pos != length $serialized 0 178 178
Condition66%2/3
not $partial and $pos != length $serialized
!ll&&!rl&&r
143350
MC/DC0%0/2
not $partial and $pos != length $serialized
not $partial $pos != length $serialized
Truth table66%2/3
not(A) and B
A= not $partialB= $pos != length $serialized
ABresult
0X0
100
111
369 ) if !$partial && $pos != length $serialized;
370  
371 178 ▶ $$pos_ref = $pos
Branch: 1 branch
BranchTrueFalseTotal
if $partial 143 35 178
372 if $partial;
373  
374 178 return \@array;
375 }
376  
377 sub hash160
378 619 ▶{
Subroutine hash160: called CC=1 SCAR=0.0
Pod undocumented
379 619 return ripemd160(sha256(shift));
380 }
381  
382 sub hash256
383 1029 ▶{
Subroutine hash256: called CC=1 SCAR=0.0
Pod undocumented
384 1029 return sha256(sha256(shift));
385 }
386  
387 sub merkle_root
388 10 ▶{
Subroutine merkle_root: called CC=4 SCAR=13.9
Pod undocumented
389 10 my ($leaves) = @_;
390  
391 10 my @parts = map { hash256($_) } @$leaves;
392  
393 10 ▶ Bitcoin::Crypto::Exception->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 10 10
394 'need at least one element to calculate a merkle root'
395 ) unless @parts;
396  
397 10 ▶ while (@parts > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts > 1) 19 10 29
398 @parts = map {
399 59 ▶ hash256($parts[$_] . ($parts[$_ + 1] // $parts[$_]))
Condition66%2/3
$parts[$_ + 1] // $parts[$_]
l!l&&r!l&&!r
5180
MC/DC0%0/2
$parts[$_ + 1] // $parts[$_]
$parts[$_ + 1] $parts[$_]
Truth table66%2/3
A // B
A= $parts[$_ + 1]B= $parts[$_]
ABresult
1X1
011
000
400 } grep {
401 19 $_ % 2 == 0
402 } 0 .. $#parts;
403 }
404  
405 10 return $parts[0];
406 }
407  
408 sub tagged_hash
409 389 ▶{
Subroutine tagged_hash: called CC=2 SCAR=6.9
Pod undocumented
410 389 my ($tag, $message) = @_;
411 389 state $tags = {};
412  
413 389 ▶ $tags->{$tag} //= sha256(encode 'UTF-8', $tag) x 2;
Condition100%2/2
$tags->{$tag} //= sha256(&encode("UTF-8", $tag)) x 2
l!l
34346
MC/DC100%1/1
$tags->{$tag} //= sha256(&encode("UTF-8", $tag)) x 2
$tags->{$tag}
Truth table100%2/2
A
A= $tags->{$tag}
Aresult
11
00
414 389 return sha256($tags->{$tag} . $message);
415 }
416  
417 sub lift_x
418 274 ▶{
Subroutine lift_x: called CC=3 SCAR=11.0
Pod undocumented
419 274 my ($x) = @_;
420  
421 274 ▶ my $key = "\x02" . ($x // '');
Condition50%1/2
$x // ""
l!l
2740
MC/DC0%0/1
$x // ""
$x
Truth table50%1/2
A
A= $x
Aresult
11
00
422 274 ▶ Bitcoin::Crypto::Exception::KeyCreate->raise(
Branch: 1 branch
BranchTrueFalseTotal
unless ecc()->verify_public_key($key) 4 270 274
423 'invalid xonly public key'
424 ) unless ecc->verify_public_key($key);
425  
426 270 return $key;
427 }
428  
429 sub has_even_y
430 86 ▶{
Subroutine has_even_y: called CC=2 SCAR=6.9
Pod undocumented
431 86 my ($key) = @_;
432 86 ▶ $key = $key->raw_key if ref $key;
Branch: 1 branch
BranchTrueFalseTotal
if ref $key 44 42 86
433  
434 return Bitcoin::Crypto::Exception->trap_into(
435 86 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
436 86 $key = ecc->compress_public_key($key);
437 85 return substr($key, 0, 1) eq "\x02";
438 }
439 86 );
440 }
441  
442 sub get_taproot_ext
443 32 ▶{
Subroutine get_taproot_ext: called CC=4 SCAR=14.1
Pod undocumented
444 32 my ($ext_flag, $args) = @_;
445  
446 32 ▶ if ($ext_flag == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ext_flag == 0) { } 1 31 32
elsif ($ext_flag == 1) { } 31 0 31
447 1 return '';
448 }
449 elsif ($ext_flag == 1) {
450  
451 # https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#common-signature-message-extension
452 return $args->{script_tree}->get_leaf($args->{leaf_id})->hash
453 31 ▶ . pack('xV', $args->{codesep_pos} // 0xffffffff);
Condition100%2/2
$args->{"codesep_pos"} // 4294967295
l!l
526
MC/DC100%1/1
$args->{"codesep_pos"} // 4294967295
$args->{"codesep_pos"}
Truth table100%2/2
A
A= $args->{"codesep_pos"}
Aresult
11
00
454 }
455 else {
456 0 Bitcoin::Crypto::Exception->raise(
457 "can not create taproot_ext for unknown ext flag $ext_flag"
458 );
459 }
460 }
461  
462 71 1;
463  
« Crypto/Role/PSBT/Finalizer.pm ↑ Summary Crypto/PSBT.pm »