ferogram_crypto/dh.rs
1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2//
3// ferogram: async Telegram MTProto client in Rust
4// https://github.com/ankit-chaubey/ferogram
5//
6// Licensed under either the MIT License or the Apache License 2.0.
7// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
8// https://github.com/ankit-chaubey/ferogram
9//
10// Feel free to use, modify, and share this code.
11// Please keep this notice when redistributing.
12
13use num_bigint::BigUint;
14
15/// Compute `base^exp mod modulus` over arbitrary-precision big-endian byte slices.
16///
17/// All three inputs are big-endian byte slices. Returns big-endian bytes,
18/// zero-padded to nothing (caller pads if needed).
19///
20/// Used for MTProto DH key exchange: `g^b mod p` and `g_a^b mod p`.
21pub fn dh_modpow(base: &[u8], exp: &[u8], modulus: &[u8]) -> Vec<u8> {
22 BigUint::from_bytes_be(base)
23 .modpow(
24 &BigUint::from_bytes_be(exp),
25 &BigUint::from_bytes_be(modulus),
26 )
27 .to_bytes_be()
28}