Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More error correction preparation (upgrades to Field, Polynomial, InvalidResidueError and others) #202

Closed
wants to merge 10 commits into from

Commits on Sep 18, 2024

  1. field: use UFCS in macro

    This is purely a code refactor to get rid of a `use` statement (which
    will trigger an "unused code" warning if the macro is ever called
    somewhere where the Field trait is already in scope). As a side effect
    it reduces the size of the macro by roughly 75% in terms of line count.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    8eaec9d View commit details
    Browse the repository at this point in the history
  2. field: split trait into "normal" field trait and sealed trait

    Several methods (and we are going to add more) don't really belong in a
    general-purpose field trait, which really is just "a type that can be
    multiplied and added and has some associated constants".
    
    This isn't a general-purpose math library, but fields are a pretty
    common thing for people to want. And it's pretty easy for us to expose
    this trait in a nice way, so we might as well do it.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    3c97cd1 View commit details
    Browse the repository at this point in the history
  3. field: add format_as_rust_code utility method

    This will make it easier for PrintImpl to output error correction
    parameters.
    
    Since this is an implementation detail of the library, stick it into the
    Bech32Field trait rather than the Field one.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    011674d View commit details
    Browse the repository at this point in the history
  4. field: add Default bound to Field

    In the next commits we'll introduce a generic no-alloc container called
    FieldVec whose implementation will be much nicer if we have a Default
    bound on all of our field elements. This way we can implement the
    "FieldVec", which despite the name really has nothing to do with fields,
    purely in terms of core traits, rather than confusing readers of the
    code by having Field bounds everywhere without ever doing algebra.
    
    Since fields all have a ZERO member, which is a sensible output for
    Default, this bound is reasonable to require.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    14e4f7b View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    c389d72 View commit details
    Browse the repository at this point in the history
  6. field: add Powers iterator

    This adds some new methods to Field, which have default impls and are
    therefore non-breaking. (And they are general-purpose "field" things.)
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    f47ec0b View commit details
    Browse the repository at this point in the history
  7. primitives: introduce FieldVec type

    This will allow `Polynomial` to work without an allocator (at least for
    "small" checksums, including every checksum supported by this library).
    Here a "small" checksum is one of length at most 6 (which covers bech32
    and bech32m).
    
    codex32 (13 characters) and "long codex32" (15 characters) will not work
    with no-alloc. I would kinda like to fix this but it results in large
    types, especially a large InvalidResidueError type, so probably it will
    need to be feature-gated or something. For now we just punt on it.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    d548ce5 View commit details
    Browse the repository at this point in the history
  8. polynomial: use FieldVec in Polynomial

    We can now use `PrintImpl` even without an allocator. This is of limited
    use of course, but it paves the ground for use to do error correction
    without an allocator, which is interesting.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    1fce601 View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    2d3833c View commit details
    Browse the repository at this point in the history
  10. primitives: introduce InvalidResidueError

    We are going to want to extend the ChecksumError::InvalidResidue error
    variant to allow error correction, and in order to do so, we need to know
    the actual residue computed from a string, not just that the residue failed
    to match the target.
    
    Uses the `Polynomial` type; see the previous commits for some caveats
    about this type when alloc is disabled. (Prior to this PR, you just
    couldn't use Polynomial at all without alloc.)
    
    As an initial application of the new error, avoid re-validating
    a bech32 address to handle the distinction between bech32m and
    bech32. Instead, if bech32m validation fails, check if the
    "bad" residue actually matches the bech32 target. If so, accept.
    
    On my system this reduces the `bech32_parse_address` benchmark
    from 610ns to 455ns, more than a 33% speedup.
    apoelstra committed Sep 18, 2024
    Configuration menu
    Copy the full SHA
    09bcf08 View commit details
    Browse the repository at this point in the history