summaryrefslogtreecommitdiff
path: root/nbtheory.cpp
diff options
context:
space:
mode:
authorweidai <weidai11@users.noreply.github.com>2003-07-29 01:18:33 +0000
committerweidai <weidai11@users.noreply.github.com>2003-07-29 01:18:33 +0000
commit9c5c4769a9c7a16aecc6cc7dd297bfe243dd022f (patch)
tree7a78ed3becd2c14f449d1cc66a58edd414256d94 /nbtheory.cpp
parent259ee22eba4e4e6471caa088479a93fbee894e11 (diff)
downloadcryptopp-git-9c5c4769a9c7a16aecc6cc7dd297bfe243dd022f.tar.gz
fix potential threading problem with initialization of static objects
Diffstat (limited to 'nbtheory.cpp')
-rw-r--r--nbtheory.cpp57
1 files changed, 33 insertions, 24 deletions
diff --git a/nbtheory.cpp b/nbtheory.cpp
index 013e4419..7d857fbf 100644
--- a/nbtheory.cpp
+++ b/nbtheory.cpp
@@ -15,36 +15,39 @@ NAMESPACE_BEGIN(CryptoPP)
const word s_lastSmallPrime = 32719;
-std::vector<word16> * NewPrimeTable()
+struct NewPrimeTable
{
- const unsigned int maxPrimeTableSize = 3511;
+ std::vector<word16> * operator()() const
+ {
+ const unsigned int maxPrimeTableSize = 3511;
- std::auto_ptr<std::vector<word16> > pPrimeTable(new std::vector<word16>);
- std::vector<word16> &primeTable = *pPrimeTable;
- primeTable.reserve(maxPrimeTableSize);
+ std::auto_ptr<std::vector<word16> > pPrimeTable(new std::vector<word16>);
+ std::vector<word16> &primeTable = *pPrimeTable;
+ primeTable.reserve(maxPrimeTableSize);
- primeTable.push_back(2);
- unsigned int testEntriesEnd = 1;
-
- for (unsigned int p=3; p<=s_lastSmallPrime; p+=2)
- {
- unsigned int j;
- for (j=1; j<testEntriesEnd; j++)
- if (p%primeTable[j] == 0)
- break;
- if (j == testEntriesEnd)
+ primeTable.push_back(2);
+ unsigned int testEntriesEnd = 1;
+
+ for (unsigned int p=3; p<=s_lastSmallPrime; p+=2)
{
- primeTable.push_back(p);
- testEntriesEnd = STDMIN((size_t)54U, primeTable.size());
+ unsigned int j;
+ for (j=1; j<testEntriesEnd; j++)
+ if (p%primeTable[j] == 0)
+ break;
+ if (j == testEntriesEnd)
+ {
+ primeTable.push_back(p);
+ testEntriesEnd = STDMIN((size_t)54U, primeTable.size());
+ }
}
- }
- return pPrimeTable.release();
-}
+ return pPrimeTable.release();
+ }
+};
const word16 * GetPrimeTable(unsigned int &size)
{
- std::vector<word16> &primeTable = StaticObject<std::vector<word16> >(&NewPrimeTable);
+ const std::vector<word16> &primeTable = Singleton<std::vector<word16>, NewPrimeTable>().Ref();
size = primeTable.size();
return &primeTable[0];
}
@@ -218,13 +221,19 @@ bool IsStrongLucasProbablePrime(const Integer &n)
return false;
}
-bool IsPrime(const Integer &p)
+struct NewLastSmallPrimeSquared
{
- static const Integer lastSmallPrimeSquared = Integer(s_lastSmallPrime).Squared();
+ Integer * operator()() const
+ {
+ return new Integer(Integer(s_lastSmallPrime).Squared());
+ }
+};
+bool IsPrime(const Integer &p)
+{
if (p <= s_lastSmallPrime)
return IsSmallPrime(p);
- else if (p <= lastSmallPrimeSquared)
+ else if (p <= Singleton<Integer, NewLastSmallPrimeSquared>().Ref())
return SmallDivisorsTest(p);
else
return SmallDivisorsTest(p) && IsStrongProbablePrime(p, 3) && IsStrongLucasProbablePrime(p);