Made GPrng::GetBlock() faster by a factor of 100
This commit is contained in:
parent
660ab5e999
commit
e9377699f2
@ -46,29 +46,23 @@ namespace Leonetienne::GCrypt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Block GPrng::GetBlock() {
|
Block GPrng::GetBlock() {
|
||||||
// Getting a block is a bit troublesome.
|
// Tactic on efficiently generating a new block:
|
||||||
// Just fetching 512 bits would be too much of a performance hog.
|
// 1) Fetch complete current hashsum (it might have been partially given out already)
|
||||||
|
// 2) Bitshift it, and matrix-mult it with the seed (that is irreversible)
|
||||||
|
// That should be a one-way function, and create a new unique block.
|
||||||
|
// We don't even have to AdvanceBlock(), because we've only given out
|
||||||
|
// hashsum', not hashsum.
|
||||||
|
|
||||||
// Slurp up the rest of the current block
|
// Fetch our current block
|
||||||
std::stringstream ss;
|
Block hashsum = hasher.GetHashsum();
|
||||||
const std::size_t bitsLeft = Block::BLOCK_SIZE_BITS - nextBit;
|
|
||||||
ss << hasher.GetHashsum().ToString().substr(nextBit, bitsLeft);
|
|
||||||
|
|
||||||
// Now we have to advance to the next block
|
// Derive/'hash' it to hashsum'
|
||||||
AdvanceBlock();
|
hashsum *= seed;
|
||||||
|
hashsum.ShiftBitsLeftInplace();
|
||||||
|
hashsum *= seed;
|
||||||
|
|
||||||
// Now, grab the remaining bits
|
// Return our hashsum
|
||||||
const std::size_t remainingBits = Block::BLOCK_SIZE_BITS - bitsLeft;
|
return hashsum;
|
||||||
ss << hasher.GetHashsum().ToString().substr(0, remainingBits);
|
|
||||||
|
|
||||||
// Assert that we have the correct number of bits
|
|
||||||
assert(ss.str().length() == Block::BLOCK_SIZE_BITS);
|
|
||||||
|
|
||||||
// Set out bitpointer
|
|
||||||
nextBit = remainingBits;
|
|
||||||
|
|
||||||
// Return our block
|
|
||||||
return Block(ss.str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user