miniproto_native::crypto
Provenance
Section titled “Provenance”- Crate:
miniproto_native - Rust visibility:
crate - Source:
rust/miniproto/src/crypto.rs - Python exposure: Not evidenced by static PyO3 attributes.
Documented items
Section titled “Documented items”miniproto_native::crypto::aes_256_gcm_decryptminiproto_native::crypto::scrypt_deriveminiproto_native::crypto::pq_factorizeminiproto_native::crypto::registerminiproto_native::crypto::native_availableminiproto_native::crypto::sha1_digestminiproto_native::crypto::sha256_digestminiproto_native::crypto::mtproto_auth_key_idminiproto_native::crypto::mtproto_message_keyminiproto_native::crypto::mtproto_derive_aes_key_ivminiproto_native::crypto::mtproto_encrypt_payloadminiproto_native::crypto::mtproto_decrypt_payloadminiproto_native::crypto::xor_bytesminiproto_native::crypto::aes_256_ige_encryptminiproto_native::crypto::aes_256_ige_decryptminiproto_native::crypto::aes_256_cbc_encryptminiproto_native::crypto::aes_256_cbc_decryptminiproto_native::crypto::aes_256_ctr_cryptminiproto_native::crypto::aes_256_gcm_encrypt
cargo-docs-md rendering
Section titled “cargo-docs-md rendering”miniproto_native / crypto
Module crypto
Section titled “Module crypto”Registers cryptographic Python callables. Native cryptographic primitives used by MTProto and its Python fallback-compatible API.
Each #[pyfunction] is exported under its Rust name in miniproto._native. Byte-heavy
wrappers conditionally release the GIL, while *_raw functions are Rust-only building blocks
that never require it. PyO3 rejects incompatible Python argument conversion first, preserving
its TypeError, OverflowError, or source exception; algorithm and byte-shape validation in
this module intentionally return Python ValueError. No caller-facing unsafe API is exposed.
Contents
Section titled “Contents”- Functions
detach_if_largeregisternative_available__pyfunction_native_availablesha1_digest__pyfunction_sha1_digestsha256_digest__pyfunction_sha256_digestmtproto_auth_key_id__pyfunction_mtproto_auth_key_idmtproto_message_key__pyfunction_mtproto_message_keymtproto_derive_aes_key_iv__pyfunction_mtproto_derive_aes_key_ivmtproto_encrypt_payload__pyfunction_mtproto_encrypt_payloadmtproto_decrypt_payload__pyfunction_mtproto_decrypt_payloadxor_bytes__pyfunction_xor_bytesaes_256_ige_encrypt__pyfunction_aes_256_ige_encryptaes_256_ige_decrypt__pyfunction_aes_256_ige_decryptaes_256_cbc_encrypt__pyfunction_aes_256_cbc_encryptaes_256_cbc_decrypt__pyfunction_aes_256_cbc_decryptaes_256_ctr_crypt__pyfunction_aes_256_ctr_cryptaes_256_gcm_encrypt__pyfunction_aes_256_gcm_encryptaes_256_gcm_decrypt__pyfunction_aes_256_gcm_decryptscrypt_derive__pyfunction_scrypt_derivepq_factorize__pyfunction_pq_factorizesha1_digest_rawsha256_digest_rawmtproto_auth_key_id_rawmtproto_message_key_rawmtproto_derive_aes_key_iv_rawmtproto_encrypt_payload_rawmtproto_decrypt_payload_rawxor_bytes_rawaes_256_ige_encrypt_rawaes_256_ige_decrypt_rawaes_256_cbc_encrypt_rawaes_256_cbc_decrypt_rawaes_256_ctr_crypt_rawaes_256_gcm_encrypt_rawaes_256_gcm_decrypt_rawscrypt_derive_rawpq_factorize_rawvalidate_auth_keyvalidate_msg_keyvalidate_block_multiplevalidate_aes_keyvalidate_ige_ivvalidate_cbc_ctr_ivvalidate_gcm_noncedirection_offsetxor_blockincrement_countergcdmul_modpow_modis_prime_u64factor_u64
- Constants
Quick Reference
Section titled “Quick Reference”| Item | Kind | Description |
|---|---|---|
detach_if_large | fn | Runs f without the GIL when its input work estimate exceeds the native threshold. |
register | fn | Registers this module’s fallback-compatible Python callables on miniproto._native. |
native_available | fn | Implements Python native_available, which always returns true while this compiled callable is importable. |
__pyfunction_native_available | fn | |
sha1_digest | fn | Computes Python sha1_digest(data) and returns the 20-byte SHA-1 digest. |
__pyfunction_sha1_digest | fn | |
sha256_digest | fn | Computes Python sha256_digest(data) and returns the 32-byte SHA-256 digest. |
__pyfunction_sha256_digest | fn | |
mtproto_auth_key_id | fn | Computes Python mtproto_auth_key_id(auth_key) from a validated 256-byte key. |
__pyfunction_mtproto_auth_key_id | fn | |
mtproto_message_key | fn | Computes Python mtproto_message_key for padded plaintext and one MTProto direction. |
__pyfunction_mtproto_message_key | fn | |
mtproto_derive_aes_key_iv | fn | Derives the AES-256 key and IV used by Python mtproto_derive_aes_key_iv. |
__pyfunction_mtproto_derive_aes_key_iv | fn | |
mtproto_encrypt_payload | fn | Encrypts padded MTProto plaintext for Python mtproto_encrypt_payload. |
__pyfunction_mtproto_encrypt_payload | fn | |
mtproto_decrypt_payload | fn | Decrypts and verifies Python mtproto_decrypt_payload ciphertext. |
__pyfunction_mtproto_decrypt_payload | fn | |
xor_bytes | fn | Returns the bytewise exclusive-or of Python xor_bytes(left, right) inputs. |
__pyfunction_xor_bytes | fn | |
aes_256_ige_encrypt | fn | Encrypts block-aligned bytes with Python aes_256_ige_encrypt. |
__pyfunction_aes_256_ige_encrypt | fn | |
aes_256_ige_decrypt | fn | Decrypts block-aligned bytes with Python aes_256_ige_decrypt. |
__pyfunction_aes_256_ige_decrypt | fn | |
aes_256_cbc_encrypt | fn | Encrypts block-aligned bytes with Python aes_256_cbc_encrypt without padding. |
__pyfunction_aes_256_cbc_encrypt | fn | |
aes_256_cbc_decrypt | fn | Decrypts block-aligned bytes with Python aes_256_cbc_decrypt without padding. |
__pyfunction_aes_256_cbc_decrypt | fn | |
aes_256_ctr_crypt | fn | Applies Python aes_256_ctr_crypt to data using AES-CTR keystream XOR. |
__pyfunction_aes_256_ctr_crypt | fn | |
aes_256_gcm_encrypt | fn | Authenticated-encrypts Python aes_256_gcm_encrypt plaintext and associated data. |
__pyfunction_aes_256_gcm_encrypt | fn | |
aes_256_gcm_decrypt | fn | Authenticated-decrypts Python aes_256_gcm_decrypt ciphertext-and-tag bytes. |
__pyfunction_aes_256_gcm_decrypt | fn | |
scrypt_derive | fn | Derives Python scrypt_derive bytes from password, salt, and scrypt cost parameters. |
__pyfunction_scrypt_derive | fn | |
pq_factorize | fn | Factorizes Python pq_factorize(pq) into ordered nontrivial u64 factors. |
__pyfunction_pq_factorize | fn | |
sha1_digest_raw | fn | Computes SHA-1 for Rust callers without Python or GIL interaction. |
sha256_digest_raw | fn | Computes SHA-256 for Rust callers without Python or GIL interaction. |
mtproto_auth_key_id_raw | fn | Derives the trailing eight SHA-1 bytes that identify a validated MTProto authorization key. |
mtproto_message_key_raw | fn | Derives an MTProto 2.0 message key from a validated authorization key and padded plaintext. |
mtproto_derive_aes_key_iv_raw | fn | Derives the MTProto 2.0 AES-IGE key and IV from validated fixed-width key material. |
mtproto_encrypt_payload_raw | fn | Produces the auth-key identifier, message key, and AES-IGE ciphertext for padded plaintext. |
mtproto_decrypt_payload_raw | fn | Decrypts and authenticates an MTProto payload using validated directional key derivation. |
xor_bytes_raw | fn | Computes bytewise XOR for equal-length Rust byte slices. |
aes_256_ige_encrypt_raw | fn | Encrypts a block-aligned byte slice using AES-256 IGE for Rust callers. |
aes_256_ige_decrypt_raw | fn | Decrypts a block-aligned AES-256 IGE ciphertext for Rust callers. |
aes_256_cbc_encrypt_raw | fn | Encrypts a block-aligned byte slice with unpadded AES-256 CBC. |
aes_256_cbc_decrypt_raw | fn | Decrypts a block-aligned unpadded AES-256 CBC ciphertext. |
aes_256_ctr_crypt_raw | fn | XORs bytes with an AES-256 CTR keystream; the same operation encrypts and decrypts. |
aes_256_gcm_encrypt_raw | fn | Authenticated-encrypts plaintext with AES-256 GCM and returns ciphertext plus tag. |
aes_256_gcm_decrypt_raw | fn | Authenticated-decrypts AES-256 GCM ciphertext-and-tag data. |
scrypt_derive_raw | fn | Runs scrypt with validated protocol-level output limits for Rust callers. |
pq_factorize_raw | fn | Finds and orders the two nontrivial factors of an MTProto pq value. |
validate_auth_key | fn | Validates the fixed 256-byte MTProto authorization-key width. |
validate_msg_key | fn | Validates the fixed 16-byte MTProto message-key width. |
validate_block_multiple | fn | Validates that AES block-mode input has a whole-number count of AES blocks. |
validate_aes_key | fn | Validates a 32-byte AES-256 key and returns ValueError otherwise. |
validate_ige_iv | fn | Validates the two-block, 32-byte IV required by AES-IGE. |
validate_cbc_ctr_iv | fn | Validates the one-block IV or counter used by CBC and CTR modes. |
validate_gcm_nonce | fn | Validates the 12-byte nonce mandated by this AES-GCM interface. |
direction_offset | fn | Returns the MTProto key-schedule offset for the requested packet direction. |
xor_block | fn | Computes a fixed-width XOR block used by the AES block-mode loops. |
increment_counter | fn | Advances a big-endian AES-CTR counter in place, wrapping at the full block width. |
gcd | fn | Computes the greatest common divisor used by Pollard-rho factorization. |
mul_mod | fn | Multiplies modulo modulus with a widened intermediate to avoid u64 overflow. |
pow_mod | fn | Computes modular exponentiation for deterministic Miller-Rabin witnesses. |
is_prime_u64 | fn | Deterministically tests whether a u64 is prime using fixed Miller-Rabin witnesses. |
factor_u64 | fn | Returns one factor of a composite u64 using trial division and Pollard-rho iteration. |
AES_BLOCK_SIZE | const | AES’s fixed block size in bytes. |
MT_PROTO_AUTH_KEY_SIZE | const | Required byte length of an MTProto authorization key. |
MT_PROTO_MSG_KEY_SIZE | const | Required byte length of an MTProto 2.0 message key. |
GIL_RELEASE_THRESHOLD_BYTES | const | Work-size threshold above which native wrappers detach from the Python GIL. |
Functions
Section titled “Functions”detach_if_large
Section titled “detach_if_large”fn detach_if_large<T, F>(py: Python<'_>, work_bytes: usize, f: F) -> Twhere T: Ungil, F: Ungil + FnOnce() -> TDefined in rust/miniproto/src/crypto.rs:44-54
Runs f without the GIL when its input work estimate exceeds the native threshold.
work_bytes is an estimate used solely for GIL scheduling; f must satisfy PyO3’s Ungil
requirements. This helper preserves synchronous results and does not itself allocate.
Arguments
Section titled “Arguments”T: The GIL-independent result type returned byf.F: The one-shot GIL-independent closure performing the native operation.py: The acquired GIL token that can detachf.work_bytes: Conservative byte-work estimate compared with the detach threshold.f: The native computation to run attached or detached.
register
Section titled “register”fn register(m: &Bound<'_, pyo3::types::PyModule>) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:63-83
Registers this module’s fallback-compatible Python callables on miniproto._native.
Returns a PyO3 exception if a callable cannot be added to m.
Arguments
Section titled “Arguments”m: The Python extension module receiving the crypto callables.
native_available
Section titled “native_available”fn native_available() -> boolDefined in rust/miniproto/src/crypto.rs:88-90
Implements Python native_available, which always returns true while this compiled callable
is importable. Python fallback selection happens before this function can be called.
__pyfunction_native_available
Section titled “__pyfunction_native_available”unsafe fn __pyfunction_native_available<'py>(py: ::pyo3::Python<'py>, _slf: *mut ::pyo3::ffi::PyObject) -> ::pyo3::PyResult<*mut ::pyo3::ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:87
sha1_digest
Section titled “sha1_digest”fn sha1_digest(py: Python<'_>, data: Vec<u8>) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:101-103
Computes Python sha1_digest(data) and returns the 20-byte SHA-1 digest.
Large inputs are hashed with the GIL released.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach a large hash operation.data: Bytes to hash.
__pyfunction_sha1_digest
Section titled “__pyfunction_sha1_digest”unsafe fn __pyfunction_sha1_digest<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:100
sha256_digest
Section titled “sha256_digest”fn sha256_digest(py: Python<'_>, data: Vec<u8>) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:114-116
Computes Python sha256_digest(data) and returns the 32-byte SHA-256 digest.
Large inputs are hashed with the GIL released.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach a large hash operation.data: Bytes to hash.
__pyfunction_sha256_digest
Section titled “__pyfunction_sha256_digest”unsafe fn __pyfunction_sha256_digest<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:113
mtproto_auth_key_id
Section titled “mtproto_auth_key_id”fn mtproto_auth_key_id(auth_key: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:126-128
Computes Python mtproto_auth_key_id(auth_key) from a validated 256-byte key.
Returns ValueError when auth_key has the wrong length.
Arguments
Section titled “Arguments”auth_key: The 256-byte MTProto authorization key.
__pyfunction_mtproto_auth_key_id
Section titled “__pyfunction_mtproto_auth_key_id”unsafe fn __pyfunction_mtproto_auth_key_id<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:125
mtproto_message_key
Section titled “mtproto_message_key”fn mtproto_message_key(py: Python<'_>, auth_key: Vec<u8>, plaintext_with_padding: Vec<u8>, client_to_server: bool) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:142-157
Computes Python mtproto_message_key for padded plaintext and one MTProto direction.
Returns a 16-byte message key; rejects an invalid authorization key and releases the GIL for large inputs.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach a large hash operation.auth_key: The 256-byte MTProto authorization key.plaintext_with_padding: Block-aligned inner plaintext that contributes to the message key.client_to_server: Selects the directional MTProto key offset.
__pyfunction_mtproto_message_key
Section titled “__pyfunction_mtproto_message_key”unsafe fn __pyfunction_mtproto_message_key<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:141
mtproto_derive_aes_key_iv
Section titled “mtproto_derive_aes_key_iv”fn mtproto_derive_aes_key_iv(py: Python<'_>, auth_key: Vec<u8>, msg_key: Vec<u8>, client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>)>Defined in rust/miniproto/src/crypto.rs:172-188
Derives the AES-256 key and IV used by Python mtproto_derive_aes_key_iv.
Returns (key, iv) and validates both fixed-width inputs. Valid inputs total 272 bytes
(auth_key 256 plus msg_key 16), so this wrapper never exceeds the detach threshold and
performs the derivation while holding the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token retained by this bounded-size operation.auth_key: The 256-byte MTProto authorization key.msg_key: The 16-byte MTProto message key.client_to_server: Selects the directional MTProto key offset.
__pyfunction_mtproto_derive_aes_key_iv
Section titled “__pyfunction_mtproto_derive_aes_key_iv”unsafe fn __pyfunction_mtproto_derive_aes_key_iv<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:171
mtproto_encrypt_payload
Section titled “mtproto_encrypt_payload”fn mtproto_encrypt_payload(py: Python<'_>, auth_key: Vec<u8>, plaintext_with_padding: Vec<u8>, client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>, Vec<u8>)>Defined in rust/miniproto/src/crypto.rs:202-212
Encrypts padded MTProto plaintext for Python mtproto_encrypt_payload.
Returns (auth_key_id, msg_key, ciphertext) or ValueError for invalid key or block input;
large work runs without the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large encryption work.auth_key: The 256-byte MTProto authorization key.plaintext_with_padding: AES-block-aligned inner plaintext to encrypt.client_to_server: Selects the directional MTProto key schedule.
__pyfunction_mtproto_encrypt_payload
Section titled “__pyfunction_mtproto_encrypt_payload”unsafe fn __pyfunction_mtproto_encrypt_payload<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:201
mtproto_decrypt_payload
Section titled “mtproto_decrypt_payload”fn mtproto_decrypt_payload(py: Python<'_>, auth_key: Vec<u8>, msg_key: Vec<u8>, ciphertext: Vec<u8>, client_to_server: bool) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:227-238
Decrypts and verifies Python mtproto_decrypt_payload ciphertext.
Returns padded plaintext or ValueError for invalid lengths, key material, or message-key
verification; large work runs without the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large decryption work.auth_key: The 256-byte MTProto authorization key.msg_key: The 16-byte message key to verify.ciphertext: AES-IGE ciphertext whose length must be an AES-block multiple.client_to_server: Selects the directional MTProto key schedule.
__pyfunction_mtproto_decrypt_payload
Section titled “__pyfunction_mtproto_decrypt_payload”unsafe fn __pyfunction_mtproto_decrypt_payload<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:226
xor_bytes
Section titled “xor_bytes”fn xor_bytes(left: &[u8], right: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:249-251
Returns the bytewise exclusive-or of Python xor_bytes(left, right) inputs.
Returns ValueError unless both inputs have equal length.
Arguments
Section titled “Arguments”left: First equal-length byte sequence.right: Second equal-length byte sequence.
__pyfunction_xor_bytes
Section titled “__pyfunction_xor_bytes”unsafe fn __pyfunction_xor_bytes<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:248
aes_256_ige_encrypt
Section titled “aes_256_ige_encrypt”fn aes_256_ige_encrypt(py: Python<'_>, plaintext: Vec<u8>, key: Vec<u8>, iv: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:265-275
Encrypts block-aligned bytes with Python aes_256_ige_encrypt.
Requires a 32-byte key and IV; returns ValueError for invalid lengths and releases the GIL
for large plaintexts.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large encryption work.plaintext: AES-block-aligned bytes to encrypt.key: The 32-byte AES-256 key.iv: The 32-byte AES-IGE initialization vector.
__pyfunction_aes_256_ige_encrypt
Section titled “__pyfunction_aes_256_ige_encrypt”unsafe fn __pyfunction_aes_256_ige_encrypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:264
aes_256_ige_decrypt
Section titled “aes_256_ige_decrypt”fn aes_256_ige_decrypt(py: Python<'_>, ciphertext: Vec<u8>, key: Vec<u8>, iv: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:289-299
Decrypts block-aligned bytes with Python aes_256_ige_decrypt.
Requires a 32-byte key and IV; returns ValueError for invalid lengths and releases the GIL
for large ciphertexts.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large decryption work.ciphertext: AES-block-aligned bytes to decrypt.key: The 32-byte AES-256 key.iv: The 32-byte AES-IGE initialization vector.
__pyfunction_aes_256_ige_decrypt
Section titled “__pyfunction_aes_256_ige_decrypt”unsafe fn __pyfunction_aes_256_ige_decrypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:288
aes_256_cbc_encrypt
Section titled “aes_256_cbc_encrypt”fn aes_256_cbc_encrypt(py: Python<'_>, plaintext: Vec<u8>, key: Vec<u8>, iv: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:313-323
Encrypts block-aligned bytes with Python aes_256_cbc_encrypt without padding.
Requires 32-byte key and 16-byte IV inputs; invalid lengths become ValueError and large
plaintexts release the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large encryption work.plaintext: AES-block-aligned bytes to encrypt.key: The 32-byte AES-256 key.iv: The 16-byte CBC initialization vector.
__pyfunction_aes_256_cbc_encrypt
Section titled “__pyfunction_aes_256_cbc_encrypt”unsafe fn __pyfunction_aes_256_cbc_encrypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:312
aes_256_cbc_decrypt
Section titled “aes_256_cbc_decrypt”fn aes_256_cbc_decrypt(py: Python<'_>, ciphertext: Vec<u8>, key: Vec<u8>, iv: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:337-347
Decrypts block-aligned bytes with Python aes_256_cbc_decrypt without padding.
Requires 32-byte key and 16-byte IV inputs; invalid lengths become ValueError and large
ciphertexts release the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large decryption work.ciphertext: AES-block-aligned bytes to decrypt.key: The 32-byte AES-256 key.iv: The 16-byte CBC initialization vector.
__pyfunction_aes_256_cbc_decrypt
Section titled “__pyfunction_aes_256_cbc_decrypt”unsafe fn __pyfunction_aes_256_cbc_decrypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:336
aes_256_ctr_crypt
Section titled “aes_256_ctr_crypt”fn aes_256_ctr_crypt(py: Python<'_>, data: Vec<u8>, key: Vec<u8>, iv: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:361-371
Applies Python aes_256_ctr_crypt to data using AES-CTR keystream XOR.
The same routine encrypts and decrypts; it validates the 32-byte key and 16-byte counter IV, and releases the GIL for large data.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large counter-mode work.data: Bytes to encrypt or decrypt.key: The 32-byte AES-256 key.iv: The 16-byte initial counter block.
__pyfunction_aes_256_ctr_crypt
Section titled “__pyfunction_aes_256_ctr_crypt”unsafe fn __pyfunction_aes_256_ctr_crypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:360
aes_256_gcm_encrypt
Section titled “aes_256_gcm_encrypt”fn aes_256_gcm_encrypt(py: Python<'_>, plaintext: Vec<u8>, key: Vec<u8>, nonce: Vec<u8>, associated_data: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:386-397
Authenticated-encrypts Python aes_256_gcm_encrypt plaintext and associated data.
Returns ciphertext followed by its GCM tag, or ValueError for a bad 32-byte key, 12-byte
nonce, or encryption failure; large work releases the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large authenticated-encryption work.plaintext: Bytes to encrypt.key: The 32-byte AES-256 key.nonce: The 12-byte GCM nonce.associated_data: Authenticated bytes that are not encrypted.
__pyfunction_aes_256_gcm_encrypt
Section titled “__pyfunction_aes_256_gcm_encrypt”unsafe fn __pyfunction_aes_256_gcm_encrypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:385
aes_256_gcm_decrypt
Section titled “aes_256_gcm_decrypt”fn aes_256_gcm_decrypt(py: Python<'_>, ciphertext_and_tag: Vec<u8>, key: Vec<u8>, nonce: Vec<u8>, associated_data: Vec<u8>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:412-423
Authenticated-decrypts Python aes_256_gcm_decrypt ciphertext-and-tag bytes.
Returns plaintext or ValueError for invalid key/nonce material or authentication failure;
large work releases the GIL.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach large authenticated-decryption work.ciphertext_and_tag: GCM ciphertext followed by its authentication tag.key: The 32-byte AES-256 key.nonce: The 12-byte GCM nonce.associated_data: Authenticated bytes that are not encrypted.
__pyfunction_aes_256_gcm_decrypt
Section titled “__pyfunction_aes_256_gcm_decrypt”unsafe fn __pyfunction_aes_256_gcm_decrypt<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:411
scrypt_derive
Section titled “scrypt_derive”fn scrypt_derive(py: Python<'_>, password: Vec<u8>, salt: Vec<u8>, n: u32, r: u32, p: u32, length: usize) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:441-456
Derives Python scrypt_derive bytes from password, salt, and scrypt cost parameters.
n must be a power of two above one and length is limited to 1..=1024; invalid parameters
return ValueError. The GIL is released only when n * r > 4096, the exact work_bytes
condition supplied to detach_if_large; it remains held at or below that threshold.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach only sufficiently large scrypt work.password: Password bytes accepted by scrypt.salt: Salt bytes accepted by scrypt.n: CPU/memory cost, required to be a power of two greater than one.r: scrypt block-size cost parameter.p: scrypt parallelization cost parameter.length: Requested derived-key length in the inclusive range 1..=1024.
__pyfunction_scrypt_derive
Section titled “__pyfunction_scrypt_derive”unsafe fn __pyfunction_scrypt_derive<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:440
pq_factorize
Section titled “pq_factorize”fn pq_factorize(py: Python<'_>, pq: u64) -> PyResult<(u64, u64)>Defined in rust/miniproto/src/crypto.rs:468-470
Factorizes Python pq_factorize(pq) into ordered nontrivial u64 factors.
Returns ValueError for non-composite values and runs the potentially expensive search with
the GIL released.
Arguments
Section titled “Arguments”py: The acquired GIL token used to detach the factorization search.pq: Composite MTProto handshake value to split into ordered factors.
__pyfunction_pq_factorize
Section titled “__pyfunction_pq_factorize”unsafe fn __pyfunction_pq_factorize<'py>(py: Python<'py>, _slf: *mut ffi::PyObject, _args: *const *mut ffi::PyObject, _nargs: ffi::Py_ssize_t, _kwargs: *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>Defined in rust/miniproto/src/crypto.rs:467
sha1_digest_raw
Section titled “sha1_digest_raw”fn sha1_digest_raw(data: &[u8]) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:477-479
Computes SHA-1 for Rust callers without Python or GIL interaction.
Arguments
Section titled “Arguments”data: Bytes to hash.
sha256_digest_raw
Section titled “sha256_digest_raw”fn sha256_digest_raw(data: &[u8]) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:486-488
Computes SHA-256 for Rust callers without Python or GIL interaction.
Arguments
Section titled “Arguments”data: Bytes to hash.
mtproto_auth_key_id_raw
Section titled “mtproto_auth_key_id_raw”fn mtproto_auth_key_id_raw(auth_key: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:497-501
Derives the trailing eight SHA-1 bytes that identify a validated MTProto authorization key.
Returns ValueError if the key is not exactly 256 bytes; does not acquire the GIL.
Arguments
Section titled “Arguments”auth_key: Authorization key whose SHA-1 tail supplies the identifier.
mtproto_message_key_raw
Section titled “mtproto_message_key_raw”fn mtproto_message_key_raw(auth_key: &[u8], plaintext_with_padding: &[u8], client_to_server: bool) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:513-524
Derives an MTProto 2.0 message key from a validated authorization key and padded plaintext.
Callers must validate the authorization-key length before use because this internal primitive slices its fixed protocol ranges directly.
Arguments
Section titled “Arguments”auth_key: Previously validated 256-byte authorization key.plaintext_with_padding: Padded inner plaintext to include in the SHA-256 derivation.client_to_server: Selects the directional key offset.
mtproto_derive_aes_key_iv_raw
Section titled “mtproto_derive_aes_key_iv_raw”fn mtproto_derive_aes_key_iv_raw(auth_key: &[u8], msg_key: &[u8], client_to_server: bool) -> (Vec<u8>, Vec<u8>)Defined in rust/miniproto/src/crypto.rs:535-561
Derives the MTProto 2.0 AES-IGE key and IV from validated fixed-width key material.
Callers must validate auth_key and msg_key before calling; the function has no GIL use.
Arguments
Section titled “Arguments”auth_key: Previously validated 256-byte authorization key.msg_key: Previously validated 16-byte message key.client_to_server: Selects the directional key offset.
mtproto_encrypt_payload_raw
Section titled “mtproto_encrypt_payload_raw”fn mtproto_encrypt_payload_raw(auth_key: &[u8], plaintext_with_padding: &[u8], client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>, Vec<u8>)>Defined in rust/miniproto/src/crypto.rs:572-584
Produces the auth-key identifier, message key, and AES-IGE ciphertext for padded plaintext.
Returns ValueError for malformed keys or non-block-aligned plaintext; no GIL interaction.
Arguments
Section titled “Arguments”auth_key: 256-byte authorization key used for id, message key, and AES derivation.plaintext_with_padding: AES-block-aligned inner plaintext to encrypt.client_to_server: Selects the directional MTProto key schedule.
mtproto_decrypt_payload_raw
Section titled “mtproto_decrypt_payload_raw”fn mtproto_decrypt_payload_raw(auth_key: &[u8], msg_key: &[u8], ciphertext: &[u8], client_to_server: bool) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:596-613
Decrypts and authenticates an MTProto payload using validated directional key derivation.
Returns ValueError for malformed inputs or a message-key mismatch; no GIL interaction.
Arguments
Section titled “Arguments”auth_key: 256-byte authorization key used for directional AES derivation.msg_key: 16-byte message key expected after decryption.ciphertext: AES-block-aligned encrypted payload.client_to_server: Selects the directional MTProto key schedule.
xor_bytes_raw
Section titled “xor_bytes_raw”fn xor_bytes_raw(left: &[u8], right: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:623-630
Computes bytewise XOR for equal-length Rust byte slices.
Returns ValueError for unequal lengths and does not use the GIL.
Arguments
Section titled “Arguments”left: First equal-length byte slice.right: Second equal-length byte slice.
aes_256_ige_encrypt_raw
Section titled “aes_256_ige_encrypt_raw”fn aes_256_ige_encrypt_raw(plaintext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:641-668
Encrypts a block-aligned byte slice using AES-256 IGE for Rust callers.
Returns ValueError unless the key, IV, and plaintext lengths meet AES-IGE requirements.
Arguments
Section titled “Arguments”plaintext: AES-block-aligned bytes to encrypt.key: The 32-byte AES-256 key.iv: The 32-byte AES-IGE initialization vector.
aes_256_ige_decrypt_raw
Section titled “aes_256_ige_decrypt_raw”fn aes_256_ige_decrypt_raw(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:679-706
Decrypts a block-aligned AES-256 IGE ciphertext for Rust callers.
Returns ValueError unless the key, IV, and ciphertext lengths meet AES-IGE requirements.
Arguments
Section titled “Arguments”ciphertext: AES-block-aligned bytes to decrypt.key: The 32-byte AES-256 key.iv: The 32-byte AES-IGE initialization vector.
aes_256_cbc_encrypt_raw
Section titled “aes_256_cbc_encrypt_raw”fn aes_256_cbc_encrypt_raw(plaintext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:717-737
Encrypts a block-aligned byte slice with unpadded AES-256 CBC.
Returns ValueError unless the key, IV, and plaintext lengths are valid.
Arguments
Section titled “Arguments”plaintext: AES-block-aligned bytes to encrypt.key: The 32-byte AES-256 key.iv: The 16-byte CBC initialization vector.
aes_256_cbc_decrypt_raw
Section titled “aes_256_cbc_decrypt_raw”fn aes_256_cbc_decrypt_raw(ciphertext: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:748-769
Decrypts a block-aligned unpadded AES-256 CBC ciphertext.
Returns ValueError unless the key, IV, and ciphertext lengths are valid.
Arguments
Section titled “Arguments”ciphertext: AES-block-aligned bytes to decrypt.key: The 32-byte AES-256 key.iv: The 16-byte CBC initialization vector.
aes_256_ctr_crypt_raw
Section titled “aes_256_ctr_crypt_raw”fn aes_256_ctr_crypt_raw(data: &[u8], key: &[u8], iv: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:780-794
XORs bytes with an AES-256 CTR keystream; the same operation encrypts and decrypts.
Returns ValueError unless key is 32 bytes and iv is one AES block.
Arguments
Section titled “Arguments”data: Bytes to XOR with the generated CTR keystream.key: The 32-byte AES-256 key.iv: The 16-byte initial counter block.
aes_256_gcm_encrypt_raw
Section titled “aes_256_gcm_encrypt_raw”fn aes_256_gcm_encrypt_raw(plaintext: &[u8], key: &[u8], nonce: &[u8], associated_data: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:806-827
Authenticated-encrypts plaintext with AES-256 GCM and returns ciphertext plus tag.
Returns ValueError for an invalid key or nonce or if the cipher rejects the operation.
Arguments
Section titled “Arguments”plaintext: Bytes to encrypt.key: The 32-byte AES-256 key.nonce: The 12-byte GCM nonce.associated_data: Authenticated bytes that are not encrypted.
aes_256_gcm_decrypt_raw
Section titled “aes_256_gcm_decrypt_raw”fn aes_256_gcm_decrypt_raw(ciphertext_and_tag: &[u8], key: &[u8], nonce: &[u8], associated_data: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:839-860
Authenticated-decrypts AES-256 GCM ciphertext-and-tag data.
Returns ValueError for invalid key/nonce material or a failed authentication check.
Arguments
Section titled “Arguments”ciphertext_and_tag: GCM ciphertext followed by its authentication tag.key: The 32-byte AES-256 key.nonce: The 12-byte GCM nonce.associated_data: Authenticated bytes that are not encrypted.
scrypt_derive_raw
Section titled “scrypt_derive_raw”fn scrypt_derive_raw(password: &[u8], salt: &[u8], n: u32, r: u32, p: u32, length: usize) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:875-901
Runs scrypt with validated protocol-level output limits for Rust callers.
Returns ValueError for invalid cost parameters, an unsupported output length, or derivation
failure. This is a synchronous, GIL-free primitive.
Arguments
Section titled “Arguments”password: Password bytes accepted by scrypt.salt: Salt bytes accepted by scrypt.n: CPU/memory cost, required to be a power of two greater than one.r: scrypt block-size cost parameter.p: scrypt parallelization cost parameter.length: Requested derived-key length in the inclusive range 1..=1024.
pq_factorize_raw
Section titled “pq_factorize_raw”fn pq_factorize_raw(pq: u64) -> PyResult<(u64, u64)>Defined in rust/miniproto/src/crypto.rs:910-924
Finds and orders the two nontrivial factors of an MTProto pq value.
Returns ValueError for values below four or values that are not composite.
Arguments
Section titled “Arguments”pq: Candidate composite integer to split into its two ordered factors.
validate_auth_key
Section titled “validate_auth_key”fn validate_auth_key(auth_key: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:933-938
Validates the fixed 256-byte MTProto authorization-key width.
Returns ValueError rather than allowing fixed-offset protocol code to panic.
Arguments
Section titled “Arguments”auth_key: Candidate authorization key expected to contain 256 bytes.
validate_msg_key
Section titled “validate_msg_key”fn validate_msg_key(msg_key: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:947-952
Validates the fixed 16-byte MTProto message-key width.
Returns ValueError on a malformed input slice.
Arguments
Section titled “Arguments”msg_key: Candidate message key expected to contain 16 bytes.
validate_block_multiple
Section titled “validate_block_multiple”fn validate_block_multiple(data: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:961-968
Validates that AES block-mode input has a whole-number count of AES blocks.
Returns ValueError when the byte length is not divisible by 16.
Arguments
Section titled “Arguments”data: Candidate AES block-mode input.
validate_aes_key
Section titled “validate_aes_key”fn validate_aes_key(key: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:975-980
Validates a 32-byte AES-256 key and returns ValueError otherwise.
Arguments
Section titled “Arguments”key: Candidate AES-256 key.
validate_ige_iv
Section titled “validate_ige_iv”fn validate_ige_iv(iv: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:987-992
Validates the two-block, 32-byte IV required by AES-IGE.
Arguments
Section titled “Arguments”iv: Candidate AES-IGE initialization vector.
validate_cbc_ctr_iv
Section titled “validate_cbc_ctr_iv”fn validate_cbc_ctr_iv(iv: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:999-1004
Validates the one-block IV or counter used by CBC and CTR modes.
Arguments
Section titled “Arguments”iv: Candidate CBC initialization vector or CTR counter block.
validate_gcm_nonce
Section titled “validate_gcm_nonce”fn validate_gcm_nonce(nonce: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:1011-1016
Validates the 12-byte nonce mandated by this AES-GCM interface.
Arguments
Section titled “Arguments”nonce: Candidate AES-GCM nonce.
direction_offset
Section titled “direction_offset”fn direction_offset(client_to_server: bool) -> usizeDefined in rust/miniproto/src/crypto.rs:1023-1025
Returns the MTProto key-schedule offset for the requested packet direction.
Arguments
Section titled “Arguments”client_to_server: Whether the packet travels from client to server.
xor_block
Section titled “xor_block”fn xor_block(left: &[u8; 16], right: &[u8; 16]) -> [u8; 16]Defined in rust/miniproto/src/crypto.rs:1033-1039
Computes a fixed-width XOR block used by the AES block-mode loops.
Arguments
Section titled “Arguments”left: First AES-sized block.right: Second AES-sized block.
increment_counter
Section titled “increment_counter”fn increment_counter(counter: &mut [u8; 16])Defined in rust/miniproto/src/crypto.rs:1046-1054
Advances a big-endian AES-CTR counter in place, wrapping at the full block width.
Arguments
Section titled “Arguments”counter: Mutable AES-sized counter block to increment.
fn gcd(left: u64, right: u64) -> u64Defined in rust/miniproto/src/crypto.rs:1062-1069
Computes the greatest common divisor used by Pollard-rho factorization.
Arguments
Section titled “Arguments”left: First nonnegative integer.right: Second nonnegative integer.
mul_mod
Section titled “mul_mod”fn mul_mod(left: u64, right: u64, modulus: u64) -> u64Defined in rust/miniproto/src/crypto.rs:1083-1085
Multiplies modulo modulus with a widened intermediate to avoid u64 overflow.
Panics
Section titled “Panics”Panics if modulus is zero because the remainder operation is undefined. Callers must also
pass a nonzero modulus; u128 widening prevents multiplication overflow for all u64 inputs.
Arguments
Section titled “Arguments”left: First modular multiplicand.right: Second modular multiplicand.modulus: Nonzero modulus used for the reduced product.
pow_mod
Section titled “pow_mod”fn pow_mod(base: u64, exponent: u64, modulus: u64) -> u64Defined in rust/miniproto/src/crypto.rs:1099-1109
Computes modular exponentiation for deterministic Miller-Rabin witnesses.
Panics
Section titled “Panics”Panics if modulus is zero because both reduction paths use remainder operations. Arithmetic
overflow is avoided by delegating products to mul_mod with its widened intermediate.
Arguments
Section titled “Arguments”base: Base to exponentiate modulomodulus.exponent: Nonnegative exponent encoded asu64.modulus: Nonzero modulus used for every reduction.
is_prime_u64
Section titled “is_prime_u64”fn is_prime_u64(value: u64) -> boolDefined in rust/miniproto/src/crypto.rs:1116-1155
Deterministically tests whether a u64 is prime using fixed Miller-Rabin witnesses.
Arguments
Section titled “Arguments”value: Unsigned integer to classify as prime or composite.
factor_u64
Section titled “factor_u64”fn factor_u64(value: u64) -> u64Defined in rust/miniproto/src/crypto.rs:1162-1190
Returns one factor of a composite u64 using trial division and Pollard-rho iteration.
Arguments
Section titled “Arguments”value: Composite unsigned integer whose nontrivial factor is requested.
Constants
Section titled “Constants”AES_BLOCK_SIZE
Section titled “AES_BLOCK_SIZE”const AES_BLOCK_SIZE: usize = 16usize;Defined in rust/miniproto/src/crypto.rs:24
AES’s fixed block size in bytes.
MT_PROTO_AUTH_KEY_SIZE
Section titled “MT_PROTO_AUTH_KEY_SIZE”const MT_PROTO_AUTH_KEY_SIZE: usize = 256usize;Defined in rust/miniproto/src/crypto.rs:26
Required byte length of an MTProto authorization key.
MT_PROTO_MSG_KEY_SIZE
Section titled “MT_PROTO_MSG_KEY_SIZE”const MT_PROTO_MSG_KEY_SIZE: usize = 16usize;Defined in rust/miniproto/src/crypto.rs:28
Required byte length of an MTProto 2.0 message key.
GIL_RELEASE_THRESHOLD_BYTES
Section titled “GIL_RELEASE_THRESHOLD_BYTES”const GIL_RELEASE_THRESHOLD_BYTES: usize = 4_096usize;Defined in rust/miniproto/src/crypto.rs:30
Work-size threshold above which native wrappers detach from the Python GIL.