Crypto/Script/Recognition.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 92.0% 46 / 50 Conditioncond 89.4% 17 / 19 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 1 totaltotal 94.5% 208 / 220 CC 37 SCAR 22.1
CC
37
Cov
96%
_check_blueprint_segment
19.0
__ANON__
8.0
check
6.0
CRAP 9.1
« Crypto/Transaction/Digest.pm ↑ Summary Crypto/Key/ExtBase.pm »
1 package Bitcoin::Crypto::Script::Recognition;
2 68 $Bitcoin::Crypto::Script::Recognition::VERSION = '4.006';
3 68 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 68 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 68 ▶use Bitcoin::Crypto::Helpers qw(standard_push);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 68 ▶use Bitcoin::Crypto::Script::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 68 ▶use Bitcoin::Crypto::Tapscript::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 68 ▶use Bitcoin::Crypto::Script::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 68 has param 'type' => (
14 default => undef,
15 );
16  
17 # one or more, depending on the script type
18 68 has param 'public_key' => (
19 default => undef,
20 );
21  
22 68 has param 'segwit_version' => (
23 default => undef,
24 );
25  
26 68 has param 'required_sigs' => (
27 default => undef,
28 );
29  
30 68 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 68 KIND_OPCODE => 1,
32 KIND_NUMBER => 2,
33 KIND_DATA => 3,
34 KIND_REPEATED => 4,
35 KIND_CHECK => 5,
36 68 };
37  
38 sub _blueprints
39 468 ▶{
Subroutine _blueprints: called CC=2 SCAR=7.0
40 468 my ($class) = @_;
41  
42 468 state $blueprints = {};
43 468 ▶ return $blueprints->{$class} //= $class->_build_blueprints;
Condition66%2/3
$blueprints->{$class} //= $class->_build_blueprints
l!l&&r!l&&!r
438300
MC/DC0%0/2
$blueprints->{$class} //= $class->_build_blueprints
$blueprints->{$class} $class->_build_blueprints
Truth table66%2/3
A //= B
A= $blueprints->{$class}B= $class->_build_blueprints
ABresult
1X1
011
000
44 }
45  
46 sub _build_blueprints
47 30 ▶{
Subroutine _build_blueprints: called CC=3 SCAR=11.0
48 # blueprints for standard transaction types
49 # constant size script types should be placed first so they are thrown away sooner
50 # more common script types should be placed first so they can be found faster
51 my @blueprints = (
52 [
53 'P2TR',
54 [KIND_NUMBER, 'segwit_version', 1],
55 [KIND_DATA, 'public_key', 32],
56 51 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 51 $_[0]->{vars}{required_sigs} = 1;
58 }
59 ],
60  
61 [
62 'P2WPKH',
63 [KIND_NUMBER, 'segwit_version', 0],
64 [KIND_DATA, 'public_key', 20],
65 114 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 114 $_[0]->{vars}{required_sigs} = 1;
67 }
68 ],
69  
70 [
71 'P2WSH',
72 [KIND_NUMBER, 'segwit_version', 0],
73 [KIND_DATA, 'public_key', 32],
74 ],
75  
76 [
77 'P2PKH',
78 'OP_DUP',
79 'OP_HASH160',
80 [KIND_DATA, 'public_key', 20],
81 'OP_EQUALVERIFY',
82 'OP_CHECKSIG',
83 22 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 22 $_[0]->{vars}{required_sigs} = 1;
85 }
86 ],
87  
88 [
89 'P2SH',
90 'OP_HASH160',
91 [KIND_DATA, 'public_key', 20],
92 'OP_EQUAL',
93 ],
94  
95 [
96 'P2PK',
97 [KIND_DATA, 'public_key', 33, 65],
98 'OP_CHECKSIG',
99 15 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 15 $_[0]->{vars}{required_sigs} = 1;
101 }
102 ],
103  
104 [
105 'UNKNOWN_SEGWIT',
106 [KIND_NUMBER, 'segwit_version', 0 .. 16],
107 [KIND_DATA, 'public_key', 2 .. 40],
108 ],
109  
110 [
111 'NULLDATA',
112 'OP_RETURN',
113 [KIND_DATA, 'public_key', 1 .. 80],
114 ],
115  
116 [
117 'P2MS',
118 [KIND_NUMBER, 'required_sigs', 0 .. 20],
119 [
120 KIND_REPEATED,
121 [KIND_DATA, 'public_keys', 33, 65],
122 ],
123 [KIND_NUMBER, 'pubkeys_number', 0 .. 20],
124 28 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
125 28 ▶ $_[0]->{vars}{public_keys} //= [];
Condition100%2/2
$_[0]{"vars"}{"public_keys"} //= []
l!l
262
MC/DC100%1/1
$_[0]{"vars"}{"public_keys"} //= []
$_[0]{"vars"}{"public_keys"}
Truth table100%2/2
A
A= $_[0]{"vars"}{"public_keys"}
Aresult
11
00
126 28 $_[0]->{vars}{pubkeys_number} == @{$_[0]->{vars}{public_keys}}
127 28 ▶ && $_[0]->{vars}{pubkeys_number} >= $_[0]->{vars}{required_sigs};
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"vars"}{"pubkeys_number"} == @{$_[0]{"vars"}{"public_keys"};} and $_[0]{"vars"}{"pubkeys_number"} >= $_[0]{"vars"}{"required_sigs"} 28 0 28
128 },
129 'OP_CHECKMULTISIG',
130 ],
131  
132 [
133 ['TAPSCRIPT_MULTISIG', 'Bitcoin::Crypto::Tapscript::Opcode'],
134 [KIND_DATA, 'first_public_key', 32],
135 'OP_CHECKSIG',
136 [
137 KIND_REPEATED,
138 [KIND_DATA, 'public_keys', 32],
139 'OP_CHECKSIGADD',
140 ],
141 [KIND_NUMBER, 'required_sigs', 0 .. 100], # arbitrary max number of signatures
142 'OP_NUMEQUAL',
143 12 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
144 12 ▶ $_[0]->{vars}{public_keys} //= [];
Condition100%2/2
$_[0]{"vars"}{"public_keys"} //= []
l!l
75
MC/DC100%1/1
$_[0]{"vars"}{"public_keys"} //= []
$_[0]{"vars"}{"public_keys"}
Truth table100%2/2
A
A= $_[0]{"vars"}{"public_keys"}
Aresult
11
00
145 12 unshift @{$_[0]->{vars}{public_keys}}, $_[0]->{vars}{first_public_key};
146 12 @{$_[0]->{vars}{public_keys}} >= $_[0]->{vars}{required_sigs};
147 },
148 30 ],
149 );
150  
151 30 my $in_repeat_group;
152 my $opcode_class;
153 30 my $preprocess_parts;
154 360 ▶ $preprocess_parts = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
155 360 my @parts = @_;
156  
157 360 foreach my $part (@parts) {
158 1140 ▶ if (ref $part eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $part eq 'ARRAY') { } 600 540 1140
elsif (ref $part eq 'CODE') { } 180 360 540
159 600 my ($kind, @vars) = @$part;
160  
161 600 ▶ if ($kind == KIND_DATA || $kind == KIND_NUMBER) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind == 3 or $kind == 2) { } 540 60 600
elsif ($kind == 4) { } 60 0 60
Condition100%3/3
$kind == 3 or $kind == 2
l!l&&r!l&&!r
33021060
MC/DC100%2/2
$kind == 3 or $kind == 2
$kind == 3 $kind == 2
Truth table100%3/3
A or B
A= $kind == 3B= $kind == 2
ABresult
1X1
011
000
162 540 my $name = shift @vars;
163  
164 540 ▶ @vars = map { Bitcoin::Crypto::Script::Runner->from_int($_) } @vars
Branch: 1 branch
BranchTrueFalseTotal
if $kind == 2 210 330 540
165 if $kind == KIND_NUMBER;
166  
167 540 my %lookup = map { $_ => !!1 } @vars;
168 540 $part = [$kind, $name, \%lookup, !!$in_repeat_group];
169 }
170 elsif ($kind == KIND_REPEATED) {
171 60 my $old_repeat_group = $in_repeat_group;
172 60 $in_repeat_group = !!1;
173 60 $part = [$kind, [$preprocess_parts->(@vars)]];
174 60 $in_repeat_group = $old_repeat_group;
175 }
176 else {
177 0 die "invalid blueprint kind: $kind";
178 }
179 }
180 elsif (ref $part eq 'CODE') {
181 180 $part = [KIND_CHECK, $part];
182 }
183 else {
184 360 my $opcode = $opcode_class->get_opcode_by_name($part);
185 360 $part = [KIND_OPCODE, $opcode->code];
186 }
187 }
188  
189 360 return @parts;
190 30 };
191  
192 # pre-process blueprints for faster execution
193 30 foreach my $variant (@blueprints) {
194 300 my ($type, @parts) = @$variant;
195  
196 300 ▶ if (ref $type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'ARRAY') { } 30 270 300
197 30 ($type, $opcode_class) = @{$type};
198 }
199 else {
200 270 $opcode_class = 'Bitcoin::Crypto::Script::Opcode';
201 }
202  
203 300 $variant = [$type, [$preprocess_parts->(@parts)]];
204 }
205  
206 30 return \@blueprints;
207 }
208  
209 sub _check_blueprint_segment
210 2095 ▶{
Subroutine _check_blueprint_segment: called CC=19 SCAR=29.4
211 2095 my ($ops, $parts, $state) = @_;
212  
213 2095 foreach my $part (@{$parts}) {
214 3252 my ($kind, @args) = @{$part};
215  
216 3252 ▶ if ($kind == KIND_OPCODE || $kind == KIND_DATA || $kind == KIND_NUMBER) {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind == 1 or $kind == 3 or $kind == 2) { } 2951 301 3252
elsif ($kind == 4) { } 59 242 301
elsif ($kind == 5) { } 242 0 242
Condition100%6/6
$kind == 1 or $kind == 3 or $kind == 2
l!l&&r!l&&!r
15221429301
$kind == 1 or $kind == 3
l!l&&r!l&&!r
6039191730
MC/DC100%3/3
$kind == 1 or $kind == 3 or $kind == 2
$kind == 1 $kind == 3 $kind == 2
Truth table100%4/4
A or B or C
A= $kind == 1B= $kind == 3C= $kind == 2
ABCresult
1XX1
01X1
0011
0000
217 2951 my $op_data = $ops->[$state->{pos}];
218 2951 ▶ return 0 unless $op_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data 39 2912 2951
219  
220 2912 ▶ if ($kind == KIND_OPCODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind == 1) { } 603 2309 2912
221 603 ▶ return 0 unless $args[0] == $op_data->[0]->code;
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0] == $op_data->[0]->code 329 274 603
222 }
223 else {
224 2309 ▶ return 0 unless $op_data->[0]->pushop;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_data->[0]->pushop 338 1971 2309
225 1971 ▶ return 0 unless standard_push($op_data->[0]->name, $op_data->[2]);
Branch: 1 branch
BranchTrueFalseTotal
unless standard_push($op_data->[0]->name, $op_data->[2]) 10 1961 1971
226  
227 1961 my ($name, $lookup, $in_repeat_group) = @args;
228 1961 my $value;
229  
230 1961 ▶ if ($kind == KIND_DATA) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind == 3) { } 828 1133 1961
elsif ($kind == 2) { } 1133 0 1133
231 828 ▶ return 0 unless $lookup->{length $op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{length $op_data->[2]} 293 535 828
232  
233 535 $value = $op_data->[2];
234 }
235 elsif ($kind == KIND_NUMBER) {
236 1133 ▶ return 0 unless $lookup->{$op_data->[2]};
Branch: 1 branch
BranchTrueFalseTotal
unless $$lookup{$op_data->[2]} 523 610 1133
237  
238 # numify bigint on 32 bit arch
239 610 $value = '' . Bitcoin::Crypto::Script::Runner->to_int($op_data->[2]);
240 }
241  
242 1145 ▶ if ($in_repeat_group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_repeat_group) { } 110 1035 1145
243 110 push @{$state->{vars}{$name}}, $value;
244 }
245 else {
246 1035 $state->{vars}{$name} = $value;
247 }
248 }
249  
250 1419 $state->{pos} += 1;
251 }
252 elsif ($kind == KIND_REPEATED) {
253 59 while (1) {
254  
255 # state copy avoids having temporary data stored inside
256 # state if repeated part fails, as well as rewinding the
257 # position to the last one which was valid
258 169 my %state_cpy = %{$state};
259 169 ▶ last unless _check_blueprint_segment($ops, $args[0], \%state_cpy);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_blueprint_segment($ops, $args[0], \%state_cpy) 59 110 169
260  
261 # commit state copy back into state (since matching succeded)
262 110 %{$state} = %state_cpy;
263 }
264 }
265 elsif ($kind == KIND_CHECK) {
266 242 ▶ return 0 unless $args[0]->($state);
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0]->($state) 2 240 242
267 }
268 }
269  
270 561 return 1;
271 }
272  
273 sub check
274 475 ▶{
Subroutine check: called CC=6 SCAR=17.9
Pod undocumented
275 475 my ($class, $script) = @_;
276  
277 475 my $compiler = $script->_compiler;
278 475 ▶ if (!$compiler->has_errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compiler->has_errors) 468 7 475
279 468 my $ops = $compiler->operations;
280  
281 468 foreach my $blueprint (@{$class->_blueprints}) {
282 1926 my %state = (
283 pos => 0,
284 vars => {},
285 );
286  
287 1926 ▶ next unless _check_blueprint_segment($ops, $blueprint->[1], \%state);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_blueprint_segment($ops, $blueprint->[1], \%state) 1475 451 1926
288 451 ▶ next unless $state{pos} == @{$ops};
Branch: 1 branch
BranchTrueFalseTotal
unless $state{"pos"} == @{$ops;} 31 420 451
289  
290 return $class->new(
291 type => $blueprint->[0],
292 public_key => $state{vars}{public_keys} // $state{vars}{public_key},
293 segwit_version => $state{vars}{segwit_version},
294 required_sigs => $state{vars}{required_sigs},
295 420 ▶ );
Condition66%2/3
$state{"vars"}{"public_keys"} // $state{"vars"}{"public_key"}
l!l&&r!l&&!r
383820
MC/DC0%0/2
$state{"vars"}{"public_keys"} // $state{"vars"}{"public_key"}
$state{"vars"}{"public_keys"} $state{"vars"}{"public_key"}
Truth table66%2/3
A // B
A= $state{"vars"}{"public_keys"}B= $state{"vars"}{"public_key"}
ABresult
1X1
011
000
296 }
297 }
298  
299 # unknown
300 55 return $class->new;
301 }
302  
303 68 1;
304  
« Crypto/Transaction/Digest.pm ↑ Summary Crypto/Key/ExtBase.pm »