Use of Data.Mod for modular exponentiation in Haskell

Viewed 239

When using powModfrom the Math.NumberTheory.Powers.Modular library, the Haskell compiler or interpreter gives the following warning:

warning: [-Wdeprecations] In the use of ‘powMod’ (imported from Math.NumberTheory.Powers.Modular): Deprecated: "Use Data.Mod or Data.Mod.Word instead"

I had the following function:

cypher n e m = powMod m e n

So I try to convert it to the recommended new library Data.Mod, substituting the powMod function by the (^%) operator:

cypher n e m = m ^% e :: Mod n

But then, the following error arises, and I don't know how to correct it:

• Couldn't match expected type ‘Mod n1’ with actual type ‘p2’
    because type variable ‘n1’ would escape its scope
  This (rigid, skolem) type variable is bound by
    an expression type signature:
      forall (n1 :: GHC.Types.Nat). Mod n1
    at RSA-cyphering.hs:43:26-30
• In the first argument of ‘(^%)’, namely ‘m’
  In the expression: m ^% e :: Mod n
  In an equation for ‘cypher’: cypher n e m = m ^% e :: Mod n

How can be used the type Mod n inside a function when the module n is taken from an argument to the function?

1 Answers

You use SomeMod:

cypher :: Natural -> Integer -> Integer -> SomeMod
cypher n e m = (m `modulo` n) ^ e
Related