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 mut stream = new; // same as ctr256, key expanded once
stream.apply_keystream;
stream.apply_keystream;
let = kdf;
AES uses the CPU's AES instructions when they are there (AES-NI on x86, the ARMv8 Cryptography Extensions on ARM), picked at runtime; no build flags needed.
Features
- 3–5× faster than tgcrypto (C extension) in multi-client benchmarks
- Hardware AES (AES-NI on x86, ARMv8 AES on ARM) via the
aescrate's runtime CPU feature detection (cpufeatures) - Zero-copy IGE with block-aligned 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 — safe Rust except two small
unsafeblocks that zero AES key schedules, checked with Miri in CI - 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.