Skip to content

Commit

Permalink
Semantic analysis now uses only thresh
Browse files Browse the repository at this point in the history
  • Loading branch information
sanket1729 committed Aug 27, 2020
1 parent 5e5b0b6 commit 0c69589
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 135 deletions.
3 changes: 1 addition & 2 deletions examples/htlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ fn main() {

assert_eq!(
format!("{}", htlc_descriptor.lift()),
"or(and(pkh(4377a5acd66dc5cb67148a24818d1e51fa183bd2),and(pkh(4377a5acd66dc5cb67148a24818d1e51fa183bd2),older(4444))),sha256(1111111111111111111111111111111111111111111111111111111111111111))"
);
"or(and(pkh(4377a5acd66dc5cb67148a24818d1e51fa183bd2),pkh(4377a5acd66dc5cb67148a24818d1e51fa183bd2),older(4444)),sha256(1111111111111111111111111111111111111111111111111111111111111111))");

assert_eq!(
format!("{:x}", htlc_descriptor.script_pubkey()),
Expand Down
10 changes: 10 additions & 0 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub enum PolicyError {
ZeroTime,
/// `after` fragment can only have ` n < 2^31`
TimeTooFar,
/// Semantic Policy Error: `And` `Or` fragments must take args: k > 1
InsufficientArgsforAnd,
/// Semantic Policy Error: `And` `Or` fragments must take args: k > 1
InsufficientArgsforOr,
}

impl error::Error for PolicyError {
Expand All @@ -97,6 +101,12 @@ impl fmt::Display for PolicyError {
f.write_str("Relative/Absolute time must be less than 2^31; n < 2^31")
}
PolicyError::ZeroTime => f.write_str("Time must be greater than 0; n > 0"),
PolicyError::InsufficientArgsforAnd => {
f.write_str("Semantic Policy 'And' fragment must have atleast 2 args ")
}
PolicyError::InsufficientArgsforOr => {
f.write_str("Semantic Policy 'Or' fragment must have atleast 2 args ")
}
}
}
}
Expand Down
25 changes: 16 additions & 9 deletions src/policy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,20 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Liftable<Pk> for Terminal<Pk, Ctx> {
| Terminal::NonZero(ref sub)
| Terminal::ZeroNotEqual(ref sub) => sub.node.lift(),
Terminal::AndV(ref left, ref right) | Terminal::AndB(ref left, ref right) => {
Semantic::And(vec![left.node.lift(), right.node.lift()])
Semantic::Threshold(2, vec![left.node.lift(), right.node.lift()])
}
Terminal::AndOr(ref a, ref b, ref c) => Semantic::Or(vec![
Semantic::And(vec![a.node.lift(), c.node.lift()]),
b.node.lift(),
]),
Terminal::AndOr(ref a, ref b, ref c) => Semantic::Threshold(
1,
vec![
Semantic::Threshold(2, vec![a.node.lift(), c.node.lift()]),
b.node.lift(),
],
),
Terminal::OrB(ref left, ref right)
| Terminal::OrD(ref left, ref right)
| Terminal::OrC(ref left, ref right)
| Terminal::OrI(ref left, ref right) => {
Semantic::Or(vec![left.node.lift(), right.node.lift()])
Semantic::Threshold(1, vec![left.node.lift(), right.node.lift()])
}
Terminal::Thresh(k, ref subs) => {
Semantic::Threshold(k, subs.into_iter().map(|s| s.node.lift()).collect())
Expand Down Expand Up @@ -129,9 +132,11 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Concrete<Pk> {
Concrete::Hash256(h) => Semantic::Hash256(h),
Concrete::Ripemd160(h) => Semantic::Ripemd160(h),
Concrete::Hash160(h) => Semantic::Hash160(h),
Concrete::And(ref subs) => Semantic::And(subs.iter().map(Liftable::lift).collect()),
Concrete::And(ref subs) => {
Semantic::Threshold(subs.len(), subs.iter().map(Liftable::lift).collect())
}
Concrete::Or(ref subs) => {
Semantic::Or(subs.iter().map(|&(_, ref sub)| sub.lift()).collect())
Semantic::Threshold(1, subs.iter().map(|&(_, ref sub)| sub.lift()).collect())
}
Concrete::Threshold(k, ref subs) => {
Semantic::Threshold(k, subs.iter().map(Liftable::lift).collect())
Expand All @@ -158,7 +163,7 @@ mod tests {

fn semantic_policy_rtt(s: &str) {
let sem = SemanticPol::from_str(s).unwrap();
let output = sem.to_string();
let output = sem.normalized().to_string();
assert_eq!(s.to_lowercase(), output.to_lowercase());
}

Expand All @@ -171,9 +176,11 @@ mod tests {

semantic_policy_rtt("pkh()");
semantic_policy_rtt("or(pkh(),pkh())");
semantic_policy_rtt("and(pkh(),pkh())");

//fuzzer crashes
assert!(ConcretePol::from_str("thresh()").is_err());
assert!(SemanticPol::from_str("thresh(0)").is_err());
assert!(SemanticPol::from_str("thresh()").is_err());
concrete_policy_rtt("ripemd160(aaaaaaaaaaaaaaaaaaaaaa0Daaaaaaaaaabaaaaa)");
}
Expand Down
Loading

0 comments on commit 0c69589

Please sign in to comment.