From 6ecc789df1cea7640f54ddc2aed149c6b188891f Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Mon, 26 Jun 2023 09:57:15 -0400 Subject: [PATCH] Add precomuted small prime table (GH #1210) --- Filelist.txt | 1 + cryptdll.vcxproj | 3 +- cryptdll.vcxproj.filters | 3 + cryptlib.vcxproj | 3 +- cryptlib.vcxproj.filters | 5 +- nbtheory.cpp | 38 +--- nbtheory.h | 5 +- primetab.cpp | 386 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 407 insertions(+), 37 deletions(-) create mode 100644 primetab.cpp diff --git a/Filelist.txt b/Filelist.txt index 0639e16e..410613a8 100644 --- a/Filelist.txt +++ b/Filelist.txt @@ -262,6 +262,7 @@ power8_ppc.cpp power9_ppc.cpp ppc_simd.cpp ppc_simd.h +primetab.cpp pssr.cpp pssr.h pubkey.cpp diff --git a/cryptdll.vcxproj b/cryptdll.vcxproj index ced2ea59..5fbee211 100644 --- a/cryptdll.vcxproj +++ b/cryptdll.vcxproj @@ -226,6 +226,7 @@ + @@ -331,4 +332,4 @@ - \ No newline at end of file + diff --git a/cryptdll.vcxproj.filters b/cryptdll.vcxproj.filters index f11b1695..4b3d1a86 100644 --- a/cryptdll.vcxproj.filters +++ b/cryptdll.vcxproj.filters @@ -146,6 +146,9 @@ Source Files + + Source Files + Source Files diff --git a/cryptlib.vcxproj b/cryptlib.vcxproj index 1294df56..e219cff3 100644 --- a/cryptlib.vcxproj +++ b/cryptlib.vcxproj @@ -289,6 +289,7 @@ + @@ -582,4 +583,4 @@ - \ No newline at end of file + diff --git a/cryptlib.vcxproj.filters b/cryptlib.vcxproj.filters index 47714ac0..46007144 100644 --- a/cryptlib.vcxproj.filters +++ b/cryptlib.vcxproj.filters @@ -347,6 +347,9 @@ Source Files + + Source Files + Source Files @@ -1104,4 +1107,4 @@ Miscellaneous - \ No newline at end of file + diff --git a/nbtheory.cpp b/nbtheory.cpp index 6e5d9957..7e60be4b 100644 --- a/nbtheory.cpp +++ b/nbtheory.cpp @@ -18,43 +18,15 @@ NAMESPACE_BEGIN(CryptoPP) +// Keep sync'd with primetab.cpp +const unsigned int maxPrimeTableSize = 3511; const word s_lastSmallPrime = 32719; -struct NewPrimeTable -{ - std::vector * operator()() const - { - const unsigned int maxPrimeTableSize = 3511; - - member_ptr > pPrimeTable(new std::vector); - std::vector &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 &primeTable = Singleton, NewPrimeTable>().Ref(); - size = (unsigned int)primeTable.size(); - return &primeTable[0]; + extern const word16 precomputedPrimeTable[maxPrimeTableSize]; + size = maxPrimeTableSize; + return precomputedPrimeTable; } bool IsSmallPrime(const Integer &p) diff --git a/nbtheory.h b/nbtheory.h index a396ab46..66b805a6 100644 --- a/nbtheory.h +++ b/nbtheory.h @@ -13,7 +13,10 @@ NAMESPACE_BEGIN(CryptoPP) /// \brief The Small Prime table -/// \details GetPrimeTable obtains pointer to small prime table and provides the size of the table. +/// \param size number of elements in the table +/// \return prime table with /p size elements +/// \details GetPrimeTable() obtains pointer to small prime table and provides the size of the table. +/// /p size is an out parameter. CRYPTOPP_DLL const word16 * CRYPTOPP_API GetPrimeTable(unsigned int &size); // ************ primality testing **************** diff --git a/primetab.cpp b/primetab.cpp new file mode 100644 index 00000000..732ea2bc --- /dev/null +++ b/primetab.cpp @@ -0,0 +1,386 @@ +// primetab.cpp - written and placed in the public domain by Jeffrey Walton +// +// nbtheory.cpp originally built the prime table on the fly, and then +// returned a reference to it in a Singleton. This was useful to +// save memory in the old days. Nowadays we can precompute the table +// and place it in the read-only data segment. Also see +// https://github.com/weidai11/cryptopp/issues/1210. + +// The table below was generated from the original Crypto++ table: +// +// $ cat test.cxx +// #include +// #include +// #include +// #include "config.h" +// #include "nbtheory.h" +// +// int main(int argc, char* argv[]) +// { +// using namespace CryptoPP; +// +// unsigned int primeTableSize = 0; +// const word16* primeTable = NULLPTR; +// primeTable = GetPrimeTable(primeTableSize); +// +// for (size_t i=0; i