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

libbitcoin / libbitcoin-system / 15078892484

16 May 2025 11:04PM UTC coverage: 81.885% (-0.3%) from 82.18%
15078892484

push

github

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

Refactor all signature_hash methods to return bool (tapscript).

293 of 491 new or added lines in 17 files covered. (59.67%)

5 existing lines in 3 files now uncovered.

10365 of 12658 relevant lines covered (81.88%)

3781254.62 hits per line

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

76.16
/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
namespace libbitcoin {
32
namespace system {
33
namespace machine {
34

35
// Operation handlers.
36
// ----------------------------------------------------------------------------
37

38
TEMPLATE
39
inline error::op_error_t CLASS::
162✔
40
op_unevaluated(opcode code) const NOEXCEPT
41
{
42
    return operation::is_invalid(code) ? error::op_invalid :
162✔
43
        error::op_reserved;
162✔
44
}
45

46
// TODO: nops_rule *must* be enabled in test cases and default config.
47
// TODO: cats_rule should be enabled in test cases and default config.
48
// Codes op_nop1..op_nop10 promoted from reserved by [0.3.6] hard fork.
49
TEMPLATE
50
inline error::op_error_t CLASS::
51
op_nop(opcode) const NOEXCEPT
52
{
53
    if (state::is_enabled(flags::nops_rule))
54
        return error::op_success;
55

56
    ////return op_unevaluated(code);
57
    return error::op_success;
58
}
59

60
TEMPLATE
61
inline error::op_error_t CLASS::
6,891✔
62
op_push_number(int8_t value) NOEXCEPT
63
{
64
    state::push_signed64(value);
65
    return error::op_success;
66
}
67

68
TEMPLATE
69
inline error::op_error_t CLASS::
4,364✔
70
op_push_size(const operation& op) NOEXCEPT
71
{
72
    if (op.is_underclaimed())
4,364✔
73
        return error::op_push_size;
74

75
    state::push_chunk(op.data_ptr());
4,364✔
76
    return error::op_success;
4,364✔
77
}
78

79
TEMPLATE
80
inline error::op_error_t CLASS::
20✔
81
op_push_one_size(const operation& op) NOEXCEPT
82
{
83
    if (op.is_underclaimed())
20✔
84
        return error::op_push_one_size;
85

86
    state::push_chunk(op.data_ptr());
20✔
87
    return error::op_success;
20✔
88
}
89

90
TEMPLATE
91
inline error::op_error_t CLASS::
86✔
92
op_push_two_size(const operation& op) NOEXCEPT
93
{
94
    if (op.is_underclaimed())
86✔
95
        return error::op_push_two_size;
96

97
    state::push_chunk(op.data_ptr());
86✔
98
    return error::op_success;
86✔
99
}
100

101
TEMPLATE
102
inline error::op_error_t CLASS::
4✔
103
op_push_four_size(const operation& op) NOEXCEPT
104
{
105
    if (op.is_underclaimed())
4✔
106
        return error::op_push_four_size;
107

108
    state::push_chunk(op.data_ptr());
4✔
109
    return error::op_success;
4✔
110
}
111

112
TEMPLATE
113
inline error::op_error_t CLASS::
114
op_nop() const NOEXCEPT
115
{
116
    return error::op_success;
117
}
118

119
// This opcode pushed the version to the stack, a hard fork per release.
120
TEMPLATE
121
inline error::op_error_t CLASS::
6✔
122
op_ver() const NOEXCEPT
123
{
124
    if (state::is_enabled(flags::nops_rule))
6✔
125
        return op_unevaluated(opcode::op_ver);
3✔
126

127
    return error::op_not_implemented;
128
}
129

130
TEMPLATE
131
inline error::op_error_t CLASS::
438✔
132
op_if() NOEXCEPT
133
{
134
    auto value = false;
438✔
135

136
    if (state::is_succeess())
438✔
137
    {
138
        if (state::is_stack_empty())
412✔
139
            return error::op_if1;
140

141
        if (!state::pop_bool_(value, state::is_enabled(flags::bip342_rule)))
142
            return error::op_if2;
143
    }
144

145
    state::begin_if(value);
436✔
146
    return error::op_success;
436✔
147
}
148

149
TEMPLATE
150
inline error::op_error_t CLASS::
40✔
151
op_notif() NOEXCEPT
152
{
153
    auto value = false;
40✔
154

155
    if (state::is_succeess())
40✔
156
    {
157
        if (state::is_stack_empty())
38✔
158
            return error::op_notif1;
159

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

164
    state::begin_if(!value);
38✔
165
    return error::op_success;
38✔
166
}
167

168
TEMPLATE
169
inline error::op_error_t CLASS::
×
170
op_verif() const NOEXCEPT
171
{
172
    if (state::is_enabled(flags::nops_rule))
×
173
        return op_unevaluated(opcode::op_verif);
×
174

175
    return error::op_not_implemented;
176
}
177

178
TEMPLATE
179
inline error::op_error_t CLASS::
×
180
op_vernotif() const NOEXCEPT
181
{
182
    if (state::is_enabled(flags::nops_rule))
×
183
        return op_unevaluated(opcode::op_vernotif);
×
184

185
    return error::op_not_implemented;
186
}
187

188
TEMPLATE
189
inline error::op_error_t CLASS::
450✔
190
op_else() NOEXCEPT
191
{
192
    if (state::is_balanced())
450✔
193
        return error::op_else;
194

195
    state::else_if_();
196
    return error::op_success;
442✔
197
}
198

199
TEMPLATE
200
inline error::op_error_t CLASS::
322✔
201
op_endif() NOEXCEPT
202
{
203
    if (state::is_balanced())
322✔
204
        return error::op_endif;
205

206
    state::end_if_();
207
    return error::op_success;
308✔
208
}
209

210
TEMPLATE
211
inline error::op_error_t CLASS::
75✔
212
op_verify() NOEXCEPT
213
{
214
    if (state::is_stack_empty())
75✔
215
        return error::op_verify1;
216

217
    if (!state::peek_bool_())
73✔
218
        return error::op_verify2;
219

220
    state::drop_();
221
    return error::op_success;
71✔
222
}
223

224
TEMPLATE
225
inline error::op_error_t CLASS::
10✔
226
op_return() const NOEXCEPT
227
{
228
    if (state::is_enabled(flags::nops_rule))
10✔
229
        return op_unevaluated(opcode::op_return);
5✔
230
        
231
    return error::op_not_implemented;
232
}
233

234
TEMPLATE
235
inline error::op_error_t CLASS::
20✔
236
op_to_alt_stack() NOEXCEPT
237
{
238
    if (state::is_stack_empty())
20✔
239
        return error::op_to_alt_stack;
240

241
    state::push_alternate(state::pop_());
18✔
242
    return error::op_success;
18✔
243
}
244

245
TEMPLATE
246
inline error::op_error_t CLASS::
10✔
247
op_from_alt_stack() NOEXCEPT
248
{
249
    if (state::is_alternate_empty())
10✔
250
        return error::op_from_alt_stack;
251

252
    state::push_variant(state::pop_alternate_());
6✔
253
    return error::op_success;
6✔
254
}
255

256
TEMPLATE
257
inline error::op_error_t CLASS::
22✔
258
op_drop2() NOEXCEPT
259
{
260
    if (state::stack_size() < 2u)
22✔
261
        return error::op_drop2;
262

263
    // 0,1,[2,3] => 1,[2,3] => [2,3]
264
    state::drop_();
265
    state::drop_();
266
    return error::op_success;
20✔
267
}
268

269
TEMPLATE
270
inline error::op_error_t CLASS::
12✔
271
op_dup2() NOEXCEPT
272
{
273
    if (state::stack_size() < 2u)
12✔
274
        return error::op_dup2;
275

276
    // [0,1,2,3] => 1,[0,1,2,3] =>  0,1,[0,1,2,3]
277
    state::push_variant(state::peek_(1));
278
    state::push_variant(state::peek_(1));
279
    return error::op_success;
6✔
280
}
281

282
TEMPLATE
283
inline error::op_error_t CLASS::
3,292✔
284
op_dup3() NOEXCEPT
285
{
286
    if (state::stack_size() < 3u)
3,292✔
287
        return error::op_dup3;
288

289
    // [0,1,2,3] => 2,[0,1,2,3] => 1,2,[0,1,2,3] => 0,1,2,[0,1,2,3]
290
    state::push_variant(state::peek_(2));
291
    state::push_variant(state::peek_(2));
292
    state::push_variant(state::peek_(2));
293
    return error::op_success;
3,284✔
294
}
295

296
TEMPLATE
297
inline error::op_error_t CLASS::
10✔
298
op_over2() NOEXCEPT
299
{
300
    if (state::stack_size() < 4u)
10✔
301
        return error::op_over2;
302

303
    // [0,1,2,3] => 3,[0,1,2,3] => 2,3,[0,1,2,3]
304
    state::push_variant(state::peek_(3));
305
    state::push_variant(state::peek_(3));
306
    return error::op_success;
4✔
307
}
308

309
TEMPLATE
310
inline error::op_error_t CLASS::
26✔
311
op_rot2() NOEXCEPT
312
{
313
    if (state::stack_size() < 6u)
26✔
314
        return error::op_rot2;
315

316
    // [0,1,2,3,4,5] => [4,1,2,3,0,5] => [4,5,2,3,0,1] =>
317
    // [4,5,0,3,2,1] => [4,5,0,1,2,3]
318
    state::swap_(0, 4);
319
    state::swap_(1, 5);
320
    state::swap_(2, 4);
321
    state::swap_(3, 5);
322
    return error::op_success;
24✔
323
}
324

325
TEMPLATE
326
inline error::op_error_t CLASS::
10✔
327
op_swap2() NOEXCEPT
328
{
329
    if (state::stack_size() < 4u)
10✔
330
        return error::op_swap2;
331

332
    // [0,1,2,3] => [0,3,2,1] => [2,3,0,1]
333
    state::swap_(1, 3);
334
    state::swap_(0, 2);
335
    return error::op_success;
4✔
336
}
337

338
TEMPLATE
339
inline error::op_error_t CLASS::
10✔
340
op_if_dup() NOEXCEPT
341
{
342
    if (state::is_stack_empty())
10✔
343
        return error::op_if_dup;
344

345
    // [0,1,2] => 0,[0,1,2]
346
    if (state::peek_bool_())
6✔
347
        state::push_variant(state::peek_());
348

349
    return error::op_success;
350
}
351

352
TEMPLATE
353
inline error::op_error_t CLASS::
183✔
354
op_depth() NOEXCEPT
355
{
356
    // [0,1,2] => 3,[0,1,2]
357
    state::push_length(state::stack_size());
358
    return error::op_success;
183✔
359
}
360

361
TEMPLATE
362
inline error::op_error_t CLASS::
22✔
363
op_drop() NOEXCEPT
364
{
365
    if (state::is_stack_empty())
22✔
366
        return error::op_drop;
367

368
    // 0,[1,2] => [1,2]
369
    state::drop_();
370
    return error::op_success;
18✔
371
}
372

373
TEMPLATE
374
inline error::op_error_t CLASS::
35✔
375
op_dup() NOEXCEPT
376
{
377
    if (state::is_stack_empty())
35✔
378
        return error::op_dup;
379

380
    // [0,1,2] => 0,[0,1 2]
381
    state::push_variant(state::peek_());
382
    return error::op_success;
31✔
383
}
384

385
TEMPLATE
386
inline error::op_error_t CLASS::
12✔
387
op_nip() NOEXCEPT
388
{
389
    if (state::stack_size() < 2u)
12✔
390
        return error::op_nip;
391

392
    // [0,1,2] => 1,[0,2] => [0,2]
393
    state::swap_(0, 1);
394
    state::drop_();
395
    return error::op_success;
6✔
396
}
397

398
TEMPLATE
399
inline error::op_error_t CLASS::
12✔
400
op_over() NOEXCEPT
401
{
402
    if (state::stack_size() < 2u)
12✔
403
        return error::op_over;
404

405
    // [0,1] => 1,[0,1]
406
    state::push_variant(state::peek_(1));
407
    return error::op_success;
6✔
408
}
409

410
TEMPLATE
411
inline error::op_error_t CLASS::
26✔
412
op_pick() NOEXCEPT
413
{
414
    size_t index;
415

416
    // 2,[0,1,2,3] => {2} [0,1,2,3]
417
    if (!state::pop_index32(index))
26✔
418
        return error::op_pick;
419

420
    // [0,1,2,3] => 2,[0,1,2,3]
421
    state::push_variant(state::peek_(index));
422
    return error::op_success;
16✔
423
}
424

425
// ****************************************************************************
426
// Rock-and-ROLL:
427
// "The value stack can store a maximum of 1000 elements. The following script
428
// fills the stack and then moves each stack element 200 times, so the number
429
// of moved elements is 200K. This took almost 5.6 seconds in my test VM (for
430
// a block filled with these scriptSigs):
431
// 1 {999 times}, 998 OP_ROLL{ 200 times }"
432
// bitslog.com/2017/04/17/new-quadratic-delays-in-bitcoin-scripts
433
// Shifting larger chunks does not change time, as vector stores references.
434
// This remains in the current satoshi implementation (std_vector).
435
// ****************************************************************************
436
TEMPLATE
437
inline error::op_error_t CLASS::
24✔
438
op_roll() NOEXCEPT
439
{
440
    size_t index;
441

442
    // 998,[0,1,2,...,997,998,999] => {998} [0,1,2,...,997,998,999] 
443
    if (!state::pop_index32(index))
24✔
444
        return error::op_roll;
445

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

449
    // Shifts maximum of n-1 references within vector of n.
450
    // [0,1,2,...,997,xxxx,999] => [0,1,2,...,997,999]
451
    state::erase_(index);
452

453
    // [0,1,2,...,997,999] => 998,[0,1,2,...,997,999]
454
    state::push_variant(std::move(temporary));
455
    return error::op_success;
16✔
456
}
457

458
TEMPLATE
459
inline error::op_error_t CLASS::
30✔
460
op_rot() NOEXCEPT
461
{
462
    if (state::stack_size() < 3u)
30✔
463
        return error::op_rot;
464

465
    // [0,1,2,3] = > [0,2,1,3] => [2,0,1,3]
466
    state::swap_(1, 2);
467
    state::swap_(0, 1);
468
    return error::op_success;
22✔
469
}
470

471
TEMPLATE
472
inline error::op_error_t CLASS::
20✔
473
op_swap() NOEXCEPT
474
{
475
    if (state::stack_size() < 2u)
20✔
476
        return error::op_swap;
477

478
    // [0,1,2] = > [1,0,2]
479
    state::swap_(0, 1);
480
    return error::op_success;
14✔
481
}
482

483
TEMPLATE
484
inline error::op_error_t CLASS::
12✔
485
op_tuck() NOEXCEPT
486
{
487
    if (state::stack_size() < 2u)
12✔
488
        return error::op_tuck;
489

490
    // [0,1,2] = > [1,0,2]  => 0,[1,0,2]
491
    state::swap_(0, 1);
492
    state::push_variant(state::peek_(1));
493
    return error::op_success;
6✔
494
}
495

496
TEMPLATE
497
inline error::op_error_t CLASS::
×
498
op_cat() const NOEXCEPT
499
{
500
    if (state::is_enabled(flags::cats_rule))
×
501
        return op_unevaluated(opcode::op_cat);
×
502

503
    return error::op_not_implemented;
504
}
505

506
TEMPLATE
507
inline error::op_error_t CLASS::
×
508
op_substr() const NOEXCEPT
509
{
510
    if (state::is_enabled(flags::cats_rule))
×
511
        return op_unevaluated(opcode::op_substr);
×
512

513
    return error::op_not_implemented;
514
}
515

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

523
    return error::op_not_implemented;
524
}
525

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

533
    return error::op_not_implemented;
534
}
535

536
TEMPLATE
537
inline error::op_error_t CLASS::
62✔
538
op_size() NOEXCEPT
539
{
540
    if (state::is_stack_empty())
62✔
541
        return error::op_size;
542

543
    state::push_length(state::peek_size());
544
    return error::op_success;
58✔
545
}
546

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

554
    return error::op_not_implemented;
555
}
556

557
TEMPLATE
558
inline error::op_error_t CLASS::
×
559
op_and() const NOEXCEPT
560
{
561
    if (state::is_enabled(flags::cats_rule))
×
562
        return op_unevaluated(opcode::op_and);
×
563

564
    return error::op_not_implemented;
565
}
566

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

574
    return error::op_not_implemented;
575
}
576

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

584
    return error::op_not_implemented;
585
}
586

587
TEMPLATE
588
inline error::op_error_t CLASS::
485✔
589
op_equal() NOEXCEPT
590
{
591
    if (state::stack_size() < 2u)
485✔
592
        return error::op_equal;
593

594
    state::push_bool(state::equal_chunks(state::pop_(), state::pop_()));
966✔
595
    return error::op_success;
483✔
596
}
597

598
TEMPLATE
599
inline error::op_error_t CLASS::
79✔
600
op_equal_verify() NOEXCEPT
601
{
602
    if (state::stack_size() < 2u)
79✔
603
        return error::op_equal_verify1;
604

605
    return state::equal_chunks(state::pop_(), state::pop_()) ?
168✔
606
        error::op_success : error::op_equal_verify2;
607
}
608

609
TEMPLATE
610
inline error::op_error_t CLASS::
18✔
611
op_add1() NOEXCEPT
612
{
613
    int32_t number;
614
    if (!state::pop_signed32(number))
16✔
615
        return error::op_add1;
6✔
616

617
    state::push_signed64(add1<int64_t>(number));
12✔
618
    return error::op_success;
12✔
619
}
620

621
TEMPLATE
622
inline error::op_error_t CLASS::
10✔
623
op_sub1() NOEXCEPT
624
{
625
    int32_t number;
626
    if (!state::pop_signed32(number))
8✔
627
        return error::op_sub1;
6✔
628

629
    state::push_signed64(sub1<int64_t>(number));
4✔
630
    return error::op_success;
4✔
631
}
632

633
TEMPLATE
634
inline error::op_error_t CLASS::
×
635
op_mul2() const NOEXCEPT
636
{
637
    if (state::is_enabled(flags::cats_rule))
×
638
        return op_unevaluated(opcode::op_mul2);
×
639

640
    return error::op_not_implemented;
641
}
642

643
TEMPLATE
644
inline error::op_error_t CLASS::
×
645
op_div2() const NOEXCEPT
646
{
647
    if (state::is_enabled(flags::cats_rule))
×
648
        return op_unevaluated(opcode::op_div2);
×
649

650
    return error::op_not_implemented;
651
}
652

653
TEMPLATE
654
inline error::op_error_t CLASS::
10✔
655
op_negate() NOEXCEPT
656
{
657
    int32_t number;
658
    if (!state::pop_signed32(number))
8✔
659
        return error::op_negate;
4✔
660

661
    // negate(minimum) overflow precluded by domain increase.
662
    state::push_signed64(negate<int64_t>(number));
6✔
663
    return error::op_success;
6✔
664
}
665

666
TEMPLATE
667
inline error::op_error_t CLASS::
10✔
668
op_abs() NOEXCEPT
669
{
670
    int32_t number;
671
    if (!state::pop_signed32(number))
8✔
672
        return error::op_abs;
2✔
673

674
    // absolute(minimum) overflow precluded by domain increase.
675
    state::push_signed64(absolute<int64_t>(number));
8✔
676
    return error::op_success;
8✔
677
}
678

679
TEMPLATE
680
inline error::op_error_t CLASS::
52✔
681
op_not() NOEXCEPT
682
{
683
    int32_t number;
684
    if (!state::pop_signed32(number))
50✔
685
        return error::op_not;
4✔
686

687
    // Do not pop_bool() above, as number must be validated.
688
    state::push_bool(!to_bool(number));
48✔
689
    return error::op_success;
48✔
690
}
691

692
TEMPLATE
693
inline error::op_error_t CLASS::
12✔
694
op_nonzero() NOEXCEPT
695
{
696
    int32_t number;
697
    if (!state::pop_signed32(number))
10✔
698
        return error::op_nonzero;
2✔
699

700
    state::push_bool(is_nonzero(number));
10✔
701
    return error::op_success;
10✔
702
}
703

704
TEMPLATE
705
inline error::op_error_t CLASS::
72✔
706
op_add() NOEXCEPT
707
{
708
    int32_t right, left;
709
    if (!state::pop_binary32(left, right))
710
        return error::op_add;
6✔
711

712
    // addition overflow precluded by domain increase.
713
    state::push_signed64(add<int64_t>(left, right));
66✔
714
    return error::op_success;
66✔
715
}
716

717
TEMPLATE
718
inline error::op_error_t CLASS::
10✔
719
op_sub() NOEXCEPT
720
{
721
    int32_t right, left;
722
    if (!state::pop_binary32(left, right))
723
        return error::op_sub;
2✔
724

725
    // subtraction overflow precluded by domain increase.
726
    state::push_signed64(subtract<int64_t>(left, right));
8✔
727
    return error::op_success;
8✔
728
}
729

730
TEMPLATE
731
inline error::op_error_t CLASS::
×
732
op_mul() const NOEXCEPT
733
{
734
    if (state::is_enabled(flags::cats_rule))
×
735
        return op_unevaluated(opcode::op_mul);
×
736

737
    return error::op_not_implemented;
738
}
739

740
TEMPLATE
741
inline error::op_error_t CLASS::
×
742
op_div() const NOEXCEPT
743
{
744
    if (state::is_enabled(flags::cats_rule))
×
745
        return op_unevaluated(opcode::op_div);
×
746

747
    return error::op_not_implemented;
748
}
749

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

757
    return error::op_not_implemented;
758
}
759

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

767
    return error::op_not_implemented;
768
}
769

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

777
    return error::op_not_implemented;
778
}
779

780
TEMPLATE
781
inline error::op_error_t CLASS::
14✔
782
op_bool_and() NOEXCEPT
783
{
784
    int32_t right, left;
785
    if (!state::pop_binary32(left, right))
786
        return error::op_bool_and;
2✔
787

788
    state::push_bool(to_bool(left) && to_bool(right));
12✔
789
    return error::op_success;
12✔
790
}
791

792
TEMPLATE
793
inline error::op_error_t CLASS::
14✔
794
op_bool_or() NOEXCEPT
795
{
796
    int32_t right, left;
797
    if (!state::pop_binary32(left, right))
798
        return error::op_bool_or;
2✔
799

800
    state::push_bool(to_bool(left) || to_bool(right));
12✔
801
    return error::op_success;
12✔
802
}
803

804
TEMPLATE
805
inline error::op_error_t CLASS::
38✔
806
op_num_equal() NOEXCEPT
807
{
808
    int32_t right, left;
809
    if (!state::pop_binary32(left, right))
810
        return error::op_num_equal;
4✔
811

812
    state::push_bool(left == right);
34✔
813
    return error::op_success;
34✔
814
}
815

816
TEMPLATE
817
inline error::op_error_t CLASS::
6✔
818
op_num_equal_verify() NOEXCEPT
819
{
820
    int32_t right, left;
821
    if (!state::pop_binary32(left, right))
822
        return error::op_num_equal_verify1;
2✔
823

824
    return (left == right) ? error::op_success : 
4✔
825
        error::op_num_equal_verify2;
826
}
827

828
TEMPLATE
829
inline error::op_error_t CLASS::
8✔
830
op_num_not_equal() NOEXCEPT
831
{
832
    int32_t right, left;
833
    if (!state::pop_binary32(left, right))
834
        return error::op_num_not_equal;
2✔
835

836
    state::push_bool(left != right);
6✔
837
    return error::op_success;
6✔
838
}
839

840
TEMPLATE
841
inline error::op_error_t CLASS::
14✔
842
op_less_than() NOEXCEPT
843
{
844
    int32_t right, left;
845
    if (!state::pop_binary32(left, right))
846
        return error::op_less_than;
2✔
847

848
    state::push_bool(is_lesser(left, right));
12✔
849
    return error::op_success;
12✔
850
}
851

852
TEMPLATE
853
inline error::op_error_t CLASS::
14✔
854
op_greater_than() NOEXCEPT
855
{
856
    int32_t right, left;
857
    if (!state::pop_binary32(left, right))
858
        return error::op_greater_than;
2✔
859

860
    state::push_bool(is_greater(left, right));
12✔
861
    return error::op_success;
12✔
862
}
863

864
TEMPLATE
865
inline error::op_error_t CLASS::
14✔
866
op_less_than_or_equal() NOEXCEPT
867
{
868
    int32_t right, left;
869
    if (!state::pop_binary32(left, right))
870
        return error::op_less_than_or_equal;
2✔
871

872
    state::push_bool(!is_greater(left, right));
12✔
873
    return error::op_success;
12✔
874
}
875

876
TEMPLATE
877
inline error::op_error_t CLASS::
14✔
878
op_greater_than_or_equal() NOEXCEPT
879
{
880
    int32_t right, left;
881
    if (!state::pop_binary32(left, right))
882
        return error::op_greater_than_or_equal;
2✔
883

884
    state::push_bool(!is_lesser(left, right));
12✔
885
    return error::op_success;
12✔
886
}
887

888
TEMPLATE
889
inline error::op_error_t CLASS::
12✔
890
op_min() NOEXCEPT
891
{
892
    int32_t right, left;
893
    if (!state::pop_binary32(left, right))
894
        return error::op_min;
2✔
895

896
    state::push_signed64(lesser(left, right));
10✔
897
    return error::op_success;
10✔
898
}
899

900
TEMPLATE
901
inline error::op_error_t CLASS::
12✔
902
op_max() NOEXCEPT
903
{
904
    int32_t right, left;
905
    if (!state::pop_binary32(left, right))
906
        return error::op_max;
2✔
907

908
    state::push_signed64(greater(left, right));
10✔
909
    return error::op_success;
10✔
910
}
911

912
TEMPLATE
913
inline error::op_error_t CLASS::
18✔
914
op_within() NOEXCEPT
915
{
916
    int32_t upper, lower, value;
917
    if (!state::pop_ternary32(upper, lower, value))
918
        return error::op_within;
2✔
919

920
    state::push_bool(!is_lesser(value, lower) && is_lesser(value, upper));
16✔
921
    return error::op_success;
16✔
922
}
923

924
TEMPLATE
925
inline error::op_error_t CLASS::
14✔
926
op_ripemd160() NOEXCEPT
927
{
928
    if (state::is_stack_empty())
14✔
929
        return error::op_ripemd160;
930

931
    state::push_chunk(rmd160_chunk(*state::pop_chunk_()));
10✔
932
    return error::op_success;
10✔
933
}
934

935
TEMPLATE
936
inline error::op_error_t CLASS::
92✔
937
op_sha1() NOEXCEPT
938
{
939
    if (state::is_stack_empty())
92✔
940
        return error::op_sha1;
941

942
    state::push_chunk(sha1_chunk(*state::pop_chunk_()));
88✔
943
    return error::op_success;
88✔
944
}
945

946
TEMPLATE
947
inline error::op_error_t CLASS::
18✔
948
op_sha256() NOEXCEPT
949
{
950
    if (state::is_stack_empty())
18✔
951
        return error::op_sha256;
952

953
    state::push_chunk(sha256_chunk(*state::pop_chunk_()));
14✔
954
    return error::op_success;
14✔
955
}
956

957
TEMPLATE
958
inline error::op_error_t CLASS::
49✔
959
op_hash160() NOEXCEPT
960
{
961
    if (state::is_stack_empty())
49✔
962
        return error::op_hash160;
963

964
    state::push_chunk(bitcoin_short_chunk(*state::pop_chunk_()));
45✔
965
    return error::op_success;
45✔
966
}
967

968
TEMPLATE
969
inline error::op_error_t CLASS::
14✔
970
op_hash256() NOEXCEPT
971
{
972
    if (state::is_stack_empty())
14✔
973
        return error::op_hash256;
974

975
    state::push_chunk(bitcoin_chunk(*state::pop_chunk_()));
10✔
976
    return error::op_success;
10✔
977
}
978

979
TEMPLATE
980
inline error::op_error_t CLASS::
9✔
981
op_codeseparator(const op_iterator& op) NOEXCEPT
982
{
983
    // Not thread safe for the script (changes script object metadata).
984
    state::set_subscript(op);
985
    return error::op_success;
986
}
987

988
TEMPLATE
989
inline error::op_error_t CLASS::
22✔
990
op_check_sig() NOEXCEPT
991
{
992
    const auto ec = op_check_sig_verify();
22✔
993
    if (ec == error::op_check_sig_empty_key)
22✔
994
        return ec;
995

996
    // BIP66: if DER encoding invalid script MUST fail and end.
997
    const auto bip66 = state::is_enabled(flags::bip66_rule);
998
    if (bip66 && ec == error::op_check_sig_parse_signature)
22✔
999
        return ec;
1000

1001
    state::push_bool(ec == error::op_success);
22✔
1002
    return error::op_success;
22✔
1003
}
1004

1005
TEMPLATE
1006
inline error::op_error_t CLASS::
26✔
1007
op_check_sig_verify() NOEXCEPT
1008
{
1009
    if (state::stack_size() < 2u)
26✔
1010
        return error::op_check_sig_verify1;
1011

1012
    const auto key = state::pop_chunk_();
1013
    const auto endorsement = state::pop_chunk_();
26✔
1014
    if (key->empty())
26✔
1015
        return error::op_check_sig_empty_key;
1016

1017
    // BIP342:
1018
    if (state::is_enabled(flags::bip342_rule))
26✔
1019
    {
1020
        // If signature is empty, script MUST fail and end (or push false).
1021
        if (endorsement->empty())
×
1022
            return error::op_check_sig_verify2;
1023

1024
        // If public key is 32 bytes it is a bip340 schnorr key.
1025
        // If signature is not empty, it is validated against public key.
1026
        if (key->size() == schnorr::public_key_size)
×
1027
        {
1028
            // Split endorsement into schnorr sig and signature hash flags.
1029
            uint8_t sighash_flags;
1030
            const auto& sig = state::schnorr_split(sighash_flags, *endorsement);
×
1031
            if (sighash_flags == chain::coverage::invalid)
×
1032
                return error::op_check_sig_verify3;
×
1033

1034
            // Generate signature hash.
NEW
1035
            hash_digest hash{};
×
NEW
1036
            if (!state::signature_hash(hash, sighash_flags))
×
1037
                return error::op_check_sig_verify4;
1038

1039
            // Verify schnorr signature against public key and signature hash.
UNCOV
1040
            if (!schnorr::verify_signature(*key, hash, sig))
×
1041
                return error::op_check_sig_verify5;
1042

1043
            // If signature not empty, opcode counted toward sigops budget.
1044
            if (!state::sigops_increment())
×
1045
                return error::op_check_sig_verify6;
1046
        }
1047

1048
        // If signature not empty, opcode counted toward sigops budget.
1049
        if (!state::sigops_increment())
×
1050
            return error::op_check_sig_verify7;
1051

1052
        // If public key size is neither 0 nor 32 bytes, it is an unknown type.
1053
        // During script execution of signature opcodes these behave exactly as
1054
        // known types except that signature validation considered successful.
1055
        return error::op_success;
×
1056
    }
1057

1058
    if (endorsement->empty())
26✔
1059
        return error::op_check_sig_verify8;
1060

1061
    // Split endorsement into DER signature and signature hash flags.
1062
    uint8_t sighash_flags;
1063
    const auto& der = state::ecdsa_split(sighash_flags, *endorsement);
1064
    const auto bip66 = state::is_enabled(flags::bip66_rule);
1065

1066
    // BIP66: if DER encoding invalid script MUST fail and end.
1067
    ec_signature sig;
1068
    if (!ecdsa::parse_signature(sig, der, bip66))
26✔
1069
        return error::op_check_sig_parse_signature;
1070

1071
    // Generate signature hash.
1072
    hash_digest hash{};
26✔
1073
    const auto subscript = state::subscript(endorsement);
1074
    if (!state::signature_hash(hash, *subscript, sighash_flags))
26✔
1075
        return error::op_check_sig_verify9;
1076

1077
    // Verify ECDSA signature against public key and signature hash.
1078
    if (!ecdsa::verify_signature(*key, hash, sig))
26✔
1079
        return error::op_check_sig_verify10;
6✔
1080

1081
    // TODO: use sighash and key to generate signature in sign mode.
1082
    return error::op_success;
1083
}
1084

1085
TEMPLATE
1086
inline error::op_error_t CLASS::
1,327✔
1087
op_check_multisig() NOEXCEPT
1088
{
1089
    if (state::is_enabled(flags::bip342_rule))
1,327✔
1090
        return op_unevaluated(opcode::checkmultisig);
×
1091

1092
    const auto ec = op_check_multisig_verify();
1,327✔
1093

1094
    // BIP66: if DER encoding invalid, script MUST fail and end.
1095
    const auto bip66 = state::is_enabled(flags::bip66_rule);
1096
    if (bip66 && ec == error::op_check_multisig_parse_signature)
1,327✔
1097
        return ec;
1098

1099
    state::push_bool(ec == error::op_success);
1,327✔
1100
    return error::op_success;
1,327✔
1101
}
1102

1103
TEMPLATE
1104
inline error::op_error_t CLASS::
2,058✔
1105
op_check_multisig_verify() NOEXCEPT
1106
{
1107
    if (state::is_enabled(flags::bip342_rule))
2,058✔
1108
        return op_unevaluated(opcode::checkmultisigverify);
×
1109

1110
    size_t count{};
1111
    if (!state::pop_index32(count))
2,058✔
1112
        return error::op_check_multisig_verify1;
1113

1114
    if (count > chain::max_script_public_keys)
2,058✔
1115
        return error::op_check_multisig_verify2;
1116

1117
    if (!state::ops_increment(count))
2,058✔
1118
        return error::op_check_multisig_verify3;
1119

1120
    chunk_xptrs keys;
1121
    if (!state::pop_chunks(keys, count))
1122
        return error::op_check_multisig_verify4;
1123

1124
    if (!state::pop_index32(count))
2,052✔
1125
        return error::op_check_multisig_verify5;
1126

1127
    if (count > keys.size())
2,050✔
1128
        return error::op_check_multisig_verify6;
1129

1130
    chunk_xptrs endorsements;
1131
    if (!state::pop_chunks(endorsements, count))
1132
        return error::op_check_multisig_verify7;
1133

1134
    if (state::is_stack_empty())
2,050✔
1135
        return error::op_check_multisig_verify8;
1136

1137
    //*************************************************************************
1138
    // CONSENSUS: Satoshi bug, discard stack element, malleable until bip147.
1139
    //*************************************************************************
1140
    const auto bip147 = state::is_enabled(flags::bip147_rule);
1141

1142
    // This check is unique in that a chunk must be empty to be false.
1143
    if (state::pop_strict_bool_() && bip147)
2,050✔
1144
        return error::op_check_multisig_verify9;
1145

1146
    state::initialize_cache();
1147
    auto it = endorsements.begin();
1148
    const auto subscript = state::subscript(endorsements);
1,949✔
1149
    const auto bip66 = state::is_enabled(flags::bip66_rule);
1150

1151
    // Keys may be empty.
1152
    for (const auto& key: keys)
1,969✔
1153
    {
1154
        // Implies that all signatures are valid.
1155
        if (it == endorsements.end())
246✔
1156
            break;
1157

1158
        // Empty endorsement does not increment iterator.
1159
        const auto endorsement = *it;
20✔
1160
        if (endorsement->empty())
20✔
1161
            continue;
×
1162

1163
        // Split endorsement into DER signature and signature hash flags.
1164
        uint8_t sighash_flags;
1165
        const auto& der = state::ecdsa_split(sighash_flags, *endorsement);
1166

1167
        // BIP66: if DER encoding invalid script MUST fail and end.
1168
        ec_signature sig;
1169
        if (!ecdsa::parse_signature(sig, der, bip66))
20✔
1170
            return error::op_check_sig_parse_signature;
1171

1172
        // Signature hash caching (bypass signature hash if same as previous).
1173
        if (state::uncached(sighash_flags))
1174
            if (!state::set_hash(*subscript, sighash_flags))
10✔
1175
                return error::op_check_multisig_verify10;
1176

1177
        // Verify ECDSA signature against public key and cache signature hash.
1178
        if (ecdsa::verify_signature(*key, state::cached_hash(), sig))
20✔
1179
            ++it;
1180
    }
1181

1182
    // All endorsements must be verified against a key.
1183
    if (it != endorsements.end())
1,949✔
1184
        return error::op_check_multisig_verify11;
1✔
1185

1186
    // TODO: use sighash and key to generate signature in sign mode.
1187
    return error::op_success;
1188
}
1189

1190
TEMPLATE
1191
inline error::op_error_t CLASS::
27✔
1192
op_check_locktime_verify() const NOEXCEPT
1193
{
1194
    // BIP65: nop2 subsumed by checklocktimeverify when bip65 fork is active.
1195
    if (!state::is_enabled(flags::bip65_rule))
27✔
1196
        return op_nop(opcode::nop2);
1197

1198
    // The tx sequence is 0xffffffff.
1199
    if (state::input().is_final())
18✔
1200
        return error::op_check_locktime_verify1;
1201

1202
    // The stack is empty.
1203
    // The top stack item is negative.
1204
    // Extend the (signed) script number range to 5 bytes.
1205
    // The stack top is positive and 40 bits are usable.
1206
    uint64_t stack_locktime40;
1207
    if (!state::peek_unsigned40(stack_locktime40))
18✔
1208
        return error::op_check_locktime_verify2;
1209

1210
    const auto trans_locktime32 = state::tx().locktime();
7✔
1211
    using namespace chain;
1212

1213
    // The stack locktime type differs from that of tx.
1214
    if ((stack_locktime40 < locktime_threshold) !=
7✔
1215
        (trans_locktime32 < locktime_threshold))
7✔
1216
        return error::op_check_locktime_verify3;
1217

1218
    // The stack locktime is greater than the tx locktime.
1219
    if (stack_locktime40 > trans_locktime32)
5✔
1220
        return error::op_check_locktime_verify4;
1221

1222
    // TODO: use sighash and key to generate signature in sign mode?
1223
    return error::op_success;
1224
}
1225

1226
TEMPLATE
1227
inline error::op_error_t CLASS::
4✔
1228
op_check_sequence_verify() const NOEXCEPT
1229
{
1230
    // BIP112: nop3 subsumed by checksequenceverify when bip112 fork is active.
1231
    if (!state::is_enabled(flags::bip112_rule))
4✔
1232
        return op_nop(opcode::nop3);
1233

1234
    // The stack is empty.
1235
    // The top stack item is negative.
1236
    // Extend the (signed) script number range to 5 bytes.
1237
    // The stack top is positive and 32 bits are used (33rd-40th discarded).
1238
    uint32_t stack_sequence32;
1239
    if (!state::peek_unsigned32(stack_sequence32))
×
1240
        return error::op_check_sequence_verify1;
1241

1242
    // Only 32 bits are tested.
1243
    const auto input_sequence32 = state::input().sequence();
×
1244
    using namespace chain;
1245

1246
    // The stack sequence is disabled, treat as nop3.
1247
    if (get_right(stack_sequence32, relative_locktime_disabled_bit))
×
1248
        return op_nop(opcode::nop3);
1249

1250
    // The stack sequence is enabled and tx version less than 2.
1251
    if (state::tx().version() < relative_locktime_min_version)
×
1252
        return error::op_check_sequence_verify2;
1253

1254
    // The transaction sequence is disabled.
1255
    if (get_right(input_sequence32, relative_locktime_disabled_bit))
×
1256
        return error::op_check_sequence_verify3;
1257

1258
    // The stack sequence type differs from that of tx input.
1259
    if (get_right(stack_sequence32, relative_locktime_time_locked_bit) !=
×
1260
        get_right(input_sequence32, relative_locktime_time_locked_bit))
×
1261
        return error::op_check_sequence_verify4;
1262

1263
    // The unmasked stack sequence is greater than that of tx sequence.
1264
    if (mask_left(stack_sequence32, relative_locktime_mask_left) >
×
1265
        mask_left(input_sequence32, relative_locktime_mask_left))
1266
        return error::op_check_sequence_verify5;
1267

1268
    return error::op_success;
1269
}
1270

1271
TEMPLATE
1272
inline error::op_error_t CLASS::
4✔
1273
op_check_sig_add() NOEXCEPT
1274
{
1275
    // BIP342: reserved_186 subsumed by op_checksigadd when tapscript active.
1276
    if (!state::is_enabled(flags::bip342_rule))
4✔
1277
        return op_unevaluated(opcode::reserved_186);
4✔
1278

1279
    // If fewer than 3 elements on stack, script MUST fail and end.
1280
    if (state::stack_size() < 3u)
×
1281
        return error::op_check_schnorr_sig1;
1282

1283
    // Public key (top) is popped.
1284
    const auto key = state::pop_chunk_();
1285

1286
    // If public key is empty, script MUST fail and end.
1287
    if (key->empty())
×
1288
        return error::op_check_schnorr_sig2;
1289

1290
    // Number (second to top) is popped.
1291
    // If number is larger than 4 bytes, script MUST fail and end.
1292
    int32_t number;
1293
    if (!state::pop_signed32_(number))
×
1294
        return error::op_check_schnorr_sig3;
1295

1296
    // Signature (third to top) is popped.
1297
    const auto endorsement = state::pop_chunk_();
1298

1299
    // If signature is empty, [number] pushed, execution continues.
1300
    if (endorsement->empty())
×
1301
    {
1302
        state::push_signed64(number);
×
1303
        return error::op_success;
×
1304
    }
1305

1306
    // Split endorsement into schnorr signature and signature hash flags.
1307
    uint8_t sighash_flags;
1308
    const auto& sig = state::schnorr_split(sighash_flags, *endorsement);
×
1309
    if (sighash_flags == chain::coverage::invalid)
×
1310
        return error::op_check_schnorr_sig4;
1311

1312
    // Generate signature hash.
NEW
1313
    hash_digest hash{};
×
NEW
1314
    if (!state::signature_hash(hash, sighash_flags))
×
1315
        return error::op_check_schnorr_sig5;
1316

1317
    // Verify schnorr signature against public key and signature hash.
UNCOV
1318
    if (!schnorr::verify_signature(*key, hash, sig))
×
1319
        return error::op_check_schnorr_sig6;
1320

1321
    // If signature not empty, opcode counted toward sigops budget.
1322
    if (!state::sigops_increment())
×
1323
        return error::op_check_schnorr_sig7;
1324

1325
    // If signature not empty (and successful), [number+1] pushed.
1326
    state::push_signed64(add1<int64_t>(number));
×
1327
    return error::op_success;
×
1328
}
1329

1330
} // namespace machine
1331
} // namespace system
1332
} // namespace libbitcoin
1333

1334
#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