Ejemplo n.º 1
0
// Initialization functions
bool RSA32::RandomizeKeys( const unsigned int p_seed )
{
    // You have to seed the rand function by yourself if you're not
    // providing any seed.
    if( p_seed != 0 )
    {
        srand( p_seed );
    }

    // Set p and q by generating random primes.
    m_p = 0;
    m_q = 0;

    // Do start at 46341 since it's closest to the lowest possible 32 bit number
    // by any number multiplied by itself. 46341^2 almost equals to the lowest 32 bit number...
    unsigned int prime_range_low = 46341; //32768;
    unsigned int prime_range_high = 65535;

    // Make sure that they're not equal to each other.
    while( m_p == m_q || m_p == 0 || m_q == 0 )
    {
        m_p = RandomPrime( prime_range_low, prime_range_high );
        m_q = RandomPrime( prime_range_low, prime_range_high );
    }

    // Calculate n, z, e and d by using two primes: p and q. Simple? :)
    if( !CalculateNZED( m_p, m_q ) )
    {
        return false;
    }

    return true;
}
Ejemplo n.º 2
0
int main()
{
   long NumContexts = 3;
   long NumPolys = 6;
   long n = 500;

   Vec<ZZ_pContext> context_vec;
   context_vec.SetLength(NumContexts);

   long i;
   for (i = 0; i < NumContexts; i++) { 
      ZZ p;
      RandomPrime(p, 150 + i*50);
      context_vec[i] = ZZ_pContext(p);
   }

   Vec<ZZ_pX> poly_vec;
   Vec<vec_pair_ZZ_pX_long> res_vec;
   Vec< SmartPtr<thread> > thread_vec;

   poly_vec.SetLength(NumPolys);
   res_vec.SetLength(NumPolys);
   thread_vec.SetLength(NumPolys);

   for (i = 0; i < NumPolys; i++) {
      ZZ_pPush push(context_vec[i % NumContexts]);
      random(poly_vec[i], n);
      SetCoeff(poly_vec[i], n);
   }

   cerr << "START\n";

   for (i = 0; i < NumPolys; i++) 
      thread_vec[i] = MakeSmart<thread>(task, context_vec[i % NumContexts],
                                        &poly_vec[i], &res_vec[i]);

   for (i = 0; i < NumPolys; i++) 
      thread_vec[i]->join();

   cerr << "checking results...\n";


   for (i = 0; i < NumPolys; i++) {
      ZZ_pPush push(context_vec[i % NumContexts]);
      vec_pair_ZZ_pX_long v;
      berlekamp(v, poly_vec[i]);
      if (v.length() == res_vec[i].length() && mul(v) == mul(res_vec[i]))
         cerr << i << " GOOD\n";
      else
         cerr << i << " BAD\n";
   }
}
Ejemplo n.º 3
0
NTL_CLIENT



//namespace NTL { extern double ip_time; }

int main(int argc, char **argv)
{
   ArgMapping amap;

   long n = 1024;
   amap.arg("n", n, "degree bound");

   long l = 1024;
   amap.arg("l", l, "coeff bound");

   long nt = 1;
   amap.arg("nt", nt, "num threads");

   amap.parse(argc, argv);

   cerr << "\n\n=============================\n\n";

   cerr << "n=" << n << "\n";
   cerr << "l=" << l << "\n";
   cerr << "nt=" << nt << "\n";

   SetSeed(ZZ(0));

   SetNumThreads(nt);

   ZZ p;

   RandomPrime(p, l);
   ZZ_p::init(p);


   ZZ_pX f;

   random(f, n);
   SetCoeff(f, n);

   Vec< Pair<ZZ_pX, long> > fac;


   double t;

   ZZ_pXFileThresh = 1e9;



   FILE *fp;
   unsigned long A[4], B[4];
   int loadavg;

   fp = fopen("/proc/stat","r");
   fscanf(fp,"cpu %lu %lu %lu %lu",&A[0],&A[1],&A[2],&A[3]);
   fclose(fp);
   
   
   t = GetTime();
   CanZass(fac, f, 1);
   t = GetTime()-t;
   double NTLTime = t;

   fp = fopen("/proc/stat","r");
   fscanf(fp,"cpu %lu %lu %lu %lu",&B[0],&B[1],&B[2],&B[3]);
   fclose(fp);
   
   // we multiply by 20 -- that's the total number of cores
   loadavg = int(100.0*20.0*double((B[0]+B[1]+B[2]) - (A[0]+A[1]+A[2])) / 
      double((B[0]+B[1]+B[2]+B[3]) - (A[0]+A[1]+A[2]+A[3])));
   fprintf(stderr, "CPU utilization: %d\%\n",loadavg);

   struct rusage rusage;
   getrusage( RUSAGE_SELF, &rusage );

   cerr << "MAX_RSS="<<rusage.ru_maxrss <<  "KB" << endl;
   cerr << "Fac: " << t << "\n";

   //cerr << "ip_time: " << ip_time << "\n";

   delete NTLThreadPool;
   NTLThreadPool = 0;


}