Skip to content

Commit 6029aba

Browse files
committed
Add num64_ prefix to comparison ops
1 parent 3dce073 commit 6029aba

File tree

3 files changed

+95
-77
lines changed

3 files changed

+95
-77
lines changed

doc/extension_spec.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ To use these with miniscript fragments, we can use them inside comparison extens
4444

4545
Name | Script
4646
--- | ---
47-
num_eq(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] EQUAL64`
48-
le(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] LESSTHAN64`
49-
ge(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] GREATERTHAN64`
50-
leq(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] LESSTHANOREQUAL64`
51-
geq(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] GREATERTHANOREQUAL64`
47+
num64_eq(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] EQUAL`
48+
num64_le(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] LESSTHAN64`
49+
num64_ge(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] GREATERTHAN64`
50+
num64_leq(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] LESSTHANOREQUAL64`
51+
num64_geq(NumExpr_X,NumExpr_Y) | `[NumExpr_X] [NumExpr_Y] GREATERTHANOREQUAL64`
5252

53-
- For example, `num_eq(inp_v(1),mul(curr_inp_v,20))` represents second input value is the multiplication of
53+
- For example, `num64_eq(inp_v(1),mul(curr_inp_v,20))` represents second input value is the multiplication of
5454
current input value and fourth output value. This would abort if any of the values are confidential.
5555

5656
### Tx Value introspection

src/extensions/arith.rs

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,15 @@ pub enum Arith {
526526
/// Eq
527527
/// [X] [Y] EQUAL
528528
Eq(Expr, Expr),
529-
/// Le
529+
/// Lt
530530
/// [X] [Y] LESSTHAN
531-
Le(Expr, Expr),
531+
Lt(Expr, Expr),
532532
/// Leq
533533
/// [X] [Y] LESSTHANOREQUAL
534534
Leq(Expr, Expr),
535-
/// Ge
535+
/// Gt
536536
/// [X] [Y] GREATERTHAN
537-
Ge(Expr, Expr),
537+
Gt(Expr, Expr),
538538
/// Geq
539539
/// [X] [Y] GREATERTHANOREQUAL
540540
Geq(Expr, Expr),
@@ -545,9 +545,9 @@ impl Arith {
545545
pub fn depth(&self) -> usize {
546546
match self {
547547
Arith::Eq(x, y)
548-
| Arith::Le(x, y)
548+
| Arith::Lt(x, y)
549549
| Arith::Leq(x, y)
550-
| Arith::Ge(x, y)
550+
| Arith::Gt(x, y)
551551
| Arith::Geq(x, y) => cmp::max(x.depth, y.depth),
552552
}
553553
}
@@ -556,9 +556,9 @@ impl Arith {
556556
pub fn script_size(&self) -> usize {
557557
match self {
558558
Arith::Eq(x, y)
559-
| Arith::Le(x, y)
559+
| Arith::Lt(x, y)
560560
| Arith::Leq(x, y)
561-
| Arith::Ge(x, y)
561+
| Arith::Gt(x, y)
562562
| Arith::Geq(x, y) => x.script_size + y.script_size + 1,
563563
}
564564
}
@@ -572,9 +572,9 @@ impl Arith {
572572
) -> Result<bool, EvalError> {
573573
let res = match self {
574574
Arith::Eq(x, y) => x.eval(curr_ind, tx, utxos)? == y.eval(curr_ind, tx, utxos)?,
575-
Arith::Le(x, y) => x.eval(curr_ind, tx, utxos)? < y.eval(curr_ind, tx, utxos)?,
575+
Arith::Lt(x, y) => x.eval(curr_ind, tx, utxos)? < y.eval(curr_ind, tx, utxos)?,
576576
Arith::Leq(x, y) => x.eval(curr_ind, tx, utxos)? <= y.eval(curr_ind, tx, utxos)?,
577-
Arith::Ge(x, y) => x.eval(curr_ind, tx, utxos)? > y.eval(curr_ind, tx, utxos)?,
577+
Arith::Gt(x, y) => x.eval(curr_ind, tx, utxos)? > y.eval(curr_ind, tx, utxos)?,
578578
Arith::Geq(x, y) => x.eval(curr_ind, tx, utxos)? >= y.eval(curr_ind, tx, utxos)?,
579579
};
580580
Ok(res)
@@ -588,7 +588,7 @@ impl Arith {
588588
let builder = y.push_to_builder(builder);
589589
builder.push_opcode(OP_EQUAL)
590590
}
591-
Arith::Le(x, y) => {
591+
Arith::Lt(x, y) => {
592592
let builder = x.push_to_builder(builder);
593593
let builder = y.push_to_builder(builder);
594594
builder.push_opcode(OP_LESSTHAN64)
@@ -598,7 +598,7 @@ impl Arith {
598598
let builder = y.push_to_builder(builder);
599599
builder.push_opcode(OP_LESSTHANOREQUAL64)
600600
}
601-
Arith::Ge(x, y) => {
601+
Arith::Gt(x, y) => {
602602
let builder = x.push_to_builder(builder);
603603
let builder = y.push_to_builder(builder);
604604
builder.push_opcode(OP_GREATERTHAN64)
@@ -623,9 +623,9 @@ impl Arith {
623623
let (x, pos) = Expr::from_tokens(tokens, pos)?;
624624
match last_opcode {
625625
Tk::Equal => Some((Self::Eq(x, y), pos)),
626-
Tk::Le64 => Some((Self::Le(x, y), pos)),
626+
Tk::Le64 => Some((Self::Lt(x, y), pos)),
627627
Tk::Leq64 => Some((Self::Leq(x, y), pos)),
628-
Tk::Ge64 => Some((Self::Ge(x, y), pos)),
628+
Tk::Ge64 => Some((Self::Gt(x, y), pos)),
629629
Tk::Geq64 => Some((Self::Geq(x, y), pos)),
630630
_ => None,
631631
}
@@ -770,12 +770,12 @@ impl FromTree for Box<Arith> {
770770
impl FromTree for Arith {
771771
fn from_tree(top: &expression::Tree<'_>) -> Result<Self, Error> {
772772
match (top.name, top.args.len()) {
773-
// Disambiguiate with num_eq to avoid confusion with asset_eq
774-
("num_eq", 2) => expression::binary(top, Arith::Eq),
775-
("geq", 2) => expression::binary(top, Arith::Geq),
776-
("ge", 2) => expression::binary(top, Arith::Ge),
777-
("le", 2) => expression::binary(top, Arith::Le),
778-
("leq", 2) => expression::binary(top, Arith::Leq),
773+
// Disambiguiate with num64_eq to avoid confusion with asset_eq
774+
("num64_eq", 2) => expression::binary(top, Arith::Eq),
775+
("num64_geq", 2) => expression::binary(top, Arith::Geq),
776+
("num64_gt", 2) => expression::binary(top, Arith::Gt),
777+
("num64_lt", 2) => expression::binary(top, Arith::Lt),
778+
("num64_leq", 2) => expression::binary(top, Arith::Leq),
779779
_ => Err(Error::Unexpected(format!(
780780
"{}({} args) while parsing Extension",
781781
top.name,
@@ -788,23 +788,23 @@ impl FromTree for Arith {
788788
impl fmt::Display for Arith {
789789
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
790790
match &self {
791-
Arith::Eq(x, y) => write!(f, "num_eq({},{})", x, y),
792-
Arith::Leq(x, y) => write!(f, "leq({},{})", x, y),
793-
Arith::Le(x, y) => write!(f, "le({},{})", x, y),
794-
Arith::Geq(x, y) => write!(f, "geq({},{})", x, y),
795-
Arith::Ge(x, y) => write!(f, "ge({},{})", x, y),
791+
Arith::Eq(x, y) => write!(f, "num64_eq({},{})", x, y),
792+
Arith::Leq(x, y) => write!(f, "num64_leq({},{})", x, y),
793+
Arith::Lt(x, y) => write!(f, "num64_lt({},{})", x, y),
794+
Arith::Geq(x, y) => write!(f, "num64_geq({},{})", x, y),
795+
Arith::Gt(x, y) => write!(f, "num64_gt({},{})", x, y),
796796
}
797797
}
798798
}
799799

800800
impl fmt::Debug for Arith {
801801
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
802802
match &self {
803-
Arith::Eq(x, y) => write!(f, "num_eq({:?},{:?})", x, y),
804-
Arith::Leq(x, y) => write!(f, "leq({:?},{:?})", x, y),
805-
Arith::Le(x, y) => write!(f, "le({:?},{:?})", x, y),
806-
Arith::Geq(x, y) => write!(f, "geq({:?},{:?})", x, y),
807-
Arith::Ge(x, y) => write!(f, "ge({:?},{:?})", x, y),
803+
Arith::Eq(x, y) => write!(f, "num64_eq({:?},{:?})", x, y),
804+
Arith::Leq(x, y) => write!(f, "num64_leq({:?},{:?})", x, y),
805+
Arith::Lt(x, y) => write!(f, "num64_lt({:?},{:?})", x, y),
806+
Arith::Geq(x, y) => write!(f, "num64_geq({:?},{:?})", x, y),
807+
Arith::Gt(x, y) => write!(f, "num64_gt({:?},{:?})", x, y),
808808
}
809809
}
810810
}
@@ -1065,37 +1065,39 @@ mod tests {
10651065
#[test]
10661066
fn arith_parse() {
10671067
// This does not test the evaluation
1068-
_arith_parse("num_eq(8,8)");
1069-
_arith_parse("ge(9223372036854775807,9223372036854775806)"); // 2**63-1
1068+
_arith_parse("num64_eq(8,8)");
1069+
_arith_parse("num64_gt(9223372036854775807,9223372036854775806)"); // 2**63-1
10701070

10711071
// negatives and comparisons
1072-
_arith_parse("num_eq(-8,-8)"); // negative nums
1073-
_arith_parse("ge(-8,-9)");
1074-
_arith_parse("geq(-8,-8)");
1075-
_arith_parse("leq(-8,-7)");
1076-
_arith_parse("le(-8,-7)");
1072+
_arith_parse("num64_eq(-8,-8)"); // negative nums
1073+
_arith_parse("num64_gt(-8,-9)");
1074+
_arith_parse("num64_geq(-8,-8)");
1075+
_arith_parse("num64_leq(-8,-7)");
1076+
_arith_parse("num64_lt(-8,-7)");
10771077

10781078
// test terminals parsing
1079-
_arith_parse("num_eq(inp_v(0),100)");
1080-
_arith_parse("num_eq(out_v(0),100)");
1081-
_arith_parse("num_eq(inp_issue_v(0),100)");
1082-
_arith_parse("num_eq(inp_reissue_v(0),100)");
1083-
_arith_parse("num_eq(inp_v(0),out_v(0))");
1084-
_arith_parse("num_eq(inp_issue_v(1),inp_reissue_v(1))");
1079+
_arith_parse("num64_eq(inp_v(0),100)");
1080+
_arith_parse("num64_eq(out_v(0),100)");
1081+
_arith_parse("num64_eq(inp_issue_v(0),100)");
1082+
_arith_parse("num64_eq(inp_reissue_v(0),100)");
1083+
_arith_parse("num64_eq(inp_v(0),out_v(0))");
1084+
_arith_parse("num64_eq(inp_issue_v(1),inp_reissue_v(1))");
10851085

10861086
// test combinator
1087-
_arith_parse("num_eq(add(4,3),mul(1,7))");
1088-
_arith_parse("num_eq(sub(3,3),div(0,9))");
1089-
_arith_parse("num_eq(mod(9,3),0)");
1090-
_arith_parse("num_eq(bitand(0,134),0)");
1091-
_arith_parse("num_eq(bitor(1,3),3)");
1092-
_arith_parse("num_eq(bitxor(1,3),2)");
1093-
_arith_parse("num_eq(bitinv(0),-9223372036854775808)");
1094-
_arith_parse("num_eq(neg(1),-1)");
1087+
_arith_parse("num64_eq(add(4,3),mul(1,7))");
1088+
_arith_parse("num64_eq(sub(3,3),div(0,9))");
1089+
_arith_parse("num64_eq(mod(9,3),0)");
1090+
_arith_parse("num64_eq(bitand(0,134),0)");
1091+
_arith_parse("num64_eq(bitor(1,3),3)");
1092+
_arith_parse("num64_eq(bitxor(1,3),2)");
1093+
_arith_parse("num64_eq(bitinv(0),-9223372036854775808)");
1094+
_arith_parse("num64_eq(neg(1),-1)");
10951095

10961096
// test some misc combinations with other miniscript fragments
1097-
_arith_parse("and_v(v:pk(K),ge(8,7))");
1098-
_arith_parse("and_v(v:pk(K),num_eq(mul(inp_v(0),out_v(1)),sub(add(3,inp_issue_v(1)),-9)))");
1097+
_arith_parse("and_v(v:pk(K),num64_gt(8,7))");
1098+
_arith_parse(
1099+
"and_v(v:pk(K),num64_eq(mul(inp_v(0),out_v(1)),sub(add(3,inp_issue_v(1)),-9)))",
1100+
);
10991101
}
11001102

11011103
fn _arith_parse(s: &str) {

tests/test_arith.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,60 +197,76 @@ fn test_descs(cl: &ElementsD, testdata: &TestData) {
197197
// X!: X-only key with corresponding secret key unknown
198198

199199
// Test 1: Simple spend with internal key
200-
let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(8,8)))");
200+
let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_eq(8,8)))");
201201
assert!(wit.len() == 3);
202202

203-
let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),geq(9,8)))");
203+
let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_geq(9,8)))");
204204
assert!(wit.len() == 3);
205205

206-
let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),ge(9,8)))");
206+
let wit = test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_gt(9,8)))");
207207
assert!(wit.len() == 3);
208208

209209
test_desc_satisfy(
210210
cl,
211211
testdata,
212-
"tr(X!,and_v(v:pk(X1),ge(9223372036854775807,9223372036854775806)))",
212+
"tr(X!,and_v(v:pk(X1),num64_gt(9223372036854775807,9223372036854775806)))",
213213
);
214214

215215
test_desc_satisfy(
216216
cl,
217217
testdata,
218-
"tr(X!,and_v(v:pk(X1),num_eq(inp_v(0),100000000)))",
218+
"tr(X!,and_v(v:pk(X1),num64_eq(inp_v(0),100000000)))",
219219
);
220220
test_desc_satisfy(
221221
cl,
222222
testdata,
223-
"tr(X!,and_v(v:pk(X1),num_eq(out_v(0),99997000)))",
223+
"tr(X!,and_v(v:pk(X1),num64_eq(out_v(0),99997000)))",
224224
);
225-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(out_v(1),3000)))");
226225
test_desc_satisfy(
227226
cl,
228227
testdata,
229-
"tr(X!,and_v(v:pk(X1),num_eq(inp_v(0),add(out_v(0),out_v(1)))))",
228+
"tr(X!,and_v(v:pk(X1),num64_eq(out_v(1),3000)))",
230229
);
231-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),leq(-10,-10)))");
232-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),leq(-10,-10)))");
230+
test_desc_satisfy(
231+
cl,
232+
testdata,
233+
"tr(X!,and_v(v:pk(X1),num64_eq(inp_v(0),add(out_v(0),out_v(1)))))",
234+
);
235+
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_leq(-10,-10)))");
236+
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_lt(-11,-10)))");
233237

234238
test_desc_satisfy(
235239
cl,
236240
testdata,
237-
"tr(X!,and_v(v:pk(X1),num_eq(add(6,2),mul(2,4))))",
241+
"tr(X!,and_v(v:pk(X1),num64_eq(add(6,2),mul(2,4))))",
242+
);
243+
test_desc_satisfy(
244+
cl,
245+
testdata,
246+
"tr(X!,and_v(v:pk(X1),num64_eq(sub(3,3),div(0,9))))",
247+
);
248+
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_eq(mod(9,3),0)))");
249+
test_desc_satisfy(
250+
cl,
251+
testdata,
252+
"tr(X!,and_v(v:pk(X1),num64_eq(bitand(0,134),0)))",
253+
);
254+
test_desc_satisfy(
255+
cl,
256+
testdata,
257+
"tr(X!,and_v(v:pk(X1),num64_eq(bitor(1,3),3)))",
238258
);
239259
test_desc_satisfy(
240260
cl,
241261
testdata,
242-
"tr(X!,and_v(v:pk(X1),num_eq(sub(3,3),div(0,9))))",
262+
"tr(X!,and_v(v:pk(X1),num64_eq(bitxor(1,3),2)))",
243263
);
244-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(mod(9,3),0)))");
245264
test_desc_satisfy(
246265
cl,
247266
testdata,
248-
"tr(X!,and_v(v:pk(X1),num_eq(bitand(0,134),0)))",
267+
"tr(X!,and_v(v:pk(X1),num64_eq(bitinv(0),-1)))",
249268
);
250-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(bitor(1,3),3)))");
251-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(bitxor(1,3),2)))");
252-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(bitinv(0),-1)))");
253-
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num_eq(neg(1),-1)))");
269+
test_desc_satisfy(cl, testdata, "tr(X!,and_v(v:pk(X1),num64_eq(neg(1),-1)))");
254270
}
255271

256272
#[test]

0 commit comments

Comments
 (0)