Use new OP_x variants (for x in 1-16)
What changed, and why it matters
This commit is a simple renaming cleanup inside the rust-bitcoin library. It replaces older, longer opcode names like OP_PUSHNUM_1 with shorter names like OP_1 everywhere they appear. The actual numeric byte values and behavior of the Bitcoin opcodes stay exactly the same, so there is no security risk.
No security action needed. Treat as a normal refactoring/rename commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is a pure identifier rename across two opcode definition files and several call sites in script builder, owned script, witness version, and tests. OP_PUSHNUM_1..OP_PUSHNUM_16 become OP_1..OP_16, and OP_PUSHNUM_NEG1 becomes OP_1NEGATE. The underlying opcode byte values (0x51-0x60 for 1-16, 0x4f for -1) are unchanged, and all logic that computes ranges, decodes pushnum values, maps witness versions, or builds scripts remains semantically identical. No functional or consensus-relevant change is introduced.
Changed components
bitcoin/src/blockdata/opcodes.rsprimitives/src/opcodes.rsbitcoin/src/blockdata/script/builder.rsbitcoin/src/blockdata/script/owned.rsbitcoin/src/blockdata/script/witness_version.rsbitcoin/src/blockdata/script/tests.rsInspect captured patch +69 / −69
diff --git a/bitcoin/src/blockdata/opcodes.rs b/bitcoin/src/blockdata/opcodes.rs
index 46503b4d..552e1603 100644
--- a/bitcoin/src/blockdata/opcodes.rs
+++ b/bitcoin/src/blockdata/opcodes.rs
@@ -53,7 +53,7 @@ macro_rules! all_opcodes {
/// Empty stack is also FALSE.
pub const OP_FALSE: Opcode = OP_PUSHBYTES_0;
/// Number 1 is also TRUE.
- pub const OP_TRUE: Opcode = OP_PUSHNUM_1;
+ pub const OP_TRUE: Opcode = OP_1;
/// Previously called OP_NOP2.
pub const OP_NOP2: Opcode = OP_CLTV;
/// Previously called OP_NOP3.
@@ -452,8 +452,8 @@ impl Opcode {
(OP_1NEGATE, _) => 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),
+ (op, _) if op.code >= OP_1.code && op.code <= OP_16.code =>
+ Class::PushNum(1 + self.code as i32 - OP_1.code as i32),
// 76 opcodes of PushBytes class
(op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(self.code as u32),
@@ -477,8 +477,8 @@ impl Opcode {
#[inline]
#[must_use]
pub const fn decode_pushnum(self) -> Option<u8> {
- const START: u8 = OP_PUSHNUM_1.code;
- const END: u8 = OP_PUSHNUM_16.code;
+ const START: u8 = OP_1.code;
+ const END: u8 = OP_16.code;
match self.code {
START..=END => Some(self.code - START + 1),
_ => None,
@@ -627,11 +627,11 @@ mod tests {
fn decode_pushnum() {
// Test all possible opcodes
// - Sanity check
- assert_eq!(OP_PUSHNUM_1.code, 0x51_u8);
- assert_eq!(OP_PUSHNUM_16.code, 0x60_u8);
+ assert_eq!(OP_1.code, 0x51_u8);
+ assert_eq!(OP_16.code, 0x60_u8);
for i in 0x00..=0xff_u8 {
let expected = match i {
- // OP_PUSHNUM_1 ..= OP_PUSHNUM_16
+ // OP_1 ..= OP_16
0x51..=0x60 => Some(i - 0x50),
_ => None,
};
@@ -641,22 +641,22 @@ mod tests {
// Test the named opcode constants
// - This is the OP right before PUSHNUMs start
assert!(OP_RESERVED.decode_pushnum().is_none());
- assert_eq!(OP_PUSHNUM_1.decode_pushnum().expect("pushnum"), 1);
- assert_eq!(OP_PUSHNUM_2.decode_pushnum().expect("pushnum"), 2);
- assert_eq!(OP_PUSHNUM_3.decode_pushnum().expect("pushnum"), 3);
- assert_eq!(OP_PUSHNUM_4.decode_pushnum().expect("pushnum"), 4);
- assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5);
- assert_eq!(OP_PUSHNUM_6.decode_pushnum().expect("pushnum"), 6);
- assert_eq!(OP_PUSHNUM_7.decode_pushnum().expect("pushnum"), 7);
- assert_eq!(OP_PUSHNUM_8.decode_pushnum().expect("pushnum"), 8);
- assert_eq!(OP_PUSHNUM_9.decode_pushnum().expect("pushnum"), 9);
- assert_eq!(OP_PUSHNUM_10.decode_pushnum().expect("pushnum"), 10);
- assert_eq!(OP_PUSHNUM_11.decode_pushnum().expect("pushnum"), 11);
- assert_eq!(OP_PUSHNUM_12.decode_pushnum().expect("pushnum"), 12);
- assert_eq!(OP_PUSHNUM_13.decode_pushnum().expect("pushnum"), 13);
- assert_eq!(OP_PUSHNUM_14.decode_pushnum().expect("pushnum"), 14);
- assert_eq!(OP_PUSHNUM_15.decode_pushnum().expect("pushnum"), 15);
- assert_eq!(OP_PUSHNUM_16.decode_pushnum().expect("pushnum"), 16);
+ assert_eq!(OP_1.decode_pushnum().expect("pushnum"), 1);
+ assert_eq!(OP_2.decode_pushnum().expect("pushnum"), 2);
+ assert_eq!(OP_3.decode_pushnum().expect("pushnum"), 3);
+ assert_eq!(OP_4.decode_pushnum().expect("pushnum"), 4);
+ assert_eq!(OP_5.decode_pushnum().expect("pushnum"), 5);
+ assert_eq!(OP_6.decode_pushnum().expect("pushnum"), 6);
+ assert_eq!(OP_7.decode_pushnum().expect("pushnum"), 7);
+ assert_eq!(OP_8.decode_pushnum().expect("pushnum"), 8);
+ assert_eq!(OP_9.decode_pushnum().expect("pushnum"), 9);
+ assert_eq!(OP_10.decode_pushnum().expect("pushnum"), 10);
+ assert_eq!(OP_11.decode_pushnum().expect("pushnum"), 11);
+ assert_eq!(OP_12.decode_pushnum().expect("pushnum"), 12);
+ assert_eq!(OP_13.decode_pushnum().expect("pushnum"), 13);
+ assert_eq!(OP_14.decode_pushnum().expect("pushnum"), 14);
+ assert_eq!(OP_15.decode_pushnum().expect("pushnum"), 15);
+ assert_eq!(OP_16.decode_pushnum().expect("pushnum"), 16);
// - This is the OP right after PUSHNUMs end
assert!(OP_NOP.decode_pushnum().is_none());
}
diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs
index 34342ec3..90f21836 100644
--- a/bitcoin/src/blockdata/script/builder.rs
+++ b/bitcoin/src/blockdata/script/builder.rs
@@ -78,9 +78,9 @@ impl<T> Builder<T> {
let bytes = data.as_ref().as_bytes();
if bytes.len() == 1 && (bytes[0] == 0x81 || bytes[0] <= 16) {
match bytes[0] {
- 0x81 => self.push_opcode(OP_PUSHNUM_NEG1),
+ 0x81 => self.push_opcode(OP_1NEGATE),
0 => self.push_opcode(OP_PUSHBYTES_0),
- 1..=16 => self.push_opcode(Opcode::from(bytes[0] + (OP_PUSHNUM_1.to_u8() - 1))),
+ 1..=16 => self.push_opcode(Opcode::from(bytes[0] + (OP_1.to_u8() - 1))),
_ => self, // unreachable arm
}
} else {
diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs
index afedf71e..1f4a67df 100644
--- a/bitcoin/src/blockdata/script/owned.rs
+++ b/bitcoin/src/blockdata/script/owned.rs
@@ -64,9 +64,9 @@ internal_macros::define_extension_trait! {
/// Does not check whether `n` is in the range of [-2^31 +1...2^31 -1].
fn push_int_unchecked(&mut self, n: i64) {
match n {
- -1 => self.push_opcode(OP_PUSHNUM_NEG1),
+ -1 => self.push_opcode(OP_1NEGATE),
0 => self.push_opcode(OP_PUSHBYTES_0),
- 1..=16 => self.push_opcode(Opcode::from(n as u8 + (OP_PUSHNUM_1.to_u8() - 1))),
+ 1..=16 => self.push_opcode(Opcode::from(n as u8 + (OP_1.to_u8() - 1))),
_ => self.push_int_non_minimal(n),
}
}
@@ -79,9 +79,9 @@ internal_macros::define_extension_trait! {
let bytes = data.as_ref().as_bytes();
if bytes.len() == 1 && (bytes[0] == 0x81 || bytes[0] <= 16) {
match bytes[0] {
- 0x81 => { self.push_opcode(OP_PUSHNUM_NEG1); },
+ 0x81 => { self.push_opcode(OP_1NEGATE); },
0 => { self.push_opcode(OP_PUSHBYTES_0); },
- 1..=16 => { self.push_opcode(Opcode::from(bytes[0] + (OP_PUSHNUM_1.to_u8() - 1))); },
+ 1..=16 => { self.push_opcode(Opcode::from(bytes[0] + (OP_1.to_u8() - 1))); },
_ => {}, // unreachable arm
}
} else {
diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs
index 67cf18b3..179b6992 100644
--- a/bitcoin/src/blockdata/script/tests.rs
+++ b/bitcoin/src/blockdata/script/tests.rs
@@ -858,34 +858,34 @@ fn script_get_sigop_count() {
.push_slice([42; 20])
.push_opcode(OP_EQUALVERIFY)
.push_opcode(OP_CHECKSIGVERIFY)
- .push_opcode(OP_PUSHNUM_1)
+ .push_opcode(OP_1)
.into_script()
.count_sigops(),
1
);
let multi = Script::builder()
- .push_opcode(OP_PUSHNUM_1)
+ .push_opcode(OP_1)
.push_slice([3; 33])
.push_slice([3; 33])
.push_slice([3; 33])
- .push_opcode(OP_PUSHNUM_3)
+ .push_opcode(OP_3)
.push_opcode(OP_CHECKMULTISIG)
.into_script();
assert_eq!(multi.count_sigops(), 3);
assert_eq!(multi.count_sigops_legacy(), 20);
let multi_verify = Script::builder()
- .push_opcode(OP_PUSHNUM_1)
+ .push_opcode(OP_1)
.push_slice([3; 33])
.push_slice([3; 33])
.push_slice([3; 33])
- .push_opcode(OP_PUSHNUM_3)
+ .push_opcode(OP_3)
.push_opcode(OP_CHECKMULTISIGVERIFY)
- .push_opcode(OP_PUSHNUM_1)
+ .push_opcode(OP_1)
.into_script();
assert_eq!(multi_verify.count_sigops(), 3);
assert_eq!(multi_verify.count_sigops_legacy(), 20);
let multi_nopushnum_pushdata = Script::builder()
- .push_opcode(OP_PUSHNUM_1)
+ .push_opcode(OP_1)
.push_slice([3; 33])
.push_slice([3; 33])
.push_slice([3; 33])
@@ -894,7 +894,7 @@ fn script_get_sigop_count() {
assert_eq!(multi_nopushnum_pushdata.count_sigops(), 20);
assert_eq!(multi_nopushnum_pushdata.count_sigops_legacy(), 20);
let multi_nopushnum_op = Script::builder()
- .push_opcode(OP_PUSHNUM_1)
+ .push_opcode(OP_1)
.push_slice([3; 33])
.push_slice([3; 33])
.push_opcode(OP_DROP)
@@ -1017,10 +1017,10 @@ fn instruction_script_num_parse() {
];
let ops = [
(Instruction::Op(opcodes::all::OP_PUSHDATA4), None),
- (Instruction::Op(opcodes::all::OP_PUSHNUM_NEG1), Some(-1)),
+ (Instruction::Op(opcodes::all::OP_1NEGATE), Some(-1)),
(Instruction::Op(opcodes::all::OP_RESERVED), None),
- (Instruction::Op(opcodes::all::OP_PUSHNUM_1), Some(1)),
- (Instruction::Op(opcodes::all::OP_PUSHNUM_16), Some(16)),
+ (Instruction::Op(opcodes::all::OP_1), Some(1)),
+ (Instruction::Op(opcodes::all::OP_16), Some(16)),
(Instruction::Op(opcodes::all::OP_NOP), None),
];
for (input, expected) in &push_bytes {
diff --git a/bitcoin/src/blockdata/script/witness_version.rs b/bitcoin/src/blockdata/script/witness_version.rs
index f77a38d3..56301feb 100644
--- a/bitcoin/src/blockdata/script/witness_version.rs
+++ b/bitcoin/src/blockdata/script/witness_version.rs
@@ -122,8 +122,8 @@ impl TryFrom<Opcode> for WitnessVersion {
fn try_from(opcode: Opcode) -> Result<Self, Self::Error> {
match opcode.to_u8() {
0 => Ok(WitnessVersion::V0),
- version if version >= OP_PUSHNUM_1.to_u8() && version <= OP_PUSHNUM_16.to_u8() =>
- WitnessVersion::try_from(version - OP_PUSHNUM_1.to_u8() + 1),
+ version if version >= OP_1.to_u8() && version <= OP_16.to_u8() =>
+ WitnessVersion::try_from(version - OP_1.to_u8() + 1),
invalid => Err(TryFromError { invalid }),
}
}
@@ -145,7 +145,7 @@ impl From<WitnessVersion> for Opcode {
fn from(version: WitnessVersion) -> Opcode {
match version {
WitnessVersion::V0 => OP_PUSHBYTES_0,
- no => Opcode::from(OP_PUSHNUM_1.to_u8() + no.to_num() - 1),
+ no => Opcode::from(OP_1.to_u8() + no.to_num() - 1),
}
}
}
diff --git a/primitives/src/opcodes.rs b/primitives/src/opcodes.rs
index dc983dc5..ea13fef7 100644
--- a/primitives/src/opcodes.rs
+++ b/primitives/src/opcodes.rs
@@ -55,7 +55,7 @@ macro_rules! all_opcodes {
/// Empty stack is also `FALSE`.
pub const OP_FALSE: Opcode = OP_PUSHBYTES_0;
/// Number 1 is also TRUE.
- pub const OP_TRUE: Opcode = OP_PUSHNUM_1;
+ pub const OP_TRUE: Opcode = OP_1;
/// Previously called `OP_NOP2`.
pub const OP_NOP2: Opcode = OP_CLTV;
/// Previously called `OP_NOP3`.
@@ -454,8 +454,8 @@ impl Opcode {
(OP_1NEGATE, _) => 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 + i32::from(self.code) - i32::from(OP_PUSHNUM_1.code)),
+ (op, _) if op.code >= OP_1.code && op.code <= OP_16.code =>
+ Class::PushNum(1 + i32::from(self.code) - i32::from(OP_1.code)),
// 76 opcodes of PushBytes class
(op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(u32::from(self.code)),
@@ -471,7 +471,7 @@ impl Opcode {
/// Decodes PUSHNUM [`Opcode`] as a `u8` representing its number (1-16).
///
- /// Does not convert `OP_FALSE` to 0. Only `1` to `OP_PUSHNUM_16` are covered.
+ /// Does not convert `OP_FALSE` to 0. Only `1` to `OP_16` are covered.
///
/// # Returns
///
@@ -479,8 +479,8 @@ impl Opcode {
#[inline]
#[must_use]
pub const fn decode_pushnum(self) -> Option<u8> {
- const START: u8 = OP_PUSHNUM_1.code;
- const END: u8 = OP_PUSHNUM_16.code;
+ const START: u8 = OP_1.code;
+ const END: u8 = OP_16.code;
match self.code {
START..=END => Some(self.code - START + 1),
_ => None,
@@ -633,11 +633,11 @@ mod tests {
fn decode_pushnum() {
// Test all possible opcodes
// - Sanity check
- assert_eq!(OP_PUSHNUM_1.code, 0x51_u8);
- assert_eq!(OP_PUSHNUM_16.code, 0x60_u8);
+ assert_eq!(OP_1.code, 0x51_u8);
+ assert_eq!(OP_16.code, 0x60_u8);
for i in 0x00..=0xff_u8 {
let expected = match i {
- // OP_PUSHNUM_1 ..= OP_PUSHNUM_16
+ // OP_1 ..= OP_16
0x51..=0x60 => Some(i - 0x50),
_ => None,
};
@@ -647,22 +647,22 @@ mod tests {
// Test the named opcode constants
// - This is the OP right before PUSHNUMs start
assert!(OP_RESERVED.decode_pushnum().is_none());
- assert_eq!(OP_PUSHNUM_1.decode_pushnum().expect("pushnum"), 1);
- assert_eq!(OP_PUSHNUM_2.decode_pushnum().expect("pushnum"), 2);
- assert_eq!(OP_PUSHNUM_3.decode_pushnum().expect("pushnum"), 3);
- assert_eq!(OP_PUSHNUM_4.decode_pushnum().expect("pushnum"), 4);
- assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5);
- assert_eq!(OP_PUSHNUM_6.decode_pushnum().expect("pushnum"), 6);
- assert_eq!(OP_PUSHNUM_7.decode_pushnum().expect("pushnum"), 7);
- assert_eq!(OP_PUSHNUM_8.decode_pushnum().expect("pushnum"), 8);
- assert_eq!(OP_PUSHNUM_9.decode_pushnum().expect("pushnum"), 9);
- assert_eq!(OP_PUSHNUM_10.decode_pushnum().expect("pushnum"), 10);
- assert_eq!(OP_PUSHNUM_11.decode_pushnum().expect("pushnum"), 11);
- assert_eq!(OP_PUSHNUM_12.decode_pushnum().expect("pushnum"), 12);
- assert_eq!(OP_PUSHNUM_13.decode_pushnum().expect("pushnum"), 13);
- assert_eq!(OP_PUSHNUM_14.decode_pushnum().expect("pushnum"), 14);
- assert_eq!(OP_PUSHNUM_15.decode_pushnum().expect("pushnum"), 15);
- assert_eq!(OP_PUSHNUM_16.decode_pushnum().expect("pushnum"), 16);
+ assert_eq!(OP_1.decode_pushnum().expect("pushnum"), 1);
+ assert_eq!(OP_2.decode_pushnum().expect("pushnum"), 2);
+ assert_eq!(OP_3.decode_pushnum().expect("pushnum"), 3);
+ assert_eq!(OP_4.decode_pushnum().expect("pushnum"), 4);
+ assert_eq!(OP_5.decode_pushnum().expect("pushnum"), 5);
+ assert_eq!(OP_6.decode_pushnum().expect("pushnum"), 6);
+ assert_eq!(OP_7.decode_pushnum().expect("pushnum"), 7);
+ assert_eq!(OP_8.decode_pushnum().expect("pushnum"), 8);
+ assert_eq!(OP_9.decode_pushnum().expect("pushnum"), 9);
+ assert_eq!(OP_10.decode_pushnum().expect("pushnum"), 10);
+ assert_eq!(OP_11.decode_pushnum().expect("pushnum"), 11);
+ assert_eq!(OP_12.decode_pushnum().expect("pushnum"), 12);
+ assert_eq!(OP_13.decode_pushnum().expect("pushnum"), 13);
+ assert_eq!(OP_14.decode_pushnum().expect("pushnum"), 14);
+ assert_eq!(OP_15.decode_pushnum().expect("pushnum"), 15);
+ assert_eq!(OP_16.decode_pushnum().expect("pushnum"), 16);
// - This is the OP right after PUSHNUMs end
assert!(OP_NOP.decode_pushnum().is_none());
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.