Skip to content

Commit eb6011e

Browse files
committed
feat(policy): change semantic::Policy Display to mathematical notation
Use (∧, ∨, #{...} = k) for visual distinction from concrete::Policy. FromStr is extended to accept the new form so Display round-trips. Policy::to_policy_syntax_string() emits the function-call form for cross-version comparison; the regression fuzz target uses it.
1 parent 04f1c58 commit eb6011e

5 files changed

Lines changed: 292 additions & 36 deletions

File tree

examples/htlc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() {
3737
// Lift the descriptor into an abstract policy.
3838
assert_eq!(
3939
format!("{}", htlc_descriptor.lift().unwrap()),
40-
"or(and(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111)),and(pk(020202020202020202020202020202020202020202020202020202020202020202),older(4444)))"
40+
"((pk(022222222222222222222222222222222222222222222222222222222222222222)sha256(1111111111111111111111111111111111111111111111111111111111111111))(pk(020202020202020202020202020202020202020202020202020202020202020202)older(4444)))"
4141
);
4242

4343
// Get the scriptPubkey for this Wsh descriptor.

fuzz/fuzz_targets/regression_descriptor_parse.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,38 @@ fn do_test(data: &[u8]) {
1414
(Ok(x), Err(e)) => panic!("new logic parses {} as {:?}, old fails with {}", data_str, x, e),
1515
(Err(e), Ok(x)) => panic!("old logic parses {} as {:?}, new fails with {}", data_str, x, e),
1616
(Ok(new), Ok(old)) => {
17-
use miniscript::policy::Liftable as _;
18-
use old_miniscript::policy::Liftable as _;
19-
2017
assert_eq!(
2118
old.to_string(),
2219
new.to_string(),
2320
"input {} (left is old, right is new)",
2421
data_str
2522
);
2623

27-
match (new.lift(), old.lift()) {
28-
(Err(_), Err(_)) => {}
29-
(Ok(x), Err(e)) => {
30-
panic!("new logic lifts {} as {:?}, old fails with {}", data_str, x, e)
31-
}
32-
(Err(e), Ok(x)) => {
33-
panic!("old logic lifts {} as {:?}, new fails with {}", data_str, x, e)
34-
}
35-
(Ok(new), Ok(old)) => {
36-
assert_eq!(
37-
old.to_string(),
38-
new.to_string(),
39-
"lifted input {} (left is old, right is new)",
40-
data_str
41-
)
24+
// The current crate's semantic::Policy Display was changed to
25+
// mathematical notation, which does not match old_miniscript's
26+
// function-call format. Use to_policy_syntax_string() to
27+
// serialize in the policy-syntax form so we can still do a full
28+
// structural comparison across crate versions.
29+
{
30+
use miniscript::policy::Liftable as _;
31+
use old_miniscript::policy::Liftable as _;
32+
33+
match (new.lift(), old.lift()) {
34+
(Err(_), Err(_)) => {}
35+
(Ok(x), Err(e)) => {
36+
panic!("new logic lifts {} as {:?}, old fails with {}", data_str, x, e)
37+
}
38+
(Err(e), Ok(x)) => {
39+
panic!("old logic lifts {} as {:?}, new fails with {}", data_str, x, e)
40+
}
41+
(Ok(new_lift), Ok(old_lift)) => {
42+
assert_eq!(
43+
old_lift.to_string(),
44+
new_lift.to_policy_syntax_string(),
45+
"lifted semantic policy mismatch for input {} (left is old, right is new as policy-syntax string)",
46+
data_str
47+
);
48+
}
4249
}
4350
}
4451
}

src/descriptor/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,15 +2175,15 @@ pk(03f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8))";
21752175
// Taproot structure is erased but key order preserved..
21762176
let desc = Descriptor::<String>::from_str("tr(ROOT,{pk(A1),{pk(B1),pk(B2)}})").unwrap();
21772177
let lift = desc.lift().unwrap();
2178-
assert_eq!(lift.to_string(), "or(pk(ROOT),or(pk(A1),pk(B1),pk(B2)))",);
2178+
assert_eq!(lift.to_string(), "(pk(ROOT)(pk(A1)pk(B1)pk(B2)))");
21792179
let desc = Descriptor::<String>::from_str("tr(ROOT,{{pk(A1),pk(B1)},pk(B2)})").unwrap();
21802180
let lift = desc.lift().unwrap();
2181-
assert_eq!(lift.to_string(), "or(pk(ROOT),or(pk(A1),pk(B1),pk(B2)))",);
2181+
assert_eq!(lift.to_string(), "(pk(ROOT)(pk(A1)pk(B1)pk(B2)))");
21822182

21832183
// And normalization happens
21842184
let desc = Descriptor::<String>::from_str("tr(ROOT,{0,{0,0}})").unwrap();
21852185
let lift = desc.lift().unwrap();
2186-
assert_eq!(lift.to_string(), "or(pk(ROOT),UNSATISFIABLE)",);
2186+
assert_eq!(lift.to_string(), "(pk(ROOT)UNSATISFIABLE)");
21872187
}
21882188

21892189
#[test]

src/policy/mod.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,6 @@ mod tests {
251251
assert_eq!(s.to_lowercase(), output.to_lowercase());
252252
}
253253

254-
fn semantic_policy_rtt(s: &str) {
255-
let sem = SemanticPol::from_str(s).unwrap();
256-
let output = sem.normalized().to_string();
257-
assert_eq!(s.to_lowercase(), output.to_lowercase());
258-
}
259-
260254
#[test]
261255
fn test_timelock_validity() {
262256
// only height
@@ -279,17 +273,53 @@ mod tests {
279273
concrete_policy_rtt("or(99@pk(X),1@pk(Y))");
280274
concrete_policy_rtt("and(pk(X),or(99@pk(Y),1@older(12960)))");
281275

282-
semantic_policy_rtt("pk()");
283-
semantic_policy_rtt("or(pk(X),pk(Y))");
284-
semantic_policy_rtt("and(pk(X),pk(Y))");
285-
286276
//fuzzer crashes
287277
assert!(ConcretePol::from_str("thresh()").is_err());
288278
assert!(SemanticPol::from_str("thresh(0)").is_err());
289279
assert!(SemanticPol::from_str("thresh()").is_err());
290280
concrete_policy_rtt("ripemd160()");
291281
}
292282

283+
#[test]
284+
fn semantic_display_uses_mathematical_notation() {
285+
let pol = SemanticPol::from_str("and(pk(A),pk(B))").unwrap();
286+
assert_eq!(pol.normalized().to_string(), "(pk(A) ∧ pk(B))");
287+
288+
let pol = SemanticPol::from_str("or(pk(A),pk(B))").unwrap();
289+
assert_eq!(pol.normalized().to_string(), "(pk(A) ∨ pk(B))");
290+
291+
let pol = SemanticPol::from_str("thresh(2,pk(A),pk(B),pk(C))").unwrap();
292+
assert_eq!(pol.normalized().to_string(), "#{pk(A), pk(B), pk(C)} = 2");
293+
}
294+
295+
#[test]
296+
fn semantic_display_roundtrips_via_from_str() {
297+
// from_str must accept the mathematical Display output so that
298+
// `Display` and `FromStr` round-trip (a property serde relies on).
299+
for s in [
300+
"pk(A)",
301+
"and(pk(A),pk(B))",
302+
"or(pk(A),pk(B))",
303+
"thresh(2,pk(A),pk(B),pk(C))",
304+
"and(pk(A),or(pk(B),pk(C)))",
305+
"thresh(2,pk(A),and(pk(B),pk(C)),pk(D))",
306+
] {
307+
let pol = SemanticPol::from_str(s).unwrap().normalized();
308+
let displayed = pol.to_string();
309+
let reparsed = SemanticPol::from_str(&displayed).unwrap().normalized();
310+
assert_eq!(pol, reparsed, "round-trip failed for {}", s);
311+
}
312+
313+
// Parser must also accept direct mathematical-form input.
314+
let math = "(pk(A) ∧ (pk(B) ∨ pk(C)))";
315+
let fncall = "and(pk(A),or(pk(B),pk(C)))";
316+
assert_eq!(SemanticPol::from_str(math).unwrap(), SemanticPol::from_str(fncall).unwrap(),);
317+
318+
let math = "#{pk(A), pk(B), pk(C)} = 2";
319+
let fncall = "thresh(2,pk(A),pk(B),pk(C))";
320+
assert_eq!(SemanticPol::from_str(math).unwrap(), SemanticPol::from_str(fncall).unwrap(),);
321+
}
322+
293323
#[test]
294324
fn compile_invalid() {
295325
// Since the root Error does not support Eq type, we have to

0 commit comments

Comments
 (0)