Skip to content

Commit 9385baa

Browse files
authored
Merge pull request #6 from arminsabouri/add-cat-knob
Add cat knob
2 parents e934bca + 1171705 commit 9385baa

File tree

8 files changed

+537
-6
lines changed

8 files changed

+537
-6
lines changed

src/policy/policy.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS{MANDATORY_SCRIPT_VERI
170170
SCRIPT_VERIFY_CONST_SCRIPTCODE |
171171
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION |
172172
SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS |
173-
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE};
173+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE |
174+
SCRIPT_VERIFY_DISCOURAGE_OP_CAT};
174175

175176
/** For convenience, standard but not mandatory verify flags. */
176177
static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS{STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS};

src/script/interpreter.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,16 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
451451
if (opcode > OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT) {
452452
return set_error(serror, SCRIPT_ERR_OP_COUNT);
453453
}
454+
455+
// When OP_SUCCESS disabled opcodes (CVE-2010-5137) are
456+
// redefined in tapscript, remove them from the if below
457+
// and put them here
458+
if (opcode == OP_CAT) {
459+
return set_error(serror, SCRIPT_ERR_DISABLED_OPCODE); // Disabled opcodes (CVE-2010-5137).
460+
}
454461
}
455462

456-
if (opcode == OP_CAT ||
457-
opcode == OP_SUBSTR ||
463+
if (opcode == OP_SUBSTR ||
458464
opcode == OP_LEFT ||
459465
opcode == OP_RIGHT ||
460466
opcode == OP_INVERT ||
@@ -518,6 +524,19 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
518524
case OP_NOP:
519525
break;
520526

527+
case OP_CAT:
528+
{
529+
if (stack.size() < 2)
530+
return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
531+
valtype& vch1 = stacktop(-2);
532+
valtype& vch2 = stacktop(-1);
533+
if (vch1.size() + vch2.size() > MAX_SCRIPT_ELEMENT_SIZE)
534+
return set_error(serror, SCRIPT_ERR_PUSH_SIZE);
535+
vch1.insert(vch1.end(), vch2.begin(), vch2.end());
536+
stack.pop_back();
537+
}
538+
break;
539+
521540
case OP_CHECKLOCKTIMEVERIFY:
522541
{
523542
if (!(flags & SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) {
@@ -1807,10 +1826,19 @@ static bool ExecuteWitnessScript(const Span<const valtype>& stack_span, const CS
18071826
}
18081827
// New opcodes will be listed here. May use a different sigversion to modify existing opcodes.
18091828
if (IsOpSuccess(opcode)) {
1810-
if (flags & SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS) {
1811-
return set_error(serror, SCRIPT_ERR_DISCOURAGE_OP_SUCCESS);
1829+
if (opcode == OP_CAT) {
1830+
if (flags & SCRIPT_VERIFY_DISCOURAGE_OP_CAT) {
1831+
return set_error(serror, SCRIPT_ERR_DISCOURAGE_OP_CAT);
1832+
} else if (!(flags & SCRIPT_VERIFY_OP_CAT)) {
1833+
return set_success(serror);
1834+
}
1835+
} else {
1836+
// OP_SUCCESS behaviour
1837+
if (flags & SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS) {
1838+
return set_error(serror, SCRIPT_ERR_DISCOURAGE_OP_SUCCESS);
1839+
}
1840+
return set_success(serror);
18121841
}
1813-
return set_success(serror);
18141842
}
18151843
}
18161844

src/script/interpreter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ enum : uint32_t {
143143
// Making unknown public key versions (in BIP 342 scripts) non-standard
144144
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE = (1U << 20),
145145

146+
// Support OP_CAT in tapscript
147+
SCRIPT_VERIFY_OP_CAT = (1U << 21),
148+
SCRIPT_VERIFY_DISCOURAGE_OP_CAT = (1U << 22),
149+
146150
// Constants to point to the highest flag in use. Add new flags above this line.
147151
//
148152
SCRIPT_VERIFY_END_MARKER

src/script/script_error.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ std::string ScriptErrorString(const ScriptError serror)
7676
case SCRIPT_ERR_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION:
7777
return "Taproot version reserved for soft-fork upgrades";
7878
case SCRIPT_ERR_DISCOURAGE_OP_SUCCESS:
79+
case SCRIPT_ERR_DISCOURAGE_OP_CAT:
7980
return "OP_SUCCESSx reserved for soft-fork upgrades";
8081
case SCRIPT_ERR_DISCOURAGE_UPGRADABLE_PUBKEYTYPE:
8182
return "Public key version reserved for soft-fork upgrades";

src/script/script_error.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ typedef enum ScriptError_t
7878
SCRIPT_ERR_TAPSCRIPT_CHECKMULTISIG,
7979
SCRIPT_ERR_TAPSCRIPT_MINIMALIF,
8080

81+
/* OP_CAT re-activation */
82+
SCRIPT_ERR_DISCOURAGE_OP_CAT,
83+
8184
/* Constant scriptCode */
8285
SCRIPT_ERR_OP_CODESEPARATOR,
8386
SCRIPT_ERR_SIG_FINDANDDELETE,

0 commit comments

Comments
 (0)