WarpCrypto
WarpCrypto: TgCrypto, Rewritten in Rust
WarpCrypto is a Python extension written in Rust (built with PyO3 and maturin). It implements the cryptography Telegram’s MTProto protocol needs, and works as a drop-in replacement for TgCrypto.
What It Implements
- AES-256-IGE: used by MTProto 2.0
- AES-256-CTR: used for CDN encrypted files
- kdf: the MTProto 2.0 key derivation function
- pack_message / unpack_message: KDF, AES-IGE and message framing in a single call, with msg_key verification on the way in
Key Features
- 3–5× faster than TgCrypto in multi-client benchmarks (1–128 concurrent clients)
- Hardware AES: AES-NI on x86_64 and ARMv8 AES on ARM, picked at runtime so one wheel runs everywhere
- Releases the GIL on large inputs, so other Python threads keep running during crypto work
- No per-call allocations: a reused per-thread scratch buffer means a 1 MiB message costs no extra Rust heap memory
- Memory safe: no buffer overflows, use-after-free or data races
- Strict input checks: key, IV and session lengths are validated instead of crashing
Performance Work
Most of the speed comes from keeping the hot loop inside the AES backend. IGE chains block to block, so the dispatch cost of each block call used to dominate. Running the whole loop inside the backend removed it. CTR keeps an 8-block unroll so blocks are processed in parallel.
Cutting redundant buffer copies gave unpack_message a 41.8% speedup on 1 MiB messages, with output byte-identical to earlier builds.
Try It
pip install WarpCrypto
import os
import warpcrypto
data = os.urandom(1024)
key, iv = os.urandom(32), os.urandom(32)
encrypted = warpcrypto.ige256_encrypt(data, key, iv)
assert warpcrypto.ige256_decrypt(encrypted, key, iv) == data