Skip to content

Bitcoin difficulty calculation problems #736

@incker

Description

@incker

Hello!
My tests failed with error:

---- difficulty::test_target_difficulty stdout ----
thread 'difficulty::test_target_difficulty' panicked at 'assertion failed: your_bits != 0', /home/incker/.cargo/registry/src/github.com-1ecc6299db9ec823/bitcoin-0.27.1/src/util/uint.rs:493:1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Tests:

use bitcoin::{BlockHash, BlockHeader};
use bitcoin::util::uint::Uint256;
use bitcoin_hashes::Hash;
use bitcoin_hashes::hex::ToHex;

#[test]
fn test_target_difficulty() {
    struct Test {
        difficulty: u32,
        output: [u8; 32],
    }

    let difficulty_1_target_str =
        "00000000ffff0000000000000000000000000000000000000000000000000000";
    let difficulty_1_target_bytes: [u8; 32] = [
        0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00,
    ];
    let difficulty_1_target_uint256 = Uint256::from_be_bytes(difficulty_1_target_bytes);

    assert_eq!(
        &difficulty_1_target_uint256.to_be_bytes().to_hex(),
        &difficulty_1_target_str
    );

    let tests = vec![
        Test {
            difficulty: 512,
            output: [
                // 00000000007fff80000000000000000000000000000000000000000000000000
                0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
            ],
        },
        Test {
            difficulty: 1638,
            output: [
                // 0000000000280258258258258258258258258258258258258258258258258258
                0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x02, 0x58, 0x25, 0x82, 0x58, 0x25, 0x82, 0x58,
                0x25, 0x82, 0x58, 0x25, 0x82, 0x58, 0x25, 0x82, 0x58, 0x25, 0x82, 0x58, 0x25, 0x82,
                0x58, 0x25, 0x82, 0x58,
            ],
        },
        Test {
            difficulty: 8192,
            output: [
                // 000000000007fff8000000000000000000000000000000000000000000000000
                0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
            ],
        },
    ];

    for test in tests {
        let difficulty_target_uint256 = Uint256::from_be_bytes(test.output);
        assert_eq!(
            difficulty_1_target_uint256 / BlockHeader::u256_from_compact_target(test.difficulty),
            difficulty_target_uint256
        );
    }
}

When I was using uint = "0.9.1" crate for this tests, everything was ok

this crate version is bitcoin = "0.27.1"

Thank you for help

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionwaiting for authorThis can only progress if the author responds to a request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions