base58

Pure Haskell base58, base58check encoding/decoding (docs.ppad.tech/base58).
git clone git://git.ppad.tech/base58.git
Log | Files | Refs | README | LICENSE

Base58.hs (2949B)


      1 {-# LANGUAGE BangPatterns #-}
      2 {-# LANGUAGE OverloadedStrings #-}
      3 
      4 -- |
      5 -- Module: Data.ByteString.Base58
      6 -- Copyright: (c) 2024 Jared Tobin
      7 -- License: MIT
      8 -- Maintainer: Jared Tobin <jared@ppad.tech>
      9 --
     10 -- base58 encoding and decoding of strict bytestrings.
     11 
     12 module Data.ByteString.Base58 (
     13     encode
     14   , decode
     15   ) where
     16 
     17 import qualified Data.Bits as B
     18 import Data.Bits ((.|.))
     19 import qualified Data.ByteString as BS
     20 import qualified Data.ByteString.Unsafe as BU
     21 import Data.Word (Word8)
     22 
     23 fi :: (Integral a, Num b) => a -> b
     24 fi = fromIntegral
     25 {-# INLINE fi #-}
     26 
     27 -- word8 base58 character to word6 (ish)
     28 word6 :: Word8 -> Maybe Word8
     29 word6 c
     30   | c >= 49  && c <= 57  = pure $! c - 49 -- 1–9
     31   | c >= 65  && c <= 72  = pure $! c - 56 -- A–H
     32   | c >= 74  && c <= 78  = pure $! c - 57 -- J–N
     33   | c >= 80  && c <= 90  = pure $! c - 58 -- P–Z
     34   | c >= 97  && c <= 107 = pure $! c - 64 -- a–k
     35   | c >= 109 && c <= 122 = pure $! c - 65 -- m–z
     36   | otherwise = Nothing
     37 {-# INLINE word6 #-}
     38 
     39 -- | Encode a base256 'ByteString' as base58.
     40 --
     41 --   >>> encode "hello world"
     42 --   "StV1DL6CwTryKyV"
     43 encode :: BS.ByteString -> BS.ByteString
     44 encode bs = ls <> unroll_base58 (roll_base256 bs) where
     45   ls = leading_ones bs
     46 
     47 -- | Decode a base58 'ByteString' to base256.
     48 --
     49 --   Invalid inputs will produce 'Nothing'.
     50 --
     51 --   >>> decode "StV1DL6CwTryKyV"
     52 --   Just "hello world"
     53 --   >>> decode "StV1DL0CwTryKyV" -- s/6/0
     54 --   Nothing
     55 decode :: BS.ByteString -> Maybe BS.ByteString
     56 decode bs = do
     57   n <- roll_base58 bs
     58   let ls = leading_zeros bs
     59   pure $ ls <> unroll_base256 n
     60 
     61 base58_charset :: BS.ByteString
     62 base58_charset = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
     63 
     64 -- produce leading ones from leading zeros
     65 leading_ones :: BS.ByteString -> BS.ByteString
     66 leading_ones bs = BS.replicate (BS.length (BS.takeWhile (== 0x00) bs)) 0x31
     67 
     68 -- produce leading zeros from leading ones
     69 leading_zeros :: BS.ByteString -> BS.ByteString
     70 leading_zeros bs = BS.replicate (BS.length (BS.takeWhile (== 0x31) bs)) 0x00
     71 
     72 -- to base256
     73 unroll_base256 :: Integer -> BS.ByteString
     74 unroll_base256 = BS.reverse . BS.unfoldr coalg where
     75   coalg a
     76     | a == 0 = Nothing
     77     | otherwise = Just $
     78         let (b, c) = quotRem a 256
     79         in  (fi c, b)
     80 
     81 -- from base256
     82 roll_base256 :: BS.ByteString -> Integer
     83 roll_base256 = BS.foldl' alg 0 where
     84   alg !a !b = a `B.shiftL` 8 .|. fi b
     85 
     86 -- to base58
     87 unroll_base58 :: Integer -> BS.ByteString
     88 unroll_base58 = BS.reverse . BS.unfoldr coalg where
     89   coalg a
     90     | a == 0 = Nothing
     91     | otherwise = Just $
     92         let (b, c) = quotRem a 58
     93         in  (BU.unsafeIndex base58_charset (fi c), b)
     94 
     95 -- from base58, failing on any non-base58 character
     96 roll_base58 :: BS.ByteString -> Maybe Integer
     97 roll_base58 bs = go 0 0 where
     98   l = BS.length bs
     99   go !acc !i
    100     | i == l = Just acc
    101     | otherwise = case word6 (BU.unsafeIndex bs i) of
    102         Nothing -> Nothing
    103         Just w  -> go (acc * 58 + fi w) (i + 1)
    104