9

I have now implemented another SHA3 candidate, namely Grøstl. This is still work in progress (very much so), but at the moment a 224-bit version pass all KATs. So now I'm wondering about performance (again :->). The difference this time, is that I chose to more closely mirror the (optimized) C implementation, i.e. I made a port from C to Haskell. The optimized C version use table-lookups to implement the algorithm. Furthermore the code is heavily based on updating an array containing 64-bit words. Thus I chose to use mutable unboxed vectors in Haskell.

My Grøstl code can be found here: https://github.com/hakoja/SHA3/blob/master/Data/Digest/GroestlMutable.hs

Short description of the algorithm: It's a Merkle-Damgård construction, iterating a compression function (f512M in my code) as long as there are 512-bits blocks of message left. The compression function is very simple: it simply runs two different independent 512-bit permutations P and Q (permP and permQ in my code) and combines their output. Its these permutations which are implemented by lookup tables.

Q1) The first thing that bothers me is that the use of mutable vectors makes my code look really fugly. This is my first time writing any major mutable code in Haskell so I don't really know how to improve this. Any tips on how I might better strucure the monadic code would be welcome.

Q2) The second is performance. Actually It's not too bad, because at the moment the Haskell code is only 3 times slower. Using GHC-7.2.1 and compiling as such:

ghc -O2 -Odph -fllvm -optlo-O3 -optlo-loop-reduce -optlo-loop-deletion

the Haskell code uses 60s. on an input of ~1GB, while the C-version uses 21-22s. But there are some things I find odd:

(1) If I try to inline rnd512QM, the code takes 4 times longer, but if I inline rnd512PM nothing happens! Why is this happening? These two functions are virtually identical!

(2) This is maybe more difficult. I've been experimenting with executing the two permutations in parallel. But currently to no avail. This is one example of what I tried:

f512 h m = V.force outP `par` (V.force outQ `pseq` (V.zipWith3 xor3 h outP outQ))
   where xor3 x1 x2 x3 = x1 `xor` x2 `xor` x3
         inP = V.zipWith xor h m
         outP = permP inP
         outQ = permQ m

When checking the run-time statistics, and using ThreadScope, I noticed that the correct number of SPARKS was created, but almost none was actually converted to useful parallel work. Thus I gained nothing in speedup. My question then becomes:

  1. Are the P and Q functions just too small for the runtime to bother to run in parallel?
  2. If not, is my use of par and pseq (and possibly Vector.Unboxed.force) wrong?
  3. Would I gain anything by switching to strategies? And how would I go about doing that?

Thank you so much for your time.

EDIT:

Sorry for not providing any real benchmark tests. The testing code in the repo was just intended for myself only. For those wanting to test the code out, you will need to compile main.hs, and then run it as:

./main "algorithm" "testvariant" "byte aligned"

For instance:

./main groestl short224 False

or

./main groestl e False

(e stands for "Extreme". It's the very long message provided with the NIST KATS).

2 Answers 2

3

I checked out the repo, but there's no simple benchmark to just run and play with, so my ideas are just from eyeballing the code. Numbering is unrelated to your questions.

1) I'm pretty sure force doesn't do what you want -- it actually forces a copy of the underlying vector.

2) I think the use of unsafeThaw and unsafeFreeze is sort of odd. I'd just put f512M in the ST monad and be done with it. Then run it something like so:

otherwise = \msg -> truncate G224 . outputTransformation . runST $ foldM f512M h0_224 (parseMessage dataBitLen 512 msg)

3) V.foldM' is sort of silly -- you can just use a normal (strict) foldM over a list -- folding over the vector in the second argument doesn't seem to buy anything.

4) i'm dubious about the bangs in columnM and for the unsafeReads.

Also...

a) I suspect that xoring unboxed vectors can probably be implemented at a lower level than zipWith, making use of Data.Vector internals.

b) However, it may be better not to do this as it could interfere with vector fusion.

c) On inspection, extractByte looks slightly inefficient? Rather than using fromIntegral to truncate, maybe use mod or quot and then a single fromIntegral to take you directly to an Int.

6
  • If you compile the main function and run the following: "ghc groestl e False" you can test the timings. Its not really a solid benchmark, nor good code at all, but it is sufficient to at least run the code :)
    – hakoja
    Nov 16, 2011 at 20:24
  • As to the answers: 1) What underlying vector? The force in the example is done on a vector from the unmutable interface, is there something I'm missing here? 2) Yes, that seems better. Will change to that. But then I still escape from ST in f512M right? 3) Yeah, I was doing that first, but hoped for some fusion magic to happen with vectors 4) Agree, most of the bangPatterns where just thrown around kinda haphazardly in hope to see some effect. TBH I'm not sure when bangs should be used or not. I seem to remeber reading somewhere that "primitive" values always should be kept strict?
    – hakoja
    Nov 16, 2011 at 20:38
  • 1) sorry, underlying byte array. in any case, it shouldn't be used just to evaluate something fully. With an unboxed vector, whnf is fully evaluated anyway.
    – sclv
    Nov 16, 2011 at 20:48
  • 4) w/r/t bang patterns, you generally only need them when you think the argument/result will otherwise be lazy and you don't want it to be. in the case of the unsafereads, they're effectful and happen immediately -- there's nothing to force. ditto for the other bangs I mentioned. But while redundant, such bangs nonetheless change code generation (since the compiler can't always tell they're redundant) and generally slow things down a tad for no good reason.
    – sclv
    Nov 16, 2011 at 20:50
  • 1
    2) Actually, stick around through the ST monad for the fold of f512. If you're doing mutable stuff, might as well stay in a monad where you can instead of entering/exiting it repeatedly. Keeping pure code pure is a virtue. There's no karma in pretending impure code is less so than it really is...
    – sclv
    Nov 16, 2011 at 20:52
1
  1. Be sure to compile with -threaded -rtsopts and execute with +RTS -N2. Without that, you won't have more than one OS thread to perform computations.

  2. Try to spark computations that are referred to elsewhere, otherwise they might be collected:

_

f512 h m = outP `par` (outQ `pseq` (V.zipWith3 xor3 h outP outQ))
   where xor3 x1 x2 x3 = x1 `xor` x2 `xor` x3
         inP = V.zipWith xor h m
         outP = V.force $ permP inP
         outQ = V.force $ permQ m

_

3) If you switch things up so parseBlock accepts strict bytestrings (or chunks and packs lazy ones when needed) then you can use Data.Vector.Storable and potentially avoid some copying.

2
  • 1) Yeah, I do that :) 2) Hmm, not sure I'm following you here. What will this change achieve? 3) I will try that out. TBH the parsing and padding was 10 times more difficult to get right than the actual algorithm :) still I don't think this is the biggest bottleneck at the moment. My current profiling shows that 75% of the time is spent in tableLookup and 16% in columnM.
    – hakoja
    Nov 16, 2011 at 21:01
  • As for #2, I was afraid your sparks were getting GCed (are they?) and if so, would advise you sparce off something that is actually used later. Also, I agree with sclv - you don't want force. WHNF should be fine for Vector.{Unboxed,Storable}. Nov 16, 2011 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.