Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Ec where
  2. import Network.Haskoin.Crypto
  3. {-
  4. import Data.Maybe (isJust, fromJust)
  5. ec = undefined
  6.  
  7. newtype BigWord n = BigWord { getBigWordInteger :: Integer }
  8.     deriving (Eq, Ord, Read, Show)
  9.  
  10. type FieldP  = BigWord ModP
  11. -- | Data type representing an Integer modulo curve order N.
  12. type FieldN  = BigWord ModN
  13.  
  14. data ModP
  15. data ModN
  16.  
  17. curveA :: FieldP
  18. curveA = fromInteger integerA
  19.  where
  20.   integerA :: Integer
  21.   integerA = 0
  22.  
  23. curveB :: FieldP
  24. curveB = fromInteger integerB
  25.  where
  26.   integerB :: Integer
  27.   integerB = 7
  28.  
  29.  
  30. -- point
  31. data Point = Point !FieldP !FieldP !FieldP | InfPoint
  32.     deriving (Show, Read)
  33.  
  34. makePoint :: FieldP -> FieldP -> Maybe Point
  35. makePoint x y
  36.     | validatePoint point = Just point
  37.     | otherwise = Nothing
  38.   where
  39.     point = Point x y 1
  40.  
  41. validatePoint :: Point -> Bool
  42. validatePoint point = case getAffine point of
  43.     -- 3.2.2.1.1 (check that point not equal to InfPoint)
  44.     Nothing    -> False
  45.     -- 3.2.2.1.2 (check that the point lies on the curve)
  46.     Just (x,y) -> y ^ (2 :: Int) == x ^ (3 :: Int) + (curveA * x) + curveB
  47.  
  48. getAffine :: Point -> Maybe (FieldP, FieldP)
  49. getAffine point = case point of
  50.     InfPoint      -> Nothing
  51.     (Point _ _ 0) -> Nothing
  52.     (Point x y z) -> Just (x/z ^ (2 :: Int), y/z ^ (3 :: Int))
  53. -- end point
  54.  
  55. curveG :: Point
  56. curveG = fromJust $ makePoint
  57.         0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798      
  58.         0X483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
  59.  
  60. -}
  61. {-
  62. -- from;
  63. -- https://github.com/lynchronan/haskoin-crypto/blob/master/src/Haskoin/Crypto/Keys.hs
  64. derivePublicKey :: PrivateKey -> PublicKey
  65. derivePublicKey k = case k of
  66.     (PrivateKey  d) -> PublicKey  $ mulPoint d curveG
  67.     (PrivateKeyU d) -> PublicKeyU $ mulPoint d curveG
  68.  
  69. curveG :: Point
  70. curveG = fromJust $ makePoint
  71.         0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798      
  72.         0X483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
  73.  
  74. http://hackage.haskell.org/package/haskoin-0.1.0.2/docs/src/Network-Haskoin-Crypto-Point.html
  75. mulPoint :: FieldN -> Point -> Point
  76. mulPoint 0 _        = InfPoint
  77. mulPoint 1 p        = p
  78. mulPoint _ InfPoint = InfPoint
  79. mulPoint n p
  80.     | n == 0    = InfPoint
  81.     | odd n     = addPoint p (mulPoint (n-1) p)
  82.     | otherwise = mulPoint (n `shiftR` 1) (doublePoint p)
  83. import Data.Bits (shiftR)
  84.  
  85.  
  86.  
  87. doublePoint :: Point -> Point
  88. doublePoint InfPoint = InfPoint
  89. doublePoint (Point x y z)
  90.     | y == 0 = InfPoint
  91.     | otherwise = Point x' y' z'
  92.   where
  93.     s  = 4*x*y ^ (2 :: Int)
  94.     m  = 3*x ^ (2 :: Int) + curveA * z ^ (4 :: Int)
  95.     x' = m ^ (2 :: Int) - 2*s
  96.     y' = m*(s - x') - 8*y ^ (4 :: Int)
  97.     z' = 2*y*z
  98.  
  99. curveA :: FieldP
  100. curveA = fromInteger integerA
  101.  
  102. integerA :: Integer
  103. integerA = 0
  104.  
  105. addPoint :: Point -> Point -> Point
  106. addPoint InfPoint point = point
  107. addPoint point InfPoint = point
  108. addPoint p1@(Point x1 y1 z1) (Point x2 y2 z2)
  109.     | u1 == u2 = if s1 == s2 then doublePoint p1 else InfPoint
  110.     | otherwise = Point x3 y3 z3
  111.   where
  112.     u1 = x1*z2 ^ (2 :: Int)
  113.     u2 = x2*z1 ^ (2 :: Int)
  114.     s1 = y1*z2 ^ (3 :: Int)
  115.     s2 = y2*z1 ^ (3 :: Int)
  116.     h  = u2 - u1
  117.     r  = s2 - s1
  118.     x3 = r ^ (2 :: Int) - h ^ (3 :: Int) - 2*u1*h ^ (2 :: Int)
  119.     y3 = r*(u1 * h ^ (2 :: Int) - x3) - s1 * h ^ (3 :: Int)
  120.     z3 = h * z1 * z2
  121. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement