Skip to content

Commit 3250e40

Browse files
committed
miniscript: take pubkey size into account in multi() type extdata
As in the previous commit, we were previously assuming that all pubkeys were compressed. This isn't so. We have the keys, we can query them for whether they are uncompressed or not.
1 parent 8874640 commit 3250e40

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/miniscript/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ mod private {
275275
pub fn multi(thresh: crate::Threshold<Pk, MAX_PUBKEYS_PER_MULTISIG>) -> Self {
276276
Self {
277277
ty: types::Type::multi(),
278-
ext: types::extra_props::ExtData::multi(thresh.k(), thresh.n()),
278+
ext: types::extra_props::ExtData::multi(&thresh),
279279
node: Terminal::Multi(thresh),
280280
phantom: PhantomData,
281281
}

src/miniscript/types/extra_props.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::cmp;
77
use core::iter::once;
88

99
use super::ScriptContext;
10+
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
1011
use crate::prelude::*;
1112
use crate::{script_num_size, AbsLockTime, MiniscriptKey, RelLockTime, Terminal};
1213

@@ -242,15 +243,23 @@ impl ExtData {
242243
}
243244

244245
/// Extra properties for the `multi` fragment.
245-
pub fn multi(k: usize, n: usize) -> Self {
246+
pub fn multi<Pk: MiniscriptKey>(
247+
thresh: &crate::Threshold<Pk, MAX_PUBKEYS_PER_MULTISIG>,
248+
) -> Self {
249+
let (n, k) = (thresh.n(), thresh.k());
246250
let num_cost = match (k > 16, n > 16) {
247251
(true, true) => 4,
248252
(false, true) => 3,
249253
(true, false) => 3,
250254
(false, false) => 2,
251255
};
252256
ExtData {
253-
pk_cost: num_cost + 34 * n + 1,
257+
pk_cost: num_cost
258+
+ thresh
259+
.iter()
260+
.map(|k| if k.is_uncompressed() { 65 } else { 34 })
261+
.sum::<usize>()
262+
+ 1,
254263
has_free_verify: true,
255264
// Multi is the only case because of which we need to count additional
256265
// executed opcodes.
@@ -940,7 +949,7 @@ impl ExtData {
940949
Terminal::PkK(ref k) => Self::pk_k::<_, Ctx>(k),
941950
Terminal::PkH(ref k) => Self::pk_h::<_, Ctx>(Some(k)),
942951
Terminal::RawPkH(..) => Self::pk_h::<Pk, Ctx>(None),
943-
Terminal::Multi(ref thresh) => Self::multi(thresh.k(), thresh.n()),
952+
Terminal::Multi(ref thresh) => Self::multi(thresh),
944953
Terminal::MultiA(ref thresh) => Self::multi_a(thresh.k(), thresh.n()),
945954
Terminal::After(t) => Self::after(t),
946955
Terminal::Older(t) => Self::older(t),

0 commit comments

Comments
 (0)