Skip to content

Commit

Permalink
Replace Pk with KeyExpr<Pk>
Browse files Browse the repository at this point in the history
  • Loading branch information
kanishk779 committed Jul 21, 2022
1 parent 102efbd commit c03e348
Show file tree
Hide file tree
Showing 17 changed files with 402 additions and 107 deletions.
5 changes: 3 additions & 2 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::expression::{self, FromTree};
use crate::miniscript::context::ScriptContext;
use crate::policy::{semantic, Liftable};
use crate::prelude::*;
use crate::miniscript::musig_key::KeyExpr;
use crate::util::{varint_len, witness_to_scriptsig};
use crate::{
BareCtx, Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey, TranslatePk,
Expand Down Expand Up @@ -192,7 +193,7 @@ where
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Pkh<Pk: MiniscriptKey> {
/// underlying publickey
pk: Pk,
pk: Pk
}

impl<Pk: MiniscriptKey> Pkh<Pk> {
Expand All @@ -219,7 +220,7 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
/// sighash suffix. Includes the weight of the VarInts encoding the
/// scriptSig and witness stack length.
pub fn max_satisfaction_weight(&self) -> usize {
4 * (1 + 73 + BareCtx::pk_len(&self.pk))
4 * (1 + 73 + BareCtx::pk_len(&KeyExpr::SingleKey(self.pk)))
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use sync::Arc;

use self::checksum::verify_checksum;
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
use crate::miniscript::musig_key::KeyExpr;
use crate::prelude::*;
use crate::{
expression, hash256, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator,
Expand Down Expand Up @@ -180,7 +181,7 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
// roundabout way to constuct `c:pk_k(pk)`
let ms: Miniscript<Pk, BareCtx> =
Miniscript::from_ast(miniscript::decode::Terminal::Check(Arc::new(
Miniscript::from_ast(miniscript::decode::Terminal::PkK(pk))
Miniscript::from_ast(miniscript::decode::Terminal::PkK(KeyExpr::SingleKey(pk)))
.expect("Type check cannot fail"),
)))
.expect("Type check cannot fail");
Expand Down
3 changes: 2 additions & 1 deletion src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::expression::{self, FromTree};
use crate::miniscript::context::{ScriptContext, ScriptContextError};
use crate::policy::{semantic, Liftable};
use crate::prelude::*;
use crate::miniscript::musig_key::KeyExpr;
use crate::util::varint_len;
use crate::{
Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey, TranslatePk,
Expand Down Expand Up @@ -330,7 +331,7 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
/// sighash suffix. Includes the weight of the VarInts encoding the
/// scriptSig and witness stack length.
pub fn max_satisfaction_weight(&self) -> usize {
4 + 1 + 73 + Segwitv0::pk_len(&self.pk)
4 + 1 + 73 + Segwitv0::pk_len(&KeyExpr::SingleKey(self.pk))
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/descriptor/sortedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use bitcoin::blockdata::script;

use crate::miniscript::context::ScriptContext;
use crate::miniscript::decode::Terminal;
use crate::miniscript::musig_key::KeyExpr;
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
use crate::prelude::*;
use crate::{
Expand Down Expand Up @@ -183,7 +184,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
script_num_size(self.k)
+ 1
+ script_num_size(self.pks.len())
+ self.pks.iter().map(|pk| Ctx::pk_len(pk)).sum::<usize>()
+ self.pks.iter().map(|pk| Ctx::pk_len(&KeyExpr::SingleKey(*pk))).sum::<usize>()
}

/// Maximum number of witness elements used to satisfy the Miniscript
Expand Down
13 changes: 9 additions & 4 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ where
Terminal::PkK(ref pk) => {
debug_assert_eq!(node_state.n_evaluated, 0);
debug_assert_eq!(node_state.n_satisfied, 0);
let res = self.stack.evaluate_pk(&mut self.verify_sig, *pk);
let res = match pk.single_key() {
Some(pk) => self.stack.evaluate_pk(&mut self.verify_sig, *pk),
None => None
};
if res.is_some() {
return res;
}
Expand Down Expand Up @@ -868,9 +871,11 @@ where
// evaluate each key with as a pk
// note that evaluate_pk will error on non-empty incorrect sigs
// push 1 on satisfied sigs and push 0 on empty sigs
match self
.stack
.evaluate_pk(&mut self.verify_sig, subs[node_state.n_evaluated])
let temp = match subs[node_state.n_evaluated].single_key() {
Some(pkk) => self.stack.evaluate_pk(&mut self.verify_sig, *pkk),
None => None,
};
match temp
{
Some(Ok(x)) => {
self.push_evaluation_state(
Expand Down
31 changes: 18 additions & 13 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use sync::Arc;
use crate::miniscript::context::SigType;
use crate::miniscript::types::{self, Property};
use crate::miniscript::ScriptContext;
use crate::miniscript::musig_key::KeyExpr;
use crate::prelude::*;
use crate::util::MsKeyBuilder;
use crate::{
Expand Down Expand Up @@ -80,7 +81,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
Pk::RawPkHash: 'a,
{
match *self {
Terminal::PkK(ref p) => pred(p),
Terminal::PkK(ref p) => p.for_each_key(pred),
Terminal::PkH(ref p) => pred(p),
Terminal::RawPkH(..)
| Terminal::After(..)
Expand Down Expand Up @@ -112,8 +113,9 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
&& c.real_for_each_key(pred)
}
Terminal::Thresh(_, ref subs) => subs.iter().all(|sub| sub.real_for_each_key(pred)),
Terminal::Multi(_, ref keys) | Terminal::MultiA(_, ref keys) => {
keys.iter().all(|key| pred(key))
Terminal::Multi(_, ref keys) => keys.iter().all(|key| pred(key)),
Terminal::MultiA(_, ref keys) => {
keys.iter().all(|key| key.for_each_key(pred))
}
}
}
Expand All @@ -125,7 +127,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
T: Translator<Pk, Q, E>,
{
let frag: Terminal<Q, CtxQ> = match *self {
Terminal::PkK(ref p) => Terminal::PkK(t.pk(p)?),
Terminal::PkK(ref p) => Terminal::PkK(p.translate_pk(t)?),
Terminal::PkH(ref p) => Terminal::PkH(t.pk(p)?),
Terminal::RawPkH(ref p) => Terminal::RawPkH(t.pkh(p)?),
Terminal::After(n) => Terminal::After(n),
Expand Down Expand Up @@ -186,7 +188,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
Terminal::Multi(k, keys?)
}
Terminal::MultiA(k, ref keys) => {
let keys: Result<Vec<Q>, _> = keys.iter().map(|k| t.pk(k)).collect();
let keys: Result<Vec<KeyExpr<Q>>, _> = keys.iter().map(|k| k.translate_pk(t)).collect();
Terminal::MultiA(k, keys?)
}
};
Expand Down Expand Up @@ -455,7 +457,7 @@ impl_from_tree!(
}
let mut unwrapped = match (frag_name, top.args.len()) {
("pk_k", 1) => {
expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkK))
expression::unary(top, Terminal::PkK)
}
("pk_h", 1) => expression::terminal(&top.args[0], |x| Pk::from_str(x).map(Terminal::PkH)),
("after", 1) => expression::terminal(&top.args[0], |x| {
Expand Down Expand Up @@ -522,15 +524,18 @@ impl_from_tree!(
return Err(errstr("higher threshold than there were keys in multi"));
}

let pks: Result<Vec<Pk>, _> = top.args[1..]
.iter()
.map(|sub| expression::terminal(sub, Pk::from_str))
.collect();

if frag_name == "multi" {
let pks: Result<Vec<Pk>, _> = top.args[1..]
.iter()
.map(|sub| expression::terminal(sub, Pk::from_str))
.collect();
pks.map(|pks| Terminal::Multi(k, pks))
} else {
// must be multi_a
let pks: Result<Vec<KeyExpr<Pk>>, _> = top.args[1..]
.iter()
.map(|sub| expression::terminal(sub, KeyExpr::<Pk>::from_str))
.collect();
pks.map(|pks| Terminal::MultiA(k, pks))
}
}
Expand Down Expand Up @@ -734,7 +739,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
builder = builder.push_ms_key::<_, Ctx>(&keys[0]);
builder = builder.push_opcode(opcodes::all::OP_CHECKSIG);
for pk in keys.iter().skip(1) {
builder = builder.push_ms_key::<_, Ctx>(pk);
builder = builder.push_ms_key::<_, Ctx>(&pk);
builder = builder.push_opcode(opcodes::all::OP_CHECKSIGADD);
}
builder
Expand Down Expand Up @@ -793,7 +798,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
script_num_size(k)
+ 1
+ script_num_size(pks.len())
+ pks.iter().map(|pk| Ctx::pk_len(pk)).sum::<usize>()
+ pks.iter().map(|pk| Ctx::pk_len(&KeyExpr::SingleKey(*pk))).sum::<usize>()
}
Terminal::MultiA(k, ref pks) => {
script_num_size(k)
Expand Down
Loading

0 comments on commit c03e348

Please sign in to comment.