From e0a6623405c50525e79717ed81ed0e75e5325fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Mon, 5 Apr 2021 21:01:33 +0200 Subject: Refactor and hs-lint --- ecc.hs | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'ecc.hs') diff --git a/ecc.hs b/ecc.hs index 01ef90e..7c485d0 100644 --- a/ecc.hs +++ b/ecc.hs @@ -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 = -- cgit v1.2.3