Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Add (experimental) Whirlpool support #7

Merged
merged 3 commits into from over 1 year ago

1 participant

Vincent Hanquez
Deleted user

The content you are editing has changed. Reload the page and try again.

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

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Vincent Hanquez
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
Deleted user

The content you are editing has changed. Reload the page and try again.

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.

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Deleted user

The content you are editing has changed. Reload the page and try again.

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.

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Vincent Hanquez
Owner

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

Vincent Hanquez vincenthz merged commit cde446f into from October 29, 2012
Vincent Hanquez vincenthz closed this October 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Aug 11, 2012
Whirlpool: renamed functions and structs 0b757c7
Merge branch 'master' of https://github.com/Tinctorius/hs-cryptohash 308975b
Aug 12, 2012
Whirlpool: optimized transformMatrix by directly addressing bytes cde446f
This page is out of date. Refresh to see the latest.
6  Crypto/Hash/Whirlpool.hs
@@ -73,13 +73,13 @@ instance Storable Ctx where
73 73
 
74 74
 	peek ptr = create sizeCtx (\bptr -> memcpy bptr (castPtr ptr) (fromIntegral sizeCtx)) >>= return . Ctx
75 75
 
76  
-foreign import ccall unsafe "whirlpool.h NESSIEinit"
  76
+foreign import ccall unsafe "whirlpool.h whirlpool_init"
77 77
 	c_whirlpool_init :: Ptr Ctx -> IO ()
78 78
 
79  
-foreign import ccall "whirlpool.h NESSIEadd"
  79
+foreign import ccall "whirlpool.h whirlpool_add"
80 80
 	c_whirlpool_update :: CString -> Word32 -> Ptr Ctx -> IO ()
81 81
 
82  
-foreign import ccall unsafe "whirlpool.h NESSIEfinalize"
  82
+foreign import ccall unsafe "whirlpool.h whirlpool_finalize"
83 83
 	c_whirlpool_finalize :: Ptr Ctx -> CString -> IO ()
84 84
 
85 85
 allocInternal :: (Ptr Ctx -> IO a) -> IO a
47  cbits/whirlpool.c
@@ -628,6 +628,51 @@ static const uint64_t rc[R + 1] = {
628 628
     LL(0xca2dbf07ad5a8333),
629 629
 };
630 630
 
  631
+/* This might not be true for all platforms and compilers. */
  632
+#define SANE_ARRAY_PACKING 1
  633
+
  634
+#if defined(SANE_ARRAY_PACKING) && (defined(ARCH_IS_LITTLE_ENDIAN) || defined(ARCH_IS_BIG_ENDIAN))
  635
+
  636
+#if defined(ARCH_IS_LITTLE_ENDIAN)
  637
+#define LB(n)  do { L[n] = C0[mu[((n+0)%8) * 8 + 7]] \
  638
+                         ^ C1[mu[((n+7)%8) * 8 + 6]] \
  639
+                         ^ C2[mu[((n+6)%8) * 8 + 5]] \
  640
+                         ^ C3[mu[((n+5)%8) * 8 + 4]] \
  641
+                         ^ C4[mu[((n+4)%8) * 8 + 3]] \
  642
+                         ^ C5[mu[((n+3)%8) * 8 + 2]] \
  643
+                         ^ C6[mu[((n+2)%8) * 8 + 1]] \
  644
+                         ^ C7[mu[((n+1)%8) * 8 + 0]] \
  645
+                         ; } while (0)
  646
+#else
  647
+#define LB(n)  do { L[n] = C0[mu[((n+0)%8) * 8 + 0]] \
  648
+                         ^ C1[mu[((n+7)%8) * 8 + 1]] \
  649
+                         ^ C2[mu[((n+6)%8) * 8 + 2]] \
  650
+                         ^ C3[mu[((n+5)%8) * 8 + 3]] \
  651
+                         ^ C4[mu[((n+4)%8) * 8 + 4]] \
  652
+                         ^ C5[mu[((n+3)%8) * 8 + 5]] \
  653
+                         ^ C6[mu[((n+2)%8) * 8 + 6]] \
  654
+                         ^ C7[mu[((n+1)%8) * 8 + 7]] \
  655
+                         ; } while (0)
  656
+#endif
  657
+
  658
+static void transformMatrix(uint64_t m[8]) {
  659
+    uint64_t L[8];
  660
+    uint8_t *mu = (uint8_t*)m;
  661
+    
  662
+    LB(0);
  663
+    LB(1);
  664
+    LB(2);
  665
+    LB(3);
  666
+    LB(4);
  667
+    LB(5);
  668
+    LB(6);
  669
+    LB(7);
  670
+
  671
+    array_copy64(m, L, 8);
  672
+}
  673
+
  674
+#else
  675
+
631 676
 static void transformMatrix(uint64_t m[8]) {
632 677
     uint64_t L[8];
633 678
     
@@ -707,6 +752,8 @@ static void transformMatrix(uint64_t m[8]) {
707 752
     array_copy64(m, L, 8);
708 753
 }
709 754
 
  755
+#endif
  756
+
710 757
 static void inplaceXor(uint64_t dst[8], uint64_t src[8]) {
711 758
     dst[0] ^= src[0];
712 759
     dst[1] ^= src[1];
2  cryptohash.cabal
@@ -22,7 +22,7 @@ extra-source-files:
22 22
   cbits/bitfn.h cbits/md2.h cbits/md4.h cbits/md5.h
23 23
   cbits/ripemd.h cbits/sha1.h cbits/sha256.h cbits/sha512.h
24 24
   cbits/skein.h cbits/skein256.h cbits/skein512.h
25  
-  cbits/tiger.h cbits/whirlpool.h cbits/whirlpool_nessie.h
  25
+  cbits/tiger.h cbits/whirlpool.h
26 26
 
27 27
 Flag test
28 28
   Description:       Build unit test
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.