2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2021-07-05 20:20:07 +02:00
|
|
|
#include "td/mtproto/DhCallback.h"
|
2019-01-31 12:26:06 +01:00
|
|
|
#include "td/mtproto/DhHandshake.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/base64.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/utils/benchmark.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#if TD_LINUX || TD_ANDROID || TD_TIZEN
|
|
|
|
#include <semaphore.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
static int32 g = 3;
|
|
|
|
static string prime_base64 =
|
|
|
|
"xxyuucaxyQSObFIvcPE_c5gNQCOOPiHBSTTQN1Y9kw9IGYoKp8FAWCKUk9IlMPTb-jNvbgrJJROVQ67UTM58NyD9UfaUWHBaxozU_mtrE6vcl0ZRKW"
|
|
|
|
"kyhFTxj6-MWV9kJHf-lrsqlB1bzR1KyMxJiAcI-ps3jjxPOpBgvuZ8-aSkppWBEFGQfhYnU7VrD2tBDbp02KhLKhSzFE4O8ShHVP0X7ZUNWWW0ud1G"
|
|
|
|
"WC2xF40WnGvEZbDW_5yjko_vW5rk5Bj8Feg-vqD4f6n_Xu1wBQ3tKEn0e_lZ2VaFDOkphR8NgRX2NbEF7i5OFdBLJFS_b0-t8DSxBAMRnNjjuS_MW"
|
|
|
|
"w";
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class HandshakeBench final : public Benchmark {
|
2021-07-03 22:51:36 +02:00
|
|
|
std::string get_description() const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
return "Handshake";
|
|
|
|
}
|
|
|
|
|
2021-07-05 20:04:23 +02:00
|
|
|
class FakeDhCallback final : public mtproto::DhCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
int is_good_prime(Slice prime_str) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = cache.find(prime_str.str());
|
|
|
|
if (it == cache.end()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void add_good_prime(Slice prime_str) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
cache[prime_str.str()] = 1;
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void add_bad_prime(Slice prime_str) const final {
|
2018-12-31 20:04:05 +01:00
|
|
|
cache[prime_str.str()] = 0;
|
|
|
|
}
|
|
|
|
mutable std::map<string, int> cache;
|
|
|
|
} dh_callback;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void run(int n) final {
|
2021-07-05 20:04:23 +02:00
|
|
|
mtproto::DhHandshake a;
|
|
|
|
mtproto::DhHandshake b;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto prime = base64url_decode(prime_base64).move_as_ok();
|
2021-07-05 20:04:23 +02:00
|
|
|
mtproto::DhHandshake::check_config(g, prime, &dh_callback).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < n; i += 2) {
|
|
|
|
a.set_config(g, prime);
|
|
|
|
b.set_config(g, prime);
|
|
|
|
b.set_g_a(a.get_g_b());
|
|
|
|
a.set_g_a(b.get_g_b());
|
2018-08-09 22:41:01 +02:00
|
|
|
a.run_checks(true, &dh_callback).ensure();
|
|
|
|
b.run_checks(true, &dh_callback).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto a_key = a.gen_key();
|
|
|
|
auto b_key = b.gen_key();
|
|
|
|
CHECK(a_key.first == b_key.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(DEBUG));
|
|
|
|
td::bench(td::HandshakeBench());
|
|
|
|
}
|