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.
This commit is contained in:
Cheng 2022-05-05 13:52:12 +10:00
parent 1b3bb9f35e
commit 2133f51ffe
No known key found for this signature in database
GPG Key ID: D51301E176B31828

View File

@ -523,9 +523,9 @@ namespace ristretto255 {
} }
} }
if (!(singletonApp->m_quick_unit_test)){ 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]) }; 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 })) { 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)"); 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"; szError = "Fail\tUnexpected strong scalar secret from text secret";
ILogError(szError.c_str()); ILogError(szError.c_str());
} }
wxLogMessage("\t\tStrong secret derivation took %d milliseconds", (end_time - start_time).count()); auto time_taken{ std::chrono::duration_cast<std::chrono::microseconds> (end_time - start_time) };
uintmax_t strengthening = (end_time - start_time).count() * shared_secrets_per_second / 1000; 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\tslows brute force password search by a factor of %ju", strengthening);
wxLogMessage("\t\tstrengthens password by %u bits", wxLogMessage("\t\tstrengthens password by %u bits",
static_cast<unsigned int>(rounded_log2(static_cast<uint32_t>(strengthening)))); static_cast<unsigned int>(rounded_log2(static_cast<uint32_t>(strengthening))));
@ -571,13 +572,13 @@ static bool TestShareSecretGenerationSpeed(void) {
ILogMessage("\tTest shared secret generation speed."); ILogMessage("\tTest shared secret generation speed.");
try { try {
// throw MyException("fake failure to test failure handling"); // 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 a{ scalar::random() };
scalar b{ scalar::random() }; scalar b{ scalar::random() };
scalar c; scalar c;
auto B{ a.timesBase() }; auto B{ a.timesBase() };
decltype(B) A, C; 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--) { for (unsigned int i{ secrets }; i; i--) {
c = a + b; c = a + b;
C = c * B; C = c * B;
@ -586,11 +587,12 @@ static bool TestShareSecretGenerationSpeed(void) {
b = c + a; b = c + a;
B = b * 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"); } 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()); auto time_taken{ std::chrono::duration_cast<std::chrono::microseconds> (end_time - start_time) };
if ((end_time - start_time).count()) { wxLogMessage("\t\t%d shared secrets from public and private keys took %lld microseconds", secrets * 3, time_taken.count());
shared_secrets_per_second = secrets * 3000 / (end_time - start_time).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", wxLogMessage("\t\t in one second, can generate %ju shared secrets",
shared_secrets_per_second shared_secrets_per_second
); );
@ -624,11 +626,12 @@ static bool TestShareSecretGenerationSpeed(void) {
ptAnnPublicKey, // Signer's public key ptAnnPublicKey, // Signer's public key
szHello // Text to be signed. 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()) if (sig.verify())
{ {
decltype(start_time) end_time{ ro::msec_since_epoch() }; auto end_time{ std::chrono::high_resolution_clock::now() };
wxLogMessage("\t\tverification of Schnorr signature took %d milliseconds", (end_time - start_time).count()); auto time_taken{ std::chrono::duration_cast<std::chrono::microseconds> (end_time - start_time) };
wxLogMessage("\t\tverification of Schnorr signature took %lld microseconds", time_taken.count());
ILogMessage("\t\tSchnorr Signature valid"); ILogMessage("\t\tSchnorr Signature valid");
} }
@ -760,7 +763,7 @@ static bool TestShareSecretGenerationSpeed(void) {
szError = "Fail\tInvalid test vectors."; szError = "Fail\tInvalid test vectors.";
ILogError(szError.c_str()); 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_a = scalar::random();
auto sclr_b = scalar::random(); auto sclr_b = scalar::random();
if (sclr_a == sclr_b)throw MyException("random not very random"); if (sclr_a == sclr_b)throw MyException("random not very random");
@ -815,11 +818,11 @@ static bool TestShareSecretGenerationSpeed(void) {
} }
else else
{ {
decltype(start_time) end_time{ ro::msec_since_epoch() }; auto end_time{ std::chrono::high_resolution_clock::now() };
auto time_to_do_crypto{ end_time - start_time }; auto time_to_do_crypto{ std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time) };
wxLogMessage(_T("\t\ttest of ristretto test vectors took %d milliseconds"), time_to_do_crypto.count()); wxLogMessage(_T("\t\ttest of ristretto test vectors took %lld microseconds"), time_to_do_crypto.count());
ILogMessage("\tTesting generation of shared secrets."); ILogMessage("\tTesting generation of shared secrets.");
start_time = ro::msec_since_epoch(); start_time = std::chrono::high_resolution_clock::now();
scalar sclrAnonSessionSecretKey{ scalar::random() }; scalar sclrAnonSessionSecretKey{ scalar::random() };
point ptAnonSessionPublicKey{ sclrAnonSessionSecretKey * point::ptBase }; point ptAnonSessionPublicKey{ sclrAnonSessionSecretKey * point::ptBase };
@ -835,25 +838,27 @@ static bool TestShareSecretGenerationSpeed(void) {
ILogError(szError.c_str()); ILogError(szError.c_str());
} }
else { else {
end_time = ro::msec_since_epoch(); end_time = std::chrono::high_resolution_clock::now();
wxLogMessage(_wx("\tshared secret generation both ways and comparison took %d milliseconds"), (end_time - start_time).count()); auto time_taken{ std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time) };
wxLogMessage(_wx("\tshared secret generation both ways and comparison took %lld microseconds"), time_taken.count());
} }
ILogMessage("\tTesting hashing speed"); ILogMessage("\tTesting hashing speed");
{ {
int hashes{ 20000 }; int hashes{ 20000 };
point sharedSecretKey = sclrAnonSessionSecretKey * ptServerPermanentPublicKey; point sharedSecretKey = sclrAnonSessionSecretKey * ptServerPermanentPublicKey;
ServerCopyOfSharedSecret = hash<512>(sharedSecretKey, MessageTextToBeSecured); 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++) { for (int i{ 0 }; i < hashes; i++) {
sharedSecretKey.blob[3] = ServerCopyOfSharedSecret.blob[5]; sharedSecretKey.blob[3] = ServerCopyOfSharedSecret.blob[5];
ServerCopyOfSharedSecret = hash<512>(sharedSecretKey, MessageTextToBeSecured); 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<std::chrono::microseconds>(end_time - start_time) };
{ bool f = false; //dont optimize pointless calculation { bool f = false; //dont optimize pointless calculation
for (auto x : ServerCopyOfSharedSecret.blob) { f = f || x; } for (auto x : ServerCopyOfSharedSecret.blob) { f = f || x; }
if (f) { 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\t%d hashes of pairs of shared secrets took %lld microseconds"), hashes, time_taken.count());
wxLogMessage(_wx("\t\tin one millisecond, can hash %d short messages"), hashes / (end_time - start_time).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); 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 }; 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++) { for (int i{ 0 }; i < hashes; i++) {
a = hash(b, c); a = hash(b, c);
b = hash(c, a); b = hash(c, a);
c = hash(a, b); 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<std::chrono::microseconds> (end_time - start_time) };
{ bool f = false; //dont optimize pointless calculation away { bool f = false; //dont optimize pointless calculation away
for (auto x : c.blob) { f = f || x; } for (auto x : c.blob) { f = f || x; }
if (f) { if (f) {
wxLogMessage(_wx("\t\t%d hashes of a pair of hashes took %d milliseconds"), 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 ((end_time - start_time).count() > 0) { if (time_taken.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\tin one millisecond, can hash %lld pairs of 256 bit hashes"), hashes * 3000ll / time_taken.count());
} }
} }
} }
uint8_t buff[hashes * 3 * 64]; uint8_t buff[hashes * 3 * 64];
randombytes_buf(std::span<byte>(buff)); randombytes_buf(std::span<byte>(buff));
start_time = ro::msec_since_epoch(); start_time = std::chrono::high_resolution_clock::now();
c = hash(buff); c = hash(buff);
end_time = ro::msec_since_epoch(); end_time = std::chrono::high_resolution_clock::now();
time_taken = std::chrono::duration_cast<std::chrono::microseconds> (end_time - start_time);
{ bool f = false; //dont optimize pointless calculation away { bool f = false; //dont optimize pointless calculation away
for (auto x : c.blob) { f = f || x; } for (auto x : c.blob) { f = f || x; }
if (f) { 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());
} }
} }
} }