• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 14957620207

11 May 2025 04:23PM UTC coverage: 82.287% (-0.2%) from 82.469%
14957620207

push

github

web-flow
Merge pull request #1659 from evoskuil/master

Refactor sig operations, reintroduce multisighash caching.

54 of 124 new or added lines in 8 files covered. (43.55%)

9 existing lines in 6 files now uncovered.

10299 of 12516 relevant lines covered (82.29%)

3849629.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.74
/include/bitcoin/system/impl/machine/interpreter.ipp
1
/**
2
 * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#ifndef LIBBITCOIN_SYSTEM_MACHINE_INTERPRETER_IPP
20
#define LIBBITCOIN_SYSTEM_MACHINE_INTERPRETER_IPP
21

22
#include <iterator>
23
#include <utility>
24
#include <bitcoin/system/chain/chain.hpp>
25
#include <bitcoin/system/chain/operation.hpp>
26
#include <bitcoin/system/data/data.hpp>
27
#include <bitcoin/system/define.hpp>
28
#include <bitcoin/system/error/error.hpp>
29
#include <bitcoin/system/math/math.hpp>
30

31
// Push data revalidation (op.is_underclaimed()):
32
// Incorrectly-sized push data is validated upon op parse, setting an invalid
33
// opcode, the underflow bit, and the op data. This invalid opcode will result
34
// in script execution failure if the script is evaluated, but is otherwise
35
// valid (such as for a coinbase input script). Underflow can only occur at the
36
// end of a script. All bytes are valid script, but as scripts have finite
37
// length, the last operation may be an undersized push data. The retention of
38
// the undersized data within an invalid op allows for the script to be
39
// serialized and for the proper size and hash of the transaction computed.
40
// The invalid opcode with underflow data is serialized as data bytes with the
41
// original opcode stored in the data member, but seen here as op_xor.
42
// By design it is not possible to populate an op.data size that does not
43
// correspond to the op.code. Size mismatch is revalidated here as final
44
// insurance against derived classes that may alter this behavior. This ensures
45
// that an opcode that does not push correctly-sized data here will fail.
46

47
// TODO: In signing mode, prepare_signature converts key from a private key to
48
// a public key and generates the signature from key and hash. The signature is
49
// then verified against the key and hash as if obtained from the script.
50

51
namespace libbitcoin {
52
namespace system {
53
namespace machine {
54

55
// Operation handlers.
56
// ----------------------------------------------------------------------------
57

58
TEMPLATE
59
inline error::op_error_t CLASS::
162✔
60
op_unevaluated(opcode code) const NOEXCEPT
61
{
62
    return operation::is_invalid(code) ? error::op_invalid :
162✔
63
        error::op_reserved;
162✔
64
}
65

66
// TODO: nops_rule *must* be enabled in test cases and default config.
67
// TODO: cats_rule should be enabled in test cases and default config.
68
// Codes op_nop1..op_nop10 promoted from reserved by [0.3.6] hard fork.
69
TEMPLATE
70
inline error::op_error_t CLASS::
71
op_nop(opcode) const NOEXCEPT
72
{
73
    if (state::is_enabled(flags::nops_rule))
74
        return error::op_success;
75

76
    ////return op_unevaluated(code);
77
    return error::op_success;
78
}
79

80
TEMPLATE
81
inline error::op_error_t CLASS::
6,891✔
82
op_push_number(int8_t value) NOEXCEPT
83
{
84
    state::push_signed64(value);
85
    return error::op_success;
86
}
87

88
TEMPLATE
89
inline error::op_error_t CLASS::
4,364✔
90
op_push_size(const operation& op) NOEXCEPT
91
{
92
    if (op.is_underclaimed())
4,364✔
93
        return error::op_push_size;
94

95
    state::push_chunk(op.data_ptr());
4,364✔
96
    return error::op_success;
4,364✔
97
}
98

99
TEMPLATE
100
inline error::op_error_t CLASS::
20✔
101
op_push_one_size(const operation& op) NOEXCEPT
102
{
103
    if (op.is_underclaimed())
20✔
104
        return error::op_push_one_size;
105

106
    state::push_chunk(op.data_ptr());
20✔
107
    return error::op_success;
20✔
108
}
109

110
TEMPLATE
111
inline error::op_error_t CLASS::
86✔
112
op_push_two_size(const operation& op) NOEXCEPT
113
{
114
    if (op.is_underclaimed())
86✔
115
        return error::op_push_two_size;
116

117
    state::push_chunk(op.data_ptr());
86✔
118
    return error::op_success;
86✔
119
}
120

121
TEMPLATE
122
inline error::op_error_t CLASS::
4✔
123
op_push_four_size(const operation& op) NOEXCEPT
124
{
125
    if (op.is_underclaimed())
4✔
126
        return error::op_push_four_size;
127

128
    state::push_chunk(op.data_ptr());
4✔
129
    return error::op_success;
4✔
130
}
131

132
TEMPLATE
133
inline error::op_error_t CLASS::
134
op_nop() const NOEXCEPT
135
{
136
    return error::op_success;
137
}
138

139
// This opcode pushed the version to the stack, a hard fork per release.
140
TEMPLATE
141
inline error::op_error_t CLASS::
6✔
142
op_ver() const NOEXCEPT
143
{
144
    if (state::is_enabled(flags::nops_rule))
6✔
145
        return op_unevaluated(opcode::op_ver);
3✔
146

147
    return error::op_not_implemented;
148
}
149

150
TEMPLATE
151
inline error::op_error_t CLASS::
438✔
152
op_if() NOEXCEPT
153
{
154
    auto value = false;
438✔
155

156
    if (state::is_succeess())
438✔
157
    {
158
        if (state::is_stack_empty())
412✔
159
            return error::op_if1;
160

161
        if (!state::pop_bool_(value, state::is_enabled(flags::bip342_rule)))
162
            return error::op_if2;
163
    }
164

165
    state::begin_if(value);
436✔
166
    return error::op_success;
436✔
167
}
168

169
TEMPLATE
170
inline error::op_error_t CLASS::
40✔
171
op_notif() NOEXCEPT
172
{
173
    auto value = false;
40✔
174

175
    if (state::is_succeess())
40✔
176
    {
177
        if (state::is_stack_empty())
38✔
178
            return error::op_notif1;
179

180
        if (!state::pop_bool_(value, state::is_enabled(flags::bip342_rule)))
181
            return error::op_notif2;
182
    }
183

184
    state::begin_if(!value);
38✔
185
    return error::op_success;
38✔
186
}
187

188
TEMPLATE
189
inline error::op_error_t CLASS::
×
190
op_verif() const NOEXCEPT
191
{
192
    if (state::is_enabled(flags::nops_rule))
×
193
        return op_unevaluated(opcode::op_verif);
×
194

195
    return error::op_not_implemented;
196
}
197

198
TEMPLATE
199
inline error::op_error_t CLASS::
×
200
op_vernotif() const NOEXCEPT
201
{
202
    if (state::is_enabled(flags::nops_rule))
×
203
        return op_unevaluated(opcode::op_vernotif);
×
204

205
    return error::op_not_implemented;
206
}
207

208
TEMPLATE
209
inline error::op_error_t CLASS::
450✔
210
op_else() NOEXCEPT
211
{
212
    if (state::is_balanced())
450✔
213
        return error::op_else;
214

215
    state::else_if_();
216
    return error::op_success;
442✔
217
}
218

219
TEMPLATE
220
inline error::op_error_t CLASS::
322✔
221
op_endif() NOEXCEPT
222
{
223
    if (state::is_balanced())
322✔
224
        return error::op_endif;
225

226
    state::end_if_();
227
    return error::op_success;
308✔
228
}
229

230
TEMPLATE
231
inline error::op_error_t CLASS::
75✔
232
op_verify() NOEXCEPT
233
{
234
    if (state::is_stack_empty())
75✔
235
        return error::op_verify1;
236

237
    if (!state::peek_bool_())
73✔
238
        return error::op_verify2;
239

240
    state::drop_();
241
    return error::op_success;
71✔
242
}
243

244
TEMPLATE
245
inline error::op_error_t CLASS::
10✔
246
op_return() const NOEXCEPT
247
{
248
    if (state::is_enabled(flags::nops_rule))
10✔
249
        return op_unevaluated(opcode::op_return);
5✔
250
        
251
    return error::op_not_implemented;
252
}
253

254
TEMPLATE
255
inline error::op_error_t CLASS::
20✔
256
op_to_alt_stack() NOEXCEPT
257
{
258
    if (state::is_stack_empty())
20✔
259
        return error::op_to_alt_stack;
260

261
    state::push_alternate(state::pop_());
18✔
262
    return error::op_success;
18✔
263
}
264

265
TEMPLATE
266
inline error::op_error_t CLASS::
10✔
267
op_from_alt_stack() NOEXCEPT
268
{
269
    if (state::is_alternate_empty())
10✔
270
        return error::op_from_alt_stack;
271

272
    state::push_variant(state::pop_alternate_());
6✔
273
    return error::op_success;
6✔
274
}
275

276
TEMPLATE
277
inline error::op_error_t CLASS::
22✔
278
op_drop2() NOEXCEPT
279
{
280
    if (state::stack_size() < 2u)
22✔
281
        return error::op_drop2;
282

283
    // 0,1,[2,3] => 1,[2,3] => [2,3]
284
    state::drop_();
285
    state::drop_();
286
    return error::op_success;
20✔
287
}
288

289
TEMPLATE
290
inline error::op_error_t CLASS::
12✔
291
op_dup2() NOEXCEPT
292
{
293
    if (state::stack_size() < 2u)
12✔
294
        return error::op_dup2;
295

296
    // [0,1,2,3] => 1,[0,1,2,3] =>  0,1,[0,1,2,3]
297
    state::push_variant(state::peek_(1));
298
    state::push_variant(state::peek_(1));
299
    return error::op_success;
6✔
300
}
301

302
TEMPLATE
303
inline error::op_error_t CLASS::
3,292✔
304
op_dup3() NOEXCEPT
305
{
306
    if (state::stack_size() < 3u)
3,292✔
307
        return error::op_dup3;
308

309
    // [0,1,2,3] => 2,[0,1,2,3] => 1,2,[0,1,2,3] => 0,1,2,[0,1,2,3]
310
    state::push_variant(state::peek_(2));
311
    state::push_variant(state::peek_(2));
312
    state::push_variant(state::peek_(2));
313
    return error::op_success;
3,284✔
314
}
315

316
TEMPLATE
317
inline error::op_error_t CLASS::
10✔
318
op_over2() NOEXCEPT
319
{
320
    if (state::stack_size() < 4u)
10✔
321
        return error::op_over2;
322

323
    // [0,1,2,3] => 3,[0,1,2,3] => 2,3,[0,1,2,3]
324
    state::push_variant(state::peek_(3));
325
    state::push_variant(state::peek_(3));
326
    return error::op_success;
4✔
327
}
328

329
TEMPLATE
330
inline error::op_error_t CLASS::
26✔
331
op_rot2() NOEXCEPT
332
{
333
    if (state::stack_size() < 6u)
26✔
334
        return error::op_rot2;
335

336
    // [0,1,2,3,4,5] => [4,1,2,3,0,5] => [4,5,2,3,0,1] =>
337
    // [4,5,0,3,2,1] => [4,5,0,1,2,3]
338
    state::swap_(0, 4);
339
    state::swap_(1, 5);
340
    state::swap_(2, 4);
341
    state::swap_(3, 5);
342
    return error::op_success;
24✔
343
}
344

345
TEMPLATE
346
inline error::op_error_t CLASS::
10✔
347
op_swap2() NOEXCEPT
348
{
349
    if (state::stack_size() < 4u)
10✔
350
        return error::op_swap2;
351

352
    // [0,1,2,3] => [0,3,2,1] => [2,3,0,1]
353
    state::swap_(1, 3);
354
    state::swap_(0, 2);
355
    return error::op_success;
4✔
356
}
357

358
TEMPLATE
359
inline error::op_error_t CLASS::
10✔
360
op_if_dup() NOEXCEPT
361
{
362
    if (state::is_stack_empty())
10✔
363
        return error::op_if_dup;
364

365
    // [0,1,2] => 0,[0,1,2]
366
    if (state::peek_bool_())
6✔
367
        state::push_variant(state::peek_());
368

369
    return error::op_success;
370
}
371

372
TEMPLATE
373
inline error::op_error_t CLASS::
183✔
374
op_depth() NOEXCEPT
375
{
376
    // [0,1,2] => 3,[0,1,2]
377
    state::push_length(state::stack_size());
378
    return error::op_success;
183✔
379
}
380

381
TEMPLATE
382
inline error::op_error_t CLASS::
22✔
383
op_drop() NOEXCEPT
384
{
385
    if (state::is_stack_empty())
22✔
386
        return error::op_drop;
387

388
    // 0,[1,2] => [1,2]
389
    state::drop_();
390
    return error::op_success;
18✔
391
}
392

393
TEMPLATE
394
inline error::op_error_t CLASS::
35✔
395
op_dup() NOEXCEPT
396
{
397
    if (state::is_stack_empty())
35✔
398
        return error::op_dup;
399

400
    // [0,1,2] => 0,[0,1 2]
401
    state::push_variant(state::peek_());
402
    return error::op_success;
31✔
403
}
404

405
TEMPLATE
406
inline error::op_error_t CLASS::
12✔
407
op_nip() NOEXCEPT
408
{
409
    if (state::stack_size() < 2u)
12✔
410
        return error::op_nip;
411

412
    // [0,1,2] => 1,[0,2] => [0,2]
413
    state::swap_(0, 1);
414
    state::drop_();
415
    return error::op_success;
6✔
416
}
417

418
TEMPLATE
419
inline error::op_error_t CLASS::
12✔
420
op_over() NOEXCEPT
421
{
422
    if (state::stack_size() < 2u)
12✔
423
        return error::op_over;
424

425
    // [0,1] => 1,[0,1]
426
    state::push_variant(state::peek_(1));
427
    return error::op_success;
6✔
428
}
429

430
TEMPLATE
431
inline error::op_error_t CLASS::
26✔
432
op_pick() NOEXCEPT
433
{
434
    size_t index;
435

436
    // 2,[0,1,2,3] => {2} [0,1,2,3]
437
    if (!state::pop_index32(index))
26✔
438
        return error::op_pick;
439

440
    // [0,1,2,3] => 2,[0,1,2,3]
441
    state::push_variant(state::peek_(index));
442
    return error::op_success;
16✔
443
}
444

445
// ****************************************************************************
446
// Rock-and-ROLL:
447
// "The value stack can store a maximum of 1000 elements. The following script
448
// fills the stack and then moves each stack element 200 times, so the number
449
// of moved elements is 200K. This took almost 5.6 seconds in my test VM (for
450
// a block filled with these scriptSigs):
451
// 1 {999 times}, 998 OP_ROLL{ 200 times }"
452
// bitslog.com/2017/04/17/new-quadratic-delays-in-bitcoin-scripts
453
// Shifting larger chunks does not change time, as vector stores references.
454
// This remains in the current satoshi implementation (std_vector).
455
// ****************************************************************************
456
TEMPLATE
457
inline error::op_error_t CLASS::
24✔
458
op_roll() NOEXCEPT
459
{
460
    size_t index;
461

462
    // 998,[0,1,2,...,997,998,999] => {998} [0,1,2,...,997,998,999] 
463
    if (!state::pop_index32(index))
24✔
464
        return error::op_roll;
465

466
    // Copy variant because should be deleted before push (no stack alloc).
467
    stack_variant temporary{ state::peek_(index) };
16✔
468

469
    // Shifts maximum of n-1 references within vector of n.
470
    // [0,1,2,...,997,xxxx,999] => [0,1,2,...,997,999]
471
    state::erase_(index);
472

473
    // [0,1,2,...,997,999] => 998,[0,1,2,...,997,999]
474
    state::push_variant(std::move(temporary));
475
    return error::op_success;
16✔
476
}
477

478
TEMPLATE
479
inline error::op_error_t CLASS::
30✔
480
op_rot() NOEXCEPT
481
{
482
    if (state::stack_size() < 3u)
30✔
483
        return error::op_rot;
484

485
    // [0,1,2,3] = > [0,2,1,3] => [2,0,1,3]
486
    state::swap_(1, 2);
487
    state::swap_(0, 1);
488
    return error::op_success;
22✔
489
}
490

491
TEMPLATE
492
inline error::op_error_t CLASS::
20✔
493
op_swap() NOEXCEPT
494
{
495
    if (state::stack_size() < 2u)
20✔
496
        return error::op_swap;
497

498
    // [0,1,2] = > [1,0,2]
499
    state::swap_(0, 1);
500
    return error::op_success;
14✔
501
}
502

503
TEMPLATE
504
inline error::op_error_t CLASS::
12✔
505
op_tuck() NOEXCEPT
506
{
507
    if (state::stack_size() < 2u)
12✔
508
        return error::op_tuck;
509

510
    // [0,1,2] = > [1,0,2]  => 0,[1,0,2]
511
    state::swap_(0, 1);
512
    state::push_variant(state::peek_(1));
513
    return error::op_success;
6✔
514
}
515

516
TEMPLATE
517
inline error::op_error_t CLASS::
×
518
op_cat() const NOEXCEPT
519
{
520
    if (state::is_enabled(flags::cats_rule))
×
521
        return op_unevaluated(opcode::op_cat);
×
522

523
    return error::op_not_implemented;
524
}
525

526
TEMPLATE
527
inline error::op_error_t CLASS::
×
528
op_substr() const NOEXCEPT
529
{
530
    if (state::is_enabled(flags::cats_rule))
×
531
        return op_unevaluated(opcode::op_substr);
×
532

533
    return error::op_not_implemented;
534
}
535

536
TEMPLATE
537
inline error::op_error_t CLASS::
×
538
op_left() const NOEXCEPT
539
{
540
    if (state::is_enabled(flags::cats_rule))
×
541
        return op_unevaluated(opcode::op_left);
×
542

543
    return error::op_not_implemented;
544
}
545

546
TEMPLATE
547
inline error::op_error_t CLASS::
×
548
op_right() const NOEXCEPT
549
{
550
    if (state::is_enabled(flags::cats_rule))
×
551
        return op_unevaluated(opcode::op_right);
×
552

553
    return error::op_not_implemented;
554
}
555

556
TEMPLATE
557
inline error::op_error_t CLASS::
62✔
558
op_size() NOEXCEPT
559
{
560
    if (state::is_stack_empty())
62✔
561
        return error::op_size;
562

563
    state::push_length(state::peek_size());
564
    return error::op_success;
58✔
565
}
566

567
TEMPLATE
568
inline error::op_error_t CLASS::
×
569
op_invert() const NOEXCEPT
570
{
571
    if (state::is_enabled(flags::cats_rule))
×
572
        return op_unevaluated(opcode::op_invert);
×
573

574
    return error::op_not_implemented;
575
}
576

577
TEMPLATE
578
inline error::op_error_t CLASS::
×
579
op_and() const NOEXCEPT
580
{
581
    if (state::is_enabled(flags::cats_rule))
×
582
        return op_unevaluated(opcode::op_and);
×
583

584
    return error::op_not_implemented;
585
}
586

587
TEMPLATE
588
inline error::op_error_t CLASS::
×
589
op_or() const NOEXCEPT
590
{
591
    if (state::is_enabled(flags::cats_rule))
×
592
        return op_unevaluated(opcode::op_or);
×
593

594
    return error::op_not_implemented;
595
}
596

597
TEMPLATE
598
inline error::op_error_t CLASS::
×
599
op_xor() const NOEXCEPT
600
{
601
    if (state::is_enabled(flags::cats_rule))
×
602
        return op_unevaluated(opcode::op_xor);
×
603

604
    return error::op_not_implemented;
605
}
606

607
TEMPLATE
608
inline error::op_error_t CLASS::
485✔
609
op_equal() NOEXCEPT
610
{
611
    if (state::stack_size() < 2u)
485✔
612
        return error::op_equal;
613

614
    state::push_bool(state::equal_chunks(state::pop_(), state::pop_()));
966✔
615
    return error::op_success;
483✔
616
}
617

618
TEMPLATE
619
inline error::op_error_t CLASS::
79✔
620
op_equal_verify() NOEXCEPT
621
{
622
    if (state::stack_size() < 2u)
79✔
623
        return error::op_equal_verify1;
624

625
    return state::equal_chunks(state::pop_(), state::pop_()) ?
168✔
626
        error::op_success : error::op_equal_verify2;
627
}
628

629
TEMPLATE
630
inline error::op_error_t CLASS::
18✔
631
op_add1() NOEXCEPT
632
{
633
    int32_t number;
634
    if (!state::pop_signed32(number))
16✔
635
        return error::op_add1;
6✔
636

637
    state::push_signed64(add1<int64_t>(number));
12✔
638
    return error::op_success;
12✔
639
}
640

641
TEMPLATE
642
inline error::op_error_t CLASS::
10✔
643
op_sub1() NOEXCEPT
644
{
645
    int32_t number;
646
    if (!state::pop_signed32(number))
8✔
647
        return error::op_sub1;
6✔
648

649
    state::push_signed64(sub1<int64_t>(number));
4✔
650
    return error::op_success;
4✔
651
}
652

653
TEMPLATE
654
inline error::op_error_t CLASS::
×
655
op_mul2() const NOEXCEPT
656
{
657
    if (state::is_enabled(flags::cats_rule))
×
658
        return op_unevaluated(opcode::op_mul2);
×
659

660
    return error::op_not_implemented;
661
}
662

663
TEMPLATE
664
inline error::op_error_t CLASS::
×
665
op_div2() const NOEXCEPT
666
{
667
    if (state::is_enabled(flags::cats_rule))
×
668
        return op_unevaluated(opcode::op_div2);
×
669

670
    return error::op_not_implemented;
671
}
672

673
TEMPLATE
674
inline error::op_error_t CLASS::
10✔
675
op_negate() NOEXCEPT
676
{
677
    int32_t number;
678
    if (!state::pop_signed32(number))
8✔
679
        return error::op_negate;
4✔
680

681
    // negate(minimum) overflow precluded by domain increase.
682
    state::push_signed64(negate<int64_t>(number));
6✔
683
    return error::op_success;
6✔
684
}
685

686
TEMPLATE
687
inline error::op_error_t CLASS::
10✔
688
op_abs() NOEXCEPT
689
{
690
    int32_t number;
691
    if (!state::pop_signed32(number))
8✔
692
        return error::op_abs;
2✔
693

694
    // absolute(minimum) overflow precluded by domain increase.
695
    state::push_signed64(absolute<int64_t>(number));
8✔
696
    return error::op_success;
8✔
697
}
698

699
TEMPLATE
700
inline error::op_error_t CLASS::
52✔
701
op_not() NOEXCEPT
702
{
703
    int32_t number;
704
    if (!state::pop_signed32(number))
50✔
705
        return error::op_not;
4✔
706

707
    // Do not pop_bool() above, as number must be validated.
708
    state::push_bool(!to_bool(number));
48✔
709
    return error::op_success;
48✔
710
}
711

712
TEMPLATE
713
inline error::op_error_t CLASS::
12✔
714
op_nonzero() NOEXCEPT
715
{
716
    int32_t number;
717
    if (!state::pop_signed32(number))
10✔
718
        return error::op_nonzero;
2✔
719

720
    state::push_bool(is_nonzero(number));
10✔
721
    return error::op_success;
10✔
722
}
723

724
TEMPLATE
725
inline error::op_error_t CLASS::
72✔
726
op_add() NOEXCEPT
727
{
728
    int32_t right, left;
729
    if (!state::pop_binary32(left, right))
730
        return error::op_add;
6✔
731

732
    // addition overflow precluded by domain increase.
733
    state::push_signed64(add<int64_t>(left, right));
66✔
734
    return error::op_success;
66✔
735
}
736

737
TEMPLATE
738
inline error::op_error_t CLASS::
10✔
739
op_sub() NOEXCEPT
740
{
741
    int32_t right, left;
742
    if (!state::pop_binary32(left, right))
743
        return error::op_sub;
2✔
744

745
    // subtraction overflow precluded by domain increase.
746
    state::push_signed64(subtract<int64_t>(left, right));
8✔
747
    return error::op_success;
8✔
748
}
749

750
TEMPLATE
751
inline error::op_error_t CLASS::
×
752
op_mul() const NOEXCEPT
753
{
754
    if (state::is_enabled(flags::cats_rule))
×
755
        return op_unevaluated(opcode::op_mul);
×
756

757
    return error::op_not_implemented;
758
}
759

760
TEMPLATE
761
inline error::op_error_t CLASS::
×
762
op_div() const NOEXCEPT
763
{
764
    if (state::is_enabled(flags::cats_rule))
×
765
        return op_unevaluated(opcode::op_div);
×
766

767
    return error::op_not_implemented;
768
}
769

770
TEMPLATE
771
inline error::op_error_t CLASS::
×
772
op_mod() const NOEXCEPT
773
{
774
    if (state::is_enabled(flags::cats_rule))
×
775
        return op_unevaluated(opcode::op_mod);
×
776

777
    return error::op_not_implemented;
778
}
779

780
TEMPLATE
781
inline error::op_error_t CLASS::
×
782
op_lshift() const NOEXCEPT
783
{
784
    if (state::is_enabled(flags::cats_rule))
×
785
        return op_unevaluated(opcode::op_lshift);
×
786

787
    return error::op_not_implemented;
788
}
789

790
TEMPLATE
791
inline error::op_error_t CLASS::
×
792
op_rshift() const NOEXCEPT
793
{
794
    if (state::is_enabled(flags::cats_rule))
×
795
        return op_unevaluated(opcode::op_rshift);
×
796

797
    return error::op_not_implemented;
798
}
799

800
TEMPLATE
801
inline error::op_error_t CLASS::
14✔
802
op_bool_and() NOEXCEPT
803
{
804
    int32_t right, left;
805
    if (!state::pop_binary32(left, right))
806
        return error::op_bool_and;
2✔
807

808
    state::push_bool(to_bool(left) && to_bool(right));
12✔
809
    return error::op_success;
12✔
810
}
811

812
TEMPLATE
813
inline error::op_error_t CLASS::
14✔
814
op_bool_or() NOEXCEPT
815
{
816
    int32_t right, left;
817
    if (!state::pop_binary32(left, right))
818
        return error::op_bool_or;
2✔
819

820
    state::push_bool(to_bool(left) || to_bool(right));
12✔
821
    return error::op_success;
12✔
822
}
823

824
TEMPLATE
825
inline error::op_error_t CLASS::
38✔
826
op_num_equal() NOEXCEPT
827
{
828
    int32_t right, left;
829
    if (!state::pop_binary32(left, right))
830
        return error::op_num_equal;
4✔
831

832
    state::push_bool(left == right);
34✔
833
    return error::op_success;
34✔
834
}
835

836
TEMPLATE
837
inline error::op_error_t CLASS::
6✔
838
op_num_equal_verify() NOEXCEPT
839
{
840
    int32_t right, left;
841
    if (!state::pop_binary32(left, right))
842
        return error::op_num_equal_verify1;
2✔
843

844
    return (left == right) ? error::op_success : 
4✔
845
        error::op_num_equal_verify2;
846
}
847

848
TEMPLATE
849
inline error::op_error_t CLASS::
8✔
850
op_num_not_equal() NOEXCEPT
851
{
852
    int32_t right, left;
853
    if (!state::pop_binary32(left, right))
854
        return error::op_num_not_equal;
2✔
855

856
    state::push_bool(left != right);
6✔
857
    return error::op_success;
6✔
858
}
859

860
TEMPLATE
861
inline error::op_error_t CLASS::
14✔
862
op_less_than() NOEXCEPT
863
{
864
    int32_t right, left;
865
    if (!state::pop_binary32(left, right))
866
        return error::op_less_than;
2✔
867

868
    state::push_bool(is_lesser(left, right));
12✔
869
    return error::op_success;
12✔
870
}
871

872
TEMPLATE
873
inline error::op_error_t CLASS::
14✔
874
op_greater_than() NOEXCEPT
875
{
876
    int32_t right, left;
877
    if (!state::pop_binary32(left, right))
878
        return error::op_greater_than;
2✔
879

880
    state::push_bool(is_greater(left, right));
12✔
881
    return error::op_success;
12✔
882
}
883

884
TEMPLATE
885
inline error::op_error_t CLASS::
14✔
886
op_less_than_or_equal() NOEXCEPT
887
{
888
    int32_t right, left;
889
    if (!state::pop_binary32(left, right))
890
        return error::op_less_than_or_equal;
2✔
891

892
    state::push_bool(!is_greater(left, right));
12✔
893
    return error::op_success;
12✔
894
}
895

896
TEMPLATE
897
inline error::op_error_t CLASS::
14✔
898
op_greater_than_or_equal() NOEXCEPT
899
{
900
    int32_t right, left;
901
    if (!state::pop_binary32(left, right))
902
        return error::op_greater_than_or_equal;
2✔
903

904
    state::push_bool(!is_lesser(left, right));
12✔
905
    return error::op_success;
12✔
906
}
907

908
TEMPLATE
909
inline error::op_error_t CLASS::
12✔
910
op_min() NOEXCEPT
911
{
912
    int32_t right, left;
913
    if (!state::pop_binary32(left, right))
914
        return error::op_min;
2✔
915

916
    state::push_signed64(lesser(left, right));
10✔
917
    return error::op_success;
10✔
918
}
919

920
TEMPLATE
921
inline error::op_error_t CLASS::
12✔
922
op_max() NOEXCEPT
923
{
924
    int32_t right, left;
925
    if (!state::pop_binary32(left, right))
926
        return error::op_max;
2✔
927

928
    state::push_signed64(greater(left, right));
10✔
929
    return error::op_success;
10✔
930
}
931

932
TEMPLATE
933
inline error::op_error_t CLASS::
18✔
934
op_within() NOEXCEPT
935
{
936
    int32_t upper, lower, value;
937
    if (!state::pop_ternary32(upper, lower, value))
938
        return error::op_within;
2✔
939

940
    state::push_bool(!is_lesser(value, lower) && is_lesser(value, upper));
16✔
941
    return error::op_success;
16✔
942
}
943

944
TEMPLATE
945
inline error::op_error_t CLASS::
14✔
946
op_ripemd160() NOEXCEPT
947
{
948
    if (state::is_stack_empty())
14✔
949
        return error::op_ripemd160;
950

951
    state::push_chunk(rmd160_chunk(*state::pop_chunk_()));
10✔
952
    return error::op_success;
10✔
953
}
954

955
TEMPLATE
956
inline error::op_error_t CLASS::
92✔
957
op_sha1() NOEXCEPT
958
{
959
    if (state::is_stack_empty())
92✔
960
        return error::op_sha1;
961

962
    state::push_chunk(sha1_chunk(*state::pop_chunk_()));
88✔
963
    return error::op_success;
88✔
964
}
965

966
TEMPLATE
967
inline error::op_error_t CLASS::
18✔
968
op_sha256() NOEXCEPT
969
{
970
    if (state::is_stack_empty())
18✔
971
        return error::op_sha256;
972

973
    state::push_chunk(sha256_chunk(*state::pop_chunk_()));
14✔
974
    return error::op_success;
14✔
975
}
976

977
TEMPLATE
978
inline error::op_error_t CLASS::
49✔
979
op_hash160() NOEXCEPT
980
{
981
    if (state::is_stack_empty())
49✔
982
        return error::op_hash160;
983

984
    state::push_chunk(bitcoin_short_chunk(*state::pop_chunk_()));
45✔
985
    return error::op_success;
45✔
986
}
987

988
TEMPLATE
989
inline error::op_error_t CLASS::
14✔
990
op_hash256() NOEXCEPT
991
{
992
    if (state::is_stack_empty())
14✔
993
        return error::op_hash256;
994

995
    state::push_chunk(bitcoin_chunk(*state::pop_chunk_()));
10✔
996
    return error::op_success;
10✔
997
}
998

999
TEMPLATE
1000
inline error::op_error_t CLASS::
9✔
1001
op_codeseparator(const op_iterator& op) NOEXCEPT
1002
{
1003
    // Not thread safe for the script (changes script object metadata).
1004
    state::set_subscript(op);
1005
    return error::op_success;
1006
}
1007

1008
TEMPLATE
1009
inline error::op_error_t CLASS::
22✔
1010
op_check_sig() NOEXCEPT
1011
{
1012
    const auto ec = op_check_sig_verify();
22✔
1013
    if (ec == error::op_check_sig_empty_key)
22✔
1014
        return ec;
1015

1016
    // BIP66: if DER encoding invalid script MUST fail and end.
1017
    const auto bip66 = state::is_enabled(flags::bip66_rule);
1018
    if (bip66 && ec == error::op_check_sig_parse_signature)
22✔
1019
        return ec;
1020

1021
    state::push_bool(ec == error::op_success);
22✔
1022
    return error::op_success;
22✔
1023
}
1024

1025
TEMPLATE
1026
inline error::op_error_t CLASS::
26✔
1027
op_check_sig_verify() NOEXCEPT
1028
{
1029
    if (state::stack_size() < 2u)
26✔
1030
        return error::op_check_sig_verify1;
1031

1032
    const auto key = state::pop_chunk_();
1033
    const auto endorsement = state::pop_chunk_();
26✔
1034
    if (key->empty())
26✔
1035
        return error::op_check_sig_empty_key;
1036

1037
    // BIP342:
1038
    if (state::is_enabled(flags::bip342_rule))
26✔
1039
    {
1040
        // If signature is empty, script MUST fail and end (or push false).
1041
        if (endorsement->empty())
×
1042
            return error::op_check_sig_verify2;
1043

1044
        // If public key is 32 bytes it is a bip340 schnorr key.
1045
        // If signature is not empty, it is validated against public key.
1046
        if (key->size() == schnorr::public_key_size)
×
1047
        {
1048
            // Split signature and signature hash flags.
1049
            uint8_t sighash_flags;
NEW
1050
            const auto& sig = state::schnorr_split(sighash_flags, *endorsement);
×
NEW
1051
            if (sighash_flags == chain::coverage::invalid)
×
UNCOV
1052
                return error::op_check_sig_verify3;
×
1053

1054
            // Verify schnorr signature against public key and signature hash.
1055
            const auto hash = state::signature_hash(sighash_flags);
NEW
1056
            if (!schnorr::verify_signature(*key, hash, sig))
×
1057
                return error::op_check_sig_verify4;
1058

1059
            // If signature not empty, opcode counted toward sigops budget.
NEW
1060
            if (!state::sigops_increment())
×
1061
                return error::op_check_sig_verify5;
1062
        }
1063

1064
        // If signature not empty, opcode counted toward sigops budget.
1065
        if (!state::sigops_increment())
×
1066
            return error::op_check_sig_verify6;
1067

1068
        // If public key size is neither 0 nor 32 bytes, it is an unknown type.
1069
        // During script execution of signature opcodes these behave exactly as
1070
        // known types except that signature validation considered successful.
UNCOV
1071
        return error::op_success;
×
1072
    }
1073

1074
    if (endorsement->empty())
26✔
1075
        return error::op_check_sig_verify7;
1076

1077
    // Split endorsement into DER signature signature hash flags.
1078
    uint8_t sighash_flags;
1079
    const auto& der = state::ecdsa_split(sighash_flags, *endorsement);
26✔
1080
    const auto bip66 = state::is_enabled(flags::bip66_rule);
1081

1082
    // BIP66: if DER encoding invalid script MUST fail and end.
1083
    ec_signature sig;
1084
    if (!ecdsa::parse_signature(sig, der, bip66))
26✔
1085
        return error::op_check_sig_parse_signature;
1086

1087
    // Verify ECDSA signature against public key and signature hash.
1088
    const auto subscript = state::subscript(endorsement);
26✔
1089
    const auto hash = state::signature_hash(*subscript, sighash_flags);
26✔
1090
    if (!ecdsa::verify_signature(*key, hash, sig))
26✔
1091
        return error::op_check_sig_verify8;
6✔
1092

1093
    // TODO: use sighash and key to generate signature in sign mode.
1094
    return error::op_success;
1095
}
1096

1097
TEMPLATE
1098
inline error::op_error_t CLASS::
1,327✔
1099
op_check_multisig() NOEXCEPT
1100
{
1101
    if (state::is_enabled(flags::bip342_rule))
1,327✔
1102
        return op_unevaluated(opcode::checkmultisig);
×
1103

1104
    const auto ec = op_check_multisig_verify();
1,327✔
1105

1106
    // BIP66: if DER encoding invalid, script MUST fail and end.
1107
    const auto bip66 = state::is_enabled(flags::bip66_rule);
1108
    if (bip66 && ec == error::op_check_multisig_parse_signature)
1,327✔
1109
        return ec;
1110

1111
    state::push_bool(ec == error::op_success);
1,327✔
1112
    return error::op_success;
1,327✔
1113
}
1114

1115
TEMPLATE
1116
inline error::op_error_t CLASS::
2,058✔
1117
op_check_multisig_verify() NOEXCEPT
1118
{
1119
    if (state::is_enabled(flags::bip342_rule))
2,058✔
1120
        return op_unevaluated(opcode::checkmultisigverify);
×
1121

1122
    size_t count{};
1123
    if (!state::pop_index32(count))
2,058✔
1124
        return error::op_check_multisig_verify1;
1125

1126
    if (count > chain::max_script_public_keys)
2,058✔
1127
        return error::op_check_multisig_verify2;
1128

1129
    if (!state::ops_increment(count))
2,058✔
1130
        return error::op_check_multisig_verify3;
1131

1132
    chunk_xptrs keys;
1133
    if (!state::pop_chunks(keys, count))
1134
        return error::op_check_multisig_verify4;
1135

1136
    if (!state::pop_index32(count))
2,052✔
1137
        return error::op_check_multisig_verify5;
1138

1139
    if (count > keys.size())
2,050✔
1140
        return error::op_check_multisig_verify6;
1141

1142
    chunk_xptrs endorsements;
1143
    if (!state::pop_chunks(endorsements, count))
1144
        return error::op_check_multisig_verify7;
1145

1146
    if (state::is_stack_empty())
2,050✔
1147
        return error::op_check_multisig_verify8;
1148

1149
    //*************************************************************************
1150
    // CONSENSUS: Satoshi bug, discard stack element, malleable until bip147.
1151
    //*************************************************************************
1152
    const auto bip147 = state::is_enabled(flags::bip147_rule);
1153

1154
    // This check is unique in that a chunk must be empty to be false.
1155
    if (state::pop_strict_bool_() && bip147)
2,050✔
1156
        return error::op_check_multisig_verify9;
1157

1158
    cache_.first = true;
1,949✔
1159
    auto it = endorsements.begin();
1160
    const auto subscript = state::subscript(endorsements);
1,949✔
1161

1162
    // Keys may be empty.
1163
    for (const auto& key: keys)
1,969✔
1164
    {
1165
        // Implies that all signatures are valid.
1166
        if (it == endorsements.end())
246✔
1167
            break;
1168

1169
        // Empty endorsement does not increment iterator.
1170
        const auto endorsement = *it;
20✔
1171
        if (endorsement->empty())
20✔
1172
            continue;
×
1173

1174
        // Split endorsement into DER signature signature hash flags.
1175
        uint8_t sighash_flags;
1176
        const auto& der = state::ecdsa_split(sighash_flags, *endorsement);
20✔
1177
        const auto bip66 = state::is_enabled(flags::bip66_rule);
1178

1179
        // BIP66: if DER encoding invalid script MUST fail and end.
1180
        ec_signature sig;
1181
        if (!ecdsa::parse_signature(sig, der, bip66))
20✔
1182
            return error::op_check_sig_parse_signature;
1183

1184
        // Signature hash caching (bypass signature hash if same as previous).
1185
        if (cache_.first || cache_.flags != sighash_flags)
20✔
1186
        {
1187
            cache_.first = false;
10✔
1188
            cache_.flags = sighash_flags;
10✔
1189
            cache_.hash = state::signature_hash(*subscript, sighash_flags);
10✔
1190
        }
1191

1192
        // Verify ECDSA signature against public key and cache signature hash.
1193
        if (ecdsa::verify_signature(*key, cache_.hash, sig))
20✔
1194
            ++it;
1195
    }
1196

1197
    // All endorsements must be verified against a key.
1198
    if (it != endorsements.end())
1,949✔
1199
        return error::op_check_multisig_verify10;
1✔
1200
    
1201
    return error::op_success;
1202
}
1203

1204
TEMPLATE
1205
inline error::op_error_t CLASS::
27✔
1206
op_check_locktime_verify() const NOEXCEPT
1207
{
1208
    // BIP65: nop2 subsumed by checklocktimeverify when bip65 fork is active.
1209
    if (!state::is_enabled(flags::bip65_rule))
27✔
1210
        return op_nop(opcode::nop2);
1211

1212
    // The tx sequence is 0xffffffff.
1213
    if (state::input().is_final())
18✔
1214
        return error::op_check_locktime_verify1;
1215

1216
    // The stack is empty.
1217
    // The top stack item is negative.
1218
    // Extend the (signed) script number range to 5 bytes.
1219
    // The stack top is positive and 40 bits are usable.
1220
    uint64_t stack_locktime40;
1221
    if (!state::peek_unsigned40(stack_locktime40))
18✔
1222
        return error::op_check_locktime_verify2;
1223

1224
    const auto trans_locktime32 = state::tx().locktime();
7✔
1225
    using namespace chain;
1226

1227
    // The stack locktime type differs from that of tx.
1228
    if ((stack_locktime40 < locktime_threshold) !=
7✔
1229
        (trans_locktime32 < locktime_threshold))
7✔
1230
        return error::op_check_locktime_verify3;
1231

1232
    // The stack locktime is greater than the tx locktime.
1233
    if (stack_locktime40 > trans_locktime32)
5✔
1234
        return error::op_check_locktime_verify4;
1235

1236
    return error::op_success;
1237
}
1238

1239
TEMPLATE
1240
inline error::op_error_t CLASS::
4✔
1241
op_check_sequence_verify() const NOEXCEPT
1242
{
1243
    // BIP112: nop3 subsumed by checksequenceverify when bip112 fork is active.
1244
    if (!state::is_enabled(flags::bip112_rule))
4✔
1245
        return op_nop(opcode::nop3);
1246

1247
    // The stack is empty.
1248
    // The top stack item is negative.
1249
    // Extend the (signed) script number range to 5 bytes.
1250
    // The stack top is positive and 32 bits are used (33rd-40th discarded).
1251
    uint32_t stack_sequence32;
1252
    if (!state::peek_unsigned32(stack_sequence32))
×
1253
        return error::op_check_sequence_verify1;
1254

1255
    // Only 32 bits are tested.
1256
    const auto input_sequence32 = state::input().sequence();
×
1257
    using namespace chain;
1258

1259
    // The stack sequence is disabled, treat as nop3.
1260
    if (get_right(stack_sequence32, relative_locktime_disabled_bit))
×
1261
        return op_nop(opcode::nop3);
1262

1263
    // The stack sequence is enabled and tx version less than 2.
1264
    if (state::tx().version() < relative_locktime_min_version)
×
1265
        return error::op_check_sequence_verify2;
1266

1267
    // The transaction sequence is disabled.
1268
    if (get_right(input_sequence32, relative_locktime_disabled_bit))
×
1269
        return error::op_check_sequence_verify3;
1270

1271
    // The stack sequence type differs from that of tx input.
1272
    if (get_right(stack_sequence32, relative_locktime_time_locked_bit) !=
×
1273
        get_right(input_sequence32, relative_locktime_time_locked_bit))
×
1274
        return error::op_check_sequence_verify4;
1275

1276
    // The unmasked stack sequence is greater than that of tx sequence.
1277
    if (mask_left(stack_sequence32, relative_locktime_mask_left) >
×
1278
        mask_left(input_sequence32, relative_locktime_mask_left))
1279
        return error::op_check_sequence_verify5;
1280

1281
    return error::op_success;
1282
}
1283

1284
TEMPLATE
1285
inline error::op_error_t CLASS::
4✔
1286
op_check_sig_add() NOEXCEPT
1287
{
1288
    // BIP342: reserved_186 subsumed by op_checksigadd when tapscript active.
1289
    if (!state::is_enabled(flags::bip342_rule))
4✔
1290
        return op_unevaluated(opcode::reserved_186);
4✔
1291

1292
    // If fewer than 3 elements on stack, script MUST fail and end.
1293
    if (state::stack_size() < 3u)
×
1294
        return error::op_check_schnorr_sig1;
1295

1296
    // Public key (top) is popped.
1297
    const auto key = state::pop_chunk_();
1298

1299
    // If public key is empty, script MUST fail and end.
1300
    if (key->empty())
×
1301
        return error::op_check_schnorr_sig2;
1302

1303
    // Number (second to top) is popped.
1304
    // If number is larger than 4 bytes, script MUST fail and end.
1305
    int32_t number;
1306
    if (!state::pop_signed32_(number))
×
1307
        return error::op_check_schnorr_sig3;
1308

1309
    // Signature (third to top) is popped.
1310
    const auto endorsement = state::pop_chunk_();
1311

1312
    // If signature is empty, [number] pushed, execution continues.
1313
    if (endorsement->empty())
×
1314
    {
1315
        state::push_signed64(number);
×
1316
        return error::op_success;
×
1317
    }
1318

1319
    // Split signature and signature hash flags.
1320
    uint8_t sighash_flags;
NEW
1321
    const auto& sig = state::schnorr_split(sighash_flags, *endorsement);
×
NEW
1322
    if (sighash_flags == chain::coverage::invalid)
×
1323
        return error::op_check_schnorr_sig4;
1324

1325
    // Verify schnorr signature against public key and signature hash.
1326
    const auto hash = state::signature_hash(sighash_flags);
NEW
1327
    if (!schnorr::verify_signature(*key, hash, sig))
×
1328
        return error::op_check_schnorr_sig5;
1329

1330
    // If signature not empty, opcode counted toward sigops budget.
1331
    if (!state::sigops_increment())
×
1332
        return error::op_check_schnorr_sig6;
1333

1334
    // If signature not empty (and successful), [number+1] pushed.
1335
    state::push_signed64(add1<int64_t>(number));
×
1336
    return error::op_success;
×
1337
}
1338

1339
// Operation disatch.
1340
// ----------------------------------------------------------------------------
1341
// It is expected that the compiler will produce a very efficient jump table.
1342

1343
// private:
1344
TEMPLATE
1345
error::op_error_t CLASS::
21,322✔
1346
run_op(const op_iterator& op) NOEXCEPT
1347
{
1348
    const auto code = op->code();
21,322✔
1349

1350
    switch (code)
21,322✔
1351
    {
1352
        case opcode::push_size_0:
1353
            return op_push_number(0);
5,480✔
1354
        case opcode::push_size_1:
1355
        case opcode::push_size_2:
1356
        case opcode::push_size_3:
1357
        case opcode::push_size_4:
1358
        case opcode::push_size_5:
1359
        case opcode::push_size_6:
1360
        case opcode::push_size_7:
1361
        case opcode::push_size_8:
1362
        case opcode::push_size_9:
1363
        case opcode::push_size_10:
1364
        case opcode::push_size_11:
1365
        case opcode::push_size_12:
1366
        case opcode::push_size_13:
1367
        case opcode::push_size_14:
1368
        case opcode::push_size_15:
1369
        case opcode::push_size_16:
1370
        case opcode::push_size_17:
1371
        case opcode::push_size_18:
1372
        case opcode::push_size_19:
1373
        case opcode::push_size_20:
1374
        case opcode::push_size_21:
1375
        case opcode::push_size_22:
1376
        case opcode::push_size_23:
1377
        case opcode::push_size_24:
1378
        case opcode::push_size_25:
1379
        case opcode::push_size_26:
1380
        case opcode::push_size_27:
1381
        case opcode::push_size_28:
1382
        case opcode::push_size_29:
1383
        case opcode::push_size_30:
1384
        case opcode::push_size_31:
1385
        case opcode::push_size_32:
1386
        case opcode::push_size_33:
1387
        case opcode::push_size_34:
1388
        case opcode::push_size_35:
1389
        case opcode::push_size_36:
1390
        case opcode::push_size_37:
1391
        case opcode::push_size_38:
1392
        case opcode::push_size_39:
1393
        case opcode::push_size_40:
1394
        case opcode::push_size_41:
1395
        case opcode::push_size_42:
1396
        case opcode::push_size_43:
1397
        case opcode::push_size_44:
1398
        case opcode::push_size_45:
1399
        case opcode::push_size_46:
1400
        case opcode::push_size_47:
1401
        case opcode::push_size_48:
1402
        case opcode::push_size_49:
1403
        case opcode::push_size_50:
1404
        case opcode::push_size_51:
1405
        case opcode::push_size_52:
1406
        case opcode::push_size_53:
1407
        case opcode::push_size_54:
1408
        case opcode::push_size_55:
1409
        case opcode::push_size_56:
1410
        case opcode::push_size_57:
1411
        case opcode::push_size_58:
1412
        case opcode::push_size_59:
1413
        case opcode::push_size_60:
1414
        case opcode::push_size_61:
1415
        case opcode::push_size_62:
1416
        case opcode::push_size_63:
1417
        case opcode::push_size_64:
1418
        case opcode::push_size_65:
1419
        case opcode::push_size_66:
1420
        case opcode::push_size_67:
1421
        case opcode::push_size_68:
1422
        case opcode::push_size_69:
1423
        case opcode::push_size_70:
1424
        case opcode::push_size_71:
1425
        case opcode::push_size_72:
1426
        case opcode::push_size_73:
1427
        case opcode::push_size_74:
1428
        case opcode::push_size_75:
1429
            return op_push_size(*op);
4,364✔
1430
        case opcode::push_one_size:
1431
            return op_push_one_size(*op);
20✔
1432
        case opcode::push_two_size:
1433
            return op_push_two_size(*op);
86✔
1434
        case opcode::push_four_size:
1435
            return op_push_four_size(*op);
4✔
1436
        case opcode::push_negative_1:
1437
            return op_push_number(-1);
47✔
1438
        case opcode::reserved_80:
8✔
1439
            return op_unevaluated(code);
8✔
1440
        case opcode::push_positive_1:
1441
            return op_push_number(1);
911✔
1442
        case opcode::push_positive_2:
1443
            return op_push_number(2);
106✔
1444
        case opcode::push_positive_3:
1445
            return op_push_number(3);
63✔
1446
        case opcode::push_positive_4:
1447
            return op_push_number(4);
49✔
1448
        case opcode::push_positive_5:
1449
            return op_push_number(5);
34✔
1450
        case opcode::push_positive_6:
1451
            return op_push_number(6);
23✔
1452
        case opcode::push_positive_7:
1453
            return op_push_number(7);
14✔
1454
        case opcode::push_positive_8:
1455
            return op_push_number(8);
14✔
1456
        case opcode::push_positive_9:
1457
            return op_push_number(9);
8✔
1458
        case opcode::push_positive_10:
1459
            return op_push_number(10);
32✔
1460
        case opcode::push_positive_11:
1461
            return op_push_number(11);
54✔
1462
        case opcode::push_positive_12:
1463
            return op_push_number(12);
12✔
1464
        case opcode::push_positive_13:
1465
            return op_push_number(13);
8✔
1466
        case opcode::push_positive_14:
1467
            return op_push_number(14);
8✔
1468
        case opcode::push_positive_15:
1469
            return op_push_number(15);
10✔
1470
        case opcode::push_positive_16:
1471
            return op_push_number(16);
18✔
1472
        case opcode::nop:
1473
            return op_nop();
1474
        case opcode::op_ver:
6✔
1475
            return op_ver();
6✔
1476
        case opcode::if_:
438✔
1477
            return op_if();
438✔
1478
        case opcode::notif:
40✔
1479
            return op_notif();
40✔
1480
        case opcode::op_verif:
×
1481
            return op_verif();
×
1482
        case opcode::op_vernotif:
×
1483
            return op_vernotif();
×
1484
        case opcode::else_:
450✔
1485
            return op_else();
450✔
1486
        case opcode::endif:
322✔
1487
            return op_endif();
322✔
1488
        case opcode::verify:
75✔
1489
            return op_verify();
75✔
1490
        case opcode::op_return:
10✔
1491
            return op_return();
10✔
1492
        case opcode::toaltstack:
20✔
1493
            return op_to_alt_stack();
20✔
1494
        case opcode::fromaltstack:
10✔
1495
            return op_from_alt_stack();
10✔
1496
        case opcode::drop2:
22✔
1497
            return op_drop2();
22✔
1498
        case opcode::dup2:
12✔
1499
            return op_dup2();
12✔
1500
        case opcode::dup3:
3,292✔
1501
            return op_dup3();
3,292✔
1502
        case opcode::over2:
10✔
1503
            return op_over2();
10✔
1504
        case opcode::rot2:
26✔
1505
            return op_rot2();
26✔
1506
        case opcode::swap2:
10✔
1507
            return op_swap2();
10✔
1508
        case opcode::ifdup:
10✔
1509
            return op_if_dup();
10✔
1510
        case opcode::depth:
183✔
1511
            return op_depth();
183✔
1512
        case opcode::drop:
22✔
1513
            return op_drop();
22✔
1514
        case opcode::dup:
35✔
1515
            return op_dup();
35✔
1516
        case opcode::nip:
12✔
1517
            return op_nip();
12✔
1518
        case opcode::over:
12✔
1519
            return op_over();
12✔
1520
        case opcode::pick:
26✔
1521
            return op_pick();
26✔
1522
        case opcode::roll:
24✔
1523
            return op_roll();
24✔
1524
        case opcode::rot:
30✔
1525
            return op_rot();
30✔
1526
        case opcode::swap:
20✔
1527
            return op_swap();
20✔
1528
        case opcode::tuck:
12✔
1529
            return op_tuck();
12✔
1530
        case opcode::op_cat:
×
1531
            return op_cat();
×
1532
        case opcode::op_substr:
×
1533
            return op_substr();
×
1534
        case opcode::op_left:
×
1535
            return op_left();
×
1536
        case opcode::op_right:
×
1537
            return op_right();
×
1538
        case opcode::size:
62✔
1539
            return op_size();
62✔
1540
        case opcode::op_invert:
×
1541
            return op_invert();
×
1542
        case opcode::op_and:
×
1543
            return op_and();
×
1544
        case opcode::op_or:
×
1545
            return op_or();
×
1546
        case opcode::op_xor:
×
1547
            return op_xor();
×
1548
        case opcode::equal:
485✔
1549
            return op_equal();
485✔
1550
        case opcode::equalverify:
79✔
1551
            return op_equal_verify();
79✔
1552
        case opcode::reserved_137:
2✔
1553
            return op_unevaluated(code);
2✔
1554
        case opcode::reserved_138:
2✔
1555
            return op_unevaluated(code);
2✔
1556
        case opcode::add1:
18✔
1557
            return op_add1();
18✔
1558
        case opcode::sub1:
10✔
1559
            return op_sub1();
10✔
1560
        case opcode::op_mul2:
×
1561
            return op_mul2();
×
1562
        case opcode::op_div2:
×
1563
            return op_div2();
×
1564
        case opcode::negate:
10✔
1565
            return op_negate();
10✔
1566
        case opcode::abs:
10✔
1567
            return op_abs();
10✔
1568
        case opcode::not_:
52✔
1569
            return op_not();
52✔
1570
        case opcode::nonzero:
12✔
1571
            return op_nonzero();
12✔
1572
        case opcode::add:
72✔
1573
            return op_add();
72✔
1574
        case opcode::sub:
10✔
1575
            return op_sub();
10✔
1576
        case opcode::op_mul:
×
1577
            return op_mul();
×
1578
        case opcode::op_div:
×
1579
            return op_div();
×
1580
        case opcode::op_mod:
×
1581
            return op_mod();
×
1582
        case opcode::op_lshift:
×
1583
            return op_lshift();
×
1584
        case opcode::op_rshift:
×
1585
            return op_rshift();
×
1586
        case opcode::booland:
14✔
1587
            return op_bool_and();
14✔
1588
        case opcode::boolor:
14✔
1589
            return op_bool_or();
14✔
1590
        case opcode::numequal:
38✔
1591
            return op_num_equal();
38✔
1592
        case opcode::numequalverify:
6✔
1593
            return op_num_equal_verify();
6✔
1594
        case opcode::numnotequal:
8✔
1595
            return op_num_not_equal();
8✔
1596
        case opcode::lessthan:
14✔
1597
            return op_less_than();
14✔
1598
        case opcode::greaterthan:
14✔
1599
            return op_greater_than();
14✔
1600
        case opcode::lessthanorequal:
14✔
1601
            return op_less_than_or_equal();
14✔
1602
        case opcode::greaterthanorequal:
14✔
1603
            return op_greater_than_or_equal();
14✔
1604
        case opcode::min:
12✔
1605
            return op_min();
12✔
1606
        case opcode::max:
12✔
1607
            return op_max();
12✔
1608
        case opcode::within:
18✔
1609
            return op_within();
18✔
1610
        case opcode::ripemd160:
14✔
1611
            return op_ripemd160();
14✔
1612
        case opcode::sha1:
92✔
1613
            return op_sha1();
92✔
1614
        case opcode::sha256:
18✔
1615
            return op_sha256();
18✔
1616
        case opcode::hash160:
49✔
1617
            return op_hash160();
49✔
1618
        case opcode::hash256:
14✔
1619
            return op_hash256();
14✔
1620
        case opcode::codeseparator:
1621
            return op_codeseparator(op);
9✔
1622
        case opcode::checksig:
22✔
1623
            return op_check_sig();
22✔
1624
        case opcode::checksigverify:
4✔
1625
            return op_check_sig_verify();
4✔
1626
        case opcode::checkmultisig:
1,327✔
1627
            return op_check_multisig();
1,327✔
1628
        case opcode::checkmultisigverify:
731✔
1629
            return op_check_multisig_verify();
731✔
1630
        case opcode::nop1:
1631
            return op_nop(code);
1632
        case opcode::checklocktimeverify:
27✔
1633
            return op_check_locktime_verify();
27✔
1634
        case opcode::checksequenceverify:
4✔
1635
            return op_check_sequence_verify();
4✔
1636
        case opcode::nop4:
1637
        case opcode::nop5:
1638
        case opcode::nop6:
1639
        case opcode::nop7:
1640
        case opcode::nop8:
1641
        case opcode::nop9:
1642
        case opcode::nop10:
1643
            return op_nop(code);
1644
        case opcode::checksigadd:
4✔
1645
            return op_check_sig_add();
4✔
1646
        default:
138✔
1647
            return op_unevaluated(code);
138✔
1648
    }
1649
}
1650

1651
// Run the program.
1652
// ----------------------------------------------------------------------------
1653

1654
TEMPLATE
1655
code CLASS::
3,095✔
1656
run() NOEXCEPT
1657
{
1658
    // Enforce initial limits, determine early success or failure.
1659
    if (const auto ec = state::initialize())
1660
        return (ec == error::prevalid_script) ? error::script_success : ec;
124✔
1661

1662
    for (auto it = state::begin(); it != state::end(); ++it)
25,009✔
1663
    {
1664
        const auto& op = *it;
1665

1666
        if (op.is_oversized())
22,413✔
1667
            return error::invalid_push_data_size;
4✔
1668

1669
        if (!state::ops_increment(op))
22,409✔
1670
            return error::invalid_operation_count;
7✔
1671

1672
        if (state::if_(op))
21,152✔
1673
        {
1674
            if (const auto ec = run_op(it))
21,322✔
1675
                return ec;
422✔
1676

1677
            if (state::is_stack_overflow())
20,900✔
1678
                return error::invalid_stack_size;
4✔
1679
        }
1680
    }
1681

1682
    if (!state::is_balanced())
2,596✔
1683
        return error::invalid_stack_scope;
12✔
1684

1685
    return error::script_success;
2,584✔
1686
}
1687

1688
TEMPLATE
1689
code CLASS::
1,555✔
1690
connect(const chain::context& state, const chain::transaction& tx,
1691
    uint32_t index) NOEXCEPT
1692
{
1693
    if (index >= tx.inputs())
1,555✔
1694
        return error::inputs_overflow;
×
1695

1696
    return connect(state, tx, std::next(tx.inputs_ptr()->begin(), index));
3,110✔
1697
}
1698

1699
// TODO: Implement original op_codeseparator concatenation [< 0.3.6].
1700
// TODO: Implement combined script size limit soft fork (20,000) [0.3.6+].
1701
TEMPLATE
1702
code CLASS::
1,559✔
1703
connect(const chain::context& state, const chain::transaction& tx,
1704
    const input_iterator& it) NOEXCEPT
1705
{
1706
    using namespace chain;
1707
    const auto& input = **it;
1708
    if (!input.prevout)
1,559✔
1709
        return error::missing_previous_output;
×
1710

1711
    // Evaluate input script.
1712
    interpreter in_program(tx, it, state.flags);
1,559✔
1713
    if (const auto ec = in_program.run())
1,559✔
1714
        return ec;
63✔
1715

1716
    // Evaluate output script using stack copied from input script evaluation.
1717
    const auto& prevout = input.prevout->script_ptr();
1,496✔
1718
    interpreter out_program(in_program, prevout);
1,496✔
1719

1720
    if (auto ec = out_program.run())
1,496✔
1721
    {
1722
        return ec;
553✔
1723
    }
1724
    else if (!out_program.is_true(false))
1,054✔
1725
    {
1726
        return error::stack_false;
85✔
1727
    }
1728
    else if (prevout->is_pay_to_script_hash(state.flags))
969✔
1729
    {
1730
        // Because output script pushed script hash program [bip16].
1731
        if ((ec = connect_embedded(state, tx, it, in_program)))
21✔
1732
            return ec;
12✔
1733
    }
1734
    else if (prevout->is_pay_to_witness(state.flags))
948✔
1735
    {
1736
        // The input script must be empty [bip141].
1737
        if (!input.script().ops().empty())
19✔
1738
            return error::dirty_witness;
×
1739

1740
        // Because output script pushed version and witness program [bip141].
1741
        if ((ec = connect_witness(state, tx, it, *prevout)))
19✔
1742
            return ec;
6✔
1743
    }
1744
    else if (!input.witness().stack().empty())
929✔
1745
    {
1746
        // A non-witness program must have empty witness field [bip141].
1747
        return error::unexpected_witness;
8✔
1748
    }
1749

1750
    return error::script_success;
943✔
1751
}
1,559✔
1752

1753
TEMPLATE
1754
code CLASS::connect_embedded(const chain::context& state,
21✔
1755
    const chain::transaction& tx, const input_iterator& it,
1756
    interpreter& in_program) NOEXCEPT
1757
{
1758
    using namespace chain;
1759
    const auto& input = **it;
1760

1761
    // Input script is limited to relaxed push data operations [bip16].
1762
    if (!script::is_relaxed_push_pattern(input.script().ops()))
21✔
1763
        return error::invalid_script_embed;
4✔
1764

1765
    // Embedded script must be at the top of the stack [bip16].
1766
    // Evaluate embedded script using stack moved from input script.
1767
    const auto embedded = to_shared<script>(in_program.pop(), false);
17✔
1768
    interpreter out_program(std::move(in_program), embedded);
17✔
1769

1770
    if (auto ec = out_program.run())
17✔
1771
    {
1772
        return ec;
8✔
1773
    }
1774
    else if (!out_program.is_true(false))
13✔
1775
    {
1776
        return error::stack_false;
×
1777
    }
1778
    else if (embedded->is_pay_to_witness(state.flags))
13✔
1779
    {
1780
        // The input script must be a push of the embedded_script [bip141].
1781
        if (input.script().ops().size() != one)
5✔
1782
            return error::dirty_witness;
×
1783

1784
        // Because output script pushed version/witness program [bip141].
1785
        if ((ec = connect_witness(state, tx, it, *embedded)))
5✔
1786
            return ec;
2✔
1787
    }
1788
    else if (!input.witness().stack().empty())
8✔
1789
    {
1790
        // A non-witness program must have empty witness field [bip141].
1791
        return error::unexpected_witness;
2✔
1792
    }
1793

1794
    return error::script_success;
9✔
1795
}
17✔
1796

1797
TEMPLATE
1798
code CLASS::connect_witness(const chain::context& state,
24✔
1799
    const chain::transaction& tx, const input_iterator& it,
1800
    const chain::script& prevout) NOEXCEPT
1801
{
1802
    using namespace chain;
1803
    const auto& input = **it;
1804
    const auto version = prevout.version();
24✔
1805

1806
    switch (version)
24✔
1807
    {
1808
        case script_version::segwit:
24✔
1809
        {
1810
            script::cptr script;
24✔
1811
            chunk_cptrs_ptr stack;
24✔
1812
            if (!input.witness().extract_script(script, stack, prevout))
24✔
1813
                return error::invalid_witness;
1✔
1814

1815
            interpreter program(tx, it, script, state.flags, version, stack);
23✔
1816

1817
            if (const auto ec = program.run())
23✔
1818
            {
1819
                return ec;
2✔
1820
            }
1821
            else if (!program.is_true(true))
21✔
1822
            {
1823
                // A v0 script must succeed with a clean true stack [bip141].
1824
                return error::stack_false;
5✔
1825
            }
1826
            else
1827
            {
1828
                return error::script_success;
16✔
1829
            }
1830
        }
23✔
1831

1832
        ///////////////////////////////////////////////////////////////////////
1833
        // TODO: properly extract tapscript/taproot [bip341].
1834
        ///////////////////////////////////////////////////////////////////////
1835
        case script_version::taproot:
×
1836
        {
1837
            script::cptr script;
×
1838
            chunk_cptrs_ptr stack;
×
1839
            if (!input.witness().extract_script(script, stack, prevout))
×
1840
                return error::invalid_witness;
×
1841

1842
            const auto flags = state.flags;
×
1843
            const auto size = input.witness().serialized_size(true);
×
1844
            interpreter program(tx, it, script, flags, version, stack, size);
×
1845

1846
            if (const auto ec = program.run())
×
1847
            {
1848
                return ec;
×
1849
            }
1850
            else if (!program.is_true(true))
×
1851
            {
1852
                // A v1 script must succeed with a clean true stack [bip342].
1853
                return error::stack_false;
×
1854
            }
1855
            else
1856
            {
1857
                return error::script_success;
×
1858
            }
UNCOV
1859
        }
×
1860

1861
        // These versions are reserved for future extensions [bip141].
1862
        case script_version::reserved:
1863
            return error::script_success;
×
1864

1865
        case script_version::unversioned:
1866
        default:
1867
            return error::unversioned_script;
×
1868
    }
1869
}
1870

1871
} // namespace machine
1872
} // namespace system
1873
} // namespace libbitcoin
1874

1875
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc