aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorÓscar Nájera <hi@oscarnajera.com>2021-07-06 20:51:59 +0200
committerÓscar Nájera <hi@oscarnajera.com>2021-07-06 20:51:59 +0200
commit9dcfc927bcc76ac68b179ce0dac4a75e8b5f6d2f (patch)
tree85ce36aa7e71fb8b036b88e15e74627b7fe7fcb2
parentf997c04eb1dcdcdf9b4cccccaa38f816d9c5c449 (diff)
downloadprogrammingbitcoin-9dcfc927bcc76ac68b179ce0dac4a75e8b5f6d2f.tar.gz
programmingbitcoin-9dcfc927bcc76ac68b179ce0dac4a75e8b5f6d2f.tar.bz2
programmingbitcoin-9dcfc927bcc76ac68b179ce0dac4a75e8b5f6d2f.zip
refactor binary expansion
-rw-r--r--ecc.hs29
1 files changed, 18 insertions, 11 deletions
diff --git a/ecc.hs b/ecc.hs
index 17029b6..0bdbf02 100644
--- a/ecc.hs
+++ b/ecc.hs
@@ -94,14 +94,15 @@ add p q
new_y = slope * (x p - new_x) - y p
in ECPoint new_x new_y (a p) (b p)
-binex :: (Eq a, Fractional a) => Integer -> ECPoint a -> ECPoint a -> ECPoint a
-binex m value result | m == 0 = result
- | m .&. 1 == 1 = loop (add result value)
- | otherwise = loop result
- where loop = binex (m `shiftR` 1) (add value value)
-crmul :: (Eq a, Fractional a) => Integer -> ECPoint a -> ECPoint a
-crmul m ec = binex m ec Infinity
+binaryExpansion :: (Eq a, Fractional a) => Integer -> ECPoint a -> ECPoint a -> ECPoint a
+binaryExpansion m value result | m == 0 = result
+ | otherwise = binaryExpansion (m `shiftR` 1) (add value value) accumulator
+ where
+ accumulator = if m .&. 1 == 1 then add result value else result
+
+scalarProduct :: (Eq a, Fractional a) => Integer -> ECPoint a -> ECPoint a
+scalarProduct m ec = binaryExpansion m ec Infinity
tre = FieldElement 3 :: FieldElement 31
cc =
@@ -120,14 +121,18 @@ cc =
)
dd =
- let prime = 223
- a = FieldElement 0 :: FieldElement prime
+ let a = FieldElement 0 :: FieldElement 223
b = FieldElement 7
x = FieldElement 192
y = FieldElement 105
- point = ECPoint x y a b
- in point
+ in ECPoint x y a b
+ee = ECPoint 192 105 (FieldElement 0 :: FieldElement 223) 7
+ff = ECPoint 192 105 0 7 :: ECPoint (FieldElement 223)
+
+aPoint = ECPoint 192 105 0 7 :: ECPoint (FieldElement 223)
+total = add aPoint $ add aPoint $ add aPoint $ add aPoint aPoint
+totalfold=foldr add Infinity $ replicate 5 aPoint
type S256Field = FieldElement (2 ^ 256- 2^ 32 - 977)
type S256Point = ECPoint S256Field
@@ -135,6 +140,8 @@ s256point :: S256Field -> S256Field -> S256Point
s256point x y = ECPoint x y 0 7
li :: S256Field
li = 12
+ll :: ECPoint ( FieldElement 31)
+ll = Infinity
ri= ECPoint 3 7 5 7 :: S256Point