miniproto_native::crypto
Provenance
Section titled “Provenance”- 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_cbc_decryptminiproto_native::crypto::aes_256_cbc_encryptminiproto_native::crypto::aes_256_ctr_cryptminiproto_native::crypto::aes_256_gcm_decryptminiproto_native::crypto::aes_256_gcm_encryptminiproto_native::crypto::aes_256_ige_decryptminiproto_native::crypto::aes_256_ige_encryptminiproto_native::crypto::mtproto_auth_key_idminiproto_native::crypto::mtproto_decrypt_payloadminiproto_native::crypto::mtproto_derive_aes_key_ivminiproto_native::crypto::mtproto_encrypt_payloadminiproto_native::crypto::mtproto_message_keyminiproto_native::crypto::native_availableminiproto_native::crypto::pq_factorizeminiproto_native::crypto::registerminiproto_native::crypto::scrypt_deriveminiproto_native::crypto::sha1_digestminiproto_native::crypto::sha256_digestminiproto_native::crypto::xor_bytes
miniproto_native / 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. Selected wrappers detach for large workloads, while *_raw functions operate without Python objects. PyO3 conversion errors propagate unchanged, and algorithm or byte-shape validation returns ValueError. Python wrappers may select a fallback even when these native exports are available.
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:50-60
Runs f without the GIL when its input work estimate exceeds the native threshold.
work_bytes controls thread detachment, and f must satisfy PyO3’s Ungil requirements. Detachment releases the GIL on GIL-enabled builds. The computation remains synchronous.
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:69-89
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:94-96
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:93
sha1_digest
Section titled “sha1_digest”fn sha1_digest(py: Python<'_>, data: std::borrow::Cow<'_, [u8]>) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:107-109
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:106
sha256_digest
Section titled “sha256_digest”fn sha256_digest(py: Python<'_>, data: std::borrow::Cow<'_, [u8]>) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:120-122
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:119
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:132-134
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:131
mtproto_message_key
Section titled “mtproto_message_key”fn mtproto_message_key(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, plaintext_with_padding: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:148-163
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: Inner plaintext including padding, with no alignment or padding validation hereclient_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:147
mtproto_derive_aes_key_iv
Section titled “mtproto_derive_aes_key_iv”fn mtproto_derive_aes_key_iv(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, msg_key: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>)>Defined in rust/miniproto/src/crypto.rs:176-192
Derives the AES-256 key and IV used by Python mtproto_derive_aes_key_iv.
Returns (key, iv) after validating both inputs. Valid inputs total 272 bytes (auth_key 256 plus msg_key 16), so successful derivations run below the detach threshold.
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:175
mtproto_encrypt_payload
Section titled “mtproto_encrypt_payload”fn mtproto_encrypt_payload(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, plaintext_with_padding: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<(Vec<u8>, Vec<u8>, Vec<u8>)>Defined in rust/miniproto/src/crypto.rs:206-216
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:205
mtproto_decrypt_payload
Section titled “mtproto_decrypt_payload”fn mtproto_decrypt_payload(py: Python<'_>, auth_key: std::borrow::Cow<'_, [u8]>, msg_key: std::borrow::Cow<'_, [u8]>, ciphertext: std::borrow::Cow<'_, [u8]>, client_to_server: bool) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:231-242
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:230
xor_bytes
Section titled “xor_bytes”fn xor_bytes(left: &[u8], right: &[u8]) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:253-255
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:252
aes_256_ige_encrypt
Section titled “aes_256_ige_encrypt”fn aes_256_ige_encrypt(py: Python<'_>, plaintext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:269-279
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:268
aes_256_ige_decrypt
Section titled “aes_256_ige_decrypt”fn aes_256_ige_decrypt(py: Python<'_>, ciphertext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:293-303
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:292
aes_256_cbc_encrypt
Section titled “aes_256_cbc_encrypt”fn aes_256_cbc_encrypt(py: Python<'_>, plaintext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:317-327
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:316
aes_256_cbc_decrypt
Section titled “aes_256_cbc_decrypt”fn aes_256_cbc_decrypt(py: Python<'_>, ciphertext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:341-351
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:340
aes_256_ctr_crypt
Section titled “aes_256_ctr_crypt”fn aes_256_ctr_crypt(py: Python<'_>, data: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, iv: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:365-375
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:364
aes_256_gcm_encrypt
Section titled “aes_256_gcm_encrypt”fn aes_256_gcm_encrypt(py: Python<'_>, plaintext: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, nonce: std::borrow::Cow<'_, [u8]>, associated_data: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:390-401
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:389
aes_256_gcm_decrypt
Section titled “aes_256_gcm_decrypt”fn aes_256_gcm_decrypt(py: Python<'_>, ciphertext_and_tag: std::borrow::Cow<'_, [u8]>, key: std::borrow::Cow<'_, [u8]>, nonce: std::borrow::Cow<'_, [u8]>, associated_data: std::borrow::Cow<'_, [u8]>) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:416-427
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:415
scrypt_derive
Section titled “scrypt_derive”fn scrypt_derive(py: Python<'_>, password: std::borrow::Cow<'_, [u8]>, salt: std::borrow::Cow<'_, [u8]>, n: u32, r: u32, p: u32, length: usize) -> PyResult<Vec<u8>>Defined in rust/miniproto/src/crypto.rs:445-462
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. Estimated memory is capped at 256 MiB and aggregate work at 1 GiB. The
GIL is released when the 128 * n * r * p work estimate exceeds 4 KiB.
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:444
pq_factorize
Section titled “pq_factorize”fn pq_factorize(py: Python<'_>, pq: u64) -> PyResult<(u64, u64)>Defined in rust/miniproto/src/crypto.rs:474-476
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:473
sha1_digest_raw
Section titled “sha1_digest_raw”fn sha1_digest_raw(data: &[u8]) -> Vec<u8>Defined in rust/miniproto/src/crypto.rs:483-485
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:492-494
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:503-507
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:519-526
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_message_key_large_raw
Section titled “mtproto_message_key_large_raw”fn mtproto_message_key_large_raw(auth_key: &[u8], plaintext_with_padding: &[u8], client_to_server: bool) -> [u8; 32]Defined in rust/miniproto/src/crypto.rs:535-545
Computes the directional digest shared by the message key and quick acknowledgement.
Arguments
Section titled “Arguments”auth_key: Previously validated 256-byte authorization key.plaintext_with_padding: Entire unencrypted MTProto body including padding.client_to_server: Selects the sender’s 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:556-582
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:593-605
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:617-634
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:644-651
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:662-670
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_encrypt_in_place
Section titled “aes_256_ige_encrypt_in_place”fn aes_256_ige_encrypt_in_place(data: &mut [u8], key: &[u8], iv: &[u8]) -> PyResult<()>Defined in rust/miniproto/src/crypto.rs:679-701
Encrypts the padded payload in its final MTProto packet buffer.
Arguments
Section titled “Arguments”data: Block-aligned payload replaced in place with ciphertext.key: The 32-byte AES-256 key.iv: The 32-byte 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:712-738
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:749-768
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:779-805
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:816-838
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:850-871
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:883-904
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:919-955
Runs scrypt with the library’s output and resource 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:964-979
Finds and orders the two nontrivial factors of an MTProto pq value.
Returns ValueError for values below four, primes or an exhausted factorization budget
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:988-993
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:1002-1007
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:1016-1023
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:1030-1035
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:1042-1047
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:1054-1059
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:1066-1071
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:1078-1080
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:1088-1094
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.
fn gcd(left: u64, right: u64) -> u64Defined in rust/miniproto/src/crypto.rs:1102-1109
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:1122-1124
Multiplies modulo modulus with a widened intermediate to avoid u64 overflow.
Panics
Section titled “Panics”A zero modulus panics at the remainder operation. Widening the product to u128 prevents multiplication overflow for 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:1137-1147
Computes modular exponentiation for deterministic Miller-Rabin witnesses.
Panics
Section titled “Panics”A zero modulus panics when exponent is nonzero. Products use mul_mod and its widened intermediate to avoid multiplication overflow.
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:1154-1193
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, attempts: usize, iterations: usize) -> Option<u64>Defined in rust/miniproto/src/crypto.rs:1202-1229
Returns one factor using bounded Pollard-rho iteration, the input when prime or None when exhausted
Arguments
Section titled “Arguments”value: Composite unsigned integer whose nontrivial factor is requested.attempts: Maximum number of polynomial constants to tryiterations: Maximum sequence iterations for each constant
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:23
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:25
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:27
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:29
Work-size threshold above which native wrappers detach from the Python GIL.
SCRYPT_MAX_MEMORY_BYTES
Section titled “SCRYPT_MAX_MEMORY_BYTES”const SCRYPT_MAX_MEMORY_BYTES: usize = 268_435_456usize;Defined in rust/miniproto/src/crypto.rs:31
Maximum memory estimate accepted by the public scrypt primitive.
SCRYPT_MAX_WORK_BYTES
Section titled “SCRYPT_MAX_WORK_BYTES”const SCRYPT_MAX_WORK_BYTES: usize = 1_073_741_824usize;Defined in rust/miniproto/src/crypto.rs:33
Maximum CPU-work estimate accepted by the public scrypt primitive.
PQ_FACTOR_ATTEMPTS
Section titled “PQ_FACTOR_ATTEMPTS”const PQ_FACTOR_ATTEMPTS: usize = 16usize;Defined in rust/miniproto/src/crypto.rs:35
Maximum Pollard-rho retries, matching the Python fallback
PQ_FACTOR_ITERATIONS
Section titled “PQ_FACTOR_ITERATIONS”const PQ_FACTOR_ITERATIONS: usize = 100_000usize;Defined in rust/miniproto/src/crypto.rs:37
Maximum Pollard-rho iterations per retry, matching the Python fallback