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 64KB data chunks (Android, AArch64).

Operation Official (C) Rust (Current) Ratio
AES-IGE 0.53 ms 2.92 ms 0.18x
AES-CTR 0.66 ms 2.97 ms 0.22x
AES-CBC 0.50 ms 2.77 ms 0.18x
SHA1 N/A 0.15 ms -
SHA256 N/A 0.30 ms -
RSA Enc. N/A 0.64 ms -
Fact. N/A 0.004 ms -

Note: The official C implementation uses highly optimized assembly for AES on ARM, whereas this Rust implementation currently uses the standard aes crate. Future optimizations may close this gap.

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.7.tar.gz (66.9 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.7-cp38-abi3-win_amd64.whl (216.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_34_x86_64.whl (303.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (341.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ s390x

tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (332.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (298.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARMv7l

tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (288.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

tgcrypto_rs-2.1.7-cp38-abi3-macosx_11_0_arm64.whl (273.6 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tgcrypto_rs-2.1.7-cp38-abi3-macosx_10_12_x86_64.whl (290.6 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: tgcrypto_rs-2.1.7.tar.gz
  • Upload date:
  • Size: 66.9 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.7.tar.gz
Algorithm Hash digest
SHA256 e3c47db73a26f826022920aff56e8097bb41e3d0ae50bbb8d84305cdc1255991
MD5 b2b63a94a30ce090f06ed7cf2f129df6
BLAKE2b-256 8f033f2421de809975d5f677a418d3461fa6293a1b0b221ea3d14901604d06d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7.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.7-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: tgcrypto_rs-2.1.7-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 216.6 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 382ab6b4c0dec99b4fbda048873cc1387f53ee645e35f0b962254bb7332dc415
MD5 ea4d76146918d90eef83082e0b3a96a8
BLAKE2b-256 1650ac56b64903155876a9a9b14e5acf7f07da805e93e848f9ff216f90306e9e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-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.7-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 09f457aa1e1dada7220b560a0ad78494b836e700912828a7fb04364c6d3f2a7f
MD5 eb683df3dc43ec724bb05bd49c659ac0
BLAKE2b-256 ab892ce4bde7912afa407bb83f3a083ceb52f145ead206837a8747ab04f9eca1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_34_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.7-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 724f7fa97ab83b2503d15ccdfa15246d063b7921b35274cddba0311e5bda624d
MD5 28bcda8db3060374b6f4d69281e605db
BLAKE2b-256 4167efa44cbc9caad7ff3b019a401614416c4672c4472156da031e0e5d3b02b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.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.7-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 a8ba919d783c5e3886eac1bac54b183defb609d16deaf91643c683ca8d4a0193
MD5 8b55d07dac612de53e8747d553bd67dd
BLAKE2b-256 814a60207e0685c36ffc31e2d0c17af426c7a86f0a516aa4b1fcc1dd0d8b1bc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.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.7-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 d9f4b05e0dc7aca7fe1f30363fc53e6bfc7407c4b0a29e5672297444992712b1
MD5 17ee579e71e544773172007c88b853bd
BLAKE2b-256 09ff4912339a240dba322c0194ee959fd1d94e3b807574bab58336b3a38163f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.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.7-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 92bf6cddef5e19a7bdec25c7e356f08aad80da1f5f457fadc4130b07940d6efa
MD5 2100bf19ac4b06c1a11bd65ce060cb33
BLAKE2b-256 89239c0d2fd816eebe39295289f436bb6081754703e3f420fe112a1c74fdd8ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_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.

File details

Details for the file tgcrypto_rs-2.1.7-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e7e2c6e5f3131a43be849713677be47ecad81cd9e12488300f8b1a166bc6e65a
MD5 30b208714a8a5bb89dddbd6c41b43295
BLAKE2b-256 818d466b7b6d4a1a207abbf917b5a809792e17dcdbd3b97fe2a3bfd2d2c5ce2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-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.7-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tgcrypto_rs-2.1.7-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 b0c6ca7820b25c81a9d58103e3f9631f7e94f886e961d281257855a6a7df605c
MD5 c478d3b578afd4e3185d135f4c416033
BLAKE2b-256 64451758a98e24dc5f9a2851a10508fe445e040ea592f10a79fc7a52487822e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for tgcrypto_rs-2.1.7-cp38-abi3-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.

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