Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add (experimental) Whirlpool support #7

Merged
merged 3 commits into from

1 participant

@ghost

Hi,

I've just added experimental support for the Whirlpool hash. I copied the reference implementation (which is in the public domain), removed some cruft, and derived bindings. I hope it is good enough for inclusion in the cryptohash library.

Sincerely,

Stijn van Drongelen

@vincenthz
Owner

Thanks, it's looking good !

i quite like to see a bit more cleanup on the C side before pulling this:

  • removing more cruft (portability workaround from old stuff)
  • remove redefinitions of standard types (u32/u64 in favor of uint32_t uint64_t)
  • use optimised rot function (rotl32_be, ..) as defined in bitfn.h
@ghost

Done, done, and skipped for now. The rotation functions in whirlpool_nessie.h were unused. Optimizing Whirlpool would require more painful tricks than rotation functions, as far as I can see.

I also noticed that struct field alignment is a problem when trying to predict the context struct size (8 bit fields take up 32 bits, for faster access). This problem may deserve its own ticket.

@ghost

I just finished some simple optimizations of Whirlpool. Benchmarks at b4f1814:

  • 256b: 18.3 us
  • 4Kib: 235.6 us
  • 1Mib: 58.8 ms

Benchmarks at cde446f:

  • 256b: 10.4 us
  • 4Kib: 138.6 us
  • 1Mib: 35.1 ms

The statistics from Criterion look borked (192 outliers among 100 samples?), but at least the means seem useful. I credit the performance gains to using array_copy64 and byte indices, instead of using per-element assignments and shifting 64-bit numbers to form indices.

@vincenthz
Owner

sorry i completely forgot about this. I'll have a look soon.

@vincenthz vincenthz merged commit cde446f into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 11, 2012
  1. Whirlpool: renamed functions and structs

    Stijn van Drongelen authored
  2. Merge branch 'master' of https://github.com/Tinctorius/hs-cryptohash

    Stijn van Drongelen authored
Commits on Aug 12, 2012
  1. Whirlpool: optimized transformMatrix by directly addressing bytes

    Stijn van Drongelen authored
This page is out of date. Refresh to see the latest.
Showing with 51 additions and 4 deletions.
  1. +3 −3 Crypto/Hash/Whirlpool.hs
  2. +47 −0 cbits/whirlpool.c
  3. +1 −1  cryptohash.cabal
View
6 Crypto/Hash/Whirlpool.hs
@@ -73,13 +73,13 @@ instance Storable Ctx where
peek ptr = create sizeCtx (\bptr -> memcpy bptr (castPtr ptr) (fromIntegral sizeCtx)) >>= return . Ctx
-foreign import ccall unsafe "whirlpool.h NESSIEinit"
+foreign import ccall unsafe "whirlpool.h whirlpool_init"
c_whirlpool_init :: Ptr Ctx -> IO ()
-foreign import ccall "whirlpool.h NESSIEadd"
+foreign import ccall "whirlpool.h whirlpool_add"
c_whirlpool_update :: CString -> Word32 -> Ptr Ctx -> IO ()
-foreign import ccall unsafe "whirlpool.h NESSIEfinalize"
+foreign import ccall unsafe "whirlpool.h whirlpool_finalize"
c_whirlpool_finalize :: Ptr Ctx -> CString -> IO ()
allocInternal :: (Ptr Ctx -> IO a) -> IO a
View
47 cbits/whirlpool.c
@@ -628,6 +628,51 @@ static const uint64_t rc[R + 1] = {
LL(0xca2dbf07ad5a8333),
};
+/* This might not be true for all platforms and compilers. */
+#define SANE_ARRAY_PACKING 1
+
+#if defined(SANE_ARRAY_PACKING) && (defined(ARCH_IS_LITTLE_ENDIAN) || defined(ARCH_IS_BIG_ENDIAN))
+
+#if defined(ARCH_IS_LITTLE_ENDIAN)
+#define LB(n) do { L[n] = C0[mu[((n+0)%8) * 8 + 7]] \
+ ^ C1[mu[((n+7)%8) * 8 + 6]] \
+ ^ C2[mu[((n+6)%8) * 8 + 5]] \
+ ^ C3[mu[((n+5)%8) * 8 + 4]] \
+ ^ C4[mu[((n+4)%8) * 8 + 3]] \
+ ^ C5[mu[((n+3)%8) * 8 + 2]] \
+ ^ C6[mu[((n+2)%8) * 8 + 1]] \
+ ^ C7[mu[((n+1)%8) * 8 + 0]] \
+ ; } while (0)
+#else
+#define LB(n) do { L[n] = C0[mu[((n+0)%8) * 8 + 0]] \
+ ^ C1[mu[((n+7)%8) * 8 + 1]] \
+ ^ C2[mu[((n+6)%8) * 8 + 2]] \
+ ^ C3[mu[((n+5)%8) * 8 + 3]] \
+ ^ C4[mu[((n+4)%8) * 8 + 4]] \
+ ^ C5[mu[((n+3)%8) * 8 + 5]] \
+ ^ C6[mu[((n+2)%8) * 8 + 6]] \
+ ^ C7[mu[((n+1)%8) * 8 + 7]] \
+ ; } while (0)
+#endif
+
+static void transformMatrix(uint64_t m[8]) {
+ uint64_t L[8];
+ uint8_t *mu = (uint8_t*)m;
+
+ LB(0);
+ LB(1);
+ LB(2);
+ LB(3);
+ LB(4);
+ LB(5);
+ LB(6);
+ LB(7);
+
+ array_copy64(m, L, 8);
+}
+
+#else
+
static void transformMatrix(uint64_t m[8]) {
uint64_t L[8];
@@ -707,6 +752,8 @@ static void transformMatrix(uint64_t m[8]) {
array_copy64(m, L, 8);
}
+#endif
+
static void inplaceXor(uint64_t dst[8], uint64_t src[8]) {
dst[0] ^= src[0];
dst[1] ^= src[1];
View
2  cryptohash.cabal
@@ -22,7 +22,7 @@ extra-source-files:
cbits/bitfn.h cbits/md2.h cbits/md4.h cbits/md5.h
cbits/ripemd.h cbits/sha1.h cbits/sha256.h cbits/sha512.h
cbits/skein.h cbits/skein256.h cbits/skein512.h
- cbits/tiger.h cbits/whirlpool.h cbits/whirlpool_nessie.h
+ cbits/tiger.h cbits/whirlpool.h
Flag test
Description: Build unit test
Something went wrong with that request. Please try again.