Avoid Singleton when possible (GH #708)

Also clear several sign conversion warnings
pull/709/head
Jeffrey Walton 2018-08-22 16:36:05 -04:00
parent 0ba3687c39
commit 8c450a9f7a
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
4 changed files with 83 additions and 15 deletions

View File

@ -11,6 +11,21 @@
#include "algebra.cpp" #include "algebra.cpp"
#include "eprecomp.cpp" #include "eprecomp.cpp"
ANONYMOUS_NAMESPACE_BEGIN
using CryptoPP::EC2N;
#if defined(HAVE_GCC_INIT_PRIORITY)
const EC2N::Point g_identity __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 50))) = EC2N::Point();
#elif defined(HAVE_MSC_INIT_PRIORITY)
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU")
const EC2N::Point g_identity;
#pragma warning(default: 4075)
#endif
ANONYMOUS_NAMESPACE_END
NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(CryptoPP)
EC2N::EC2N(BufferedTransformation &bt) EC2N::EC2N(BufferedTransformation &bt)
@ -103,7 +118,7 @@ void EC2N::EncodePoint(BufferedTransformation &bt, const Point &P, bool compress
NullStore().TransferTo(bt, EncodedPointSize(compressed)); NullStore().TransferTo(bt, EncodedPointSize(compressed));
else if (compressed) else if (compressed)
{ {
bt.Put(2 + (!P.x ? 0 : m_field->Divide(P.y, P.x).GetBit(0))); bt.Put((byte)(2U + (!P.x ? 0U : m_field->Divide(P.y, P.x).GetBit(0))));
P.x.Encode(bt, m_field->MaxElementByteLength()); P.x.Encode(bt, m_field->MaxElementByteLength());
} }
else else
@ -177,7 +192,14 @@ bool EC2N::Equal(const Point &P, const Point &Q) const
const EC2N::Point& EC2N::Identity() const const EC2N::Point& EC2N::Identity() const
{ {
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_identity;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static const EC2N::Point g_identity;
return g_identity;
#else
return Singleton<Point>().Ref(); return Singleton<Point>().Ref();
#endif
} }
const EC2N::Point& EC2N::Inverse(const Point &P) const const EC2N::Point& EC2N::Inverse(const Point &P) const

33
ecp.cpp
View File

@ -12,20 +12,34 @@
#include "filters.h" #include "filters.h"
#include "algebra.cpp" #include "algebra.cpp"
NAMESPACE_BEGIN(CryptoPP)
ANONYMOUS_NAMESPACE_BEGIN ANONYMOUS_NAMESPACE_BEGIN
static inline ECP::Point ToMontgomery(const ModularArithmetic &mr, const ECP::Point &P)
using CryptoPP::ECP;
using CryptoPP::ModularArithmetic;
#if defined(HAVE_GCC_INIT_PRIORITY)
const ECP::Point g_identity __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 51))) = ECP::Point();
#elif defined(HAVE_MSC_INIT_PRIORITY)
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU")
const ECP::Point g_identity;
#pragma warning(default: 4075)
#endif
inline ECP::Point ToMontgomery(const ModularArithmetic &mr, const ECP::Point &P)
{ {
return P.identity ? P : ECP::Point(mr.ConvertIn(P.x), mr.ConvertIn(P.y)); return P.identity ? P : ECP::Point(mr.ConvertIn(P.x), mr.ConvertIn(P.y));
} }
static inline ECP::Point FromMontgomery(const ModularArithmetic &mr, const ECP::Point &P) inline ECP::Point FromMontgomery(const ModularArithmetic &mr, const ECP::Point &P)
{ {
return P.identity ? P : ECP::Point(mr.ConvertOut(P.x), mr.ConvertOut(P.y)); return P.identity ? P : ECP::Point(mr.ConvertOut(P.x), mr.ConvertOut(P.y));
} }
NAMESPACE_END NAMESPACE_END
NAMESPACE_BEGIN(CryptoPP)
ECP::ECP(const ECP &ecp, bool convertToMontgomeryRepresentation) ECP::ECP(const ECP &ecp, bool convertToMontgomeryRepresentation)
{ {
if (convertToMontgomeryRepresentation && !ecp.GetField().IsMontgomeryRepresentation()) if (convertToMontgomeryRepresentation && !ecp.GetField().IsMontgomeryRepresentation())
@ -124,13 +138,13 @@ void ECP::EncodePoint(BufferedTransformation &bt, const Point &P, bool compresse
NullStore().TransferTo(bt, EncodedPointSize(compressed)); NullStore().TransferTo(bt, EncodedPointSize(compressed));
else if (compressed) else if (compressed)
{ {
bt.Put(2 + P.y.GetBit(0)); bt.Put((byte)(2U + P.y.GetBit(0)));
P.x.Encode(bt, GetField().MaxElementByteLength()); P.x.Encode(bt, GetField().MaxElementByteLength());
} }
else else
{ {
unsigned int len = GetField().MaxElementByteLength(); unsigned int len = GetField().MaxElementByteLength();
bt.Put(4); // uncompressed bt.Put(4U); // uncompressed
P.x.Encode(bt, len); P.x.Encode(bt, len);
P.y.Encode(bt, len); P.y.Encode(bt, len);
} }
@ -201,7 +215,14 @@ bool ECP::Equal(const Point &P, const Point &Q) const
const ECP::Point& ECP::Identity() const const ECP::Point& ECP::Identity() const
{ {
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_identity;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static const ECP::Point g_identity;
return g_identity;
#else
return Singleton<Point>().Ref(); return Singleton<Point>().Ref();
#endif
} }
const ECP::Point& ECP::Inverse(const Point &P) const const ECP::Point& ECP::Inverse(const Point &P) const

View File

@ -18,12 +18,23 @@
#include <iostream> #include <iostream>
// Issue 340 ANONYMOUS_NAMESPACE_BEGIN
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
# pragma GCC diagnostic ignored "-Wconversion" using CryptoPP::PolynomialMod2;
# pragma GCC diagnostic ignored "-Wsign-conversion"
#if defined(HAVE_GCC_INIT_PRIORITY)
const PolynomialMod2 g_zero __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 60))) = PolynomialMod2();
const PolynomialMod2 g_one __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 61))) = PolynomialMod2(1);
#elif defined(HAVE_MSC_INIT_PRIORITY)
#pragma warning(disable: 4075)
#pragma init_seg(".CRT$XCU")
const PolynomialMod2 g_zero;
const PolynomialMod2 g_one(1);
#pragma warning(default: 4075)
#endif #endif
ANONYMOUS_NAMESPACE_END
NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(CryptoPP)
PolynomialMod2::PolynomialMod2() PolynomialMod2::PolynomialMod2()
@ -133,12 +144,26 @@ struct NewPolynomialMod2
const PolynomialMod2 &PolynomialMod2::Zero() const PolynomialMod2 &PolynomialMod2::Zero()
{ {
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_zero;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static const PolynomialMod2 g_zero;
return g_zero;
#else
return Singleton<PolynomialMod2>().Ref(); return Singleton<PolynomialMod2>().Ref();
#endif
} }
const PolynomialMod2 &PolynomialMod2::One() const PolynomialMod2 &PolynomialMod2::One()
{ {
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_one;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static const PolynomialMod2 g_one(1);
return g_one;
#else
return Singleton<PolynomialMod2, NewPolynomialMod2<1> >().Ref(); return Singleton<PolynomialMod2, NewPolynomialMod2<1> >().Ref();
#endif
} }
void PolynomialMod2::Decode(const byte *input, size_t inputLen) void PolynomialMod2::Decode(const byte *input, size_t inputLen)

View File

@ -4813,7 +4813,7 @@ public:
// if init priorities are available. Dynamic initialization will be used if // if init priorities are available. Dynamic initialization will be used if
// init priorities are not available. // init priorities are not available.
#if HAVE_GCC_INIT_PRIORITY #if defined(HAVE_GCC_INIT_PRIORITY)
const InitInteger s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = InitInteger(); const InitInteger s_init __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = InitInteger();
const Integer g_zero __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 11))) = Integer(0L); const Integer g_zero __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 11))) = Integer(0L);
const Integer g_one __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 12))) = Integer(1L); const Integer g_one __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 12))) = Integer(1L);
@ -4837,7 +4837,7 @@ const Integer &Integer::Zero()
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_zero; return g_zero;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static Integer s_zero(0L); static const Integer s_zero(0L);
return s_zero; return s_zero;
#else // Potential memory leak. Avoid if possible. #else // Potential memory leak. Avoid if possible.
return Singleton<Integer, NewInteger<0L> >().Ref(); return Singleton<Integer, NewInteger<0L> >().Ref();
@ -4849,7 +4849,7 @@ const Integer &Integer::One()
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_one; return g_one;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static Integer s_one(1L); static const Integer s_one(1L);
return s_one; return s_one;
#else // Potential memory leak. Avoid if possible. #else // Potential memory leak. Avoid if possible.
return Singleton<Integer, NewInteger<1L> >().Ref(); return Singleton<Integer, NewInteger<1L> >().Ref();
@ -4861,7 +4861,7 @@ const Integer &Integer::Two()
#if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY)
return g_two; return g_two;
#elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT)
static Integer s_two(2L); static const Integer s_two(2L);
return s_two; return s_two;
#else // Potential memory leak. Avoid if possible. #else // Potential memory leak. Avoid if possible.
return Singleton<Integer, NewInteger<2L> >().Ref(); return Singleton<Integer, NewInteger<2L> >().Ref();