README.md (3189B)
1 # ppad-bech32 2 3 A pure Haskell implementation of bech32m and bech32 encoding/decoding on 4 strict ByteStrings, as specified by [BIP350][bi350] and [BIP173][bi173]. 5 6 ## Usage 7 8 A sample GHCi session: 9 10 ``` 11 > :set -XOverloadedStrings 12 > 13 > -- import qualified 14 > import qualified Data.ByteString.Bech32m as Bech32m 15 > 16 > -- create a bech32m-encoded string using a human-readable part (HRP) 17 > -- and some input 18 > let Just bech32m = Bech32m.encode "bc" "a standard word8 bytestring" 19 > bech32m 20 "bc1vys8xarpdejxzunyypmk7uny8qsxy7t5v4ehgunfdenswyuz0e" 21 > 22 > -- verify that a bech32m string has a valid checksum 23 > Bech32m.verify bech32 24 True 25 > 26 > -- tweaked stuff will obviously fail to verify (s/m/w below) 27 > Bech32m.verify "bc1vys8xarpdejxzunyypwk7uny8qsxy7t5v4ehgunfdenswyuz0e" 28 False 29 > 30 > -- decode bech32m-encoded input 31 > Bech32m.decode bech32m 32 Just ("bc","a standard word8 bytestring") 33 ``` 34 35 ## Documentation 36 37 Haddocks (API documentation, etc.) are hosted at 38 [docs.ppad.tech/bech32](https://docs.ppad.tech/bech32). 39 40 ## Performance 41 42 The aim is best-in-class performance for pure, highly-auditable Haskell 43 code. At present we're a little over twice as fast as the official 44 BIP173 reference implementation. 45 46 Current benchmark figures on my mid-2020 MacBook Air look like (use 47 `cabal bench` to run the benchmark suite): 48 49 ``` 50 benchmarking benchmarks/ppad-bech32/bech32 encode/120b 51 time 1.278 μs (1.264 μs .. 1.293 μs) 52 0.999 R² (0.999 R² .. 1.000 R²) 53 mean 1.271 μs (1.261 μs .. 1.283 μs) 54 std dev 37.83 ns (31.42 ns .. 45.63 ns) 55 56 benchmarking benchmarks/ppad-bech32/bech32 decode/120b 57 time 1.567 μs (1.519 μs .. 1.611 μs) 58 0.996 R² (0.994 R² .. 0.998 R²) 59 mean 1.535 μs (1.511 μs .. 1.565 μs) 60 std dev 88.19 ns (71.27 ns .. 108.7 ns) 61 62 benchmarking benchmarks/reference/bech32 encode/120b 63 time 2.953 μs (2.785 μs .. 3.143 μs) 64 0.975 R² (0.958 R² .. 0.991 R²) 65 mean 2.817 μs (2.723 μs .. 2.998 μs) 66 std dev 415.9 ns (287.2 ns .. 640.3 ns) 67 ``` 68 69 ## Security 70 71 This library aims at the maximum security achievable in a 72 garbage-collected language under an optimizing compiler such as GHC, in 73 which strict constant-timeness can be challenging to achieve. 74 75 If you discover any vulnerabilities, please disclose them via 76 security@ppad.tech. 77 78 ## Development 79 80 You'll require [Nix][nixos] with [flake][flake] support enabled. Enter a 81 development shell with: 82 83 ``` 84 $ nix develop 85 ``` 86 87 Then do e.g.: 88 89 ``` 90 $ cabal repl ppad-bech32 91 ``` 92 93 to get a REPL for the main library. 94 95 ## Attribution 96 97 The base32 implementation used internally is more or less a pure 98 translation of the [base32][bas32] package on Hackage. 99 100 [nixos]: https://nixos.org/ 101 [flake]: https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html 102 [bi173]: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki 103 [bi350]: https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki 104 [bas32]: https://hackage.haskell.org/package/base32