Downcasting in difficulty calculation can lead to overflow #3923
Labels
C-bug
Category - fixes a bug, typically associated with an issue.
E-good_first_issue
Experience Level - Good for newcomers
The difficulty calculation has an overflow issue:
Here,
scalar
is the target for the mining algorithm. The smaller the target, the more difficult it is to find a hash with the requisite number of leading zeroes.The definition of difficulty is
TARGET_MAX / target
. Once target falls below 2^192, the difficulty will overflow to zero, since we downcast to 64 bits by truncating the highest portion of the actual difficulty.Now, this is possibly only a theoretical problem since we need a ridiculous hashrate before this become an eventuality. We will certainly have native 256-bit integers long before this happens.
However, to protect against weird things happening, it might be better to prevent the overflow anyway by returning
u64::max
if scalar <= 2^192.The text was updated successfully, but these errors were encountered: