Skip to content

Instantly share code, notes, and snippets.

@Sophia-Gold
Created April 4, 2018 07:11
Show Gist options
  • Select an option

  • Save Sophia-Gold/b121d76e3e7e80772eb87ab365a14dc1 to your computer and use it in GitHub Desktop.

Select an option

Save Sophia-Gold/b121d76e3e7e80772eb87ab365a14dc1 to your computer and use it in GitHub Desktop.
Montgomery multiplication in Haskell
{-# 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