Skip to main content

High-performance Rust-powered TgCrypto module providing secure and optimized MTProto cryptography for Pyrogram.

Project description

tgcrypto-rs

A high-performance Rust implementation of the tgcrypto Python extension module for Pyrogram.

This module provides cryptographic primitives required for Telegram's MTProto protocol, implemented in Rust for optimal performance and security.

Features

  • AES-256-IGE encryption/decryption
  • AES-256-CTR encryption/decryption
  • AES-256-CBC encryption/decryption
  • SHA-1 hashing
  • SHA-256 hashing
  • RSA encryption with Telegram server public keys
  • Pollard's rho integer factorization for MTProto handshake
  • MTProto helpers (session ID generation)

Recent Updates

  • PyO3 0.28+: Updated to the latest PyO3 for better performance and support for Python 3.13.
  • Modern Dependencies: Updated all cryptographic and utility crates to their latest stable versions.
  • Enhanced Type Hints: Improved .pyi file for better IDE support.
  • Modern CI/CD: Updated GitHub Actions to use latest tools and secure publishing.

Performance Comparison

Benchmarks were performed on 1MB data chunks (Android, AArch64).

Operation Official (C) Rust (Current) Ratio (Rust/C)
AES-IGE 88.33 MB/s 196.82 MB/s 2.23x
AES-CTR 73.09 MB/s 163.55 MB/s 2.24x
AES-CBC 89.76 MB/s 436.91 MB/s 4.87x

Note: Rust now uses OpenSSL's AES backend for these measurements on ARM.

Installation

Prerequisites

  • Rust toolchain (install via rustup)
  • Python 3.8+
  • maturin for building Python extensions
pip install maturin

Build and Install

cd pyrogram-tgcrypto
maturin develop --release

This will compile the Rust code and install the tgcrypto module into your current Python environment.

Usage

Once installed, the module can be imported directly in Python:

import tgcrypto

# AES-256-IGE
encrypted = tgcrypto.ige256_encrypt(data, key, iv)
decrypted = tgcrypto.ige256_decrypt(encrypted, key, iv)

# AES-256-CTR
encrypted = tgcrypto.ctr256_encrypt(data, key, iv, state)
decrypted = tgcrypto.ctr256_decrypt(encrypted, key, iv, state)

# AES-256-CBC
encrypted = tgcrypto.cbc256_encrypt(data, key, iv)
decrypted = tgcrypto.cbc256_decrypt(encrypted, key, iv)

# Hashing
sha1_hash = tgcrypto.sha1(data)
sha256_hash = tgcrypto.sha256(data)

# RSA encryption
encrypted = tgcrypto.rsa_encrypt(data, fingerprint)

# Factorization
factor = tgcrypto.factorize(pq)

# Session ID
session_id = tgcrypto.get_session_id(auth_key)

API Reference

ige256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Encrypt data using AES-256 in IGE mode.

  • data: Must be a multiple of 16 bytes
  • key: Must be 32 bytes
  • iv: Must be 32 bytes

ige256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Decrypt data using AES-256 in IGE mode.

ctr256_encrypt(data: bytes, key: bytes, iv: bytes, state: int) -> bytes

Encrypt data using AES-256 in CTR mode.

  • data: Any length
  • key: Must be 32 bytes
  • iv: Must be 16 bytes
  • state: Counter state offset

ctr256_decrypt(data: bytes, key: bytes, iv: bytes, state: int) -> bytes

Decrypt data using AES-256 in CTR mode.

cbc256_encrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Encrypt data using AES-256 in CBC mode.

  • data: Must be a multiple of 16 bytes
  • key: Must be 32 bytes
  • iv: Must be 16 bytes

cbc256_decrypt(data: bytes, key: bytes, iv: bytes) -> bytes

Decrypt data using AES-256 in CBC mode.

sha1(data: bytes) -> bytes

Compute SHA-1 hash of data. Returns 20 bytes.

sha256(data: bytes) -> bytes

Compute SHA-256 hash of data. Returns 32 bytes.

rsa_encrypt(data: bytes, fingerprint: int) -> bytes

Encrypt data using RSA with Telegram server public key.

  • data: Data to encrypt
  • fingerprint: Telegram server key fingerprint (e.g., -4344800451088585951)

Returns 256-byte encrypted data.

factorize(pq: int) -> int

Find a non-trivial factor of a semiprime number using Pollard's rho algorithm. Used in MTProto key exchange.

get_session_id(auth_key: bytes) -> bytes

Generate session ID from authentication key. Returns 8 bytes.

Performance

This Rust implementation provides significant performance improvements over pure Python implementations:

  • AES operations: ~10-50x faster
  • Hashing: ~5-20x faster
  • Factorization: ~100x+ faster for large numbers

The GIL is released during heavy cryptographic operations, allowing true parallelism in multi-threaded applications.

Security

This implementation uses well-audited cryptographic crates:

  • aes - AES block cipher
  • ctr - CTR mode
  • cbc - CBC mode
  • sha1 - SHA-1 hash
  • sha2 - SHA-2 family hashes
  • num-bigint - Big integer arithmetic

No unsafe code is used for cryptographic operations.

License

LGPL-3.0-or-later (same as original tgcrypto)

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tgcrypto_rs-2.1.13.tar.gz (66.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tgcrypto_rs-2.1.13-cp314-cp314-macosx_11_0_arm64.whl (262.3 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

tgcrypto_rs-2.1.13-cp314-cp314-macosx_10_12_x86_64.whl (276.3 kB view details)

Uploaded CPython 3.14macOS 10.12+ x86-64

tgcrypto_rs-2.1.13-cp312-cp312-win_amd64.whl (198.4 kB view details)

Uploaded CPython 3.12Windows x86-64

tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.38+ x86-64

tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_aarch64.whl (2.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.38+ ARM64

File details

Details for the file tgcrypto_rs-2.1.13.tar.gz.

File metadata

  • Download URL: tgcrypto_rs-2.1.13.tar.gz
  • Upload date:
  • Size: 66.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcrypto_rs-2.1.13.tar.gz
Algorithm Hash digest
SHA256 92bb50abd86890c2162d1840ee67f67dce31bfd309ab6c6b1157c2934196bfe9
MD5 71706a355e76ec723f97b052f62bfb33
BLAKE2b-256 365d77605ac235a43399ce16e1efa64d18bf64b6412716194972d6d63408e21d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.13.tar.gz:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.13-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.13-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fa9a53f29a5cb300ecbffa98bf9b5800245a5bc85b145ebbef82859e016492e5
MD5 29769184780158f88620702df5cf3476
BLAKE2b-256 da93e700bfa5fceb09167c1fa59d66b6b6aa47c72bf61c149139f54b33f3cf40

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.13-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.13-cp314-cp314-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.13-cp314-cp314-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 60e043f64f505299a294562e1048d1ae2b44e6aed333d8da53e1af9418d8f158
MD5 c895f471366f77d8c18f163720ee2c94
BLAKE2b-256 0cfad7441637f1e7ec4f9cd0f4c2c1782dd1bbf83c619f36b17c714a4b277205

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.13-cp314-cp314-macosx_10_12_x86_64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.13-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.13-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 777b35ba088199046d31843428d7f6da8b61f6ba044192cda7d79c80ef3ef8c6
MD5 375ba80c0beca47f8f7f2e1ec790ba05
BLAKE2b-256 0664fc462e59f41ee9d163f56d10cc5a760a1d42377ebf5a429e368ec6675575

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.13-cp312-cp312-win_amd64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 bd06ec3e5d0ed2025217c43c48dee3aaa767d1e353b8529dd6c3f5af00250381
MD5 0349f81fc4924fcb26d0b673fd550e83
BLAKE2b-256 11e4b1dc2a9d394cd035835c02881c1fcb87b86b2bfc4fa8aecf0da4999784b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_x86_64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_aarch64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_aarch64.whl
Algorithm Hash digest
SHA256 8bd835bb8cc83f747d119729dd1d38fcada7e784f15fe65372d9d1f17109e4d8
MD5 424397a4451ddded0a84ac619856ad93
BLAKE2b-256 61cc7eb43d4267b445be97e9e01157d92f27b7ce071f8788a88d9bb455757bd2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.13-cp312-cp312-manylinux_2_38_aarch64.whl:

Publisher: release.yaml on troublescope/tgcrypto-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page