From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p2OCHh44017482 for ; Thu, 24 Mar 2011 13:17:43 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtwCAOjSik1KfVI0imdsb2JhbAClPAgVAQsJFBIGIaceilSCH4UfL4hcAQEDBYVkBIU1hzqJBjo X-IronPort-AV: E=Sophos;i="4.63,236,1299452400"; d="scan'208";a="94768872" Received: from mail-ww0-f52.google.com ([74.125.82.52]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 24 Mar 2011 13:17:38 +0100 Received: by wwj40 with SMTP id 40so12669200wwj.9 for ; Thu, 24 Mar 2011 05:17:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=Q1Oi7zscQZ99/VrDGWGnpKqSV1qmUON+Fw26jpJWmAY=; b=LO+NQ98LdYGblWRhU+u3xJe+NjlKRX6qZyj9n5hu9lsiGyx86lI8g4zEwY2g4iFSL2 gI7ff7nbglEj8nYUa4hdaNT7bgEOG+Ja2frE6XrVXqAm8fgkUaGfgUvecZrynC6bGSdF ySbh11iYNwZR1GvXusLBM/tBPwP9n7u2WunAs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=DxIkE41rYIeCjVLvhYQzORj4PIv9duOp5wDLUpjhvOEwWOCkuQyq85lpav4ndtUPgs OvQeu+LVExoTi4PXSgPezpACvfNesPmvQAP4gqCIgfIjULvDLesHP0QdEcCokwD+2tL0 vGtsXEkmWMhR+7/pILm+bheO3UUjuqbA8x8GM= MIME-Version: 1.0 Received: by 10.216.145.200 with SMTP id p50mr636136wej.79.1300969057790; Thu, 24 Mar 2011 05:17:37 -0700 (PDT) Received: by 10.216.188.196 with HTTP; Thu, 24 Mar 2011 05:17:37 -0700 (PDT) Date: Thu, 24 Mar 2011 21:17:37 +0900 Message-ID: From: =?ISO-8859-1?Q?Jehan_Pag=E8s?= To: caml-list Content-Type: text/plain; charset=ISO-8859-1 Subject: [Caml-list] Ocaml and cryptography 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. ;-)