|
| 1 | +-- Copyright (c) 2025 Blockstream |
| 2 | +-- |
| 3 | +-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +-- of this software and associated documentation files (the "Software"), to deal |
| 5 | +-- in the Software without restriction, including without limitation the rights |
| 6 | +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +-- copies of the Software, and to permit persons to whom the Software is |
| 8 | +-- furnished to do so, subject to the following conditions: |
| 9 | +-- |
| 10 | +-- The above copyright notice and this permission notice shall be included in |
| 11 | +-- all copies or substantial portions of the Software. |
| 12 | +-- |
| 13 | +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +-- THE SOFTWARE. |
| 20 | +module Codex32 where |
| 21 | + |
| 22 | +import Control.Monad (guard) |
| 23 | +import Data.Maybe (fromMaybe, listToMaybe) |
| 24 | +import Data.Word (Word8) |
| 25 | +import Data.Char (isAlpha, isDigit, isLower, isUpper, toLower) |
| 26 | +import Data.Bits (testBit) |
| 27 | + |
| 28 | +import Codex32.Polynomial |
| 29 | +import Codex32.Word5 |
| 30 | +import Codex32.Word10 |
| 31 | + |
| 32 | +fromBytes :: [Word8] -> [Word5] |
| 33 | +fromBytes bytes = go $ bits ++ replicate ((negate (length bits)) `mod` 5) False |
| 34 | + where |
| 35 | + bits = bytes >>= \b -> [testBit b i | i <- [7,6..0]] |
| 36 | + go [] = [] |
| 37 | + go (a:b:c:d:e:l) = w:go l |
| 38 | + where |
| 39 | + w = sum $ zipWith f [a,b,c,d,e] (word5 <$> [16, 8, 4, 2, 1]) |
| 40 | + f True v = v |
| 41 | + f False v = 0 |
| 42 | + |
| 43 | +toBytes :: [Word5] -> [Word8] |
| 44 | +toBytes l = go $ bits |
| 45 | + where |
| 46 | + bits = l >>= \(UnsafeWord5 b) -> [testBit b i | i <- [4,3..0]] |
| 47 | + go (a:b:c:d:e:f:g:h:l) = w:go l |
| 48 | + where |
| 49 | + w = sum $ zipWith fn [a,b,c,d,e,f,g,h] [128, 64, 32, 16, 8, 4, 2, 1] |
| 50 | + fn True v = v |
| 51 | + fn False v = 0 |
| 52 | + go _ = [] |
| 53 | + |
| 54 | +hrpExpand str = [1] ++ [word5 (fromEnum x `div` 32) | x <- str] ++ [0] ++ [word5 (fromEnum x) | x <- str] |
| 55 | + |
| 56 | +-- Specification of BCH code of degree 2 over GF[32] |
| 57 | +data Spec = Spec { specPrefix :: String -- Must be lowercase |
| 58 | + , specBase :: Word10 |
| 59 | + , specFcr :: Int -- First consecutive root |
| 60 | + , specDistance :: Int |
| 61 | + , specTarget :: [Word5] -- Must be have length equal to specDegree. |
| 62 | + } |
| 63 | +specHrp = hrpExpand . specPrefix |
| 64 | +specLength spec = fromMaybe err (order (specBase spec)) |
| 65 | + where |
| 66 | + err = error "Codex32.specLength: zero base" |
| 67 | +specDataLength spec = specLength spec - specDegree spec |
| 68 | +specRoots spec = [specBase spec^(i + specFcr spec) | i <- [0..specDistance spec-1]] |
| 69 | +specGenerator spec = foldr1 monicMult (minPoly <$> specRoots spec) |
| 70 | +specBias :: Spec -> Poly Word5 |
| 71 | +specBias = reverse . specTarget |
| 72 | +specDegree = length . specGenerator |
| 73 | + |
| 74 | +residue :: Spec -> [Word5] -> Poly Word5 |
| 75 | +residue spec body = p `polyMod` generator |
| 76 | + where |
| 77 | + generator = specGenerator spec |
| 78 | + p = reverse $ specHrp spec ++ body |
| 79 | + |
| 80 | +codex32Prefix = "ms" |
| 81 | +codex32Spec :: Spec |
| 82 | +codex32Spec = Spec { specPrefix = codex32Prefix |
| 83 | + , specBase = Word10 0 (read "G") |
| 84 | + , specFcr = 77 |
| 85 | + , specDistance = 8 |
| 86 | + , specTarget = fromRight (fromString "secretshare32") |
| 87 | + } |
| 88 | + |
| 89 | +codex32LongSpec :: Spec |
| 90 | +codex32LongSpec = Spec { specPrefix = codex32Prefix |
| 91 | + , specBase = Word10 (read "E") (read "X") |
| 92 | + , specFcr = 1019 |
| 93 | + , specDistance = 8 |
| 94 | + , specTarget = fromRight (fromString "secretshare32ex") |
| 95 | + } |
| 96 | + |
| 97 | +decodeErrString :: String -> Maybe (String, [Either Char Word5]) |
| 98 | +decodeErrString str | (all isLower `or` all isUpper) (filter isAlpha str) && Just '1' == listToMaybe xiferp = return (prefix, body) |
| 99 | + | otherwise = Nothing |
| 100 | + where |
| 101 | + (ydob, xiferp) = break (=='1') (reverse (toLower <$> str)) |
| 102 | + prefix = reverse $ tail xiferp |
| 103 | + body = charsetMap <$> reverse ydob |
| 104 | + or p q x = p x || q x |
| 105 | + |
| 106 | +decodeCodex32 str = do |
| 107 | + (pre, body) <- decodeErrString str |
| 108 | + guard $ pre == codex32Prefix |
| 109 | + let spec = if length body <= specLength codex32Spec then codex32Spec else codex32LongSpec |
| 110 | + return (spec, body) |
| 111 | + |
| 112 | +decodeString :: String -> Maybe (String, [Word5]) |
| 113 | +decodeString str = do |
| 114 | + (pre, errBody) <- decodeErrString str |
| 115 | + body <- traverse (either (const Nothing) Just) errBody |
| 116 | + return (pre, body) |
| 117 | + |
| 118 | +createGenericChecksum spec = \dat -> residue spec (dat ++ target) |
| 119 | + where |
| 120 | + hrp = hrpExpand (specPrefix spec) |
| 121 | + generator = specGenerator spec |
| 122 | + target = specTarget spec |
| 123 | + |
| 124 | +createGenericString spec = \dat -> prefix ++ "1" ++ toString (dat ++ reverse (createGenericChecksum spec dat)) |
| 125 | + where |
| 126 | + prefix = specPrefix spec |
| 127 | + |
| 128 | + |
| 129 | +createCodex32Checksum l = createGenericChecksum spec l |
| 130 | + where |
| 131 | + spec | length l <= specDataLength codex32Spec = codex32Spec |
| 132 | + | otherwise = codex32LongSpec |
| 133 | + |
| 134 | +verifyGenericChecksum spec l | length l <= specLength spec = bias == residue spec l |
| 135 | + | otherwise = False |
| 136 | + where |
| 137 | + hrp = hrpExpand (specPrefix spec) |
| 138 | + generator = specGenerator spec |
| 139 | + bias = specBias spec |
| 140 | + |
| 141 | +verifyCodex32Checksum l = any (flip verifyGenericChecksum l) [codex32Spec, codex32LongSpec] |
| 142 | + |
| 143 | +verifyGenericString spec str = (Just True ==) $ do |
| 144 | + (pre, dat) <- decodeString str |
| 145 | + guard $ pre == specPrefix spec |
| 146 | + return $ verifyGenericChecksum spec dat |
| 147 | + |
| 148 | +verifyCodex32String str = (Just True ==) $ do |
| 149 | + (pre, dat) <- decodeString str |
| 150 | + guard $ pre == codex32Prefix |
| 151 | + return $ verifyCodex32Checksum dat |
| 152 | + |
| 153 | +bip173Spec :: Spec |
| 154 | +bip173Spec = Spec { specPrefix = "bc" |
| 155 | + , specBase = Word10 (read "H") (read "F") |
| 156 | + , specFcr = 997 |
| 157 | + , specDistance = 3 |
| 158 | + , specTarget = fromRight (fromString "qqqqqp") |
| 159 | + } |
| 160 | +bip350Spec :: Spec |
| 161 | +bip350Spec = bip173Spec { specTarget = fromRight (fromString "4usv9r") } |
| 162 | + |
| 163 | +fromRight (Right x) = x |
| 164 | +fromRight (Left _) = error "Program error: fromRight: Left" |
0 commit comments