Skip to content

miniproto_native::crypto

  • Crate: miniproto_native
  • Rust visibility: crate
  • Source: rust/miniproto/src/crypto.rs
  • Python exposure: Not evidenced by static PyO3 attributes.

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. 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.

ItemKindDescription
detach_if_largefnRuns f without the GIL when its input work estimate exceeds the native threshold.
registerfnRegisters this module’s fallback-compatible Python callables on miniproto._native.
native_availablefnImplements Python native_available, which always returns true while this compiled callable is importable.
__pyfunction_native_availablefn
sha1_digestfnComputes Python sha1_digest(data) and returns the 20-byte SHA-1 digest.
__pyfunction_sha1_digestfn
sha256_digestfnComputes Python sha256_digest(data) and returns the 32-byte SHA-256 digest.
__pyfunction_sha256_digestfn
mtproto_auth_key_idfnComputes Python mtproto_auth_key_id(auth_key) from a validated 256-byte key.
__pyfunction_mtproto_auth_key_idfn
mtproto_message_keyfnComputes Python mtproto_message_key for padded plaintext and one MTProto direction.
__pyfunction_mtproto_message_keyfn
mtproto_derive_aes_key_ivfnDerives the AES-256 key and IV used by Python mtproto_derive_aes_key_iv.
__pyfunction_mtproto_derive_aes_key_ivfn
mtproto_encrypt_payloadfnEncrypts padded MTProto plaintext for Python mtproto_encrypt_payload.
__pyfunction_mtproto_encrypt_payloadfn
mtproto_decrypt_payloadfnDecrypts and verifies Python mtproto_decrypt_payload ciphertext.
__pyfunction_mtproto_decrypt_payloadfn
xor_bytesfnReturns the bytewise exclusive-or of Python xor_bytes(left, right) inputs.
__pyfunction_xor_bytesfn
aes_256_ige_encryptfnEncrypts block-aligned bytes with Python aes_256_ige_encrypt.
__pyfunction_aes_256_ige_encryptfn
aes_256_ige_decryptfnDecrypts block-aligned bytes with Python aes_256_ige_decrypt.
__pyfunction_aes_256_ige_decryptfn
aes_256_cbc_encryptfnEncrypts block-aligned bytes with Python aes_256_cbc_encrypt without padding.
__pyfunction_aes_256_cbc_encryptfn
aes_256_cbc_decryptfnDecrypts block-aligned bytes with Python aes_256_cbc_decrypt without padding.
__pyfunction_aes_256_cbc_decryptfn
aes_256_ctr_cryptfnApplies Python aes_256_ctr_crypt to data using AES-CTR keystream XOR.
__pyfunction_aes_256_ctr_cryptfn
aes_256_gcm_encryptfnAuthenticated-encrypts Python aes_256_gcm_encrypt plaintext and associated data.
__pyfunction_aes_256_gcm_encryptfn
aes_256_gcm_decryptfnAuthenticated-decrypts Python aes_256_gcm_decrypt ciphertext-and-tag bytes.
__pyfunction_aes_256_gcm_decryptfn
scrypt_derivefnDerives Python scrypt_derive bytes from password, salt, and scrypt cost parameters.
__pyfunction_scrypt_derivefn
pq_factorizefnFactorizes Python pq_factorize(pq) into ordered nontrivial u64 factors.
__pyfunction_pq_factorizefn
sha1_digest_rawfnComputes SHA-1 for Rust callers without Python or GIL interaction.
sha256_digest_rawfnComputes SHA-256 for Rust callers without Python or GIL interaction.
mtproto_auth_key_id_rawfnDerives the trailing eight SHA-1 bytes that identify a validated MTProto authorization key.
mtproto_message_key_rawfnDerives an MTProto 2.0 message key from a validated authorization key and padded plaintext.
mtproto_derive_aes_key_iv_rawfnDerives the MTProto 2.0 AES-IGE key and IV from validated fixed-width key material.
mtproto_encrypt_payload_rawfnProduces the auth-key identifier, message key, and AES-IGE ciphertext for padded plaintext.
mtproto_decrypt_payload_rawfnDecrypts and authenticates an MTProto payload using validated directional key derivation.
xor_bytes_rawfnComputes bytewise XOR for equal-length Rust byte slices.
aes_256_ige_encrypt_rawfnEncrypts a block-aligned byte slice using AES-256 IGE for Rust callers.
aes_256_ige_decrypt_rawfnDecrypts a block-aligned AES-256 IGE ciphertext for Rust callers.
aes_256_cbc_encrypt_rawfnEncrypts a block-aligned byte slice with unpadded AES-256 CBC.
aes_256_cbc_decrypt_rawfnDecrypts a block-aligned unpadded AES-256 CBC ciphertext.
aes_256_ctr_crypt_rawfnXORs bytes with an AES-256 CTR keystream; the same operation encrypts and decrypts.
aes_256_gcm_encrypt_rawfnAuthenticated-encrypts plaintext with AES-256 GCM and returns ciphertext plus tag.
aes_256_gcm_decrypt_rawfnAuthenticated-decrypts AES-256 GCM ciphertext-and-tag data.
scrypt_derive_rawfnRuns scrypt with validated protocol-level output limits for Rust callers.
pq_factorize_rawfnFinds and orders the two nontrivial factors of an MTProto pq value.
validate_auth_keyfnValidates the fixed 256-byte MTProto authorization-key width.
validate_msg_keyfnValidates the fixed 16-byte MTProto message-key width.
validate_block_multiplefnValidates that AES block-mode input has a whole-number count of AES blocks.
validate_aes_keyfnValidates a 32-byte AES-256 key and returns ValueError otherwise.
validate_ige_ivfnValidates the two-block, 32-byte IV required by AES-IGE.
validate_cbc_ctr_ivfnValidates the one-block IV or counter used by CBC and CTR modes.
validate_gcm_noncefnValidates the 12-byte nonce mandated by this AES-GCM interface.
direction_offsetfnReturns the MTProto key-schedule offset for the requested packet direction.
xor_blockfnComputes a fixed-width XOR block used by the AES block-mode loops.
increment_counterfnAdvances a big-endian AES-CTR counter in place, wrapping at the full block width.
gcdfnComputes the greatest common divisor used by Pollard-rho factorization.
mul_modfnMultiplies modulo modulus with a widened intermediate to avoid u64 overflow.
pow_modfnComputes modular exponentiation for deterministic Miller-Rabin witnesses.
is_prime_u64fnDeterministically tests whether a u64 is prime using fixed Miller-Rabin witnesses.
factor_u64fnReturns one factor of a composite u64 using trial division and Pollard-rho iteration.
AES_BLOCK_SIZEconstAES’s fixed block size in bytes.
MT_PROTO_AUTH_KEY_SIZEconstRequired byte length of an MTProto authorization key.
MT_PROTO_MSG_KEY_SIZEconstRequired byte length of an MTProto 2.0 message key.
GIL_RELEASE_THRESHOLD_BYTESconstWork-size threshold above which native wrappers detach from the Python GIL.
fn detach_if_large<T, F>(py: Python<'_>, work_bytes: usize, f: F) -> T
where
T: Ungil,
F: Ungil + FnOnce() -> T

Defined 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.

  • T: The GIL-independent result type returned by f.
  • F: The one-shot GIL-independent closure performing the native operation.
  • py: The acquired GIL token that can detach f.
  • work_bytes: Conservative byte-work estimate compared with the detach threshold.
  • f: The native computation to run attached or detached.
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.

  • m: The Python extension module receiving the crypto callables.
fn native_available() -> bool

Defined 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.

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

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.

  • py: The acquired GIL token used to detach a large hash operation.
  • data: Bytes to hash.
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

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.

  • py: The acquired GIL token used to detach a large hash operation.
  • data: Bytes to hash.
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

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.

  • auth_key: The 256-byte MTProto authorization key.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • left: First equal-length byte sequence.
  • right: Second equal-length byte sequence.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • 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.
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

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.

  • py: The acquired GIL token used to detach the factorization search.
  • pq: Composite MTProto handshake value to split into ordered factors.
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

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.

  • data: Bytes to hash.
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.

  • data: Bytes to hash.
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.

  • auth_key: Authorization key whose SHA-1 tail supplies the identifier.
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.

  • 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.
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.

  • auth_key: Previously validated 256-byte authorization key.
  • msg_key: Previously validated 16-byte message key.
  • client_to_server: Selects the directional key offset.
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.

  • 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.
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.

  • 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.
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.

  • left: First equal-length byte slice.
  • right: Second equal-length byte slice.
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.

  • plaintext: AES-block-aligned bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte AES-IGE initialization vector.
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.

  • ciphertext: AES-block-aligned bytes to decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 32-byte AES-IGE initialization vector.
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.

  • plaintext: AES-block-aligned bytes to encrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte CBC initialization vector.
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.

  • ciphertext: AES-block-aligned bytes to decrypt.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte CBC initialization vector.
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.

  • data: Bytes to XOR with the generated CTR keystream.
  • key: The 32-byte AES-256 key.
  • iv: The 16-byte initial counter block.
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.

  • 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.
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.

  • 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.
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.

  • 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.
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.

  • pq: Candidate composite integer to split into its two ordered factors.
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.

  • auth_key: Candidate authorization key expected to contain 256 bytes.
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.

  • msg_key: Candidate message key expected to contain 16 bytes.
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.

  • data: Candidate AES block-mode input.
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.

  • key: Candidate AES-256 key.
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.

  • iv: Candidate AES-IGE initialization vector.
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.

  • iv: Candidate CBC initialization vector or CTR counter block.
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.

  • nonce: Candidate AES-GCM nonce.
fn direction_offset(client_to_server: bool) -> usize

Defined in rust/miniproto/src/crypto.rs:1023-1025

Returns the MTProto key-schedule offset for the requested packet direction.

  • client_to_server: Whether the packet travels from client to server.
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.

  • left: First AES-sized block.
  • right: Second AES-sized block.
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.

  • counter: Mutable AES-sized counter block to increment.
fn gcd(left: u64, right: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1062-1069

Computes the greatest common divisor used by Pollard-rho factorization.

  • left: First nonnegative integer.
  • right: Second nonnegative integer.
fn mul_mod(left: u64, right: u64, modulus: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1083-1085

Multiplies modulo modulus with a widened intermediate to avoid u64 overflow.

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.

  • left: First modular multiplicand.
  • right: Second modular multiplicand.
  • modulus: Nonzero modulus used for the reduced product.
fn pow_mod(base: u64, exponent: u64, modulus: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1099-1109

Computes modular exponentiation for deterministic Miller-Rabin witnesses.

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.

  • base: Base to exponentiate modulo modulus.
  • exponent: Nonnegative exponent encoded as u64.
  • modulus: Nonzero modulus used for every reduction.
fn is_prime_u64(value: u64) -> bool

Defined in rust/miniproto/src/crypto.rs:1116-1155

Deterministically tests whether a u64 is prime using fixed Miller-Rabin witnesses.

  • value: Unsigned integer to classify as prime or composite.
fn factor_u64(value: u64) -> u64

Defined in rust/miniproto/src/crypto.rs:1162-1190

Returns one factor of a composite u64 using trial division and Pollard-rho iteration.

  • value: Composite unsigned integer whose nontrivial factor is requested.
const AES_BLOCK_SIZE: usize = 16usize;

Defined in rust/miniproto/src/crypto.rs:24

AES’s fixed block size in bytes.

const MT_PROTO_AUTH_KEY_SIZE: usize = 256usize;

Defined in rust/miniproto/src/crypto.rs:26

Required byte length of an MTProto authorization key.

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.

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.