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