commit 21be7b8655da65e8da88fa89ad155b5d91bf5885
parent 569dbfcc9e9952a74b8b45d582ad4bd88d070748
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 11 Jun 2025 11:55:15 +0400
lib: total functions
Diffstat:
4 files changed, 15 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
@@ -21,7 +21,7 @@ A sample GHCi session:
> let key = "i'll never use this key again!!!"
> let msg = "i am a message that is in need of authentication"
> Poly1305.mac key msg
- "\247\247\GSZ^\140\168\r\177\197\242\182b#\210g"
+ Just "\247\247\GSZ^\140\168\r\177\197\242\182b#\210g"
```
## Documentation
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -22,6 +22,10 @@ key_small :: BS.ByteString
key_small = fromJust . B16.decode $
"0000000000000000000000000000000000000000000000000000000000000003"
+key_mid :: BS.ByteString
+key_mid = fromJust . B16.decode $
+ "8888888888888888888888888888888888888888888888888888888888888883"
+
key_big :: BS.ByteString
key_big = fromJust . B16.decode $
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff3"
@@ -30,6 +34,7 @@ suite :: Benchmark
suite =
bgroup "ppad-poly1305" [
bench "mac (small key)" $ nf (Poly1305.mac key_small) msg
+ , bench "mac (mid key)" $ nf (Poly1305.mac key_mid) msg
, bench "mac (big key)" $ nf (Poly1305.mac key_big) msg
]
diff --git a/lib/Crypto/MAC/Poly1305.hs b/lib/Crypto/MAC/Poly1305.hs
@@ -68,16 +68,16 @@ clamp r = r .&. 0x0ffffffc0ffffffc0ffffffc0fffffff
-- key will cause the function to throw an ErrorCall exception.
--
-- >>> mac "i'll never use this key again!!!" "a message needing authentication"
--- "O'\231Z\224\149\148\246\203[}\210\203\b\200\207"
+-- Just "O'\231Z\224\149\148\246\203[}\210\203\b\200\207"
mac
:: BS.ByteString -- ^ 256-bit one-time key
-> BS.ByteString -- ^ arbitrary-length message
- -> BS.ByteString -- ^ 128-bit message authentication code
+ -> Maybe BS.ByteString -- ^ 128-bit message authentication code
mac key@(BI.PS _ _ kl) msg
- | kl /= 32 = error "ppad-poly1305 (mac): invalid key"
+ | kl /= 32 = Nothing
| otherwise =
let (clamp . _roll -> r, _roll -> s) = BS.splitAt 16 key
- in _poly1305_loop r s msg
+ in pure (_poly1305_loop r s msg)
_poly1305_loop :: Integer -> Integer -> BS.ByteString -> BS.ByteString
_poly1305_loop !r !s !msg =
diff --git a/test/Main.hs b/test/Main.hs
@@ -34,7 +34,7 @@ mac = H.testCase "mac" $ do
Just e = B16.decode "a8061dc1305136c6c22b8baf0c0127a9"
- o = Poly1305.mac key msg
+ Just o = Poly1305.mac key msg
H.assertEqual mempty e o
mac1 :: TestTree
@@ -45,7 +45,7 @@ mac1 = H.testCase "mac (A.3 #1)" $ do
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
Just tag = B16.decode
"00000000000000000000000000000000"
- out = Poly1305.mac key msg
+ Just out = Poly1305.mac key msg
H.assertEqual mempty tag out
mac2 :: TestTree
@@ -56,7 +56,7 @@ mac2 = H.testCase "mac (A.3 #2)" $ do
"416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f"
Just tag = B16.decode
"36e5f6b5c5e06070f0efca96227a863e"
- out = Poly1305.mac key msg
+ Just out = Poly1305.mac key msg
H.assertEqual mempty tag out
mac3 :: TestTree
@@ -67,7 +67,7 @@ mac3 = H.testCase "mac (A.3 #3)" $ do
"416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207768696368206172652061646472657373656420746f"
Just tag = B16.decode
"f3477e7cd95417af89a6b8794c310cf0"
- out = Poly1305.mac key msg
+ Just out = Poly1305.mac key msg
H.assertEqual mempty tag out
mac4 :: TestTree
@@ -78,7 +78,7 @@ mac4 = H.testCase "mac (A.3 #4)" $ do
"2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e"
Just tag = B16.decode
"4541669a7eaaee61e708dc7cbcc5eb62"
- out = Poly1305.mac key msg
+ Just out = Poly1305.mac key msg
H.assertEqual mempty tag out
mac5 :: TestTree