From 8c450a9f7a98800b6e08358454e9d35c0832a24f Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Wed, 22 Aug 2018 16:36:05 -0400 Subject: Avoid Singleton when possible (GH #708) Also clear several sign conversion warnings --- ec2n.cpp | 24 +++++++++++++++++++++++- ecp.cpp | 33 +++++++++++++++++++++++++++------ gf2n.cpp | 33 +++++++++++++++++++++++++++++---- integer.cpp | 8 ++++---- 4 files changed, 83 insertions(+), 15 deletions(-) diff --git a/ec2n.cpp b/ec2n.cpp index 3b6cf2a2..e3a0b285 100644 --- a/ec2n.cpp +++ b/ec2n.cpp @@ -11,6 +11,21 @@ #include "algebra.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) EC2N::EC2N(BufferedTransformation &bt) @@ -103,7 +118,7 @@ void EC2N::EncodePoint(BufferedTransformation &bt, const Point &P, bool compress NullStore().TransferTo(bt, EncodedPointSize(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()); } else @@ -177,7 +192,14 @@ bool EC2N::Equal(const Point &P, const Point &Q) 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().Ref(); +#endif } const EC2N::Point& EC2N::Inverse(const Point &P) const diff --git a/ecp.cpp b/ecp.cpp index 73f66685..4805785a 100644 --- a/ecp.cpp +++ b/ecp.cpp @@ -12,20 +12,34 @@ #include "filters.h" #include "algebra.cpp" -NAMESPACE_BEGIN(CryptoPP) - 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)); } -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)); } + NAMESPACE_END +NAMESPACE_BEGIN(CryptoPP) + ECP::ECP(const ECP &ecp, bool convertToMontgomeryRepresentation) { if (convertToMontgomeryRepresentation && !ecp.GetField().IsMontgomeryRepresentation()) @@ -124,13 +138,13 @@ void ECP::EncodePoint(BufferedTransformation &bt, const Point &P, bool compresse NullStore().TransferTo(bt, EncodedPointSize(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()); } else { unsigned int len = GetField().MaxElementByteLength(); - bt.Put(4); // uncompressed + bt.Put(4U); // uncompressed P.x.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 { +#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().Ref(); +#endif } const ECP::Point& ECP::Inverse(const Point &P) const diff --git a/gf2n.cpp b/gf2n.cpp index a1d24ff3..22b09974 100644 --- a/gf2n.cpp +++ b/gf2n.cpp @@ -18,12 +18,23 @@ #include -// Issue 340 -#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE -# pragma GCC diagnostic ignored "-Wconversion" -# pragma GCC diagnostic ignored "-Wsign-conversion" +ANONYMOUS_NAMESPACE_BEGIN + +using CryptoPP::PolynomialMod2; + +#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 +ANONYMOUS_NAMESPACE_END + NAMESPACE_BEGIN(CryptoPP) PolynomialMod2::PolynomialMod2() @@ -133,12 +144,26 @@ struct NewPolynomialMod2 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().Ref(); +#endif } 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 >().Ref(); +#endif } void PolynomialMod2::Decode(const byte *input, size_t inputLen) diff --git a/integer.cpp b/integer.cpp index 2d7f41dc..ff807547 100644 --- a/integer.cpp +++ b/integer.cpp @@ -4813,7 +4813,7 @@ public: // if init priorities are available. Dynamic initialization will be used if // 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 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); @@ -4837,7 +4837,7 @@ const Integer &Integer::Zero() #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) return g_zero; #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) - static Integer s_zero(0L); + static const Integer s_zero(0L); return s_zero; #else // Potential memory leak. Avoid if possible. return Singleton >().Ref(); @@ -4849,7 +4849,7 @@ const Integer &Integer::One() #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) return g_one; #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) - static Integer s_one(1L); + static const Integer s_one(1L); return s_one; #else // Potential memory leak. Avoid if possible. return Singleton >().Ref(); @@ -4861,7 +4861,7 @@ const Integer &Integer::Two() #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) return g_two; #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) - static Integer s_two(2L); + static const Integer s_two(2L); return s_two; #else // Potential memory leak. Avoid if possible. return Singleton >().Ref(); -- cgit v1.2.1