Skip to main content

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]"

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, HashAlg, KeyExchangeAlg, SigningAlg
from ratchetkit.suite.double import CryptoSuiteDoubleRatchet

suite = CryptoSuiteDoubleRatchet(
    digest=HashAlg.SHA256,
    cipher=CipherAlg.AES256,
    key_exchange=KeyExchangeAlg.X25519,
    signing=SigningAlg.ED25519,
)

alice = IdentityKeyPair.generate(suite)
bob = IdentityKeyPair.generate(suite)
spk = SignedPreKey.generate(1, bob, suite)
opk = OneTimePreKey.generate(1, suite)
bundle = suite.create_bundle(bob, spk, opk)

alice_session, initial = suite.initiate(alice, bundle)
msg = alice_session.encrypt(b"hello")
# Wire formats: initial.to_bytes(), msg.to_bytes()

bob_session, plaintext = suite.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 HMAC-SIV (tag as CTR IV). All PQ fields are required

from ratchetkit.keys.prekey import OneTimePreKey, SignedPqPreKey, SignedPreKey
from ratchetkit.suite import CipherAlg, HashAlg, KeyExchangeAlg, SigningAlg
from ratchetkit.suite.triple import CryptoSuiteTripleRatchet

suite = CryptoSuiteTripleRatchet(
    digest=HashAlg.SHA256,
    cipher=CipherAlg.AES256,
    key_exchange=KeyExchangeAlg.X25519,       # classical DH ratchet
    signing=SigningAlg.ED25519,
    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, suite)
opk = OneTimePreKey.generate(1, suite)
pq = SignedPqPreKey.generate(1, bob, suite, suite.pq_kem)
bundle = suite.create_bundle(bob, spk, pq, opk)

alice_session, initial = suite.initiate(alice, bundle)
msg = alice_session.encrypt(b"triple hello")
bob_session, plain = suite.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, HashAlg, KeyExchangeAlg, SigningAlg
from ratchetkit.suite.double import CryptoSuiteDoubleRatchet
from ratchetkit.suite.triple import CryptoSuiteTripleRatchet

# Classical Double Ratchet only
double = CryptoSuiteDoubleRatchet(
    digest=HashAlg.SHA512,
    cipher=CipherAlg.CHACHA20,
    key_exchange=KeyExchangeAlg.SECP256R1,
    signing=SigningAlg.ECDSA_SECP256R1,
)

# Triple Ratchet: classical + required PQ half
triple = CryptoSuiteTripleRatchet(
    digest=HashAlg.SHA256,
    cipher=CipherAlg.AES256,
    key_exchange=KeyExchangeAlg.X25519,
    signing=SigningAlg.ED25519,
    pq_key_exchange=KeyExchangeAlg.MLKEM768,
    scka_chunks=8,
)

# KEM-only agreement 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,
    signing=SigningAlg.MLDSA65,
)

CryptoSuiteDoubleRatchet holds digest / cipher / key_exchange / signing / kem / signer. CryptoSuiteTripleRatchet subclasses it and adds required pq_key_exchange, scka_chunks, and pq_kem. Agreement and signing are independent (KeyExchangeAlg vs SigningAlg).

Block ciphers always use CTR inside the cipher algorithm class. The HMAC tag is the CTR / ChaCha nonce (SIV). Cipher keys must be at least 256 bits (AES-128 / Camellia-128 are rejected).

Each hash, cipher, KEM, and signing backend lives under ratchetkit/algorithms/hash/, cipher/, kem/, and signing/.

KeyExchangeAlg Agreement Notes
X25519 / X448 / SECP... DH classic Double Ratchet
RSA2048 / RSA3072 / RSA4096 RSA-OAEP KEM OPK required
MLKEM768 / MLKEM1024 ML-KEM OPK required; cryptography>=45
SigningAlg Notes
ED25519 / ED448 Edwards curves
ECDSA_SECP... NIST ECDSA
RSA_PSS_2048 / 3072 / 4096 RSA-PSS
MLDSA65 / MLDSA87 ML-DSA; 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 HMAC-SIV (tag is CTR IV), 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, ...
    signing/  # Ed25519, Ecdsa, RsaPss, Mldsa, ...
  suite/      # Double + Triple suites, alg enums, registry
  keys/       # ChainKey, RootKey, MessageKeys, PreKeyBundle, ...
  handshake/  # X3DH + PQXDH handshakes
  scka/       # Chunked ML-KEM Sparse Continuous Key Agreement
  packing.py  # Constructor base + ConstructorId
  ratchet/
    message/    # RatchetMessage ABC, Double + Triple wire codecs
    protocol/   # Ratchet ABC, DoubleRatchet, TripleRatchet
    spqr.py     # SparsePostQuantumRatchet
  session.py  # Session facade

Examples under example/:

  • demo1/ — Unix-socket Alice/Bob (X3DH/PQXDH + live Double/Triple Ratchet)
  • demo2/ — session resume (to_bytes / from_bytes) + out-of-order delivery

Tests

pytest

Metadata

Release files for ratchetkit 3.0.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 ratchetkit 3.0.0
File Size Uploaded Provenance Actions
ratchetkit-3.0.0.tar.gz 115.7 kB With provenance Details for ratchetkit-3.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 197.4 kB

Release files / ratchetkit-3.0.0.tar.gz

Download URL ratchetkit-3.0.0.tar.gz
Size 115.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6b0ad17566fc68a2b810f25a244b52c43869479617842e320890c714dd796f6c
BLAKE2b-256 checksum
How to use checksums
2c686f9d2e7f1e1118a609f9ad52b682e3008d699931942609229da010229df5
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 8, 2026.

Transparency log

Release files / ratchetkit-3.0.0-py3-none-any.whl

Download URL ratchetkit-3.0.0-py3-none-any.whl
Size 81.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
196c3a2ebcc4ecd1fa26392449d967bef603bdb1f99a341dadd29955d349205e
BLAKE2b-256 checksum
How to use checksums
99477ff667b4eda7f63b5a82fd3485173ab1a9c3e0124bbc684e8333b5b12050
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 8, 2026.

Transparency log

Security for ratchetkit 3.0.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 ratchetkit-3.0.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 8, 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