From 2133f51ffe582c9b2e22c9a45f956c8ca1db5100 Mon Sep 17 00:00:00 2001 From: Cheng Date: Thu, 5 May 2022 13:52:12 +1000 Subject: [PATCH] My too clever time code is obsolete now that C++ has long had good support for durations. Switched everything that needed a high resolution timer to use a high resolution timer. --- unit_test.cpp | 71 ++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/unit_test.cpp b/unit_test.cpp index 8edc6a9..f10c5fd 100644 --- a/unit_test.cpp +++ b/unit_test.cpp @@ -523,9 +523,9 @@ namespace ristretto255 { } } if (!(singletonApp->m_quick_unit_test)){ - ro::msec start_time{ ro::msec_since_epoch() }; + auto start_time{ std::chrono::high_resolution_clock::now() }; auto s1{ DeriveStrongSecret(&text_secret[0]) }; - decltype(start_time) end_time{ ro::msec_since_epoch() }; + auto end_time{ std::chrono::high_resolution_clock::now() }; if (s1 == scalar({ 0x59, 0xf6, 0x73, 0xb4, 0xa0, 0xc7, 0x0d, 0x8b, 0x51, 0xe5, 0x87, 0x7c, 0xf5, 0xd7, 0x6f, 0x55, 0x31, 0xa7, 0x0b, 0x14, 0x28, 0x54, 0x97, 0x08, 0x9f, 0x27, 0x83, 0xe1, 0xc7, 0x5f, 0x55, 0x0f })) { wxLogMessage("\t\tObtained expected strong scalar secret from scalar(7)"); } @@ -534,8 +534,9 @@ namespace ristretto255 { szError = "Fail\tUnexpected strong scalar secret from text secret"; ILogError(szError.c_str()); } - wxLogMessage("\t\tStrong secret derivation took %d milliseconds", (end_time - start_time).count()); - uintmax_t strengthening = (end_time - start_time).count() * shared_secrets_per_second / 1000; + auto time_taken{ std::chrono::duration_cast (end_time - start_time) }; + wxLogMessage("\t\tStrong secret derivation took %lld microseconds", time_taken.count()); + uintmax_t strengthening = time_taken.count() * shared_secrets_per_second / 1000000; wxLogMessage("\t\tslows brute force password search by a factor of %ju", strengthening); wxLogMessage("\t\tstrengthens password by %u bits", static_cast(rounded_log2(static_cast(strengthening)))); @@ -571,13 +572,13 @@ static bool TestShareSecretGenerationSpeed(void) { ILogMessage("\tTest shared secret generation speed."); try { // throw MyException("fake failure to test failure handling"); - unsigned int secrets{ 10*(3-2*debug_mode) }; + unsigned int secrets{ 10 }; scalar a{ scalar::random() }; scalar b{ scalar::random() }; scalar c; auto B{ a.timesBase() }; decltype(B) A, C; - ro::msec start_time{ ro::msec_since_epoch() }; + auto start_time{ std::chrono::high_resolution_clock::now() }; for (unsigned int i{ secrets }; i; i--) { c = a + b; C = c * B; @@ -586,11 +587,12 @@ static bool TestShareSecretGenerationSpeed(void) { b = c + a; B = b * A; } - decltype(start_time) end_time{ ro::msec_since_epoch() }; + auto end_time{ std::chrono::high_resolution_clock::now() }; if (!B.valid()) { throw MyException("Unexpected invalid scalar"); } - wxLogMessage("\t\t%d shared secrets from public and private keys took %d milliseconds", secrets * 3, (end_time - start_time).count()); - if ((end_time - start_time).count()) { - shared_secrets_per_second = secrets * 3000 / (end_time - start_time).count(); + auto time_taken{ std::chrono::duration_cast (end_time - start_time) }; + wxLogMessage("\t\t%d shared secrets from public and private keys took %lld microseconds", secrets * 3, time_taken.count()); + if (time_taken.count()) { + shared_secrets_per_second = secrets * 3000000 / time_taken.count(); wxLogMessage("\t\t in one second, can generate %ju shared secrets", shared_secrets_per_second ); @@ -624,11 +626,12 @@ static bool TestShareSecretGenerationSpeed(void) { ptAnnPublicKey, // Signer's public key szHello // Text to be signed. ); - ro::msec start_time{ ro::msec_since_epoch() }; + auto start_time{ std::chrono::high_resolution_clock::now() }; if (sig.verify()) { - decltype(start_time) end_time{ ro::msec_since_epoch() }; - wxLogMessage("\t\tverification of Schnorr signature took %d milliseconds", (end_time - start_time).count()); + auto end_time{ std::chrono::high_resolution_clock::now() }; + auto time_taken{ std::chrono::duration_cast (end_time - start_time) }; + wxLogMessage("\t\tverification of Schnorr signature took %lld microseconds", time_taken.count()); ILogMessage("\t\tSchnorr Signature valid"); } @@ -760,7 +763,7 @@ static bool TestShareSecretGenerationSpeed(void) { szError = "Fail\tInvalid test vectors."; ILogError(szError.c_str()); } - ro::msec start_time{ ro::msec_since_epoch() }; + auto start_time{ std::chrono::high_resolution_clock::now() }; auto sclr_a = scalar::random(); auto sclr_b = scalar::random(); if (sclr_a == sclr_b)throw MyException("random not very random"); @@ -815,11 +818,11 @@ static bool TestShareSecretGenerationSpeed(void) { } else { - decltype(start_time) end_time{ ro::msec_since_epoch() }; - auto time_to_do_crypto{ end_time - start_time }; - wxLogMessage(_T("\t\ttest of ristretto test vectors took %d milliseconds"), time_to_do_crypto.count()); + auto end_time{ std::chrono::high_resolution_clock::now() }; + auto time_to_do_crypto{ std::chrono::duration_cast(end_time - start_time) }; + wxLogMessage(_T("\t\ttest of ristretto test vectors took %lld microseconds"), time_to_do_crypto.count()); ILogMessage("\tTesting generation of shared secrets."); - start_time = ro::msec_since_epoch(); + start_time = std::chrono::high_resolution_clock::now(); scalar sclrAnonSessionSecretKey{ scalar::random() }; point ptAnonSessionPublicKey{ sclrAnonSessionSecretKey * point::ptBase }; @@ -835,25 +838,27 @@ static bool TestShareSecretGenerationSpeed(void) { ILogError(szError.c_str()); } else { - end_time = ro::msec_since_epoch(); - wxLogMessage(_wx("\tshared secret generation both ways and comparison took %d milliseconds"), (end_time - start_time).count()); + end_time = std::chrono::high_resolution_clock::now(); + auto time_taken{ std::chrono::duration_cast(end_time - start_time) }; + wxLogMessage(_wx("\tshared secret generation both ways and comparison took %lld microseconds"), time_taken.count()); } ILogMessage("\tTesting hashing speed"); { int hashes{ 20000 }; point sharedSecretKey = sclrAnonSessionSecretKey * ptServerPermanentPublicKey; ServerCopyOfSharedSecret = hash<512>(sharedSecretKey, MessageTextToBeSecured); - start_time = ro::msec_since_epoch(); + start_time = std::chrono::high_resolution_clock::now(); for (int i{ 0 }; i < hashes; i++) { sharedSecretKey.blob[3] = ServerCopyOfSharedSecret.blob[5]; ServerCopyOfSharedSecret = hash<512>(sharedSecretKey, MessageTextToBeSecured); } - end_time = ro::msec_since_epoch(); + end_time = std::chrono::high_resolution_clock::now(); + auto time_taken{ std::chrono::duration_cast(end_time - start_time) }; { bool f = false; //dont optimize pointless calculation for (auto x : ServerCopyOfSharedSecret.blob) { f = f || x; } if (f) { - wxLogMessage(_wx("\t\t%d hashes of pairs of shared secrets took %d milliseconds"), hashes, (end_time - start_time).count()); - wxLogMessage(_wx("\t\tin one millisecond, can hash %d short messages"), hashes / (end_time - start_time).count()); + wxLogMessage(_wx("\t\t%d hashes of pairs of shared secrets took %lld microseconds"), hashes, time_taken.count()); + wxLogMessage(_wx("\t\tin one millisecond, can hash %lld short messages"), 1000LL*hashes / time_taken.count()); } } } @@ -864,31 +869,33 @@ static bool TestShareSecretGenerationSpeed(void) { hash c(b); if(hash(b, c) != hash(hsh() << b << c)) throw MyException("inconsistent hashes generated"); constexpr int hashes{ 3000 }; - start_time = ro::msec_since_epoch(); + start_time = std::chrono::high_resolution_clock::now(); for (int i{ 0 }; i < hashes; i++) { a = hash(b, c); b = hash(c, a); c = hash(a, b); } - end_time = ro::msec_since_epoch(); + end_time = std::chrono::high_resolution_clock::now(); + auto time_taken{ std::chrono::duration_cast (end_time - start_time) }; { bool f = false; //dont optimize pointless calculation away for (auto x : c.blob) { f = f || x; } if (f) { - wxLogMessage(_wx("\t\t%d hashes of a pair of hashes took %d milliseconds"), hashes * 3, (end_time - start_time).count()); - if ((end_time - start_time).count() > 0) { - wxLogMessage(_wx("\t\tin one millisecond, can hash %d pairs of 256 bit hashes"), hashes * 3 / (end_time - start_time).count()); + wxLogMessage(_wx("\t\t%d hashes of a pair of hashes took %lld microseconds"), hashes * 3, time_taken.count()); + if (time_taken.count() > 0) { + wxLogMessage(_wx("\t\tin one millisecond, can hash %lld pairs of 256 bit hashes"), hashes * 3000ll / time_taken.count()); } } } uint8_t buff[hashes * 3 * 64]; randombytes_buf(std::span(buff)); - start_time = ro::msec_since_epoch(); + start_time = std::chrono::high_resolution_clock::now(); c = hash(buff); - end_time = ro::msec_since_epoch(); + end_time = std::chrono::high_resolution_clock::now(); + time_taken = std::chrono::duration_cast (end_time - start_time); { bool f = false; //dont optimize pointless calculation away for (auto x : c.blob) { f = f || x; } if (f) { - wxLogMessage(_wx("\t\tone hash of %d bytes took %d milliseconds"), hashes * 3 * 64, (end_time - start_time).count()); + wxLogMessage(_wx("\t\tone hash of %d bytes took %lld microseconds"), hashes * 3 * 64, time_taken.count()); } } }