bitcoin_primitives/
opcodes.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Bitcoin script opcodes.
4//!
5//! Bitcoin's script uses a stack-based assembly language. This module defines
6//! all of the opcodes for that language.
7
8#![allow(non_camel_case_types)]
9
10use core::fmt;
11
12use internals::debug_from_display;
13
14#[cfg(feature = "serde")]
15use crate::prelude::ToString;
16
17/// A script Opcode.
18///
19/// We do not implement Ord on this type because there is no natural ordering on opcodes, but there
20/// may appear to be one (e.g. because all the push opcodes appear in a consecutive block) and we
21/// don't want to encourage subtly buggy code. Please use [`Opcode::classify`] to distinguish different
22/// types of opcodes.
23///
24/// <details>
25///   <summary>Example of Core bug caused by assuming ordering</summary>
26///
27///   Bitcoin Core's `IsPushOnly` considers `OP_RESERVED` to be a "push code", allowing this opcode
28///   in contexts where only pushes are supposed to be allowed.
29/// </details>
30#[derive(Copy, Clone, PartialEq, Eq)]
31pub struct Opcode {
32    code: u8,
33}
34
35use self::all::*;
36
37macro_rules! all_opcodes {
38    ($($op:ident => $val:expr, $doc:expr);*) => {
39        /// Enables wildcard imports to bring into scope all opcodes and nothing else.
40        ///
41        /// The `all` module is provided so one can use a wildcard import `use bitcoin::opcodes::all::*` to
42        /// get all the `OP_FOO` opcodes without getting other types defined in `opcodes` (e.g. `Opcode`, `Class`).
43        ///
44        /// This module is guaranteed to never contain anything except opcode constants and all opcode
45        /// constants are guaranteed to begin with OP_.
46        pub mod all {
47            use super::Opcode;
48            $(
49                #[doc = $doc]
50                pub const $op: Opcode = Opcode { code: $val};
51            )*
52        }
53
54        /// Push an empty array onto the stack.
55        pub static OP_0: Opcode = OP_PUSHBYTES_0;
56        /// Empty stack is also FALSE.
57        pub static OP_FALSE: Opcode = OP_PUSHBYTES_0;
58        /// Number 1 is also TRUE.
59        pub static OP_TRUE: Opcode = OP_PUSHNUM_1;
60        /// Previously called OP_NOP2.
61        pub static OP_NOP2: Opcode = OP_CLTV;
62        /// Previously called OP_NOP3.
63        pub static OP_NOP3: Opcode = OP_CSV;
64
65        impl fmt::Display for Opcode {
66            fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
67                 match *self {
68                   $(
69                        $op => core::fmt::Display::fmt(stringify!($op), f),
70                    )+
71                }
72            }
73        }
74    }
75}
76
77all_opcodes! {
78    OP_PUSHBYTES_0 => 0x00, "Push an empty array onto the stack.";
79    OP_PUSHBYTES_1 => 0x01, "Push the next byte as an array onto the stack.";
80    OP_PUSHBYTES_2 => 0x02, "Push the next 2 bytes as an array onto the stack.";
81    OP_PUSHBYTES_3 => 0x03, "Push the next 3 bytes as an array onto the stack.";
82    OP_PUSHBYTES_4 => 0x04, "Push the next 4 bytes as an array onto the stack.";
83    OP_PUSHBYTES_5 => 0x05, "Push the next 5 bytes as an array onto the stack.";
84    OP_PUSHBYTES_6 => 0x06, "Push the next 6 bytes as an array onto the stack.";
85    OP_PUSHBYTES_7 => 0x07, "Push the next 7 bytes as an array onto the stack.";
86    OP_PUSHBYTES_8 => 0x08, "Push the next 8 bytes as an array onto the stack.";
87    OP_PUSHBYTES_9 => 0x09, "Push the next 9 bytes as an array onto the stack.";
88    OP_PUSHBYTES_10 => 0x0a, "Push the next 10 bytes as an array onto the stack.";
89    OP_PUSHBYTES_11 => 0x0b, "Push the next 11 bytes as an array onto the stack.";
90    OP_PUSHBYTES_12 => 0x0c, "Push the next 12 bytes as an array onto the stack.";
91    OP_PUSHBYTES_13 => 0x0d, "Push the next 13 bytes as an array onto the stack.";
92    OP_PUSHBYTES_14 => 0x0e, "Push the next 14 bytes as an array onto the stack.";
93    OP_PUSHBYTES_15 => 0x0f, "Push the next 15 bytes as an array onto the stack.";
94    OP_PUSHBYTES_16 => 0x10, "Push the next 16 bytes as an array onto the stack.";
95    OP_PUSHBYTES_17 => 0x11, "Push the next 17 bytes as an array onto the stack.";
96    OP_PUSHBYTES_18 => 0x12, "Push the next 18 bytes as an array onto the stack.";
97    OP_PUSHBYTES_19 => 0x13, "Push the next 19 bytes as an array onto the stack.";
98    OP_PUSHBYTES_20 => 0x14, "Push the next 20 bytes as an array onto the stack.";
99    OP_PUSHBYTES_21 => 0x15, "Push the next 21 bytes as an array onto the stack.";
100    OP_PUSHBYTES_22 => 0x16, "Push the next 22 bytes as an array onto the stack.";
101    OP_PUSHBYTES_23 => 0x17, "Push the next 23 bytes as an array onto the stack.";
102    OP_PUSHBYTES_24 => 0x18, "Push the next 24 bytes as an array onto the stack.";
103    OP_PUSHBYTES_25 => 0x19, "Push the next 25 bytes as an array onto the stack.";
104    OP_PUSHBYTES_26 => 0x1a, "Push the next 26 bytes as an array onto the stack.";
105    OP_PUSHBYTES_27 => 0x1b, "Push the next 27 bytes as an array onto the stack.";
106    OP_PUSHBYTES_28 => 0x1c, "Push the next 28 bytes as an array onto the stack.";
107    OP_PUSHBYTES_29 => 0x1d, "Push the next 29 bytes as an array onto the stack.";
108    OP_PUSHBYTES_30 => 0x1e, "Push the next 30 bytes as an array onto the stack.";
109    OP_PUSHBYTES_31 => 0x1f, "Push the next 31 bytes as an array onto the stack.";
110    OP_PUSHBYTES_32 => 0x20, "Push the next 32 bytes as an array onto the stack.";
111    OP_PUSHBYTES_33 => 0x21, "Push the next 33 bytes as an array onto the stack.";
112    OP_PUSHBYTES_34 => 0x22, "Push the next 34 bytes as an array onto the stack.";
113    OP_PUSHBYTES_35 => 0x23, "Push the next 35 bytes as an array onto the stack.";
114    OP_PUSHBYTES_36 => 0x24, "Push the next 36 bytes as an array onto the stack.";
115    OP_PUSHBYTES_37 => 0x25, "Push the next 37 bytes as an array onto the stack.";
116    OP_PUSHBYTES_38 => 0x26, "Push the next 38 bytes as an array onto the stack.";
117    OP_PUSHBYTES_39 => 0x27, "Push the next 39 bytes as an array onto the stack.";
118    OP_PUSHBYTES_40 => 0x28, "Push the next 40 bytes as an array onto the stack.";
119    OP_PUSHBYTES_41 => 0x29, "Push the next 41 bytes as an array onto the stack.";
120    OP_PUSHBYTES_42 => 0x2a, "Push the next 42 bytes as an array onto the stack.";
121    OP_PUSHBYTES_43 => 0x2b, "Push the next 43 bytes as an array onto the stack.";
122    OP_PUSHBYTES_44 => 0x2c, "Push the next 44 bytes as an array onto the stack.";
123    OP_PUSHBYTES_45 => 0x2d, "Push the next 45 bytes as an array onto the stack.";
124    OP_PUSHBYTES_46 => 0x2e, "Push the next 46 bytes as an array onto the stack.";
125    OP_PUSHBYTES_47 => 0x2f, "Push the next 47 bytes as an array onto the stack.";
126    OP_PUSHBYTES_48 => 0x30, "Push the next 48 bytes as an array onto the stack.";
127    OP_PUSHBYTES_49 => 0x31, "Push the next 49 bytes as an array onto the stack.";
128    OP_PUSHBYTES_50 => 0x32, "Push the next 50 bytes as an array onto the stack.";
129    OP_PUSHBYTES_51 => 0x33, "Push the next 51 bytes as an array onto the stack.";
130    OP_PUSHBYTES_52 => 0x34, "Push the next 52 bytes as an array onto the stack.";
131    OP_PUSHBYTES_53 => 0x35, "Push the next 53 bytes as an array onto the stack.";
132    OP_PUSHBYTES_54 => 0x36, "Push the next 54 bytes as an array onto the stack.";
133    OP_PUSHBYTES_55 => 0x37, "Push the next 55 bytes as an array onto the stack.";
134    OP_PUSHBYTES_56 => 0x38, "Push the next 56 bytes as an array onto the stack.";
135    OP_PUSHBYTES_57 => 0x39, "Push the next 57 bytes as an array onto the stack.";
136    OP_PUSHBYTES_58 => 0x3a, "Push the next 58 bytes as an array onto the stack.";
137    OP_PUSHBYTES_59 => 0x3b, "Push the next 59 bytes as an array onto the stack.";
138    OP_PUSHBYTES_60 => 0x3c, "Push the next 60 bytes as an array onto the stack.";
139    OP_PUSHBYTES_61 => 0x3d, "Push the next 61 bytes as an array onto the stack.";
140    OP_PUSHBYTES_62 => 0x3e, "Push the next 62 bytes as an array onto the stack.";
141    OP_PUSHBYTES_63 => 0x3f, "Push the next 63 bytes as an array onto the stack.";
142    OP_PUSHBYTES_64 => 0x40, "Push the next 64 bytes as an array onto the stack.";
143    OP_PUSHBYTES_65 => 0x41, "Push the next 65 bytes as an array onto the stack.";
144    OP_PUSHBYTES_66 => 0x42, "Push the next 66 bytes as an array onto the stack.";
145    OP_PUSHBYTES_67 => 0x43, "Push the next 67 bytes as an array onto the stack.";
146    OP_PUSHBYTES_68 => 0x44, "Push the next 68 bytes as an array onto the stack.";
147    OP_PUSHBYTES_69 => 0x45, "Push the next 69 bytes as an array onto the stack.";
148    OP_PUSHBYTES_70 => 0x46, "Push the next 70 bytes as an array onto the stack.";
149    OP_PUSHBYTES_71 => 0x47, "Push the next 71 bytes as an array onto the stack.";
150    OP_PUSHBYTES_72 => 0x48, "Push the next 72 bytes as an array onto the stack.";
151    OP_PUSHBYTES_73 => 0x49, "Push the next 73 bytes as an array onto the stack.";
152    OP_PUSHBYTES_74 => 0x4a, "Push the next 74 bytes as an array onto the stack.";
153    OP_PUSHBYTES_75 => 0x4b, "Push the next 75 bytes as an array onto the stack.";
154    OP_PUSHDATA1 => 0x4c, "Read the next byte as N; push the next N bytes as an array onto the stack.";
155    OP_PUSHDATA2 => 0x4d, "Read the next 2 bytes as N; push the next N bytes as an array onto the stack.";
156    OP_PUSHDATA4 => 0x4e, "Read the next 4 bytes as N; push the next N bytes as an array onto the stack.";
157    OP_PUSHNUM_NEG1 => 0x4f, "Push the array `0x81` onto the stack.";
158    OP_RESERVED => 0x50, "Synonym for OP_RETURN.";
159    OP_PUSHNUM_1 => 0x51, "Push the array `0x01` onto the stack.";
160    OP_PUSHNUM_2 => 0x52, "Push the array `0x02` onto the stack.";
161    OP_PUSHNUM_3 => 0x53, "Push the array `0x03` onto the stack.";
162    OP_PUSHNUM_4 => 0x54, "Push the array `0x04` onto the stack.";
163    OP_PUSHNUM_5 => 0x55, "Push the array `0x05` onto the stack.";
164    OP_PUSHNUM_6 => 0x56, "Push the array `0x06` onto the stack.";
165    OP_PUSHNUM_7 => 0x57, "Push the array `0x07` onto the stack.";
166    OP_PUSHNUM_8 => 0x58, "Push the array `0x08` onto the stack.";
167    OP_PUSHNUM_9 => 0x59, "Push the array `0x09` onto the stack.";
168    OP_PUSHNUM_10 => 0x5a, "Push the array `0x0a` onto the stack.";
169    OP_PUSHNUM_11 => 0x5b, "Push the array `0x0b` onto the stack.";
170    OP_PUSHNUM_12 => 0x5c, "Push the array `0x0c` onto the stack.";
171    OP_PUSHNUM_13 => 0x5d, "Push the array `0x0d` onto the stack.";
172    OP_PUSHNUM_14 => 0x5e, "Push the array `0x0e` onto the stack.";
173    OP_PUSHNUM_15 => 0x5f, "Push the array `0x0f` onto the stack.";
174    OP_PUSHNUM_16 => 0x60, "Push the array `0x10` onto the stack.";
175    OP_NOP => 0x61, "Does nothing.";
176    OP_VER => 0x62, "Synonym for OP_RETURN.";
177    OP_IF => 0x63, "Pop and execute the next statements if a nonzero element was popped.";
178    OP_NOTIF => 0x64, "Pop and execute the next statements if a zero element was popped.";
179    OP_VERIF => 0x65, "Fail the script unconditionally, does not even need to be executed.";
180    OP_VERNOTIF => 0x66, "Fail the script unconditionally, does not even need to be executed.";
181    OP_ELSE => 0x67, "Execute statements if those after the previous OP_IF were not, and vice-versa. \
182             If there is no previous OP_IF, this acts as a RETURN.";
183    OP_ENDIF => 0x68, "Pop and execute the next statements if a zero element was popped.";
184    OP_VERIFY => 0x69, "If the top value is zero or the stack is empty, fail; otherwise, pop the stack.";
185    OP_RETURN => 0x6a, "Fail the script immediately. (Must be executed.).";
186    OP_TOALTSTACK => 0x6b, "Pop one element from the main stack onto the alt stack.";
187    OP_FROMALTSTACK => 0x6c, "Pop one element from the alt stack onto the main stack.";
188    OP_2DROP => 0x6d, "Drops the top two stack items.";
189    OP_2DUP => 0x6e, "Duplicates the top two stack items as AB -> ABAB.";
190    OP_3DUP => 0x6f, "Duplicates the two three stack items as ABC -> ABCABC.";
191    OP_2OVER => 0x70, "Copies the two stack items of items two spaces back to the front, as xxAB -> ABxxAB.";
192    OP_2ROT => 0x71, "Moves the two stack items four spaces back to the front, as xxxxAB -> ABxxxx.";
193    OP_2SWAP => 0x72, "Swaps the top two pairs, as ABCD -> CDAB.";
194    OP_IFDUP => 0x73, "Duplicate the top stack element unless it is zero.";
195    OP_DEPTH => 0x74, "Push the current number of stack items onto the stack.";
196    OP_DROP => 0x75, "Drops the top stack item.";
197    OP_DUP => 0x76, "Duplicates the top stack item.";
198    OP_NIP => 0x77, "Drops the second-to-top stack item.";
199    OP_OVER => 0x78, "Copies the second-to-top stack item, as xA -> AxA.";
200    OP_PICK => 0x79, "Pop the top stack element as N. Copy the Nth stack element to the top.";
201    OP_ROLL => 0x7a, "Pop the top stack element as N. Move the Nth stack element to the top.";
202    OP_ROT => 0x7b, "Rotate the top three stack items, as [top next1 next2] -> [next2 top next1].";
203    OP_SWAP => 0x7c, "Swap the top two stack items.";
204    OP_TUCK => 0x7d, "Copy the top stack item to before the second item, as [top next] -> [top next top].";
205    OP_CAT => 0x7e, "Fail the script unconditionally, does not even need to be executed.";
206    OP_SUBSTR => 0x7f, "Fail the script unconditionally, does not even need to be executed.";
207    OP_LEFT => 0x80, "Fail the script unconditionally, does not even need to be executed.";
208    OP_RIGHT => 0x81, "Fail the script unconditionally, does not even need to be executed.";
209    OP_SIZE => 0x82, "Pushes the length of the top stack item onto the stack.";
210    OP_INVERT => 0x83, "Fail the script unconditionally, does not even need to be executed.";
211    OP_AND => 0x84, "Fail the script unconditionally, does not even need to be executed.";
212    OP_OR => 0x85, "Fail the script unconditionally, does not even need to be executed.";
213    OP_XOR => 0x86, "Fail the script unconditionally, does not even need to be executed.";
214    OP_EQUAL => 0x87, "Pushes 1 if the inputs are exactly equal, 0 otherwise.";
215    OP_EQUALVERIFY => 0x88, "Returns success if the inputs are exactly equal, failure otherwise.";
216    OP_RESERVED1 => 0x89, "Synonym for OP_RETURN.";
217    OP_RESERVED2 => 0x8a, "Synonym for OP_RETURN.";
218    OP_1ADD => 0x8b, "Increment the top stack element in place.";
219    OP_1SUB => 0x8c, "Decrement the top stack element in place.";
220    OP_2MUL => 0x8d, "Fail the script unconditionally, does not even need to be executed.";
221    OP_2DIV => 0x8e, "Fail the script unconditionally, does not even need to be executed.";
222    OP_NEGATE => 0x8f, "Multiply the top stack item by -1 in place.";
223    OP_ABS => 0x90, "Absolute value the top stack item in place.";
224    OP_NOT => 0x91, "Map 0 to 1 and everything else to 0, in place.";
225    OP_0NOTEQUAL => 0x92, "Map 0 to 0 and everything else to 1, in place.";
226    OP_ADD => 0x93, "Pop two stack items and push their sum.";
227    OP_SUB => 0x94, "Pop two stack items and push the second minus the top.";
228    OP_MUL => 0x95, "Fail the script unconditionally, does not even need to be executed.";
229    OP_DIV => 0x96, "Fail the script unconditionally, does not even need to be executed.";
230    OP_MOD => 0x97, "Fail the script unconditionally, does not even need to be executed.";
231    OP_LSHIFT => 0x98, "Fail the script unconditionally, does not even need to be executed.";
232    OP_RSHIFT => 0x99, "Fail the script unconditionally, does not even need to be executed.";
233    OP_BOOLAND => 0x9a, "Pop the top two stack items and push 1 if both are nonzero, else push 0.";
234    OP_BOOLOR => 0x9b, "Pop the top two stack items and push 1 if either is nonzero, else push 0.";
235    OP_NUMEQUAL => 0x9c, "Pop the top two stack items and push 1 if both are numerically equal, else push 0.";
236    OP_NUMEQUALVERIFY => 0x9d, "Pop the top two stack items and return success if both are numerically equal, else return failure.";
237    OP_NUMNOTEQUAL => 0x9e, "Pop the top two stack items and push 0 if both are numerically equal, else push 1.";
238    OP_LESSTHAN  => 0x9f, "Pop the top two items; push 1 if the second is less than the top, 0 otherwise.";
239    OP_GREATERTHAN  => 0xa0, "Pop the top two items; push 1 if the second is greater than the top, 0 otherwise.";
240    OP_LESSTHANOREQUAL  => 0xa1, "Pop the top two items; push 1 if the second is <= the top, 0 otherwise.";
241    OP_GREATERTHANOREQUAL  => 0xa2, "Pop the top two items; push 1 if the second is >= the top, 0 otherwise.";
242    OP_MIN => 0xa3, "Pop the top two items; push the smaller.";
243    OP_MAX => 0xa4, "Pop the top two items; push the larger.";
244    OP_WITHIN => 0xa5, "Pop the top three items; if the top is >= the second and < the third, push 1, otherwise push 0.";
245    OP_RIPEMD160 => 0xa6, "Pop the top stack item and push its RIPEMD160 hash.";
246    OP_SHA1 => 0xa7, "Pop the top stack item and push its SHA1 hash.";
247    OP_SHA256 => 0xa8, "Pop the top stack item and push its SHA256 hash.";
248    OP_HASH160 => 0xa9, "Pop the top stack item and push its RIPEMD(SHA256) hash.";
249    OP_HASH256 => 0xaa, "Pop the top stack item and push its SHA256(SHA256) hash.";
250    OP_CODESEPARATOR => 0xab, "Ignore this and everything preceding when deciding what to sign when signature-checking.";
251    OP_CHECKSIG => 0xac, "<https://en.bitcoin.it/wiki/OP_CHECKSIG> pushing 1/0 for success/failure.";
252    OP_CHECKSIGVERIFY => 0xad, "<https://en.bitcoin.it/wiki/OP_CHECKSIG> returning success/failure.";
253    OP_CHECKMULTISIG => 0xae, "Pop N, N pubkeys, M, M signatures, a dummy (due to bug in reference code), \
254                      and verify that all M signatures are valid. Push 1 for 'all valid', 0 otherwise.";
255    OP_CHECKMULTISIGVERIFY => 0xaf, "Like the above but return success/failure.";
256    OP_NOP1 => 0xb0, "Does nothing.";
257    OP_CLTV => 0xb1, "<https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki>";
258    OP_CSV => 0xb2, "<https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki>";
259    OP_NOP4 => 0xb3, "Does nothing.";
260    OP_NOP5 => 0xb4, "Does nothing.";
261    OP_NOP6 => 0xb5, "Does nothing.";
262    OP_NOP7 => 0xb6, "Does nothing.";
263    OP_NOP8 => 0xb7, "Does nothing.";
264    OP_NOP9 => 0xb8, "Does nothing.";
265    OP_NOP10 => 0xb9, "Does nothing.";
266    // Every other opcode acts as OP_RETURN
267    OP_CHECKSIGADD => 0xba, "OP_CHECKSIGADD post tapscript.";
268    OP_RETURN_187 => 0xbb, "Synonym for OP_RETURN.";
269    OP_RETURN_188 => 0xbc, "Synonym for OP_RETURN.";
270    OP_RETURN_189 => 0xbd, "Synonym for OP_RETURN.";
271    OP_RETURN_190 => 0xbe, "Synonym for OP_RETURN.";
272    OP_RETURN_191 => 0xbf, "Synonym for OP_RETURN.";
273    OP_RETURN_192 => 0xc0, "Synonym for OP_RETURN.";
274    OP_RETURN_193 => 0xc1, "Synonym for OP_RETURN.";
275    OP_RETURN_194 => 0xc2, "Synonym for OP_RETURN.";
276    OP_RETURN_195 => 0xc3, "Synonym for OP_RETURN.";
277    OP_RETURN_196 => 0xc4, "Synonym for OP_RETURN.";
278    OP_RETURN_197 => 0xc5, "Synonym for OP_RETURN.";
279    OP_RETURN_198 => 0xc6, "Synonym for OP_RETURN.";
280    OP_RETURN_199 => 0xc7, "Synonym for OP_RETURN.";
281    OP_RETURN_200 => 0xc8, "Synonym for OP_RETURN.";
282    OP_RETURN_201 => 0xc9, "Synonym for OP_RETURN.";
283    OP_RETURN_202 => 0xca, "Synonym for OP_RETURN.";
284    OP_RETURN_203 => 0xcb, "Synonym for OP_RETURN.";
285    OP_RETURN_204 => 0xcc, "Synonym for OP_RETURN.";
286    OP_RETURN_205 => 0xcd, "Synonym for OP_RETURN.";
287    OP_RETURN_206 => 0xce, "Synonym for OP_RETURN.";
288    OP_RETURN_207 => 0xcf, "Synonym for OP_RETURN.";
289    OP_RETURN_208 => 0xd0, "Synonym for OP_RETURN.";
290    OP_RETURN_209 => 0xd1, "Synonym for OP_RETURN.";
291    OP_RETURN_210 => 0xd2, "Synonym for OP_RETURN.";
292    OP_RETURN_211 => 0xd3, "Synonym for OP_RETURN.";
293    OP_RETURN_212 => 0xd4, "Synonym for OP_RETURN.";
294    OP_RETURN_213 => 0xd5, "Synonym for OP_RETURN.";
295    OP_RETURN_214 => 0xd6, "Synonym for OP_RETURN.";
296    OP_RETURN_215 => 0xd7, "Synonym for OP_RETURN.";
297    OP_RETURN_216 => 0xd8, "Synonym for OP_RETURN.";
298    OP_RETURN_217 => 0xd9, "Synonym for OP_RETURN.";
299    OP_RETURN_218 => 0xda, "Synonym for OP_RETURN.";
300    OP_RETURN_219 => 0xdb, "Synonym for OP_RETURN.";
301    OP_RETURN_220 => 0xdc, "Synonym for OP_RETURN.";
302    OP_RETURN_221 => 0xdd, "Synonym for OP_RETURN.";
303    OP_RETURN_222 => 0xde, "Synonym for OP_RETURN.";
304    OP_RETURN_223 => 0xdf, "Synonym for OP_RETURN.";
305    OP_RETURN_224 => 0xe0, "Synonym for OP_RETURN.";
306    OP_RETURN_225 => 0xe1, "Synonym for OP_RETURN.";
307    OP_RETURN_226 => 0xe2, "Synonym for OP_RETURN.";
308    OP_RETURN_227 => 0xe3, "Synonym for OP_RETURN.";
309    OP_RETURN_228 => 0xe4, "Synonym for OP_RETURN.";
310    OP_RETURN_229 => 0xe5, "Synonym for OP_RETURN.";
311    OP_RETURN_230 => 0xe6, "Synonym for OP_RETURN.";
312    OP_RETURN_231 => 0xe7, "Synonym for OP_RETURN.";
313    OP_RETURN_232 => 0xe8, "Synonym for OP_RETURN.";
314    OP_RETURN_233 => 0xe9, "Synonym for OP_RETURN.";
315    OP_RETURN_234 => 0xea, "Synonym for OP_RETURN.";
316    OP_RETURN_235 => 0xeb, "Synonym for OP_RETURN.";
317    OP_RETURN_236 => 0xec, "Synonym for OP_RETURN.";
318    OP_RETURN_237 => 0xed, "Synonym for OP_RETURN.";
319    OP_RETURN_238 => 0xee, "Synonym for OP_RETURN.";
320    OP_RETURN_239 => 0xef, "Synonym for OP_RETURN.";
321    OP_RETURN_240 => 0xf0, "Synonym for OP_RETURN.";
322    OP_RETURN_241 => 0xf1, "Synonym for OP_RETURN.";
323    OP_RETURN_242 => 0xf2, "Synonym for OP_RETURN.";
324    OP_RETURN_243 => 0xf3, "Synonym for OP_RETURN.";
325    OP_RETURN_244 => 0xf4, "Synonym for OP_RETURN.";
326    OP_RETURN_245 => 0xf5, "Synonym for OP_RETURN.";
327    OP_RETURN_246 => 0xf6, "Synonym for OP_RETURN.";
328    OP_RETURN_247 => 0xf7, "Synonym for OP_RETURN.";
329    OP_RETURN_248 => 0xf8, "Synonym for OP_RETURN.";
330    OP_RETURN_249 => 0xf9, "Synonym for OP_RETURN.";
331    OP_RETURN_250 => 0xfa, "Synonym for OP_RETURN.";
332    OP_RETURN_251 => 0xfb, "Synonym for OP_RETURN.";
333    OP_RETURN_252 => 0xfc, "Synonym for OP_RETURN.";
334    OP_RETURN_253 => 0xfd, "Synonym for OP_RETURN.";
335    OP_RETURN_254 => 0xfe, "Synonym for OP_RETURN.";
336    OP_INVALIDOPCODE => 0xff, "Synonym for OP_RETURN."
337}
338
339/// Classification context for the opcode.
340///
341/// Some opcodes like [`OP_RESERVED`] abort the script in `ClassifyContext::Legacy` context,
342/// but will act as `OP_SUCCESSx` in `ClassifyContext::TapScript` (see BIP342 for full list).
343#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
344pub enum ClassifyContext {
345    /// Opcode used in tapscript context.
346    TapScript,
347    /// Opcode used in legacy context.
348    Legacy,
349}
350
351impl Opcode {
352    /// Classifies an Opcode into a broad class.
353    #[inline]
354    pub fn classify(self, ctx: ClassifyContext) -> Class {
355        match (self, ctx) {
356            // 3 opcodes illegal in all contexts
357            (OP_VERIF, _) | (OP_VERNOTIF, _) | (OP_INVALIDOPCODE, _) => Class::IllegalOp,
358
359            // 15 opcodes illegal in Legacy context
360            #[rustfmt::skip]
361            (OP_CAT, ctx) | (OP_SUBSTR, ctx)
362            | (OP_LEFT, ctx) | (OP_RIGHT, ctx)
363            | (OP_INVERT, ctx)
364            | (OP_AND, ctx) | (OP_OR, ctx) | (OP_XOR, ctx)
365            | (OP_2MUL, ctx) | (OP_2DIV, ctx)
366            | (OP_MUL, ctx) | (OP_DIV, ctx) | (OP_MOD, ctx)
367            | (OP_LSHIFT, ctx) | (OP_RSHIFT, ctx) if ctx == ClassifyContext::Legacy => Class::IllegalOp,
368
369            // 87 opcodes of SuccessOp class only in TapScript context
370            (op, ClassifyContext::TapScript)
371                if op.code == 80
372                    || op.code == 98
373                    || (op.code >= 126 && op.code <= 129)
374                    || (op.code >= 131 && op.code <= 134)
375                    || (op.code >= 137 && op.code <= 138)
376                    || (op.code >= 141 && op.code <= 142)
377                    || (op.code >= 149 && op.code <= 153)
378                    || (op.code >= 187 && op.code <= 254) =>
379                Class::SuccessOp,
380
381            // 11 opcodes of NoOp class
382            (OP_NOP, _) => Class::NoOp,
383            (op, _) if op.code >= OP_NOP1.code && op.code <= OP_NOP10.code => Class::NoOp,
384
385            // 1 opcode for `OP_RETURN`
386            (OP_RETURN, _) => Class::ReturnOp,
387
388            // 4 opcodes operating equally to `OP_RETURN` only in Legacy context
389            (OP_RESERVED, ctx) | (OP_RESERVED1, ctx) | (OP_RESERVED2, ctx) | (OP_VER, ctx)
390                if ctx == ClassifyContext::Legacy =>
391                Class::ReturnOp,
392
393            // 71 opcodes operating equally to `OP_RETURN` only in Legacy context
394            (op, ClassifyContext::Legacy) if op.code >= OP_CHECKSIGADD.code => Class::ReturnOp,
395
396            // 2 opcodes operating equally to `OP_RETURN` only in TapScript context
397            (OP_CHECKMULTISIG, ClassifyContext::TapScript)
398            | (OP_CHECKMULTISIGVERIFY, ClassifyContext::TapScript) => Class::ReturnOp,
399
400            // 1 opcode of PushNum class
401            (OP_PUSHNUM_NEG1, _) => Class::PushNum(-1),
402
403            // 16 opcodes of PushNum class
404            (op, _) if op.code >= OP_PUSHNUM_1.code && op.code <= OP_PUSHNUM_16.code =>
405                Class::PushNum(1 + self.code as i32 - OP_PUSHNUM_1.code as i32),
406
407            // 76 opcodes of PushBytes class
408            (op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(self.code as u32),
409
410            // opcodes of Ordinary class: 61 for Legacy and 60 for TapScript context
411            (_, _) => Class::Ordinary(Ordinary::with(self)),
412        }
413    }
414
415    /// Encodes [`Opcode`] as a byte.
416    #[inline]
417    pub const fn to_u8(self) -> u8 { self.code }
418
419    /// Encodes PUSHNUM [`Opcode`] as a `u8` representing its number (1-16).
420    ///
421    /// Does not convert `OP_FALSE` to 0. Only `1` to `OP_PUSHNUM_16` are covered.
422    ///
423    /// # Returns
424    ///
425    /// Returns `None` if `self` is not a PUSHNUM.
426    #[inline]
427    pub const fn decode_pushnum(self) -> Option<u8> {
428        const START: u8 = OP_PUSHNUM_1.code;
429        const END: u8 = OP_PUSHNUM_16.code;
430        match self.code {
431            START..=END => Some(self.code - START + 1),
432            _ => None,
433        }
434    }
435}
436
437impl From<u8> for Opcode {
438    #[inline]
439    fn from(b: u8) -> Opcode { Opcode { code: b } }
440}
441
442debug_from_display!(Opcode);
443
444#[cfg(feature = "serde")]
445impl serde::Serialize for Opcode {
446    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
447    where
448        S: serde::Serializer,
449    {
450        serializer.serialize_str(&self.to_string())
451    }
452}
453
454/// Broad categories of opcodes with similar behavior.
455#[derive(Copy, Clone, PartialEq, Eq, Debug)]
456pub enum Class {
457    /// Pushes the given number onto the stack.
458    PushNum(i32),
459    /// Pushes the given number of bytes onto the stack.
460    PushBytes(u32),
461    /// Fails the script if executed.
462    ReturnOp,
463    /// Succeeds the script even if not executed.
464    SuccessOp,
465    /// Fails the script even if not executed.
466    IllegalOp,
467    /// Does nothing.
468    NoOp,
469    /// Any opcode not covered above.
470    Ordinary(Ordinary),
471}
472
473macro_rules! ordinary_opcode {
474    ($($op:ident),*) => (
475        #[repr(u8)]
476        #[doc(hidden)]
477        #[derive(Copy, Clone, PartialEq, Eq, Debug)]
478        pub enum Ordinary {
479            $( $op = $op.code ),*
480        }
481
482        impl fmt::Display for Ordinary {
483            fn fmt(&self, f: &mut fmt::Formatter) -> core::fmt::Result {
484                match *self {
485                   $(Ordinary::$op => { f.pad(stringify!($op)) }),*
486                }
487            }
488        }
489
490        impl Ordinary {
491            fn with(b: Opcode) -> Self {
492                match b {
493                    $( $op => { Ordinary::$op } ),*
494                    _ => unreachable!("construction of `Ordinary` type from non-ordinary opcode {}", b),
495                }
496            }
497
498            /// Constructs a new [`Ordinary`] from an [`Opcode`].
499            pub fn from_opcode(b: Opcode) -> Option<Self> {
500                match b {
501                    $( $op => { Some(Ordinary::$op) } ),*
502                    _ => None,
503                }
504            }
505        }
506    );
507}
508
509// "Ordinary" opcodes -- should be 61 of these
510ordinary_opcode! {
511    // pushdata
512    OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4,
513    // control flow
514    OP_IF, OP_NOTIF, OP_ELSE, OP_ENDIF, OP_VERIFY,
515    // stack
516    OP_TOALTSTACK, OP_FROMALTSTACK,
517    OP_2DROP, OP_2DUP, OP_3DUP, OP_2OVER, OP_2ROT, OP_2SWAP,
518    OP_DROP, OP_DUP, OP_NIP, OP_OVER, OP_PICK, OP_ROLL, OP_ROT, OP_SWAP, OP_TUCK,
519    OP_IFDUP, OP_DEPTH, OP_SIZE,
520    // equality
521    OP_EQUAL, OP_EQUALVERIFY,
522    // arithmetic
523    OP_1ADD, OP_1SUB, OP_NEGATE, OP_ABS, OP_NOT, OP_0NOTEQUAL,
524    OP_ADD, OP_SUB, OP_BOOLAND, OP_BOOLOR,
525    OP_NUMEQUAL, OP_NUMEQUALVERIFY, OP_NUMNOTEQUAL, OP_LESSTHAN,
526    OP_GREATERTHAN, OP_LESSTHANOREQUAL, OP_GREATERTHANOREQUAL,
527    OP_MIN, OP_MAX, OP_WITHIN,
528    // crypto
529    OP_RIPEMD160, OP_SHA1, OP_SHA256, OP_HASH160, OP_HASH256,
530    OP_CODESEPARATOR, OP_CHECKSIG, OP_CHECKSIGVERIFY,
531    OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY,
532    OP_CHECKSIGADD
533}
534
535impl Ordinary {
536    /// Encodes [`Opcode`] as a byte.
537    #[inline]
538    pub fn to_u8(self) -> u8 { self as u8 }
539}
540
541#[cfg(test)]
542mod tests {
543    use std::collections::HashSet;
544
545    use super::*;
546
547    macro_rules! roundtrip {
548        ($unique:expr, $op:ident) => {
549            assert_eq!($op, Opcode::from($op.to_u8()));
550
551            let s1 = format!("{}", $op);
552            let s2 = format!("{:?}", $op);
553            assert_eq!(s1, s2);
554            assert_eq!(s1, stringify!($op));
555            assert!($unique.insert(s1));
556        };
557    }
558
559    #[test]
560    fn formatting_works() {
561        let op = all::OP_NOP;
562        let s = format!("{:>10}", op);
563        assert_eq!(s, "    OP_NOP");
564    }
565
566    #[test]
567    fn decode_pushnum() {
568        // Test all possible opcodes
569        // - Sanity check
570        assert_eq!(OP_PUSHNUM_1.code, 0x51_u8);
571        assert_eq!(OP_PUSHNUM_16.code, 0x60_u8);
572        for i in 0x00..=0xff_u8 {
573            let expected = match i {
574                // OP_PUSHNUM_1 ..= OP_PUSHNUM_16
575                0x51..=0x60 => Some(i - 0x50),
576                _ => None,
577            };
578            assert_eq!(Opcode::from(i).decode_pushnum(), expected);
579        }
580
581        // Test the named opcode constants
582        // - This is the OP right before PUSHNUMs start
583        assert!(OP_RESERVED.decode_pushnum().is_none());
584        assert_eq!(OP_PUSHNUM_1.decode_pushnum().expect("pushnum"), 1);
585        assert_eq!(OP_PUSHNUM_2.decode_pushnum().expect("pushnum"), 2);
586        assert_eq!(OP_PUSHNUM_3.decode_pushnum().expect("pushnum"), 3);
587        assert_eq!(OP_PUSHNUM_4.decode_pushnum().expect("pushnum"), 4);
588        assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5);
589        assert_eq!(OP_PUSHNUM_6.decode_pushnum().expect("pushnum"), 6);
590        assert_eq!(OP_PUSHNUM_7.decode_pushnum().expect("pushnum"), 7);
591        assert_eq!(OP_PUSHNUM_8.decode_pushnum().expect("pushnum"), 8);
592        assert_eq!(OP_PUSHNUM_9.decode_pushnum().expect("pushnum"), 9);
593        assert_eq!(OP_PUSHNUM_10.decode_pushnum().expect("pushnum"), 10);
594        assert_eq!(OP_PUSHNUM_11.decode_pushnum().expect("pushnum"), 11);
595        assert_eq!(OP_PUSHNUM_12.decode_pushnum().expect("pushnum"), 12);
596        assert_eq!(OP_PUSHNUM_13.decode_pushnum().expect("pushnum"), 13);
597        assert_eq!(OP_PUSHNUM_14.decode_pushnum().expect("pushnum"), 14);
598        assert_eq!(OP_PUSHNUM_15.decode_pushnum().expect("pushnum"), 15);
599        assert_eq!(OP_PUSHNUM_16.decode_pushnum().expect("pushnum"), 16);
600        // - This is the OP right after PUSHNUMs end
601        assert!(OP_NOP.decode_pushnum().is_none());
602    }
603
604    #[test]
605    fn classify_test() {
606        let op174 = OP_CHECKMULTISIG;
607        assert_eq!(
608            op174.classify(ClassifyContext::Legacy),
609            Class::Ordinary(Ordinary::OP_CHECKMULTISIG)
610        );
611        assert_eq!(op174.classify(ClassifyContext::TapScript), Class::ReturnOp);
612
613        let op175 = OP_CHECKMULTISIGVERIFY;
614        assert_eq!(
615            op175.classify(ClassifyContext::Legacy),
616            Class::Ordinary(Ordinary::OP_CHECKMULTISIGVERIFY)
617        );
618        assert_eq!(op175.classify(ClassifyContext::TapScript), Class::ReturnOp);
619
620        let op186 = OP_CHECKSIGADD;
621        assert_eq!(op186.classify(ClassifyContext::Legacy), Class::ReturnOp);
622        assert_eq!(
623            op186.classify(ClassifyContext::TapScript),
624            Class::Ordinary(Ordinary::OP_CHECKSIGADD)
625        );
626
627        let op187 = OP_RETURN_187;
628        assert_eq!(op187.classify(ClassifyContext::Legacy), Class::ReturnOp);
629        assert_eq!(op187.classify(ClassifyContext::TapScript), Class::SuccessOp);
630    }
631
632    #[test]
633    fn str_roundtrip() {
634        let mut unique = HashSet::new();
635        roundtrip!(unique, OP_PUSHBYTES_0);
636        roundtrip!(unique, OP_PUSHBYTES_1);
637        roundtrip!(unique, OP_PUSHBYTES_2);
638        roundtrip!(unique, OP_PUSHBYTES_3);
639        roundtrip!(unique, OP_PUSHBYTES_4);
640        roundtrip!(unique, OP_PUSHBYTES_5);
641        roundtrip!(unique, OP_PUSHBYTES_6);
642        roundtrip!(unique, OP_PUSHBYTES_7);
643        roundtrip!(unique, OP_PUSHBYTES_8);
644        roundtrip!(unique, OP_PUSHBYTES_9);
645        roundtrip!(unique, OP_PUSHBYTES_10);
646        roundtrip!(unique, OP_PUSHBYTES_11);
647        roundtrip!(unique, OP_PUSHBYTES_12);
648        roundtrip!(unique, OP_PUSHBYTES_13);
649        roundtrip!(unique, OP_PUSHBYTES_14);
650        roundtrip!(unique, OP_PUSHBYTES_15);
651        roundtrip!(unique, OP_PUSHBYTES_16);
652        roundtrip!(unique, OP_PUSHBYTES_17);
653        roundtrip!(unique, OP_PUSHBYTES_18);
654        roundtrip!(unique, OP_PUSHBYTES_19);
655        roundtrip!(unique, OP_PUSHBYTES_20);
656        roundtrip!(unique, OP_PUSHBYTES_21);
657        roundtrip!(unique, OP_PUSHBYTES_22);
658        roundtrip!(unique, OP_PUSHBYTES_23);
659        roundtrip!(unique, OP_PUSHBYTES_24);
660        roundtrip!(unique, OP_PUSHBYTES_25);
661        roundtrip!(unique, OP_PUSHBYTES_26);
662        roundtrip!(unique, OP_PUSHBYTES_27);
663        roundtrip!(unique, OP_PUSHBYTES_28);
664        roundtrip!(unique, OP_PUSHBYTES_29);
665        roundtrip!(unique, OP_PUSHBYTES_30);
666        roundtrip!(unique, OP_PUSHBYTES_31);
667        roundtrip!(unique, OP_PUSHBYTES_32);
668        roundtrip!(unique, OP_PUSHBYTES_33);
669        roundtrip!(unique, OP_PUSHBYTES_34);
670        roundtrip!(unique, OP_PUSHBYTES_35);
671        roundtrip!(unique, OP_PUSHBYTES_36);
672        roundtrip!(unique, OP_PUSHBYTES_37);
673        roundtrip!(unique, OP_PUSHBYTES_38);
674        roundtrip!(unique, OP_PUSHBYTES_39);
675        roundtrip!(unique, OP_PUSHBYTES_40);
676        roundtrip!(unique, OP_PUSHBYTES_41);
677        roundtrip!(unique, OP_PUSHBYTES_42);
678        roundtrip!(unique, OP_PUSHBYTES_43);
679        roundtrip!(unique, OP_PUSHBYTES_44);
680        roundtrip!(unique, OP_PUSHBYTES_45);
681        roundtrip!(unique, OP_PUSHBYTES_46);
682        roundtrip!(unique, OP_PUSHBYTES_47);
683        roundtrip!(unique, OP_PUSHBYTES_48);
684        roundtrip!(unique, OP_PUSHBYTES_49);
685        roundtrip!(unique, OP_PUSHBYTES_50);
686        roundtrip!(unique, OP_PUSHBYTES_51);
687        roundtrip!(unique, OP_PUSHBYTES_52);
688        roundtrip!(unique, OP_PUSHBYTES_53);
689        roundtrip!(unique, OP_PUSHBYTES_54);
690        roundtrip!(unique, OP_PUSHBYTES_55);
691        roundtrip!(unique, OP_PUSHBYTES_56);
692        roundtrip!(unique, OP_PUSHBYTES_57);
693        roundtrip!(unique, OP_PUSHBYTES_58);
694        roundtrip!(unique, OP_PUSHBYTES_59);
695        roundtrip!(unique, OP_PUSHBYTES_60);
696        roundtrip!(unique, OP_PUSHBYTES_61);
697        roundtrip!(unique, OP_PUSHBYTES_62);
698        roundtrip!(unique, OP_PUSHBYTES_63);
699        roundtrip!(unique, OP_PUSHBYTES_64);
700        roundtrip!(unique, OP_PUSHBYTES_65);
701        roundtrip!(unique, OP_PUSHBYTES_66);
702        roundtrip!(unique, OP_PUSHBYTES_67);
703        roundtrip!(unique, OP_PUSHBYTES_68);
704        roundtrip!(unique, OP_PUSHBYTES_69);
705        roundtrip!(unique, OP_PUSHBYTES_70);
706        roundtrip!(unique, OP_PUSHBYTES_71);
707        roundtrip!(unique, OP_PUSHBYTES_72);
708        roundtrip!(unique, OP_PUSHBYTES_73);
709        roundtrip!(unique, OP_PUSHBYTES_74);
710        roundtrip!(unique, OP_PUSHBYTES_75);
711        roundtrip!(unique, OP_PUSHDATA1);
712        roundtrip!(unique, OP_PUSHDATA2);
713        roundtrip!(unique, OP_PUSHDATA4);
714        roundtrip!(unique, OP_PUSHNUM_NEG1);
715        roundtrip!(unique, OP_RESERVED);
716        roundtrip!(unique, OP_PUSHNUM_1);
717        roundtrip!(unique, OP_PUSHNUM_2);
718        roundtrip!(unique, OP_PUSHNUM_3);
719        roundtrip!(unique, OP_PUSHNUM_4);
720        roundtrip!(unique, OP_PUSHNUM_5);
721        roundtrip!(unique, OP_PUSHNUM_6);
722        roundtrip!(unique, OP_PUSHNUM_7);
723        roundtrip!(unique, OP_PUSHNUM_8);
724        roundtrip!(unique, OP_PUSHNUM_9);
725        roundtrip!(unique, OP_PUSHNUM_10);
726        roundtrip!(unique, OP_PUSHNUM_11);
727        roundtrip!(unique, OP_PUSHNUM_12);
728        roundtrip!(unique, OP_PUSHNUM_13);
729        roundtrip!(unique, OP_PUSHNUM_14);
730        roundtrip!(unique, OP_PUSHNUM_15);
731        roundtrip!(unique, OP_PUSHNUM_16);
732        roundtrip!(unique, OP_NOP);
733        roundtrip!(unique, OP_VER);
734        roundtrip!(unique, OP_IF);
735        roundtrip!(unique, OP_NOTIF);
736        roundtrip!(unique, OP_VERIF);
737        roundtrip!(unique, OP_VERNOTIF);
738        roundtrip!(unique, OP_ELSE);
739        roundtrip!(unique, OP_ENDIF);
740        roundtrip!(unique, OP_VERIFY);
741        roundtrip!(unique, OP_RETURN);
742        roundtrip!(unique, OP_TOALTSTACK);
743        roundtrip!(unique, OP_FROMALTSTACK);
744        roundtrip!(unique, OP_2DROP);
745        roundtrip!(unique, OP_2DUP);
746        roundtrip!(unique, OP_3DUP);
747        roundtrip!(unique, OP_2OVER);
748        roundtrip!(unique, OP_2ROT);
749        roundtrip!(unique, OP_2SWAP);
750        roundtrip!(unique, OP_IFDUP);
751        roundtrip!(unique, OP_DEPTH);
752        roundtrip!(unique, OP_DROP);
753        roundtrip!(unique, OP_DUP);
754        roundtrip!(unique, OP_NIP);
755        roundtrip!(unique, OP_OVER);
756        roundtrip!(unique, OP_PICK);
757        roundtrip!(unique, OP_ROLL);
758        roundtrip!(unique, OP_ROT);
759        roundtrip!(unique, OP_SWAP);
760        roundtrip!(unique, OP_TUCK);
761        roundtrip!(unique, OP_CAT);
762        roundtrip!(unique, OP_SUBSTR);
763        roundtrip!(unique, OP_LEFT);
764        roundtrip!(unique, OP_RIGHT);
765        roundtrip!(unique, OP_SIZE);
766        roundtrip!(unique, OP_INVERT);
767        roundtrip!(unique, OP_AND);
768        roundtrip!(unique, OP_OR);
769        roundtrip!(unique, OP_XOR);
770        roundtrip!(unique, OP_EQUAL);
771        roundtrip!(unique, OP_EQUALVERIFY);
772        roundtrip!(unique, OP_RESERVED1);
773        roundtrip!(unique, OP_RESERVED2);
774        roundtrip!(unique, OP_1ADD);
775        roundtrip!(unique, OP_1SUB);
776        roundtrip!(unique, OP_2MUL);
777        roundtrip!(unique, OP_2DIV);
778        roundtrip!(unique, OP_NEGATE);
779        roundtrip!(unique, OP_ABS);
780        roundtrip!(unique, OP_NOT);
781        roundtrip!(unique, OP_0NOTEQUAL);
782        roundtrip!(unique, OP_ADD);
783        roundtrip!(unique, OP_SUB);
784        roundtrip!(unique, OP_MUL);
785        roundtrip!(unique, OP_DIV);
786        roundtrip!(unique, OP_MOD);
787        roundtrip!(unique, OP_LSHIFT);
788        roundtrip!(unique, OP_RSHIFT);
789        roundtrip!(unique, OP_BOOLAND);
790        roundtrip!(unique, OP_BOOLOR);
791        roundtrip!(unique, OP_NUMEQUAL);
792        roundtrip!(unique, OP_NUMEQUALVERIFY);
793        roundtrip!(unique, OP_NUMNOTEQUAL);
794        roundtrip!(unique, OP_LESSTHAN);
795        roundtrip!(unique, OP_GREATERTHAN);
796        roundtrip!(unique, OP_LESSTHANOREQUAL);
797        roundtrip!(unique, OP_GREATERTHANOREQUAL);
798        roundtrip!(unique, OP_MIN);
799        roundtrip!(unique, OP_MAX);
800        roundtrip!(unique, OP_WITHIN);
801        roundtrip!(unique, OP_RIPEMD160);
802        roundtrip!(unique, OP_SHA1);
803        roundtrip!(unique, OP_SHA256);
804        roundtrip!(unique, OP_HASH160);
805        roundtrip!(unique, OP_HASH256);
806        roundtrip!(unique, OP_CODESEPARATOR);
807        roundtrip!(unique, OP_CHECKSIG);
808        roundtrip!(unique, OP_CHECKSIGVERIFY);
809        roundtrip!(unique, OP_CHECKMULTISIG);
810        roundtrip!(unique, OP_CHECKMULTISIGVERIFY);
811        roundtrip!(unique, OP_NOP1);
812        roundtrip!(unique, OP_CLTV);
813        roundtrip!(unique, OP_CSV);
814        roundtrip!(unique, OP_NOP4);
815        roundtrip!(unique, OP_NOP5);
816        roundtrip!(unique, OP_NOP6);
817        roundtrip!(unique, OP_NOP7);
818        roundtrip!(unique, OP_NOP8);
819        roundtrip!(unique, OP_NOP9);
820        roundtrip!(unique, OP_NOP10);
821        roundtrip!(unique, OP_CHECKSIGADD);
822        roundtrip!(unique, OP_RETURN_187);
823        roundtrip!(unique, OP_RETURN_188);
824        roundtrip!(unique, OP_RETURN_189);
825        roundtrip!(unique, OP_RETURN_190);
826        roundtrip!(unique, OP_RETURN_191);
827        roundtrip!(unique, OP_RETURN_192);
828        roundtrip!(unique, OP_RETURN_193);
829        roundtrip!(unique, OP_RETURN_194);
830        roundtrip!(unique, OP_RETURN_195);
831        roundtrip!(unique, OP_RETURN_196);
832        roundtrip!(unique, OP_RETURN_197);
833        roundtrip!(unique, OP_RETURN_198);
834        roundtrip!(unique, OP_RETURN_199);
835        roundtrip!(unique, OP_RETURN_200);
836        roundtrip!(unique, OP_RETURN_201);
837        roundtrip!(unique, OP_RETURN_202);
838        roundtrip!(unique, OP_RETURN_203);
839        roundtrip!(unique, OP_RETURN_204);
840        roundtrip!(unique, OP_RETURN_205);
841        roundtrip!(unique, OP_RETURN_206);
842        roundtrip!(unique, OP_RETURN_207);
843        roundtrip!(unique, OP_RETURN_208);
844        roundtrip!(unique, OP_RETURN_209);
845        roundtrip!(unique, OP_RETURN_210);
846        roundtrip!(unique, OP_RETURN_211);
847        roundtrip!(unique, OP_RETURN_212);
848        roundtrip!(unique, OP_RETURN_213);
849        roundtrip!(unique, OP_RETURN_214);
850        roundtrip!(unique, OP_RETURN_215);
851        roundtrip!(unique, OP_RETURN_216);
852        roundtrip!(unique, OP_RETURN_217);
853        roundtrip!(unique, OP_RETURN_218);
854        roundtrip!(unique, OP_RETURN_219);
855        roundtrip!(unique, OP_RETURN_220);
856        roundtrip!(unique, OP_RETURN_221);
857        roundtrip!(unique, OP_RETURN_222);
858        roundtrip!(unique, OP_RETURN_223);
859        roundtrip!(unique, OP_RETURN_224);
860        roundtrip!(unique, OP_RETURN_225);
861        roundtrip!(unique, OP_RETURN_226);
862        roundtrip!(unique, OP_RETURN_227);
863        roundtrip!(unique, OP_RETURN_228);
864        roundtrip!(unique, OP_RETURN_229);
865        roundtrip!(unique, OP_RETURN_230);
866        roundtrip!(unique, OP_RETURN_231);
867        roundtrip!(unique, OP_RETURN_232);
868        roundtrip!(unique, OP_RETURN_233);
869        roundtrip!(unique, OP_RETURN_234);
870        roundtrip!(unique, OP_RETURN_235);
871        roundtrip!(unique, OP_RETURN_236);
872        roundtrip!(unique, OP_RETURN_237);
873        roundtrip!(unique, OP_RETURN_238);
874        roundtrip!(unique, OP_RETURN_239);
875        roundtrip!(unique, OP_RETURN_240);
876        roundtrip!(unique, OP_RETURN_241);
877        roundtrip!(unique, OP_RETURN_242);
878        roundtrip!(unique, OP_RETURN_243);
879        roundtrip!(unique, OP_RETURN_244);
880        roundtrip!(unique, OP_RETURN_245);
881        roundtrip!(unique, OP_RETURN_246);
882        roundtrip!(unique, OP_RETURN_247);
883        roundtrip!(unique, OP_RETURN_248);
884        roundtrip!(unique, OP_RETURN_249);
885        roundtrip!(unique, OP_RETURN_250);
886        roundtrip!(unique, OP_RETURN_251);
887        roundtrip!(unique, OP_RETURN_252);
888        roundtrip!(unique, OP_RETURN_253);
889        roundtrip!(unique, OP_RETURN_254);
890        roundtrip!(unique, OP_INVALIDOPCODE);
891        assert_eq!(unique.len(), 256);
892    }
893}