Miller Rabin primality test
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
module ModularArithmeticUtils (modExp, modMul, legendre) where
|
||||
module ModularArithmeticUtils (modExp, modMul, legendre, factorOutTwos) where
|
||||
|
||||
import Data.Bits (testBit, shiftR)
|
||||
|
||||
@@ -17,3 +17,11 @@ modMul a b m = (a * b) `mod` m
|
||||
-- (a/p) in {-1, 0, 1}
|
||||
legendre :: Integer -> Integer -> Integer
|
||||
legendre a p = modExp a ((p - 1) `div` 2) p
|
||||
|
||||
-- Factor p-1 as q * 2^s, where q is odd
|
||||
factorOutTwos :: Integer -> (Integer, Integer)
|
||||
factorOutTwos n = go n 0
|
||||
where
|
||||
go x s
|
||||
| even x = go (x `div` 2) (s + 1)
|
||||
| otherwise = (x, s)
|
||||
|
||||
Reference in New Issue
Block a user