From dfc9fb9fb254e4cdf517585c406bf4b01704ccaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Ch=C3=A9ron?= Date: Fri, 12 Jun 2020 19:01:52 +0200 Subject: [PATCH] Fix powF2m when exponent is not a power of 2 Integer multiplication cannot be used because it includes carry propagation. This needs to use carry-less mulF2m instead. --- Crypto/Number/F2m.hs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Crypto/Number/F2m.hs b/Crypto/Number/F2m.hs index 4dd0db2..6ca2604 100644 --- a/Crypto/Number/F2m.hs +++ b/Crypto/Number/F2m.hs @@ -111,10 +111,10 @@ powF2m :: BinaryPolynomial -- ^Modulus -> Integer -- ^b -> Integer powF2m fx a b - | b == 0 = 1 - | b > 0 = squareF2m fx x * if even b then 1 else a - | b < 0 = error "powF2m: negative exponents disallowed" - | otherwise = error "powF2m: impossible" + | b < 0 = error "powF2m: negative exponents disallowed" + | b == 0 = if fx > 1 then 1 else 0 + | even b = squareF2m fx x + | otherwise = mulF2m fx a (squareF2m' x) where x = powF2m fx a (b `div` 2) -- | Square rooot in F₂m.