implement Tonelli Shanks

This commit is contained in:
2025-10-24 10:21:46 +02:00
parent 12a64f8008
commit cb41957ed9
3 changed files with 71 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
import Text.Read (readMaybe)
import System.Exit (exitSuccess)
import Data.Bits
import Utils (askNumber)
import ModularArithmeticUtils (modExp)
main :: IO ()
main = do
@@ -41,10 +41,3 @@ pollardP1WithParams n a b = go a 2
else if d == n
then Nothing -- Found n itself, try with different parameters
else go newA (k + 1)
-- Modular exponentiation: compute a^b mod m
modExp :: Integer -> Integer -> Integer -> Integer
modExp b 0 m = 1
modExp b e m = t * modExp ((b * b) `mod` m) (shiftR e 1) m `mod` m
where t = if testBit e 0 then b `mod` m else 1