Skip to content

Commit 3cbbcb6

Browse files
committed
script/interpreter: make script_verify_flag_name an ordinary enum
Instead of having `SCRIPT_VERIFY_FOO = (1U << n)` just have it be `n` directly, and do the bit shifting when converting it to `script_verify_flags`.
1 parent bddcade commit 3cbbcb6

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

src/script/interpreter.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <span.h>
1515
#include <uint256.h>
1616

17-
#include <bit>
1817
#include <cstddef>
1918
#include <cstdint>
2019
#include <optional>
@@ -47,33 +46,33 @@ enum
4746

4847
static constexpr script_verify_flags SCRIPT_VERIFY_NONE{0};
4948

50-
enum class script_verify_flag_name : uint32_t {
49+
enum class script_verify_flag_name : uint8_t {
5150
// Evaluate P2SH subscripts (BIP16).
52-
SCRIPT_VERIFY_P2SH = (1U << 0),
51+
SCRIPT_VERIFY_P2SH,
5352

5453
// Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
5554
// Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
5655
// (not used or intended as a consensus rule).
57-
SCRIPT_VERIFY_STRICTENC = (1U << 1),
56+
SCRIPT_VERIFY_STRICTENC,
5857

5958
// Passing a non-strict-DER signature to a checksig operation causes script failure (BIP62 rule 1)
60-
SCRIPT_VERIFY_DERSIG = (1U << 2),
59+
SCRIPT_VERIFY_DERSIG,
6160

6261
// Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
6362
// (BIP62 rule 5).
64-
SCRIPT_VERIFY_LOW_S = (1U << 3),
63+
SCRIPT_VERIFY_LOW_S,
6564

6665
// verify dummy stack item consumed by CHECKMULTISIG is of zero-length (BIP62 rule 7).
67-
SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
66+
SCRIPT_VERIFY_NULLDUMMY,
6867

6968
// Using a non-push operator in the scriptSig causes script failure (BIP62 rule 2).
70-
SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
69+
SCRIPT_VERIFY_SIGPUSHONLY,
7170

7271
// Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
7372
// pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
7473
// any other push causes the script to fail (BIP62 rule 3).
7574
// In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
76-
SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
75+
SCRIPT_VERIFY_MINIMALDATA,
7776

7877
// Discourage use of NOPs reserved for upgrades (NOP1-10)
7978
//
@@ -85,7 +84,7 @@ enum class script_verify_flag_name : uint32_t {
8584
// executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
8685
// NOPs that have associated forks to give them new meaning (CLTV, CSV)
8786
// are not subject to this rule.
88-
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
87+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS,
8988

9089
// Require that only a single stack element remains after evaluation. This changes the success criterion from
9190
// "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
@@ -94,69 +93,70 @@ enum class script_verify_flag_name : uint32_t {
9493
// Note: CLEANSTACK should never be used without P2SH or WITNESS.
9594
// Note: WITNESS_V0 and TAPSCRIPT script execution have behavior similar to CLEANSTACK as part of their
9695
// consensus rules. It is automatic there and does not need this flag.
97-
SCRIPT_VERIFY_CLEANSTACK = (1U << 8),
96+
SCRIPT_VERIFY_CLEANSTACK,
9897

9998
// Verify CHECKLOCKTIMEVERIFY
10099
//
101100
// See BIP65 for details.
102-
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
101+
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY,
103102

104103
// support CHECKSEQUENCEVERIFY opcode
105104
//
106105
// See BIP112 for details
107-
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10),
106+
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY,
108107

109108
// Support segregated witness
110109
//
111-
SCRIPT_VERIFY_WITNESS = (1U << 11),
110+
SCRIPT_VERIFY_WITNESS,
112111

113112
// Making v1-v16 witness program non-standard
114113
//
115-
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM = (1U << 12),
114+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM,
116115

117116
// Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector
118117
//
119118
// Note: TAPSCRIPT script execution has behavior similar to MINIMALIF as part of its consensus
120119
// rules. It is automatic there and does not depend on this flag.
121-
SCRIPT_VERIFY_MINIMALIF = (1U << 13),
120+
SCRIPT_VERIFY_MINIMALIF,
122121

123122
// Signature(s) must be empty vector if a CHECK(MULTI)SIG operation failed
124123
//
125-
SCRIPT_VERIFY_NULLFAIL = (1U << 14),
124+
SCRIPT_VERIFY_NULLFAIL,
126125

127126
// Public keys in segregated witness scripts must be compressed
128127
//
129-
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE = (1U << 15),
128+
SCRIPT_VERIFY_WITNESS_PUBKEYTYPE,
130129

131130
// Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts
132131
//
133-
SCRIPT_VERIFY_CONST_SCRIPTCODE = (1U << 16),
132+
SCRIPT_VERIFY_CONST_SCRIPTCODE,
134133

135134
// Taproot/Tapscript validation (BIPs 341 & 342)
136135
//
137-
SCRIPT_VERIFY_TAPROOT = (1U << 17),
136+
SCRIPT_VERIFY_TAPROOT,
138137

139138
// Making unknown Taproot leaf versions non-standard
140139
//
141-
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION = (1U << 18),
140+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION,
142141

143142
// Making unknown OP_SUCCESS non-standard
144-
SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS = (1U << 19),
143+
SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS,
145144

146145
// Making unknown public key versions (in BIP 342 scripts) non-standard
147-
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE = (1U << 20),
146+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE,
148147

149148
// Constants to point to the highest flag in use. Add new flags above this line.
150149
//
151150
SCRIPT_VERIFY_END_MARKER
152151
};
153152
using enum script_verify_flag_name;
154153

154+
static constexpr int MAX_SCRIPT_VERIFY_FLAGS_BITS = static_cast<int>(SCRIPT_VERIFY_END_MARKER);
155+
155156
// assert there is still a spare bit
156-
static_assert(static_cast<script_verify_flags::value_type>(SCRIPT_VERIFY_END_MARKER) < (1u << 31));
157+
static_assert(0 < MAX_SCRIPT_VERIFY_FLAGS_BITS && MAX_SCRIPT_VERIFY_FLAGS_BITS <= 31);
157158

158-
static constexpr script_verify_flags::value_type MAX_SCRIPT_VERIFY_FLAGS = ((static_cast<script_verify_flags::value_type>(SCRIPT_VERIFY_END_MARKER) - 1) << 1) - 1;
159-
static constexpr int MAX_SCRIPT_VERIFY_FLAGS_BITS = std::bit_width(MAX_SCRIPT_VERIFY_FLAGS);
159+
static constexpr script_verify_flags::value_type MAX_SCRIPT_VERIFY_FLAGS = ((script_verify_flags::value_type{1} << MAX_SCRIPT_VERIFY_FLAGS_BITS) - 1);
160160

161161
bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, script_verify_flags flags, ScriptError* serror);
162162

src/script/verify_flags.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <compare>
1010
#include <cstdint>
1111

12-
enum class script_verify_flag_name : uint32_t;
12+
enum class script_verify_flag_name : uint8_t;
1313

1414
class script_verify_flags
1515
{
@@ -22,7 +22,7 @@ class script_verify_flags
2222
consteval explicit(false) script_verify_flags(value_type f) : m_value{f} { if (f != 0) throw 0; }
2323

2424
// implicit construction from a hard-coded SCRIPT_VERIFY_* constant is also okay
25-
constexpr explicit(false) script_verify_flags(script_verify_flag_name f) : m_value{static_cast<value_type>(f)} { }
25+
constexpr explicit(false) script_verify_flags(script_verify_flag_name f) : m_value{value_type{1} << static_cast<uint8_t>(f)} { }
2626

2727
// rule of 5
2828
constexpr script_verify_flags(const script_verify_flags&) = default;

0 commit comments

Comments
 (0)