β‘ layer
A modular, production-grade async Rust implementation of the Telegram MTProto protocol.
Written from the ground up to understand Telegram's internals at the lowest level.
π§© What is layer?
layer is a hand-written, bottom-up async Rust implementation of the
Telegram MTProto protocol. Every component β
from the .tl schema parser, to the AES-IGE cipher, to the Diffie-Hellman key
exchange, to the async update stream β is written and owned by this project.
No black boxes. No magic. Just Rust, all the way down.
ποΈ Crate Overview
| Crate | Description |
|---|---|
layer-tl-parser |
Parses .tl schema text into an AST |
layer-tl-gen |
Generates Rust code from the AST at build time |
layer-tl-types |
All Layer 223 constructors, functions and enums |
layer-crypto |
AES-IGE, RSA, SHA, DH key derivation |
layer-mtproto |
MTProto session, DH exchange, message framing |
layer-client |
High-level async client β auth, bots, updates, 2FA |
layer-app |
Interactive demo binary (not published) |
layer-connect |
Raw DH connection demo (not published) |
layer/
βββ layer-tl-parser/ ββ Parses .tl schema text β AST
βββ layer-tl-gen/ ββ AST β Rust source (build-time)
βββ layer-tl-types/ ββ Auto-generated types, functions & enums (Layer 223)
βββ layer-crypto/ ββ AES-IGE, RSA, SHA, auth key derivation
βββ layer-mtproto/ ββ MTProto session, DH, framing, transport
βββ layer-client/ ββ High-level async Client API
βββ layer-connect/ ββ Demo: raw DH + getConfig
βββ layer-app/ ββ Demo: interactive login + update stream
π Quick Start
Add to your Cargo.toml:
[]
= "0.1.2"
= { = "1", = ["full"] }
π€ User Account
use ;
async
π€ Bot
use ;
async
β Features
π Cryptography
- AES-IGE encryption / decryption (MTProto 2.0)
- RSA encryption with Telegram's public keys
- SHA-1 and SHA-256 hashing
- Auth key derivation from DH nonce material
- PQ factorization (Pollard's rho)
- Diffie-Hellman shared secret computation
π‘ MTProto
- Full 3-step DH key exchange handshake
- MTProto 2.0 encrypted sessions
- Proper message framing (salt, session_id, msg_id, seq_no)
- Abridged TCP transport
msg_container(multi-message) unpacking- gzip-packed response decompression
- Server salt tracking, pong, bad_server_salt handling
π¦ TL Type System
- Full
.tlschema parser - Build-time Rust code generation
- All Layer 223 constructors β 2,295 definitions
Serializable/Deserializabletraits for all typesRemoteCalltrait for all RPC functions- Optional:
Debug,serde,name_for_id(u32)
π€ Client
Client::connect()β async TCP + DH + initConnection- Session persistence across restarts
- Phone code login + 2FA SRP
- Bot token login
- DC migration (PHONE_MIGRATE, USER_MIGRATE)
- FLOOD_WAIT auto-retry with configurable policy
- Async update stream with typed events
- Send / delete / fetch messages
- Dialogs list
- Username / peer resolution
- Raw
RemoteCallescape hatch for any API method
π§ Feature Flags (layer-tl-types)
| Feature | Default | Description |
|---|---|---|
tl-api |
β | High-level Telegram API schema |
tl-mtproto |
β | Low-level MTProto schema |
impl-debug |
β | #[derive(Debug)] on generated types |
impl-from-type |
β | From<types::T> for enums::E |
impl-from-enum |
β | TryFrom<enums::E> for types::T |
name-for-id |
β | name_for_id(u32) -> Option<&'static str> |
impl-serde |
β | serde::Serialize / Deserialize |
π Updating to a New TL Layer
# 1. Replace the schema
# 2. Build β types regenerate automatically
π§ͺ Tests
π License
Licensed under either of, at your option:
- MIT License β see LICENSE-MIT
- Apache License, Version 2.0 β see LICENSE-APACHE
π€ Author
π¦ Project: github.com/ankit-chaubey/layer
π Acknowledgements
-
Lonami for grammers. Portions of this project include code derived from the grammers project, which is dual-licensed under the MIT or Apache-2.0 licenses. The architecture, design decisions, SRP math, and session handling are deeply inspired by grammers. It's a fantastic library and an even better learning resource. Thank you for making it open source! π
-
Telegram for the detailed MTProto specification.
-
The Rust async ecosystem
tokio,getrandom,flate2, and friends. -
π€ AI tools used for clearer documentation and better comments across the repo (2026 is a good year to use AI).
Even regrets π after making these docs through AI. iykyk. Too lazy to revert and type again, so it stays as is!
β οΈ Telegram Terms of Service
As with any third-party Telegram library, please ensure that your usage complies with Telegramβs Terms of Service. Misuse or abuse of the Telegram API may result in temporary limitations or permanent bans of Telegram accounts.
layer.. because sometimes you have to build it yourself to truly understand it.