From 2e926396796aca084d61c85f554bedee577970d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Ch=C3=A9ron?= Date: Sat, 7 Oct 2017 15:16:53 +0200 Subject: [PATCH] Add P256.scalarMul --- Crypto/PubKey/ECC/P256.hs | 9 +++++++++ tests/KAT_PubKey/P256.hs | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/Crypto/PubKey/ECC/P256.hs b/Crypto/PubKey/ECC/P256.hs index 3c350cd..7b8c7c1 100644 --- a/Crypto/PubKey/ECC/P256.hs +++ b/Crypto/PubKey/ECC/P256.hs @@ -34,6 +34,7 @@ module Crypto.PubKey.ECC.P256 , scalarIsZero , scalarAdd , scalarSub + , scalarMul , scalarInv , scalarCmp , scalarFromBinary @@ -237,6 +238,14 @@ scalarSub a b = withNewScalarFreeze $ \d -> withScalar a $ \pa -> withScalar b $ \pb -> ccryptonite_p256e_modsub ccryptonite_SECP256r1_n pa pb d +-- | Perform multiplication between two scalars +-- +-- > a * b +scalarMul :: Scalar -> Scalar -> Scalar +scalarMul a b = + withNewScalarFreeze $ \d -> withScalar a $ \pa -> withScalar b $ \pb -> + ccryptonite_p256_modmul ccryptonite_SECP256r1_n pa 0 pb d + -- | Give the inverse of the scalar -- -- > 1 / a diff --git a/tests/KAT_PubKey/P256.hs b/tests/KAT_PubKey/P256.hs index c570548..f038133 100644 --- a/tests/KAT_PubKey/P256.hs +++ b/tests/KAT_PubKey/P256.hs @@ -92,6 +92,10 @@ tests = testGroup "P256" let v = unP256 r `mod` curveN v' = P256.scalarSub (unP256Scalar r) P256.scalarZero in v `propertyEq` p256ScalarToInteger v' + , testProperty "mul" $ \r1 r2 -> + let r = (unP256 r1 * unP256 r2) `mod` curveN + r' = P256.scalarMul (unP256Scalar r1) (unP256Scalar r2) + in r `propertyEq` p256ScalarToInteger r' , testProperty "inv" $ \r' -> let inv = inverseCoprimes (unP256 r') curveN inv' = P256.scalarInv (unP256Scalar r')