Skip to content

Commit

Permalink
fix: possible overflow in difficulty calculation (fixes #3923) (#4090)
Browse files Browse the repository at this point in the history
Description
 - First attempt to tackle issue #3923

Motivation and Context
 - The main goal is to avoid possible overflow of difficulty, which even though extremely unlikely, it is a possible scenario. 

How Has This Been Tested?
 - So far, changes have not been tested. I am more than happy to get recommendations of tests. I haven't add them at the moment because it is not clear how to build an hash whose big endian is a scalar less than 2^192. I am therefore, very interested to understand this concept and add tests accordingly.
  • Loading branch information
jorgeantonio21 committed May 12, 2022
1 parent cf91e91 commit e8d1091
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions base_layer/core/src/proof_of_work/difficulty.rs
Expand Up @@ -119,6 +119,7 @@ pub mod util {
pub(crate) fn big_endian_difficulty(hash: &[u8]) -> Difficulty {
let scalar = U256::from_big_endian(hash); // Big endian so the hash has leading zeroes
let result = U256::MAX / scalar;
let result = result.min(u64::MAX.into());
result.low_u64().into()
}

Expand All @@ -128,7 +129,38 @@ pub mod util {
let result = U256::MAX / scalar;
result.low_u64().into()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn high_target() {
let target: &[u8] = &[
0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
];
let expected = Difficulty::from(1);
assert_eq!(big_endian_difficulty(target), expected);
}

#[test]
fn max_difficulty() {
let target = U256::MAX / U256::from(u64::MAX);
let mut bytes = [0u8; 32];
target.to_big_endian(&mut bytes);
assert_eq!(big_endian_difficulty(&bytes), Difficulty::from(u64::MAX));
}

#[test]
fn stop_overflow() {
let target: u64 = 64;
let expected = u64::MAX;
assert_eq!(big_endian_difficulty(&target.to_be_bytes()), Difficulty::from(expected));
}
}
}

#[cfg(test)]
mod test {
use crate::proof_of_work::difficulty::Difficulty;
Expand Down

0 comments on commit e8d1091

Please sign in to comment.