simplicity/bit_encoding/
decode.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! # Decoding
4//!
5//! Functionality to decode Simplicity programs.
6//! Refer to [`crate::encode`] for information on the encoding.
7
8use crate::dag::{Dag, DagLike, InternalSharing};
9use crate::jet::Jet;
10use crate::merkle::cmr::Cmr;
11use crate::node::{
12    ConstructNode, CoreConstructible, DisconnectConstructible, JetConstructible,
13    WitnessConstructible,
14};
15use crate::types;
16use crate::value::Word;
17use crate::{BitIter, FailEntropy};
18use std::collections::HashSet;
19use std::sync::Arc;
20use std::{cmp, error, fmt};
21
22use super::bititer::{u2, DecodeNaturalError};
23
24type ArcNode<'brand, J> = Arc<ConstructNode<'brand, J>>;
25
26/// Decoding error
27#[non_exhaustive]
28#[derive(Debug)]
29pub enum Error {
30    /// Error closing the bitstream
31    BitIter(crate::BitIterCloseError),
32    /// Both children of a node are hidden
33    BothChildrenHidden,
34    /// Bitstream ended early
35    EndOfStream,
36    /// Hidden node occurred outside of a case combinator
37    HiddenNode,
38    /// Tried to parse a jet but the name wasn't recognized
39    InvalidJet,
40    /// Error decoding a natural number.
41    Natural(DecodeNaturalError),
42    /// Program is not encoded in canonical order
43    NotInCanonicalOrder,
44    /// Program does not have maximal sharing
45    SharingNotMaximal,
46    /// Type-checking error
47    Type(crate::types::Error),
48}
49
50impl From<crate::EarlyEndOfStreamError> for Error {
51    fn from(_: crate::EarlyEndOfStreamError) -> Self {
52        Error::EndOfStream
53    }
54}
55
56impl From<crate::BitIterCloseError> for Error {
57    fn from(e: crate::BitIterCloseError) -> Self {
58        Error::BitIter(e)
59    }
60}
61
62impl From<DecodeNaturalError> for Error {
63    fn from(e: DecodeNaturalError) -> Error {
64        Error::Natural(e)
65    }
66}
67
68impl From<crate::types::Error> for Error {
69    fn from(e: crate::types::Error) -> Error {
70        Error::Type(e)
71    }
72}
73
74impl fmt::Display for Error {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        match self {
77            Error::BitIter(ref e) => fmt::Display::fmt(e, f),
78            Error::BothChildrenHidden => f.write_str("both children of a case node are hidden"),
79            Error::EndOfStream => f.write_str("bitstream ended early"),
80            Error::HiddenNode => write!(f, "hidden node occurred outside of a case combinator"),
81            Error::InvalidJet => write!(f, "unrecognized jet"),
82            Error::Natural(ref e) => e.fmt(f),
83            Error::NotInCanonicalOrder => f.write_str("program not in canonical order"),
84            Error::SharingNotMaximal => f.write_str("Decoded programs must have maximal sharing"),
85            Error::Type(ref e) => fmt::Display::fmt(e, f),
86        }
87    }
88}
89
90impl error::Error for Error {
91    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
92        match *self {
93            Error::BitIter(ref e) => Some(e),
94            Error::BothChildrenHidden => None,
95            Error::EndOfStream => None,
96            Error::HiddenNode => None,
97            Error::InvalidJet => None,
98            Error::Natural(ref e) => Some(e),
99            Error::NotInCanonicalOrder => None,
100            Error::SharingNotMaximal => None,
101            Error::Type(ref e) => Some(e),
102        }
103    }
104}
105
106#[derive(Debug)]
107enum DecodeNode<J: Jet> {
108    Iden,
109    Unit,
110    InjL(usize),
111    InjR(usize),
112    Take(usize),
113    Drop(usize),
114    Comp(usize, usize),
115    Case(usize, usize),
116    Pair(usize, usize),
117    Disconnect1(usize),
118    Disconnect(usize, usize),
119    Witness,
120    Fail(FailEntropy),
121    Hidden(Cmr),
122    Jet(J),
123    Word(Word),
124}
125
126impl<J: Jet> DagLike for (usize, &'_ [DecodeNode<J>]) {
127    type Node = DecodeNode<J>;
128
129    fn data(&self) -> &DecodeNode<J> {
130        &self.1[self.0]
131    }
132
133    fn as_dag_node(&self) -> Dag<Self> {
134        let nodes = &self.1;
135        match self.1[self.0] {
136            DecodeNode::Iden
137            | DecodeNode::Unit
138            | DecodeNode::Fail(..)
139            | DecodeNode::Hidden(..)
140            | DecodeNode::Jet(..)
141            | DecodeNode::Word(..) => Dag::Nullary,
142            DecodeNode::InjL(i)
143            | DecodeNode::InjR(i)
144            | DecodeNode::Take(i)
145            | DecodeNode::Drop(i)
146            | DecodeNode::Disconnect1(i) => Dag::Unary((i, nodes)),
147            DecodeNode::Comp(li, ri)
148            | DecodeNode::Case(li, ri)
149            | DecodeNode::Pair(li, ri)
150            | DecodeNode::Disconnect(li, ri) => Dag::Binary((li, nodes), (ri, nodes)),
151            DecodeNode::Witness => Dag::Nullary,
152        }
153    }
154}
155
156pub fn decode_expression<'brand, I: Iterator<Item = u8>, J: Jet>(
157    ctx: &types::Context<'brand>,
158    bits: &mut BitIter<I>,
159) -> Result<ArcNode<'brand, J>, Error> {
160    enum Converted<'brand, J: Jet> {
161        Node(ArcNode<'brand, J>),
162        Hidden(Cmr),
163    }
164    use Converted::{Hidden, Node};
165    impl<'brand, J: Jet> Converted<'brand, J> {
166        fn get(&self) -> Result<&ArcNode<'brand, J>, Error> {
167            match self {
168                Node(arc) => Ok(arc),
169                Hidden(_) => Err(Error::HiddenNode),
170            }
171        }
172    }
173
174    let len = bits.read_natural::<usize>(None)?;
175    assert_ne!(len, 0, "impossible to encode 0 in Simplicity");
176
177    let mut nodes = Vec::with_capacity(cmp::min(len, 10_000));
178    for _ in 0..len {
179        let new_node = decode_node(bits, nodes.len())?;
180        nodes.push(new_node);
181    }
182
183    // It is a sharing violation for any hidden node to be repeated. Track them in this set.
184    let mut hidden_set = HashSet::<Cmr>::new();
185    // Convert the DecodeNode structure into a CommitNode structure
186    let mut converted = Vec::<Converted<J>>::with_capacity(len);
187    for data in (nodes.len() - 1, &nodes[..]).post_order_iter::<InternalSharing>() {
188        // Check canonical order as we go
189        if data.index != data.node.0 {
190            return Err(Error::NotInCanonicalOrder);
191        }
192
193        let new = match nodes[data.node.0] {
194            DecodeNode::Unit => Node(ArcNode::unit(ctx)),
195            DecodeNode::Iden => Node(ArcNode::iden(ctx)),
196            DecodeNode::InjL(i) => Node(ArcNode::injl(converted[i].get()?)),
197            DecodeNode::InjR(i) => Node(ArcNode::injr(converted[i].get()?)),
198            DecodeNode::Take(i) => Node(ArcNode::take(converted[i].get()?)),
199            DecodeNode::Drop(i) => Node(ArcNode::drop_(converted[i].get()?)),
200            DecodeNode::Comp(i, j) => {
201                Node(ArcNode::comp(converted[i].get()?, converted[j].get()?)?)
202            }
203            DecodeNode::Case(i, j) => {
204                // Case is a special case, since it uniquely is allowed to have hidden
205                // children (but only one!) in which case it becomes an assertion.
206                match (&converted[i], &converted[j]) {
207                    (Node(left), Node(right)) => Node(ArcNode::case(left, right)?),
208                    (Node(left), Hidden(cmr)) => Node(ArcNode::assertl(left, *cmr)?),
209                    (Hidden(cmr), Node(right)) => Node(ArcNode::assertr(*cmr, right)?),
210                    (Hidden(_), Hidden(_)) => return Err(Error::BothChildrenHidden),
211                }
212            }
213            DecodeNode::Pair(i, j) => {
214                Node(ArcNode::pair(converted[i].get()?, converted[j].get()?)?)
215            }
216            DecodeNode::Disconnect1(i) => Node(ArcNode::disconnect(converted[i].get()?, &None)?),
217            DecodeNode::Disconnect(i, j) => Node(ArcNode::disconnect(
218                converted[i].get()?,
219                &Some(Arc::clone(converted[j].get()?)),
220            )?),
221            DecodeNode::Witness => Node(ArcNode::witness(ctx, None)),
222            DecodeNode::Fail(entropy) => Node(ArcNode::fail(ctx, entropy)),
223            DecodeNode::Hidden(cmr) => {
224                if !hidden_set.insert(cmr) {
225                    return Err(Error::SharingNotMaximal);
226                }
227                Hidden(cmr)
228            }
229            DecodeNode::Jet(j) => Node(ArcNode::jet(ctx, j)),
230            DecodeNode::Word(ref w) => Node(ArcNode::const_word(ctx, w.shallow_clone())),
231        };
232        converted.push(new);
233    }
234
235    converted[len - 1].get().map(Arc::clone)
236}
237
238/// Decode a single Simplicity node from bits and
239/// insert it into a hash map at its index for future reference by ancestor nodes.
240fn decode_node<I: Iterator<Item = u8>, J: Jet>(
241    bits: &mut BitIter<I>,
242    index: usize,
243) -> Result<DecodeNode<J>, Error> {
244    // First bit: 1 for jets/words, 0 for normal combinators
245    if bits.read_bit()? {
246        // Second bit: 1 for jets, 0 for words
247        if bits.read_bit()? {
248            J::decode(bits).map(|jet| DecodeNode::Jet(jet))
249        } else {
250            let n = bits.read_natural(Some(32))?;
251            let word = Word::from_bits(bits, n - 1)?;
252            Ok(DecodeNode::Word(word))
253        }
254    } else {
255        // Bits 2 and 3: code
256        match bits.read_u2()? {
257            u2::_0 => {
258                let subcode = bits.read_u2()?;
259                let i_abs = index - bits.read_natural(Some(index))?;
260                let j_abs = index - bits.read_natural(Some(index))?;
261
262                // Bits 4 and 5: subcode
263                match subcode {
264                    u2::_0 => Ok(DecodeNode::Comp(i_abs, j_abs)),
265                    u2::_1 => Ok(DecodeNode::Case(i_abs, j_abs)),
266                    u2::_2 => Ok(DecodeNode::Pair(i_abs, j_abs)),
267                    u2::_3 => Ok(DecodeNode::Disconnect(i_abs, j_abs)),
268                }
269            }
270            u2::_1 => {
271                let subcode = bits.read_u2()?;
272                let i_abs = index - bits.read_natural(Some(index))?;
273                // Bits 4 and 5: subcode
274                match subcode {
275                    u2::_0 => Ok(DecodeNode::InjL(i_abs)),
276                    u2::_1 => Ok(DecodeNode::InjR(i_abs)),
277                    u2::_2 => Ok(DecodeNode::Take(i_abs)),
278                    u2::_3 => Ok(DecodeNode::Drop(i_abs)),
279                }
280            }
281            u2::_2 => {
282                // Bits 4 and 5: subcode
283                match bits.read_u2()? {
284                    u2::_0 => Ok(DecodeNode::Iden),
285                    u2::_1 => Ok(DecodeNode::Unit),
286                    u2::_2 => Ok(DecodeNode::Fail(bits.read_fail_entropy()?)),
287                    u2::_3 => {
288                        let i_abs = index - bits.read_natural(Some(index))?;
289                        Ok(DecodeNode::Disconnect1(i_abs))
290                    }
291                }
292            }
293            u2::_3 => {
294                // Bit 4: subcode
295                if bits.read_bit()? {
296                    Ok(DecodeNode::Witness)
297                } else {
298                    Ok(DecodeNode::Hidden(bits.read_cmr()?))
299                }
300            }
301        }
302    }
303}
304
305#[cfg(test)]
306mod tests {
307    use super::*;
308    use crate::encode;
309    use crate::jet::Core;
310    use crate::node::{CommitNode, RedeemNode};
311    use crate::BitWriter;
312
313    #[test]
314    fn root_unit_to_unit() {
315        // main = jet_eq_32 :: 2^64 -> 2 # 7387d279
316        let justjet = [0x6d, 0xb8, 0x80];
317        // Should be able to decode this as an expression...
318        let mut iter = BitIter::from(&justjet[..]);
319        types::Context::with_context(|ctx| {
320            decode_expression::<_, Core>(&ctx, &mut iter).unwrap();
321        });
322        // ...but NOT as a CommitNode
323        let iter = BitIter::from(&justjet[..]);
324        CommitNode::<Core>::decode(iter).unwrap_err();
325        // ...or as a RedeemNode
326        let iter = BitIter::from(&justjet[..]);
327        RedeemNode::<Core>::decode(iter, BitIter::from(&[][..])).unwrap_err();
328    }
329
330    #[test]
331    fn decode_fixed_natural() {
332        let tries: Vec<(usize, usize, &[u8])> = vec![
333            (1, 1, &[0b00000000]),
334            (2, 3, &[0b10000000]),
335            (3, 3, &[0b10100000]),
336            (4, 6, &[0b11_000000]),
337            (5, 6, &[0b11_000100]),
338            (6, 6, &[0b11_001000]),
339            (7, 6, &[0b11_001100]),
340            (8, 7, &[0b110_10000]),
341            (15, 7, &[0b110_11110]),
342            (16, 11, &[0b11100000, 0b00000000]),
343            // 31
344            (31, 11, &[0b11100001, 0b11100000]),
345            // 32
346            (32, 12, &[0b11100010, 0b00000000]),
347            // 2^15
348            (32768, 23, &[0b11101111, 0b00000000, 0b00000000]),
349            (65535, 23, &[0b11101111, 0b11111111, 0b11111110]),
350            (65536, 28, &[0b11110000, 0b00000000, 0b00000000, 0b00000000]),
351            (
352                u32::MAX as usize - 1, // cast ok, in unit test
353                43,
354                &[
355                    0b11110000, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11000000,
356                ],
357            ),
358            (
359                u32::MAX as usize, // cast ok, in unit test
360                43,
361                &[
362                    0b11110000, 0b11111111, 0b11111111, 0b11111111, 0b11111111, 0b11100000,
363                ],
364            ),
365        ];
366
367        for (natural, len, bitvec) in tries {
368            let mut iter = BitIter::new(bitvec.iter().copied());
369
370            // Truncating the iterator causes a clean failure.
371            let mut truncated = BitIter::new(bitvec.iter().copied().take(bitvec.len() - 1));
372            assert_eq!(
373                truncated.read_natural::<usize>(None),
374                Err(DecodeNaturalError::EndOfStream(
375                    crate::EarlyEndOfStreamError
376                )),
377            );
378
379            // Test decoding under various bounds
380            assert_eq!(iter.clone().read_natural(None), Ok(natural),);
381            assert_eq!(iter.clone().read_natural::<u64>(None), Ok(natural as u64),);
382
383            assert_eq!(iter.clone().read_natural(Some(natural)), Ok(natural),);
384            assert_eq!(iter.clone().read_natural(Some(natural + 1)), Ok(natural),);
385            assert_eq!(
386                iter.clone().read_natural(Some(natural - 1)),
387                Err(DecodeNaturalError::BadIndex {
388                    got: natural,
389                    max: natural - 1
390                }),
391            );
392            assert_eq!(
393                iter.clone().read_natural(Some(0u64)),
394                Err(DecodeNaturalError::BadIndex {
395                    got: natural,
396                    max: 0
397                }),
398            );
399
400            // Try decoding as a small type.
401            if let Ok(natural) = u16::try_from(natural) {
402                assert_eq!(iter.read_natural::<u16>(None), Ok(natural),);
403            } else {
404                assert_eq!(
405                    iter.read_natural::<u16>(None),
406                    Err(DecodeNaturalError::Overflow),
407                );
408            }
409            // Attempt to re-encode.
410            let mut sink = Vec::<u8>::new();
411
412            let mut w = BitWriter::from(&mut sink);
413            encode::encode_natural(natural, &mut w).expect("encoding to vector");
414            w.flush_all().expect("flushing");
415            assert_eq!(w.n_total_written(), len);
416
417            assert_eq!(sink, bitvec);
418        }
419
420        // Test u32::MAX + 1 separately. This should always return an overflow and
421        // never succeed or panic. Just hammer it with a bunch of different types
422        // and call patterns.
423        let iter = BitIter::new(
424            [
425                0b11110001, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
426            ]
427            .into_iter(),
428        );
429        assert_eq!(
430            iter.clone().read_natural::<usize>(None),
431            Err(DecodeNaturalError::Overflow),
432        );
433        assert_eq!(
434            iter.clone().read_natural::<u16>(None),
435            Err(DecodeNaturalError::Overflow),
436        );
437        assert_eq!(
438            iter.clone().read_natural::<i32>(None),
439            Err(DecodeNaturalError::Overflow),
440        );
441        assert_eq!(
442            iter.clone().read_natural::<u32>(None),
443            Err(DecodeNaturalError::Overflow),
444        );
445        assert_eq!(
446            iter.clone().read_natural::<u64>(None),
447            Err(DecodeNaturalError::Overflow),
448        );
449        assert_eq!(
450            iter.clone().read_natural(Some(0u8)),
451            Err(DecodeNaturalError::Overflow),
452        );
453        assert_eq!(
454            iter.clone().read_natural(Some(0i32)),
455            Err(DecodeNaturalError::Overflow),
456        );
457        assert_eq!(
458            iter.clone().read_natural(Some(0u32)),
459            Err(DecodeNaturalError::Overflow),
460        );
461    }
462}