1
0
forked from cheng/wallet

Spent far too much time making template code of unnecessary generality.

This commit is contained in:
Cheng 2022-03-27 14:46:34 +11:00
parent 342135c3da
commit b5bc0d40a9
No known key found for this signature in database
GPG Key ID: D51301E176B31828
5 changed files with 30 additions and 16 deletions

View File

@ -19,7 +19,7 @@ namespace ro {
//Has move semantics. //Has move semantics.
class sql : public std::unique_ptr<Icompiled_sql> { class sql : public std::unique_ptr<Icompiled_sql> {
public: public:
class null {}; class monostate {};
sql(ISqlite3* p, const char* sz) :std::unique_ptr<Icompiled_sql>(sqlite3_prepare(p, sz)) {} sql(ISqlite3* p, const char* sz) :std::unique_ptr<Icompiled_sql>(sqlite3_prepare(p, sz)) {}
sql(const std::unique_ptr<ISqlite3>& p, const char* sz) :std::unique_ptr<Icompiled_sql>(sqlite3_prepare(p.get(), sz)) {} sql(const std::unique_ptr<ISqlite3>& p, const char* sz) :std::unique_ptr<Icompiled_sql>(sqlite3_prepare(p.get(), sz)) {}
// copy constructor // copy constructor
@ -64,7 +64,7 @@ namespace ro {
else { else {
assert(false); assert(false);
//, "Don't know how to read this datatype from database"); //, "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 { else {
assert(false); 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; return *this;
} }
void bind(int i, null) { (*this)->Isqlite3_bind(i); } void bind(int i, monostate) { (*this)->Isqlite3_bind(i); }
template < typename T, template < typename T,
typename std::enable_if<is_sqlite3_field_type<T>::value, int >::type dummy_arg = 0 > typename std::enable_if<is_sqlite3_field_type<T>::value, int >::type dummy_arg = 0 >
void bind(int i, T j) { void bind(int i, T j) {

View File

@ -20,6 +20,15 @@ namespace ro {
constexpr bool is_standard_signed_integer = constexpr bool is_standard_signed_integer =
_Is_any_of_v<std::remove_cvref_t<T>, signed char, signed short, signed int, signed long, signed long long>; _Is_any_of_v<std::remove_cvref_t<T>, signed char, signed short, signed int, signed long, signed long long>;
template <class T>
constexpr bool is_standard_integer = ro::is_standard_unsigned_integer<T> || ro::is_standard_signed_integer<T>;
template <class T, class U = std::enable_if_t<is_standard_integer<T>,std::make_unsigned<std::remove_cvref_t<T>>::type>>
constexpr U iabs(T i) {
if constexpr (is_standard_signed_integer<T>)return U(abs(i));
else return i;
}
// A compile time test to see if desired casts work, and make sure that // A compile time test to see if desired casts work, and make sure that
// undesired casts do not work // undesired casts do not work
template <class T, class U> struct is_constructible_from { template <class T, class U> struct is_constructible_from {

View File

@ -27,19 +27,19 @@ namespace ristretto255 {
bool scalar::constant_time_required{ true }; bool scalar::constant_time_required{ true };
bool point::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) { if (i >= 0) {
auto k{ uint64_t(i) }; auto k{ intmax_t(i) };
for (auto& j : blob) { j = k; k = k >> 8; } for (auto& j : blob) { j = k; k = k >> 8; }
} }
else { else {
std::array<uint8_t, crypto_core_ristretto255_BYTES> absdata; std::array<uint8_t, crypto_core_ristretto255_BYTES> absdata;
auto k{ uint64_t(-i) }; auto k{ uintmax_t(-i) };
for (auto& j : absdata) { j = k; k = k >> 8; } for (auto& j : absdata) { j = k; k = k >> 8; }
crypto_core_ristretto255_scalar_negate(&blob[0], &absdata[0]); crypto_core_ristretto255_scalar_negate(&blob[0], &absdata[0]);
} }
} }
*/
point point::operator*(const scalar &sclr) const& noexcept { point point::operator*(const scalar &sclr) const& noexcept {
point me; point me;
auto i{ crypto_scalarmult_ristretto255(&me.blob[0], &sclr.blob[0], &blob[0]) }; auto i{ crypto_scalarmult_ristretto255(&me.blob[0], &sclr.blob[0], &blob[0]) };

View File

@ -653,10 +653,13 @@ namespace ristretto255 {
~scalar() = default; ~scalar() = default;
explicit constexpr scalar(std::array<uint8_t, crypto_core_ristretto255_BYTES>&& in) : blob{ in } {}; explicit constexpr scalar(std::array<uint8_t, crypto_core_ristretto255_BYTES>&& in) : blob{ in } {};
explicit constexpr scalar(std::array<uint8_t, crypto_core_ristretto255_BYTES>* in) :blob(*in) {}; explicit constexpr scalar(std::array<uint8_t, crypto_core_ristretto255_BYTES>* in) :blob(*in) {};
explicit constexpr scalar(int64_t); explicit constexpr scalar(uintmax_t k){ for (auto& j : blob) { j = k; k = k >> 8; } }
explicit constexpr scalar(int i):scalar(int64_t(i)){} template <class T, class U = std::enable_if_t<ro::is_standard_integer<T>, uintmax_t>>
explicit constexpr scalar(uint64_t k){ for (auto& j : blob) { j = k; k = k >> 8; } } explicit constexpr scalar(T i) :scalar(U(ro::iabs<T>(i))) {
explicit constexpr scalar(unsigned int i) :scalar(uint64_t(i)) {} if constexpr (ro::is_standard_signed_integer<T>) {
if (i < 0) crypto_core_ristretto255_scalar_negate(&blob[0], &blob[0]);
}
}
scalar(scalar&&) = default; // Move constructor scalar(scalar&&) = default; // Move constructor
scalar(const scalar&) = default; // Copy constructor scalar(const scalar&) = default; // Copy constructor
scalar& operator=(scalar&&) = default; // Move assignment. scalar& operator=(scalar&&) = default; // Move assignment.

View File

@ -11,10 +11,10 @@ public:
}; };
class UnitTestEndException : public MyException { class EndUnitTestOfExceptions : public MyException {
public: public:
using MyException::MyException; using MyException::MyException;
UnitTestEndException() noexcept : EndUnitTestOfExceptions() noexcept :
MyException("\t\tEnd unit test") {} MyException("\t\tEnd unit test") {}
}; };
@ -65,9 +65,9 @@ static bool EndUnitTest() {
static std::string intestbed("Testbed: "); static std::string intestbed("Testbed: ");
try { try {
testbed::testbed(); testbed::testbed();
throw UnitTestEndException(); throw EndUnitTestOfExceptions();
} }
catch (const UnitTestEndException&) {} catch (const EndUnitTestOfExceptions&) {}
catch (const std::exception& e) { catch (const std::exception& e) {
errorCode = 1001; errorCode = 1001;
szError = intestbed + e.what(); szError = intestbed + e.what();
@ -804,6 +804,7 @@ static bool TestShareSecretGenerationSpeed(void) {
&& scalar(0x0103) == scalar(0xFE) + scalar(5) && scalar(0x0103) == scalar(0xFE) + scalar(5)
&& scalar(0xDEADBEEF) == scalar(0xDEADBDDE) + scalar(0x111) && scalar(0xDEADBEEF) == scalar(0xDEADBDDE) + scalar(0x111)
&& scalar(0xBEEFDEADBEEF) == scalar(0xBEEFDEADBDDE) + scalar(0x111) && scalar(0xBEEFDEADBEEF) == scalar(0xBEEFDEADBDDE) + scalar(0x111)
&& scalar(0xBEEF0000000F) + scalar(-0xBEEF0EADBEEF) == scalar(int(-0xEADBEE0))
) )
) )
{ {