## Description

CompactToBig converts a compact representation of a whole number N to an unsigned 32-bit number. The representation is similar to IEEE754 floating point numbers.

Like IEEE754 floating point, there are three basic components: the sign, the exponent, and the mantissa. They are broken out as follows:

* the most significant 8 bits represent the unsigned base 256 exponent * bit 23 (the 24th bit) represents the sign bit * the least significant 23 bits represent the mantissa ------------------------------------------------- | Exponent | Sign | Mantissa | ------------------------------------------------- | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] | -------------------------------------------------

The formula to calculate N is:

N = (-1^sign) * mantissa * 256^(exponent-3)

This compact form is only used in bitcoin to encode unsigned 256-bit numbers which represent difficulty targets, thus there really is not a need for a sign bit, but it is implemented here to stay consistent with bitcoind.

## Examples

// a negative number. Note this should not happen in practice with valid // blocks, but an invalid block could trigger it. difficultyNum := CompactToBig(bits) if difficultyNum.Sign() <= 0 { return big.NewInt(0)

// the number of retargets for the duration and starting difficulty // multiplied by the max adjustment factor. newTarget := CompactToBig(bits) for durationVal > 0 && newTarget.Cmp(b.chainParams.PowLimit) < 0 { newTarget.Mul(newTarget, adjustmentFactor)

// rounded down. Bitcoind also uses integer division to calculate this // result. oldTarget := CompactToBig(lastNode.bits) newTarget := new(big.Int).Mul(oldTarget, big.NewInt(adjustedTimespan)) newTarget.Div(newTarget, big.NewInt(int64(b.chainParams.TargetTimespan)))

## CompactToBig is referenced in 3 repositories

**github.com/btcsuite/btcd**

- 8 references in rpcserver.go
- 4 references in blockchain/difficulty.go
- 2 references in blockchain/process.go
- 1 reference in blockchain/difficulty_test.go
- 1 reference in blockchain/example_test.go

**github.com/OpenBazaar/spvwallet**

- 3 references in blockchain.go
- 2 references in header.go

...