smart-math-0.1: Mathematical smart constructors
Safe HaskellNone
LanguageHaskell2010

Numeric.Data.ModP.Internal.Primality

Description

Internal tools for modular arithmetic and primality testing. The main functions are isPrime and findInverse, though others are exported for testing.

Since: 0.1

Synopsis

Primality Testing

data MaybePrime Source #

Result of running Miller-Rabin algorithm. At best we can determine if some n is definitely composite or "probably prime".

Since: 0.1

Constructors

Composite 
ProbablyPrime 

Instances

Instances details
Monoid MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Semigroup MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Generic MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Associated Types

type Rep MaybePrime 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

type Rep MaybePrime = D1 ('MetaData "MaybePrime" "Numeric.Data.ModP.Internal.Primality" "smart-math-0.1-inplace" 'False) (C1 ('MetaCons "Composite" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ProbablyPrime" 'PrefixI 'False) (U1 :: Type -> Type))
Show MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

NFData MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

rnf :: MaybePrime -> () #

Eq MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

type Rep MaybePrime Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

type Rep MaybePrime = D1 ('MetaData "MaybePrime" "Numeric.Data.ModP.Internal.Primality" "smart-math-0.1-inplace" 'False) (C1 ('MetaCons "Composite" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ProbablyPrime" 'PrefixI 'False) (U1 :: Type -> Type))

isPrime :: Integer -> MaybePrime Source #

Tests primality via the Miller-Rabin algorithm with 100 trials. Returns Composite if the number is definitely composite, otherwise ProbablyPrime.

Examples

Expand
>>> isPrime 7
ProbablyPrime
>>> isPrime 22
Composite
>>> isPrime 373
ProbablyPrime

Since: 0.1

isPrimeTrials :: Int -> Integer -> MaybePrime Source #

isPrime that takes in an additional Int parameter for the number of trials to run. The more trials, the more confident we can be in ProbablyPrime.

Examples

Expand
>>> isPrimeTrials 1 91
ProbablyPrime
>>> isPrimeTrials 2 91
Composite

Note: False positives can be found via:

-- search for "ProbablyPrime" after 1 trial in the composite sequence
-- for a given prime p
counter p = filter ((== ProbablyPrime) . snd) $
  fmap (x -> (x, isPrimeTrials 1 x)) [p + p, p + p + p ..]

Since: 0.1

millerRabin :: Modulus -> Int -> MaybePrime Source #

Miller-Rabin algorithm. Takes in the \(n\) to be tested and the number of trials to perform. The higher the trials, the higher our confidence in ProbablyPrime.

Arithmoi vs. Default

By default, our isPrime function implements miller-rabin directly. Unfortunately, the performance scales poorly (this is an issue with our implementation, not miller-rabin). Thus we provide the optional flag arithmoi that instead uses the arithmoi package. Arithmoi is much faster, though it is not a light dependency, hence the option.

In other words, the flag controls the tradeoff between isPrime speed vs. dependency footprint. So why do we also provide isPrimeDefault and isPrimeArithmoi? Benchmarking. We want to benchmark the difference, hence we need both available when the flag is on.

isPrimeArithmoi :: Integer -> MaybePrime Source #

Uses arithmoi if available, otherwise errors.

Helper Types

For the following functions/types, a core concept is rewriting our \(n\) as

\[ n = 2^r d + 1, \]

where \(d\) is odd i.e. we have factored out 2 as much as possible. We use newtypes to track these numbers.

newtype Modulus Source #

Represents a modulus. When testing for primality, this is the \(n\) in \(n = 2^{r} d + 1\).

Since: 0.1

Constructors

MkModulus Integer 

Instances

Instances details
Enum Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Num Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Integral Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Real Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Show Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Eq Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: Modulus -> Modulus -> Bool #

(/=) :: Modulus -> Modulus -> Bool #

Ord Modulus Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

newtype Pow Source #

The \(r\) in \(n = 2^{r} d + 1\).

Since: 0.1

Constructors

MkPow Integer 

Instances

Instances details
Enum Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

succ :: Pow -> Pow #

pred :: Pow -> Pow #

toEnum :: Int -> Pow #

fromEnum :: Pow -> Int #

enumFrom :: Pow -> [Pow] #

enumFromThen :: Pow -> Pow -> [Pow] #

enumFromTo :: Pow -> Pow -> [Pow] #

enumFromThenTo :: Pow -> Pow -> Pow -> [Pow] #

Num Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(+) :: Pow -> Pow -> Pow #

(-) :: Pow -> Pow -> Pow #

(*) :: Pow -> Pow -> Pow #

negate :: Pow -> Pow #

abs :: Pow -> Pow #

signum :: Pow -> Pow #

fromInteger :: Integer -> Pow #

Integral Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

quot :: Pow -> Pow -> Pow #

rem :: Pow -> Pow -> Pow #

div :: Pow -> Pow -> Pow #

mod :: Pow -> Pow -> Pow #

quotRem :: Pow -> Pow -> (Pow, Pow) #

divMod :: Pow -> Pow -> (Pow, Pow) #

toInteger :: Pow -> Integer #

Real Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

toRational :: Pow -> Rational #

Show Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

showsPrec :: Int -> Pow -> ShowS #

show :: Pow -> String #

showList :: [Pow] -> ShowS #

Eq Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: Pow -> Pow -> Bool #

(/=) :: Pow -> Pow -> Bool #

Ord Pow Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

compare :: Pow -> Pow -> Ordering #

(<) :: Pow -> Pow -> Bool #

(<=) :: Pow -> Pow -> Bool #

(>) :: Pow -> Pow -> Bool #

(>=) :: Pow -> Pow -> Bool #

max :: Pow -> Pow -> Pow #

min :: Pow -> Pow -> Pow #

newtype Mult Source #

The \(d\) in \(n = 2^{r} d + 1\).

Since: 0.1

Constructors

MkMult Integer 

Instances

Instances details
Enum Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

succ :: Mult -> Mult #

pred :: Mult -> Mult #

toEnum :: Int -> Mult #

fromEnum :: Mult -> Int #

enumFrom :: Mult -> [Mult] #

enumFromThen :: Mult -> Mult -> [Mult] #

enumFromTo :: Mult -> Mult -> [Mult] #

enumFromThenTo :: Mult -> Mult -> Mult -> [Mult] #

Num Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(+) :: Mult -> Mult -> Mult #

(-) :: Mult -> Mult -> Mult #

(*) :: Mult -> Mult -> Mult #

negate :: Mult -> Mult #

abs :: Mult -> Mult #

signum :: Mult -> Mult #

fromInteger :: Integer -> Mult #

Integral Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

quot :: Mult -> Mult -> Mult #

rem :: Mult -> Mult -> Mult #

div :: Mult -> Mult -> Mult #

mod :: Mult -> Mult -> Mult #

quotRem :: Mult -> Mult -> (Mult, Mult) #

divMod :: Mult -> Mult -> (Mult, Mult) #

toInteger :: Mult -> Integer #

Real Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

toRational :: Mult -> Rational #

Show Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

showsPrec :: Int -> Mult -> ShowS #

show :: Mult -> String #

showList :: [Mult] -> ShowS #

Eq Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: Mult -> Mult -> Bool #

(/=) :: Mult -> Mult -> Bool #

Ord Mult Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

compare :: Mult -> Mult -> Ordering #

(<) :: Mult -> Mult -> Bool #

(<=) :: Mult -> Mult -> Bool #

(>) :: Mult -> Mult -> Bool #

(>=) :: Mult -> Mult -> Bool #

max :: Mult -> Mult -> Mult #

min :: Mult -> Mult -> Mult #

newtype Rand Source #

Randomly generated \(m \in [2, n - 2] \) for testing \(n\)'s primality.

Since: 0.1

Constructors

MkRand Integer 

Instances

Instances details
Enum Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

succ :: Rand -> Rand #

pred :: Rand -> Rand #

toEnum :: Int -> Rand #

fromEnum :: Rand -> Int #

enumFrom :: Rand -> [Rand] #

enumFromThen :: Rand -> Rand -> [Rand] #

enumFromTo :: Rand -> Rand -> [Rand] #

enumFromThenTo :: Rand -> Rand -> Rand -> [Rand] #

Num Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(+) :: Rand -> Rand -> Rand #

(-) :: Rand -> Rand -> Rand #

(*) :: Rand -> Rand -> Rand #

negate :: Rand -> Rand #

abs :: Rand -> Rand #

signum :: Rand -> Rand #

fromInteger :: Integer -> Rand #

Integral Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

quot :: Rand -> Rand -> Rand #

rem :: Rand -> Rand -> Rand #

div :: Rand -> Rand -> Rand #

mod :: Rand -> Rand -> Rand #

quotRem :: Rand -> Rand -> (Rand, Rand) #

divMod :: Rand -> Rand -> (Rand, Rand) #

toInteger :: Rand -> Integer #

Real Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

toRational :: Rand -> Rational #

Show Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

showsPrec :: Int -> Rand -> ShowS #

show :: Rand -> String #

showList :: [Rand] -> ShowS #

Eq Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: Rand -> Rand -> Bool #

(/=) :: Rand -> Rand -> Bool #

Ord Rand Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

compare :: Rand -> Rand -> Ordering #

(<) :: Rand -> Rand -> Bool #

(<=) :: Rand -> Rand -> Bool #

(>) :: Rand -> Rand -> Bool #

(>=) :: Rand -> Rand -> Bool #

max :: Rand -> Rand -> Rand #

min :: Rand -> Rand -> Rand #

UniformRange Rand Source #

Since: 0.1

Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

uniformRM :: StatefulGen g m => (Rand, Rand) -> g -> m Rand Source #

Helper Functions

trial :: Modulus -> (Pow, Mult) -> Rand -> MaybePrime Source #

For \(n, r, d, x\) with \(n = 2^{r} d + 1\) and \(x \in [2, n - 2] \), returns Composite if \(n\) is definitely composite, ProbablyPrime otherwise.

Examples

Expand
>>> trial 12 (factor2 (12 - 1)) 3
Composite
>>> trial 7 (factor2 (7 - 1)) 3
ProbablyPrime

Since: 0.1

isWitness :: Modulus -> Pow -> Rand -> MaybePrime Source #

For \(n, r, x\) with \(n = 2^{r} d + 1\) and some \(x \equiv a^d \pmod n \), returns Composite if \(x\) is a witness to \(n\) being composite. Otherwise returns ProbablyPrime.

Examples

Expand
>>> let (pow, mult) = factor2 (12 - 1)
>>> let testVal = 3 ^ mult `mod` 12
>>> isWitness 12 pow testVal
Composite
>>> let (pow, mult) = factor2 (7 - 1)
>>> let testVal = 3 ^ mult `mod` 7
>>> isWitness 7 pow testVal
ProbablyPrime

Since: 0.1

sqProgression :: Modulus -> Integer -> [Integer] Source #

For \(n, x\), returns the infinite progression

\[ x, x^2, x^4, x^8, \ldots \pmod n. \]

Examples

Expand
>>> take 5 $ sqProgression 7 3
[3,2,4,2,4]

Since: 0.1

factor2 :: Modulus -> (Pow, Mult) Source #

Given \(n\), returns \((r, d)\) such that \(n = 2^r d\) with \(d\) odd i.e. \(2\) has been factored out.

Examples

Expand
>>> factor2 7
(MkPow 0,MkMult 7)
>>> factor2 8
(MkPow 3,MkMult 1)
>>> factor2 20
(MkPow 2,MkMult 5)

Since: 0.1

Multiplicative Inverses

Arithmoi vs. Default

invert :: forall (p :: Nat). KnownNat p => Natural -> Natural Source #

Finds the multiplicative inverse.

invertArithmoi :: forall (p :: Nat). KnownNat p => Natural -> Natural Source #

Finds the multiplicative inverse with arithmoi if available, otherwise errors.

invertDefault :: forall (p :: Nat). KnownNat p => Natural -> Natural Source #

Finds the multiplicative inverse using the built-in algorithm.

Types / Low-level

data Bezout Source #

@since 0.1t

Constructors

MkBezout 

Fields

Instances

Instances details
Show Bezout Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Eq Bezout Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: Bezout -> Bezout -> Bool #

(/=) :: Bezout -> Bezout -> Bool #

newtype R Source #

Since: 0.1

Constructors

R' Integer 

Instances

Instances details
Enum R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

succ :: R -> R #

pred :: R -> R #

toEnum :: Int -> R #

fromEnum :: R -> Int #

enumFrom :: R -> [R] #

enumFromThen :: R -> R -> [R] #

enumFromTo :: R -> R -> [R] #

enumFromThenTo :: R -> R -> R -> [R] #

Num R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(+) :: R -> R -> R #

(-) :: R -> R -> R #

(*) :: R -> R -> R #

negate :: R -> R #

abs :: R -> R #

signum :: R -> R #

fromInteger :: Integer -> R #

Integral R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

quot :: R -> R -> R #

rem :: R -> R -> R #

div :: R -> R -> R #

mod :: R -> R -> R #

quotRem :: R -> R -> (R, R) #

divMod :: R -> R -> (R, R) #

toInteger :: R -> Integer #

Real R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

toRational :: R -> Rational #

Show R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

showsPrec :: Int -> R -> ShowS #

show :: R -> String #

showList :: [R] -> ShowS #

Eq R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: R -> R -> Bool #

(/=) :: R -> R -> Bool #

Ord R Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

compare :: R -> R -> Ordering #

(<) :: R -> R -> Bool #

(<=) :: R -> R -> Bool #

(>) :: R -> R -> Bool #

(>=) :: R -> R -> Bool #

max :: R -> R -> R #

min :: R -> R -> R #

newtype S Source #

Since: 0.1

Constructors

S' Integer 

Instances

Instances details
Enum S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

succ :: S -> S #

pred :: S -> S #

toEnum :: Int -> S #

fromEnum :: S -> Int #

enumFrom :: S -> [S] #

enumFromThen :: S -> S -> [S] #

enumFromTo :: S -> S -> [S] #

enumFromThenTo :: S -> S -> S -> [S] #

Num S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(+) :: S -> S -> S #

(-) :: S -> S -> S #

(*) :: S -> S -> S #

negate :: S -> S #

abs :: S -> S #

signum :: S -> S #

fromInteger :: Integer -> S #

Integral S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

quot :: S -> S -> S #

rem :: S -> S -> S #

div :: S -> S -> S #

mod :: S -> S -> S #

quotRem :: S -> S -> (S, S) #

divMod :: S -> S -> (S, S) #

toInteger :: S -> Integer #

Real S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

toRational :: S -> Rational #

Show S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

showsPrec :: Int -> S -> ShowS #

show :: S -> String #

showList :: [S] -> ShowS #

Eq S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: S -> S -> Bool #

(/=) :: S -> S -> Bool #

Ord S Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

compare :: S -> S -> Ordering #

(<) :: S -> S -> Bool #

(<=) :: S -> S -> Bool #

(>) :: S -> S -> Bool #

(>=) :: S -> S -> Bool #

max :: S -> S -> S #

min :: S -> S -> S #

newtype T Source #

Since: 0.1

Constructors

T' Integer 

Instances

Instances details
Enum T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

succ :: T -> T #

pred :: T -> T #

toEnum :: Int -> T #

fromEnum :: T -> Int #

enumFrom :: T -> [T] #

enumFromThen :: T -> T -> [T] #

enumFromTo :: T -> T -> [T] #

enumFromThenTo :: T -> T -> T -> [T] #

Num T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(+) :: T -> T -> T #

(-) :: T -> T -> T #

(*) :: T -> T -> T #

negate :: T -> T #

abs :: T -> T #

signum :: T -> T #

fromInteger :: Integer -> T #

Integral T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

quot :: T -> T -> T #

rem :: T -> T -> T #

div :: T -> T -> T #

mod :: T -> T -> T #

quotRem :: T -> T -> (T, T) #

divMod :: T -> T -> (T, T) #

toInteger :: T -> Integer #

Real T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

toRational :: T -> Rational #

Show T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

showsPrec :: Int -> T -> ShowS #

show :: T -> String #

showList :: [T] -> ShowS #

Eq T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

(==) :: T -> T -> Bool #

(/=) :: T -> T -> Bool #

Ord T Source # 
Instance details

Defined in Numeric.Data.ModP.Internal.Primality

Methods

compare :: T -> T -> Ordering #

(<) :: T -> T -> Bool #

(<=) :: T -> T -> Bool #

(>) :: T -> T -> Bool #

(>=) :: T -> T -> Bool #

max :: T -> T -> T #

min :: T -> T -> T #

findInverse :: Integer -> Modulus -> Integer Source #

For \(a, p\), finds the multiplicative inverse of \(a\) in \(\mathbb{Z}/p\mathbb{Z}\). That is, finds e such that

\[ ae \equiv 1 \pmod p. \]

Note: The returned \(e\) is only an inverse when \(a\) and \(p\) are coprime i.e. \((a,p) = 1\). Of course this is guaranteed when \(p\) is prime and \(0 < a < p \), but it otherwise not true in general.

Also, this function requires division, it is partial when the modulus is 0.

Since: 0.1

Misc

errMsg :: String -> String -> String Source #

Since: 0.1