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
//! # Dlc-trie
//! Package for storing and retrieving DLC data using tries.

#![crate_name = "dlc_trie"]
// Coding conventions
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]

extern crate bitcoin;
extern crate dlc;
#[cfg(feature = "parallel")]
extern crate rayon;
extern crate secp256k1_zkp;
#[cfg(feature = "use-serde")]
extern crate serde;

use bitcoin::{Script, Transaction};
use dlc::{Error, RangePayout};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use secp256k1_zkp::{All, EcdsaAdaptorSignature, PublicKey, Secp256k1, SecretKey};
#[cfg(feature = "use-serde")]
use serde::{Deserialize, Serialize};

pub mod combination_iterator;
pub mod digit_decomposition;
pub mod digit_trie;
pub mod multi_oracle;
pub mod multi_oracle_trie;
pub mod multi_oracle_trie_with_diff;
pub mod multi_trie;
#[cfg(test)]
mod test_utils;
mod utils;

pub(crate) type IndexedPath = (usize, Vec<usize>);

/// Structure containing a reference to a looked-up value and the
/// path at which it was found.
#[derive(Debug, Clone)]
pub struct LookupResult<'a, TValue, TPath> {
    /// The path at which the `value` was found.
    pub path: Vec<TPath>,
    /// The value that was returned.
    pub value: &'a TValue,
}

/// Enum representing the different type of nodes in a tree
#[derive(Debug, Clone)]
pub enum Node<TLeaf, TNode> {
    /// None is only used as a placeholder when taking mutable ownership of a
    /// node during insertion.
    None,
    /// A leaf is a node in the tree that does not have any children.
    Leaf(TLeaf),
    /// A node is parent to at least one other node in a tree.
    Node(TNode),
}

#[derive(Eq, PartialEq, Debug, Clone)]
/// Structure that stores the indexes at which the CET and adaptor signature
/// related to a given outcome are located in CET and adaptor signatures arrays
/// respectively.
pub struct RangeInfo {
    /// a cet index
    pub cet_index: usize,
    /// an adaptor signature index
    pub adaptor_index: usize,
}

#[derive(Clone, Debug)]
#[cfg_attr(
    feature = "use-serde",
    derive(Serialize, Deserialize),
    serde(rename_all = "camelCase")
)]
/// Information about the base and number of digits used by the oracle.
pub struct OracleNumericInfo {
    /// The base in which the oracle will represent the outcome value.
    pub base: usize,
    /// The number of digits that each oracle will use to represent the outcome value.
    pub nb_digits: Vec<usize>,
}

impl OracleNumericInfo {
    /// Return the minimum number of digits supported by an oracle in the group.
    pub fn get_min_nb_digits(&self) -> usize {
        *self.nb_digits.iter().min().unwrap()
    }

    /// Returns whether oracles have varying number of digits.
    pub fn has_diff_nb_digits(&self) -> bool {
        self.nb_digits
            .iter()
            .skip(1)
            .any(|x| *x != self.nb_digits[0])
    }
}

/// A common trait for trie data structures that store DLC adaptor signature
/// information.
pub trait DlcTrie<'a, TrieIterator: Iterator<Item = TrieIterInfo>> {
    /// Generate the trie using the provided outcomes and oracle information,
    /// calling the provided callback with the CET index and adaptor point for
    /// each adaptor signature.
    fn generate(
        &'a mut self,
        adaptor_index_start: usize,
        outcomes: &[RangePayout],
    ) -> Result<Vec<TrieIterInfo>, Error>;

    /// Returns an iterator to this trie.
    fn iter(&'a self) -> TrieIterator;

    /// Generate the trie while verifying the provided adaptor signatures.
    fn generate_verify(
        &'a mut self,
        secp: &Secp256k1<secp256k1_zkp::All>,
        fund_pubkey: &PublicKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        outcomes: &[RangePayout],
        cets: &[Transaction],
        precomputed_points: &[Vec<Vec<PublicKey>>],
        adaptor_sigs: &[EcdsaAdaptorSignature],
        adaptor_index_start: usize,
    ) -> Result<usize, Error> {
        let trie_info = self.generate(adaptor_index_start, outcomes)?;
        verify_helper(
            secp,
            cets,
            adaptor_sigs,
            fund_pubkey,
            funding_script_pubkey,
            fund_output_value,
            precomputed_points,
            trie_info.into_iter(),
        )
    }

    /// Generate the trie while creating the set of adaptor signatures.
    fn generate_sign(
        &'a mut self,
        secp: &Secp256k1<All>,
        fund_privkey: &SecretKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        outcomes: &[RangePayout],
        cets: &[Transaction],
        precomputed_points: &[Vec<Vec<PublicKey>>],
        adaptor_index_start: usize,
    ) -> Result<Vec<EcdsaAdaptorSignature>, Error> {
        let trie_info = self.generate(adaptor_index_start, outcomes)?;
        sign_helper(
            secp,
            cets,
            fund_privkey,
            funding_script_pubkey,
            fund_output_value,
            precomputed_points,
            trie_info.into_iter(),
        )
    }

    /// Verify that the provided signatures are valid with respect to the
    /// information stored in the trie.
    fn verify(
        &'a self,
        secp: &Secp256k1<All>,
        fund_pubkey: &PublicKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        adaptor_sigs: &[EcdsaAdaptorSignature],
        cets: &[Transaction],
        precomputed_points: &[Vec<Vec<PublicKey>>],
    ) -> Result<usize, Error> {
        verify_helper(
            secp,
            cets,
            adaptor_sigs,
            fund_pubkey,
            funding_script_pubkey,
            fund_output_value,
            precomputed_points,
            self.iter(),
        )
    }

    /// Produce the set of adaptor signatures for the trie.
    fn sign(
        &'a self,
        secp: &Secp256k1<All>,
        fund_privkey: &SecretKey,
        funding_script_pubkey: &Script,
        fund_output_value: u64,
        cets: &[Transaction],
        precomputed_points: &[Vec<Vec<PublicKey>>],
    ) -> Result<Vec<EcdsaAdaptorSignature>, Error> {
        let trie_info = self.iter();
        sign_helper(
            secp,
            cets,
            fund_privkey,
            funding_script_pubkey,
            fund_output_value,
            precomputed_points,
            trie_info,
        )
    }
}

#[derive(Debug)]
/// Holds information provided when iterating a DlcTrie.
pub struct TrieIterInfo {
    indexes: Vec<usize>,
    paths: Vec<Vec<usize>>,
    value: RangeInfo,
}

#[cfg(not(feature = "parallel"))]
fn sign_helper<T: Iterator<Item = TrieIterInfo>>(
    secp: &Secp256k1<All>,
    cets: &[Transaction],
    fund_privkey: &SecretKey,
    funding_script_pubkey: &Script,
    fund_output_value: u64,
    precomputed_points: &[Vec<Vec<PublicKey>>],
    trie_info: T,
) -> Result<Vec<EcdsaAdaptorSignature>, Error> {
    let mut unsorted = trie_info
        .map(|x| {
            let adaptor_point = utils::get_adaptor_point_for_indexed_paths(
                &x.indexes,
                &x.paths,
                precomputed_points,
            )?;
            let adaptor_sig = dlc::create_cet_adaptor_sig_from_point(
                secp,
                &cets[x.value.cet_index],
                &adaptor_point,
                fund_privkey,
                funding_script_pubkey,
                fund_output_value,
            )?;
            Ok((x.value.adaptor_index, adaptor_sig))
        })
        .collect::<Result<Vec<(usize, EcdsaAdaptorSignature)>, Error>>()?;
    unsorted.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
    Ok(unsorted.into_iter().map(|(_, y)| y).collect())
}

#[cfg(feature = "parallel")]
fn sign_helper<T: Iterator<Item = TrieIterInfo>>(
    secp: &Secp256k1<All>,
    cets: &[Transaction],
    fund_privkey: &SecretKey,
    funding_script_pubkey: &Script,
    fund_output_value: u64,
    precomputed_points: &[Vec<Vec<PublicKey>>],
    trie_info: T,
) -> Result<Vec<EcdsaAdaptorSignature>, Error> {
    let trie_info: Vec<TrieIterInfo> = trie_info.collect();
    let mut unsorted = trie_info
        .par_iter()
        .map(|x| {
            let adaptor_point = utils::get_adaptor_point_for_indexed_paths(
                &x.indexes,
                &x.paths,
                precomputed_points,
            )?;
            let adaptor_sig = dlc::create_cet_adaptor_sig_from_point(
                secp,
                &cets[x.value.cet_index],
                &adaptor_point,
                fund_privkey,
                funding_script_pubkey,
                fund_output_value,
            )?;
            Ok((x.value.adaptor_index, adaptor_sig))
        })
        .collect::<Result<Vec<(usize, EcdsaAdaptorSignature)>, Error>>()?;
    unsorted.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
    Ok(unsorted.into_iter().map(|(_, y)| y).collect())
}

#[cfg(not(feature = "parallel"))]
fn verify_helper<T: Iterator<Item = TrieIterInfo>>(
    secp: &Secp256k1<All>,
    cets: &[Transaction],
    adaptor_sigs: &[EcdsaAdaptorSignature],
    fund_pubkey: &PublicKey,
    funding_script_pubkey: &Script,
    fund_output_value: u64,
    precomputed_points: &[Vec<Vec<PublicKey>>],
    trie_info: T,
) -> Result<usize, Error> {
    let mut max_adaptor_index = 0;
    for x in trie_info {
        let adaptor_point =
            utils::get_adaptor_point_for_indexed_paths(&x.indexes, &x.paths, precomputed_points)?;
        let adaptor_sig = adaptor_sigs[x.value.adaptor_index];
        let cet = &cets[x.value.cet_index];
        if x.value.adaptor_index > max_adaptor_index {
            max_adaptor_index = x.value.adaptor_index;
        }
        dlc::verify_cet_adaptor_sig_from_point(
            secp,
            &adaptor_sig,
            cet,
            &adaptor_point,
            fund_pubkey,
            funding_script_pubkey,
            fund_output_value,
        )?;
    }
    Ok(max_adaptor_index + 1)
}

#[cfg(feature = "parallel")]
fn verify_helper<T: Iterator<Item = TrieIterInfo>>(
    secp: &Secp256k1<All>,
    cets: &[Transaction],
    adaptor_sigs: &[EcdsaAdaptorSignature],
    fund_pubkey: &PublicKey,
    funding_script_pubkey: &Script,
    fund_output_value: u64,
    precomputed_points: &[Vec<Vec<PublicKey>>],
    trie_info: T,
) -> Result<usize, Error> {
    let trie_info: Vec<TrieIterInfo> = trie_info.collect();
    let max_adaptor_index = trie_info
        .iter()
        .max_by(|x, y| x.value.adaptor_index.cmp(&y.value.adaptor_index))
        .unwrap();
    trie_info.par_iter().try_for_each(|x| {
        let adaptor_point =
            utils::get_adaptor_point_for_indexed_paths(&x.indexes, &x.paths, precomputed_points)?;
        let adaptor_sig = adaptor_sigs[x.value.adaptor_index];
        let cet = &cets[x.value.cet_index];
        dlc::verify_cet_adaptor_sig_from_point(
            secp,
            &adaptor_sig,
            cet,
            &adaptor_point,
            fund_pubkey,
            funding_script_pubkey,
            fund_output_value,
        )
    })?;

    Ok(max_adaptor_index.value.adaptor_index + 1)
}