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.
251if modulus != nmodulus:252# If the bitstream modulus is not the best modulus for253# this implementation a conversion table will be needed.254ctyp = rtyp255NF.<nf> = GF(2**bits, modulus=nmodulus)256ctables = conv_tables(NF, F, bits)257loadtbl = "&LOAD_TABLE_%s" % table_id258savetbl = "&SAVE_TABLE_%s" % table_id259if include_table:260print("constexpr %s LOAD_TABLE_%s({%s});" % (ctyp, table_id, ", ".join([fmt(x,typ) for x in ctables[0]])))261print("constexpr %s SAVE_TABLE_%s({%s});" % (ctyp, table_id, ", ".join([fmt(x,typ) for x in ctables[1]])))262else:263ctyp = "IdTrans"264loadtbl = "&ID_TRANS"265savetbl = "&ID_TRANS"266else:267assert(modulus == nmodulus)269if include_table:270print("constexpr %s SQR_TABLE_%s({%s});" % (rtyp, table_id, ", ".join([fmt(x,typ) for x in sqr_table(f, bits, 1)])))271if multi_sqr:272# Repeated squaring is a linearised polynomial so in F(2^n) it is273# F(2) linear and can be computed by a simple bit-matrix.274# Repeated squaring is especially useful in powering ladders such as275# for inversion.276# When certain repeated squaring tables are not in use, use the QRT277# table instead to make the C++ compiler happy (it always has the278# same type).279sqr2 = "&QRT_TABLE_%s" % table_id280sqr4 = "&QRT_TABLE_%s" % table_id281sqr8 = "&QRT_TABLE_%s" % table_id282sqr16 = "&QRT_TABLE_%s" % table_id283if ((bits - 1) >= 4):284if include_table:285print("constexpr %s SQR2_TABLE_%s({%s});" % (rtyp, table_id, ", ".join([fmt(x,typ) for x in sqr_table(f, bits, 2)])))286sqr2 = "&SQR2_TABLE_%s" % table_id287if ((bits - 1) >= 8):288if include_table:289print("constexpr %s SQR4_TABLE_%s({%s});" % (rtyp, table_id, ", ".join([fmt(x,typ) for x in sqr_table(f, bits, 4)])))290sqr4 = "&SQR4_TABLE_%s" % table_id291if ((bits - 1) >= 16):292if include_table:293print("constexpr %s SQR8_TABLE_%s({%s});" % (rtyp, table_id, ", ".join([fmt(x,typ) for x in sqr_table(f, bits, 8)])))294sqr8 = "&SQR8_TABLE_%s" % table_id295if ((bits - 1) >= 32):296if include_table:297print("constexpr %s SQR16_TABLE_%s({%s});" % (rtyp, table_id, ", ".join([fmt(x,typ) for x in sqr_table(f, bits, 16)])))298sqr16 = "&SQR16_TABLE_%s" % table_id299if include_table:300print("constexpr %s QRT_TABLE_%s({%s});" % (rtyp, table_id, ", ".join([fmt(x,typ) for x in qrt_table(F, f, bits)])))302modulus_weight = modulus.hamming_weight()303modulus_degree = (modulus - p**bits).degree()304modulus_int = (modulus - p**bits).change_ring(ZZ)(2)306lfsr = ""308if style == INT:309print("typedef Field<%s, %i, %i, %s, %s, &SQR_TABLE_%s, &QRT_TABLE_%s%s> Field%i;" % (typ, bits, modulus_int, rtyp, ttyp, table_id, table_id, lfsr, bits))310elif style == CLMUL:311print("typedef Field<%s, %i, %i, %s, &SQR_TABLE_%s, %s, %s, %s, %s, &QRT_TABLE_%s, %s, %s, %s%s> Field%i;" % (typ, bits, modulus_int, rtyp, table_id, sqr2, sqr4, sqr8, sqr16, table_id, ctyp, loadtbl, savetbl, lfsr, bits))312elif style == CLMUL_TRI:313print("typedef FieldTri<%s, %i, %i, %s, &SQR_TABLE_%s, %s, %s, %s, %s, &QRT_TABLE_%s, %s, %s, %s> FieldTri%i;" % (typ, bits, modulus_degree, rtyp, table_id, sqr2, sqr4, sqr8, sqr16, table_id, ctyp, loadtbl, savetbl, bits))314else:315assert(False)317for bits in range(2, 65):318print("#ifdef ENABLE_FIELD_INT_%i" % bits)319print("// %i bit field" % bits)320print_result(bits, INT)321print("#endif")322print("")324for bits in range(2, 65):325print("#ifdef ENABLE_FIELD_INT_%i" % bits)326print("// %i bit field" % bits)327print_result(bits, CLMUL)328print_result(bits, CLMUL_TRI)329print("#endif")330print("")332for bits in range(2, 65):333print_result(bits, MD)
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.