HORIZON HASKELLDocslts/ghc-10.0.x8174fa42026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8174fa4 · 2026-10-11

Modulecrypton-2.1.7Haskell2010

Crypto.Number.Prime

  • 9 values
  • Packagecrypton-2.1.7
  • Exports9
  • LanguageHaskell2010
  • LicenceBSD-3-Clause AND MIT AND ISC
  • SourcePrime.hs
valuegeneratePrime :: MonadRandom m => Int -> m Integer
#

Generate a prime number of the required bitsize (i.e. in the range [2^(b-1)+2^(b-2), 2^b)).

May throw a CryptoError_PrimeSizeInvalid if the requested size is less than 5 bits, as the smallest prime meeting these conditions is 29. This function requires that the two highest bits are set, so that when multiplied with another prime to create a key, it is guaranteed to be of the proper size.

valuegenerateSafePrime :: MonadRandom m => Int -> m Integer
#

Generate a prime number of the form 2p+1 where p is also prime. it is also knowed as a Sophie Germaine prime or safe prime.

The number of safe prime is significantly smaller to the number of prime, as such it shouldn't be used if this number is supposed to be kept safe.

May throw a CryptoError_PrimeSizeInvalid if the requested size is less than 6 bits, as the smallest safe prime with the two highest bits set is 59.

valueisProbablyPrime :: Integer -> Bool
#

Returns if the number is probably prime.

The small primes are tested for divisibility first, and then the Miller-Rabin algorithm with an accuracy of 30 rounds.

A Fermat test of fifty consecutive bases used to run between the two. It ruled out nothing Miller-Rabin does not: a strong probable prime to a base is a Fermat probable prime to that base, and the converse is what Carmichael numbers are. What it cost was fifty modular exponentiations on every number that turned out to be prime -- 2167 of the 3480 microseconds spent on a 512-bit prime, and about two thirds of the time to generate one.

Miller Rabin algorithm return if the number is probably prime or composite. the tries parameter is the number of recursion, that determines the accuracy of the test.

The witnesses are drawn from a generator derived from n itself and from a secret drawn once per process: testing the same number twice gives the same answer, testing two numbers draws independent witnesses for each, and an attacker choosing the number cannot tell which witnesses it will face.

valueprimalityTestFermat
  1. :: Int

    number of iterations of the algorithm

  2. -> Integer

    starting a

  3. -> Integer

    number to test for primality

  4. -> Bool
#

Probabilitic Test using Fermat primility test. Beware of Carmichael numbers that are Fermat liars, i.e. this test is useless for them. always combines with some other test.