Arbitrary-precision NATURAL numbers, in portable ClojureScript-safe arithmetic.
This exists for exactly one reason: Nakamoto fork choice compares cumulative
proof-of-work, which is a sum of terms of the form floor(2^256 / (target+1)).
Those numbers do not fit in a JavaScript double, and the comparison must be
EXACT -- btcd's rule is > and not >= (chain.go:1257), so ties go to the
incumbent and first-seen wins. An approximate comparison would silently
convert a tie into a reorg, which is a consensus fault, not a rounding error.
java.math.BigInteger exists; js/BigInt exists. Using them would mean a
reader conditional in every arithmetic expression, and -- worse -- two
independent implementations of the consensus arithmetic, which is precisely
the shape of bug this repository has already been bitten by once: rng
produced different draws under ClojureScript for as long as it existed,
because bit-and there yields a SIGNED 32-bit integer. Nothing in this
namespace can diverge that way, because it is one implementation.
The JVM's BigInteger is still useful -- as an ORACLE. bignat_test property-
tests every operation here against it.
A vector of limbs, base 2^16, LEAST significant first, with no trailing zero
limbs. Zero is [].
Base 2^16 is chosen so that every intermediate stays exactly representable in
a double. The largest quantity computed anywhere below is a schoolbook
multiply accumulator, limb*limb + limb + carry < 2^32 + 2^17, about 4.3e9 --
eleven orders of magnitude short of 2^53. A base of 2^24 would still fit a
single product but not a long accumulation, and base 2^32 would not fit at
all. Bit operations are never applied to anything wider than a limb, so
ClojureScript's signed 32-bit bit-and is never reached.
Everything here is total and non-negative: sub requires a >= b.
Arbitrary-precision NATURAL numbers, in portable ClojureScript-safe arithmetic. This exists for exactly one reason: Nakamoto fork choice compares cumulative proof-of-work, which is a sum of terms of the form `floor(2^256 / (target+1))`. Those numbers do not fit in a JavaScript double, and the comparison must be EXACT -- btcd's rule is `>` and not `>=` (`chain.go:1257`), so ties go to the incumbent and first-seen wins. An approximate comparison would silently convert a tie into a reorg, which is a consensus fault, not a rounding error. ## Why not the host's big integers `java.math.BigInteger` exists; `js/BigInt` exists. Using them would mean a reader conditional in every arithmetic expression, and -- worse -- two independent implementations of the consensus arithmetic, which is precisely the shape of bug this repository has already been bitten by once: `rng` produced different draws under ClojureScript for as long as it existed, because `bit-and` there yields a SIGNED 32-bit integer. Nothing in this namespace can diverge that way, because it is one implementation. The JVM's BigInteger is still useful -- as an ORACLE. `bignat_test` property- tests every operation here against it. ## Representation A vector of limbs, base 2^16, LEAST significant first, with no trailing zero limbs. Zero is `[]`. Base 2^16 is chosen so that every intermediate stays exactly representable in a double. The largest quantity computed anywhere below is a schoolbook multiply accumulator, `limb*limb + limb + carry < 2^32 + 2^17`, about 4.3e9 -- eleven orders of magnitude short of 2^53. A base of 2^24 would still fit a single product but not a long accumulation, and base 2^32 would not fit at all. Bit operations are never applied to anything wider than a limb, so ClojureScript's signed 32-bit `bit-and` is never reached. Everything here is total and non-negative: `sub` requires `a >= b`.
(bit-length a)Number of significant bits; 0 for zero.
Number of significant bits; 0 for zero.
(compare a b)-1, 0 or 1. Structural: both arguments must be trimmed.
-1, 0 or 1. Structural: both arguments must be trimmed.
(divmod a b)Floor division: [quotient remainder], for any non-zero divisor.
Binary long division rather than Knuth D. It is O(bits * limbs), so a 256-bit
divide is a few thousand operations -- more than fast enough, since the only
caller is calc-work, once per header, and it is a lot easier to be sure it
is right.
Floor division: [quotient remainder], for any non-zero divisor. Binary long division rather than Knuth D. It is O(bits * limbs), so a 256-bit divide is a few thousand operations -- more than fast enough, since the only caller is `calc-work`, once per header, and it is a lot easier to be sure it is right.
(divmod-small a d)[quotient remainder] for a plain integer divisor d in (0, 2^31). The largest intermediate is r*2^16 + limb < 2^47.
[quotient remainder] for a plain integer divisor d in (0, 2^31). The largest intermediate is r*2^16 + limb < 2^47.
(from-bytes-be bs)A sequence of unsigned bytes (0-255), most significant first.
A sequence of unsigned bytes (0-255), most significant first.
(from-int n)A non-negative integer below 2^53 as a bignat.
A non-negative integer below 2^53 as a bignat.
(hex->bytes s)An even-length hex string to a vector of unsigned bytes.
An even-length hex string to a vector of unsigned bytes.
(mul a b)Schoolbook multiply. O(n*m), which is ample for 256-bit quantities.
Schoolbook multiply. O(n*m), which is ample for 256-bit quantities.
(mul-small a m)a * m for a plain integer m in [0, 2^31). The largest intermediate is limb*m + carry < 2^47 + 2^31.
a * m for a plain integer m in [0, 2^31). The largest intermediate is limb*m + carry < 2^47 + 2^31.
(sub a b)a - b, requiring a >= b. A negative result is a bug in the caller, so it throws rather than wrapping.
a - b, requiring a >= b. A negative result is a bug in the caller, so it throws rather than wrapping.
(to-bytes-be a n)n unsigned bytes, most significant first, zero-padded. Throws if a does
not fit -- a silently truncated target would be a consensus fault.
`n` unsigned bytes, most significant first, zero-padded. Throws if `a` does not fit -- a silently truncated target would be a consensus fault.
(to-int a)Back to a plain integer. Throws above 2^53, where a double stops being exact -- returning a silently-wrong number is the one thing this namespace exists to prevent.
Back to a plain integer. Throws above 2^53, where a double stops being exact -- returning a silently-wrong number is the one thing this namespace exists to prevent.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |