unison/unison-src/base58.u

61 lines
1.3 KiB
Plaintext
Raw Normal View History

2018-06-28 00:12:17 +03:00
-- TODO: Characters
-- TODO: Bytes
type Optional a = Some a | None
2019-06-27 16:36:57 +03:00
type Words = Words (List Nat)
2018-06-28 00:12:17 +03:00
type Integer = Integer
Integer.zero : Integer
Integer.zero = _
2018-09-24 20:15:40 +03:00
shiftLeft : Nat -> Integer -> Integer
2018-06-28 00:12:17 +03:00
shiftLeft x y = _
(+) : Integer -> Integer -> Integer
(+) x y = _
2019-06-27 16:36:57 +03:00
unfoldRight : ∀ a b . (a -> Optional (a, b)) -> a -> List b
2018-06-28 00:12:17 +03:00
unfoldRight f z = _
2019-06-27 16:36:57 +03:00
foldLeft : ∀ a b . a -> (a -> b -> a) -> List b -> a
2018-06-28 00:12:17 +03:00
foldLeft z f s = _
2018-09-24 20:15:40 +03:00
toInteger : Nat -> Integer
2018-06-28 00:12:17 +03:00
toInteger x = _
bigEndian : Words -> Integer
bigEndian = cases
Words.Words s ->
2018-06-28 00:12:17 +03:00
foldLeft Integer.zero (acc w -> shiftLeft 8 acc + toInteger w) s
-- TODO: Need some conversions between integers and machine integers
2018-09-24 20:15:40 +03:00
divmod : Integer -> Nat -> (Integer, Nat)
2018-06-28 00:12:17 +03:00
divmod x y = _
(|>) : ∀ a b c . (a -> b) -> (b -> c) -> a -> c
(|>) g f x = f (g x)
2018-09-24 20:15:40 +03:00
(==) : Integer -> Nat -> Boolean
2018-06-28 00:12:17 +03:00
(==) a b = _
2018-09-24 20:15:40 +03:00
charAt : Nat -> Text -> Text
2018-06-28 00:12:17 +03:00
charAt n = Text.drop n |> Text.take 1
codeString : Text
codeString = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
base58Encode : Words -> Text
base58Encode ws =
2018-06-28 00:12:17 +03:00
x = bigEndian ws
base58 : Integer -> Optional (Integer, Text)
base58 a =
2018-06-28 00:12:17 +03:00
if a == 0
then Optional.None
else match divmod a 58 with
2018-06-28 00:12:17 +03:00
(d, m) -> Optional.Some (d, charAt m codeString)
foldLeft "" Text.concatenate (unfoldRight base58 x)
base58Decode : Text -> Words
base58Decode txt = _