Copyright | (c) 2017 Andrew Lelechenko |
---|---|
License | MIT |
Maintainer | Andrew Lelechenko <andrew.lelechenko@gmail.com> |
Stability | Provisional |
Portability | Non-portable (GHC extensions) |
Safe Haskell | None |
Language | Haskell2010 |
Primitive roots and cyclic groups.
- isPrimitiveRoot :: KnownNat n => Mod n -> Bool
- data CyclicGroup a
- = CG2
- | CG4
- | CGOddPrimePower (Prime a) Word
- | CGDoubleOddPrimePower (Prime a) Word
- cyclicGroupFromModulo :: (Ord a, Integral a, UniqueFactorisation a) => a -> Maybe (CyclicGroup a)
- cyclicGroupToModulo :: (Integral a, UniqueFactorisation a) => CyclicGroup a -> Prefactored a
- isPrimitiveRoot' :: (Integral a, UniqueFactorisation a) => CyclicGroup a -> a -> Bool
Documentation
isPrimitiveRoot :: KnownNat n => Mod n -> Bool Source #
Check whether a given modular residue is a primitive root.
> isPrimitiveRoot (1 :: Mod 13) False > isPrimitiveRoot (2 :: Mod 13) True
Here is how to list all primitive roots:
> filter isPrimitiveRoot [minBound .. maxBound] :: [Mod 13] [(2 `modulo` 13), (6 `modulo` 13), (7 `modulo` 13), (11 `modulo` 13)]
This function is a convenient wrapper around isPrimitiveRoot'
. The latter
provides better control and performance, if you need them.
Cyclic groups
data CyclicGroup a Source #
A multiplicative group of residues is called cyclic,
if there is a primitive root g
,
whose powers generates all elements.
Any cyclic multiplicative group of residues
falls into one of the following cases.
CG2 | Residues modulo 2. |
CG4 | Residues modulo 4. |
CGOddPrimePower (Prime a) Word | Residues modulo |
CGDoubleOddPrimePower (Prime a) Word | Residues modulo 2 |
cyclicGroupFromModulo :: (Ord a, Integral a, UniqueFactorisation a) => a -> Maybe (CyclicGroup a) Source #
Check whether a multiplicative group of residues, characterized by its modulo, is cyclic and, if yes, return its form.
> cyclicGroupFromModulo 4 Just CG4 > cyclicGroupFromModulo (2 * 13 ^ 3) Just (CGDoubleOddPrimePower (PrimeNat 13) 3) > cyclicGroupFromModulo (4 * 13) Nothing
cyclicGroupToModulo :: (Integral a, UniqueFactorisation a) => CyclicGroup a -> Prefactored a Source #
Extract modulo and its factorisation from a cyclic multiplicative group of residues.
> cyclicGroupToModulo CG4 Prefactored {prefValue = 4, prefFactors = [(2, 2)]} > cyclicGroupToModulo (CGDoubleOddPrimePower (PrimeNat 13) 3) Prefactored {prefValue = 4394, prefFactors = [(2, 1), (13, 3)]}
isPrimitiveRoot' :: (Integral a, UniqueFactorisation a) => CyclicGroup a -> a -> Bool Source #
isPrimitiveRoot'
cg
a
checks whether a
is
a primitive root
of a given cyclic multiplicative group of residues cg
.
> let Just cg = cyclicGroupFromModulo 13 > isPrimitiveRoot cg 1 False > isPrimitiveRoot cg 2 True