Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:05:51+00:00 2026-06-01T17:05:51+00:00

Note: for those of you that cannot do better than coming up with boring,

  • 0

Note:
for those of you that cannot do better than coming up with boring, witless comments and even suggestions to close a valid question, please see the accepted answer here: Using GNU/Linux system call `splice` for zero-copy Socket to Socket data transfers in Haskell as an excellent example of how to be of proper help to those that really seek constructive answers!!


Hi I was just reading PowerMod in Mathematica 8’s documentation and wanted to test the Haksell RSA package (ghc --make -O2 -O3 -fllvm -optlo-O3 test.hs):

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad
import System.Random
import Codec.Crypto.RSA
import Data.ByteString.Lazy
import Data.ByteString.Char8

import Criterion.Main
import Criterion.Config

main :: IO ()
main = do
  print m1
  print m4
  print m8
  defaultMainWith defaultConfig (return ()) [
    bgroup "RSA" [
       bench "1" $ ed m1
     , bench "4" $ ed m4
     , bench "8" $ ed m8
     ]
   ]

m1 = fromChunks [ Data.ByteString.Char8.replicate (1*1024) '0' ]
m4 = fromChunks [ Data.ByteString.Char8.replicate (4*1024) '0' ]
m8 = fromChunks [ Data.ByteString.Char8.replicate (8*1024) '0' ]

ed m = do
  g1 <- newStdGen
  let (el,il,g2) = generateKeyPair g1 1024
  loop 1 g2 el il m

loop :: RandomGen g => Int -> g -> PublicKey -> PrivateKey -> Data.ByteString.Lazy.ByteString -> IO ()
loop n g e i m = do
  let   nn     = n-1
  let  (em,ng) = encrypt g e  m
  let   dm     = decrypt   i em
  when (m == dm) $ Data.ByteString.Char8.putStr "1"
  when (nn > 0 ) $ loop nn ng e i m

Also tried this in Mathematica:

{p, q} = Prime[RandomInteger[{10^4, 10^5}, {2}]];
{p, q, n = p q}
\[Lambda] = CarmichaelLambda[n]
d = NestWhile[#1 + 1 & , Round[n/3], GCD[\[Lambda], #1] =!= 1 &]
e = PowerMod[d, -1, \[Lambda]]
enc = PowerMod[#, e, n] &;
dec = PowerMod[#, d, n] &;
c = ConstantArray[48, 8 1024];
t = Table[c // enc // dec; // AbsoluteTiming, {10}][[All, 1]]

Timings both in Haskell (m8) and Mathematica cases are similar:

{0.313015, 0.302337, 0.303766, 0.303321, 0.303018, 0.302574, \
0.302511, 0.303958, 0.301411, 0.300820}

Is 300ms per 8192-bytes-long message an acceptable performance for RSA? How do OpenSSL or other implementations compare?

(Test rig: 64-bit linux; 4xCORE, Intel(R) Core(TM) i5 CPU M 430 @ 2.27GHz)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:05:53+00:00Added an answer on June 1, 2026 at 5:05 pm

    First off, good question – the performance difference of RSA to OpenSSL is a question I had too. That said, here’s a bunch of text that doesn’t give the answer.

    The Haskell RSA Package Changed

    I’ve recently moved RSA to using CryptoRandomGen from RandomGen. You are using the painfully slow StdGen so switching to the generator in the intel-aes package or HashDRBG (perhaps a buffered version) from the DRBG package will help.

    This is not how you’re supposed to use Public Key Cryptography

    Generally you use public keys to either exchange a secret key or encrypt a secret key such that only the recipient can decrypt it. You seem to be intending to use RSA to continually encrypt a stream of messages. The performance of RSA is of such little concern to people precisely because it is such a rare operation.

    Proper Benchmarking

    As Daniel said, you are currently benchmarking key generation, encryption and decryption all in one batch. You responded that you won’t be generating many keys, just doing lots of enc/dec operations… so don’t you think you should fix the benchmark?

    Also, you’re benchmark seems incomplete and thus suspect – at the very least it’s missing an import.

    Other Alarming Things

    You say “Randomnes of key pairs [are] of no importance at the moment.” Until they are important, there is no reason bothering with cryptography.

    Benchmarking
    Oli also had a good point. Benchmarking OpenSSL is the way to go.

    From the command line (which as far as I’m going with the part of the answer) OpenSSL forces you to use RSA semi-correctly, so we’ll just be benchmarking encryption of really small files:

    dd if=/dev/urandom of=64B bs=64 count=1
    openssl genrsa -out test.key 1024
    openssl rsa -in test.key -out public.pem -outform PEM -pubout
    openssl rsa -in test.key -out private.pem -outform PEM
    time openssl rsautl -raw -ssl -encrypt -inkey private.pem -in 64B -out 64B.enc
    

    Which gives us anywhere from 5 to 12 ms.

    Now for the Haskell. Aside from cosmetic changes, I’ve moved to the new RSA using CryptoRandomGen and the not-so-fast but OK HashDRBG generator at the same time as making your encrypt function pure and ditching the unneeded comparison. We end up with:

    import Criterion.Main
    import Criterion.Config
    import qualified Data.ByteString as B
    import qualified Data.ByteString.Lazy as L
    import Codec.Crypto.RSA
    import Crypto.Random.DRBG
    
    main :: IO ()
    main = do
      r1 <- newGenIO :: IO HashDRBG
      r2 <- newGenIO :: IO (GenBuffered HashDRBG)
    
      -- We don't care about the performance of generate, so we do it outside the benchmark framework
      let (pub,priv,g2) = generateKeyPair r2 1024
    
      defaultMainWith defaultConfig (return ()) [
        bgroup "RSA" [
           bench "1" $ whnf (enc r1 pub priv) m1
           , bench "2" $ whnf (enc r2 pub priv) m1
         ]
       ]
    
    m1 :: L.ByteString
    m1 = L.pack [0..63]
    
    enc :: CryptoRandomGen g => g -> PublicKey -> PrivateKey -> L.ByteString -> L.ByteString
    enc g pub priv m = 
        let (em,ng) = encrypt g pub m
            dm     = decrypt   priv em 
        in dm
    

    This yields measurements around 3.5ms (compiled with GHC 7.4 and -O2). To be clear: I’m not saying RSA is faster than OpenSSL – the OpenSSL test had a LOT more overhead (loading the executable, reading the key, reading the plaintext, encrypting, writing the result) and it very believably could be an order of magnitude faster than the RSA package. What I am saying is “Hey look, the Haskell RSA code performed arbitrarily fast to the point where I don’t really care and you can perfect the benchmark more if you’d like.”

    For reference, openssl speed rsa1024 says it sign’s in 0.5 ms (on my machine, obviously), which I suspect is an RSA encrypt of 16 bytes along with other operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: The restriction is that I cannot use exceptions (the code is eventually compiled
How would you do this PHP switch statement? Also note that these are much
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
Note that I am not asking which to choose (MVC or MVP), but rather
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
(Note: I cannot change structure of the XML I receive. I am only able
I have several (more than 20) methods ( getXXX() ) that may throw an
Note : Before asking this question I did an exhaustive search, and found little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.