aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorÓscar Nájera <hi@oscarnajera.com>2021-04-05 21:01:33 +0200
committerÓscar Nájera <hi@oscarnajera.com>2021-04-05 21:01:33 +0200
commite0a6623405c50525e79717ed81ed0e75e5325fc8 (patch)
treed0b3ebb3f154c035ff34ff35b54d0110bd645a5c
parentb8741b7680c6d9659a829a72c759e8397534b7c3 (diff)
downloadprogrammingbitcoin-e0a6623405c50525e79717ed81ed0e75e5325fc8.tar.gz
programmingbitcoin-e0a6623405c50525e79717ed81ed0e75e5325fc8.tar.bz2
programmingbitcoin-e0a6623405c50525e79717ed81ed0e75e5325fc8.zip
Refactor and hs-lint
-rw-r--r--ecc.hs25
1 files changed, 11 insertions, 14 deletions
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 =