Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1# Require exact values up to2FPBITS = 2564# Overkill accuracy5F = RealField(400)7def BaseFPBits(bits, capacity):8return bits * capacity - int(ceil(F(log(sum(binomial(2**bits - 1, i) for i in range(capacity+1)), 2))))10def Log2Factorial(capacity):11return int(floor(log(factorial(capacity), 2)))13print("uint64_t BaseFPBits(uint32_t bits, uint32_t capacity) {")14print(" // Correction table for low bits/capacities")15TBLS={}16FARS={}17SKIPS={}18for bits in range(1, 32):19TBL = []20for capacity in range(1, min(2**bits, FPBITS)):21exact = BaseFPBits(bits, capacity)22approx = Log2Factorial(capacity)23TBL.append((exact, approx))24MIN = 1000000000025while len(TBL) and ((TBL[-1][0] == TBL[-1][1]) or (TBL[-1][0] >= FPBITS and TBL[-1][1] >= FPBITS)):26MIN = min(MIN, TBL[-1][0] - TBL[-1][1])27TBL.pop()28while len(TBL) and (TBL[-1][0] - TBL[-1][1] == MIN):29TBL.pop()30SKIP = 031while SKIP < len(TBL) and TBL[SKIP][0] == TBL[SKIP][1]:32SKIP += 133DIFFS = [TBL[i][0] - TBL[i][1] for i in range(SKIP, len(TBL))]34if len(DIFFS) > 0 and len(DIFFS) * Integer(max(DIFFS)).nbits() > 64:35print(" static constexpr uint8_t ADD%i[] = {%s};" % (bits, ", ".join(("%i" % (TBL[i][0] - TBL[i][1])) for i in range(SKIP, len(TBL)))))36TBLS[bits] = DIFFS37FARS[bits] = MIN38SKIPS[bits] = SKIP39print("")40print(" if (capacity == 0) return 0;")41print(" uint64_t ret = 0;")42print(" if (bits < 32 && capacity >= (1U << bits)) {")43print(" ret = uint64_t{bits} * (capacity - (1U << bits) + 1);")44print(" capacity = (1U << bits) - 1;")45print(" }")46print(" ret += Log2Factorial(capacity);")47print(" switch (bits) {")48for bits in sorted(TBLS.keys()):49if len(TBLS[bits]) == 0:50continue51width = Integer(max(TBLS[bits])).nbits()52if len(TBLS[bits]) == 1:53add = "%i" % TBLS[bits][0]54elif len(TBLS[bits]) * width <= 64:55code = sum((2**(width*i) * TBLS[bits][i]) for i in range(len(TBLS[bits])))56if width == 1:57add = "(0x%x >> (capacity - %i)) & 1" % (code, 1 + SKIPS[bits])58else:59add = "(0x%x >> %i * (capacity - %i)) & %i" % (code, width, 1 + SKIPS[bits], 2**width - 1)60else:61add = "ADD%i[capacity - %i]" % (bits, 1 + SKIPS[bits])62if len(TBLS[bits]) + SKIPS[bits] == 2**bits - 1:63print(" case %i: return ret + (capacity <= %i ? 0 : %s);" % (bits, SKIPS[bits], add))64else:65print(" case %i: return ret + (capacity <= %i ? 0 : capacity > %i ? %i : %s);" % (bits, SKIPS[bits], len(TBLS[bits]) + SKIPS[bits], FARS[bits], add))66print(" default: return ret;")67print(" }")68print("}")70print("void TestBaseFPBits() {")71print(" static constexpr uint16_t TBL[20][100] = {%s};" % (", ".join("{" + ", ".join(("%i" % BaseFPBits(bits, capacity)) for capacity in range(0, 100)) + "}" for bits in range(1, 21))))72print(" for (int bits = 1; bits <= 20; ++bits) {")73print(" for (int capacity = 0; capacity < 100; ++capacity) {")74print(" uint64_t computed = BaseFPBits(bits, capacity), exact = TBL[bits - 1][capacity];")75print(" CHECK(exact == computed || (exact >= 256 && computed >= 256));")76print(" }")77print(" }")78print("}")
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.