Skip to content

Commit ee68e80

Browse files
committed
Merge rust-bitcoin#2535: Move read_scriptint to PushBytes & create read_int function
d6ef16a update api (Divyansh Gupta) a336ec0 refactor(script): move `read_scriptint` to `PushBytes` & create `read_int` function (Divyansh Gupta) Pull request description: * Moved `read_scriptint` method to `Push_Bytes` struct * Created `Instruction::read_int` method which acts as a wrappper around this function. Done as part of rust-bitcoin#1547 ACKs for top commit: apoelstra: ACK d6ef16a tcharding: ACK d6ef16a Tree-SHA512: e33df8adcb1c23351da303f6bad1ea4a8eae30e65943d230ae886183a01f970aecd0c8c8fd3a6c337cfe6dde1b7590778d88c308415e393f137065ef7da4b29c
2 parents 6cfdbc9 + d6ef16a commit ee68e80

File tree

9 files changed

+113
-61
lines changed

9 files changed

+113
-61
lines changed

api/bitcoin/all-features.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7849,6 +7849,7 @@ pub fn bitcoin::blockdata::script::Instruction<'a>::eq(&self, other: &bitcoin::b
78497849
pub fn bitcoin::blockdata::script::Instruction<'a>::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
78507850
pub fn bitcoin::blockdata::script::Instruction<'a>::opcode(&self) -> core::option::Option<bitcoin::blockdata::opcodes::Opcode>
78517851
pub fn bitcoin::blockdata::script::Instruction<'a>::push_bytes(&self) -> core::option::Option<&bitcoin::blockdata::script::PushBytes>
7852+
pub fn bitcoin::blockdata::script::Instruction<'a>::read_int(&self) -> core::option::Option<i64>
78527853
pub fn bitcoin::blockdata::script::Instruction<'a>::script_num(&self) -> core::option::Option<i64>
78537854
pub fn bitcoin::blockdata::script::InstructionIndices<'a>::as_script(&self) -> &'a bitcoin::blockdata::script::Script
78547855
pub fn bitcoin::blockdata::script::InstructionIndices<'a>::clone(&self) -> bitcoin::blockdata::script::InstructionIndices<'a>
@@ -7883,6 +7884,7 @@ pub fn bitcoin::blockdata::script::PushBytes::index(&self, index: usize) -> &Sel
78837884
pub fn bitcoin::blockdata::script::PushBytes::is_empty(&self) -> bool
78847885
pub fn bitcoin::blockdata::script::PushBytes::len(&self) -> usize
78857886
pub fn bitcoin::blockdata::script::PushBytes::partial_cmp(&self, other: &bitcoin::blockdata::script::PushBytes) -> core::option::Option<core::cmp::Ordering>
7887+
pub fn bitcoin::blockdata::script::PushBytes::read_scriptint(&self) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
78867888
pub fn bitcoin::blockdata::script::PushBytes::to_owned(&self) -> Self::Owned
78877889
pub fn bitcoin::blockdata::script::PushBytesBuf::as_mut(&mut self) -> &mut bitcoin::blockdata::script::PushBytes
78887890
pub fn bitcoin::blockdata::script::PushBytesBuf::as_mut_push_bytes(&mut self) -> &mut bitcoin::blockdata::script::PushBytes
@@ -8247,7 +8249,6 @@ pub fn bitcoin::blockdata::script::WScriptHash::serialize<S: serde::ser::Seriali
82478249
pub fn bitcoin::blockdata::script::WScriptHash::to_byte_array(self) -> Self::Bytes
82488250
pub fn bitcoin::blockdata::script::WScriptHash::to_raw_hash(self) -> bitcoin_hashes::sha256::Hash
82498251
pub fn bitcoin::blockdata::script::read_scriptbool(v: &[u8]) -> bool
8250-
pub fn bitcoin::blockdata::script::read_scriptint(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
82518252
pub fn bitcoin::blockdata::script::read_scriptint_non_minimal(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
82528253
pub fn bitcoin::blockdata::script::witness_program::Error::clone(&self) -> bitcoin::blockdata::script::witness_program::Error
82538254
pub fn bitcoin::blockdata::script::witness_program::Error::eq(&self, other: &bitcoin::blockdata::script::witness_program::Error) -> bool
@@ -9260,7 +9261,6 @@ pub fn bitcoin::psbt::raw::ProprietaryKey<Subtype>::to_key(&self) -> bitcoin::ps
92609261
pub fn bitcoin::psbt::raw::ProprietaryKey<Subtype>::try_from(key: bitcoin::psbt::raw::Key) -> core::result::Result<Self, Self::Error>
92619262
pub fn bitcoin::script::PushBytesErrorReport::input_len(&self) -> usize
92629263
pub fn bitcoin::script::read_scriptbool(v: &[u8]) -> bool
9263-
pub fn bitcoin::script::read_scriptint(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
92649264
pub fn bitcoin::script::read_scriptint_non_minimal(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
92659265
pub fn bitcoin::script::write_scriptint(out: &mut [u8; 8], n: i64) -> usize
92669266
pub fn bitcoin::sighash::Annex<'a>::as_bytes(&self) -> &[u8]

api/bitcoin/default-features.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7465,6 +7465,7 @@ pub fn bitcoin::blockdata::script::Instruction<'a>::eq(&self, other: &bitcoin::b
74657465
pub fn bitcoin::blockdata::script::Instruction<'a>::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
74667466
pub fn bitcoin::blockdata::script::Instruction<'a>::opcode(&self) -> core::option::Option<bitcoin::blockdata::opcodes::Opcode>
74677467
pub fn bitcoin::blockdata::script::Instruction<'a>::push_bytes(&self) -> core::option::Option<&bitcoin::blockdata::script::PushBytes>
7468+
pub fn bitcoin::blockdata::script::Instruction<'a>::read_int(&self) -> core::option::Option<i64>
74687469
pub fn bitcoin::blockdata::script::Instruction<'a>::script_num(&self) -> core::option::Option<i64>
74697470
pub fn bitcoin::blockdata::script::InstructionIndices<'a>::as_script(&self) -> &'a bitcoin::blockdata::script::Script
74707471
pub fn bitcoin::blockdata::script::InstructionIndices<'a>::clone(&self) -> bitcoin::blockdata::script::InstructionIndices<'a>
@@ -7499,6 +7500,7 @@ pub fn bitcoin::blockdata::script::PushBytes::index(&self, index: usize) -> &Sel
74997500
pub fn bitcoin::blockdata::script::PushBytes::is_empty(&self) -> bool
75007501
pub fn bitcoin::blockdata::script::PushBytes::len(&self) -> usize
75017502
pub fn bitcoin::blockdata::script::PushBytes::partial_cmp(&self, other: &bitcoin::blockdata::script::PushBytes) -> core::option::Option<core::cmp::Ordering>
7503+
pub fn bitcoin::blockdata::script::PushBytes::read_scriptint(&self) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
75027504
pub fn bitcoin::blockdata::script::PushBytes::to_owned(&self) -> Self::Owned
75037505
pub fn bitcoin::blockdata::script::PushBytesBuf::as_mut(&mut self) -> &mut bitcoin::blockdata::script::PushBytes
75047506
pub fn bitcoin::blockdata::script::PushBytesBuf::as_mut_push_bytes(&mut self) -> &mut bitcoin::blockdata::script::PushBytes
@@ -7852,7 +7854,6 @@ pub fn bitcoin::blockdata::script::WScriptHash::partial_cmp(&self, other: &bitco
78527854
pub fn bitcoin::blockdata::script::WScriptHash::to_byte_array(self) -> Self::Bytes
78537855
pub fn bitcoin::blockdata::script::WScriptHash::to_raw_hash(self) -> bitcoin_hashes::sha256::Hash
78547856
pub fn bitcoin::blockdata::script::read_scriptbool(v: &[u8]) -> bool
7855-
pub fn bitcoin::blockdata::script::read_scriptint(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
78567857
pub fn bitcoin::blockdata::script::read_scriptint_non_minimal(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
78577858
pub fn bitcoin::blockdata::script::witness_program::Error::clone(&self) -> bitcoin::blockdata::script::witness_program::Error
78587859
pub fn bitcoin::blockdata::script::witness_program::Error::eq(&self, other: &bitcoin::blockdata::script::witness_program::Error) -> bool
@@ -8770,7 +8771,6 @@ pub fn bitcoin::psbt::raw::ProprietaryKey<Subtype>::to_key(&self) -> bitcoin::ps
87708771
pub fn bitcoin::psbt::raw::ProprietaryKey<Subtype>::try_from(key: bitcoin::psbt::raw::Key) -> core::result::Result<Self, Self::Error>
87718772
pub fn bitcoin::script::PushBytesErrorReport::input_len(&self) -> usize
87728773
pub fn bitcoin::script::read_scriptbool(v: &[u8]) -> bool
8773-
pub fn bitcoin::script::read_scriptint(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
87748774
pub fn bitcoin::script::read_scriptint_non_minimal(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
87758775
pub fn bitcoin::script::write_scriptint(out: &mut [u8; 8], n: i64) -> usize
87768776
pub fn bitcoin::sighash::Annex<'a>::as_bytes(&self) -> &[u8]

api/bitcoin/no-features.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6821,6 +6821,7 @@ pub fn bitcoin::blockdata::script::Instruction<'a>::eq(&self, other: &bitcoin::b
68216821
pub fn bitcoin::blockdata::script::Instruction<'a>::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
68226822
pub fn bitcoin::blockdata::script::Instruction<'a>::opcode(&self) -> core::option::Option<bitcoin::blockdata::opcodes::Opcode>
68236823
pub fn bitcoin::blockdata::script::Instruction<'a>::push_bytes(&self) -> core::option::Option<&bitcoin::blockdata::script::PushBytes>
6824+
pub fn bitcoin::blockdata::script::Instruction<'a>::read_int(&self) -> core::option::Option<i64>
68246825
pub fn bitcoin::blockdata::script::Instruction<'a>::script_num(&self) -> core::option::Option<i64>
68256826
pub fn bitcoin::blockdata::script::InstructionIndices<'a>::as_script(&self) -> &'a bitcoin::blockdata::script::Script
68266827
pub fn bitcoin::blockdata::script::InstructionIndices<'a>::clone(&self) -> bitcoin::blockdata::script::InstructionIndices<'a>
@@ -6855,6 +6856,7 @@ pub fn bitcoin::blockdata::script::PushBytes::index(&self, index: usize) -> &Sel
68556856
pub fn bitcoin::blockdata::script::PushBytes::is_empty(&self) -> bool
68566857
pub fn bitcoin::blockdata::script::PushBytes::len(&self) -> usize
68576858
pub fn bitcoin::blockdata::script::PushBytes::partial_cmp(&self, other: &bitcoin::blockdata::script::PushBytes) -> core::option::Option<core::cmp::Ordering>
6859+
pub fn bitcoin::blockdata::script::PushBytes::read_scriptint(&self) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
68586860
pub fn bitcoin::blockdata::script::PushBytes::to_owned(&self) -> Self::Owned
68596861
pub fn bitcoin::blockdata::script::PushBytesBuf::as_mut(&mut self) -> &mut bitcoin::blockdata::script::PushBytes
68606862
pub fn bitcoin::blockdata::script::PushBytesBuf::as_mut_push_bytes(&mut self) -> &mut bitcoin::blockdata::script::PushBytes
@@ -7207,7 +7209,6 @@ pub fn bitcoin::blockdata::script::WScriptHash::partial_cmp(&self, other: &bitco
72077209
pub fn bitcoin::blockdata::script::WScriptHash::to_byte_array(self) -> Self::Bytes
72087210
pub fn bitcoin::blockdata::script::WScriptHash::to_raw_hash(self) -> bitcoin_hashes::sha256::Hash
72097211
pub fn bitcoin::blockdata::script::read_scriptbool(v: &[u8]) -> bool
7210-
pub fn bitcoin::blockdata::script::read_scriptint(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
72117212
pub fn bitcoin::blockdata::script::read_scriptint_non_minimal(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
72127213
pub fn bitcoin::blockdata::script::witness_program::Error::clone(&self) -> bitcoin::blockdata::script::witness_program::Error
72137214
pub fn bitcoin::blockdata::script::witness_program::Error::eq(&self, other: &bitcoin::blockdata::script::witness_program::Error) -> bool
@@ -7910,7 +7911,6 @@ pub fn bitcoin::psbt::raw::ProprietaryKey<Subtype>::to_key(&self) -> bitcoin::ps
79107911
pub fn bitcoin::psbt::raw::ProprietaryKey<Subtype>::try_from(key: bitcoin::psbt::raw::Key) -> core::result::Result<Self, Self::Error>
79117912
pub fn bitcoin::script::PushBytesErrorReport::input_len(&self) -> usize
79127913
pub fn bitcoin::script::read_scriptbool(v: &[u8]) -> bool
7913-
pub fn bitcoin::script::read_scriptint(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
79147914
pub fn bitcoin::script::read_scriptint_non_minimal(v: &[u8]) -> core::result::Result<i64, bitcoin::blockdata::script::Error>
79157915
pub fn bitcoin::script::write_scriptint(out: &mut [u8; 8], n: i64) -> usize
79167916
pub fn bitcoin::sighash::Annex<'a>::as_bytes(&self) -> &[u8]

bitcoin/src/blockdata/block.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::consensus::{encode, Decodable, Encodable, Params};
1919
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
2020
use crate::pow::{CompactTarget, Target, Work};
2121
use crate::prelude::*;
22+
use crate::script::PushBytes;
2223
use crate::{merkle_tree, VarInt};
2324

2425
hashes::hash_newtype! {
@@ -378,7 +379,7 @@ impl Block {
378379
match push.map_err(|_| Bip34Error::NotPresent)? {
379380
script::Instruction::PushBytes(b) => {
380381
// Check that the number is encoded in the minimal way.
381-
let h = script::read_scriptint(b.as_bytes())
382+
let h = PushBytes::read_scriptint(b)
382383
.map_err(|_e| Bip34Error::UnexpectedPush(b.as_bytes().to_vec()))?;
383384
if h < 0 {
384385
Err(Bip34Error::NegativeHeight)

bitcoin/src/blockdata/script/instruction.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ impl<'a> Instruction<'a> {
6161
Instruction::PushBytes(bytes) => ScriptBuf::reserved_len_for_slice(bytes.len()),
6262
}
6363
}
64+
65+
/// Reads an integer from an Instruction,
66+
/// returning Some(i64) for valid opcodes or pushed bytes, otherwise None
67+
pub fn read_int(&self) -> Option<i64> {
68+
match self {
69+
Instruction::Op(op) => {
70+
let v = op.to_u8();
71+
match v {
72+
// OP_PUSHNUM_1 ..= OP_PUSHNUM_16
73+
0x51..=0x60 => Some(v as i64 - 0x50),
74+
// OP_PUSHNUM_NEG1
75+
0x4f => Some(-1),
76+
_ => None,
77+
}
78+
}
79+
Instruction::PushBytes(bytes) => match PushBytes::read_scriptint(bytes) {
80+
Ok(v) => Some(v),
81+
_ => None,
82+
},
83+
}
84+
}
6485
}
6586

6687
/// Iterator over a script returning parsed opcodes.

bitcoin/src/blockdata/script/mod.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -155,54 +155,10 @@ pub fn write_scriptint(out: &mut [u8; 8], n: i64) -> usize {
155155
len
156156
}
157157

158-
/// Decodes an integer in script(minimal CScriptNum) format.
159-
///
160-
/// Notice that this fails on overflow: the result is the same as in
161-
/// bitcoind, that only 4-byte signed-magnitude values may be read as
162-
/// numbers. They can be added or subtracted (and a long time ago,
163-
/// multiplied and divided), and this may result in numbers which
164-
/// can't be written out in 4 bytes or less. This is ok! The number
165-
/// just can't be read as a number again.
166-
/// This is a bit crazy and subtle, but it makes sense: you can load
167-
/// 32-bit numbers and do anything with them, which back when mult/div
168-
/// was allowed, could result in up to a 64-bit number. We don't want
169-
/// overflow since that's surprising --- and we don't want numbers that
170-
/// don't fit in 64 bits (for efficiency on modern processors) so we
171-
/// simply say, anything in excess of 32 bits is no longer a number.
172-
/// This is basically a ranged type implementation.
173-
///
174-
/// This code is based on the `CScriptNum` constructor in Bitcoin Core (see `script.h`).
175-
pub fn read_scriptint(v: &[u8]) -> Result<i64, Error> {
176-
let last = match v.last() {
177-
Some(last) => last,
178-
None => return Ok(0),
179-
};
180-
if v.len() > 4 {
181-
return Err(Error::NumericOverflow);
182-
}
183-
// Comment and code copied from Bitcoin Core:
184-
// https://github.com/bitcoin/bitcoin/blob/447f50e4aed9a8b1d80e1891cda85801aeb80b4e/src/script/script.h#L247-L262
185-
// If the most-significant-byte - excluding the sign bit - is zero
186-
// then we're not minimal. Note how this test also rejects the
187-
// negative-zero encoding, 0x80.
188-
if (*last & 0x7f) == 0 {
189-
// One exception: if there's more than one byte and the most
190-
// significant bit of the second-most-significant-byte is set
191-
// it would conflict with the sign bit. An example of this case
192-
// is +-255, which encode to 0xff00 and 0xff80 respectively.
193-
// (big-endian).
194-
if v.len() <= 1 || (v[v.len() - 2] & 0x80) == 0 {
195-
return Err(Error::NonMinimalPush);
196-
}
197-
}
198-
199-
Ok(scriptint_parse(v))
200-
}
201-
202158
/// Decodes an integer in script format without non-minimal error.
203159
///
204160
/// The overflow error for slices over 4 bytes long is still there.
205-
/// See [`read_scriptint`] for a description of some subtleties of
161+
/// See [`push_bytes::PushBytes::read_scriptint`] for a description of some subtleties of
206162
/// this function.
207163
pub fn read_scriptint_non_minimal(v: &[u8]) -> Result<i64, Error> {
208164
if v.is_empty() {

bitcoin/src/blockdata/script/push_bytes.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ mod primitive {
1919
};
2020

2121
use super::PushBytesError;
22+
use crate::blockdata::script::Error;
2223
#[allow(unused)]
2324
use crate::prelude::*;
25+
use crate::script::scriptint_parse;
2426

2527
#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
2628
fn check_limit(_: usize) -> Result<(), PushBytesError> { Ok(()) }
@@ -73,6 +75,50 @@ mod primitive {
7375

7476
/// Returns the underlying mutbale bytes.
7577
pub fn as_mut_bytes(&mut self) -> &mut [u8] { &mut self.0 }
78+
79+
/// Decodes an integer in script(minimal CScriptNum) format.
80+
///
81+
/// Notice that this fails on overflow: the result is the same as in
82+
/// bitcoind, that only 4-byte signed-magnitude values may be read as
83+
/// numbers. They can be added or subtracted (and a long time ago,
84+
/// multiplied and divided), and this may result in numbers which
85+
/// can't be written out in 4 bytes or less. This is ok! The number
86+
/// just can't be read as a number again.
87+
/// This is a bit crazy and subtle, but it makes sense: you can load
88+
/// 32-bit numbers and do anything with them, which back when mult/div
89+
/// was allowed, could result in up to a 64-bit number. We don't want
90+
/// overflow since that's surprising --- and we don't want numbers that
91+
/// don't fit in 64 bits (for efficiency on modern processors) so we
92+
/// simply say, anything in excess of 32 bits is no longer a number.
93+
/// This is basically a ranged type implementation.
94+
///
95+
/// This code is based on the `CScriptNum` constructor in Bitcoin Core (see `script.h`).
96+
pub fn read_scriptint(&self) -> Result<i64, Error> {
97+
let last = match self.as_bytes().last() {
98+
Some(last) => last,
99+
None => return Ok(0),
100+
};
101+
if self.len() > 4 {
102+
return Err(Error::NumericOverflow);
103+
}
104+
// Comment and code copied from Bitcoin Core:
105+
// https://github.com/bitcoin/bitcoin/blob/447f50e4aed9a8b1d80e1891cda85801aeb80b4e/src/script/script.h#L247-L262
106+
// If the most-significant-byte - excluding the sign bit - is zero
107+
// then we're not minimal. Note how this test also rejects the
108+
// negative-zero encoding, 0x80.
109+
if (*last & 0x7f) == 0 {
110+
// One exception: if there's more than one byte and the most
111+
// significant bit of the second-most-significant-byte is set
112+
// it would conflict with the sign bit. An example of this case
113+
// is +-255, which encode to 0xff00 and 0xff80 respectively.
114+
// (big-endian).
115+
if self.len() <= 1 || (self[self.len() - 2] & 0x80) == 0 {
116+
return Err(Error::NonMinimalPush);
117+
}
118+
}
119+
120+
Ok(scriptint_parse(self.as_bytes()))
121+
}
76122
}
77123

78124
macro_rules! delegate_index {

bitcoin/src/blockdata/script/tests.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,23 +310,51 @@ fn scriptint_round_trip() {
310310
-((1 << 31) - 1),
311311
];
312312
for &i in test_vectors.iter() {
313-
assert_eq!(Ok(i), read_scriptint(&build_scriptint(i)));
314-
assert_eq!(Ok(-i), read_scriptint(&build_scriptint(-i)));
313+
assert_eq!(
314+
Ok(i),
315+
PushBytes::read_scriptint(
316+
<&PushBytes>::try_from(build_scriptint(i).as_slice()).unwrap()
317+
)
318+
);
319+
assert_eq!(
320+
Ok(-i),
321+
PushBytes::read_scriptint(
322+
<&PushBytes>::try_from(build_scriptint(-i).as_slice()).unwrap()
323+
)
324+
);
315325
assert_eq!(Ok(i), read_scriptint_non_minimal(&build_scriptint(i)));
316326
assert_eq!(Ok(-i), read_scriptint_non_minimal(&build_scriptint(-i)));
317327
}
318-
assert!(read_scriptint(&build_scriptint(1 << 31)).is_err());
319-
assert!(read_scriptint(&build_scriptint(-(1 << 31))).is_err());
328+
assert!(PushBytes::read_scriptint(
329+
<&PushBytes>::try_from(build_scriptint(1 << 31).as_slice()).unwrap()
330+
)
331+
.is_err());
332+
assert!(PushBytes::read_scriptint(
333+
<&PushBytes>::try_from(build_scriptint(-(1 << 31)).as_slice()).unwrap()
334+
)
335+
.is_err());
320336
assert!(read_scriptint_non_minimal(&build_scriptint(1 << 31)).is_err());
321337
assert!(read_scriptint_non_minimal(&build_scriptint(-(1 << 31))).is_err());
322338
}
323339

324340
#[test]
325341
fn non_minimal_scriptints() {
326-
assert_eq!(read_scriptint(&[0x80, 0x00]), Ok(0x80));
327-
assert_eq!(read_scriptint(&[0xff, 0x00]), Ok(0xff));
328-
assert_eq!(read_scriptint(&[0x8f, 0x00, 0x00]), Err(Error::NonMinimalPush));
329-
assert_eq!(read_scriptint(&[0x7f, 0x00]), Err(Error::NonMinimalPush));
342+
assert_eq!(
343+
PushBytes::read_scriptint(<[_; 2] as AsRef<PushBytes>>::as_ref(&[0x80, 0x00])),
344+
Ok(0x80)
345+
);
346+
assert_eq!(
347+
PushBytes::read_scriptint(<[_; 2] as AsRef<PushBytes>>::as_ref(&[0xff, 0x00])),
348+
Ok(0xff)
349+
);
350+
assert_eq!(
351+
PushBytes::read_scriptint(<[_; 3] as AsRef<PushBytes>>::as_ref(&[0x8f, 0x00, 0x00])),
352+
Err(Error::NonMinimalPush)
353+
);
354+
assert_eq!(
355+
PushBytes::read_scriptint(<[_; 2] as AsRef<PushBytes>>::as_ref(&[0x7f, 0x00])),
356+
Err(Error::NonMinimalPush)
357+
);
330358

331359
assert_eq!(read_scriptint_non_minimal(&[0x80, 0x00]), Ok(0x80));
332360
assert_eq!(read_scriptint_non_minimal(&[0xff, 0x00]), Ok(0xff));

0 commit comments

Comments
 (0)