Skip to main content

b53

English | 🇷🇺 Русская версия

PyPI version Python versions License: MIT CI

b53 is a zero-dependency Python library for reversible cryptographic packing of boolean arrays of arbitrary length $L \in [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 \in [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 = 53$ bits) keyed by HMAC-SHA256 with your secret salt.
  • Pure Python standard library: zero runtime dependencies, typed (py.typed), Python 3.10+.

Installation

pip install b53

Quick Start

Python API

import b53

# 1. Generate a 256-bit secret salt (store securely)
salt = b53.generate_salt()

# 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

For repeated operations with the same salt, use B53Cipher (alias Cipher):

from b53 import B53Cipher

cipher = B53Cipher(salt)
encoded = cipher.encrypt_str("1011001011")
print(repr(encoded))
print(cipher.decrypt_str(encoded))  # "1011001011"

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^{-53}$ grid represents $x = C \cdot 2^{-53}$ for $C \in {0, 1, \dots, 2^{53} - 1}$.

The entire 53-bit state space ($2^{53} = 9,007,199,254,740,992$ states) is partitioned without gaps or overlaps across all 50 lengths $L \in [1, 50]$ so that:

$$\sum_{L=1}^{50} M_L \cdot 2^L = 2^{53}$$

Space is allocated top-down from $L = 50$ to $L = 1$, reserving $\approx 25%$ of the remaining budget at each step (at least $M_L \ge 3$ nonces even at $L = 50$):

Length $L$ Data combinations ($2^L$) Nonces per array ($M_L$) Segment size ($M_L \cdot 2^L$)
50 $2^{50}$ 3 $3 \times 2^{50}$
49 $2^{49}$ 3 $3 \times 2^{49}$
48 $2^{48}$ 3 $3 \times 2^{48}$
47 $2^{47}$ 5 $5 \times 2^{47}$
45 $2^{45}$ 13 $13 \times 2^{45}$
40 $2^{40}$ 100 $100 \times 2^{40}$
30 $2^{30}$ 5,811 $5{,}811 \times 2^{30}$
20 $2^{20}$ 335,103 $335{,}103 \times 2^{20}$
10 $2^{10}$ 19,323,738 $\approx 1.98 \times 10^{10}$
1 $2^1$ 2,971,477,630 $\approx 5.94 \times 10^{9}$
  1. Pack: encode bits as $D \in [0, 2^L - 1]$, pick $r \in [0, M_L - 1]$ via secrets.randbelow, and compute $P = \text{BASE}[L] + r \cdot 2^L + D$.
  2. Permute: apply an 8-round unbalanced Feistel network ($26 + 27$ bits) with HMAC-SHA256 keyed by the normalized salt to obtain $C \in [0, 2^{53} - 1]$.
  3. Float conversion: return math.ldexp(C, -53) ($C \cdot 2^{-53}$).
  4. Decrypt: verify $x \in [0.0, 1.0)$ lies exactly on the $2^{-53}$ grid, invert the Feistel permutation to recover $P$, find the unique range $L$, discard $r$, and unpack the lower $L$ bits of $P - \text{BASE}[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.

API Reference

  • generate_salt(num_bytes: int = 32) -> str — generate a cryptographically random hex salt ($\ge 16$ bytes).
  • encrypt(bits, salt) -> float / encrypt_bool_array(bits, salt) -> float — encrypt $1..50$ booleans (or 0/1 ints) 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.
  • parse_bits(raw: str) -> list[bool] — parse "10110" or "true,false,1,0".
  • format_bits(bits) -> str — format booleans as "10110".
  • B53Cipher(salt) (alias Cipher) — reusable cipher object with .encrypt(), .decrypt(), .encrypt_str(), .decrypt_str(), and B53Cipher.generate().

Development

pip install -e ".[dev]"
pytest
ruff check src tests
mypy src

License

MIT

Metadata

Release files for b53 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for b53 0.1.0
File Size Uploaded Provenance Actions
b53-0.1.0.tar.gz 15.3 kB With provenance Details for b53-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for b53 0.1.0
File Interpreter ABI Platform Actions
b53-0.1.0-py3-none-any.whl Python 3 none any Details for b53-0.1.0-py3-none-any.whl

Total release size: 26.3 kB

Release files / b53-0.1.0.tar.gz

Download URL b53-0.1.0.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
28e39f6f74298f93fe9a9b05624a7ef34ea32d5dd46dfc403f44d229a980a752
BLAKE2b-256 checksum
How to use checksums
565c7e385f92c4aa49896da90dad441409e077849e2ec1e5e192ae016160aa34
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 20, 2026.

Transparency log

Release files / b53-0.1.0-py3-none-any.whl

Download URL b53-0.1.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a20c6410c1df5f8f75bfde8265b1ba80d2b6c042b03d3e5525b68029f24100ea
BLAKE2b-256 checksum
How to use checksums
644f67601e01cc2452bd44d2581dd09dc4fb6d0d4e327e2a6bb05946db56fe6d
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 20, 2026.

Transparency log

Security for b53 0.1.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.

Select from 2 files

Attestation for b53-0.1.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 20, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page