sha256

Pure Haskell SHA-256, HMAC-SHA256 as specified by RFC's 6234 and 2104.
git clone git://git.ppad.tech/sha256.git
Log | Files | Refs | README | LICENSE

commit 942786886f21f063fdc21ce131e35ac2d8c622f2
parent 7bde26893b2985cbf39fd9992d5a978b54336c6b
Author: Jared Tobin <jared@jtobin.io>
Date:   Fri, 13 Sep 2024 17:25:52 +0400

lib: employ 'maj' optimisation

Diffstat:
Mlib/Crypto/Hash/SHA256.hs | 11++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/lib/Crypto/Hash/SHA256.hs b/lib/Crypto/Hash/SHA256.hs @@ -104,8 +104,17 @@ pad_lazy (BL.toChunks -> m) = BL.fromChunks (walk 0 m) where ch :: Word32 -> Word32 -> Word32 -> Word32 ch x y z = (x .&. y) `B.xor` (B.complement x .&. z) +-- credit to SHA authors for the following optimisation. their text: +-- +-- > note: +-- > the original functions is (x & y) ^ (x & z) ^ (y & z) +-- > if you fire off truth tables, this is equivalent to +-- > (x & y) | (x & z) | (y & z) +-- > which you can the use distribution on: +-- > (x & (y | z)) | (y & z) +-- > which saves us one operation. maj :: Word32 -> Word32 -> Word32 -> Word32 -maj x y z = (x .&. y) `B.xor` (x .&. z) `B.xor` (y .&. z) +maj x y z = (x .&. (y .|. z)) .|. (y .&. z) bsig0 :: Word32 -> Word32 bsig0 x = B.rotateR x 2 `B.xor` B.rotateR x 13 `B.xor` B.rotateR x 22