From fbe1c213e2f156fd5ecafbafa93ed336b776d79d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Ch=C3=A9ron?= Date: Mon, 11 Dec 2017 06:40:41 +0100 Subject: [PATCH] Use throwCryptoError --- Crypto/ECC/Edwards25519.hs | 5 +---- tests/ECC/Edwards25519.hs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Crypto/ECC/Edwards25519.hs b/Crypto/ECC/Edwards25519.hs index e82ccf5..2e91fb9 100644 --- a/Crypto/ECC/Edwards25519.hs +++ b/Crypto/ECC/Edwards25519.hs @@ -80,11 +80,8 @@ instance Eq Point where -- | Generate a random scalar. scalarGenerate :: MonadRandom randomly => randomly Scalar -scalarGenerate = unwrap . scalarDecodeLong . clamp <$> generate +scalarGenerate = throwCryptoError . scalarDecodeLong . clamp <$> generate where - unwrap (CryptoPassed x) = x - unwrap (CryptoFailed _) = error "scalarGenerate: assumption failed" - generate :: MonadRandom randomly => randomly ScrubbedBytes generate = getRandomBytes 32 diff --git a/tests/ECC/Edwards25519.hs b/tests/ECC/Edwards25519.hs index 46893b0..fd887c3 100644 --- a/tests/ECC/Edwards25519.hs +++ b/tests/ECC/Edwards25519.hs @@ -94,19 +94,19 @@ tests = testGroup "ECC.Edwards25519" ] where p0 = toPoint s0 - CryptoPassed s0 = scalarDecodeLong ("" :: ByteString) - CryptoPassed s1 = scalarDecodeLong ("\x01" :: ByteString) - CryptoPassed s2 = scalarDecodeLong ("\x02" :: ByteString) - CryptoPassed sI = scalarDecodeLong ("\236\211\245\\\SUBc\DC2X\214\156\247\162\222\249\222\DC4\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\DLE" :: ByteString) - CryptoPassed sN = scalarDecodeLong ("\237\211\245\\\SUBc\DC2X\214\156\247\162\222\249\222\DC4\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\DLE" :: ByteString) + s0 = throwCryptoError $ scalarDecodeLong ("" :: ByteString) + s1 = throwCryptoError $ scalarDecodeLong ("\x01" :: ByteString) + s2 = throwCryptoError $ scalarDecodeLong ("\x02" :: ByteString) + sI = throwCryptoError $ scalarDecodeLong ("\236\211\245\\\SUBc\DC2X\214\156\247\162\222\249\222\DC4\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\DLE" :: ByteString) + sN = throwCryptoError $ scalarDecodeLong ("\237\211\245\\\SUBc\DC2X\214\156\247\162\222\249\222\DC4\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\DLE" :: ByteString) - CryptoPassed s011 = scalarDecodeLong ("\011" :: ByteString) - CryptoPassed s123 = scalarDecodeLong ("\123" :: ByteString) - CryptoPassed s134 = scalarDecodeLong ("\134" :: ByteString) + s011 = throwCryptoError $ scalarDecodeLong ("\011" :: ByteString) + s123 = throwCryptoError $ scalarDecodeLong ("\123" :: ByteString) + s134 = throwCryptoError $ scalarDecodeLong ("\134" :: ByteString) - CryptoPassed p011 = pointDecode ("\x13\x37\x03\x6a\xc3\x2d\x8f\x30\xd4\x58\x9c\x3c\x1c\x59\x58\x12\xce\x0f\xff\x40\xe3\x7c\x6f\x5a\x97\xab\x21\x3f\x31\x82\x90\xad" :: ByteString) - CryptoPassed p123 = pointDecode ("\xc4\xb8\x00\xc8\x70\x10\xf9\x46\x83\x03\xde\xea\x87\x65\x03\xe8\x86\xbf\xde\x19\x00\xe9\xe8\x46\xfd\x4c\x3c\xd0\x9c\x1c\xbc\x9f" :: ByteString) - CryptoPassed p134 = pointDecode ("\x51\x20\xab\xe0\x3c\xa2\xaf\x66\xc7\x7c\xa3\x20\xf0\xb2\x1f\xb5\x56\xf6\xb6\x5f\xdd\x7e\x32\x64\xc1\x4a\x30\xd9\x7b\xf7\xa7\x6f" :: ByteString) + p011 = throwCryptoError $ pointDecode ("\x13\x37\x03\x6a\xc3\x2d\x8f\x30\xd4\x58\x9c\x3c\x1c\x59\x58\x12\xce\x0f\xff\x40\xe3\x7c\x6f\x5a\x97\xab\x21\x3f\x31\x82\x90\xad" :: ByteString) + p123 = throwCryptoError $ pointDecode ("\xc4\xb8\x00\xc8\x70\x10\xf9\x46\x83\x03\xde\xea\x87\x65\x03\xe8\x86\xbf\xde\x19\x00\xe9\xe8\x46\xfd\x4c\x3c\xd0\x9c\x1c\xbc\x9f" :: ByteString) + p134 = throwCryptoError $ pointDecode ("\x51\x20\xab\xe0\x3c\xa2\xaf\x66\xc7\x7c\xa3\x20\xf0\xb2\x1f\xb5\x56\xf6\xb6\x5f\xdd\x7e\x32\x64\xc1\x4a\x30\xd9\x7b\xf7\xa7\x6f" :: ByteString) -- Using : --