Compact Target
Compact Target (nBits)
Bitcoin encodes the proof-of-work target in a 4-byte "compact" format:
[exponent (1 byte)] [mantissa (3 bytes)]
The target is:
T = mantissa * 256^(exponent-3)
Functions
func CompactToTarget(bits uint32) *big.Int
func TargetToCompact(target *big.Int) uint32
Rules
- Use unsigned arithmetic (ignore sign bit).
- If
target == 0,TargetToCompactshould return0. TargetToCompactmust produce the canonical encoding:- Let
size = len(target.Bytes()). - Mantissa is the first 3 bytes of the big-endian target.
- If the first mantissa byte has the high bit set (
>= 0x80), shift the mantissa right by 8 and incrementsize.
- Let
Notes
- You can use
math/bigfor big integers. - Treat the hash as a big-endian integer when comparing against targets.
Run tests to see results
No issues detected