Skip to content

Commit 152e8ba

Browse files
committed
Use salted hasher instead of nonce in sigcache
Use salted hasher instead of nonce in Script Execution Cache Don't read more than 32 bytes from GetRand Apply g_* naming convention to scriptExecutionCache in validation.cpp Fully apply g_* naming convention to scriptCacheHasher Write same uint256 nonce twice for cache hash rather than calling getrand twice Use salted hasher instead of nonce in sigcache Use salted hasher instead of nonce in Script Execution Cache Don't read more than 32 bytes from GetRand Apply g_* naming convention to scriptExecutionCache in validation.cpp Fully apply g_* naming convention to scriptCacheHasher Write same uint256 nonce twice for cache hash rather than calling getrand twice
1 parent 5495fa5 commit 152e8ba

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/script/sigcache.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,27 @@ class CSignatureCache
2323
{
2424
private:
2525
//! Entries are SHA256(nonce || signature hash || public key || signature):
26-
uint256 nonce;
26+
CSHA256 m_salted_hasher;
2727
typedef CuckooCache::cache<uint256, SignatureCacheHasher> map_type;
2828
map_type setValid;
2929
boost::shared_mutex cs_sigcache;
3030

3131
public:
3232
CSignatureCache()
3333
{
34-
GetRandBytes(nonce.begin(), 32);
34+
uint256 nonce = GetRandHash();
35+
// We want the nonce to be 64 bytes long to force the hasher to process
36+
// this chunk, which makes later hash computations more efficient. We
37+
// just write our 32-byte entropy twice to fill the 64 bytes.
38+
m_salted_hasher.Write(nonce.begin(), 32);
39+
m_salted_hasher.Write(nonce.begin(), 32);
3540
}
3641

3742
void
3843
ComputeEntry(uint256& entry, const uint256 &hash, const std::vector<unsigned char>& vchSig, const CPubKey& pubkey)
3944
{
40-
CSHA256().Write(nonce.begin(), 32).Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
45+
CSHA256 hasher = m_salted_hasher;
46+
hasher.Write(hash.begin(), 32).Write(&pubkey[0], pubkey.size()).Write(&vchSig[0], vchSig.size()).Finalize(entry.begin());
4147
}
4248

4349
bool

src/validation.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,14 +1481,21 @@ int GetSpendHeight(const CCoinsViewCache& inputs)
14811481
}
14821482

14831483

1484-
static CuckooCache::cache<uint256, SignatureCacheHasher> scriptExecutionCache;
1485-
static uint256 scriptExecutionCacheNonce(GetRandHash());
1484+
static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
1485+
static CSHA256 g_scriptExecutionCacheHasher;
14861486

14871487
void InitScriptExecutionCache() {
1488+
// Setup the salted hasher
1489+
uint256 nonce = GetRandHash();
1490+
// We want the nonce to be 64 bytes long to force the hasher to process
1491+
// this chunk, which makes later hash computations more efficient. We
1492+
// just write our 32-byte entropy twice to fill the 64 bytes.
1493+
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
1494+
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
14881495
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
14891496
// setup_bytes creates the minimum possible cache (2 elements).
14901497
size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
1491-
size_t nElems = scriptExecutionCache.setup_bytes(nMaxCacheSize);
1498+
size_t nElems = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
14921499
LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
14931500
(nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
14941501
}
@@ -1526,12 +1533,10 @@ bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const C
15261533
// properly commits to the scriptPubKey in the inputs view of that
15271534
// transaction).
15281535
uint256 hashCacheEntry;
1529-
// We only use the first 19 bytes of nonce to avoid a second SHA
1530-
// round - giving us 19 + 32 + 4 = 55 bytes (+ 8 + 1 = 64)
1531-
static_assert(55 - sizeof(flags) - 32 >= 128/8, "Want at least 128 bits of nonce for script execution cache");
1532-
CSHA256().Write(scriptExecutionCacheNonce.begin(), 55 - sizeof(flags) - 32).Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
1536+
CSHA256 hasher = g_scriptExecutionCacheHasher;
1537+
hasher.Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
15331538
AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
1534-
if (scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) {
1539+
if (g_scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) {
15351540
return true;
15361541
}
15371542

@@ -1586,7 +1591,7 @@ bool CheckInputScripts(const CTransaction& tx, TxValidationState &state, const C
15861591
if (cacheFullScriptStore && !pvChecks) {
15871592
// We executed all of the provided scripts, and were told to
15881593
// cache the result. Do so now.
1589-
scriptExecutionCache.insert(hashCacheEntry);
1594+
g_scriptExecutionCache.insert(hashCacheEntry);
15901595
}
15911596

15921597
return true;

0 commit comments

Comments
 (0)