From 08a8155f129b243ec26d18c99e9fc80691006a5a Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Tue, 22 Sep 2015 17:22:13 +0100 Subject: [PATCH] [P256] add function to generate a new scalar, and to get the base point. --- Crypto/PubKey/ECC/P256.hs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Crypto/PubKey/ECC/P256.hs b/Crypto/PubKey/ECC/P256.hs index 0e79729..f433aee 100644 --- a/Crypto/PubKey/ECC/P256.hs +++ b/Crypto/PubKey/ECC/P256.hs @@ -15,6 +15,7 @@ module Crypto.PubKey.ECC.P256 ( Scalar , Point -- * point arithmetic + , pointBase , pointAdd , pointMul , pointsMulVarTime @@ -25,6 +26,7 @@ module Crypto.PubKey.ECC.P256 , pointToBinary , pointFromBinary -- * scalar arithmetic + , scalarGenerate , scalarZero , scalarIsZero , scalarAdd @@ -48,6 +50,7 @@ import Crypto.Internal.ByteArray import qualified Crypto.Internal.ByteArray as B import Data.Memory.PtrMethods (memSet) import Crypto.Error +import Crypto.Random import Crypto.Number.Serialize.Internal (os2ip, i2ospOf) import qualified Crypto.Number.Serialize as S (os2ip, i2ospOf) @@ -75,6 +78,11 @@ data P256X -- Point methods ------------------------------------------------------------------------ +pointBase :: Point +pointBase = + case scalarFromInteger 1 of + CryptoPassed s -> toPoint s + CryptoFailed _ -> error "pointBase: assumption failed" -- | Lift to curve a scalar -- @@ -163,6 +171,15 @@ pointFromBinary ba -- Scalar methods ------------------------------------------------------------------------ +-- | Generate a randomly generated new scalar +scalarGenerate :: MonadRandom randomly => randomly Scalar +scalarGenerate = unwrap . scalarFromBinary . witness <$> getRandomBytes 32 + where + unwrap (CryptoFailed _) = error "scalarGenerate: assumption failed" + unwrap (CryptoPassed s) = s + witness :: ScrubbedBytes -> ScrubbedBytes + witness = id + -- | The scalar representing 0 scalarZero :: Scalar scalarZero = withNewScalarFreeze $ \d -> ccryptonite_p256_init d