ratchetkit
Python X3DH / PQXDH key agreement + Double Ratchet, Sparse Post-Quantum Ratchet (SPQR), and Triple Ratchet with pluggable crypto primitives.
Not Signal-wire-compatible: no protobuf, no session-record format. Includes native PQXDH (classical DH + signed PQ prekey) for Triple suites. Triple Ratchet follows Signal Double Ratchet rev 4 §5-§6 and the hybrid design in ePrint 2025/078.
Licensed under AGPL-3.0-only (same family as libsignal).
Install
pip install ratchetkit
Requires Python 3.10+ and cryptography>=45 (ML-KEM / ML-DSA).
Development
pip install -e ".[dev]"
That installs ratchetkit into the active environment so import ratchetkit
works without PYTHONPATH or sys.path hacks. The example/ demo is not
part of the wheel; run it from the repo root as python -m example....
Quick start (classical Double Ratchet)
from ratchetkit.keys.identity import IdentityKeyPair
from ratchetkit.keys.prekey import OneTimePreKey, SignedPreKey
from ratchetkit.session import Session
from ratchetkit.suite import (
CipherAlg,
CryptoSuiteDoubleRatchet,
HashAlg,
KeyExchangeAlg,
)
suite = CryptoSuiteDoubleRatchet(
digest=HashAlg.SHA256,
cipher=CipherAlg.AES256,
key_exchange=KeyExchangeAlg.X25519,
)
alice = IdentityKeyPair.generate(suite)
bob = IdentityKeyPair.generate(suite)
spk = SignedPreKey.generate(1, bob)
opk = OneTimePreKey.generate(1, suite)
bundle = suite.create_bundle(bob, spk, opk)
alice_session, initial = Session.initiate(alice, bundle)
msg = alice_session.encrypt(b"hello")
# Wire formats: initial.to_bytes(), msg.to_bytes()
bob_session, plaintext = Session.accept(bob, spk, initial, msg, opk)
assert plaintext == b"hello"
reply = bob_session.encrypt(b"hi back")
assert alice_session.decrypt(reply) == b"hi back"
# Optional per-message AD is appended to the handshake identity AD.
msg = alice_session.encrypt(b"meta", associated_data=b"msg-id:1")
assert bob_session.decrypt(msg, associated_data=b"msg-id:1") == b"meta"
# Persist / restore full session state (includes suite + ratchet keys).
blob = alice_session.to_bytes()
alice_session = Session.from_bytes(blob)
Triple Ratchet (classical + sparse PQ)
Use CryptoSuiteTripleRatchet: classical Double Ratchet in parallel with a
Sparse PQ Ratchet driven by chunked ML-KEM SCKA. Message keys are combined with
KDF_HYBRID before encrypt-then-HMAC. All PQ fields are required
from ratchetkit.suite import (
CipherAlg,
CryptoSuiteTripleRatchet,
HashAlg,
KeyExchangeAlg,
)
suite = CryptoSuiteTripleRatchet(
digest=HashAlg.SHA256,
cipher=CipherAlg.AES256,
key_exchange=KeyExchangeAlg.X25519, # classical DH ratchet
pq_key_exchange=KeyExchangeAlg.MLKEM768, # sparse PQ ratchet
scka_chunks=8, # split each ML-KEM payload across 8 app messages
)
alice = IdentityKeyPair.generate(suite)
bob = IdentityKeyPair.generate(suite)
spk = SignedPreKey.generate(1, bob)
opk = OneTimePreKey.generate(1, suite)
pq = suite.generate_pq_prekey(1, bob)
bundle = suite.create_bundle(bob, spk, opk, pq)
alice_session, initial = Session.initiate(alice, bundle)
msg = alice_session.encrypt(b"triple hello")
bob_session, plain = Session.accept(
bob, spk, initial, msg, opk, our_pq_prekey=pq,
)
assert plain == b"triple hello"
scka_chunks amortizes large ML-KEM public keys / ciphertexts (same idea as
the Triple Ratchet paper's chunk encoding). Smaller values recover PQ entropy
faster at higher per-message overhead.
Crypto suites
from ratchetkit.suite import (
CipherAlg,
CryptoSuiteDoubleRatchet,
CryptoSuiteTripleRatchet,
HashAlg,
KeyExchangeAlg,
)
# Classical Double Ratchet only
double = CryptoSuiteDoubleRatchet(
digest=HashAlg.SHA512,
cipher=CipherAlg.CHACHA20,
key_exchange=KeyExchangeAlg.SECP256R1,
)
# Triple Ratchet: classical + required PQ half
triple = CryptoSuiteTripleRatchet(
digest=HashAlg.SHA256,
cipher=CipherAlg.AES256,
key_exchange=KeyExchangeAlg.X25519,
pq_key_exchange=KeyExchangeAlg.MLKEM768,
scka_chunks=8,
)
# KEM-only identity path still uses Double Ratchet suite (OPK required).
# This is not Triple Ratchet - KEM steps run inside Double Ratchet.
pq_identity = CryptoSuiteDoubleRatchet(
digest=HashAlg.SHA3_256,
cipher=CipherAlg.AES256,
key_exchange=KeyExchangeAlg.MLKEM768,
)
CryptoSuiteDoubleRatchet holds digest / cipher / key_exchange / kem.
CryptoSuiteTripleRatchet subclasses it and adds required
pq_key_exchange, scka_chunks, and pq_kem.
Block ciphers always use CTR inside the cipher algorithm class. Message authentication is always encrypt-then-HMAC. Cipher keys must be at least 256 bits (AES-128 / Camellia-128 are rejected).
Each hash, cipher, and KEM lives in its own class file under
ratchetkit/algorithms/hash/, ratchetkit/algorithms/cipher/, and
ratchetkit/algorithms/kem/.
KeyExchangeAlg |
Agreement | Identity signatures | Notes |
|---|---|---|---|
X25519 / X448 / SECP... |
DH | Ed25519 / Ed448 / ECDSA | classic Double Ratchet |
RSA2048 / RSA4096 |
RSA-OAEP KEM | RSA-PSS | OPK required |
MLKEM768 / MLKEM1024 |
ML-KEM | ML-DSA-65 / ML-DSA-87 | OPK required; cryptography>=45 |
For Triple Ratchet, use classical key_exchange (e.g. X25519) and set
pq_key_exchange to MLKEM768 or MLKEM1024 on CryptoSuiteTripleRatchet.
Security notes (Double Ratchet alignment)
This is not a wire-compatible Signal client. It follows the Double Ratchet design (trial decrypt, KDF chains, skipped-key handling) with these hardened behaviors:
| Topic | Behavior |
|---|---|
| Trial decrypt | State clones on decrypt; only commits after MAC success |
| Skipped keys | Hard cap (MAX_MESSAGE_KEYS); overflow raises, never silently drops |
| Forward jump | Cap (MAX_FORWARD_JUMPS) stops skip DoS |
| DH public keys | Invalid / low-order points -> InvalidMessage (no state change) |
| Empty / zero secrets | Root, chain, and DH outputs rejected |
| MAC | Encrypt-then-HMAC, constant-time compare; tag_size default 16 (min 10) |
| Secure deletion | Python cannot guarantee wipe of GC'd bytes; drop references promptly |
| OPKs | Caller must delete one-time prekeys after a successful accept |
Limits default to libsignal-like values (RatchetLimits). Tune carefully:
raising MAX_MESSAGE_KEYS / MAX_FORWARD_JUMPS increases DoS surface.
State
The ratchet mutates on every successful encrypt/decrypt. Failed MAC checks do not advance ratchet state.
Session persistence
Session.to_bytes() / Session.from_bytes() serialize the full live session:
crypto suite, associated data, and all ratchet state (root/chain keys, skipped
message keys, SPQR/SCKA state for Triple). The blob is self-describing (suite
is inside), so restore does not need a separate suite argument.
blob = session.to_bytes() # secret key material - store encrypted
session = Session.from_bytes(blob)
Treat the blob as secret. This is not a Signal session-record format and is not stable across library major versions unless documented otherwise.
Callers must delete one-time prekeys after a successful accept (the library does not track OPK consumption).
Layout
ratchetkit/
algorithms/
hash/ # Sha256, Sha512, ...
cipher/ # Aes256, ChaCha20, ...
kem/ # X25519, Secp256r1, Rsa2048, Mlkem768, ...
suite/ # Double + Triple suites, alg enums
keys/ # ChainKey, RootKey, MessageKeys, PreKeyBundle, ...
handshake/ # X3DH + PQXDH handshakes
scka/ # Chunked ML-KEM Sparse Continuous Key Agreement
packing.py # ConstructorId enum
ratchet/
message/ # RatchetMessage ABC, Double + Triple wire codecs
protocol/ # Ratchet ABC, DoubleRatchet, TripleRatchet
spqr.py # SparsePostQuantumRatchet
session.py # Session facade
Tests
pytest
Metadata
Release files for ratchetkit 1.2.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 |
|---|---|---|---|---|
| ratchetkit-1.2.0.tar.gz | 70.9 kB | With provenance | Details for ratchetkit-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ratchetkit-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for ratchetkit-1.2.0-py3-none-any.whl |
Total release size: 155.0 kB
Release files / ratchetkit-1.2.0.tar.gz
| Download URL | ratchetkit-1.2.0.tar.gz |
|---|---|
| Size | 70.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
15c1b01c1b063926b668255847387f21794b5528a2ffa6e890e9120c0172fd99
|
|
BLAKE2b-256 checksum How to use checksums |
e3efc5f253d69d0ad4477f928ab7853464aee70cd8c2c6a89bb742e0c5bfe0e8
|
| 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 Oct 28, 2026.
Transparency logRelease files / ratchetkit-1.2.0-py3-none-any.whl
| Download URL | ratchetkit-1.2.0-py3-none-any.whl |
|---|---|
| Size | 84.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
985e280ecd1bb6688fcd748b82da7ce6eb551155d49cb0dee41e087654bdb600
|
|
BLAKE2b-256 checksum How to use checksums |
9965bf9f0037d0a045775826e70d706bd4c65c9ba33523b1ce4a8d91c030f090
|
| 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 Oct 28, 2026.
Transparency logSecurity for ratchetkit 1.2.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 ratchetkit-1.2.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 Oct 28, 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.