HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulecryptonite-0.30Haskell2010

Crypto.Number.Nat

Numbers at type level.

This module provides extensions to GHC.TypeLits and GHC.TypeNats useful to work with cryptographic algorithms parameterized with a variable bit length. Constraints like IsDivisibleBy8 n ensure that the type-level parameter is applicable to the algorithm.

Functions are also provided to test whether constraints are satisfied from values known at runtime. The following example shows how to discharge IsDivisibleBy8 in a computation fn requiring this constraint:

withDivisibleBy8 :: Integer
                 -> (forall proxy n . (KnownNat n, IsDivisibleBy8 n) => proxy n -> a)
                 -> Maybe a
withDivisibleBy8 len fn = do
    SomeNat p <- someNatVal len
    Refl <- isDivisibleBy8 p
    pure (fn p)

Function withDivisibleBy8 above returns Nothing when the argument len is negative or not divisible by 8.

  • 3 types
  • 3 values
  • Packagecryptonite-0.30
  • Exports6
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceNat.hs
typetype IsDivisibleBy8 (bitLen :: Nat) = IsDiv8 bitLen bitLen ~ 'True
#

ensure the given bitlen is divisible by 8

typetype IsAtMost (bitlen :: Nat) (n :: Nat) = IsLE bitlen n (bitlen <=? n) ~ 'True
#

ensure the given bitlen is lesser or equal to n

typetype IsAtLeast (bitlen :: Nat) (n :: Nat) = IsGE bitlen n (n <=? bitlen) ~ 'True
#

ensure the given bitlen is greater or equal to n