Skip to content

Commit be41d8b

Browse files
committed
miniscript: add constants for 1 and 0
These are fairly common leaf nodes and throughout the codebase we tend to reconstruct and typecheck them. It is faster and clearer to just have constants.
1 parent 270e65d commit be41d8b

File tree

5 files changed

+83
-35
lines changed

5 files changed

+83
-35
lines changed

src/miniscript/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ pub struct Miniscript<Pk: MiniscriptKey, Ctx: ScriptContext> {
6464
}
6565

6666
impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
67+
/// The `1` combinator.
68+
pub const TRUE: Self = Miniscript {
69+
node: Terminal::True,
70+
ty: types::Type::TRUE,
71+
ext: types::extra_props::ExtData::TRUE,
72+
phantom: PhantomData,
73+
};
74+
75+
/// The `0` combinator.
76+
pub const FALSE: Self = Miniscript {
77+
node: Terminal::False,
78+
ty: types::Type::FALSE,
79+
ext: types::extra_props::ExtData::FALSE,
80+
phantom: PhantomData,
81+
};
82+
6783
/// Add type information(Type and Extdata) to Miniscript based on
6884
/// `AstElem` fragment. Dependent on display and clone because of Error
6985
/// Display code of type_check.

src/miniscript/types/correctness.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ pub struct Correctness {
8383
}
8484

8585
impl Correctness {
86+
/// Correctness data for the `1` combinator
87+
pub const TRUE: Self =
88+
Correctness { base: Base::B, input: Input::Zero, dissatisfiable: false, unit: true };
89+
90+
/// Correctness data for the `0` combinator
91+
pub const FALSE: Self =
92+
Correctness { base: Base::B, input: Input::Zero, dissatisfiable: true, unit: true };
93+
8694
/// Check whether the `self` is a subtype of `other` argument .
8795
/// This checks whether the argument `other` has attributes which are present
8896
/// in the given `Type`. This returns `true` on same arguments
@@ -113,13 +121,9 @@ impl Property for Correctness {
113121
}
114122
}
115123

116-
fn from_true() -> Self {
117-
Correctness { base: Base::B, input: Input::Zero, dissatisfiable: false, unit: true }
118-
}
124+
fn from_true() -> Self { Self::TRUE }
119125

120-
fn from_false() -> Self {
121-
Correctness { base: Base::B, input: Input::Zero, dissatisfiable: true, unit: true }
122-
}
126+
fn from_false() -> Self { Self::FALSE }
123127

124128
fn from_pk_k<Ctx: ScriptContext>() -> Self {
125129
Correctness { base: Base::K, input: Input::OneNonZero, dissatisfiable: true, unit: true }

src/miniscript/types/extra_props.rs

+44-29
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct OpLimits {
4242

4343
impl OpLimits {
4444
/// Creates a new instance of [`OpLimits`]
45-
pub fn new(op_static: usize, op_sat: Option<usize>, op_nsat: Option<usize>) -> Self {
45+
pub const fn new(op_static: usize, op_sat: Option<usize>, op_nsat: Option<usize>) -> Self {
4646
OpLimits { count: op_static, sat: op_sat, nsat: op_nsat }
4747
}
4848

@@ -51,6 +51,17 @@ impl OpLimits {
5151
}
5252

5353
impl TimelockInfo {
54+
/// Creates a new `TimelockInfo` with all fields set to false.
55+
pub const fn new() -> Self {
56+
TimelockInfo {
57+
csv_with_height: false,
58+
csv_with_time: false,
59+
cltv_with_height: false,
60+
cltv_with_time: false,
61+
contains_combination: false,
62+
}
63+
}
64+
5465
/// Returns true if the current `TimelockInfo` contains any possible unspendable paths.
5566
pub fn contains_unspendable_path(self) -> bool { self.contains_combination }
5667

@@ -133,6 +144,36 @@ pub struct ExtData {
133144
pub exec_stack_elem_count_dissat: Option<usize>,
134145
}
135146

147+
impl ExtData {
148+
/// Extra data for the `0` combinator
149+
pub const FALSE: Self = ExtData {
150+
pk_cost: 1,
151+
has_free_verify: false,
152+
ops: OpLimits::new(0, None, Some(0)),
153+
stack_elem_count_sat: None,
154+
stack_elem_count_dissat: Some(0),
155+
max_sat_size: None,
156+
max_dissat_size: Some((0, 0)),
157+
timelock_info: TimelockInfo::new(),
158+
exec_stack_elem_count_sat: None,
159+
exec_stack_elem_count_dissat: Some(1),
160+
};
161+
162+
/// Extra data for the `1` combinator
163+
pub const TRUE: Self = ExtData {
164+
pk_cost: 1,
165+
has_free_verify: false,
166+
ops: OpLimits::new(0, Some(0), None),
167+
stack_elem_count_sat: Some(0),
168+
stack_elem_count_dissat: None,
169+
max_sat_size: Some((0, 0)),
170+
max_dissat_size: None,
171+
timelock_info: TimelockInfo::new(),
172+
exec_stack_elem_count_sat: Some(1),
173+
exec_stack_elem_count_dissat: None,
174+
};
175+
}
176+
136177
impl Property for ExtData {
137178
fn sanity_checks(&self) {
138179
debug_assert_eq!(
@@ -145,35 +186,9 @@ impl Property for ExtData {
145186
);
146187
}
147188

148-
fn from_true() -> Self {
149-
ExtData {
150-
pk_cost: 1,
151-
has_free_verify: false,
152-
ops: OpLimits::new(0, Some(0), None),
153-
stack_elem_count_sat: Some(0),
154-
stack_elem_count_dissat: None,
155-
max_sat_size: Some((0, 0)),
156-
max_dissat_size: None,
157-
timelock_info: TimelockInfo::default(),
158-
exec_stack_elem_count_sat: Some(1),
159-
exec_stack_elem_count_dissat: None,
160-
}
161-
}
189+
fn from_true() -> Self { Self::TRUE }
162190

163-
fn from_false() -> Self {
164-
ExtData {
165-
pk_cost: 1,
166-
has_free_verify: false,
167-
ops: OpLimits::new(0, None, Some(0)),
168-
stack_elem_count_sat: None,
169-
stack_elem_count_dissat: Some(0),
170-
max_sat_size: None,
171-
max_dissat_size: Some((0, 0)),
172-
timelock_info: TimelockInfo::default(),
173-
exec_stack_elem_count_sat: None,
174-
exec_stack_elem_count_dissat: Some(1),
175-
}
176-
}
191+
fn from_false() -> Self { Self::FALSE }
177192

178193
fn from_pk_k<Ctx: ScriptContext>() -> Self {
179194
ExtData {

src/miniscript/types/malleability.rs

+7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ pub struct Malleability {
5555
}
5656

5757
impl Malleability {
58+
/// Malleability data for the `1` combinator
59+
pub const TRUE: Self = Malleability { dissat: Dissat::None, safe: false, non_malleable: true };
60+
61+
/// Malleability data for the `0` combinator
62+
pub const FALSE: Self =
63+
Malleability { dissat: Dissat::Unique, safe: true, non_malleable: true };
64+
5865
/// Check whether the `self` is a subtype of `other` argument .
5966
/// This checks whether the argument `other` has attributes which are present
6067
/// in the given `Type`. This returns `true` on same arguments

src/miniscript/types/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ pub struct Type {
216216
}
217217

218218
impl Type {
219+
/// Type of the `0` combinator
220+
pub const TRUE: Self = Type { corr: Correctness::TRUE, mall: Malleability::TRUE };
221+
222+
/// Type of the `0` combinator
223+
pub const FALSE: Self = Type { corr: Correctness::FALSE, mall: Malleability::FALSE };
224+
219225
/// Check whether the `self` is a subtype of `other` argument .
220226
/// This checks whether the argument `other` has attributes which are present
221227
/// in the given `Type`. This returns `true` on same arguments

0 commit comments

Comments
 (0)