sapio_bitcoin/blockdata/
opcodes.rs

1// Rust Bitcoin Library
2// Written in 2014 by
3//   Andrew Poelstra <apoelstra@wpsoftware.net>
4//
5// To the extent possible under law, the author(s) have dedicated all
6// copyright and related and neighboring rights to this software to
7// the public domain worldwide. This software is distributed without
8// any warranty.
9//
10// You should have received a copy of the CC0 Public Domain Dedication
11// along with this software.
12// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13//
14
15//! Bitcoin script opcodes.
16//!
17//! Bitcoin's script uses a stack-based assembly language. This module defines
18//! all of the opcodes for that language.
19//!
20
21#![allow(non_camel_case_types)]
22
23#[cfg(feature = "serde")] use serde;
24
25#[cfg(feature = "serde")] use prelude::*;
26
27use core::{fmt, convert::From};
28
29// Note: I am deliberately not implementing PartialOrd or Ord on the
30//       opcode enum. If you want to check ranges of opcodes, etc.,
31//       write an #[inline] helper function which casts to u8s.
32
33/// A script Opcode.
34#[derive(Copy, Clone, PartialEq, Eq)]
35pub struct All {
36    code: u8,
37}
38
39pub mod all {
40    //! Constants associated with All type
41    use super::All;
42
43    /// Push an empty array onto the stack.
44    pub const OP_PUSHBYTES_0: All = All {code: 0x00};
45    /// Push the next byte as an array onto the stack.
46    pub const OP_PUSHBYTES_1: All = All {code: 0x01};
47    /// Push the next 2 bytes as an array onto the stack.
48    pub const OP_PUSHBYTES_2: All = All {code: 0x02};
49    /// Push the next 2 bytes as an array onto the stack.
50    pub const OP_PUSHBYTES_3: All = All {code: 0x03};
51    /// Push the next 4 bytes as an array onto the stack.
52    pub const OP_PUSHBYTES_4: All = All {code: 0x04};
53    /// Push the next 5 bytes as an array onto the stack.
54    pub const OP_PUSHBYTES_5: All = All {code: 0x05};
55    /// Push the next 6 bytes as an array onto the stack.
56    pub const OP_PUSHBYTES_6: All = All {code: 0x06};
57    /// Push the next 7 bytes as an array onto the stack.
58    pub const OP_PUSHBYTES_7: All = All {code: 0x07};
59    /// Push the next 8 bytes as an array onto the stack.
60    pub const OP_PUSHBYTES_8: All = All {code: 0x08};
61    /// Push the next 9 bytes as an array onto the stack.
62    pub const OP_PUSHBYTES_9: All = All {code: 0x09};
63    /// Push the next 10 bytes as an array onto the stack.
64    pub const OP_PUSHBYTES_10: All = All {code: 0x0a};
65    /// Push the next 11 bytes as an array onto the stack.
66    pub const OP_PUSHBYTES_11: All = All {code: 0x0b};
67    /// Push the next 12 bytes as an array onto the stack.
68    pub const OP_PUSHBYTES_12: All = All {code: 0x0c};
69    /// Push the next 13 bytes as an array onto the stack.
70    pub const OP_PUSHBYTES_13: All = All {code: 0x0d};
71    /// Push the next 14 bytes as an array onto the stack.
72    pub const OP_PUSHBYTES_14: All = All {code: 0x0e};
73    /// Push the next 15 bytes as an array onto the stack.
74    pub const OP_PUSHBYTES_15: All = All {code: 0x0f};
75    /// Push the next 16 bytes as an array onto the stack.
76    pub const OP_PUSHBYTES_16: All = All {code: 0x10};
77    /// Push the next 17 bytes as an array onto the stack.
78    pub const OP_PUSHBYTES_17: All = All {code: 0x11};
79    /// Push the next 18 bytes as an array onto the stack.
80    pub const OP_PUSHBYTES_18: All = All {code: 0x12};
81    /// Push the next 19 bytes as an array onto the stack.
82    pub const OP_PUSHBYTES_19: All = All {code: 0x13};
83    /// Push the next 20 bytes as an array onto the stack.
84    pub const OP_PUSHBYTES_20: All = All {code: 0x14};
85    /// Push the next 21 bytes as an array onto the stack.
86    pub const OP_PUSHBYTES_21: All = All {code: 0x15};
87    /// Push the next 22 bytes as an array onto the stack.
88    pub const OP_PUSHBYTES_22: All = All {code: 0x16};
89    /// Push the next 23 bytes as an array onto the stack.
90    pub const OP_PUSHBYTES_23: All = All {code: 0x17};
91    /// Push the next 24 bytes as an array onto the stack.
92    pub const OP_PUSHBYTES_24: All = All {code: 0x18};
93    /// Push the next 25 bytes as an array onto the stack.
94    pub const OP_PUSHBYTES_25: All = All {code: 0x19};
95    /// Push the next 26 bytes as an array onto the stack.
96    pub const OP_PUSHBYTES_26: All = All {code: 0x1a};
97    /// Push the next 27 bytes as an array onto the stack.
98    pub const OP_PUSHBYTES_27: All = All {code: 0x1b};
99    /// Push the next 28 bytes as an array onto the stack.
100    pub const OP_PUSHBYTES_28: All = All {code: 0x1c};
101    /// Push the next 29 bytes as an array onto the stack.
102    pub const OP_PUSHBYTES_29: All = All {code: 0x1d};
103    /// Push the next 30 bytes as an array onto the stack.
104    pub const OP_PUSHBYTES_30: All = All {code: 0x1e};
105    /// Push the next 31 bytes as an array onto the stack.
106    pub const OP_PUSHBYTES_31: All = All {code: 0x1f};
107    /// Push the next 32 bytes as an array onto the stack.
108    pub const OP_PUSHBYTES_32: All = All {code: 0x20};
109    /// Push the next 33 bytes as an array onto the stack.
110    pub const OP_PUSHBYTES_33: All = All {code: 0x21};
111    /// Push the next 34 bytes as an array onto the stack.
112    pub const OP_PUSHBYTES_34: All = All {code: 0x22};
113    /// Push the next 35 bytes as an array onto the stack.
114    pub const OP_PUSHBYTES_35: All = All {code: 0x23};
115    /// Push the next 36 bytes as an array onto the stack.
116    pub const OP_PUSHBYTES_36: All = All {code: 0x24};
117    /// Push the next 37 bytes as an array onto the stack.
118    pub const OP_PUSHBYTES_37: All = All {code: 0x25};
119    /// Push the next 38 bytes as an array onto the stack.
120    pub const OP_PUSHBYTES_38: All = All {code: 0x26};
121    /// Push the next 39 bytes as an array onto the stack.
122    pub const OP_PUSHBYTES_39: All = All {code: 0x27};
123    /// Push the next 40 bytes as an array onto the stack.
124    pub const OP_PUSHBYTES_40: All = All {code: 0x28};
125    /// Push the next 41 bytes as an array onto the stack.
126    pub const OP_PUSHBYTES_41: All = All {code: 0x29};
127    /// Push the next 42 bytes as an array onto the stack.
128    pub const OP_PUSHBYTES_42: All = All {code: 0x2a};
129    /// Push the next 43 bytes as an array onto the stack.
130    pub const OP_PUSHBYTES_43: All = All {code: 0x2b};
131    /// Push the next 44 bytes as an array onto the stack.
132    pub const OP_PUSHBYTES_44: All = All {code: 0x2c};
133    /// Push the next 45 bytes as an array onto the stack.
134    pub const OP_PUSHBYTES_45: All = All {code: 0x2d};
135    /// Push the next 46 bytes as an array onto the stack.
136    pub const OP_PUSHBYTES_46: All = All {code: 0x2e};
137    /// Push the next 47 bytes as an array onto the stack.
138    pub const OP_PUSHBYTES_47: All = All {code: 0x2f};
139    /// Push the next 48 bytes as an array onto the stack.
140    pub const OP_PUSHBYTES_48: All = All {code: 0x30};
141    /// Push the next 49 bytes as an array onto the stack.
142    pub const OP_PUSHBYTES_49: All = All {code: 0x31};
143    /// Push the next 50 bytes as an array onto the stack.
144    pub const OP_PUSHBYTES_50: All = All {code: 0x32};
145    /// Push the next 51 bytes as an array onto the stack.
146    pub const OP_PUSHBYTES_51: All = All {code: 0x33};
147    /// Push the next 52 bytes as an array onto the stack.
148    pub const OP_PUSHBYTES_52: All = All {code: 0x34};
149    /// Push the next 53 bytes as an array onto the stack.
150    pub const OP_PUSHBYTES_53: All = All {code: 0x35};
151    /// Push the next 54 bytes as an array onto the stack.
152    pub const OP_PUSHBYTES_54: All = All {code: 0x36};
153    /// Push the next 55 bytes as an array onto the stack.
154    pub const OP_PUSHBYTES_55: All = All {code: 0x37};
155    /// Push the next 56 bytes as an array onto the stack.
156    pub const OP_PUSHBYTES_56: All = All {code: 0x38};
157    /// Push the next 57 bytes as an array onto the stack.
158    pub const OP_PUSHBYTES_57: All = All {code: 0x39};
159    /// Push the next 58 bytes as an array onto the stack.
160    pub const OP_PUSHBYTES_58: All = All {code: 0x3a};
161    /// Push the next 59 bytes as an array onto the stack.
162    pub const OP_PUSHBYTES_59: All = All {code: 0x3b};
163    /// Push the next 60 bytes as an array onto the stack.
164    pub const OP_PUSHBYTES_60: All = All {code: 0x3c};
165    /// Push the next 61 bytes as an array onto the stack.
166    pub const OP_PUSHBYTES_61: All = All {code: 0x3d};
167    /// Push the next 62 bytes as an array onto the stack.
168    pub const OP_PUSHBYTES_62: All = All {code: 0x3e};
169    /// Push the next 63 bytes as an array onto the stack.
170    pub const OP_PUSHBYTES_63: All = All {code: 0x3f};
171    /// Push the next 64 bytes as an array onto the stack.
172    pub const OP_PUSHBYTES_64: All = All {code: 0x40};
173    /// Push the next 65 bytes as an array onto the stack.
174    pub const OP_PUSHBYTES_65: All = All {code: 0x41};
175    /// Push the next 66 bytes as an array onto the stack.
176    pub const OP_PUSHBYTES_66: All = All {code: 0x42};
177    /// Push the next 67 bytes as an array onto the stack.
178    pub const OP_PUSHBYTES_67: All = All {code: 0x43};
179    /// Push the next 68 bytes as an array onto the stack.
180    pub const OP_PUSHBYTES_68: All = All {code: 0x44};
181    /// Push the next 69 bytes as an array onto the stack.
182    pub const OP_PUSHBYTES_69: All = All {code: 0x45};
183    /// Push the next 70 bytes as an array onto the stack.
184    pub const OP_PUSHBYTES_70: All = All {code: 0x46};
185    /// Push the next 71 bytes as an array onto the stack.
186    pub const OP_PUSHBYTES_71: All = All {code: 0x47};
187    /// Push the next 72 bytes as an array onto the stack.
188    pub const OP_PUSHBYTES_72: All = All {code: 0x48};
189    /// Push the next 73 bytes as an array onto the stack.
190    pub const OP_PUSHBYTES_73: All = All {code: 0x49};
191    /// Push the next 74 bytes as an array onto the stack.
192    pub const OP_PUSHBYTES_74: All = All {code: 0x4a};
193    /// Push the next 75 bytes as an array onto the stack.
194    pub const OP_PUSHBYTES_75: All = All {code: 0x4b};
195    /// Read the next byte as N; push the next N bytes as an array onto the stack.
196    pub const OP_PUSHDATA1: All = All {code: 0x4c};
197    /// Read the next 2 bytes as N; push the next N bytes as an array onto the stack.
198    pub const OP_PUSHDATA2: All = All {code: 0x4d};
199    /// Read the next 4 bytes as N; push the next N bytes as an array onto the stack.
200    pub const OP_PUSHDATA4: All = All {code: 0x4e};
201    /// Push the array `0x81` onto the stack.
202    pub const OP_PUSHNUM_NEG1: All = All {code: 0x4f};
203    /// Synonym for OP_RETURN.
204    pub const OP_RESERVED: All = All {code: 0x50};
205    /// Push the array `0x01` onto the stack.
206    pub const OP_PUSHNUM_1: All = All {code: 0x51};
207    /// Push the array `0x02` onto the stack.
208    pub const OP_PUSHNUM_2: All = All {code: 0x52};
209    /// Push the array `0x03` onto the stack.
210    pub const OP_PUSHNUM_3: All = All {code: 0x53};
211    /// Push the array `0x04` onto the stack.
212    pub const OP_PUSHNUM_4: All = All {code: 0x54};
213    /// Push the array `0x05` onto the stack.
214    pub const OP_PUSHNUM_5: All = All {code: 0x55};
215    /// Push the array `0x06` onto the stack.
216    pub const OP_PUSHNUM_6: All = All {code: 0x56};
217    /// Push the array `0x07` onto the stack.
218    pub const OP_PUSHNUM_7: All = All {code: 0x57};
219    /// Push the array `0x08` onto the stack.
220    pub const OP_PUSHNUM_8: All = All {code: 0x58};
221    /// Push the array `0x09` onto the stack.
222    pub const OP_PUSHNUM_9: All = All {code: 0x59};
223    /// Push the array `0x0a` onto the stack.
224    pub const OP_PUSHNUM_10: All = All {code: 0x5a};
225    /// Push the array `0x0b` onto the stack.
226    pub const OP_PUSHNUM_11: All = All {code: 0x5b};
227    /// Push the array `0x0c` onto the stack.
228    pub const OP_PUSHNUM_12: All = All {code: 0x5c};
229    /// Push the array `0x0d` onto the stack.
230    pub const OP_PUSHNUM_13: All = All {code: 0x5d};
231    /// Push the array `0x0e` onto the stack.
232    pub const OP_PUSHNUM_14: All = All {code: 0x5e};
233    /// Push the array `0x0f` onto the stack.
234    pub const OP_PUSHNUM_15: All = All {code: 0x5f};
235    /// Push the array `0x10` onto the stack.
236    pub const OP_PUSHNUM_16: All = All {code: 0x60};
237    /// Does nothing.
238    pub const OP_NOP: All = All {code: 0x61};
239    /// Synonym for OP_RETURN.
240    pub const OP_VER: All = All {code: 0x62};
241    /// Pop and execute the next statements if a nonzero element was popped.
242    pub const OP_IF: All = All {code: 0x63};
243    /// Pop and execute the next statements if a zero element was popped.
244    pub const OP_NOTIF: All = All {code: 0x64};
245    /// Fail the script unconditionally, does not even need to be executed.
246    pub const OP_VERIF: All = All {code: 0x65};
247    /// Fail the script unconditionally, does not even need to be executed.
248    pub const OP_VERNOTIF: All = All {code: 0x66};
249    /// Execute statements if those after the previous OP_IF were not, and vice-versa.
250    /// If there is no previous OP_IF, this acts as a RETURN.
251    pub const OP_ELSE: All = All {code: 0x67};
252    /// Pop and execute the next statements if a zero element was popped.
253    pub const OP_ENDIF: All = All {code: 0x68};
254    /// If the top value is zero or the stack is empty, fail; otherwise, pop the stack.
255    pub const OP_VERIFY: All = All {code: 0x69};
256    /// Fail the script immediately. (Must be executed.).
257    pub const OP_RETURN: All = All {code: 0x6a};
258    /// Pop one element from the main stack onto the alt stack.
259    pub const OP_TOALTSTACK: All = All {code: 0x6b};
260    /// Pop one element from the alt stack onto the main stack.
261    pub const OP_FROMALTSTACK: All = All {code: 0x6c};
262    /// Drops the top two stack items.
263    pub const OP_2DROP: All = All {code: 0x6d};
264    /// Duplicates the top two stack items as AB -> ABAB.
265    pub const OP_2DUP: All = All {code: 0x6e};
266    /// Duplicates the two three stack items as ABC -> ABCABC.
267    pub const OP_3DUP: All = All {code: 0x6f};
268    /// Copies the two stack items of items two spaces back to
269    /// the front, as xxAB -> ABxxAB.
270    pub const OP_2OVER: All = All {code: 0x70};
271    /// Moves the two stack items four spaces back to the front,
272    /// as xxxxAB -> ABxxxx.
273    pub const OP_2ROT: All = All {code: 0x71};
274    /// Swaps the top two pairs, as ABCD -> CDAB.
275    pub const OP_2SWAP: All = All {code: 0x72};
276    /// Duplicate the top stack element unless it is zero.
277    pub const OP_IFDUP: All = All {code: 0x73};
278    /// Push the current number of stack items onto the stack.
279    pub const OP_DEPTH: All = All {code: 0x74};
280    /// Drops the top stack item.
281    pub const OP_DROP: All = All {code: 0x75};
282    /// Duplicates the top stack item.
283    pub const OP_DUP: All = All {code: 0x76};
284    /// Drops the second-to-top stack item.
285    pub const OP_NIP: All = All {code: 0x77};
286    /// Copies the second-to-top stack item, as xA -> AxA.
287    pub const OP_OVER: All = All {code: 0x78};
288    /// Pop the top stack element as N. Copy the Nth stack element to the top.
289    pub const OP_PICK: All = All {code: 0x79};
290    /// Pop the top stack element as N. Move the Nth stack element to the top.
291    pub const OP_ROLL: All = All {code: 0x7a};
292    /// Rotate the top three stack items, as [top next1 next2] -> [next2 top next1].
293    pub const OP_ROT: All = All {code: 0x7b};
294    /// Swap the top two stack items.
295    pub const OP_SWAP: All = All {code: 0x7c};
296    /// Copy the top stack item to before the second item, as [top next] -> [top next top].
297    pub const OP_TUCK: All = All {code: 0x7d};
298    /// Fail the script unconditionally, does not even need to be executed.
299    pub const OP_CAT: All = All {code: 0x7e};
300    /// Fail the script unconditionally, does not even need to be executed.
301    pub const OP_SUBSTR: All = All {code: 0x7f};
302    /// Fail the script unconditionally, does not even need to be executed.
303    pub const OP_LEFT: All = All {code: 0x80};
304    /// Fail the script unconditionally, does not even need to be executed.
305    pub const OP_RIGHT: All = All {code: 0x81};
306    /// Pushes the length of the top stack item onto the stack.
307    pub const OP_SIZE: All = All {code: 0x82};
308    /// Fail the script unconditionally, does not even need to be executed.
309    pub const OP_INVERT: All = All {code: 0x83};
310    /// Fail the script unconditionally, does not even need to be executed.
311    pub const OP_AND: All = All {code: 0x84};
312    /// Fail the script unconditionally, does not even need to be executed.
313    pub const OP_OR: All = All {code: 0x85};
314    /// Fail the script unconditionally, does not even need to be executed.
315    pub const OP_XOR: All = All {code: 0x86};
316    /// Pushes 1 if the inputs are exactly equal, 0 otherwise.
317    pub const OP_EQUAL: All = All {code: 0x87};
318    /// Returns success if the inputs are exactly equal, failure otherwise.
319    pub const OP_EQUALVERIFY: All = All {code: 0x88};
320    /// Synonym for OP_RETURN.
321    pub const OP_RESERVED1: All = All {code: 0x89};
322    /// Synonym for OP_RETURN.
323    pub const OP_RESERVED2: All = All {code: 0x8a};
324    /// Increment the top stack element in place.
325    pub const OP_1ADD: All = All {code: 0x8b};
326    /// Decrement the top stack element in place.
327    pub const OP_1SUB: All = All {code: 0x8c};
328    /// Fail the script unconditionally, does not even need to be executed.
329    pub const OP_2MUL: All = All {code: 0x8d};
330    /// Fail the script unconditionally, does not even need to be executed.
331    pub const OP_2DIV: All = All {code: 0x8e};
332    /// Multiply the top stack item by -1 in place.
333    pub const OP_NEGATE: All = All {code: 0x8f};
334    /// Absolute value the top stack item in place.
335    pub const OP_ABS: All = All {code: 0x90};
336    /// Map 0 to 1 and everything else to 0, in place.
337    pub const OP_NOT: All = All {code: 0x91};
338    /// Map 0 to 0 and everything else to 1, in place.
339    pub const OP_0NOTEQUAL: All = All {code: 0x92};
340    /// Pop two stack items and push their sum.
341    pub const OP_ADD: All = All {code: 0x93};
342    /// Pop two stack items and push the second minus the top.
343    pub const OP_SUB: All = All {code: 0x94};
344    /// Fail the script unconditionally, does not even need to be executed.
345    pub const OP_MUL: All = All {code: 0x95};
346    /// Fail the script unconditionally, does not even need to be executed.
347    pub const OP_DIV: All = All {code: 0x96};
348    /// Fail the script unconditionally, does not even need to be executed.
349    pub const OP_MOD: All = All {code: 0x97};
350    /// Fail the script unconditionally, does not even need to be executed.
351    pub const OP_LSHIFT: All = All {code: 0x98};
352    /// Fail the script unconditionally, does not even need to be executed.
353    pub const OP_RSHIFT: All = All {code: 0x99};
354    /// Pop the top two stack items and push 1 if both are nonzero, else push 0.
355    pub const OP_BOOLAND: All = All {code: 0x9a};
356    /// Pop the top two stack items and push 1 if either is nonzero, else push 0.
357    pub const OP_BOOLOR: All = All {code: 0x9b};
358    /// Pop the top two stack items and push 1 if both are numerically equal, else push 0.
359    pub const OP_NUMEQUAL: All = All {code: 0x9c};
360    /// Pop the top two stack items and return success if both are numerically equal, else return failure.
361    pub const OP_NUMEQUALVERIFY: All = All {code: 0x9d};
362    /// Pop the top two stack items and push 0 if both are numerically equal, else push 1.
363    pub const OP_NUMNOTEQUAL: All = All {code: 0x9e};
364    /// Pop the top two items; push 1 if the second is less than the top, 0 otherwise.
365    pub const OP_LESSTHAN : All = All {code: 0x9f};
366    /// Pop the top two items; push 1 if the second is greater than the top, 0 otherwise.
367    pub const OP_GREATERTHAN : All = All {code: 0xa0};
368    /// Pop the top two items; push 1 if the second is <= the top, 0 otherwise.
369    pub const OP_LESSTHANOREQUAL : All = All {code: 0xa1};
370    /// Pop the top two items; push 1 if the second is >= the top, 0 otherwise.
371    pub const OP_GREATERTHANOREQUAL : All = All {code: 0xa2};
372    /// Pop the top two items; push the smaller.
373    pub const OP_MIN: All = All {code: 0xa3};
374    /// Pop the top two items; push the larger.
375    pub const OP_MAX: All = All {code: 0xa4};
376    /// Pop the top three items; if the top is >= the second and < the third, push 1, otherwise push 0.
377    pub const OP_WITHIN: All = All {code: 0xa5};
378    /// Pop the top stack item and push its RIPEMD160 hash.
379    pub const OP_RIPEMD160: All = All {code: 0xa6};
380    /// Pop the top stack item and push its SHA1 hash.
381    pub const OP_SHA1: All = All {code: 0xa7};
382    /// Pop the top stack item and push its SHA256 hash.
383    pub const OP_SHA256: All = All {code: 0xa8};
384    /// Pop the top stack item and push its RIPEMD(SHA256) hash.
385    pub const OP_HASH160: All = All {code: 0xa9};
386    /// Pop the top stack item and push its SHA256(SHA256) hash.
387    pub const OP_HASH256: All = All {code: 0xaa};
388    /// Ignore this and everything preceding when deciding what to sign when signature-checking.
389    pub const OP_CODESEPARATOR: All = All {code: 0xab};
390    /// <https://en.bitcoin.it/wiki/OP_CHECKSIG> pushing 1/0 for success/failure.
391    pub const OP_CHECKSIG: All = All {code: 0xac};
392    /// <https://en.bitcoin.it/wiki/OP_CHECKSIG> returning success/failure.
393    pub const OP_CHECKSIGVERIFY: All = All {code: 0xad};
394    /// Pop N, N pubkeys, M, M signatures, a dummy (due to bug in reference code), and verify that all M signatures are valid.
395    /// Push 1 for "all valid", 0 otherwise.
396    pub const OP_CHECKMULTISIG: All = All {code: 0xae};
397    /// Like the above but return success/failure.
398    pub const OP_CHECKMULTISIGVERIFY: All = All {code: 0xaf};
399    /// Does nothing.
400    pub const OP_NOP1: All = All {code: 0xb0};
401    /// <https://github.com/bitcoin/bips/blob/master/bip-0065.mediawiki>
402    pub const OP_CLTV: All = All {code: 0xb1};
403    /// <https://github.com/bitcoin/bips/blob/master/bip-0112.mediawiki>
404    pub const OP_CSV: All = All {code: 0xb2};
405    /// Does nothing.
406    pub const OP_NOP4: All = All {code: 0xb3};
407    /// Does nothing.
408    pub const OP_NOP5: All = All {code: 0xb4};
409    /// Does nothing.
410    pub const OP_NOP6: All = All {code: 0xb5};
411    /// Does nothing.
412    pub const OP_NOP7: All = All {code: 0xb6};
413    /// Does nothing.
414    pub const OP_NOP8: All = All {code: 0xb7};
415    /// Does nothing.
416    pub const OP_NOP9: All = All {code: 0xb8};
417    /// Does nothing.
418    pub const OP_NOP10: All = All {code: 0xb9};
419    // Every other opcode acts as OP_RETURN
420    /// OP_CHECKSIGADD post tapscript.
421    pub const OP_CHECKSIGADD: All = All {code: 0xba};
422    /// Synonym for OP_RETURN.
423    pub const OP_RETURN_187: All = All {code: 0xbb};
424    /// Synonym for OP_RETURN.
425    pub const OP_RETURN_188: All = All {code: 0xbc};
426    /// Synonym for OP_RETURN.
427    pub const OP_RETURN_189: All = All {code: 0xbd};
428    /// Synonym for OP_RETURN.
429    pub const OP_RETURN_190: All = All {code: 0xbe};
430    /// Synonym for OP_RETURN.
431    pub const OP_RETURN_191: All = All {code: 0xbf};
432    /// Synonym for OP_RETURN.
433    pub const OP_RETURN_192: All = All {code: 0xc0};
434    /// Synonym for OP_RETURN.
435    pub const OP_RETURN_193: All = All {code: 0xc1};
436    /// Synonym for OP_RETURN.
437    pub const OP_RETURN_194: All = All {code: 0xc2};
438    /// Synonym for OP_RETURN.
439    pub const OP_RETURN_195: All = All {code: 0xc3};
440    /// Synonym for OP_RETURN.
441    pub const OP_RETURN_196: All = All {code: 0xc4};
442    /// Synonym for OP_RETURN.
443    pub const OP_RETURN_197: All = All {code: 0xc5};
444    /// Synonym for OP_RETURN.
445    pub const OP_RETURN_198: All = All {code: 0xc6};
446    /// Synonym for OP_RETURN.
447    pub const OP_RETURN_199: All = All {code: 0xc7};
448    /// Synonym for OP_RETURN.
449    pub const OP_RETURN_200: All = All {code: 0xc8};
450    /// Synonym for OP_RETURN.
451    pub const OP_RETURN_201: All = All {code: 0xc9};
452    /// Synonym for OP_RETURN.
453    pub const OP_RETURN_202: All = All {code: 0xca};
454    /// Synonym for OP_RETURN.
455    pub const OP_RETURN_203: All = All {code: 0xcb};
456    /// Synonym for OP_RETURN.
457    pub const OP_RETURN_204: All = All {code: 0xcc};
458    /// Synonym for OP_RETURN.
459    pub const OP_RETURN_205: All = All {code: 0xcd};
460    /// Synonym for OP_RETURN.
461    pub const OP_RETURN_206: All = All {code: 0xce};
462    /// Synonym for OP_RETURN.
463    pub const OP_RETURN_207: All = All {code: 0xcf};
464    /// Synonym for OP_RETURN.
465    pub const OP_RETURN_208: All = All {code: 0xd0};
466    /// Synonym for OP_RETURN.
467    pub const OP_RETURN_209: All = All {code: 0xd1};
468    /// Synonym for OP_RETURN.
469    pub const OP_RETURN_210: All = All {code: 0xd2};
470    /// Synonym for OP_RETURN.
471    pub const OP_RETURN_211: All = All {code: 0xd3};
472    /// Synonym for OP_RETURN.
473    pub const OP_RETURN_212: All = All {code: 0xd4};
474    /// Synonym for OP_RETURN.
475    pub const OP_RETURN_213: All = All {code: 0xd5};
476    /// Synonym for OP_RETURN.
477    pub const OP_RETURN_214: All = All {code: 0xd6};
478    /// Synonym for OP_RETURN.
479    pub const OP_RETURN_215: All = All {code: 0xd7};
480    /// Synonym for OP_RETURN.
481    pub const OP_RETURN_216: All = All {code: 0xd8};
482    /// Synonym for OP_RETURN.
483    pub const OP_RETURN_217: All = All {code: 0xd9};
484    /// Synonym for OP_RETURN.
485    pub const OP_RETURN_218: All = All {code: 0xda};
486    /// Synonym for OP_RETURN.
487    pub const OP_RETURN_219: All = All {code: 0xdb};
488    /// Synonym for OP_RETURN.
489    pub const OP_RETURN_220: All = All {code: 0xdc};
490    /// Synonym for OP_RETURN.
491    pub const OP_RETURN_221: All = All {code: 0xdd};
492    /// Synonym for OP_RETURN.
493    pub const OP_RETURN_222: All = All {code: 0xde};
494    /// Synonym for OP_RETURN.
495    pub const OP_RETURN_223: All = All {code: 0xdf};
496    /// Synonym for OP_RETURN.
497    pub const OP_RETURN_224: All = All {code: 0xe0};
498    /// Synonym for OP_RETURN.
499    pub const OP_RETURN_225: All = All {code: 0xe1};
500    /// Synonym for OP_RETURN.
501    pub const OP_RETURN_226: All = All {code: 0xe2};
502    /// Synonym for OP_RETURN.
503    pub const OP_RETURN_227: All = All {code: 0xe3};
504    /// Synonym for OP_RETURN.
505    pub const OP_RETURN_228: All = All {code: 0xe4};
506    /// Synonym for OP_RETURN.
507    pub const OP_RETURN_229: All = All {code: 0xe5};
508    /// Synonym for OP_RETURN.
509    pub const OP_RETURN_230: All = All {code: 0xe6};
510    /// Synonym for OP_RETURN.
511    pub const OP_RETURN_231: All = All {code: 0xe7};
512    /// Synonym for OP_RETURN.
513    pub const OP_RETURN_232: All = All {code: 0xe8};
514    /// Synonym for OP_RETURN.
515    pub const OP_RETURN_233: All = All {code: 0xe9};
516    /// Synonym for OP_RETURN.
517    pub const OP_RETURN_234: All = All {code: 0xea};
518    /// Synonym for OP_RETURN.
519    pub const OP_RETURN_235: All = All {code: 0xeb};
520    /// Synonym for OP_RETURN.
521    pub const OP_RETURN_236: All = All {code: 0xec};
522    /// Synonym for OP_RETURN.
523    pub const OP_RETURN_237: All = All {code: 0xed};
524    /// Synonym for OP_RETURN.
525    pub const OP_RETURN_238: All = All {code: 0xee};
526    /// Synonym for OP_RETURN.
527    pub const OP_RETURN_239: All = All {code: 0xef};
528    /// Synonym for OP_RETURN.
529    pub const OP_RETURN_240: All = All {code: 0xf0};
530    /// Synonym for OP_RETURN.
531    pub const OP_RETURN_241: All = All {code: 0xf1};
532    /// Synonym for OP_RETURN.
533    pub const OP_RETURN_242: All = All {code: 0xf2};
534    /// Synonym for OP_RETURN.
535    pub const OP_RETURN_243: All = All {code: 0xf3};
536    /// Synonym for OP_RETURN.
537    pub const OP_RETURN_244: All = All {code: 0xf4};
538    /// Synonym for OP_RETURN.
539    pub const OP_RETURN_245: All = All {code: 0xf5};
540    /// Synonym for OP_RETURN.
541    pub const OP_RETURN_246: All = All {code: 0xf6};
542    /// Synonym for OP_RETURN.
543    pub const OP_RETURN_247: All = All {code: 0xf7};
544    /// Synonym for OP_RETURN.
545    pub const OP_RETURN_248: All = All {code: 0xf8};
546    /// Synonym for OP_RETURN.
547    pub const OP_RETURN_249: All = All {code: 0xf9};
548    /// Synonym for OP_RETURN.
549    pub const OP_RETURN_250: All = All {code: 0xfa};
550    /// Synonym for OP_RETURN.
551    pub const OP_RETURN_251: All = All {code: 0xfb};
552    /// Synonym for OP_RETURN.
553    pub const OP_RETURN_252: All = All {code: 0xfc};
554    /// Synonym for OP_RETURN.
555    pub const OP_RETURN_253: All = All {code: 0xfd};
556    /// Synonym for OP_RETURN.
557    pub const OP_RETURN_254: All = All {code: 0xfe};
558    /// Synonym for OP_RETURN.
559    pub const OP_INVALIDOPCODE: All = All {code: 0xff};
560}
561
562impl fmt::Debug for All {
563    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
564        f.write_str("OP_")?;
565        match *self {
566            All {code: x} if x <= 75 => write!(f, "PUSHBYTES_{}", self.code),
567            all::OP_PUSHDATA1 => write!(f, "PUSHDATA1"),
568            all::OP_PUSHDATA2 => write!(f, "PUSHDATA2"),
569            all::OP_PUSHDATA4 => write!(f, "PUSHDATA4"),
570            all::OP_PUSHNUM_NEG1 => write!(f, "PUSHNUM_NEG1"),
571            all::OP_RESERVED => write!(f, "RESERVED"),
572            All {code: x} if x >= all::OP_PUSHNUM_1.code && x <= all::OP_PUSHNUM_16.code => write!(f, "PUSHNUM_{}", x - all::OP_PUSHNUM_1.code + 1),
573            all::OP_NOP => write!(f, "NOP"),
574            all::OP_VER => write!(f, "VER"),
575            all::OP_IF => write!(f, "IF"),
576            all::OP_NOTIF => write!(f, "NOTIF"),
577            all::OP_VERIF => write!(f, "VERIF"),
578            all::OP_VERNOTIF => write!(f, "VERNOTIF"),
579            all::OP_ELSE => write!(f, "ELSE"),
580            all::OP_ENDIF => write!(f, "ENDIF"),
581            all::OP_VERIFY => write!(f, "VERIFY"),
582            all::OP_RETURN => write!(f, "RETURN"),
583            all::OP_TOALTSTACK => write!(f, "TOALTSTACK"),
584            all::OP_FROMALTSTACK => write!(f, "FROMALTSTACK"),
585            all::OP_2DROP => write!(f, "2DROP"),
586            all::OP_2DUP => write!(f, "2DUP"),
587            all::OP_3DUP => write!(f, "3DUP"),
588            all::OP_2OVER => write!(f, "2OVER"),
589            all::OP_2ROT => write!(f, "2ROT"),
590            all::OP_2SWAP => write!(f, "2SWAP"),
591            all::OP_IFDUP => write!(f, "IFDUP"),
592            all::OP_DEPTH => write!(f, "DEPTH"),
593            all::OP_DROP => write!(f, "DROP"),
594            all::OP_DUP => write!(f, "DUP"),
595            all::OP_NIP => write!(f, "NIP"),
596            all::OP_OVER => write!(f, "OVER"),
597            all::OP_PICK => write!(f, "PICK"),
598            all::OP_ROLL => write!(f, "ROLL"),
599            all::OP_ROT => write!(f, "ROT"),
600            all::OP_SWAP => write!(f, "SWAP"),
601            all::OP_TUCK => write!(f, "TUCK"),
602            all::OP_CAT => write!(f, "CAT"),
603            all::OP_SUBSTR => write!(f, "SUBSTR"),
604            all::OP_LEFT => write!(f, "LEFT"),
605            all::OP_RIGHT => write!(f, "RIGHT"),
606            all::OP_SIZE => write!(f, "SIZE"),
607            all::OP_INVERT => write!(f, "INVERT"),
608            all::OP_AND => write!(f, "AND"),
609            all::OP_OR => write!(f, "OR"),
610            all::OP_XOR => write!(f, "XOR"),
611            all::OP_EQUAL => write!(f, "EQUAL"),
612            all::OP_EQUALVERIFY => write!(f, "EQUALVERIFY"),
613            all::OP_RESERVED1 => write!(f, "RESERVED1"),
614            all::OP_RESERVED2 => write!(f, "RESERVED2"),
615            all::OP_1ADD => write!(f, "1ADD"),
616            all::OP_1SUB => write!(f, "1SUB"),
617            all::OP_2MUL => write!(f, "2MUL"),
618            all::OP_2DIV => write!(f, "2DIV"),
619            all::OP_NEGATE => write!(f, "NEGATE"),
620            all::OP_ABS => write!(f, "ABS"),
621            all::OP_NOT => write!(f, "NOT"),
622            all::OP_0NOTEQUAL => write!(f, "0NOTEQUAL"),
623            all::OP_ADD => write!(f, "ADD"),
624            all::OP_SUB => write!(f, "SUB"),
625            all::OP_MUL => write!(f, "MUL"),
626            all::OP_DIV => write!(f, "DIV"),
627            all::OP_MOD => write!(f, "MOD"),
628            all::OP_LSHIFT => write!(f, "LSHIFT"),
629            all::OP_RSHIFT => write!(f, "RSHIFT"),
630            all::OP_BOOLAND => write!(f, "BOOLAND"),
631            all::OP_BOOLOR => write!(f, "BOOLOR"),
632            all::OP_NUMEQUAL => write!(f, "NUMEQUAL"),
633            all::OP_NUMEQUALVERIFY => write!(f, "NUMEQUALVERIFY"),
634            all::OP_NUMNOTEQUAL => write!(f, "NUMNOTEQUAL"),
635            all::OP_LESSTHAN => write!(f, "LESSTHAN"),
636            all::OP_GREATERTHAN => write!(f, "GREATERTHAN"),
637            all::OP_LESSTHANOREQUAL => write!(f, "LESSTHANOREQUAL"),
638            all::OP_GREATERTHANOREQUAL => write!(f, "GREATERTHANOREQUAL"),
639            all::OP_MIN => write!(f, "MIN"),
640            all::OP_MAX => write!(f, "MAX"),
641            all::OP_WITHIN => write!(f, "WITHIN"),
642            all::OP_RIPEMD160 => write!(f, "RIPEMD160"),
643            all::OP_SHA1 => write!(f, "SHA1"),
644            all::OP_SHA256 => write!(f, "SHA256"),
645            all::OP_HASH160 => write!(f, "HASH160"),
646            all::OP_HASH256 => write!(f, "HASH256"),
647            all::OP_CODESEPARATOR => write!(f, "CODESEPARATOR"),
648            all::OP_CHECKSIG => write!(f, "CHECKSIG"),
649            all::OP_CHECKSIGVERIFY => write!(f, "CHECKSIGVERIFY"),
650            all::OP_CHECKMULTISIG => write!(f, "CHECKMULTISIG"),
651            all::OP_CHECKMULTISIGVERIFY => write!(f, "CHECKMULTISIGVERIFY"),
652            all::OP_CLTV => write!(f, "CLTV"),
653            all::OP_CSV => write!(f, "CSV"),
654            All {code: x} if x >= all::OP_NOP1.code && x <= all::OP_NOP10.code => write!(f, "NOP{}", x - all::OP_NOP1.code + 1),
655            all::OP_INVALIDOPCODE => write!(f, "INVALIDOPCODE"),
656            all::OP_CHECKSIGADD => write!(f, "CHECKSIGADD"),
657            All {code: x} => write!(f, "RETURN_{}", x),
658        }
659    }
660}
661
662/// Classification context for the opcode.
663///
664/// Some opcodes like [`all::OP_RESERVED`] abort the script in `ClassifyContext::Legacy` context,
665/// but will act as `OP_SUCCESSx` in `ClassifyContext::TapScript` (see BIP342 for full list).
666#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
667pub enum ClassifyContext {
668    /// Opcode used in tapscript context.
669    TapScript,
670    /// Opcode used in legacy context.
671    Legacy,
672}
673
674impl All {
675    /// Classifies an Opcode into a broad class.
676    #[inline]
677    pub fn classify(self, ctx: ClassifyContext) -> Class {
678        use self::all::*;
679        match (self, ctx) {
680            // 3 opcodes illegal in all contexts
681            (OP_VERIF, _) | (OP_VERNOTIF, _) | (OP_INVALIDOPCODE, _) => Class::IllegalOp,
682
683            // 15 opcodes illegal in Legacy context
684            (OP_CAT, ctx) | (OP_SUBSTR, ctx)
685            | (OP_LEFT, ctx) | (OP_RIGHT, ctx)
686            | (OP_INVERT, ctx)
687            | (OP_AND, ctx) | (OP_OR, ctx) | (OP_XOR, ctx)
688            | (OP_2MUL, ctx) | (OP_2DIV, ctx)
689            | (OP_MUL, ctx) | (OP_DIV, ctx) | (OP_MOD, ctx)
690            | (OP_LSHIFT, ctx) | (OP_RSHIFT, ctx) if ctx == ClassifyContext::Legacy => Class::IllegalOp,
691
692            // 87 opcodes of SuccessOp class only in TapScript context
693            (op, ClassifyContext::TapScript)
694            if op.code == 80 || op.code == 98 ||
695                (op.code >= 126 && op.code <= 129) ||
696                (op.code >= 131 && op.code <= 134) ||
697                (op.code >= 137 && op.code <= 138) ||
698                (op.code >= 141 && op.code <= 142) ||
699                (op.code >= 149 && op.code <= 153) ||
700                (op.code >= 187 && op.code <= 254) => Class::SuccessOp,
701
702            // 11 opcodes of NoOp class
703            (OP_NOP, _) => Class::NoOp,
704            (op, _) if op.code >= OP_NOP1.code && op.code <= OP_NOP10.code => Class::NoOp,
705
706            // 1 opcode for `OP_RETURN`
707            (OP_RETURN, _) => Class::ReturnOp,
708
709            // 4 opcodes operating equally to `OP_RETURN` only in Legacy context
710            (OP_RESERVED, ctx)
711            | (OP_RESERVED1, ctx) | (OP_RESERVED2, ctx)
712            | (OP_VER, ctx) if ctx == ClassifyContext::Legacy => Class::ReturnOp,
713
714            // 71 opcodes operating equally to `OP_RETURN` only in Legacy context
715            (op, ClassifyContext::Legacy) if op.code >= OP_CHECKSIGADD.code => Class::ReturnOp,
716
717            // 2 opcodes operating equally to `OP_RETURN` only in TapScript context
718            (OP_CHECKMULTISIG, ClassifyContext::TapScript)
719            | (OP_CHECKMULTISIGVERIFY, ClassifyContext::TapScript) => Class::ReturnOp,
720
721            // 1 opcode of PushNum class
722            (OP_PUSHNUM_NEG1, _) => Class::PushNum(-1),
723
724            // 16 opcodes of PushNum class
725            (op, _) if op.code >= OP_PUSHNUM_1.code && op.code <= OP_PUSHNUM_16.code => {
726                Class::PushNum(1 + self.code as i32 - OP_PUSHNUM_1.code as i32)
727            },
728
729            // 76 opcodes of PushBytes class
730            (op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(self.code as u32),
731
732            // opcodes of Ordinary class: 61 for Legacy and 60 for TapScript context
733            (_, _) => Class::Ordinary(Ordinary::with(self)),
734        }
735    }
736
737    /// Encode as a byte
738    #[inline]
739    pub fn into_u8(self) -> u8 {
740        self.code
741    }
742}
743
744impl From<u8> for All {
745    #[inline]
746    fn from(b: u8) -> All {
747        All {code: b}
748    }
749}
750
751display_from_debug!(All);
752
753#[cfg(feature = "serde")]
754#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
755impl serde::Serialize for All {
756    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
757    where
758        S: serde::Serializer,
759    {
760        serializer.serialize_str(&self.to_string())
761    }
762}
763
764/// Empty stack is also FALSE.
765pub static OP_FALSE: All = all::OP_PUSHBYTES_0;
766/// Number 1 is also TRUE.
767pub static OP_TRUE: All = all::OP_PUSHNUM_1;
768/// Previously called OP_NOP2.
769pub static OP_NOP2: All = all::OP_CLTV;
770/// Previously called OP_NOP3.
771pub static OP_NOP3: All = all::OP_CSV;
772
773/// Broad categories of opcodes with similar behavior.
774#[derive(Copy, Clone, PartialEq, Eq, Debug)]
775pub enum Class {
776    /// Pushes the given number onto the stack.
777    PushNum(i32),
778    /// Pushes the given number of bytes onto the stack.
779    PushBytes(u32),
780    /// Fails the script if executed.
781    ReturnOp,
782    /// Succeeds the script even if not executed.
783    SuccessOp,
784    /// Fails the script even if not executed.
785    IllegalOp,
786    /// Does nothing.
787    NoOp,
788    /// Any opcode not covered above.
789    Ordinary(Ordinary)
790}
791
792display_from_debug!(Class);
793
794#[cfg(feature = "serde")]
795#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
796impl serde::Serialize for Class {
797    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
798    where
799        S: serde::Serializer,
800    {
801        serializer.serialize_str(&self.to_string())
802    }
803}
804
805macro_rules! ordinary_opcode {
806    ($($op:ident),*) => (
807        #[repr(u8)]
808        #[doc(hidden)]
809        #[derive(Copy, Clone, PartialEq, Eq, Debug)]
810        pub enum Ordinary {
811            $( $op = all::$op.code ),*
812        }
813
814        impl Ordinary {
815            fn with(b: All) -> Self {
816                match b {
817                    $( all::$op => { Ordinary::$op } ),*
818                    _ => unreachable!("construction of `Ordinary` type from non-ordinary opcode {}", b),
819                }
820            }
821
822            /// Try to create from an All
823            pub fn try_from_all(b: All) -> Option<Self> {
824                match b {
825                    $( all::$op => { Some(Ordinary::$op) } ),*
826                    _ => None,
827                }
828            }
829        }
830    );
831}
832
833// "Ordinary" opcodes -- should be 61 of these
834ordinary_opcode! {
835    // pushdata
836    OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4,
837    // control flow
838    OP_IF, OP_NOTIF, OP_ELSE, OP_ENDIF, OP_VERIFY,
839    // stack
840    OP_TOALTSTACK, OP_FROMALTSTACK,
841    OP_2DROP, OP_2DUP, OP_3DUP, OP_2OVER, OP_2ROT, OP_2SWAP,
842    OP_DROP, OP_DUP, OP_NIP, OP_OVER, OP_PICK, OP_ROLL, OP_ROT, OP_SWAP, OP_TUCK,
843    OP_IFDUP, OP_DEPTH, OP_SIZE,
844    // equality
845    OP_EQUAL, OP_EQUALVERIFY,
846    // arithmetic
847    OP_1ADD, OP_1SUB, OP_NEGATE, OP_ABS, OP_NOT, OP_0NOTEQUAL,
848    OP_ADD, OP_SUB, OP_BOOLAND, OP_BOOLOR,
849    OP_NUMEQUAL, OP_NUMEQUALVERIFY, OP_NUMNOTEQUAL, OP_LESSTHAN,
850    OP_GREATERTHAN, OP_LESSTHANOREQUAL, OP_GREATERTHANOREQUAL,
851    OP_MIN, OP_MAX, OP_WITHIN,
852    // crypto
853    OP_RIPEMD160, OP_SHA1, OP_SHA256, OP_HASH160, OP_HASH256,
854    OP_CODESEPARATOR, OP_CHECKSIG, OP_CHECKSIGVERIFY,
855    OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY,
856    OP_CHECKSIGADD
857}
858
859impl Ordinary {
860    /// Encode as a byte
861    #[inline]
862    pub fn into_u8(self) -> u8 {
863        self as u8
864    }
865}
866
867#[cfg(test)]
868mod tests {
869    use std::collections::HashSet;
870
871    use super::*;
872
873    macro_rules! roundtrip {
874        ($unique:expr, $op:ident) => {
875            assert_eq!(all::$op, All::from(all::$op.into_u8()));
876
877            let s1 = format!("{}", all::$op);
878            let s2 = format!("{:?}", all::$op);
879            assert_eq!(s1, s2);
880            assert_eq!(s1, stringify!($op));
881            assert!($unique.insert(s1));
882        }
883    }
884
885    #[test]
886    fn classify_test() {
887        let op174 = all::OP_CHECKMULTISIG;
888        assert_eq!(op174.classify(ClassifyContext::Legacy), Class::Ordinary(Ordinary::OP_CHECKMULTISIG));
889        assert_eq!(op174.classify(ClassifyContext::TapScript), Class::ReturnOp);
890
891        let op175 = all::OP_CHECKMULTISIGVERIFY;
892        assert_eq!(op175.classify(ClassifyContext::Legacy),  Class::Ordinary(Ordinary::OP_CHECKMULTISIGVERIFY));
893        assert_eq!(op175.classify(ClassifyContext::TapScript), Class::ReturnOp);
894
895        let op186 = all::OP_CHECKSIGADD;
896        assert_eq!(op186.classify(ClassifyContext::Legacy), Class::ReturnOp);
897        assert_eq!(op186.classify(ClassifyContext::TapScript), Class::Ordinary(Ordinary::OP_CHECKSIGADD));
898
899        let op187 = all::OP_RETURN_187;
900        assert_eq!(op187.classify(ClassifyContext::Legacy), Class::ReturnOp);
901        assert_eq!(op187.classify(ClassifyContext::TapScript), Class::SuccessOp);
902    }
903
904    #[test]
905    fn str_roundtrip() {
906        let mut unique = HashSet::new();
907        roundtrip!(unique, OP_PUSHBYTES_0);
908        roundtrip!(unique, OP_PUSHBYTES_1);
909        roundtrip!(unique, OP_PUSHBYTES_2);
910        roundtrip!(unique, OP_PUSHBYTES_3);
911        roundtrip!(unique, OP_PUSHBYTES_4);
912        roundtrip!(unique, OP_PUSHBYTES_5);
913        roundtrip!(unique, OP_PUSHBYTES_6);
914        roundtrip!(unique, OP_PUSHBYTES_7);
915        roundtrip!(unique, OP_PUSHBYTES_8);
916        roundtrip!(unique, OP_PUSHBYTES_9);
917        roundtrip!(unique, OP_PUSHBYTES_10);
918        roundtrip!(unique, OP_PUSHBYTES_11);
919        roundtrip!(unique, OP_PUSHBYTES_12);
920        roundtrip!(unique, OP_PUSHBYTES_13);
921        roundtrip!(unique, OP_PUSHBYTES_14);
922        roundtrip!(unique, OP_PUSHBYTES_15);
923        roundtrip!(unique, OP_PUSHBYTES_16);
924        roundtrip!(unique, OP_PUSHBYTES_17);
925        roundtrip!(unique, OP_PUSHBYTES_18);
926        roundtrip!(unique, OP_PUSHBYTES_19);
927        roundtrip!(unique, OP_PUSHBYTES_20);
928        roundtrip!(unique, OP_PUSHBYTES_21);
929        roundtrip!(unique, OP_PUSHBYTES_22);
930        roundtrip!(unique, OP_PUSHBYTES_23);
931        roundtrip!(unique, OP_PUSHBYTES_24);
932        roundtrip!(unique, OP_PUSHBYTES_25);
933        roundtrip!(unique, OP_PUSHBYTES_26);
934        roundtrip!(unique, OP_PUSHBYTES_27);
935        roundtrip!(unique, OP_PUSHBYTES_28);
936        roundtrip!(unique, OP_PUSHBYTES_29);
937        roundtrip!(unique, OP_PUSHBYTES_30);
938        roundtrip!(unique, OP_PUSHBYTES_31);
939        roundtrip!(unique, OP_PUSHBYTES_32);
940        roundtrip!(unique, OP_PUSHBYTES_33);
941        roundtrip!(unique, OP_PUSHBYTES_34);
942        roundtrip!(unique, OP_PUSHBYTES_35);
943        roundtrip!(unique, OP_PUSHBYTES_36);
944        roundtrip!(unique, OP_PUSHBYTES_37);
945        roundtrip!(unique, OP_PUSHBYTES_38);
946        roundtrip!(unique, OP_PUSHBYTES_39);
947        roundtrip!(unique, OP_PUSHBYTES_40);
948        roundtrip!(unique, OP_PUSHBYTES_41);
949        roundtrip!(unique, OP_PUSHBYTES_42);
950        roundtrip!(unique, OP_PUSHBYTES_43);
951        roundtrip!(unique, OP_PUSHBYTES_44);
952        roundtrip!(unique, OP_PUSHBYTES_45);
953        roundtrip!(unique, OP_PUSHBYTES_46);
954        roundtrip!(unique, OP_PUSHBYTES_47);
955        roundtrip!(unique, OP_PUSHBYTES_48);
956        roundtrip!(unique, OP_PUSHBYTES_49);
957        roundtrip!(unique, OP_PUSHBYTES_50);
958        roundtrip!(unique, OP_PUSHBYTES_51);
959        roundtrip!(unique, OP_PUSHBYTES_52);
960        roundtrip!(unique, OP_PUSHBYTES_53);
961        roundtrip!(unique, OP_PUSHBYTES_54);
962        roundtrip!(unique, OP_PUSHBYTES_55);
963        roundtrip!(unique, OP_PUSHBYTES_56);
964        roundtrip!(unique, OP_PUSHBYTES_57);
965        roundtrip!(unique, OP_PUSHBYTES_58);
966        roundtrip!(unique, OP_PUSHBYTES_59);
967        roundtrip!(unique, OP_PUSHBYTES_60);
968        roundtrip!(unique, OP_PUSHBYTES_61);
969        roundtrip!(unique, OP_PUSHBYTES_62);
970        roundtrip!(unique, OP_PUSHBYTES_63);
971        roundtrip!(unique, OP_PUSHBYTES_64);
972        roundtrip!(unique, OP_PUSHBYTES_65);
973        roundtrip!(unique, OP_PUSHBYTES_66);
974        roundtrip!(unique, OP_PUSHBYTES_67);
975        roundtrip!(unique, OP_PUSHBYTES_68);
976        roundtrip!(unique, OP_PUSHBYTES_69);
977        roundtrip!(unique, OP_PUSHBYTES_70);
978        roundtrip!(unique, OP_PUSHBYTES_71);
979        roundtrip!(unique, OP_PUSHBYTES_72);
980        roundtrip!(unique, OP_PUSHBYTES_73);
981        roundtrip!(unique, OP_PUSHBYTES_74);
982        roundtrip!(unique, OP_PUSHBYTES_75);
983        roundtrip!(unique, OP_PUSHDATA1);
984        roundtrip!(unique, OP_PUSHDATA2);
985        roundtrip!(unique, OP_PUSHDATA4);
986        roundtrip!(unique, OP_PUSHNUM_NEG1);
987        roundtrip!(unique, OP_RESERVED);
988        roundtrip!(unique, OP_PUSHNUM_1);
989        roundtrip!(unique, OP_PUSHNUM_2);
990        roundtrip!(unique, OP_PUSHNUM_3);
991        roundtrip!(unique, OP_PUSHNUM_4);
992        roundtrip!(unique, OP_PUSHNUM_5);
993        roundtrip!(unique, OP_PUSHNUM_6);
994        roundtrip!(unique, OP_PUSHNUM_7);
995        roundtrip!(unique, OP_PUSHNUM_8);
996        roundtrip!(unique, OP_PUSHNUM_9);
997        roundtrip!(unique, OP_PUSHNUM_10);
998        roundtrip!(unique, OP_PUSHNUM_11);
999        roundtrip!(unique, OP_PUSHNUM_12);
1000        roundtrip!(unique, OP_PUSHNUM_13);
1001        roundtrip!(unique, OP_PUSHNUM_14);
1002        roundtrip!(unique, OP_PUSHNUM_15);
1003        roundtrip!(unique, OP_PUSHNUM_16);
1004        roundtrip!(unique, OP_NOP);
1005        roundtrip!(unique, OP_VER);
1006        roundtrip!(unique, OP_IF);
1007        roundtrip!(unique, OP_NOTIF);
1008        roundtrip!(unique, OP_VERIF);
1009        roundtrip!(unique, OP_VERNOTIF);
1010        roundtrip!(unique, OP_ELSE);
1011        roundtrip!(unique, OP_ENDIF);
1012        roundtrip!(unique, OP_VERIFY);
1013        roundtrip!(unique, OP_RETURN);
1014        roundtrip!(unique, OP_TOALTSTACK);
1015        roundtrip!(unique, OP_FROMALTSTACK);
1016        roundtrip!(unique, OP_2DROP);
1017        roundtrip!(unique, OP_2DUP);
1018        roundtrip!(unique, OP_3DUP);
1019        roundtrip!(unique, OP_2OVER);
1020        roundtrip!(unique, OP_2ROT);
1021        roundtrip!(unique, OP_2SWAP);
1022        roundtrip!(unique, OP_IFDUP);
1023        roundtrip!(unique, OP_DEPTH);
1024        roundtrip!(unique, OP_DROP);
1025        roundtrip!(unique, OP_DUP);
1026        roundtrip!(unique, OP_NIP);
1027        roundtrip!(unique, OP_OVER);
1028        roundtrip!(unique, OP_PICK);
1029        roundtrip!(unique, OP_ROLL);
1030        roundtrip!(unique, OP_ROT);
1031        roundtrip!(unique, OP_SWAP);
1032        roundtrip!(unique, OP_TUCK);
1033        roundtrip!(unique, OP_CAT);
1034        roundtrip!(unique, OP_SUBSTR);
1035        roundtrip!(unique, OP_LEFT);
1036        roundtrip!(unique, OP_RIGHT);
1037        roundtrip!(unique, OP_SIZE);
1038        roundtrip!(unique, OP_INVERT);
1039        roundtrip!(unique, OP_AND);
1040        roundtrip!(unique, OP_OR);
1041        roundtrip!(unique, OP_XOR);
1042        roundtrip!(unique, OP_EQUAL);
1043        roundtrip!(unique, OP_EQUALVERIFY);
1044        roundtrip!(unique, OP_RESERVED1);
1045        roundtrip!(unique, OP_RESERVED2);
1046        roundtrip!(unique, OP_1ADD);
1047        roundtrip!(unique, OP_1SUB);
1048        roundtrip!(unique, OP_2MUL);
1049        roundtrip!(unique, OP_2DIV);
1050        roundtrip!(unique, OP_NEGATE);
1051        roundtrip!(unique, OP_ABS);
1052        roundtrip!(unique, OP_NOT);
1053        roundtrip!(unique, OP_0NOTEQUAL);
1054        roundtrip!(unique, OP_ADD);
1055        roundtrip!(unique, OP_SUB);
1056        roundtrip!(unique, OP_MUL);
1057        roundtrip!(unique, OP_DIV);
1058        roundtrip!(unique, OP_MOD);
1059        roundtrip!(unique, OP_LSHIFT);
1060        roundtrip!(unique, OP_RSHIFT);
1061        roundtrip!(unique, OP_BOOLAND);
1062        roundtrip!(unique, OP_BOOLOR);
1063        roundtrip!(unique, OP_NUMEQUAL);
1064        roundtrip!(unique, OP_NUMEQUALVERIFY);
1065        roundtrip!(unique, OP_NUMNOTEQUAL);
1066        roundtrip!(unique, OP_LESSTHAN );
1067        roundtrip!(unique, OP_GREATERTHAN );
1068        roundtrip!(unique, OP_LESSTHANOREQUAL );
1069        roundtrip!(unique, OP_GREATERTHANOREQUAL );
1070        roundtrip!(unique, OP_MIN);
1071        roundtrip!(unique, OP_MAX);
1072        roundtrip!(unique, OP_WITHIN);
1073        roundtrip!(unique, OP_RIPEMD160);
1074        roundtrip!(unique, OP_SHA1);
1075        roundtrip!(unique, OP_SHA256);
1076        roundtrip!(unique, OP_HASH160);
1077        roundtrip!(unique, OP_HASH256);
1078        roundtrip!(unique, OP_CODESEPARATOR);
1079        roundtrip!(unique, OP_CHECKSIG);
1080        roundtrip!(unique, OP_CHECKSIGVERIFY);
1081        roundtrip!(unique, OP_CHECKMULTISIG);
1082        roundtrip!(unique, OP_CHECKMULTISIGVERIFY);
1083        roundtrip!(unique, OP_NOP1);
1084        roundtrip!(unique, OP_CLTV);
1085        roundtrip!(unique, OP_CSV);
1086        roundtrip!(unique, OP_NOP4);
1087        roundtrip!(unique, OP_NOP5);
1088        roundtrip!(unique, OP_NOP6);
1089        roundtrip!(unique, OP_NOP7);
1090        roundtrip!(unique, OP_NOP8);
1091        roundtrip!(unique, OP_NOP9);
1092        roundtrip!(unique, OP_NOP10);
1093        roundtrip!(unique, OP_CHECKSIGADD);
1094        roundtrip!(unique, OP_RETURN_187);
1095        roundtrip!(unique, OP_RETURN_188);
1096        roundtrip!(unique, OP_RETURN_189);
1097        roundtrip!(unique, OP_RETURN_190);
1098        roundtrip!(unique, OP_RETURN_191);
1099        roundtrip!(unique, OP_RETURN_192);
1100        roundtrip!(unique, OP_RETURN_193);
1101        roundtrip!(unique, OP_RETURN_194);
1102        roundtrip!(unique, OP_RETURN_195);
1103        roundtrip!(unique, OP_RETURN_196);
1104        roundtrip!(unique, OP_RETURN_197);
1105        roundtrip!(unique, OP_RETURN_198);
1106        roundtrip!(unique, OP_RETURN_199);
1107        roundtrip!(unique, OP_RETURN_200);
1108        roundtrip!(unique, OP_RETURN_201);
1109        roundtrip!(unique, OP_RETURN_202);
1110        roundtrip!(unique, OP_RETURN_203);
1111        roundtrip!(unique, OP_RETURN_204);
1112        roundtrip!(unique, OP_RETURN_205);
1113        roundtrip!(unique, OP_RETURN_206);
1114        roundtrip!(unique, OP_RETURN_207);
1115        roundtrip!(unique, OP_RETURN_208);
1116        roundtrip!(unique, OP_RETURN_209);
1117        roundtrip!(unique, OP_RETURN_210);
1118        roundtrip!(unique, OP_RETURN_211);
1119        roundtrip!(unique, OP_RETURN_212);
1120        roundtrip!(unique, OP_RETURN_213);
1121        roundtrip!(unique, OP_RETURN_214);
1122        roundtrip!(unique, OP_RETURN_215);
1123        roundtrip!(unique, OP_RETURN_216);
1124        roundtrip!(unique, OP_RETURN_217);
1125        roundtrip!(unique, OP_RETURN_218);
1126        roundtrip!(unique, OP_RETURN_219);
1127        roundtrip!(unique, OP_RETURN_220);
1128        roundtrip!(unique, OP_RETURN_221);
1129        roundtrip!(unique, OP_RETURN_222);
1130        roundtrip!(unique, OP_RETURN_223);
1131        roundtrip!(unique, OP_RETURN_224);
1132        roundtrip!(unique, OP_RETURN_225);
1133        roundtrip!(unique, OP_RETURN_226);
1134        roundtrip!(unique, OP_RETURN_227);
1135        roundtrip!(unique, OP_RETURN_228);
1136        roundtrip!(unique, OP_RETURN_229);
1137        roundtrip!(unique, OP_RETURN_230);
1138        roundtrip!(unique, OP_RETURN_231);
1139        roundtrip!(unique, OP_RETURN_232);
1140        roundtrip!(unique, OP_RETURN_233);
1141        roundtrip!(unique, OP_RETURN_234);
1142        roundtrip!(unique, OP_RETURN_235);
1143        roundtrip!(unique, OP_RETURN_236);
1144        roundtrip!(unique, OP_RETURN_237);
1145        roundtrip!(unique, OP_RETURN_238);
1146        roundtrip!(unique, OP_RETURN_239);
1147        roundtrip!(unique, OP_RETURN_240);
1148        roundtrip!(unique, OP_RETURN_241);
1149        roundtrip!(unique, OP_RETURN_242);
1150        roundtrip!(unique, OP_RETURN_243);
1151        roundtrip!(unique, OP_RETURN_244);
1152        roundtrip!(unique, OP_RETURN_245);
1153        roundtrip!(unique, OP_RETURN_246);
1154        roundtrip!(unique, OP_RETURN_247);
1155        roundtrip!(unique, OP_RETURN_248);
1156        roundtrip!(unique, OP_RETURN_249);
1157        roundtrip!(unique, OP_RETURN_250);
1158        roundtrip!(unique, OP_RETURN_251);
1159        roundtrip!(unique, OP_RETURN_252);
1160        roundtrip!(unique, OP_RETURN_253);
1161        roundtrip!(unique, OP_RETURN_254);
1162        roundtrip!(unique, OP_INVALIDOPCODE);
1163        assert_eq!(unique.len(), 256);
1164    }
1165}