aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/Common.h1
-rw-r--r--src/common/Cryptography/Authentication/AuthDefines.h27
-rw-r--r--src/common/Cryptography/Authentication/SRP6.cpp118
-rw-r--r--src/common/Cryptography/Authentication/SRP6.h92
-rw-r--r--src/common/Cryptography/BigNumber.cpp40
-rw-r--r--src/common/Cryptography/BigNumber.h51
-rw-r--r--src/common/Cryptography/CryptoHash.h2
-rw-r--r--src/common/Cryptography/HMAC.h2
-rw-r--r--src/common/Utilities/advstd.h102
9 files changed, 405 insertions, 30 deletions
diff --git a/src/common/Common.h b/src/common/Common.h
index 0a3ab68d39f..114f24ea132 100644
--- a/src/common/Common.h
+++ b/src/common/Common.h
@@ -19,6 +19,7 @@
#define TRINITYCORE_COMMON_H
#include "Define.h"
+#include <array>
#include <memory>
#include <string>
#include <utility>
diff --git a/src/common/Cryptography/Authentication/AuthDefines.h b/src/common/Cryptography/Authentication/AuthDefines.h
new file mode 100644
index 00000000000..a32de20dcc6
--- /dev/null
+++ b/src/common/Cryptography/Authentication/AuthDefines.h
@@ -0,0 +1,27 @@
+/*
+ * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TRINITY_AUTHDEFINES_H
+#define TRINITY_AUTHDEFINES_H
+
+#include "Define.h"
+#include <array>
+
+constexpr size_t SESSION_KEY_LENGTH = 40;
+using SessionKey = std::array<uint8, SESSION_KEY_LENGTH>;
+
+#endif
diff --git a/src/common/Cryptography/Authentication/SRP6.cpp b/src/common/Cryptography/Authentication/SRP6.cpp
new file mode 100644
index 00000000000..c8b80d58f9b
--- /dev/null
+++ b/src/common/Cryptography/Authentication/SRP6.cpp
@@ -0,0 +1,118 @@
+/*
+ * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "SRP6.h"
+#include "CryptoRandom.h"
+#include "Util.h"
+#include <algorithm>
+#include <functional>
+
+using SHA1 = Trinity::Crypto::SHA1;
+using SRP6 = Trinity::Crypto::SRP6;
+
+/*static*/ std::array<uint8, 1> const SRP6::g = { 7 };
+/*static*/ std::array<uint8, 32> const SRP6::N = HexStrToByteArray<32>("894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7", true);
+/*static*/ BigNumber const SRP6::_g(SRP6::g);
+/*static*/ BigNumber const SRP6::_N(N);
+
+/*static*/ std::pair<SRP6::Salt, SRP6::Verifier> SRP6::MakeRegistrationData(std::string const& username, std::string const& password)
+{
+ std::pair<SRP6::Salt, SRP6::Verifier> res;
+ Crypto::GetRandomBytes(res.first); // random salt
+ res.second = CalculateVerifier(username, password, res.first);
+ return res;
+}
+
+/*static*/ std::pair<SRP6::Salt, SRP6::Verifier> SRP6::MakeRegistrationDataFromHash_DEPRECATED_DONOTUSE(SHA1::Digest const& hash)
+{
+ std::pair<SRP6::Salt, SRP6::Verifier> res;
+ Crypto::GetRandomBytes(res.first);
+ res.second = CalculateVerifierFromHash(hash, res.first);
+ return res;
+}
+
+/*static*/ SRP6::Verifier SRP6::CalculateVerifier(std::string const& username, std::string const& password, SRP6::Salt const& salt)
+{
+ // v = g ^ H(s || H(u || ':' || p)) mod N
+ return CalculateVerifierFromHash(SHA1::GetDigestOf(username, ":", password), salt);
+}
+
+// merge this into CalculateVerifier once the sha_pass hack finally gets nuked from orbit
+/*static*/ SRP6::Verifier SRP6::CalculateVerifierFromHash(SHA1::Digest const& hash, SRP6::Salt const& salt)
+{
+ return _g.ModExp(SHA1::GetDigestOf(salt, hash), _N).ToByteArray<32>(false);
+}
+
+/*static*/ SessionKey SRP6::SHA1Interleave(SRP6::EphemeralKey const& S)
+{
+ // split S into two buffers
+ std::array<uint8, EPHEMERAL_KEY_LENGTH/2> buf0, buf1;
+ for (size_t i = 0; i < EPHEMERAL_KEY_LENGTH/2; ++i)
+ {
+ buf0[i] = S[2 * i + 0];
+ buf1[i] = S[2 * i + 1];
+ }
+
+ // find position of first nonzero byte
+ size_t p = 0;
+ while (p < EPHEMERAL_KEY_LENGTH && !S[p]) ++p;
+ if (p & 1) ++p; // skip one extra byte if p is odd
+ p /= 2; // offset into buffers
+
+ // hash each of the halves, starting at the first nonzero byte
+ SHA1::Digest const hash0 = SHA1::GetDigestOf(buf0.data() + p, EPHEMERAL_KEY_LENGTH/2 - p);
+ SHA1::Digest const hash1 = SHA1::GetDigestOf(buf1.data() + p, EPHEMERAL_KEY_LENGTH/2 - p);
+
+ // stick the two hashes back together
+ SessionKey K;
+ for (size_t i = 0; i < SHA1::DIGEST_LENGTH; ++i)
+ {
+ K[2 * i + 0] = hash0[i];
+ K[2 * i + 1] = hash1[i];
+ }
+ return K;
+}
+
+SRP6::SRP6(std::string const& username, Salt const& salt, Verifier const& verifier)
+ : _I(SHA1::GetDigestOf(username)), _b(Crypto::GetRandomBytes<19>()), _v(verifier, false), s(salt), B(_B(_b, _v)) {}
+
+Optional<SessionKey> SRP6::VerifyChallengeResponse(EphemeralKey const& A, SHA1::Digest const& clientM)
+{
+ ASSERT(!_used, "A single SRP6 object must only ever be used to verify ONCE!");
+ _used = true;
+
+ BigNumber const _A(A);
+ if ((_A % _N).IsZero())
+ return {};
+
+ BigNumber const u(SHA1::GetDigestOf(A, B));
+ EphemeralKey const S = (_A * (_v.ModExp(u, _N))).ModExp(_b, N).ToByteArray<32>();
+
+ SessionKey K = SHA1Interleave(S);
+
+ // NgHash = H(N) xor H(g)
+ SHA1::Digest const NHash = SHA1::GetDigestOf(N);
+ SHA1::Digest const gHash = SHA1::GetDigestOf(g);
+ SHA1::Digest NgHash;
+ std::transform(NHash.begin(), NHash.end(), gHash.begin(), NgHash.begin(), std::bit_xor<>());
+
+ SHA1::Digest const ourM = SHA1::GetDigestOf(NgHash, _I, s, A, B, K);
+ if (ourM == clientM)
+ return K;
+ else
+ return {};
+}
diff --git a/src/common/Cryptography/Authentication/SRP6.h b/src/common/Cryptography/Authentication/SRP6.h
new file mode 100644
index 00000000000..3c511d6f7f3
--- /dev/null
+++ b/src/common/Cryptography/Authentication/SRP6.h
@@ -0,0 +1,92 @@
+/*
+ * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TRINITY_SRP6_H
+#define TRINITY_SRP6_H
+
+#include "AuthDefines.h"
+#include "BigNumber.h"
+#include "Define.h"
+#include "Common.h"
+#include "CryptoHash.h"
+#include "Optional.h"
+#include <array>
+
+namespace Trinity
+{
+namespace Crypto
+{
+ class TC_COMMON_API SRP6
+ {
+ public:
+ static constexpr size_t SALT_LENGTH = 32;
+ using Salt = std::array<uint8, SALT_LENGTH>;
+ static constexpr size_t VERIFIER_LENGTH = 32;
+ using Verifier = std::array<uint8, VERIFIER_LENGTH>;
+ static constexpr size_t EPHEMERAL_KEY_LENGTH = 32;
+ using EphemeralKey = std::array<uint8, EPHEMERAL_KEY_LENGTH>;
+
+ static std::array<uint8, 1> const g;
+ static std::array<uint8, 32> const N;
+
+ // this is the old sha_pass_hash hack
+ // YOU SHOULD NEVER STORE THIS HASH, if you do you are breaking SRP6 guarantees
+ // use MakeRegistrationData instead
+ static std::pair<Salt, Verifier> MakeRegistrationDataFromHash_DEPRECATED_DONOTUSE(SHA1::Digest const& hash);
+
+ // username + password must be passed through Utf8ToUpperOnlyLatin FIRST!
+ static std::pair<Salt, Verifier> MakeRegistrationData(std::string const& username, std::string const& password);
+ // username + password must be passed through Utf8ToUpperOnlyLatin FIRST!
+ static bool CheckLogin(std::string const& username, std::string const& password, Salt const& salt, Verifier const& verifier)
+ {
+ return (verifier == CalculateVerifier(username, password, salt));
+ }
+
+ static SHA1::Digest GetSessionVerifier(EphemeralKey const& A, SHA1::Digest const& clientM, SessionKey const& K)
+ {
+ return SHA1::GetDigestOf(A, clientM, K);
+ }
+
+ SRP6(std::string const& username, Salt const& salt, Verifier const& verifier);
+ Optional<SessionKey> VerifyChallengeResponse(EphemeralKey const& A, SHA1::Digest const& clientM);
+
+ private:
+ bool _used = false; // a single instance can only be used to verify once
+
+ static Verifier CalculateVerifier(std::string const& username, std::string const& password, Salt const& salt);
+ static Verifier CalculateVerifierFromHash(SHA1::Digest const& hash, Salt const& salt);
+ static SessionKey SHA1Interleave(EphemeralKey const& S);
+
+ /* global algorithm parameters */
+ static BigNumber const _g; // a [g]enerator for the ring of integers mod N, algorithm parameter
+ static BigNumber const _N; // the modulus, an algorithm parameter; all operations are mod this
+
+ static EphemeralKey _B(BigNumber const& b, BigNumber const& v) { return ((_g.ModExp(b,_N) + (v * 3)) % N).ToByteArray<EPHEMERAL_KEY_LENGTH>(); }
+
+ /* per-instantiation parameters, set on construction */
+ SHA1::Digest const _I; // H(I) - the username, all uppercase
+ BigNumber const _b; // b - randomly chosen by the server, 19 bytes, never given out
+ BigNumber const _v; // v - the user's password verifier, derived from s + H(USERNAME || ":" || PASSWORD)
+
+ public:
+ Salt const s; // s - the user's password salt, random, used to calculate v on registration
+ EphemeralKey const B; // B = 3v + g^b
+ };
+}
+}
+
+#endif
diff --git a/src/common/Cryptography/BigNumber.cpp b/src/common/Cryptography/BigNumber.cpp
index e0241c77486..62e459c6dce 100644
--- a/src/common/Cryptography/BigNumber.cpp
+++ b/src/common/Cryptography/BigNumber.cpp
@@ -27,7 +27,7 @@ BigNumber::BigNumber()
{ }
BigNumber::BigNumber(BigNumber const& bn)
- : _bn(BN_dup(bn._bn))
+ : _bn(BN_dup(bn.BN()))
{ }
BigNumber::~BigNumber()
@@ -35,6 +35,13 @@ BigNumber::~BigNumber()
BN_free(_bn);
}
+void BigNumber::SetDword(int32 val)
+{
+ SetDword(uint32(abs(val)));
+ if (val < 0)
+ BN_set_negative(_bn, 1);
+}
+
void BigNumber::SetDword(uint32 val)
{
BN_set_word(_bn, val);
@@ -88,19 +95,19 @@ BigNumber& BigNumber::operator=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::operator+=(BigNumber const& bn)
+BigNumber& BigNumber::operator+=(BigNumber const& bn)
{
BN_add(_bn, _bn, bn._bn);
return *this;
}
-BigNumber BigNumber::operator-=(BigNumber const& bn)
+BigNumber& BigNumber::operator-=(BigNumber const& bn)
{
BN_sub(_bn, _bn, bn._bn);
return *this;
}
-BigNumber BigNumber::operator*=(BigNumber const& bn)
+BigNumber& BigNumber::operator*=(BigNumber const& bn)
{
BN_CTX *bnctx;
@@ -111,18 +118,18 @@ BigNumber BigNumber::operator*=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::operator/=(BigNumber const& bn)
+BigNumber& BigNumber::operator/=(BigNumber const& bn)
{
BN_CTX *bnctx;
bnctx = BN_CTX_new();
- BN_div(_bn, NULL, _bn, bn._bn, bnctx);
+ BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
BN_CTX_free(bnctx);
return *this;
}
-BigNumber BigNumber::operator%=(BigNumber const& bn)
+BigNumber& BigNumber::operator%=(BigNumber const& bn)
{
BN_CTX *bnctx;
@@ -133,7 +140,18 @@ BigNumber BigNumber::operator%=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::Exp(BigNumber const& bn)
+BigNumber& BigNumber::operator<<=(int n)
+{
+ BN_lshift(_bn, _bn, n);
+ return *this;
+}
+
+int BigNumber::CompareTo(BigNumber const& bn) const
+{
+ return BN_cmp(_bn, bn._bn);
+}
+
+BigNumber BigNumber::Exp(BigNumber const& bn) const
{
BigNumber ret;
BN_CTX *bnctx;
@@ -145,7 +163,7 @@ BigNumber BigNumber::Exp(BigNumber const& bn)
return ret;
}
-BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2)
+BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2) const
{
BigNumber ret;
BN_CTX *bnctx;
@@ -157,12 +175,12 @@ BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2)
return ret;
}
-int32 BigNumber::GetNumBytes(void) const
+int32 BigNumber::GetNumBytes() const
{
return BN_num_bytes(_bn);
}
-uint32 BigNumber::AsDword()
+uint32 BigNumber::AsDword() const
{
return (uint32)BN_get_word(_bn);
}
diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h
index 509a63b6fe9..da4b43b30a5 100644
--- a/src/common/Cryptography/BigNumber.h
+++ b/src/common/Cryptography/BigNumber.h
@@ -32,17 +32,19 @@ class TC_COMMON_API BigNumber
BigNumber();
BigNumber(BigNumber const& bn);
BigNumber(uint32 v) : BigNumber() { SetDword(v); }
+ BigNumber(int32 v) : BigNumber() { SetDword(v); }
BigNumber(std::string const& v) : BigNumber() { SetHexStr(v); }
template <size_t Size>
BigNumber(std::array<uint8, Size> const& v, bool littleEndian = true) : BigNumber() { SetBinary(v.data(), Size, littleEndian); }
~BigNumber();
+ void SetDword(int32);
void SetDword(uint32);
void SetQword(uint64);
void SetBinary(uint8 const* bytes, int32 len, bool littleEndian = true);
template <typename Container>
- auto SetBinary(Container const& c, bool littleEndian = true) -> std::enable_if_t<!std::is_pointer_v<std::decay_t<Container>>> { SetBinary(advstd::data(c), advstd::size(c), littleEndian); }
+ auto SetBinary(Container const& c, bool littleEndian = true) -> std::enable_if_t<!advstd::is_pointer_v<std::decay_t<Container>>> { SetBinary(advstd::data(c), advstd::size(c), littleEndian); }
bool SetHexStr(char const* str);
bool SetHexStr(std::string const& str) { return SetHexStr(str.c_str()); }
@@ -50,52 +52,67 @@ class TC_COMMON_API BigNumber
BigNumber& operator=(BigNumber const& bn);
- BigNumber operator+=(BigNumber const& bn);
- BigNumber operator+(BigNumber const& bn)
+ BigNumber& operator+=(BigNumber const& bn);
+ BigNumber operator+(BigNumber const& bn) const
{
BigNumber t(*this);
return t += bn;
}
- BigNumber operator-=(BigNumber const& bn);
- BigNumber operator-(BigNumber const& bn)
+ BigNumber& operator-=(BigNumber const& bn);
+ BigNumber operator-(BigNumber const& bn) const
{
BigNumber t(*this);
return t -= bn;
}
- BigNumber operator*=(BigNumber const& bn);
- BigNumber operator*(BigNumber const& bn)
+ BigNumber& operator*=(BigNumber const& bn);
+ BigNumber operator*(BigNumber const& bn) const
{
BigNumber t(*this);
return t *= bn;
}
- BigNumber operator/=(BigNumber const& bn);
- BigNumber operator/(BigNumber const& bn)
+ BigNumber& operator/=(BigNumber const& bn);
+ BigNumber operator/(BigNumber const& bn) const
{
BigNumber t(*this);
return t /= bn;
}
- BigNumber operator%=(BigNumber const& bn);
- BigNumber operator%(BigNumber const& bn)
+ BigNumber& operator%=(BigNumber const& bn);
+ BigNumber operator%(BigNumber const& bn) const
{
BigNumber t(*this);
return t %= bn;
}
+ BigNumber& operator<<=(int n);
+ BigNumber operator<<(int n) const
+ {
+ BigNumber t(*this);
+ return t <<= n;
+ }
+
+ int CompareTo(BigNumber const& bn) const;
+ bool operator<=(BigNumber const& bn) const { return (CompareTo(bn) <= 0); }
+ bool operator==(BigNumber const& bn) const { return (CompareTo(bn) == 0); }
+ bool operator>=(BigNumber const& bn) const { return (CompareTo(bn) >= 0); }
+ bool operator<(BigNumber const& bn) const { return (CompareTo(bn) < 0); }
+ bool operator>(BigNumber const& bn) const { return (CompareTo(bn) > 0); }
+
bool IsZero() const;
bool IsNegative() const;
- BigNumber ModExp(BigNumber const& bn1, BigNumber const& bn2);
- BigNumber Exp(BigNumber const&);
+ BigNumber ModExp(BigNumber const& bn1, BigNumber const& bn2) const;
+ BigNumber Exp(BigNumber const&) const;
- int32 GetNumBytes(void) const;
+ int32 GetNumBytes() const;
- struct bignum_st *BN() { return _bn; }
+ struct bignum_st* BN() { return _bn; }
+ struct bignum_st const* BN() const { return _bn; }
- uint32 AsDword();
+ uint32 AsDword() const;
void GetBytes(uint8* buf, size_t bufsize, bool littleEndian = true) const;
std::vector<uint8> ToByteVector(int32 minSize = 0, bool littleEndian = true) const;
@@ -112,7 +129,7 @@ class TC_COMMON_API BigNumber
std::string AsDecStr() const;
private:
- struct bignum_st *_bn;
+ struct bignum_st* _bn;
};
#endif
diff --git a/src/common/Cryptography/CryptoHash.h b/src/common/Cryptography/CryptoHash.h
index bb9fbc51c15..991bf15944e 100644
--- a/src/common/Cryptography/CryptoHash.h
+++ b/src/common/Cryptography/CryptoHash.h
@@ -78,7 +78,7 @@ namespace Impl
public:
template <typename... Ts>
- static auto GetDigestOf(Ts&&... pack) -> /*std::enable_if_t<!(std::is_integral_v<std::decay_t<Ts>> || ...),*/ Digest/*>*/
+ static auto GetDigestOf(Ts&&... pack) -> std::enable_if_t<advstd::conjunction<advstd::negation<std::is_integral<Ts>>...>::value, Digest>
{
GenericHash hash;
UpdateData_OLDCPP(hash, std::forward<Ts>(pack)...);
diff --git a/src/common/Cryptography/HMAC.h b/src/common/Cryptography/HMAC.h
index 8562d3b5e2e..6b9d2a081bd 100644
--- a/src/common/Cryptography/HMAC.h
+++ b/src/common/Cryptography/HMAC.h
@@ -88,7 +88,7 @@ namespace Impl
public:
template <typename Container, typename... Ts>
- static auto GetDigestOf(Container const& seed, Ts&&... pack) -> /*std::enable_if_t<!(std::is_integral_v<std::decay_t<Ts>> || ...),*/ Digest/*>*/
+ static auto GetDigestOf(Container const& seed, Ts&&... pack) -> std::enable_if_t<advstd::conjunction<advstd::negation<std::is_integral<Ts>>...>::value, Digest>
{
GenericHMAC hash(seed);
UpdateData_OLDCPP(hash, std::forward<Ts>(pack)...);
diff --git a/src/common/Utilities/advstd.h b/src/common/Utilities/advstd.h
index 7514f156050..048765ab2c9 100644
--- a/src/common/Utilities/advstd.h
+++ b/src/common/Utilities/advstd.h
@@ -25,6 +25,65 @@
// this namespace holds implementations of upcoming stdlib features that our c++ version doesn't have yet
namespace advstd
{
+ // C++17 std::apply (constrained to only function pointers, not all callable)
+ template <typename... Ts>
+ using apply_tuple_type = std::tuple<std::remove_cv_t<std::remove_reference_t<Ts>>...>;
+ template <typename R, typename... Ts, std::size_t... I>
+ R apply_impl(R(*func)(Ts...), apply_tuple_type<Ts...>&& args, std::index_sequence<I...>)
+ {
+ return func(std::get<I>(std::forward<apply_tuple_type<Ts...>>(args))...);
+ }
+ template <typename R, typename... Ts>
+ R apply(R(*func)(Ts...), apply_tuple_type<Ts...>&& args)
+ {
+ return apply_impl(func, std::forward<apply_tuple_type<Ts...>>(args), std::index_sequence_for<Ts...>{});
+ }
+
+#define forward_1v(stdname, type) template <typename T> constexpr type stdname ## _v = std::stdname<T>::value
+#define forward_2v(stdname, type) template <typename U, typename V> constexpr type stdname ## _v = std::stdname<U,V>::value
+
+ // C++17 std::is_same_v
+ forward_2v(is_same, bool);
+
+ // C++17 std::is_integral_v
+ forward_1v(is_integral, bool);
+
+ // C++17 std::is_assignable_v
+ forward_2v(is_assignable, bool);
+
+ // C++17 std::is_signed_v
+ forward_1v(is_signed, bool);
+
+ // C++17 std::is_unsigned_v
+ forward_1v(is_unsigned, bool);
+
+ // C++17 std::is_base_of_v
+ forward_2v(is_base_of, bool);
+
+ // C++17 std::is_floating_point_v
+ forward_1v(is_floating_point, bool);
+
+ // C++17 std::is_pointer_v
+ forward_1v(is_pointer, bool);
+
+ // C++17 std::is_reference_v
+ forward_1v(is_reference, bool);
+
+ // C++17 std::tuple_size_v
+ forward_1v(tuple_size, size_t);
+
+ // C++17 std::is_enum_v
+ forward_1v(is_enum, bool);
+
+ // C++17 std::is_arithmetic_v
+ forward_1v(is_arithmetic, bool);
+
+ // C++17 std::is_move_assignable_v
+ forward_1v(is_move_assignable, bool);
+
+#undef forward_1v
+#undef forward_2v
+
// C++17 std::size
template <typename C>
constexpr auto size(const C& c) { return c.size(); }
@@ -47,6 +106,49 @@ namespace advstd
template <typename T>
constexpr T const* data(std::initializer_list<T> l) noexcept { return l.begin(); }
+
+ // C++17 std::gcd
+ template <typename T1, typename T2>
+ constexpr std::enable_if_t<advstd::is_unsigned_v<T1> && advstd::is_unsigned_v<T2>, std::common_type_t<T1, T2>> gcd(T1 _m, T2 _n)
+ {
+ using T = std::common_type_t<T1, T2>;
+ T n=_n, m=_m;
+ while (n)
+ {
+ T o = m;
+ m = n;
+ n = o%n;
+ }
+ return m;
+ }
+
+ // C++17 std::lcm
+ template <typename T1, typename T2>
+ constexpr std::enable_if_t<advstd::is_unsigned_v<T1> && advstd::is_unsigned_v<T2>, std::common_type_t<T1, T2>> lcm(T1 m, T2 n)
+ {
+ return (m/gcd(m, n))*n;
+ }
+
+ // C++20 std::remove_cvref_t
+ template <class T>
+ using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
+
+ template<typename B>
+ struct negation : std::integral_constant<bool, !bool(B::value)> { };
+
+ template <typename...>
+ struct conjunction : std::true_type { };
+ template <typename B1>
+ struct conjunction<B1> : B1 { };
+ template <typename B1, class... Bn>
+ struct conjunction<B1, Bn...> : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> { };
+
+ template <typename...>
+ struct disjunction : std::false_type { };
+ template <typename B1>
+ struct disjunction<B1> : B1 { };
+ template <typename B1, class... Bn>
+ struct disjunction<B1, Bn...> : std::conditional_t<bool(B1::value), B1, disjunction<Bn...>> { };
}
#endif