Add constexpr-ness to seckey.h classes.

Coarse grained benchmarking with GCC 4.8 at -O2 using 'time' and 'cryptest.exe' shows we shaved about 100ms to 150ms off the running time. We are ready to break the 1-second mark for the running time (its elluded me for some time now)
pull/131/merge
Jeffrey Walton 2016-09-05 03:13:45 -04:00
parent 75f4281857
commit cf81d8a099
1 changed files with 27 additions and 22 deletions

View File

@ -70,14 +70,16 @@ public:
//! provided by a static function. //! provided by a static function.
//! \param keylength the size of the key, in bytes //! \param keylength the size of the key, in bytes
//! \details keylength is unused in the default implementation. //! \details keylength is unused in the default implementation.
static unsigned int StaticGetDefaultRounds(size_t keylength) CRYPTOPP_CONSTEXPR static unsigned int StaticGetDefaultRounds(size_t keylength)
{CRYPTOPP_UNUSED(keylength); return DEFAULT_ROUNDS;} {return CRYPTOPP_UNUSED(keylength), DEFAULT_ROUNDS;}
protected: protected:
//! \brief Validates the number of rounds for an algorithm. //! \brief Validates the number of rounds for an algorithm.
//! \param rounds the canddiate number of rounds //! \param rounds the canddiate number of rounds
//! \param alg an Algorithm object used if the number of rounds are invalid //! \param alg an Algorithm object used if the number of rounds are invalid
//! \throws InvalidRounds if the number of rounds are invalid //! \throws InvalidRounds if the number of rounds are invalid
//! \details ThrowIfInvalidRounds() validates the number of rounds and throws if invalid.
//! The function is not a C++11 <tt>constexpr</tt> due to the potential throw operation.
inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg) inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
{ {
if (M == INT_MAX) // Coverity and result_independent_of_operands if (M == INT_MAX) // Coverity and result_independent_of_operands
@ -97,6 +99,8 @@ protected:
//! \param alg an Algorithm object used if the number of rounds are invalid //! \param alg an Algorithm object used if the number of rounds are invalid
//! \returns the number of rounds for the algorithm //! \returns the number of rounds for the algorithm
//! \throws InvalidRounds if the number of rounds are invalid //! \throws InvalidRounds if the number of rounds are invalid
//! \details GetRoundsAndThrowIfInvalid() validates the number of rounds and throws if invalid.
//! The function is not a C++11 <tt>constexpr</tt> due to the potential throw operation.
inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg) inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
{ {
int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS); int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
@ -140,8 +144,8 @@ public:
//! \param keylength the size of the key, in bytes //! \param keylength the size of the key, in bytes
//! \details The default implementation returns KEYLENGTH. keylength is unused //! \details The default implementation returns KEYLENGTH. keylength is unused
//! in the default implementation. //! in the default implementation.
static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) CRYPTOPP_CONSTEXPR static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
{CRYPTOPP_UNUSED(keylength); return KEYLENGTH;} {return CRYPTOPP_UNUSED(keylength), KEYLENGTH;}
}; };
//! \class VariableKeyLength //! \class VariableKeyLength
@ -192,6 +196,7 @@ public:
//! then keylength is returned. Otherwise, the function returns keylength rounded //! then keylength is returned. Otherwise, the function returns keylength rounded
//! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE. //! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE.
//! \details keylength is provided in bytes, not bits. //! \details keylength is provided in bytes, not bits.
// TODO: Figure out how to make this CRYPTOPP_CONSTEXPR
static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
{ {
if (keylength < (size_t)MIN_KEYLENGTH) if (keylength < (size_t)MIN_KEYLENGTH)
@ -240,7 +245,7 @@ public:
//! then keylength is returned. Otherwise, the function returns keylength rounded //! then keylength is returned. Otherwise, the function returns keylength rounded
//! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE. //! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE.
//! \details keylength is provided in bytes, not bits. //! \details keylength is provided in bytes, not bits.
static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength) CRYPTOPP_CONSTEXPR static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
{return T::StaticGetValidKeyLength(keylength);} {return T::StaticGetValidKeyLength(keylength);}
}; };
@ -257,17 +262,17 @@ class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
public: public:
//! \brief The minimum key length used by the algorithm //! \brief The minimum key length used by the algorithm
//! \returns minimum key length used by the algorithm, in bytes //! \returns minimum key length used by the algorithm, in bytes
size_t MinKeyLength() const CRYPTOPP_CONSTEXPR size_t MinKeyLength() const
{return INFO::MIN_KEYLENGTH;} {return INFO::MIN_KEYLENGTH;}
//! \brief The maximum key length used by the algorithm //! \brief The maximum key length used by the algorithm
//! \returns maximum key length used by the algorithm, in bytes //! \returns maximum key length used by the algorithm, in bytes
size_t MaxKeyLength() const CRYPTOPP_CONSTEXPR size_t MaxKeyLength() const
{return (size_t)INFO::MAX_KEYLENGTH;} {return (size_t)INFO::MAX_KEYLENGTH;}
//! \brief The default key length used by the algorithm //! \brief The default key length used by the algorithm
//! \returns default key length used by the algorithm, in bytes //! \returns default key length used by the algorithm, in bytes
size_t DefaultKeyLength() const CRYPTOPP_CONSTEXPR size_t DefaultKeyLength() const
{return INFO::DEFAULT_KEYLENGTH;} {return INFO::DEFAULT_KEYLENGTH;}
//! \brief Provides a valid key length for the algorithm //! \brief Provides a valid key length for the algorithm
@ -278,17 +283,17 @@ public:
//! then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE, //! then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE,
//! then keylength is returned. Otherwise, the function returns a \a lower multiple of //! then keylength is returned. Otherwise, the function returns a \a lower multiple of
//! KEYLENGTH_MULTIPLE. //! KEYLENGTH_MULTIPLE.
size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);} CRYPTOPP_CONSTEXPR size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);}
//! \brief The default IV requirements for the algorithm //! \brief The default IV requirements for the algorithm
//! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
//! in cryptlib.h for allowed values. //! in cryptlib.h for allowed values.
SimpleKeyingInterface::IV_Requirement IVRequirement() const CRYPTOPP_CONSTEXPR SimpleKeyingInterface::IV_Requirement IVRequirement() const
{return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;} {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;}
//! \brief The default initialization vector length for the algorithm //! \brief The default initialization vector length for the algorithm
//! \details IVSize is provided in bytes, not bits. The default implementation uses IV_LENGTH, which is 0. //! \details IVSize is provided in bytes, not bits. The default implementation uses IV_LENGTH, which is 0.
unsigned int IVSize() const CRYPTOPP_CONSTEXPR unsigned int IVSize() const
{return INFO::IV_LENGTH;} {return INFO::IV_LENGTH;}
}; };
@ -344,7 +349,7 @@ public:
//! \brief Provides the direction of the cipher //! \brief Provides the direction of the cipher
//! \returns true if DIR is ENCRYPTION, false otherwise //! \returns true if DIR is ENCRYPTION, false otherwise
//! \sa GetCipherDirection(), IsPermutation() //! \sa GetCipherDirection(), IsPermutation()
bool IsForwardTransformation() const {return DIR == ENCRYPTION;} CRYPTOPP_CONSTEXPR bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
}; };
//! \class MessageAuthenticationCodeImpl //! \class MessageAuthenticationCodeImpl