Liking cljdoc? Tell your friends :D

urd.consensus.bignat

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.

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`.
raw docstring

addclj/s

(add a b)
source

bit-lengthclj/s

(bit-length a)

Number of significant bits; 0 for zero.

Number of significant bits; 0 for zero.
sourceraw docstring

byte-lengthclj/s

(byte-length a)
source

bytes->hexclj/s

(bytes->hex bs)
source

compareclj/s

(compare a b)

-1, 0 or 1. Structural: both arguments must be trimmed.

-1, 0 or 1. Structural: both arguments must be trimmed.
sourceraw docstring

divclj/s

(div a b)
source

div-smallclj/s

(div-small a d)
source

divmodclj/s

(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.
sourceraw docstring

divmod-smallclj/s

(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.
sourceraw docstring

from-bytes-beclj/s

(from-bytes-be bs)

A sequence of unsigned bytes (0-255), most significant first.

A sequence of unsigned bytes (0-255), most significant first.
sourceraw docstring

from-hexclj/s

(from-hex s)
source

from-intclj/s

(from-int n)

A non-negative integer below 2^53 as a bignat.

A non-negative integer below 2^53 as a bignat.
sourceraw docstring

geclj/s

(ge a b)
source

gtclj/s

(gt a b)
source

hex->bytesclj/s

(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.
sourceraw docstring

leclj/s

(le a b)
source

limb-baseclj/s

source

limb-bitsclj/s

source

ltclj/s

(lt a b)
source

mulclj/s

(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.
sourceraw docstring

mul-smallclj/s

(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.
sourceraw docstring

oneclj/s

source

pow2clj/s

(pow2 n)
source

shift-leftclj/s

(shift-left a n)

a * 2^n.

a * 2^n.
sourceraw docstring

shift-rightclj/s

(shift-right a n)

floor(a / 2^n).

floor(a / 2^n).
sourceraw docstring

subclj/s

(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.
sourceraw docstring

test-bitclj/s

(test-bit a i)
source

to-bytes-beclj/s

(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.
sourceraw docstring

to-hexclj/s

(to-hex a n)
source

to-intclj/s

(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.
sourceraw docstring

zeroclj/s

source

zero?clj/s

(zero? a)
source

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close