Add P256.scalarMul

This commit is contained in:
Olivier Chéron 2017-10-07 15:16:53 +02:00
parent 68c93ccbb1
commit 2e92639679
2 changed files with 13 additions and 0 deletions

View File

@ -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

View File

@ -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')