From 003d671c258f40f252762122d987222e30060410 Mon Sep 17 00:00:00 2001 From: Cheng Date: Fri, 17 Feb 2023 12:43:22 +0800 Subject: [PATCH] crash worked around But the way I fixed it proves it is a visual studio bug, not my bug --- src/ristretto255.h | 49 +++++++++++++++++++++++++++++++++------------- src/unit_test.cpp | 11 ++++++++++- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/ristretto255.h b/src/ristretto255.h index 1d98037..1a8c31a 100644 --- a/src/ristretto255.h +++ b/src/ristretto255.h @@ -446,6 +446,38 @@ namespace ristretto255 { }; assert(i == 0); } + + template + ristretto255::hsh& operator << (const T& j) { + if constexpr (std::is_same_v, std::span >) { + int i = crypto_generichash_blake2b_update( + &(this->st), + &j[0], + j.size() + ); + if (i) throw HashReuseException(); + return *this; + } + else if constexpr (std::is_same_v, const char*>) { + int i = crypto_generichash_blake2b_update( + &(this->st), + (const unsigned char*)(j), + strlen(j) + 1 + ); + if (i) throw HashReuseException(); + return *this; + } + else { + auto sj = ro::serialize(j); + int i = crypto_generichash_blake2b_update( + &(this->st), + (const unsigned char*)&sj[0], + sj.size() + ); + if (i) throw HashReuseException(); + return *this; + } + } template::value, int >::type dummy_arg = 0 >explicit hsh(const T first, Args... args) { @@ -470,9 +502,9 @@ namespace ristretto255 { } } }; - + /* template - ristretto255::hsh& operator <<(ristretto255::hsh u, const T& j) { + ristretto255::hsh& operator <<(ristretto255::hsh &u, const T& j) { if constexpr (std::is_same_v, std::span >) { int i = crypto_generichash_blake2b_update( &u.st, @@ -502,18 +534,7 @@ namespace ristretto255 { return u; } } - - 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. diff --git a/src/unit_test.cpp b/src/unit_test.cpp index ae8fb26..78b045b 100644 --- a/src/unit_test.cpp +++ b/src/unit_test.cpp @@ -885,11 +885,20 @@ static bool TestShareSecretGenerationSpeed(void) { } } ILogMessage("\tTesting hashing speed, hashes of hashes"); + auto first{ "first" }; + auto second{ "second" }; + if ( hash(first, second) + != + hash( + hsh() << first << second + ) + ) throw MyException("inconsistent hashes generated on strings"); const char* _ = "hello"; hash a(_); hash b(a); hash c(b); - if(hash(b, c) != hash(hsh() << b << c)) throw MyException("inconsistent hashes generated"); + if (hash(b, c) != hash(hsh() << b << c) + ) throw MyException("inconsistent hashes generated"); constexpr int hashes{ 3000 }; start_time = std::chrono::high_resolution_clock::now(); for (int i{ 0 }; i < hashes; i++) {