Skip to content

Commit cf3d919

Browse files
committed
FIx equal verify bugs
And other bugs
1 parent ffae3ad commit cf3d919

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

src/miniscript/astelem.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
701701
.push_opcode(opcodes::all::OP_EQUAL),
702702
Terminal::True => builder.push_opcode(opcodes::OP_TRUE),
703703
Terminal::False => builder.push_opcode(opcodes::OP_FALSE),
704-
Terminal::Version(n) => builder.check_item_eq(11, &serialize(&n)),
705-
Terminal::OutputsPref(ref pref) => builder.check_item_pref(3, pref),
704+
Terminal::Version(n) => builder.check_item_eq(1, &serialize(&n)),
705+
Terminal::OutputsPref(ref pref) => builder.check_item_pref(9, pref),
706706
Terminal::Alt(ref sub) => builder
707707
.push_opcode(opcodes::all::OP_TOALTSTACK)
708708
.push_astelem(sub)

src/miniscript/decode.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,31 @@ pub fn parse<Ctx: ScriptContext>(
282282
))?
283283
},
284284
),
285+
Tk::Push4(ver), Tk::Pick, Tk::Sub, Tk::Depth => match_token!(
286+
tokens,
287+
Tk::Num(2) => {
288+
non_term.push(NonTerm::Verify);
289+
term.reduce0(Terminal::Version(ver))?
290+
},
291+
),
292+
Tk::Pick, Tk::Sub, Tk::Depth => match_token!(
293+
tokens,
294+
Tk::Num(10) => match_token!(
295+
tokens,
296+
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::Push(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
297+
{
298+
non_term.push(NonTerm::Verify);
299+
term.reduce0(Terminal::OutputsPref(bytes))?
300+
},
301+
),
302+
),
303+
Tk::Num(k) => {
304+
non_term.push(NonTerm::Verify);
305+
non_term.push(NonTerm::ThreshW {
306+
k: k as usize,
307+
n: 0
308+
});
309+
},
285310
),
286311
x => {
287312
tokens.un_next(x);
@@ -337,13 +362,13 @@ pub fn parse<Ctx: ScriptContext>(
337362
),
338363
Tk::Push4(ver), Tk::Pick, Tk::Sub, Tk::Depth => match_token!(
339364
tokens,
340-
Tk::Num(12) => term.reduce0(Terminal::Version(ver))?,
365+
Tk::Num(2) => term.reduce0(Terminal::Version(ver))?,
341366
),
342367
Tk::Pick, Tk::Sub, Tk::Depth => match_token!(
343368
tokens,
344-
Tk::Num(3) => match_token!(
369+
Tk::Num(10) => match_token!(
345370
tokens,
346-
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::PushPrefCat(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
371+
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::Push(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
347372
term.reduce0(Terminal::OutputsPref(bytes))?,
348373
),
349374
),

src/miniscript/lex.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub enum Token {
6666
Hash32([u8; 32]),
6767
Pubkey(PublicKey),
6868
Push4(u32),
69-
PushPrefCat(Vec<u8>), // <pref> <swap> <cat> /* prefix is catted */
69+
Push(Vec<u8>), // <pref> <swap> <cat> /* prefix is catted */
7070
}
7171

7272
impl fmt::Display for Token {
@@ -253,12 +253,15 @@ pub fn lex(script: &script::Script) -> Result<Vec<Token>, Error> {
253253
ret.push(Token::Hash256);
254254
}
255255
script::Instruction::PushBytes(bytes) => {
256-
// Check for PushPrefCat
256+
// Check for Push
257257
let ret_len = ret.len();
258-
if ret.last() == Some(&Token::Swap) && ret.get(ret_len - 2) == Some(&Token::Cat) {
259-
ret.pop().unwrap();
260-
ret.pop().unwrap();
261-
ret.push(Token::PushPrefCat(bytes.to_owned()));
258+
// See that last five elements are CAT
259+
// Checking only two here
260+
if ret_len >= 5
261+
&& ret.last() == Some(&Token::Cat)
262+
&& ret.get(ret_len - 2) == Some(&Token::Cat)
263+
{
264+
ret.push(Token::Push(bytes.to_owned()));
262265
}
263266
// Special handling of tokens for Covenants
264267
// To determine whether some Token is actually

src/miniscript/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,10 @@ mod tests {
653653
&ms_str!("tv:1"),
654654
"Script(OP_PUSHNUM_1 OP_VERIFY OP_PUSHNUM_1)",
655655
);
656+
roundtrip(
657+
&ms_str!("tv:thresh(1,pk(02d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e))", ),
658+
"Script(OP_PUSHBYTES_33 02d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e OP_CHECKSIG OP_PUSHNUM_1 OP_EQUALVERIFY OP_PUSHNUM_1)",
659+
);
656660
roundtrip(&ms_str!("0"), "Script(OP_0)");
657661
roundtrip(
658662
&ms_str!("andor(0,1,0)"),
@@ -867,6 +871,12 @@ mod tests {
867871
OP_PUSHNUM_1\
868872
)"
869873
);
874+
875+
// Thresh bug with equal verify roundtrip
876+
roundtrip(
877+
&ms_str!("tv:thresh(1,pk(02d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e))", ),
878+
"Script(OP_PUSHBYTES_33 02d7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e OP_CHECKSIG OP_PUSHNUM_1 OP_EQUALVERIFY OP_PUSHNUM_1)",
879+
);
870880
}
871881

872882
#[test]
@@ -896,7 +906,7 @@ mod tests {
896906
fn cov_script_rtt() {
897907
roundtrip(
898908
&ms_str!("ver_eq(4)"),
899-
"Script(OP_PUSHNUM_12 OP_DEPTH OP_SUB OP_PICK OP_PUSHBYTES_4 04000000 OP_EQUAL)",
909+
"Script(OP_PUSHNUM_2 OP_DEPTH OP_SUB OP_PICK OP_PUSHBYTES_4 04000000 OP_EQUAL)",
900910
);
901911
}
902912
}

0 commit comments

Comments
 (0)