Architecture¶
btclib is a library that runs inside its caller’s process. This page is its high-level design: the major components, how they depend on one another, and the properties those dependencies keep. What a user can expect of it in terms of security is SECURITY, and why those expectations hold is the assurance case.
The curve arithmetic is ecc’s¶
Elliptic-curve arithmetic and the cryptography built on it are
ecc, a required
dependency: curves, number_theory, kdf, ecc and the SwiftEC map
of ecc.ellswift. btclib imports each name it uses from
ecc and publishes none of them: ecc.curves.mult is spelt
that way and no other, and tests/all_test.py’s
test_no_module_exports_another_distributions_name holds every __all__
of btclib to it. What those names raise is ecc’s exception classes,
which derive from the built-in classes and not from BTClibException,
and a caller names them from ecc.exceptions. The exception to
publishing none is the type aliases btclib.alias imports from
ecc.alias, Octets among them.
ecc delegates secp256k1 to btclib-secp256k1, the cffi bindings to Bitcoin Core’s libsecp256k1, conditionally. btclib calls the bindings only through ecc.
One switch.
ecc.curves.is_libsecp256k1_servinganswers whether the process delegates, andecc.curves.set_libsecp256k1_servingsets it;ECC_NO_LIBSECP256K1in the environment sets it off at import. ecc’s delegating calls ask it first and add their own conditions to it. SECURITY.md’s Limitations, not vulnerabilities states those conditions operation by operation.The Python arithmetic. Whatever the switch and the call site decline runs ecc’s Python arithmetic. That path is not dead code: it serves every other curve, every other hash function and a caller-imposed nonce, it answers for the point at infinity, which libsecp256k1 has no public key for, and it is the whole library in an install without the
secp256k1extra. It is not constant-time, which SECURITY.md publishes as a known limitation.The bindings are the authority on the answer. The suite validates the Python arithmetic against them.
tests/no_bindings_test.pyimports btclib in an interpreter with the bindings out of reach and compares what it computes with what the bindings compute.
Layers¶
Roughly bottom-up. The directions that hold without exception are the
substrate, btclib.ellswift, the pairs in the next section and the edges
to bip32_descriptors and btclib_wallet below, each held by a test;
elsewhere a module reaches up where its subject asks for it, as
btclib.bms does for the address a message signature names.
the substrate:
alias,exceptions,utilsthe curve and what is built on it: ecc’s
curves,number_theory,kdfandecc, in a distribution of their own, with btclib’sellswiftandbmsbeside thembitcoin’s hashes, integers and constants:
hashes,var_int,var_bytes,obfuscation,consensus,amount,compressornetworks, keys and addresses:
network,key,b58,b32, on the codecs ofbase58_bech32, in a distribution of their ownthe chain:
script,tx,block,fee,policy,coinstats,muhash,utreexothe wire:
p2pfixtures for tests:
testing
alias holds the types the public API accepts, much of it taking anything
convertible rather than one type, and exceptions the errors it raises.
consensus holds the consensus constants and the per-network tables, and
importing it imports nothing else of btclib, which tests/imports_test.py
asserts.
What of the curve’s layer is btclib’s¶
btclib.bms and btclib.ellswift are btclib’s own:
btclib.bmsnames its signer by an address, so it importsb32,b58,keyandnetwork.btclib.ellswiftisxdh,XDH_TAGandELL_SIZE, BIP324’s agreement on ecc’s SwiftEC map, which is btclib’s.
Each pair is one idea split in two¶
the codec or the arithmetic |
the bitcoin semantics on top |
|---|---|
|
|
|
|
The right column imports the left one, and the left never imports the
right. So btclib.b58 for an address, and
base58-bech32, a required
dependency, for the encoding on its own. That distribution imports the
standard library alone, which its own tests/imports_test.py asserts.
btclib publishes none of its names, and re-raises its refusals as
BTClibValueError and BTClibTypeError, so that a caller of b58,
b32, bms and network catches this library’s exceptions.
The same split lies between ecc’s curves and its ecc, which
btclib imports in place of a pair of its own.
Where a key’s spelling is read¶
A caller states which half of a pair it holds, rather than leaving a size
or a format to decide it. key.PubKeyData is the SEC octets and the
network an address builder takes, and key.PrvKeyData the scalar, the
network and the compression flag btclib.bms signs with.
Each spelling of a key is read by the module that defines it:
the scalar and the curve point in their octet spellings are facts about the curve, read by
ecc.curves.scalar_from_prv_keyandecc.curves.point_from_pub_keythe network and the compression, which a record carries and a key does not, are
key’sa WIF is Base58Check with a prefix and a flag, so
b58.prv_key_data_from_wifreads it andb58.wif_from_prv_keywrites itan extended key is BIP32’s format, parsed by
bip32_descriptors.bip32; a caller holding one parses it there and passes on the scalar or the point
The chain and the wire¶
script, tx and block build and validate what goes on the chain, and
script.engine verifies a transaction against the consensus rules.
p2p is the wire format peers speak: the message envelope, its framing,
the message start of each network, and the payloads. It turns octets
into objects and objects into octets, and opens no socket: a caller
reading from a connection hands it what it read. It also holds Bitcoin
Core’s low-work headers sync, which decides what to ask a peer for and
sends nothing itself.
What sits outside the package¶
BIP32 key derivation and output descriptors are the
bip32-descriptorsdistribution, imported asbip32_descriptors. It imports btclib, and nothing in btclib imports it:tests/imports_test.pyholds that edge to one direction.The rest of the wallet side (mnemonics, PSBTs, signers, chain backends and the Electrum protocol’s codec) is the
btclib-walletdistribution, imported asbtclib_wallet. It imports btclib, and nothing in btclib imports it:tests/imports_test.pyholds that edge to one direction.bitcoin-core-rpc is not a runtime dependency:
btclib.p2p.magicholds the message start of each network. The suite uses the package to drive a bitcoind, to read Core’s chain names, and to compare that table with the package’s copy. No module loadsurllib.requeston its way to anything else, so a caller who parses messages pays nothing for a client it never uses.tests/imports_test.py’stest_the_codec_does_not_pay_for_the_rpc_packageholds the codec to that.Package data. The networks are JSON under
src/btclib/_data/, read once at import bysrc/btclib/network.py. The blocksblock_170.bin,block_200000.binandblock_481824_complete.binare undersrc/btclib/testing/_data/, read bybtclib.testing.blocks.read_bytes. The catalogued curves are ecc’s package data.
The public surface¶
Every module and every package declares __all__, and a public function
validates its inputs before a private twin does the work.
CONTRIBUTING.md’s The public surface states both rules and names the
tests that hold them.