Skip to content

ppad-tech/bech32

Repository files navigation

bech32

A pure Haskell implementation of bech32m and bech32 encoding/decoding on strict ByteStrings, as specified by BIP350 and BIP173.

Usage

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")

Documentation

Haddocks (API documentation, etc.) are hosted at docs.ppad.tech/bech32.

Performance

The aim is best-in-class performance for pure, highly-auditable Haskell code. At present we're a little over twice as fast as the official BIP173 reference implementation.

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                 783.5 ns   (781.4 ns .. 786.6 ns)
                       1.000 R²   (1.000 R² .. 1.000 R²)
  mean                 791.1 ns   (788.7 ns .. 793.5 ns)
  std dev              8.193 ns   (7.461 ns .. 8.973 ns)

  benchmarking benchmarks/ppad-bech32/bech32 decode/120b
  time                 944.0 ns   (943.1 ns .. 944.7 ns)
                       1.000 R²   (1.000 R² .. 1.000 R²)
  mean                 942.4 ns   (941.7 ns .. 943.1 ns)
  std dev              2.197 ns   (1.838 ns .. 2.669 ns)

  benchmarking benchmarks/reference/bech32 encode/120b
  time                 1.282 μs   (1.281 μs .. 1.283 μs)
                       1.000 R²   (1.000 R² .. 1.000 R²)
  mean                 1.282 μs   (1.282 μs .. 1.283 μs)
  std dev              1.338 ns   (996.0 ps .. 1.881 ns)

Security

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.

Development

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.

Attribution

The base32 implementation used internally is more or less a pure translation of the base32 package on Hackage.

About

Pure Haskell bech32, bech32m encoding/decoding

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published