{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
module Crypto.Number.Nat
    ( type IsDivisibleBy8
    , type IsAtMost, type IsAtLeast
    , isDivisibleBy8
    , isAtMost
    , isAtLeast
    ) where
import           Data.Type.Equality
import           GHC.TypeLits
import           Unsafe.Coerce (unsafeCoerce)
import           Crypto.Internal.Nat
isDivisibleBy8 :: KnownNat n => proxy n -> Maybe (IsDiv8 n n :~: 'True)
isDivisibleBy8 :: forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Maybe (IsDiv8 n n :~: 'True)
isDivisibleBy8 proxy n
n
    | Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
mod (proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy n
n) Integer
8 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = (IsDiv8 n n :~: 'True) -> Maybe (IsDiv8 n n :~: 'True)
forall a. a -> Maybe a
Just ((Any :~: Any) -> IsDiv8 n n :~: 'True
forall a b. a -> b
unsafeCoerce Any :~: Any
forall {k} (a :: k). a :~: a
Refl)
    | Bool
otherwise             = Maybe (IsDiv8 n n :~: 'True)
forall a. Maybe a
Nothing
isAtMost :: (KnownNat value, KnownNat bound)
         => proxy value -> proxy' bound -> Maybe ((value <=? bound) :~: 'True)
isAtMost :: forall (value :: Nat) (bound :: Nat) (proxy :: Nat -> *)
       (proxy' :: Nat -> *).
(KnownNat value, KnownNat bound) =>
proxy value -> proxy' bound -> Maybe ((value <=? bound) :~: 'True)
isAtMost proxy value
x proxy' bound
y
    | proxy value -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy value
x Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= proxy' bound -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy' bound
y  = (OrdCond (CmpNat value bound) 'True 'True 'False :~: 'True)
-> Maybe
     (OrdCond (CmpNat value bound) 'True 'True 'False :~: 'True)
forall a. a -> Maybe a
Just ((Any :~: Any)
-> OrdCond (CmpNat value bound) 'True 'True 'False :~: 'True
forall a b. a -> b
unsafeCoerce Any :~: Any
forall {k} (a :: k). a :~: a
Refl)
    | Bool
otherwise             = Maybe (OrdCond (CmpNat value bound) 'True 'True 'False :~: 'True)
Maybe (OrdCond (Compare value bound) 'True 'True 'False :~: 'True)
forall a. Maybe a
Nothing
isAtLeast :: (KnownNat value, KnownNat bound)
          => proxy value -> proxy' bound -> Maybe ((bound <=? value) :~: 'True)
isAtLeast :: forall (value :: Nat) (bound :: Nat) (proxy :: Nat -> *)
       (proxy' :: Nat -> *).
(KnownNat value, KnownNat bound) =>
proxy value -> proxy' bound -> Maybe ((bound <=? value) :~: 'True)
isAtLeast = (proxy' bound
 -> proxy value
 -> Maybe
      (OrdCond (CmpNat bound value) 'True 'True 'False :~: 'True))
-> proxy value
-> proxy' bound
-> Maybe
     (OrdCond (CmpNat bound value) 'True 'True 'False :~: 'True)
forall a b c. (a -> b -> c) -> b -> a -> c
flip proxy' bound
-> proxy value
-> Maybe
     (OrdCond (CmpNat bound value) 'True 'True 'False :~: 'True)
proxy' bound
-> proxy value
-> Maybe
     (OrdCond (Compare bound value) 'True 'True 'False :~: 'True)
forall (value :: Nat) (bound :: Nat) (proxy :: Nat -> *)
       (proxy' :: Nat -> *).
(KnownNat value, KnownNat bound) =>
proxy value -> proxy' bound -> Maybe ((value <=? bound) :~: 'True)
isAtMost