aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ecc.hs33
1 files changed, 15 insertions, 18 deletions
diff --git a/ecc.hs b/ecc.hs
index 200b613..8146711 100644
--- a/ecc.hs
+++ b/ecc.hs
@@ -9,36 +9,33 @@ data FieldElement =
instance Show FieldElement where
show a = "FieldElement_" ++ show (prime a) ++ " " ++ show (number a)
-add :: FieldElement -> FieldElement -> FieldElement
-add (FieldElement a b) (FieldElement c d)
- | b /= d = error "Distinct Fields"
- | otherwise = FieldElement (mod (a + c) b) b
-
-sub :: FieldElement -> FieldElement -> FieldElement
-sub (FieldElement a b) (FieldElement c d)
- | b /= d = error "Distinct Fields"
- | otherwise = FieldElement (mod (a - c) b) b
-
-mul :: FieldElement -> FieldElement -> FieldElement
-mul (FieldElement a b) (FieldElement c d)
- | b /= d = error "Distinct Fields"
- | otherwise = FieldElement (mod (a * c) b) b
-
assert :: Bool -> Bool
assert False = error "WRONG"
assert x = x
+instance Num FieldElement where
+ (FieldElement a b) + (FieldElement c d)
+ | b /= d = error "Distinct Fields"
+ | otherwise = FieldElement (mod (a + c) b) b
+ (FieldElement a b) * (FieldElement c d)
+ | b /= d = error "Distinct Fields"
+ | otherwise = FieldElement (mod (a * c) b) b
+ abs a = a
+ signum _ = 1
+ negate (FieldElement a b) = FieldElement (mod (b - a) b) b
+ fromInteger _ = error "can't transform"
+
aa =
let a = FieldElement 2 31
b = FieldElement 15 31
in assert
(and
- [ add a b == FieldElement 17 31
+ [ a + b == FieldElement 17 31
, a /= b
- , sub a b == FieldElement 18 31
+ , a - b == FieldElement 18 31
])
bb =
let a = FieldElement 19 31
b = FieldElement 24 31
- in mul a b
+ in a * b