Skip to content

Add Clone bound to BitIter in decode function of Jet trait #342

@topologoanatom

Description

@topologoanatom

Motivation

Adding custom jet extensions in simplicity-unchained requires cumbersome code duplication of the entire decode tree defined by the macro

fn decode<I: Iterator<Item = u8>>(bits: &mut BitIter<I>) -> Result<Self, decode::Error> {
        decode_bits!(bits, {
            0 => {
                0 => {
                    0 => {Elements::Verify},
                    1 => {
                        ...

It would be nice if, in the context of simplicity-unchained, we could use the original Elements::decode() function as a fallback, defining the decoding tree only for custom jets:

fn decode<I: Iterator<Item = u8>>(
        bits: &mut BitIter<I>,
    ) -> Result<Self, decode::Error> {
        let try_elements = Elements::decode(&mut elements_iter);

        if let Ok(jet) = try_elements {
            return Ok(ElementsExtension::Elements(jet));
        }

        let try_custom = decode_bits!(bits, {
            0 => {},
            1 => {
                0 => {},
                1 => {
                    0 => {},
                    1 => {
                             // custom jets branches ...
        });

        try_custom
    }

Unfortunately, such approach is not possible under the current BitIter API because it does not provide a way to rewind steps. Furthermore, collecting bits using BitCollector::collect_bits() to create unique iterators for separate decode() calls is also not possible, as we cannot reassign the remaining bits back to the original bits iterator.

Proposed solution

Add a Clone bound for I: Iterator<Item = u8> in jet::Jet::decode() and related functions. This approach does not affect the autogenerated files from jet::init, therefore, it does not seem to significantly constrain maintainability.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions