diff options
author | Óscar Nájera <hi@oscarnajera.com> | 2021-04-05 18:24:19 +0200 |
---|---|---|
committer | Óscar Nájera <hi@oscarnajera.com> | 2021-04-05 18:24:19 +0200 |
commit | c67c5d23114e079f9f5bd79f782b6c87dd73c640 (patch) | |
tree | f85fe6114f392c74609bf9a64566c8865d0df337 | |
parent | 0f6af3be53e80f124a9c7d9294cccf8b4d1c272a (diff) | |
download | programmingbitcoin-c67c5d23114e079f9f5bd79f782b6c87dd73c640.tar.gz programmingbitcoin-c67c5d23114e079f9f5bd79f782b6c87dd73c640.tar.bz2 programmingbitcoin-c67c5d23114e079f9f5bd79f782b6c87dd73c640.zip |
Define FieldElement substraction and multiplication
-rw-r--r-- | ecc.hs | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -13,3 +13,32 @@ 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 + +aa = + let a = FieldElement 2 31 + b = FieldElement 15 31 + in assert + (and + [ add a b == FieldElement 17 31 + , a /= b + , sub a b == FieldElement 18 31 + ]) + +bb = + let a = FieldElement 19 31 + b = FieldElement 24 31 + in mul a b |