Skip to content

Commit

Permalink
Merge pull request #644 from sanket1729/tap_opcodes
Browse files Browse the repository at this point in the history
Add OP_CHECKSIGADD and OP_SUCCESSxxx
  • Loading branch information
apoelstra committed Sep 24, 2021
2 parents 72dbe1d + c75f3ef commit 9fe840c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 47 deletions.
145 changes: 105 additions & 40 deletions src/blockdata/opcodes.rs
Expand Up @@ -417,8 +417,8 @@ pub mod all {
/// Does nothing
pub const OP_NOP10: All = All {code: 0xb9};
// Every other opcode acts as OP_RETURN
/// Synonym for OP_RETURN
pub const OP_RETURN_186: All = All {code: 0xba};
/// OP_CHECKSIGADD post tapscript
pub const OP_CHECKSIGADD: All = All {code: 0xba};
/// Synonym for OP_RETURN
pub const OP_RETURN_187: All = All {code: 0xbb};
/// Synonym for OP_RETURN
Expand Down Expand Up @@ -556,7 +556,7 @@ pub mod all {
/// Synonym for OP_RETURN
pub const OP_RETURN_254: All = All {code: 0xfe};
/// Synonym for OP_RETURN
pub const OP_RETURN_255: All = All {code: 0xff};
pub const OP_INVALIDOPCODE: All = All {code: 0xff};
}

impl fmt::Debug for All {
Expand Down Expand Up @@ -652,48 +652,84 @@ impl fmt::Debug for All {
all::OP_CLTV => write!(f, "CLTV"),
all::OP_CSV => write!(f, "CSV"),
All {code: x} if x >= all::OP_NOP1.code && x <= all::OP_NOP10.code => write!(f, "NOP{}", x - all::OP_NOP1.code + 1),
all::OP_INVALIDOPCODE => write!(f, "INVALIDOPCODE"),
all::OP_CHECKSIGADD => write!(f, "CHECKSIGADD"),
All {code: x} => write!(f, "RETURN_{}", x),
}
}
}


/// Classification context for the opcode. Some opcodes like `OP_RESERVED`
/// abort the script in [`ClassifyContext::Legacy`] context, but will act as OP_SUCCESS in tapscript
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ClassifyContext {
/// Opcode used in tapscript context
TapScript,
/// Opcode used in legacy context
Legacy,
}

impl All {
/// Classifies an Opcode into a broad class
#[inline]
pub fn classify(self) -> Class {
// 17 opcodes
if self == all::OP_VERIF || self == all::OP_VERNOTIF ||
self == all::OP_CAT || self == all::OP_SUBSTR ||
self == all::OP_LEFT || self == all::OP_RIGHT ||
self == all::OP_INVERT || self == all::OP_AND ||
self == all::OP_OR || self == all::OP_XOR ||
self == all::OP_2MUL || self == all::OP_2DIV ||
self == all::OP_MUL || self == all::OP_DIV || self == all::OP_MOD ||
self == all::OP_LSHIFT || self == all::OP_RSHIFT {
Class::IllegalOp
// 11 opcodes
} else if self == all::OP_NOP ||
(all::OP_NOP1.code <= self.code &&
self.code <= all::OP_NOP10.code) {
Class::NoOp
// 75 opcodes
} else if self == all::OP_RESERVED || self == all::OP_VER || self == all::OP_RETURN ||
self == all::OP_RESERVED1 || self == all::OP_RESERVED2 ||
self.code >= all::OP_RETURN_186.code {
Class::ReturnOp
// 1 opcode
} else if self == all::OP_PUSHNUM_NEG1 {
Class::PushNum(-1)
// 16 opcodes
} else if all::OP_PUSHNUM_1.code <= self.code &&
self.code <= all::OP_PUSHNUM_16.code {
Class::PushNum(1 + self.code as i32 - all::OP_PUSHNUM_1.code as i32)
// 76 opcodes
} else if self.code <= all::OP_PUSHBYTES_75.code {
Class::PushBytes(self.code as u32)
// 60 opcodes
} else {
Class::Ordinary(Ordinary::try_from_all(self).unwrap())
pub fn classify(self, ctx: ClassifyContext) -> Class {
use self::all::*;
match (self, ctx) {
// 3 opcodes illegal in all contexts
(OP_VERIF, _) | (OP_VERNOTIF, _) | (OP_INVALIDOPCODE, _) => Class::IllegalOp,

// 15 opcodes illegal in Legacy context
(OP_CAT, ctx) | (OP_SUBSTR, ctx)
| (OP_LEFT, ctx) | (OP_RIGHT, ctx)
| (OP_INVERT, ctx)
| (OP_AND, ctx) | (OP_OR, ctx) | (OP_XOR, ctx)
| (OP_2MUL, ctx) | (OP_2DIV, ctx)
| (OP_MUL, ctx) | (OP_DIV, ctx) | (OP_MOD, ctx)
| (OP_LSHIFT, ctx) | (OP_RSHIFT, ctx) if ctx == ClassifyContext::Legacy => Class::IllegalOp,

// 87 opcodes of SuccessOp class only in TapScript context
(op, ClassifyContext::TapScript)
if op.code == 80 || op.code == 98 ||
(op.code >= 126 && op.code <= 129) ||
(op.code >= 131 && op.code <= 134) ||
(op.code >= 137 && op.code <= 138) ||
(op.code >= 141 && op.code <= 142) ||
(op.code >= 149 && op.code <= 153) ||
(op.code >= 187 && op.code <= 254) => Class::SuccessOp,

// 11 opcodes of NoOp class
(OP_NOP, _) => Class::NoOp,
(op, _) if op.code >= OP_NOP1.code && op.code <= OP_NOP10.code => Class::NoOp,

// 1 opcode for `OP_RETURN`
(OP_RETURN, _) => Class::ReturnOp,

// 4 opcodes operating equally to `OP_RETURN` only in Legacy context
(OP_RESERVED, ctx)
| (OP_RESERVED1, ctx) | (OP_RESERVED2, ctx)
| (OP_VER, ctx) if ctx == ClassifyContext::Legacy => Class::ReturnOp,

// 71 opcodes operating equally to `OP_RETURN` only in Legacy context
(op, ClassifyContext::Legacy) if op.code >= OP_CHECKSIGADD.code => Class::ReturnOp,

// 2 opcodes operating equally to `OP_RETURN` only in TapScript context
(OP_CHECKMULTISIG, ClassifyContext::TapScript)
| (OP_CHECKMULTISIGVERIFY, ClassifyContext::TapScript) => Class::ReturnOp,

// 1 opcode of PushNum class
(OP_PUSHNUM_NEG1, _) => Class::PushNum(-1),

// 16 opcodes of PushNum class
(op, _) if op.code >= OP_PUSHNUM_1.code && op.code <= OP_PUSHNUM_16.code => {
Class::PushNum(1 + self.code as i32 - OP_PUSHNUM_1.code as i32)
},

// 76 opcodes of PushBytes class
(op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(self.code as u32),

// opcodes of Ordinary class: 61 for Legacy and 60 for TapScript context
(_, _) => Class::Ordinary(Ordinary::with(self)),
}
}

Expand Down Expand Up @@ -743,6 +779,8 @@ pub enum Class {
PushBytes(u32),
/// Fails the script if executed
ReturnOp,
/// Succeeds the script even if not executed
SuccessOp,
/// Fails the script even if not executed
IllegalOp,
/// Does nothing
Expand Down Expand Up @@ -774,6 +812,13 @@ macro_rules! ordinary_opcode {
}

impl Ordinary {
fn with(b: All) -> Self {
match b {
$( all::$op => { Ordinary::$op } ),*
_ => unreachable!("construction of `Ordinary` type from non-ordinary opcode {}", b),
}
}

/// Try to create from an All
pub fn try_from_all(b: All) -> Option<Self> {
match b {
Expand Down Expand Up @@ -807,7 +852,8 @@ ordinary_opcode! {
// crypto
OP_RIPEMD160, OP_SHA1, OP_SHA256, OP_HASH160, OP_HASH256,
OP_CODESEPARATOR, OP_CHECKSIG, OP_CHECKSIGVERIFY,
OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY
OP_CHECKMULTISIG, OP_CHECKMULTISIGVERIFY,
OP_CHECKSIGADD
}

impl Ordinary {
Expand Down Expand Up @@ -836,6 +882,25 @@ mod tests {
}
}

#[test]
fn classify_test() {
let op174 = all::OP_CHECKMULTISIG;
assert_eq!(op174.classify(ClassifyContext::Legacy), Class::Ordinary(Ordinary::OP_CHECKMULTISIG));
assert_eq!(op174.classify(ClassifyContext::TapScript), Class::ReturnOp);

let op175 = all::OP_CHECKMULTISIGVERIFY;
assert_eq!(op175.classify(ClassifyContext::Legacy), Class::Ordinary(Ordinary::OP_CHECKMULTISIGVERIFY));
assert_eq!(op175.classify(ClassifyContext::TapScript), Class::ReturnOp);

let op186 = all::OP_CHECKSIGADD;
assert_eq!(op186.classify(ClassifyContext::Legacy), Class::ReturnOp);
assert_eq!(op186.classify(ClassifyContext::TapScript), Class::Ordinary(Ordinary::OP_CHECKSIGADD));

let op187 = all::OP_RETURN_187;
assert_eq!(op187.classify(ClassifyContext::Legacy), Class::ReturnOp);
assert_eq!(op187.classify(ClassifyContext::TapScript), Class::SuccessOp);
}

#[test]
fn str_roundtrip() {
let mut unique = HashSet::new();
Expand Down Expand Up @@ -1025,7 +1090,7 @@ mod tests {
roundtrip!(unique, OP_NOP8);
roundtrip!(unique, OP_NOP9);
roundtrip!(unique, OP_NOP10);
roundtrip!(unique, OP_RETURN_186);
roundtrip!(unique, OP_CHECKSIGADD);
roundtrip!(unique, OP_RETURN_187);
roundtrip!(unique, OP_RETURN_188);
roundtrip!(unique, OP_RETURN_189);
Expand Down Expand Up @@ -1094,7 +1159,7 @@ mod tests {
roundtrip!(unique, OP_RETURN_252);
roundtrip!(unique, OP_RETURN_253);
roundtrip!(unique, OP_RETURN_254);
roundtrip!(unique, OP_RETURN_255);
roundtrip!(unique, OP_INVALIDOPCODE);
assert_eq!(unique.len(), 256);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/blockdata/script.rs
Expand Up @@ -407,8 +407,9 @@ impl Script {

/// Whether a script can be proven to have no satisfying input
pub fn is_provably_unspendable(&self) -> bool {
!self.0.is_empty() && (opcodes::All::from(self.0[0]).classify() == opcodes::Class::ReturnOp ||
opcodes::All::from(self.0[0]).classify() == opcodes::Class::IllegalOp)
!self.0.is_empty() &&
(opcodes::All::from(self.0[0]).classify(opcodes::ClassifyContext::Legacy) == opcodes::Class::ReturnOp ||
opcodes::All::from(self.0[0]).classify(opcodes::ClassifyContext::Legacy) == opcodes::Class::IllegalOp)
}

/// Gets the minimum value an output with this script should have in order to be
Expand Down Expand Up @@ -481,7 +482,7 @@ impl Script {
let opcode = opcodes::All::from(script[index]);
index += 1;

let data_len = if let opcodes::Class::PushBytes(n) = opcode.classify() {
let data_len = if let opcodes::Class::PushBytes(n) = opcode.classify(opcodes::ClassifyContext::Legacy) {
n as usize
} else {
match opcode {
Expand Down Expand Up @@ -594,7 +595,9 @@ impl<'a> Iterator for Instructions<'a> {
return None;
}

match opcodes::All::from(self.data[0]).classify() {
// classify parameter does not really matter here since we are only using
// it for pushes and nums
match opcodes::All::from(self.data[0]).classify(opcodes::ClassifyContext::Legacy) {
opcodes::Class::PushBytes(n) => {
let n = n as usize;
if self.data.len() < n + 1 {
Expand Down
4 changes: 2 additions & 2 deletions src/util/contracthash.rs
Expand Up @@ -133,7 +133,7 @@ impl Template {
pub fn first_push_as_number(&self) -> Option<usize> {
if !self.0.is_empty() {
if let TemplateElement::Op(op) = self.0[0] {
if let opcodes::Class::PushNum(n) = op.classify() {
if let opcodes::Class::PushNum(n) = op.classify(opcodes::ClassifyContext::Legacy) {
if n >= 0 {
return Some(n as usize);
}
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn untemplate(script: &script::Script) -> Result<(Template, Vec<PublicKey>),
}
}
script::Instruction::Op(op) => {
match op.classify() {
match op.classify(opcodes::ClassifyContext::Legacy) {
// CHECKSIG should only come after a list of keys
opcodes::Class::Ordinary(opcodes::Ordinary::OP_CHECKSIG) |
opcodes::Class::Ordinary(opcodes::Ordinary::OP_CHECKSIGVERIFY) => {
Expand Down
2 changes: 1 addition & 1 deletion src/util/misc.rs
Expand Up @@ -231,7 +231,7 @@ pub fn script_find_and_remove(haystack: &mut Vec<u8>, needle: &[u8]) -> usize {
top = top.wrapping_sub(needle.len());
if overflow { break; }
} else {
i += match opcodes::All::from((*haystack)[i]).classify() {
i += match opcodes::All::from((*haystack)[i]).classify(opcodes::ClassifyContext::Legacy) {
opcodes::Class::PushBytes(n) => n as usize + 1,
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA1) => 2,
opcodes::Class::Ordinary(opcodes::Ordinary::OP_PUSHDATA2) => 3,
Expand Down

0 comments on commit 9fe840c

Please sign in to comment.