From b5bc0d40a90b5efb3f3095b99492a353dfd40e13 Mon Sep 17 00:00:00 2001 From: Cheng Date: Sun, 27 Mar 2022 14:46:34 +1100 Subject: [PATCH] Spent far too much time making template code of unnecessary generality. --- db_accessors.h | 9 +++++---- introspection_of_standard_C_types.h | 9 +++++++++ ristretto255.cpp | 8 ++++---- ristretto255.h | 11 +++++++---- unit_test.cpp | 9 +++++---- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/db_accessors.h b/db_accessors.h index 94e3ef9..21481a1 100644 --- a/db_accessors.h +++ b/db_accessors.h @@ -19,7 +19,7 @@ namespace ro { //Has move semantics. class sql : public std::unique_ptr { public: - class null {}; + class monostate {}; sql(ISqlite3* p, const char* sz) :std::unique_ptr(sqlite3_prepare(p, sz)) {} sql(const std::unique_ptr& p, const char* sz) :std::unique_ptr(sqlite3_prepare(p.get(), sz)) {} // copy constructor @@ -64,7 +64,7 @@ namespace ro { else { assert(false); //, "Don't know how to read this datatype from database"); - return null(); + return monostate(); //should generate somewhat relevant type error at compile time. } } @@ -92,12 +92,13 @@ namespace ro { } else { assert(false); - //, "Don't know how to read this datatype from database"); + //, "Don't know how to read this datatype from database") + j = monostate(); //Should generate somewhat relevant error at compile time. } return *this; } - void bind(int i, null) { (*this)->Isqlite3_bind(i); } + void bind(int i, monostate) { (*this)->Isqlite3_bind(i); } template < typename T, typename std::enable_if::value, int >::type dummy_arg = 0 > void bind(int i, T j) { diff --git a/introspection_of_standard_C_types.h b/introspection_of_standard_C_types.h index 5532c4e..6f82b9b 100644 --- a/introspection_of_standard_C_types.h +++ b/introspection_of_standard_C_types.h @@ -20,6 +20,15 @@ namespace ro { constexpr bool is_standard_signed_integer = _Is_any_of_v, signed char, signed short, signed int, signed long, signed long long>; + template + constexpr bool is_standard_integer = ro::is_standard_unsigned_integer || ro::is_standard_signed_integer; + + template ,std::make_unsigned>::type>> + constexpr U iabs(T i) { + if constexpr (is_standard_signed_integer)return U(abs(i)); + else return i; + } + // A compile time test to see if desired casts work, and make sure that // undesired casts do not work template struct is_constructible_from { diff --git a/ristretto255.cpp b/ristretto255.cpp index 0e55c49..0f71085 100644 --- a/ristretto255.cpp +++ b/ristretto255.cpp @@ -27,19 +27,19 @@ namespace ristretto255 { bool scalar::constant_time_required{ true }; bool point::constant_time_required{ true }; - constexpr scalar::scalar(int64_t i) { +/* constexpr scalar::scalar(intmax_t i) { if (i >= 0) { - auto k{ uint64_t(i) }; + auto k{ intmax_t(i) }; for (auto& j : blob) { j = k; k = k >> 8; } } else { std::array absdata; - auto k{ uint64_t(-i) }; + auto k{ uintmax_t(-i) }; for (auto& j : absdata) { j = k; k = k >> 8; } crypto_core_ristretto255_scalar_negate(&blob[0], &absdata[0]); } } - + */ point point::operator*(const scalar &sclr) const& noexcept { point me; auto i{ crypto_scalarmult_ristretto255(&me.blob[0], &sclr.blob[0], &blob[0]) }; diff --git a/ristretto255.h b/ristretto255.h index d5e8711..23bb41e 100644 --- a/ristretto255.h +++ b/ristretto255.h @@ -653,10 +653,13 @@ namespace ristretto255 { ~scalar() = default; explicit constexpr scalar(std::array&& in) : blob{ in } {}; explicit constexpr scalar(std::array* in) :blob(*in) {}; - explicit constexpr scalar(int64_t); - explicit constexpr scalar(int i):scalar(int64_t(i)){} - explicit constexpr scalar(uint64_t k){ for (auto& j : blob) { j = k; k = k >> 8; } } - explicit constexpr scalar(unsigned int i) :scalar(uint64_t(i)) {} + explicit constexpr scalar(uintmax_t k){ for (auto& j : blob) { j = k; k = k >> 8; } } + template , uintmax_t>> + explicit constexpr scalar(T i) :scalar(U(ro::iabs(i))) { + if constexpr (ro::is_standard_signed_integer) { + if (i < 0) crypto_core_ristretto255_scalar_negate(&blob[0], &blob[0]); + } + } scalar(scalar&&) = default; // Move constructor scalar(const scalar&) = default; // Copy constructor scalar& operator=(scalar&&) = default; // Move assignment. diff --git a/unit_test.cpp b/unit_test.cpp index d998a82..31d139a 100644 --- a/unit_test.cpp +++ b/unit_test.cpp @@ -11,10 +11,10 @@ public: }; -class UnitTestEndException : public MyException { +class EndUnitTestOfExceptions : public MyException { public: using MyException::MyException; - UnitTestEndException() noexcept : + EndUnitTestOfExceptions() noexcept : MyException("\t\tEnd unit test") {} }; @@ -65,9 +65,9 @@ static bool EndUnitTest() { static std::string intestbed("Testbed: "); try { testbed::testbed(); - throw UnitTestEndException(); + throw EndUnitTestOfExceptions(); } - catch (const UnitTestEndException&) {} + catch (const EndUnitTestOfExceptions&) {} catch (const std::exception& e) { errorCode = 1001; szError = intestbed + e.what(); @@ -804,6 +804,7 @@ static bool TestShareSecretGenerationSpeed(void) { && scalar(0x0103) == scalar(0xFE) + scalar(5) && scalar(0xDEADBEEF) == scalar(0xDEADBDDE) + scalar(0x111) && scalar(0xBEEFDEADBEEF) == scalar(0xBEEFDEADBDDE) + scalar(0x111) + && scalar(0xBEEF0000000F) + scalar(-0xBEEF0EADBEEF) == scalar(int(-0xEADBEE0)) ) ) {