| 1 | package Bitcoin::Crypto::Tapscript::Opcode; | ||||||||||||||||||||||||||||
| 2 | 68 | $Bitcoin::Crypto::Tapscript::Opcode::VERSION = '4.006'; | |||||||||||||||||||||||||||
| 3 | 68 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 68 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 68 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 68 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 68 | ▶ | use Bitcoin::Crypto::Util::Internal qw(lift_x get_taproot_ext); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 68 | ▶ | use Bitcoin::Crypto::Helpers qw(ecc die_no_trace check_sighash); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 68 | ▶ | use Bitcoin::Crypto::Script::Opcode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 68 | ▶ | use Bitcoin::Crypto::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 68 | ▶ | use Bitcoin::Crypto::Types -types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 68 | ▶ | use Bitcoin::Crypto::Constants qw(:sighash); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 68 | extends 'Bitcoin::Crypto::Script::Opcode'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub _compile_OP_SUCCESS | ||||||||||||||||||||||||||||
| 19 | 3132 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_compile_OP_SUCCESS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3132 | my ($class) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 174 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 174 | my ($compiler) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 174 | $compiler->_unconditionally_valid_script('OP_SUCCESS encountered'); | |||||||||||||||||||||||||||
| 26 | 3132 | }; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _OP_CHECKSIG | ||||||||||||||||||||||||||||
| 30 | 144 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_OP_CHECKSIG: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 144 | my ($class) = @_; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 64 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.9
| |||||||||||||||||||||||||||||
| 34 | 64 | my $runner = shift; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 64 | my $stack = $runner->stack; | |||||||||||||||||||||||||||
| 37 | 64 | ▶ | $runner->_stack_error unless @$stack >= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 64 | my $raw_pubkey = pop @$stack; | |||||||||||||||||||||||||||
| 40 | 64 | my $sig = pop @$stack; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 64 | my $known_pubkey_type; | |||||||||||||||||||||||||||
| 43 | my $hashtype; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # rules according to https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#rules-for-signature-opcodes | ||||||||||||||||||||||||||||
| 46 | 64 | ▶ | if (length $raw_pubkey == 32) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 63 | $known_pubkey_type = !!1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | elsif (length $raw_pubkey == 0) { | ||||||||||||||||||||||||||||
| 50 | 0 | $runner->_invalid_script('bad pubkey'); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||
| 53 | 1 | $known_pubkey_type = !!0; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 64 | ▶ | if (length $sig == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # empty signature | ||||||||||||||||||||||||||||
| 59 | 10 | push @$stack, $runner->from_bool(!!0); | |||||||||||||||||||||||||||
| 60 | 10 | return; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 54 | my $tx = $runner->transaction; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 54 | ▶ | $runner->_invalid_script('sigop budget exceeded') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | unless $tx->reduce_sigop_budget; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 54 | my $ext_flag = $tx->taproot_ext_flag; | |||||||||||||||||||||||||||
| 69 | 54 | my $ext; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 54 | ▶ | if ($ext_flag == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 19 | ▶ | die_no_trace 'no script_tree in script transaction object' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | unless $tx->has_script_tree; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 19 | $ext = get_taproot_ext( | |||||||||||||||||||||||||||
| 77 | $ext_flag, { | ||||||||||||||||||||||||||||
| 78 | script_tree => $tx->script_tree, | ||||||||||||||||||||||||||||
| 79 | leaf_id => $tx->leaf_id, | ||||||||||||||||||||||||||||
| 80 | codesep_pos => $runner->codeseparator, | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 54 | ▶ | if (!$known_pubkey_type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | push @$stack, $runner->from_bool(!!1); | |||||||||||||||||||||||||||
| 87 | 1 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 53 | ▶ | ($sig, $hashtype) = unpack 'a64C', $sig | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | if length $sig == 65; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 53 | ▶ | $runner->_invalid_script('bad sighash') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hashtype and not check_sighash($hashtype)
MC/DC0%0/2
defined $hashtype and not check_sighash($hashtype)
defined $hashtype
not check_sighash($hashtype)
Truth table66%2/3
A and not(B)
A= defined $hashtypeB= not check_sighash($hashtype)
| |||||||||||||||||||||||||||||
| 94 | if defined $hashtype && !check_sighash($hashtype); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 53 | my $preimage = $tx->get_digest( | |||||||||||||||||||||||||||
| 97 | sighash => $hashtype, | ||||||||||||||||||||||||||||
| 98 | taproot_ext => $ext | ||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # TODO: this uses ecc directly and skips creating a new public key | ||||||||||||||||||||||||||||
| 102 | # with lift_x - this saves time, but maybe an abstraction for that | ||||||||||||||||||||||||||||
| 103 | # should be made as Key::Schnorr (which would use SignVerify)? | ||||||||||||||||||||||||||||
| 104 | 53 | my $result = ecc->verify_digest_schnorr($raw_pubkey, $sig, $preimage->hash); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 52 | ▶ | $runner->_invalid_script('signature verification failed') unless $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 50 | push @$stack, $runner->from_bool($result); | |||||||||||||||||||||||||||
| 108 | 144 | }; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub _OP_CHECKMULTISIG | ||||||||||||||||||||||||||||
| 112 | 144 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_OP_CHECKMULTISIG: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 113 | 144 | my ($class) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 116 | 0 | my $runner = shift; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | $runner->_invalid_script; | |||||||||||||||||||||||||||
| 119 | 144 | }; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub _OP_CHECKSIGADD | ||||||||||||||||||||||||||||
| 123 | 36 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_OP_CHECKSIGADD: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 124 | 36 | my ($class) = @_; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 36 | my $checksig = $class->_OP_CHECKSIG; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 14 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 129 | 14 | my $runner = shift; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 14 | my $stack = $runner->stack; | |||||||||||||||||||||||||||
| 132 | 14 | ▶ | $runner->_stack_error unless @$stack >= 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 14 | my $n = $runner->to_int(splice @$stack, -2, 1); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 14 | $checksig->($runner); | |||||||||||||||||||||||||||
| 136 | 14 | push @$stack, $runner->from_int($n + $runner->to_int(pop @$stack)); | |||||||||||||||||||||||||||
| 137 | 36 | }; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub _build_opcodes | ||||||||||||||||||||||||||||
| 141 | 36 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_opcodes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 142 | 36 | my ($class) = @_; | |||||||||||||||||||||||||||
| 143 | 36 | my %parent_opcodes = $class->SUPER::_build_opcodes; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # NOTE: multisig no longer is a sigop and no longer requires transaction | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 36 | my %opcodes = ( | |||||||||||||||||||||||||||
| 148 | %parent_opcodes, | ||||||||||||||||||||||||||||
| 149 | OP_CHECKSIG => { | ||||||||||||||||||||||||||||
| 150 | code => 0xac, | ||||||||||||||||||||||||||||
| 151 | needs_transaction => !!1, | ||||||||||||||||||||||||||||
| 152 | sigop => !!1, | ||||||||||||||||||||||||||||
| 153 | runner => $class->_OP_CHECKSIG, | ||||||||||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||||||||||
| 155 | OP_CHECKMULTISIG => { | ||||||||||||||||||||||||||||
| 156 | code => 0xae, | ||||||||||||||||||||||||||||
| 157 | runner => $class->_OP_CHECKMULTISIG, | ||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | OP_CHECKMULTISIGVERIFY => { | ||||||||||||||||||||||||||||
| 160 | code => 0xaf, | ||||||||||||||||||||||||||||
| 161 | runner => $class->_OP_CHECKMULTISIGVERIFY, | ||||||||||||||||||||||||||||
| 162 | }, | ||||||||||||||||||||||||||||
| 163 | OP_CHECKSIGADD => { | ||||||||||||||||||||||||||||
| 164 | code => 0xba, | ||||||||||||||||||||||||||||
| 165 | needs_transaction => !!1, | ||||||||||||||||||||||||||||
| 166 | sigop => !!1, | ||||||||||||||||||||||||||||
| 167 | runner => $class->_OP_CHECKSIGADD, | ||||||||||||||||||||||||||||
| 168 | }, | ||||||||||||||||||||||||||||
| 169 | ); | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 36 | my %opcodes_by_code = map { $opcodes{$_}{code} => $_ } keys %opcodes; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki#specification | ||||||||||||||||||||||||||||
| 174 | 36 | for my $succ (80, 98, 126 .. 129, 131 .. 134, 137, 138, 141, 142, 149 .. 153, 187 .. 254) { | |||||||||||||||||||||||||||
| 175 | delete $opcodes{$opcodes_by_code{$succ}} | ||||||||||||||||||||||||||||
| 176 | 3132 | ▶ | if exists $opcodes_by_code{$succ}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3132 | $opcodes{"OP_SUCCESS$succ"} = { | |||||||||||||||||||||||||||
| 179 | code => $succ, | ||||||||||||||||||||||||||||
| 180 | on_compilation => $class->_compile_OP_SUCCESS, | ||||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 36 | return %opcodes; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 68 | 1; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||