Created
April 4, 2018 07:11
-
-
Save Sophia-Gold/b121d76e3e7e80772eb87ab365a14dc1 to your computer and use it in GitHub Desktop.
Montgomery multiplication in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE DataKinds #-} | |
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
| {-# LANGUAGE OverlappingInstances #-} | |
| module Montgomery where | |
| -- type R = 2 | |
| r :: Integer | |
| r = 2 | |
| newtype MontG = MontG Integer deriving (Eq, Ord, Real, Enum) | |
| instance Integral MontG where | |
| MontG a `mod` MontG b = MontG (a*r `mod` b) | |
| instance Num MontG where | |
| MontG a * MontG b = MontG (a*b*r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment