Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Ec where
- import Network.Haskoin.Crypto
- {-
- import Data.Maybe (isJust, fromJust)
- ec = undefined
- newtype BigWord n = BigWord { getBigWordInteger :: Integer }
- deriving (Eq, Ord, Read, Show)
- type FieldP = BigWord ModP
- -- | Data type representing an Integer modulo curve order N.
- type FieldN = BigWord ModN
- data ModP
- data ModN
- curveA :: FieldP
- curveA = fromInteger integerA
- where
- integerA :: Integer
- integerA = 0
- curveB :: FieldP
- curveB = fromInteger integerB
- where
- integerB :: Integer
- integerB = 7
- -- point
- data Point = Point !FieldP !FieldP !FieldP | InfPoint
- deriving (Show, Read)
- makePoint :: FieldP -> FieldP -> Maybe Point
- makePoint x y
- | validatePoint point = Just point
- | otherwise = Nothing
- where
- point = Point x y 1
- validatePoint :: Point -> Bool
- validatePoint point = case getAffine point of
- -- 3.2.2.1.1 (check that point not equal to InfPoint)
- Nothing -> False
- -- 3.2.2.1.2 (check that the point lies on the curve)
- Just (x,y) -> y ^ (2 :: Int) == x ^ (3 :: Int) + (curveA * x) + curveB
- getAffine :: Point -> Maybe (FieldP, FieldP)
- getAffine point = case point of
- InfPoint -> Nothing
- (Point _ _ 0) -> Nothing
- (Point x y z) -> Just (x/z ^ (2 :: Int), y/z ^ (3 :: Int))
- -- end point
- curveG :: Point
- curveG = fromJust $ makePoint
- 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
- 0X483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
- -}
- {-
- -- from;
- -- https://github.com/lynchronan/haskoin-crypto/blob/master/src/Haskoin/Crypto/Keys.hs
- derivePublicKey :: PrivateKey -> PublicKey
- derivePublicKey k = case k of
- (PrivateKey d) -> PublicKey $ mulPoint d curveG
- (PrivateKeyU d) -> PublicKeyU $ mulPoint d curveG
- curveG :: Point
- curveG = fromJust $ makePoint
- 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
- 0X483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
- http://hackage.haskell.org/package/haskoin-0.1.0.2/docs/src/Network-Haskoin-Crypto-Point.html
- mulPoint :: FieldN -> Point -> Point
- mulPoint 0 _ = InfPoint
- mulPoint 1 p = p
- mulPoint _ InfPoint = InfPoint
- mulPoint n p
- | n == 0 = InfPoint
- | odd n = addPoint p (mulPoint (n-1) p)
- | otherwise = mulPoint (n `shiftR` 1) (doublePoint p)
- import Data.Bits (shiftR)
- doublePoint :: Point -> Point
- doublePoint InfPoint = InfPoint
- doublePoint (Point x y z)
- | y == 0 = InfPoint
- | otherwise = Point x' y' z'
- where
- s = 4*x*y ^ (2 :: Int)
- m = 3*x ^ (2 :: Int) + curveA * z ^ (4 :: Int)
- x' = m ^ (2 :: Int) - 2*s
- y' = m*(s - x') - 8*y ^ (4 :: Int)
- z' = 2*y*z
- curveA :: FieldP
- curveA = fromInteger integerA
- integerA :: Integer
- integerA = 0
- addPoint :: Point -> Point -> Point
- addPoint InfPoint point = point
- addPoint point InfPoint = point
- addPoint p1@(Point x1 y1 z1) (Point x2 y2 z2)
- | u1 == u2 = if s1 == s2 then doublePoint p1 else InfPoint
- | otherwise = Point x3 y3 z3
- where
- u1 = x1*z2 ^ (2 :: Int)
- u2 = x2*z1 ^ (2 :: Int)
- s1 = y1*z2 ^ (3 :: Int)
- s2 = y2*z1 ^ (3 :: Int)
- h = u2 - u1
- r = s2 - s1
- x3 = r ^ (2 :: Int) - h ^ (3 :: Int) - 2*u1*h ^ (2 :: Int)
- y3 = r*(u1 * h ^ (2 :: Int) - x3) - s1 * h ^ (3 :: Int)
- z3 = h * z1 * z2
- -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement