Time-constant modular inverse
This commit is contained in:
parent
ce35a1e07d
commit
19b7ab375a
@ -16,6 +16,7 @@ module Crypto.Number.ModArithmetic
|
|||||||
, inverse
|
, inverse
|
||||||
, inverseCoprimes
|
, inverseCoprimes
|
||||||
, jacobi
|
, jacobi
|
||||||
|
, inverseFermat
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Exception (throw, Exception)
|
import Control.Exception (throw, Exception)
|
||||||
@ -120,3 +121,8 @@ jacobi a n
|
|||||||
n1 = n `mod` a1
|
n1 = n `mod` a1
|
||||||
in if a1 == 1 then Just s
|
in if a1 == 1 then Just s
|
||||||
else fmap (*s) (jacobi n1 a1)
|
else fmap (*s) (jacobi n1 a1)
|
||||||
|
|
||||||
|
-- | Modular inverse using Fermat's little theorem. This works only when
|
||||||
|
-- the modulus is prime but avoids side channels like in 'expSafe'.
|
||||||
|
inverseFermat :: Integer -> Integer -> Integer
|
||||||
|
inverseFermat g p = expSafe g (p - 2) p
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user