proj home

Files   Classes   Functions   Hierarchy  

largePrimeGenerator Class Reference

Generate n-bit prime numbers. More...

#include <primegen.h>

Collaboration diagram for largePrimeGenerator:

List of all members.

Public Member Functions

 largePrimeGenerator ()
 Constructor generates a small prime table.
 ~largePrimeGenerator ()
void findprime_sequentially (ZZ &n, uintc nbits) const
void findprime_randomly (ZZ &n, uintc nbits) const
 Keep looking for primes randomly till one found.
bool const isprime (ZZ const &n) const
 Statistical prime test.
void ensurenbits (ZZ &n, uintc nbits) const
 n is >= 2^nbits.


Detailed Description

Generate n-bit prime numbers.

The library could do many of these functions but these functions are so easily to implement that another library could be easily used instead, so the library calls are isolated.

ie ZZ could be templated, random bit generation implemented and prime testing implemented then the only dependency is ZZ.

Definition at line 21 of file primegen.h.


Constructor & Destructor Documentation

largePrimeGenerator::largePrimeGenerator (  ) 

Constructor generates a small prime table.

Definition at line 64 of file primegen.cpp.

00065   : v(0), vmax(1000)
00066 {
00067   PrimeSeq s;
00068 
00069   v = new long int [vmax];
00070 
00071   for (uint i=0; i<vmax; ++i)
00072     v[i] = s.next();
00073 }

largePrimeGenerator::~largePrimeGenerator (  ) 

Definition at line 75 of file primegen.cpp.

00076 {
00077   delete[] v;
00078   v=0;
00079 }


Member Function Documentation

void largePrimeGenerator::ensurenbits ( ZZ &  n,
uintc  nbits 
) const

n is >= 2^nbits.

Definition at line 7 of file primegen.cpp.

00008 {
00009   ZZ n2;
00010   ZZ x;
00011   conv(x,"2"); 
00012   power(n2,x,nbits);
00013   if (n<n2)
00014     n += n2;
00015  
00016   if ((n%2)==0)
00017     ++n;
00018 }

void largePrimeGenerator::findprime_randomly ( ZZ &  n,
uintc  nbits 
) const

Keep looking for primes randomly till one found.

Definition at line 21 of file primegen.cpp.

Referenced by rsaGenKey::rsaGenKey(), and rsatest::test02().

00025 {
00026   for ( ; ; )
00027   {
00028     RandomBits(n,nbits);
00029     ensurenbits(n,nbits);
00030     if (isprime(n))
00031       return;
00032   }
00033 }

void largePrimeGenerator::findprime_sequentially ( ZZ &  n,
uintc  nbits 
) const

Definition at line 37 of file primegen.cpp.

Referenced by rsatest::test02().

00041 {
00042   RandomBits(n,nbits);
00043   ensurenbits(n,nbits);
00044 
00045   for ( ; ; )
00046   {
00047     if (isprime(n))
00048       return;
00049 
00050     n += 2;
00051   }
00052 }

bool const largePrimeGenerator::isprime ( ZZ const &  n  )  const

Statistical prime test.

Do not use with small primes.

Definition at line 54 of file primegen.cpp.

00055 {
00056   for (uint i=0; i<vmax; ++i)
00057     if ((n%v[i])==0)
00058       return false;
00059 
00060   return ProbPrime(n,15);
00061 }


The documentation for this class was generated from the following files:

Generated on Fri Mar 4 00:50:04 2011 for Chelton Evans Source by  doxygen 1.5.8