caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Ocaml and cryptography
@ 2011-03-24 12:17 Jehan Pagès
  2011-03-24 13:26 ` Gerd Stolpmann
  2011-03-24 16:51 ` Francis Dupont
  0 siblings, 2 replies; 8+ messages in thread
From: Jehan Pagès @ 2011-03-24 12:17 UTC (permalink / raw)
  To: caml-list

Hi,

I was just having a few thoughts/questions.

I have developed, in the context of another project, a Sha1
implementation (also a HMAC implementation and above this all a SASL
implementation with SCRAM-SHA1 mechanism support). That's not a
binding to any existent library and is fully native Ocaml. No C in
particular.

That's all working fine and is fast enough for being comfortable. But
that's definitely not as fast as a C implementation.
I made a small benchmark with OpenSSL's SHA1 functions and mine. Both
tests are loops running 10.000 Sha1 of the same string, then exiting.
Basically C goes around 6-10 times faster.

Note: my machine is a small notebook which is not powerful enough to
play most video games, nor even watch videos when they have a little
too high quality (and I am not talking of HD)! So on common desktop
machines, the test should go much faster for both version, but I guess
keep the same speed proportions. You might want to raise the loop to
100.000 though in order to see the difference.

Here are examples of such a run checked with time (there is some
variance between 2 runs, but the Ocaml version usually computes the
10.000 hashes in around 0.4 seconds while the C version computes them
in about 0.06 seconds):

~/SHA1$ time ./obench

real	0m0.416s
user	0m0.400s
sys	0m0.000s
~/SHA1$ time ./cbench

real	0m0.069s
user	0m0.060s
sys	0m0.004s

I checked the standard lib code and the MD5 code has been written in C
over there. Apparently the cryptokit library as well is writing core
cryptography in C.

I still think my code is pretty clean. I have made as much
optimization as I saw possible and though maybe other may be able to
still optimize it, I wonder up to which point now.

I am doing all computations over Int32 because the Sha1 algorithm
works over 4 octets words. At first, I was doing the "naive" approach
and was working on strings directly (going back and forth from a
character code to the character) but that was like extremely slow
(really). Still it allowed me to get the first working implementation.

Implementing with int, which is supposed to be much faster than int32,
would be nice (even though integers can be 64 bits on a 64 bits
platform, I can just mask the 4 higher octets in such case), but int
in OCaml is actually 31/63 bits because of the flag bit so I cannot
represent SHA1 words with int.

For those interested, you can download the benchmark (which includes
the code of Sha1) here:
http://download.tuxfamily.org/ocamlxmpp/sha1_benchmark.tar.gz (just
3ko).
Just run make in the SHA1 directory which will be uncompressed, it
will create cbench and obench (a SSL library is needed for the C
benchmark, likely OpenSSL or other with the same API. Nothing external
is needed for the Ocaml one).

Maybe anyone has suggestions to improve?
Or Ocaml simply cannot compete with C here? (I don't mean to do
better, but getting closer would be already nice)

Thanks.

Jehan

P.S.: don't tell me there are already libraries out there which binds
to C libraries, so why did I ever bother write this Module. I did
this:
1/ for fun of writing a full SHA1 implementation in Ocaml.
2/ because I was wondering how fast it could be.
3/ No reason.
4/ Because and that's all. ;-)

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-03-28 19:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-24 12:17 [Caml-list] Ocaml and cryptography Jehan Pagès
2011-03-24 13:26 ` Gerd Stolpmann
2011-03-26  6:44   ` Jehan Pagès
2011-03-28 19:13     ` Gerd Stolpmann
2011-03-24 16:51 ` Francis Dupont
2011-03-26  6:47   ` Jehan Pagès
2011-03-26  9:06     ` Anil Madhavapeddy
2011-03-26 11:53       ` Jehan Pagès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).