2 | import Syntax.WithProof
6 | import Data.Fin.Extra
10 | import Data.Nat.Factor
11 | import Data.Nat.Order.Properties
14 | weakenN' : {m : Nat} -> (0 n : Nat) -> Fin m -> Fin (n + m)
15 | weakenN' n m' = rewrite plusCommutative n m in weakenN n m'
17 | fix_fin : (m : Nat) -> (n : Nat) -> (S m) = n -> S (S (S (S (S (S (S (S (mult m 8)))))))) = mult n 8
18 | fix_fin m n prf = rewrite sym prf in Refl
21 | to_le : (FiniteBits a, Cast a Bits8) => {n : _} -> {auto 0 no0 : NonZero n} -> {auto 0 prf : n * 8 = (bitSize {a})} -> a -> Vect n Bits8
22 | to_le x = let (S m) = n;
nmeq = Refl in map (go nmeq x) Fin.range
24 | go : {m : Nat} -> ((S m) = n) -> a -> Fin (S m) -> Bits8
25 | go nmeq b i = cast $
shiftR b (bitsToIndex $
coerce prf $
coerce (fix_fin m n nmeq) $
weakenN' 7 $
i * 8)
28 | to_be : (FiniteBits a, Cast a Bits8) => {n : _} -> {auto 0 no0 : NonZero n} -> {auto 0 prf : n * 8 = (bitSize {a})} -> a -> Vect n Bits8
29 | to_be = reverse . to_le
32 | from_le : (FiniteBits a, Cast Bits8 a) => {n : _} -> {auto 0 no0 : NonZero n} -> {auto 0 prf : n * 8 = (bitSize {a})} -> Vect n Bits8 -> a
33 | from_le p = let (S m) = n;
nmeq = Refl in foldl (.|.) zeroBits $
zipWith (go nmeq) p Fin.range
35 | go : {m : Nat} -> ((S m) = n) -> Bits8 -> Fin (S m) -> a
36 | go nmeq b i = shiftL (cast b) (bitsToIndex $
coerce prf $
coerce (fix_fin m n nmeq) $
weakenN' 7 $
i * 8)
39 | from_be : (FiniteBits a, Cast Bits8 a) => {n : _} -> {auto 0 no0 : NonZero n} -> {auto 0 prf : n * 8 = (bitSize {a})} -> Vect n Bits8 -> a
40 | from_be = from_le . reverse
43 | set_bit_to : Bits a => Bool -> Index {a} -> a -> a
44 | set_bit_to False _ x = x
45 | set_bit_to True pos x = setBit x pos
48 | to_bools_be' : FiniteBits a => (n : Fin (S (bitSize {a}))) -> a -> Vect (finToNat n) Bool
49 | to_bools_be' FZ x = []
50 | to_bools_be' (FS wit) x = testBit x (bitsToIndex wit) :: (replace_vect finToNatWeakenNeutral $
to_bools_be' (weaken wit) x)
53 | to_bools_be : FiniteBits a => a -> Vect (bitSize {a}) Bool
54 | to_bools_be x = replace_vect finToNatLastIsBound $
to_bools_be' {a = a} Fin.last x
57 | le_to_integer : Foldable t => t Bits8 -> Integer
58 | le_to_integer = go . toList
60 | go : List Bits8 -> Integer
61 | go v = foldr (.|.) 0 $
zipWith shiftL (cast {to=Integer} <$> v) ((*8) <$> [0..(length v)])
64 | be_to_integer : Foldable t => t Bits8 -> Integer
65 | be_to_integer = le_to_integer . reverse . toList
68 | integer_to_le : (n : Nat) -> Integer -> Vect n Bits8
69 | integer_to_le n v = (cast . shiftR v) <$> (((*8) . finToNat) <$> Fin.range)
72 | integer_to_be : (n : Nat) -> Integer -> Vect n Bits8
73 | integer_to_be n = reverse . integer_to_le n
76 | to_digit : Bool -> Char
77 | to_digit False = '0'
81 | show_bin : FiniteBits a => a -> String
82 | show_bin = pack . toList . map to_digit . to_bools_be
87 | hex_lower : Bits8 -> Char
107 | show_hex : Bits8 -> String
108 | show_hex x = strCons (hex_lower (div x 16)) $
strCons (hex_lower (mod x 16)) $
""
112 | xxd : Foldable t => t Bits8 -> String
113 | xxd = concat . intersperse " " . map show_hex . toList
116 | string_to_ascii : (x : String) -> List Bits8
117 | string_to_ascii = map (cast . ord) . unpack
120 | ascii_to_string : List Bits8 -> String
121 | ascii_to_string = pack . map cast
123 | maybe_a_a : Lazy a -> Maybe a -> a
124 | maybe_a_a a Nothing = Force a
125 | maybe_a_a _ (Just a) = a
128 | shiftL' : FiniteBits a => a -> Nat -> a
129 | shiftL' x i = maybe_a_a zeroBits $
do
131 | Just $
shiftL x (bitsToIndex i')
134 | shiftR' : FiniteBits a => a -> Nat -> a
135 | shiftR' x i = maybe_a_a zeroBits $
do
137 | Just $
shiftR x (bitsToIndex i')
140 | rotl : FiniteBits a => {n : Nat} -> {auto prf : ((S n) = bitSize {a})} -> Fin (S n) -> a -> a
142 | rotl (FS i) x = (shiftL x $
bitsToIndex (coerce prf $
FS i)) .|. (shiftR x $
bitsToIndex $
complement $
coerce prf $
weaken i)
145 | rotr : FiniteBits a => {n : Nat} -> {auto prf : ((S n) = bitSize {a})} -> Fin (S n) -> a -> a
147 | rotr (FS i) x = (shiftR x $
bitsToIndex (coerce prf $
FS i)) .|. (shiftL x $
bitsToIndex $
complement $
coerce prf $
weaken i)