1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Miniscript
// Written in 2018 by
//     Andrew Poelstra <apoelstra@wpsoftware.net>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

//! # Satisfaction and Dissatisfaction
//!
//! Traits and implementations to support producing witnesses for Miniscript
//! scriptpubkeys.
//!

use std::{isize, mem};

use bitcoin::blockdata::transaction::SigHashType;
use bitcoin_hashes::sha256;
use secp256k1;

use Error;
use miniscript::astelem::AstElem;
use ToPublicKey;

/// Trait that lets us write `.rb()` to reborrow `Option<&mut T>` objects
trait Reborrow<T> {
    fn rb(&mut self) -> Option<&mut T>;
}
impl<'a, T> Reborrow<T> for Option<&'a mut T>
{
    fn rb(&mut self) -> Option<&mut T> {
        self.as_mut().map(|x| &mut **x)
    }
}


/// Trait describing an AST element which can be satisfied, given maps from the
/// public data to corresponding witness data.
pub trait Satisfiable<P> {
    /// Attempt to produce a witness that satisfies the AST element
    fn satisfy<F, H>(&self, keyfn: Option<&mut F>, hashfn: Option<&mut H>, age: u32)
        -> Result<Vec<Vec<u8>>, Error>
        where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
              H: FnMut(sha256::Hash) -> Option<[u8; 32]>;
}

/// Trait describing an AST element which can be dissatisfied (without failing the
/// whole script). This only applies to `E` and `W`, since the other AST elements
/// are expected to fail the script on error.
pub trait Dissatisfiable<P> {
    /// For AST elements satisfying the "expression" and "wrapped" calling
    /// conventions, produce a dissatisfying witness. For other elements,
    /// panic.
    fn dissatisfy(&self) -> Vec<Vec<u8>>;
}

impl<P: ToPublicKey> Satisfiable<P> for AstElem<P> {
    fn satisfy<F, H>(&self, mut keyfn: Option<&mut F>, mut hashfn: Option<&mut H>, age: u32)
        -> Result<Vec<Vec<u8>>, Error>
        where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
              H: FnMut(sha256::Hash) -> Option<[u8; 32]>
    {
        match *self {
            AstElem::True => Ok(vec![]),
            AstElem::Pk(ref p) |
            AstElem::PkV(ref p) |
            AstElem::PkQ(ref p) |
            AstElem::PkW(ref p) => satisfy_checksig(p, keyfn),
            AstElem::Multi(k, ref keys) |
            AstElem::MultiV(k, ref keys) => satisfy_checkmultisig(k, keys, keyfn),
            AstElem::Time(t) |
            AstElem::TimeV(t) |
            AstElem::TimeF(t) => satisfy_csv(t, age),
            AstElem::TimeE(t) |
            AstElem::TimeW(t) => satisfy_csv(t, age).map(|_| vec![vec![1]]),
            AstElem::Hash(h) |
            AstElem::HashV(h) |
            AstElem::HashW(h) => satisfy_hashequal(h, hashfn),
            AstElem::Wrap(ref sub) => sub.satisfy(keyfn, hashfn, age),
            AstElem::Likely(ref sub) => {
                let mut ret = sub.satisfy(keyfn, hashfn, age)?;
                ret.push(vec![]);
                Ok(ret)
            },
            AstElem::Unlikely(ref sub) => {
                let mut ret = sub.satisfy(keyfn, hashfn, age)?;
                ret.push(vec![1]);
                Ok(ret)
            },
            AstElem::AndCat(ref left, ref right) |
            AstElem::AndBool(ref left, ref right) |
            AstElem::AndCasc(ref left, ref right) => {
                let mut ret = right.satisfy(keyfn.rb(), hashfn.rb(), age)?;
                ret.extend(left.satisfy(keyfn, hashfn, age)?);
                Ok(ret)
            },
            AstElem::OrBool(ref left, ref right) => satisfy_parallel_or(&*left, &*right, keyfn, hashfn, age),
            AstElem::OrCasc(ref left, ref right) |
            AstElem::OrCont(ref left, ref right) => satisfy_cascade_or(&*left, &*right, keyfn, hashfn, age),
            AstElem::OrKey(ref left, ref right) |
            AstElem::OrKeyV(ref left, ref right) |
            AstElem::OrIf(ref left, ref right) |
            AstElem::OrIfV(ref left, ref right) => satisfy_switch_or(&*left, &*right, keyfn, hashfn, age),
            AstElem::OrNotif(ref left, ref right) => satisfy_switch_or(&*right, &*left, keyfn, hashfn, age),
            AstElem::Thresh(k, ref subs) |
            AstElem::ThreshV(k, ref subs) => satisfy_threshold(k, subs, keyfn, hashfn, age),
        }
    }
}

impl<P: ToPublicKey> Dissatisfiable<P> for AstElem<P> {
    fn dissatisfy(&self) -> Vec<Vec<u8>> {
        match *self {
            AstElem::True => unreachable!(),
            AstElem::Pk(..) |
            AstElem::PkW(..) |
            AstElem::TimeW(..) |
            AstElem::HashW(..) => vec![vec![]],
            AstElem::Multi(k, _) => vec![vec![]; k + 1],
            AstElem::PkV(..) |
            AstElem::PkQ(..) |
            AstElem::MultiV(..) |
            AstElem::Time(..) |
            AstElem::TimeV(..) |
            AstElem::TimeF(..) |
            AstElem::TimeE(..) |
            AstElem::Hash(..) |
            AstElem::HashV(..) => unreachable!(),
            AstElem::Wrap(ref sub) => sub.dissatisfy(),
            AstElem::Likely(..) => vec![vec![1]],
            AstElem::Unlikely(..) => vec![vec![]],
            AstElem::AndCat(..) => unreachable!(),
            AstElem::AndBool(ref left, ref right) => {
                let mut ret = right.dissatisfy();
                ret.extend(left.dissatisfy());
                ret
            },
            AstElem::AndCasc(ref left, _) => left.dissatisfy(),
            AstElem::OrBool(ref left, ref right) |
            AstElem::OrCasc(ref left, ref right) => {
                let mut ret = right.dissatisfy();
                ret.extend(left.dissatisfy());
                ret
            },
            AstElem::OrCont(..) |
            AstElem::OrKey(..) |
            AstElem::OrIfV(..) |
            AstElem::OrKeyV(..) => unreachable!(),
            AstElem::OrIf(_, ref right) => {
                let mut ret = right.dissatisfy();
                ret.push(vec![]);
                ret
            },
            AstElem::OrNotif(ref left, _) => {
                let mut ret = left.dissatisfy();
                ret.push(vec![]);
                ret
            },
            AstElem::Thresh(_, ref subs) => {
                let mut ret = vec![];
                for sub in subs.iter().rev() {
                    ret.extend(sub.dissatisfy());
                }
                ret
            },
            AstElem::ThreshV(..) => unreachable!(),
        }
    }
}

// Helper functions to produce satisfactions for the various AST element types,
// e.g. cascade OR, parallel AND, etc., which typically do not depend on the
// specific choice of E/W/F/V/T that is chosen.

/// Computes witness size, assuming individual pushes are less than 254 bytes
fn satisfy_cost(s: &[Vec<u8>]) -> usize {
    s.iter().map(|s| 1 + s.len()).sum()
}

/// Helper function that produces a checksig(verify) satisfaction
fn satisfy_checksig<P, F>(pk: &P, keyfn: Option<&mut F>) -> Result<Vec<Vec<u8>>, Error>
    where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
          P: ToPublicKey,
{
    let ret = keyfn
        .and_then(|keyfn| keyfn(pk))
        .map(|(sig, hashtype)| {
            let mut ret = sig.serialize_der();
            ret.push(hashtype.as_u32() as u8);
            vec![ret]
        });
        
    match ret {
        Some(ret) => Ok(ret),
        None => Err(Error::MissingSig(pk.to_public_key())),
    }
}

/// Helper function that produces a checkmultisig(verify) satisfaction
fn satisfy_checkmultisig<P, F>(k: usize, keys: &[P], mut keyfn: Option<&mut F>) -> Result<Vec<Vec<u8>>, Error>
    where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
          P: ToPublicKey,
{
    let mut ret = Vec::with_capacity(k + 1);

    ret.push(vec![]);
    for pk in keys {
        if let Ok(mut sig_vec) = satisfy_checksig(pk, keyfn.rb()) {
            ret.push(sig_vec.pop().expect("satisfied checksig has one witness element"));
            if ret.len() > k + 1 {
                let max_idx = ret
                    .iter()
                    .enumerate()
                    .max_by_key(|&(_, ref sig)| sig.len())
                    .unwrap()
                    .0;
                ret.remove(max_idx);
            }
        }
    }

    if ret.len() == k + 1 {
        Ok(ret)
    } else {
        Err(Error::CouldNotSatisfy)
    }
}

fn satisfy_hashequal<H>(hash: sha256::Hash, hashfn: Option<&mut H>) -> Result<Vec<Vec<u8>>, Error>
    where H: FnMut(sha256::Hash) -> Option<[u8; 32]>,
{
    match hashfn.and_then(|hashfn| hashfn(hash)).map(|preimage| vec![preimage[..].to_owned()]) {
        Some(ret) => Ok(ret),
        None => Err(Error::MissingHash(hash)),
    }
}

fn satisfy_csv(n: u32, age: u32) -> Result<Vec<Vec<u8>>, Error> {
    if age >= n {
        Ok(vec![])
    } else {
        Err(Error::LocktimeNotMet(n))
    }
}

fn satisfy_threshold<P, F, H>(
    k: usize,
    subs: &[AstElem<P>],
    mut keyfn: Option<&mut F>,
    mut hashfn: Option<&mut H>,
    age: u32,
    ) -> Result<Vec<Vec<u8>>, Error>
    where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
          H: FnMut(sha256::Hash) -> Option<[u8; 32]>,
          P: ToPublicKey,
{
    fn flatten(v: Vec<Vec<Vec<u8>>>) -> Vec<Vec<u8>> {
        v.into_iter().fold(vec![], |mut acc, x| { acc.extend(x); acc })
    }

    if k == 0 {
        return Ok(vec![]);
    }

    let mut satisfied = 0;
    let mut ret = Vec::with_capacity(subs.len());
    let mut ret_dis = Vec::with_capacity(subs.len());

    for sub in subs.iter().rev() {
        let dissat = sub.dissatisfy();
        if let Ok(sat) = sub.satisfy(keyfn.rb(), hashfn.rb(), age) {
            ret.push(sat);
            satisfied += 1;
        } else {
            ret.push(dissat.clone());
        }
        ret_dis.push(dissat);
    }

    debug_assert_eq!(ret.len(), subs.len());
    debug_assert_eq!(ret_dis.len(), subs.len());

    if satisfied < k {
        return Err(Error::CouldNotSatisfy);
    }
    if satisfied == k {
        return Ok(flatten(ret));
    }

    // If we have more satisfactions than needed, throw away the extras, choosing
    // the ones that would yield the biggest savings.
    let mut indices: Vec<usize> = (0..subs.len()).collect();
    indices.sort_by_key(|i| {
        satisfy_cost(&ret_dis[*i]) as isize - satisfy_cost(&ret[*i]) as isize
    });
    for i in indices.iter().take(satisfied - k) {
        mem::swap(&mut ret[*i], &mut ret_dis[*i]);
    }

    Ok(flatten(ret))
}

fn satisfy_parallel_or<P, F, H>(
    left: &AstElem<P>,
    right: &AstElem<P>,
    mut keyfn: Option<&mut F>,
    mut hashfn: Option<&mut H>,
    age: u32,
    ) -> Result<Vec<Vec<u8>>, Error>
    where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
          H: FnMut(sha256::Hash) -> Option<[u8; 32]>,
          P: ToPublicKey,
{
    match (
        left.satisfy(keyfn.rb(), hashfn.rb(), age),
        right.satisfy(keyfn, hashfn, age),
    ) {
        (Ok(lsat), Err(..)) => {
            let mut rdissat = right.dissatisfy();
            rdissat.extend(lsat);
            Ok(rdissat)
        }
        (Err(..), Ok(mut rsat)) => {
            let ldissat = left.dissatisfy();
            rsat.extend(ldissat);
            Ok(rsat)
        }
        (Err(e), Err(..)) => {
            Err(e)
        }
        (Ok(lsat), Ok(mut rsat)) => {
            let ldissat = left.dissatisfy();
            let mut rdissat = right.dissatisfy();

            if satisfy_cost(&lsat) + satisfy_cost(&rdissat) <= satisfy_cost(&rsat) + satisfy_cost(&ldissat) {
                rdissat.extend(lsat);
                Ok(rdissat)
            } else {
                rsat.extend(ldissat);
                Ok(rsat)
            }
        }
    }
}

fn satisfy_switch_or<P, F, H>(
    left: &AstElem<P>,
    right: &AstElem<P>,
    mut keyfn: Option<&mut F>,
    mut hashfn: Option<&mut H>,
    age: u32,
    ) -> Result<Vec<Vec<u8>>, Error>
    where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
          H: FnMut(sha256::Hash) -> Option<[u8; 32]>,
          P: ToPublicKey,
{
    match (
        left.satisfy(keyfn.rb(), hashfn.rb(), age),
        right.satisfy(keyfn, hashfn, age),
    ) {
        (Err(e), Err(..)) => Err(e),
        (Ok(mut lsat), Err(..)) => {
            lsat.push(vec![1]);
            Ok(lsat)
        }
        (Err(..), Ok(mut rsat)) => {
            rsat.push(vec![]);
            Ok(rsat)
        }
        (Ok(mut lsat), Ok(mut rsat)) => {
            if satisfy_cost(&lsat) + 2 <= satisfy_cost(&rsat) + 1 {
                lsat.push(vec![1]);
                Ok(lsat)
            } else {
                rsat.push(vec![]);
                Ok(rsat)
            }
        }
    }
}

fn satisfy_cascade_or<P, F, H>(
    left: &AstElem<P>,
    right: &AstElem<P>,
    mut keyfn: Option<&mut F>,
    mut hashfn: Option<&mut H>,
    age: u32,
    ) -> Result<Vec<Vec<u8>>, Error>
    where F: FnMut(&P) -> Option<(secp256k1::Signature, SigHashType)>,
          H: FnMut(sha256::Hash) -> Option<[u8; 32]>,
          P: ToPublicKey,
{
    match (
        left.satisfy(keyfn.rb(), hashfn.rb(), age),
        right.satisfy(keyfn, hashfn, age),
    ) {
        (Err(e), Err(..)) => Err(e),
        (Ok(lsat), Err(..)) => Ok(lsat),
        (Err(..), Ok(mut rsat)) => {
            let ldissat = left.dissatisfy();
            rsat.extend(ldissat);
            Ok(rsat)
        }
        (Ok(lsat), Ok(mut rsat)) => {
            let ldissat = left.dissatisfy();

            if satisfy_cost(&lsat) <= satisfy_cost(&rsat) + satisfy_cost(&ldissat) {
                Ok(lsat)
            } else {
                rsat.extend(ldissat);
                Ok(rsat)
            }
        }
    }
}