A fast Haskell implementation of bech32m and bech32 encoding/decoding on strict ByteStrings, as specified by BIP350 and BIP173.
A sample GHCi session:
> :set -XOverloadedStrings
>
> -- import qualified
> import qualified Data.ByteString.Bech32m as Bech32m
>
> -- create a bech32m-encoded string using a human-readable part (HRP)
> -- and some input
> let Just bech32m = Bech32m.encode "bc" "a standard word8 bytestring"
> bech32m
"bc1vys8xarpdejxzunyypmk7uny8qsxy7t5v4ehgunfdenswyuz0e"
>
> -- verify that a bech32m string has a valid checksum
> Bech32m.verify bech32
True
>
> -- tweaked stuff will obviously fail to verify (s/m/w below)
> Bech32m.verify "bc1vys8xarpdejxzunyypwk7uny8qsxy7t5v4ehgunfdenswyuz0e"
False
>
> -- decode bech32m-encoded input
> Bech32m.decode bech32m
Just ("bc","a standard word8 bytestring")
Haddocks (API documentation, etc.) are hosted at docs.ppad.tech/bech32.
The aim is best-in-class performance.
Current benchmark figures on a M4 Silicon MacBook Air look like (use
cabal bench to run the benchmark suite):
benchmarking benchmarks/ppad-bech32/bech32 encode/120b
time 240.8 ns (238.1 ns .. 243.3 ns)
0.999 R² (0.999 R² .. 1.000 R²)
mean 240.3 ns (238.7 ns .. 242.4 ns)
std dev 6.035 ns (5.059 ns .. 7.607 ns)
variance introduced by outliers: 36% (moderately inflated)
benchmarking benchmarks/ppad-bech32/bech32 decode/120b
time 257.5 ns (256.0 ns .. 259.7 ns)
0.999 R² (0.999 R² .. 1.000 R²)
mean 262.3 ns (260.5 ns .. 264.5 ns)
std dev 6.611 ns (5.482 ns .. 8.776 ns)
variance introduced by outliers: 36% (moderately inflated)
You should compile with the 'llvm' flag for maximum performance.
This library aims at the maximum security achievable in a garbage-collected language under an optimizing compiler such as GHC, in which strict constant-timeness can be challenging to achieve.
If you discover any vulnerabilities, please disclose them via security@ppad.tech.
You'll require Nix with flake support enabled. Enter a development shell with:
$ nix develop
Then do e.g.:
$ cabal repl ppad-bech32
to get a REPL for the main library.