2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/BigNum.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <utility>
|
|
|
|
|
2021-10-14 14:08:35 +02:00
|
|
|
static bool is_prime(td::uint64 x) {
|
|
|
|
for (td::uint64 d = 2; d < x && d * d <= x; d++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (x % d == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-19 22:01:07 +01:00
|
|
|
static td::vector<td::uint64> gen_primes(td::uint64 L, td::uint64 R, std::size_t limit = 0) {
|
2021-10-14 14:08:35 +02:00
|
|
|
td::vector<td::uint64> res;
|
2022-03-19 22:01:07 +01:00
|
|
|
for (auto x = L; x <= R && (limit <= 0 || res.size() < limit); x++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_prime(x)) {
|
|
|
|
res.push_back(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-03-21 18:43:28 +01:00
|
|
|
static td::vector<td::uint64> gen_primes(int mode) {
|
2021-10-14 14:08:35 +02:00
|
|
|
td::vector<td::uint64> result;
|
2022-03-21 18:43:28 +01:00
|
|
|
if (mode == 1) {
|
|
|
|
for (size_t i = 10; i <= 19; i++) {
|
|
|
|
td::append(result, gen_primes(i * 100000000, (i + 1) * 100000000, 1));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
td::append(result, gen_primes(1, 100));
|
|
|
|
td::append(result, gen_primes((1ull << 31) - 500000, std::numeric_limits<td::uint64>::max(), 5));
|
|
|
|
td::append(result, gen_primes((1ull << 32) - 500000, std::numeric_limits<td::uint64>::max(), 2));
|
|
|
|
td::append(result, gen_primes((1ull << 39) - 500000, std::numeric_limits<td::uint64>::max(), 1));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:08:35 +02:00
|
|
|
using PqQuery = std::pair<td::uint64, td::uint64>;
|
|
|
|
|
2022-03-21 18:43:28 +01:00
|
|
|
static td::vector<PqQuery> gen_pq_queries(int mode = 0) {
|
2021-10-14 14:08:35 +02:00
|
|
|
td::vector<PqQuery> res;
|
2022-03-21 18:43:28 +01:00
|
|
|
auto primes = gen_primes(mode);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto q : primes) {
|
|
|
|
for (auto p : primes) {
|
|
|
|
if (p > q) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
res.emplace_back(p, q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-03-19 22:01:07 +01:00
|
|
|
static td::string to_binary(td::uint64 x) {
|
|
|
|
td::string result;
|
|
|
|
do {
|
|
|
|
result = static_cast<char>(x & 255) + result;
|
|
|
|
x >>= 8;
|
|
|
|
} while (x > 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_pq_fast(td::uint64 first, td::uint64 second) {
|
|
|
|
if ((static_cast<td::uint64>(1) << 63) / first <= second) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
td::string p_str;
|
|
|
|
td::string q_str;
|
|
|
|
int err = td::pq_factorize(to_binary(first * second), &p_str, &q_str);
|
|
|
|
ASSERT_EQ(err, 0);
|
|
|
|
|
|
|
|
ASSERT_STREQ(p_str, to_binary(first));
|
|
|
|
ASSERT_STREQ(q_str, to_binary(second));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
|
|
|
static void test_pq_slow(td::uint64 first, td::uint64 second) {
|
|
|
|
if ((static_cast<td::uint64>(1) << 63) / first > second) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:08:35 +02:00
|
|
|
td::BigNum p = td::BigNum::from_decimal(PSLICE() << first).move_as_ok();
|
|
|
|
td::BigNum q = td::BigNum::from_decimal(PSLICE() << second).move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-14 14:08:35 +02:00
|
|
|
td::BigNum pq;
|
|
|
|
td::BigNumContext context;
|
|
|
|
td::BigNum::mul(pq, p, q, context);
|
|
|
|
td::string pq_str = pq.to_binary();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
td::string p_str;
|
|
|
|
td::string q_str;
|
2018-12-31 20:04:05 +01:00
|
|
|
int err = td::pq_factorize(pq_str, &p_str, &q_str);
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(err == 0) << first << " * " << second;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-14 14:08:35 +02:00
|
|
|
td::BigNum p_res = td::BigNum::from_binary(p_str);
|
|
|
|
td::BigNum q_res = td::BigNum::from_binary(q_str);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(p_str == p.to_binary()) << td::tag("got", p_res.to_decimal()) << td::tag("expected", first);
|
|
|
|
LOG_CHECK(q_str == q.to_binary()) << td::tag("got", q_res.to_decimal()) << td::tag("expected", second);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST(CryptoPQ, hands) {
|
|
|
|
ASSERT_EQ(1ull, td::pq_factorize(0));
|
|
|
|
ASSERT_EQ(1ull, td::pq_factorize(1));
|
|
|
|
ASSERT_EQ(1ull, td::pq_factorize(2));
|
|
|
|
ASSERT_EQ(1ull, td::pq_factorize(3));
|
|
|
|
ASSERT_EQ(2ull, td::pq_factorize(4));
|
|
|
|
ASSERT_EQ(1ull, td::pq_factorize(5));
|
|
|
|
ASSERT_EQ(3ull, td::pq_factorize(7 * 3));
|
|
|
|
ASSERT_EQ(179424611ull, td::pq_factorize(179424611ull * 179424673ull));
|
|
|
|
|
|
|
|
#if TD_HAVE_OPENSSL
|
2022-03-19 22:01:07 +01:00
|
|
|
test_pq_slow(4294467311, 4294467449);
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-03-19 22:01:07 +01:00
|
|
|
TEST(CryptoPQ, four) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < 100000; i++) {
|
2022-03-19 22:01:07 +01:00
|
|
|
test_pq_fast(2, 2);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-03-19 22:01:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CryptoPQ, generated_fast) {
|
|
|
|
auto queries = gen_pq_queries();
|
|
|
|
for (const auto &query : queries) {
|
|
|
|
test_pq_fast(query.first, query.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 18:43:28 +01:00
|
|
|
TEST(CryptoPQ, generated_server) {
|
|
|
|
auto queries = gen_pq_queries(1);
|
|
|
|
for (const auto &query : queries) {
|
|
|
|
test_pq_fast(query.first, query.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 22:01:07 +01:00
|
|
|
#if TD_HAVE_OPENSSL
|
|
|
|
TEST(CryptoPQ, generated_slow) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto queries = gen_pq_queries();
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &query : queries) {
|
2022-03-19 22:01:07 +01:00
|
|
|
test_pq_slow(query.first, query.second);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
#endif
|