From 9dcfc927bcc76ac68b179ce0dac4a75e8b5f6d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Tue, 6 Jul 2021 20:51:59 +0200 Subject: refactor binary expansion --- ecc.hs | 29 ++++++++++++++++++----------- 1 file 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 -- cgit v1.2.3