【问题标题】:Very Slow Random Number Generation using dev/urandom on Mac OS 10.8在 Mac OS 10.8 上使用 dev/urandom 生成非常慢的随机数
【发布时间】:2013-05-25 04:41:49
【问题描述】:

我正在编写一个使用 Paillier Cryptosystem 加密矩阵的程序。加密一个 50 x 50 矩阵大约需要 12 秒!考虑到我打算加密大小为 5000 x 5000 及以上的矩阵,这太长了。在 Xcode 上分析程序我发现这个 paillier_get_rand_devurandom() 是罪魁祸首。

这是呼叫跟踪快照:

这是这个特定 Paillier C 库函数的源代码

    void paillier_get_rand_devurandom( void* buf, int len )
    {
          paillier_get_rand_file(buf, len, "/dev/urandom");
    }  


    void paillier_get_rand_file( void* buf, int len, char* file )
   {
         FILE* fp;
         void* p;

         fp = fopen(file, "r");

         p = buf;
         while( len )
         {
             size_t s;
             s = fread(p, 1, len, fp);
             p += s;
             len -= s;
         }

        fclose(fp);
    }

万一,

    <http://www.en.wikipedia.org/wiki/Paillier_cryptosystem>
    Paillier Cryptosytem C library : <http://acsc.cs.utexas.edu/libpaillier/> 

我读过使用 dev/random 生成随机数很慢,而使用 dev/urandom 生成随机数要快得多。 就我而言,两者都同样慢。 这种随机数生成能不能更快?

编辑: 这是一个例子

    #include <stdio.h>
    #include <stdlib.h>
    #include <gmp.h>
    #include<paillier.h>


   int main(void)
   {

       paillier_pubkey_t* pub;//The public key
   paillier_prvkey_t* prv;//The private key 


       paillier_keygen(1024, &pub, &prv,paillier_get_rand_devurandom);


      paillier_ciphertext_t* ca;
      paillier_ciphertext_t* cb;
      paillier_ciphertext_t* res;


      paillier_plaintext_t* a;
      paillier_plaintext_t* b;
      paillier_plaintext_t* sum;



     a=paillier_plaintext_from_ui(45);
     b=paillier_plaintext_from_ui(100);


     //This is the encryption function 
     ca=paillier_enc(0, pub, a, paillier_get_rand_devurandom);
     cb=paillier_enc(0, pub, b, paillier_get_rand_devurandom);


     res=paillier_create_enc_zero();


     paillier_mul(pub, res,ca, cb);

     sum=paillier_dec(0, pub, prv, res);


     gmp_printf("The sum is : %Zd\n",sum);


     return 0;

   }

这是加密函数签名

    /*
Encrypt the given plaintext with the given public key using
randomness from get_rand for blinding. If res is not null, its
contents will be overwritten with the result. Otherwise, a new
paillier_ciphertext_t will be allocated and returned.
    */
    paillier_ciphertext_t* paillier_enc( paillier_ciphertext_t* res,
                                           paillier_pubkey_t* pub,
                        paillier_plaintext_t* pt,
                         paillier_get_rand_t get_rand );

对不起,这个问题只会越来越长

 The actual scale_encrypt_T()

 void scale_encrypt_T(char *scaledTfile)
{
    ...
    ...

//Printing scaled and then encrypted T[][] in a file 
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        void *buf2;

        //T[][] is the 50 x 50 matrix 
        temp=(int)(T[i][j]*pow(10,6));  //Scale factor q = 10 to the power of 6

        p0=paillier_plaintext_from_ui(temp);

        //c0 is the cipher text
         /***So this is where its taking so long***/
        c0=paillier_enc(0, pub, p0, paillier_get_rand_devurandom);


        buf2=paillier_ciphertext_to_bytes(PAILLIER_BITS_TO_BYTES(pub->bits)*2, c0);

        fwrite(buf2, 1, PAILLIER_BITS_TO_BYTES(pub->bits)*2, fpt);
        free(buf2);

    }

}

【问题讨论】:

  • 每次从 /dev/urandom 读取多长时间?你在做几个?您的程序是否正常工作?您的程序是否出于初始密钥生成以外的目的从 /dev/urandom 读取?什么目的?你能提供一个self-contained compilable example吗?
  • 您应该只使用 /dev/random 或 /dev/urandom 来播种或生成加密密钥。
  • @Eric dev/urandom 为每次加密调用
  • @Jim Balter 好吧,函数签名要求随机数生成器函数作为其参数之一。
  • 我运行了你的那个程序,这样就会有 2500 次调用 paillier_enc - 在我运行 Linux 的弱 AMD 无风扇机器上,每次调用需要 3.8 毫秒。如果我将 FILE* fp 设为静态并且仅在每次调用可以可靠地减少 0.3 毫秒时才打开它!我想知道 OS X 在系统调用上是否很慢,如果避免重复打开和关闭 /dev/urandom,是否可以做得更好。

标签: c random xcode4 cryptography gmp


【解决方案1】:

假设 n 为 50,您的代码将执行 2500 次单独的加密。每次加密都可能需要一些随机数据。

通过在一次调用paillier_enc 中加密整个数组来进行一次加密会更有效。我不熟悉 libpaillier API,但看起来您可以通过将 50×50 数组的所有数据安排在连续内存中来做到这一点,然后使用 paillier_plaintext_from_bytes 将整个数组打包成 @ 987654324@ 对象,然后将pallier_enc 与该对象一起使用。

【讨论】:

  • 非常感谢。我会试试这个然后回来。
猜你喜欢
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2011-07-23
相关资源
最近更新 更多