fast-bech32-1.0.0: Fast implementation of the Bech32 encoding format.
Safe HaskellNone
LanguageHaskell2010

Data.ByteString.Bech32

Description

This package provides an implementation of the bech32 encoding. In essence, this encoding is comprised of three parts:

  • A human readable prefix
  • A base32-encoded payload (with a modified alphabet to avoid similar letters)
  • A checksum calculated on the prefix and the encoded payload

The implementation is optimized on mainly three aspects:

  • Low-level memory manipulation with typed pointers is used to traverse the input string and to write the encoded value. Each byte is encoded on-the-fly, which requires keeping track of a residue (since we encode from 8 bit to 5 bit, but can only read 8 bit at the same, the remainder is stored and passed to the next builder loop).
  • Since the checksum is calculated by folding over the encoded bytestring, it can be computed in-place, as the string is being encoded. This is exactly what this implementation does as well. In such way that once the string is encoded, the checksum is already calculated and only need to be base32 encoded in turn.
  • The first part of the checksum is pre-computed during the construction of the HRP. It is indeed quite common for a single HRP to encode many payload. To calculate the checksum, one must first expand the HRP which is in itself an already costly operation. Pre-calculating the expansion and the beginning of the checksum shaves off some time .
Synopsis

Encoding

encodeBech32 :: HumanReadablePart -> ByteString -> Text Source #

Encode some binary data to bech32 using the given human readable prefix.

HumanReadablePart

data HumanReadablePart where Source #

Bundled Patterns

pattern HumanReadablePart :: Text -> HumanReadablePart

Construct a human readable part from a text string, and pre-calculate the checksum corresponding to it.

Instances

Instances details
Show HumanReadablePart Source # 
Instance details

Defined in Data.ByteString.Bech32