b53
English | 🇷🇺 Русская версия
b53 is a zero-dependency Python library for reversible cryptographic packing of boolean arrays of arbitrary length L ∈ [1, 50] into a single standard IEEE 754 binary64 (double / Python float) in the half-open interval [0.0, 1.0).
Key Properties
- Hidden length inside the float: the original array length
L ∈ [1, 50]is encoded directly inside the 53-bit mantissa space—no external length metadata is required. - Randomized output (nonce) per encryption: encrypting the same boolean array with the same salt produces different pseudorandom doubles in
[0.0, 1.0). - Bijective 53-bit permutation: uses an 8-round unbalanced Feistel network (
26 + 27 = 53bits) keyed byHMAC-SHA256with your secret salt. - Chained arbitrary-length serialization: splits arrays of any length into a fixed number of random-size chunks with per-chunk salt chaining so individual doubles cannot be decrypted in isolation.
- Password key stretching (
scrypt): derives strong 256-bit salts from human passwords via standard-libraryhashlib.scrypt(RFC 7914). - Pure Python standard library: zero runtime dependencies, typed (
py.typed), Python 3.10+.
Installation
pip install b53
# Optional: asymmetric X25519 extension (b53a / b53.asymmetric)
pip install "b53[asymmetric]"
Quick Start
Python API
import b53
# 1. Generate a 256-bit secret salt (or stretch a password with scrypt)
salt = b53.generate_salt()
# salt = b53.derive_salt("my-passphrase", context="user:42")
# 2. Encrypt a boolean array (1..50 elements) into a float in [0.0, 1.0)
bits = [True, False, True, True, False, False, True, False, True, True]
x1 = b53.encrypt(bits, salt)
x2 = b53.encrypt(bits, salt) # different float due to random nonce
assert x1 != x2
# 3. Decrypt back to the exact original list[bool] and length
assert b53.decrypt(x1, salt) == bits
assert b53.decrypt(x2, salt) == bits
# 4. Serialize/deserialize an arbitrary-length boolean array into list[float]
many_bits = [i % 3 == 0 for i in range(250)]
doubles = b53.serialize(many_bits, salt, min_size=10)
assert b53.deserialize(doubles, salt) == many_bits
For repeated operations with the same salt (or directly from a password), use B53Cipher (alias Cipher):
from b53 import B53Cipher
cipher = B53Cipher(salt)
# or: cipher = B53Cipher.from_password("my-passphrase", context="user:42")
encoded = cipher.encrypt_str("1011001011")
print(repr(encoded))
print(cipher.decrypt_str(encoded)) # "1011001011"
doubles = cipher.serialize(many_bits) # default min_size=10
assert cipher.deserialize(doubles) == many_bits
Asymmetric Mode (b53a / b53.asymmetric)
Requires pip install "b53[asymmetric]". Uses X25519 + HKDF-SHA256 without embedding keys into the double output:
import b53a # or: from b53 import b53a
bob = b53a.generate_keypair()
# 1. One-way sealed session: encryptor only needs bob.public_key;
# capsule is stored separately from the double payloads.
enc, capsule = b53a.seal_session(bob.public_key, context="table:flags")
d = enc.encrypt([True, False, True, True]) # 1 exact double in [0.0, 1.0)
ds = enc.serialize([True, False] * 15) # fixed-count list[float]
dec = b53a.open_session(capsule, bob.private_key, context="table:flags")
assert dec.decrypt(d) == [True, False, True, True]
assert dec.deserialize(ds) == [True, False] * 15
# 2. Pairwise X25519 (no capsule needed when both sides hold keypairs)
alice = b53a.generate_keypair()
x = b53a.encrypt([True, False, True], alice.private_key, bob.public_key)
assert b53a.decrypt(x, bob.private_key, alice.public_key) == [True, False, True]
Command-Line Interface
# 1. Generate a 256-bit hex salt
SALT=$(b53 generate-salt)
# 2. Encrypt the same bit string 3 times
b53 encrypt 1011001011 --salt "$SALT" --count 3
# 3. Decrypt any generated double back to the original array
b53 decrypt 0.7419283058129402 --salt "$SALT"
You can also run via python -m b53 ..., pass --salt-file path/to/salt, or set the B53_SALT environment variable.
How It Works
An IEEE 754 binary64 float on the uniform 2⁻⁵³ grid represents x = C · 2⁻⁵³ for C ∈ {0, 1, ..., 2⁵³ - 1}.
The entire 53-bit state space (2⁵³ = 9,007,199,254,740,992 states) is partitioned without gaps or overlaps across all 50 lengths L ∈ [1, 50] so that:
∑L=1..50 ML · 2L = 253
Space is allocated top-down from L = 50 to L = 1, reserving ~25% of the remaining budget at each step (at least M_L ≥ 3 nonces even at L = 50):
Length L |
Data combinations (2^L) |
Nonces per array (M_L) |
Segment size (M_L · 2^L) |
|---|---|---|---|
| 50 | 2^50 |
3 | 3 × 2^50 |
| 49 | 2^49 |
3 | 3 × 2^49 |
| 48 | 2^48 |
3 | 3 × 2^48 |
| 47 | 2^47 |
5 | 5 × 2^47 |
| 45 | 2^45 |
13 | 13 × 2^45 |
| 40 | 2^40 |
100 | 100 × 2^40 |
| 30 | 2^30 |
5,811 | 5,811 × 2^30 |
| 20 | 2^20 |
335,103 | 335,103 × 2^20 |
| 10 | 2^10 |
19,323,738 | ~1.98 × 10^10 |
| 1 | 2^1 |
2,971,477,630 | ~5.94 × 10^9 |
- Pack: encode
bitsasD ∈ [0, 2^L - 1], pickr ∈ [0, M_L - 1]viasecrets.randbelow, and computeP = BASES[L] + r * 2^L + D. - Permute: apply an 8-round unbalanced Feistel network (
26 + 27bits) withHMAC-SHA256keyed by the normalized salt to obtainC ∈ [0, 2^53 - 1]. - Float conversion: return
math.ldexp(C, -53)(C · 2⁻⁵³). - Decrypt: verify
x ∈ [0.0, 1.0)lies exactly on the2⁻⁵³grid, invert the Feistel permutation to recoverP, find the unique rangeL, discardr, and unpack the lowerLbits ofP - BASES[L].
Precision Requirements
When serializing the encrypted double to text, JSON, CSV, or CLI arguments, always preserve all significant binary64 digits using repr(x) / f"{x:.17g}" (or native DOUBLE PRECISION / FLOAT8 in databases). Rounding even the least significant digit alters the lower bits of the 53-bit ciphertext and breaks decryption.
When using serialize() / deserialize(), keep the full list of doubles in its original order: chunk 0 derives its seed from the total chunk count, and each subsequent chunk derives a modified salt from the root salt, previous seed, index, and previous chunk length—so individual doubles cannot be decrypted in isolation.
API Reference
generate_salt(num_bytes: int = 32) -> str— generate a cryptographically random hex salt (≥ 16 bytes).derive_salt(password, context=None, *, num_bytes=32, n=32768, r=8, p=1, maxmem=...) -> str— stretch a human password/passphrase into a hex salt via stdlibhashlib.scrypt(RFC 7914).encrypt(bits, salt) -> float/encrypt_bool_array(bits, salt) -> float— encrypt1..50booleans (or0/1ints) into a double in[0.0, 1.0).decrypt(double_val, salt) -> list[bool]/decrypt_bool_array(double_val, salt) -> list[bool]— recover the exact boolean list and length.serialize(bits, salt, min_size=10) -> list[float]/serialize_bool_array(...)— split an arbitrary-length boolean sequence into a fixed number of chunks(len(bits) // min_size) + 1of random sizes in[1, min_size](first chunk strictly< min_size,2 <= min_size <= 50) and encrypt them using a chained per-chunk salt (HMAC-SHA256over total chunk count for chunk 0, then previous seed + index + previous chunk size).deserialize(values, salt) -> list[bool]/deserialize_bool_array(values, salt)— verify and restore the full boolean list from chained doubles.parse_bits(raw: str) -> list[bool]— parse"10110"or"true,false,1,0".format_bits(bits) -> str— format booleans as"10110".B53Cipher(salt)(aliasCipher) — reusable cipher object with.encrypt(),.decrypt(),.serialize(),.deserialize(),.encrypt_str(),.decrypt_str(),B53Cipher.generate(), andB53Cipher.from_password(password, context=...).
Development
pip install -e ".[dev]"
pytest
ruff check src tests
mypy src
License
Metadata
Release files for b53 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| b53-0.3.0.tar.gz | 26.8 kB | With provenance | Details for b53-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| b53-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for b53-0.3.0-py3-none-any.whl |
Total release size: 45.0 kB
Release files / b53-0.3.0.tar.gz
| Download URL | b53-0.3.0.tar.gz |
|---|---|
| Size | 26.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5d6f7840c9bc977516f20126908f71ebf2ea6c9a56fc06b110d25732169d74a2
|
|
BLAKE2b-256 checksum How to use checksums |
9171697ce45856b5b31e103d6b8b1159adab12871eb6157c47a790596da5a42b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.
Transparency logRelease files / b53-0.3.0-py3-none-any.whl
| Download URL | b53-0.3.0-py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e57a31aef3d9b2628cd39ad840fb75e48a5030d725b48b0667fd353a9a91c0e0
|
|
BLAKE2b-256 checksum How to use checksums |
abb4623fc20bcf305b8e1590c4d7219075d757ffbb493ddba19fa267ba441014
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.
Transparency logSecurity for b53 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for b53-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.