Skip to content

Commit 859534b

Browse files
committed
Merge #431: Refactor PkH fragment to support Pk
8d0cf06 Remove `ForEach` Trait (Aman Rojjha) 5cb2bcf Refactor PkH to include key. (Aman Rojjha) 09299b0 Refactor ForEach to contain only Pk. (Aman Rojjha) 701c9cc Remove un-necessary lifetimes (Aman Rojjha) ca16bd8 Fix formatter configuration errors with latest nightly (sanket1729) Pull request description: As suggested in #427, this refactor helps to incorporate musig in the codebase. ACKs for top commit: apoelstra: ACK 8d0cf06 sanket1729: ACK 8d0cf06. The last commit message should be `Remove ForEach struct` instead of `Remove ForEach trait`. But won't holdoff this over that. Tree-SHA512: 645974d9055e0458f64e214d95a73647420d46f05246be6809ae11dbfd8dacc05500c5e9f9f1b10b532b539b2d30883c014177a33e7666aea8ae383722910ff9
2 parents c80aa7c + 8d0cf06 commit 859534b

24 files changed

+245
-183
lines changed

rustfmt.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ format_code_in_doc_comments = false
1616
comment_width = 80
1717
normalize_comments = false
1818
normalize_doc_attributes = false
19-
license_template_path = ""
2019
format_strings = false
2120
format_macro_matchers = false
2221
format_macro_bodies = true
@@ -53,7 +52,7 @@ trailing_comma = "Vertical"
5352
match_block_trailing_comma = false
5453
blank_lines_upper_bound = 1
5554
blank_lines_lower_bound = 0
56-
edition = "2015"
55+
edition = "2018"
5756
version = "One"
5857
inline_attribute_width = 0
5958
format_generated_files = true
@@ -63,15 +62,12 @@ use_field_init_shorthand = false
6362
force_explicit_abi = true
6463
condense_wildcard_suffixes = false
6564
color = "Auto"
66-
required_version = "1.4.38"
6765
unstable_features = false
6866
disable_all_formatting = false
6967
skip_children = false
7068
hide_parse_errors = false
7169
error_on_line_overflow = false
7270
error_on_unformatted = false
73-
report_todo = "Never"
74-
report_fixme = "Never"
7571
ignore = []
7672
emit_mode = "Files"
7773
make_backup = false

src/descriptor/bare.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ use crate::policy::{semantic, Liftable};
3030
use crate::prelude::*;
3131
use crate::util::{varint_len, witness_to_scriptsig};
3232
use crate::{
33-
BareCtx, Error, ForEach, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey,
34-
TranslatePk, Translator,
33+
BareCtx, Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, ToPublicKey, TranslatePk,
34+
Translator,
3535
};
3636

3737
/// Create a Bare Descriptor. That is descriptor that is
@@ -164,7 +164,7 @@ impl_from_str!(
164164
);
165165

166166
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
167-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
167+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
168168
where
169169
Pk: 'a,
170170
Pk::Hash: 'a,
@@ -326,12 +326,12 @@ impl_from_str!(
326326
);
327327

328328
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
329-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
329+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
330330
where
331331
Pk: 'a,
332332
Pk::Hash: 'a,
333333
{
334-
pred(ForEach::Key(&self.pk))
334+
pred(&self.pk)
335335
}
336336
}
337337

src/descriptor/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use self::checksum::verify_checksum;
3737
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
3838
use crate::prelude::*;
3939
use crate::{
40-
expression, miniscript, BareCtx, Error, ForEach, ForEachKey, MiniscriptKey, PkTranslator,
41-
Satisfier, ToPublicKey, TranslatePk, Translator,
40+
expression, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator, Satisfier,
41+
ToPublicKey, TranslatePk, Translator,
4242
};
4343

4444
mod bare;
@@ -495,7 +495,7 @@ where
495495
}
496496

497497
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
498-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
498+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
499499
where
500500
Pk: 'a,
501501
Pk::Hash: 'a,
@@ -514,7 +514,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
514514
impl Descriptor<DescriptorPublicKey> {
515515
/// Whether or not the descriptor has any wildcards
516516
pub fn is_deriveable(&self) -> bool {
517-
self.for_any_key(|key| key.as_key().is_deriveable())
517+
self.for_any_key(|key| key.is_deriveable())
518518
}
519519

520520
/// Derives all wildcard keys in the descriptor using the supplied index
@@ -1253,19 +1253,20 @@ mod tests {
12531253
let p2 = "020000000000000000000000000000000000000000000000000000000000000002";
12541254
let p3 = "020000000000000000000000000000000000000000000000000000000000000003";
12551255
let p4 = "020000000000000000000000000000000000000000000000000000000000000004";
1256-
let p5 = "f54a5851e9372b87810a8e60cdd2e7cfd80b6e31";
1256+
let p5 = "03f8551772d66557da28c1de858124f365a8eb30ce6ad79c10e0f4c546d0ab0f82";
12571257
let descriptor = Tr::<PublicKey>::from_str(&format!(
12581258
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})",
12591259
p1, p2, p3, p4, p5
12601260
))
12611261
.unwrap()
12621262
.to_string();
12631263

1264+
// p5.to_pubkeyhash() = 516ca378e588a7ed71336147e2a72848b20aca1a
12641265
assert_eq!(
12651266
descriptor,
12661267
format!(
1267-
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})#fdhmu4fj",
1268-
p1, p2, p3, p4, p5
1268+
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh(516ca378e588a7ed71336147e2a72848b20aca1a))}}}})#xz8ny8ae",
1269+
p1, p2, p3, p4,
12691270
)
12701271
)
12711272
}

src/descriptor/segwitv0.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::policy::{semantic, Liftable};
2828
use crate::prelude::*;
2929
use crate::util::varint_len;
3030
use crate::{
31-
Error, ForEach, ForEachKey, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey,
32-
TranslatePk, Translator,
31+
Error, ForEachKey, Miniscript, MiniscriptKey, Satisfier, Segwitv0, ToPublicKey, TranslatePk,
32+
Translator,
3333
};
3434
/// A Segwitv0 wsh descriptor
3535
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
@@ -246,7 +246,7 @@ impl_from_str!(
246246
);
247247

248248
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
249-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
249+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
250250
where
251251
Pk: 'a,
252252
Pk::Hash: 'a,
@@ -439,12 +439,12 @@ impl_from_str!(
439439
);
440440

441441
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
442-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
442+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
443443
where
444444
Pk: 'a,
445445
Pk::Hash: 'a,
446446
{
447-
pred(ForEach::Key(&self.pk))
447+
pred(&self.pk)
448448
}
449449
}
450450

src/descriptor/sh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use crate::policy::{semantic, Liftable};
3131
use crate::prelude::*;
3232
use crate::util::{varint_len, witness_to_scriptsig};
3333
use crate::{
34-
push_opcode_size, Error, ForEach, ForEachKey, Legacy, Miniscript, MiniscriptKey, Satisfier,
35-
Segwitv0, ToPublicKey, TranslatePk, Translator,
34+
push_opcode_size, Error, ForEachKey, Legacy, Miniscript, MiniscriptKey, Satisfier, Segwitv0,
35+
ToPublicKey, TranslatePk, Translator,
3636
};
3737

3838
/// A Legacy p2sh Descriptor
@@ -377,7 +377,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
377377
}
378378

379379
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {
380-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, pred: F) -> bool
380+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
381381
where
382382
Pk: 'a,
383383
Pk::Hash: 'a,

src/descriptor/sortedmulti.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::miniscript::decode::Terminal;
2727
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
2828
use crate::prelude::*;
2929
use crate::{
30-
errstr, expression, miniscript, policy, script_num_size, Error, ForEach, ForEachKey,
31-
Miniscript, MiniscriptKey, Satisfier, ToPublicKey, Translator,
30+
errstr, expression, miniscript, policy, script_num_size, Error, ForEachKey, Miniscript,
31+
MiniscriptKey, Satisfier, ToPublicKey, Translator,
3232
};
3333

3434
/// Contents of a "sortedmulti" descriptor
@@ -112,12 +112,12 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
112112
}
113113

114114
impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk, Ctx> {
115-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
115+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
116116
where
117117
Pk: 'a,
118118
Pk::Hash: 'a,
119119
{
120-
self.pks.iter().all(|key| pred(ForEach::Key(key)))
120+
self.pks.iter().all(|key| pred(key))
121121
}
122122
}
123123

src/descriptor/tr.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use crate::policy::Liftable;
1919
use crate::prelude::*;
2020
use crate::util::{varint_len, witness_size};
2121
use crate::{
22-
errstr, Error, ForEach, ForEachKey, MiniscriptKey, Satisfier, Tap, ToPublicKey, TranslatePk,
23-
Translator,
22+
errstr, Error, ForEachKey, MiniscriptKey, Satisfier, Tap, ToPublicKey, TranslatePk, Translator,
2423
};
2524

2625
/// A Taproot Tree representation.
@@ -579,15 +578,15 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
579578
}
580579

581580
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
582-
fn for_each_key<'a, F: FnMut(ForEach<'a, Pk>) -> bool>(&'a self, mut pred: F) -> bool
581+
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
583582
where
584583
Pk: 'a,
585584
Pk::Hash: 'a,
586585
{
587586
let script_keys_res = self
588587
.iter_scripts()
589588
.all(|(_d, ms)| ms.for_each_key(&mut pred));
590-
script_keys_res && pred(ForEach::Key(&self.internal_key))
589+
script_keys_res && pred(&self.internal_key)
591590
}
592591
}
593592

@@ -702,9 +701,6 @@ mod tests {
702701
let desc = desc.replace(&[' ', '\n'][..], "");
703702
let tr = Tr::<String>::from_str(&desc).unwrap();
704703
// Note the last ac12 only has ac and fails the predicate
705-
assert!(!tr.for_each_key(|k| match k {
706-
ForEach::Key(k) => k.starts_with("acc"),
707-
ForEach::Hash(_h) => unreachable!(),
708-
}));
704+
assert!(!tr.for_each_key(|k| k.starts_with("acc")));
709705
}
710706
}

src/interpreter/mod.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -590,15 +590,25 @@ where
590590
Terminal::PkK(ref pk) => {
591591
debug_assert_eq!(node_state.n_evaluated, 0);
592592
debug_assert_eq!(node_state.n_satisfied, 0);
593-
let res = self.stack.evaluate_pk(&mut self.verify_sig, pk);
593+
let res = self.stack.evaluate_pk(&mut self.verify_sig, *pk);
594594
if res.is_some() {
595595
return res;
596596
}
597597
}
598-
Terminal::PkH(ref pkh) => {
598+
Terminal::PkH(ref pk) => {
599599
debug_assert_eq!(node_state.n_evaluated, 0);
600600
debug_assert_eq!(node_state.n_satisfied, 0);
601-
let res = self.stack.evaluate_pkh(&mut self.verify_sig, pkh);
601+
let res = self
602+
.stack
603+
.evaluate_pkh(&mut self.verify_sig, pk.to_pubkeyhash());
604+
if res.is_some() {
605+
return res;
606+
}
607+
}
608+
Terminal::RawPkH(ref pkh) => {
609+
debug_assert_eq!(node_state.n_evaluated, 0);
610+
debug_assert_eq!(node_state.n_satisfied, 0);
611+
let res = self.stack.evaluate_pkh(&mut self.verify_sig, *pkh);
602612
if res.is_some() {
603613
return res;
604614
}
@@ -857,7 +867,7 @@ where
857867
// push 1 on satisfied sigs and push 0 on empty sigs
858868
match self
859869
.stack
860-
.evaluate_pk(&mut self.verify_sig, &subs[node_state.n_evaluated])
870+
.evaluate_pk(&mut self.verify_sig, subs[node_state.n_evaluated])
861871
{
862872
Some(Ok(x)) => {
863873
self.push_evaluation_state(
@@ -1138,7 +1148,7 @@ mod tests {
11381148
}
11391149

11401150
let pk = no_checks_ms(&format!("c:pk_k({})", pks[0]));
1141-
let pkh = no_checks_ms(&format!("c:pk_h({})", pks[1].to_pubkeyhash()));
1151+
let pkh = no_checks_ms(&format!("c:pk_h({})", pks[1]));
11421152
//Time
11431153
let after = no_checks_ms(&format!("after({})", 1000));
11441154
let older = no_checks_ms(&format!("older({})", 1000));
@@ -1270,11 +1280,7 @@ mod tests {
12701280
stack::Element::Push(&pk_bytes),
12711281
stack::Element::Push(&der_sigs[0]),
12721282
]);
1273-
let elem = no_checks_ms(&format!(
1274-
"and_v(vc:pk_k({}),c:pk_h({}))",
1275-
pks[0],
1276-
pks[1].to_pubkeyhash()
1277-
));
1283+
let elem = no_checks_ms(&format!("and_v(vc:pk_k({}),c:pk_h({}))", pks[0], pks[1]));
12781284
let vfyfn = vfyfn_.clone(); // sigh rust 1.29...
12791285
let constraints = from_stack(Box::new(vfyfn), stack, &elem);
12801286

@@ -1325,9 +1331,7 @@ mod tests {
13251331
]);
13261332
let elem = no_checks_ms(&format!(
13271333
"andor(c:pk_k({}),jtv:sha256({}),c:pk_h({}))",
1328-
pks[0],
1329-
sha256_hash,
1330-
pks[1].to_pubkeyhash(),
1334+
pks[0], sha256_hash, pks[1],
13311335
));
13321336
let vfyfn = vfyfn_.clone(); // sigh rust 1.29...
13331337
let constraints = from_stack(Box::new(vfyfn), stack, &elem);

src/interpreter/stack.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<'txin> Stack<'txin> {
134134
pub(super) fn evaluate_pk<'intp>(
135135
&mut self,
136136
verify_sig: &mut Box<dyn FnMut(&KeySigPair) -> bool + 'intp>,
137-
pk: &'intp BitcoinKey,
137+
pk: BitcoinKey,
138138
) -> Option<Result<SatisfiedConstraint, Error>> {
139139
if let Some(sigser) = self.pop() {
140140
match sigser {
@@ -143,7 +143,7 @@ impl<'txin> Stack<'txin> {
143143
None
144144
}
145145
Element::Push(sigser) => {
146-
let key_sig = verify_sersig(verify_sig, pk, sigser);
146+
let key_sig = verify_sersig(verify_sig, &pk, sigser);
147147
match key_sig {
148148
Ok(key_sig) => {
149149
self.push(Element::Satisfied);
@@ -152,9 +152,7 @@ impl<'txin> Stack<'txin> {
152152
Err(e) => Some(Err(e)),
153153
}
154154
}
155-
Element::Satisfied => {
156-
Some(Err(Error::PkEvaluationError(PkEvalErrInner::from(*pk))))
157-
}
155+
Element::Satisfied => Some(Err(Error::PkEvaluationError(PkEvalErrInner::from(pk)))),
158156
}
159157
} else {
160158
Some(Err(Error::UnexpectedStackEnd))
@@ -170,7 +168,7 @@ impl<'txin> Stack<'txin> {
170168
pub(super) fn evaluate_pkh<'intp>(
171169
&mut self,
172170
verify_sig: &mut Box<dyn FnMut(&KeySigPair) -> bool + 'intp>,
173-
pkh: &'intp TypedHash160,
171+
pkh: TypedHash160,
174172
) -> Option<Result<SatisfiedConstraint, Error>> {
175173
// Parse a bitcoin key from witness data slice depending on hash context
176174
// when we encounter a pkh(hash)
@@ -189,7 +187,7 @@ impl<'txin> Stack<'txin> {
189187
if pk_hash != pkh.hash160() {
190188
return Some(Err(Error::PkHashVerifyFail(pkh.hash160())));
191189
}
192-
match bitcoin_key_from_slice(pk, *pkh) {
190+
match bitcoin_key_from_slice(pk, pkh) {
193191
Some(pk) => {
194192
if let Some(sigser) = self.pop() {
195193
match sigser {

0 commit comments

Comments
 (0)