WarpCrypto
Fast Cryptography Extension Library — Rust Implementation
WarpCrypto is a high-performance cryptography library written in Rust as a Python extension (via PyO3 + maturin). It implements the cryptographic algorithms required by Telegram's MTProto protocol:
AES-256-IGE— used in MTProto v2.0AES-256-CTR— used for CDN encrypted fileskdf— key derivation function per MTProto 2.0 specpack_message/unpack_message— combined KDF+AES-IGE in a single call (1 GIL release vs 3 for tgcrypto)
It is also a plain Rust crate, without Python:
[]
= "2"
let packed = pack_message;
let msg = unpack_message?;
ige256_encrypt?; // data.len() must be a multiple of 16
ctr256; // encrypt and decrypt are the same
let = kdf;
On ARM (Apple Silicon, ARM servers), build with RUSTFLAGS="--cfg aes_armv8" to use the CPU's AES instructions; without it AES runs in software. The Python wheels already do this.
Features
- 3–5× faster than tgcrypto (C extension) in multi-client benchmarks
- AES-NI hardware acceleration via the
aescrate's runtime CPU feature detection (cpufeatures) - Zero-copy IGE with block-aligned GenericArray operations
- Block-based CTR — processes 16-byte blocks with
chunks_exact_mut, not byte-by-byte - Proper state propagation — IV and state bytearrays are mutated in-place matching tgcrypto exactly
- Memory safe — Rust's type system guarantees no buffer overflows, use-after-free, or data races
- Zero compiler warnings
Requirements
- Python 3.8 or higher
- Rust toolchain (for building from source; pre-built wheels available for most platforms)
Installation
Install from source:
API
...
...
...
...
...
...
...
Usage
IGE Mode (MTProto 2.0)
=
=
=
=
=
assert ==
CTR Mode (CDN)
=
=
=
=
=
=
assert ==
KDF (MTProto 2.0 key derivation)
=
=
, = # x=0
, = # x=8
Testing
Performance
WarpCrypto outperforms tgcrypto (C extension) by 3–5× across all client counts (1–128 concurrent clients). Performance scales with AES-NI availability (most x86_64 and Apple Silicon CPUs).
License
Apache-2.0. Forks and copies must keep the NOTICE file with credit to WarpCrypto and Riajul. Originally based on TgCrypto by Dan.