diff --git a/src/ristretto255.h b/src/ristretto255.h index 0460991..377f129 100644 --- a/src/ristretto255.h +++ b/src/ristretto255.h @@ -146,10 +146,11 @@ namespace ro { // template class that generates a std::span of bytes over the blob // field of any object containing a blob record, which is normally sufficient // for a machine independent representation of that object - template < - typename T, - decltype(std::size(std::declval().blob)) dummy_arg = 0 - > auto serialize(const T& pt) { + template + std::enable_if_t< + is_blob_field_type::value, + std::span + > serialize(const T& pt) { return serialize(pt.blob); } @@ -182,17 +183,17 @@ namespace ro { template, int> = 0> class userial : public std::span { public: - std::array::digits + 6) / 7> blob; + std::array::digits + 6) / 7> bblob; userial(T i) { - byte* p = &blob[0] + sizeof(blob); + byte* p = &bblob[0] + sizeof(bblob); *(--p) = i & 0x7f; i >>= 7; while (i != 0) { *(--p) = (i & 0x7f) | 0x80; i >>= 7; } - assert(p >= &blob[0]); - *static_cast*>(this) = std::span(p, &blob[0] + sizeof(blob));; + assert(p >= &bblob[0]); + *static_cast*>(this) = std::span(p, &bblob[0] + sizeof(bblob));; } }; @@ -200,10 +201,10 @@ namespace ro { template, int> = 0> class iserial : public std::span { public: - std::array::digits + 7) / 7> blob; + std::array::digits + 7) / 7> bblob; iserial(T i) { // Throw away the repeated leading bits, and g - byte* p = &blob[0] + sizeof(blob); + byte* p = &bblob[0] + sizeof(bblob); unsigned count; if (i < 0) { size_t ui = i; @@ -218,8 +219,8 @@ namespace ro { i >>= 7; *(--p) = (i & 0x7f) | 0x80; } - assert(p >= &blob[0]); - *static_cast*>(this) = std::span(p, &blob[0] + sizeof(blob));; + assert(p >= &bblob[0]); + *static_cast*>(this) = std::span(p, &bblob[0] + sizeof(bblob));; } }; @@ -462,13 +463,6 @@ namespace ristretto255 { } } - template < typename T> - decltype(ro::serialize(std::declval::type >()), hsh())& - operator <<(const T & j) { - return *this << ro::serialize(j); - } - - template::value, int >::type dummy_arg = 0 > void hashinto(const T first, Args... args) { @@ -477,27 +471,45 @@ namespace ristretto255 { (*this).hashinto(args...); } } - - hsh& operator <<(const std::span j) { + }; + + template + ristretto255::hsh& operator <<(ristretto255::hsh u, const T& j) { + if constexpr (std::is_same_v, std::span >) { int i = crypto_generichash_blake2b_update( - &st, + &u.st, &j[0], j.size() ); if (i) throw HashReuseException(); - return *this; + return u; } - - hsh& operator <<(char* sz) { + else if constexpr (std::is_same_v, const char*>) { int i = crypto_generichash_blake2b_update( - &st, - static_cast(sz), - strlen(sz) + 1 + &u.st, + (const unsigned char *)(j), + strlen(j) + 1 ); if (i) throw HashReuseException(); - return *this; + return u; } - }; + else { + return u << ro::serialize(j); + } + } + + template + ristretto255::hsh& operator <<(ristretto255::hsh u, const char* sz) { + int i = crypto_generichash_blake2b_update( + &u.st, + static_cast(sz), + // (unsigned char*)(sz), + strlen(sz) + 1 + ); + if (i) throw HashReuseException(); + return u; + } + // This constructs a finalized hash. // If it has one argument, and that argument is hsh (unfinalized hash) object,