Support modular division of signed numbers.

This commit is contained in:
2018-12-23 21:55:07 -08:00
parent 2480bafe06
commit ae8266885b
66 changed files with 221461 additions and 1 deletions

View File

@@ -4,11 +4,13 @@ module Math(
, barrett, computeK, base
, modulate, modulate'
, isqrt
, divmod
, showX, showB
)
where
import Data.Bits(shiftL,shiftR)
import GHC.Integer.GMP.Internals(recipModInteger)
import Numeric(showHex)
data AlgState = AlgState {
@@ -122,6 +124,13 @@ isqrt bits val = final
| num >= (res + bit) = (num - (res + bit), res + (bit `shiftL` 1))
| otherwise = (num, res)
divmod :: Integer -> Integer -> Integer -> Maybe Integer
divmod x y m =
let y' = y `mod` m
in case recipModInteger y' m of
0 -> Nothing
i -> Just ((x * i) `mod` m)
_run :: Integer -> Integer -> IO ()
_run inputx inputy =
do let (x, y, g, initState) = initialState inputx inputy 1