Crypto/Helpers.pm

filter: Statementstmt 93.8% 106 / 113 Branchbran 79.3% 46 / 58 Conditioncond 61.4% 35 / 57 MC/DCmcdc 32.2% 10 / 31 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 12 totaltotal 74.7% 219 / 293 CC 50 SCAR 24.0
CC
50
Cov
82%
check_strict_der_signature
25.6
standard_push
14.8
make_strict_der_signature
10.2
CRAP 11.0
« Crypto/Transaction.pm ↑ Summary Crypto/Transaction/Digest.pm »
1 package Bitcoin::Crypto::Helpers;
2 75 $Bitcoin::Crypto::Helpers::VERSION = '4.006';
3 75 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 75 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 75 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 75 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 75 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 75 ▶use Bitcoin::Secp256k1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 75 ▶use Bitcoin::Crypto::Constants qw(USE_BIGINTS :sighash);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 75 ▶use Bitcoin::Crypto::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 75 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.5
14 75 require Math::BigInt;
15  
16 # Version 1.6003 of optional GMP is required for the from_bytes / to_bytes implementations
17 0 ▶ if (eval { require Math::BigInt::GMP; Math::BigInt::GMP->VERSION('1.6003'); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Math::BigInt::GMP; 'Math::BigInt::GMP'->VERSION('1.6003'); 1 } }) { } 0 75 75
18 0 Math::BigInt->import(try => 'GMP,LTM');
19 }
20 else {
21 75 Math::BigInt->import(try => 'LTM');
22 }
23 }
24  
25 75 our @EXPORT_OK = qw(
26 pad_hex
27 ensure_length
28 encode_64bit
29 decode_64bit
30 parse_formatdesc
31 ecc
32 standard_push
33 check_strict_public_key
34 check_strict_der_signature
35 make_strict_der_signature
36 die_no_trace
37 check_sighash
38 );
39  
40 75 our @CARP_NOT;
41 75 my %warned;
42  
43 sub pad_hex
44 496 ▶{
Subroutine pad_hex: called CC=2 SCAR=6.9
Pod undocumented
45 496 my ($hex) = @_;
46 496 $hex =~ s/\A0x//;
47  
48 496 ▶ Bitcoin::Crypto::Exception->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $hex =~ /[^0-9a-fA-F]/u 2 494 496
49 'non-hexadecimal characters in a hex string'
50 ) if $hex =~ /[^0-9a-fA-F]/;
51  
52 494 return '0' x (length($hex) % 2) . $hex;
53 }
54  
55 sub ensure_length
56 2685 ▶{
Subroutine ensure_length: called CC=2 SCAR=6.9
Pod undocumented
57 2685 my ($packed, $bytelen) = @_;
58 2685 my $missing = $bytelen - length $packed;
59  
60 2685 ▶ Bitcoin::Crypto::Exception->raise(
Branch: 1 branch
BranchTrueFalseTotal
if $missing < 0 1 2684 2685
61 "packed string exceeds maximum number of bytes allowed ($bytelen)"
62 ) if $missing < 0;
63  
64 2684 return pack("x$missing") . $packed;
65 }
66  
67 sub encode_64bit
68 1261 ▶{
Subroutine encode_64bit: called CC=1 SCAR=0.0
Pod undocumented
69 1261 my $value = shift;
70  
71 1261 if (USE_BIGINTS) {
72 return scalar reverse ensure_length $value->as_bytes, 8;
73 }
74 else {
75 1261 return pack 'VV', $value & 0xffffffff, $value >> 32;
76 }
77 }
78  
79 sub decode_64bit
80 1228 ▶{
Subroutine decode_64bit: called CC=1 SCAR=0.0
Pod undocumented
81 1228 my $bytes = shift;
82  
83 1228 if (USE_BIGINTS) {
84 return Math::BigInt->from_bytes(scalar reverse $bytes);
85 }
86 else {
87 1228 my ($lower, $upper) = unpack 'VV', $bytes;
88 1228 return ($upper << 32) + $lower;
89 }
90 }
91  
92 # default operation is to decode based on formatdesc
93 # passing $reverse makes it encode instead
94 sub parse_formatdesc
95 1845 ▶{
Subroutine parse_formatdesc: called CC=8 SCAR=20.9
Pod undocumented
96 1845 my ($type, $data, $reverse) = @_;
97  
98 1845 ▶ if ($type eq 'hex') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'hex') { } 1543 302 1845
elsif ($type eq 'base58') { } 185 117 302
elsif ($type eq 'base64') { } 115 2 117
elsif ($type ne 'bytes') { } 0 2 2
99 1543 ▶ $data = $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 1050 493 1543
100 ? unpack 'H*', $data
101 : pack 'H*', pad_hex $data
102 ;
103 }
104 elsif ($type eq 'base58') {
105 185 require Bitcoin::Crypto::Base58;
106 185 ▶ $data = $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 109 76 185
107 ? Bitcoin::Crypto::Base58::encode_base58check($data)
108 : Bitcoin::Crypto::Base58::decode_base58check($data)
109 ;
110 }
111 elsif ($type eq 'base64') {
112 115 ▶ $data = $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 31 84 115
113 ? encode_base64($data, '')
114 : decode_base64($data)
115 ;
116 }
117 elsif ($type ne 'bytes') {
118 0 croak "bad format type: $type";
119 }
120  
121 1840 return $data;
122 }
123  
124 # define an arbitrary number of times a single secp256k1 context can be
125 # used. Create a new context after that. This gives an increased security
126 # according to libsecp256k1 documentation.
127 75 ▶use constant ECC_MAX_USES => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 sub ecc
130 4256 ▶{
Subroutine ecc: called CC=2 SCAR=6.9
Pod undocumented
131 4256 state $secp;
132 4256 state $used_times = ECC_MAX_USES;
133  
134 4256 ▶ if (++$used_times > ECC_MAX_USES) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$used_times > 100) 68 4188 4256
135 68 $secp = Bitcoin::Secp256k1->new;
136 68 $used_times = 0;
137 }
138  
139 4256 return $secp;
140 }
141  
142 sub standard_push
143 2007 ▶{
Subroutine standard_push: called CC=11 SCAR=26.9
Pod undocumented
144 2007 my ($opcode_name, $bytes) = @_;
145  
146 # standard push is not checked for opcodes that push constant data
147 2007 ▶ return !!1 if !$opcode_name || $opcode_name =~ /^OP_\d/;
Branch: 1 branch
BranchTrueFalseTotal
if not $opcode_name or $opcode_name =~ /^OP_\d/u 1124 883 2007
Condition100%3/3
not $opcode_name or $opcode_name =~ /^OP_\d/u
l!l&&r!l&&!r
131111883
MC/DC100%2/2
not $opcode_name or $opcode_name =~ /^OP_\d/u
not $opcode_name $opcode_name =~ /^OP_\d/u
Truth table100%3/3
not(A) or B
A= not $opcode_nameB= $opcode_name =~ /^OP_\d/u
ABresult
1X1
011
000
148  
149 883 ▶ if (length $bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes == 0) 0 883 883
150  
151 # empty vectors are only pushed by OP_0
152 0 return !!0;
153 }
154  
155 883 ▶ if (length $bytes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bytes == 1) 24 859 883
156 24 my $ord = ord $bytes;
157  
158 # anything up to 0x10 (excluding 0x00) and 0x81 has a special push
159 # opcode
160 24 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
unless $ord == 0 || $ord > 16 and $ord != 129 0 24 24
Condition50%3/6
$ord == 0 || $ord > 16 and $ord != 129
!ll&&!rl&&r
0024
$ord == 0 or $ord > 16
l!l&&r!l&&!r
14100
MC/DC0%0/3
$ord == 0 || $ord > 16 and $ord != 129
$ord == 0 $ord > 16 $ord != 129
Truth table40%2/5
A or B and C
A= $ord == 0B= $ord > 16C= $ord != 129
ABCresult
00X0
1X00
0100
1X11
0111
161 unless ($ord == 0x00 || $ord > 0x10)
162 && $ord != 0x81;
163 }
164  
165 883 ▶ if (length $bytes <= 75) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $bytes <= 75) { } 881 2 883
elsif (length $bytes < 256) { } 2 0 2
elsif (length $bytes < 65536) { } 0 0 0
166  
167 # byte lengths from 1 to 75 use OP_PUSH
168 881 return $opcode_name eq 'OP_PUSH';
169 }
170 elsif (length $bytes < (1 << 8)) {
171  
172 # byte lengths fitting on 1 byte use OP_PUSHDATA1
173 2 return $opcode_name eq 'OP_PUSHDATA1';
174 }
175 elsif (length $bytes < (1 << 16)) {
176  
177 # byte lengths fitting on 2 bytes use OP_PUSHDATA2
178 0 return $opcode_name eq 'OP_PUSHDATA2';
179 }
180 else {
181  
182 # any other push uses OP_PUSHDATA4
183 0 return $opcode_name eq 'OP_PUSHDATA4';
184 }
185 }
186  
187 sub check_strict_public_key
188 8 ▶{
Subroutine check_strict_public_key: called CC=6 SCAR=18.2
Pod undocumented
189 8 my ($pubkey) = @_;
190  
191 8 my $byte = unpack('C', $pubkey);
192  
193 8 ▶ return !!1 if length($pubkey) == 65 && $byte == 0x04;
Branch: 1 branch
BranchTrueFalseTotal
if length $pubkey == 65 and $byte == 4 1 7 8
Condition100%3/3
length $pubkey == 65 and $byte == 4
!ll&&!rl&&r
611
MC/DC100%2/2
length $pubkey == 65 and $byte == 4
length $pubkey == 65 $byte == 4
Truth table100%3/3
A and B
A= length $pubkey == 65B= $byte == 4
ABresult
0X0
100
111
194 7 ▶ return !!1 if length($pubkey) == 33 && ($byte == 0x03 || $byte == 0x02);
Branch: 1 branch
BranchTrueFalseTotal
if length $pubkey == 33 and $byte == 3 || $byte == 2 6 1 7
Condition50%3/6
length $pubkey == 33 and $byte == 3 || $byte == 2
!ll&&!rl&&r
106
$byte == 3 or $byte == 2
l!l&&r!l&&!r
600
MC/DC33%1/3
length $pubkey == 33 and $byte == 3 || $byte == 2
length $pubkey == 33 $byte == 3 $byte == 2
Truth table50%2/4
A and B or C
A= length $pubkey == 33B= $byte == 3C= $byte == 2
ABCresult
0XX0
1000
11X1
1011
195  
196 1 return !!0;
197 }
198  
199 # translated to Perl from:
200 # https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki#der-encoding-reference
201 sub check_strict_der_signature
202 136 ▶{
Subroutine check_strict_der_signature: called CC=15 SCAR=32.4
Pod undocumented
203 136 my ($signature) = @_;
204  
205 # NOTE: increment by 1 to take (stripped earlier) sighash into account
206 # without changing the algorithm
207 136 my $len = length($signature) + 1;
208  
209 136 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $len < 9 or $len > 73 0 136 136
Condition33%1/3
$len < 9 or $len > 73
l!l&&r!l&&!r
00136
MC/DC0%0/2
$len < 9 or $len > 73
$len < 9 $len > 73
Truth table33%1/3
A or B
A= $len < 9B= $len > 73
ABresult
1X1
011
000
210 if $len < 9 || $len > 73;
211  
212 136 my ($t1, $t2, $t3, $r_len, $s_len);
213 136 ($t1, $t2, $r_len) = unpack 'CC @3C', $signature;
214  
215 # check start and r length
216 136 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $t1 != 48 or $t2 != $len - 3 or $r_len + 5 >= $len 4 132 136
Condition66%4/6
$t1 != 48 or $t2 != $len - 3 or $r_len + 5 >= $len
l!l&&r!l&&!r
40132
$t1 != 48 or $t2 != $len - 3
l!l&&r!l&&!r
04132
MC/DC33%1/3
$t1 != 48 or $t2 != $len - 3 or $r_len + 5 >= $len
$t1 != 48 $t2 != $len - 3 $r_len + 5 >= $len
Truth table50%2/4
A or B or C
A= $t1 != 48B= $t2 != $len - 3C= $r_len + 5 >= $len
ABCresult
1XX1
01X1
0011
0000
217 if $t1 != 0x30
218 || $t2 != $len - 3
219 || $r_len + 5 >= $len;
220  
221 132 $s_len = unpack '@' . (5 + $r_len) . 'C', $signature;
222  
223 # check s length
224 132 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if not defined $s_len or $r_len + $s_len + 7 != $len 0 132 132
Condition33%1/3
not defined $s_len or $r_len + $s_len + 7 != $len
l!l&&r!l&&!r
00132
MC/DC0%0/2
not defined $s_len or $r_len + $s_len + 7 != $len
not defined $s_len $r_len + $s_len + 7 != $len
Truth table33%1/3
not(A) or B
A= not defined $s_lenB= $r_len + $s_len + 7 != $len
ABresult
1X1
011
000
225 if !defined $s_len || $r_len + $s_len + 7 != $len;
226  
227 132 for my $item ([$r_len, 2], [$s_len, $r_len + 4]) {
228 264 ($t1, $t2, $t3) = unpack '@0C @2C @3C', substr $signature, $item->[1];
229  
230 # check parts of r or s
231 264 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
if $t1 != 2 or $item->[0] == 0 or $t2 & 128 or $item->[0] > 1 and $t2 == 0 and not $t3 & 128 0 264 264
Condition46%7/15
$t1 != 2 or $item->[0] == 0 or $t2 & 128 or $item->[0] > 1 and $t2 == 0 and not $t3 & 128
l!l&&r!l&&!r
00264
$t1 != 2 or $item->[0] == 0 or $t2 & 128
l!l&&r!l&&!r
00264
$t1 != 2 or $item->[0] == 0
l!l&&r!l&&!r
00264
$item->[0] > 1 and $t2 == 0 and not $t3 & 128
!ll&&!rl&&r
192720
$item->[0] > 1 and $t2 == 0
!ll&&!rl&&r
019272
MC/DC0%0/6
$t1 != 2 or $item->[0] == 0 or $t2 & 128 or $item->[0] > 1 and $t2 == 0 and not $t3 & 128
$t1 != 2 $item->[0] == 0 $t2 & 128 $item->[0] > 1 $t2 == 0 not $t3 & 128
Truth table28%2/7
A or B or C or D and E and not(F)
A= $t1 != 2B= $item->[0] == 0C= $t2 & 128D= $item->[0] > 1E= $t2 == 0F= not $t3 & 128
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001111
0000XX0
00010X0
0001100
232 if $t1 != 0x02
233 || $item->[0] == 0
234 || $t2 & 0x80
235 || ($item->[0] > 1 && $t2 == 0 && !($t3 & 0x80));
236 }
237  
238 132 return !!1;
239 }
240  
241 # this does not fix low s, just strict encoding
242 sub make_strict_der_signature
243 7 ▶{
Subroutine make_strict_der_signature: called CC=10 SCAR=23.2
Pod undocumented
244 7 my ($signature) = @_;
245 7 ▶ return $signature unless length $signature >= 8;
Branch: 1 branch
BranchTrueFalseTotal
unless length $signature >= 8 4 3 7
246  
247 # https://bitcoin.stackexchange.com/questions/92680/what-are-the-der-signature-and-sec-format
248 # also:
249 # - ignore any trailing data
250 # - fix negative r and s
251  
252 3 my ($compound, $total_len, $int1, $r_len) = unpack 'aCaC', $signature;
253 3 my $r = substr $signature, 4, $r_len;
254  
255 3 $signature = substr $signature, 4 + $r_len;
256 3 my ($int2, $s_len) = unpack 'aC', $signature;
257 3 my $s = substr $signature, 2, $s_len;
258  
259 # remove padding
260 3 ▶ $r = substr($r, 1)
Branch: 1 branch
BranchTrueFalseTotal
length $r and unpack("C", $r) == 0 and do { $r = substr($r, 1) } 1 3 4
Condition100%3/3
length $r and unpack("C", $r) == 0
!ll&&!rl&&r
121
MC/DC100%2/2
length $r and unpack("C", $r) == 0
length $r unpack("C", $r) == 0
Truth table100%3/3
A and B
A= length $rB= unpack("C", $r) == 0
ABresult
0X0
100
111
261 while length $r && unpack('C', $r) == 0;
262 3 ▶ $s = substr($s, 1)
Branch: 1 branch
BranchTrueFalseTotal
length $s and unpack("C", $s) == 0 and do { $s = substr($s, 1) } 1 3 4
Condition100%3/3
length $s and unpack("C", $s) == 0
!ll&&!rl&&r
121
MC/DC100%2/2
length $s and unpack("C", $s) == 0
length $s unpack("C", $s) == 0
Truth table100%3/3
A and B
A= length $sB= unpack("C", $s) == 0
ABresult
0X0
100
111
263 while length $s && unpack('C', $s) == 0;
264  
265 # top bit may be 1, so prepend with zero to avoid being interpreted as
266 # negative
267 3 ▶ $r = "\x00$r"
Branch: 1 branch
BranchTrueFalseTotal
if length $r and unpack("C", $r) & 128 0 3 3
Condition66%2/3
length $r and unpack("C", $r) & 128
!ll&&!rl&&r
120
MC/DC0%0/2
length $r and unpack("C", $r) & 128
length $r unpack("C", $r) & 128
Truth table66%2/3
A and B
A= length $rB= unpack("C", $r) & 128
ABresult
0X0
100
111
268 if length $r && unpack('C', $r) & 0x80;
269 3 ▶ $s = "\x00$s"
Branch: 1 branch
BranchTrueFalseTotal
if length $s and unpack("C", $s) & 128 0 3 3
Condition66%2/3
length $s and unpack("C", $s) & 128
!ll&&!rl&&r
120
MC/DC0%0/2
length $s and unpack("C", $s) & 128
length $s unpack("C", $s) & 128
Truth table66%2/3
A and B
A= length $sB= unpack("C", $s) & 128
ABresult
0X0
100
111
270 if length $s && unpack('C', $s) & 0x80;
271  
272 # return extracted strict signature
273 3 return pack "aCaCa*aCa*",
274 $compound,
275 4 + length($r) + length($s),
276 $int1,
277 length($r),
278 $r,
279 $int2,
280 length($s),
281 $s;
282 }
283  
284 sub die_no_trace
285 21 ▶{
Subroutine die_no_trace: called CC=1 SCAR=0.0
Pod undocumented
286 21 die $_[0] . "\n";
287 }
288  
289 sub check_sighash
290 76 ▶{
Subroutine check_sighash: called CC=1 SCAR=0.0
Pod undocumented
291 76 my ($hashtype) = @_;
292  
293 state $allowed_sighash = {
294 76 map { $_ => 1 } (
295 SIGHASH_ALL,
296 SIGHASH_ALL | SIGHASH_ANYONECANPAY,
297 SIGHASH_SINGLE,
298 SIGHASH_SINGLE | SIGHASH_ANYONECANPAY,
299 SIGHASH_NONE,
300 SIGHASH_NONE | SIGHASH_ANYONECANPAY,
301 )
302 };
303  
304 76 return exists $allowed_sighash->{$hashtype};
305 }
306  
307 75 1;
308  
309 # Internal use only
310  
« Crypto/Transaction.pm ↑ Summary Crypto/Transaction/Digest.pm »