From e7c0ad2cf0e9c0c88bb6da5d2176ea0dbe3978ab Mon Sep 17 00:00:00 2001 From: Cheng Date: Wed, 27 Nov 2024 03:58:27 +0000 Subject: [PATCH] random generation in a range functor with seed in an instance of a class --- src/secrets.cpp | 29 +++++++++++++++++++++++++++++ src/secrets.h | 15 +++++++++++++++ src/testbed.cpp | 31 ++++--------------------------- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/secrets.cpp b/src/secrets.cpp index 9cec927..7b00c39 100644 --- a/src/secrets.cpp +++ b/src/secrets.cpp @@ -81,6 +81,35 @@ ristretto255::scalar DeriveSecret(const scalar &sc, uint_fast64_t i) { return ristretto255::scalar(ristretto255::hash<512>(sc, i)); } +uint32_t randInRangeClass::rand() { + hash<256>& mybase(*this); + if (--i < 0) { + mybase = hash<256>(hsh<256>() << (mybase)); + //hash of hash, to get a new block of eight pseudo random values. + i = 7; + } + return static_cast(static_cast(&(mybase.blob[0])))[i]; +} + +uint32_t randInRangeClass::operator()(uint32_t range) { + assert(range != 0); + if (range == 0) throw(sz_unexpected_error); //There are no valid values in the range zero to less than zero + uint64_t random32bit = this->rand(); //32-bit random number + uint64_t multiresult = random32bit * range; + uint32_t leftover = (uint32_t)multiresult; + if (leftover < range) { + // Discard potentially biased results + uint32_t threshold = (UINT32_MAX + 1 - range) % range; + while (leftover < threshold) { + random32bit = this->rand(); + multiresult = random32bit * range; + leftover = (uint32_t)multiresult; + } + } + return multiresult >> 32; +}; + + namespace ristretto255 { extended_public_key::extended_public_key(point&& p, hash<256>&& h) : publickey(p), diff --git a/src/secrets.h b/src/secrets.h index f4ee6ab..a3729d8 100644 --- a/src/secrets.h +++ b/src/secrets.h @@ -14,6 +14,21 @@ constexpr int rounded_log2(const T val) noexcept { } std::string DeriveWordListSecret(); +class randInRangeClass :ristretto255::hash<256> { + // generates a pseudo random sequence determined by + // the initializer of this instance of the class + int i = 8; + uint32_t rand(); // pseudo random function in range zero to UINT32_MAX inclusive; +public: + using hash::hash; + randInRangeClass() = delete; + uint32_t operator()(uint32_t range); + // pseudo random functor that returns a value + // in the range zero to less than argument. + // with the pseudo random sequence determined by + // the initializer of this instance of the class. +}; + namespace ristretto255 { class extended_public_key; class extended_private_key { diff --git a/src/testbed.cpp b/src/testbed.cpp index c7f2e68..abdf19f 100644 --- a/src/testbed.cpp +++ b/src/testbed.cpp @@ -64,8 +64,6 @@ If using queumessage, the testbed code will complete while the dialog */ void testbed() { - hsh a_hsh=hsh().hashinto("the quick brown fox", "jumped over the lazy dog"); - hash<256> a_hash = a_hsh; // wxVersionInfo wx = wxWebView::GetBackendVersionInfo(wxASCII_STR(wxWebViewBackendDefault)); // wxLogMessage(wx.ToString()); // wx = wxWebView::GetBackendVersionInfo(wxASCII_STR(wxWebViewBackendEdge)); @@ -73,32 +71,11 @@ If using queumessage, the testbed code will complete while the dialog // queue_error_message("hello world"); // throw MyException("hello world exception", __LINE__, __func__, SrcFilename); ILogMessage("begin testbed"); -/* auto text_secret{DeriveTextSecret(scalar(7), 1)}; - if (text_secret == decltype(text_secret){"Rmc mLSu mDk DhfV 9gBK kKj"}) { - ILogMessage("\t\tObtained expected text secret from scalar(7)"); + /* randInRangeClass randInRange("the quicku brown fox"); + for (int i = 0; i < 100; i++) { + wxLogMessage(wxT("%d "), randInRange(1000000000)); } - else { - errorCode = 18; - szError = "Fail\tUnexpected text secret from scalar(7)"; - ILogError(szError.c_str()); - } - auto start_time{ std::chrono::high_resolution_clock::now() }; - extended_private_key s1(& text_secret[0]); - auto end_time{ std::chrono::high_resolution_clock::now() }; - std::string str_s1{ &(base58(s1))[0] }; - wxLogMessage("\t\textended private key: %s", str_s1); - const char * str_s2 = "jyRioJfob242toZDETkd7nG4YW6oYeGBEVEw4KLGWZaJfCPV2hQbT3AFUcnu6ZXEvDPPpSmM5ivMJ2awJxSBS5DoE"; - if (str_s2 != str_s1) { - throw MyException("unexpected value of private key", __LINE__, __func__, SrcFilename); - } - auto s2 = base58::bin(str_s2); - if ( s1 != s2) { - throw MyException("Round trip for extended_private_key to and from base 58 representation failed", __LINE__, __func__, SrcFilename); - } - - auto time_taken{ std::chrono::duration_cast (end_time - start_time) }; - wxLogMessage("\t\tStrong secret derivation took %lld microseconds", time_taken.count());*/ - + */ ILogMessage("end testbed"); } }