diff options
-rw-r--r-- | ecc.hs | 25 |
1 files changed, 11 insertions, 14 deletions
@@ -62,26 +62,23 @@ instance Show ECPoint where validECPoint :: ECPoint -> Bool validECPoint Infinity = True -validECPoint p = (y p)^2 == (x p)^3 + (a p) * (x p) + (b p) +validECPoint p = y p ^ 2 == x p ^ 3 + a p * x p + b p add :: ECPoint -> ECPoint -> ECPoint add Infinity p = p add p Infinity = p add p q - | (a p) /= (a q) || (b p) /= (b q) = error "point not on same curve" - | (x p) == (x q) && (y p) /= (y q) = Infinity - | (x p) /= (x q) = - let slope = ((y q) - (y p)) / ((x q) - (x p)) - new_x = slope ^ 2 - (x p) - (x q) - new_y = slope * (x p - new_x) - (y p) - in ECPoint new_x new_y (a p) (b p) - | (x p) == (x q) && (y p) == 0 = Infinity - | p == q = - let slope = (3 * (x p) ^ 2 + (a p)) / (2 * (y p)) - new_x = slope ^ 2 - (x p) - (x q) - new_y = slope * (x p - new_x) - (y p) - in ECPoint new_x new_y (a p) (b p) + | a p /= a q || b p /= b q = error "point not on same curve" + | x p == x q && y p /= y q = Infinity + | x p /= x q = new_point $ (y q - y p) / (x q - x p) + | x p == x q && y p == 0 = Infinity + | p == q = new_point $ (3 * x p ^ 2 + a p) / (2 * y p) | otherwise = error "Unexpected case of points" + where + new_point slope = + let new_x = slope ^ 2 - x p - x q + new_y = slope * (x p - new_x) - y p + in ECPoint new_x new_y (a p) (b p) cc = |