1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-03-09 15:40:10 +00:00

pow-testgiver support

This commit is contained in:
ton 2020-07-06 17:07:20 +03:00
parent dbde9c1c40
commit f064b1047a
257 changed files with 6665 additions and 2608 deletions

View file

@ -21,6 +21,7 @@
#include "td/utils/common.h"
#include "td/utils/crypto.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
#include "td/utils/tests.h"
#include "td/utils/UInt.h"
@ -30,6 +31,29 @@
static td::vector<td::string> strings{"", "1", "short test string", td::string(1000000, 'a')};
#if TD_HAVE_OPENSSL
#if TD_HAVE_ZLIB
TEST(Crypto, Aes) {
td::Random::Xorshift128plus rnd(123);
td::UInt256 key;
rnd.bytes(as_slice(key));
td::string plaintext(16, '\0');
td::string encrypted(16, '\0');
td::string decrypted(16, '\0');
rnd.bytes(plaintext);
td::AesState encryptor;
encryptor.init(as_slice(key), true);
td::AesState decryptor;
decryptor.init(as_slice(key), false);
encryptor.encrypt(td::as_slice(plaintext).ubegin(), td::as_slice(encrypted).ubegin(), 16);
decryptor.decrypt(td::as_slice(encrypted).ubegin(), td::as_slice(decrypted).ubegin(), 16);
CHECK(decrypted == plaintext);
CHECK(decrypted != encrypted);
CHECK(td::crc32(encrypted) == 178892237);
}
TEST(Crypto, AesCtrState) {
td::vector<td::uint32> answers1{0u, 1141589763u, 596296607u, 3673001485u, 2302125528u,
330967191u, 2047392231u, 3537459563u, 307747798u, 2149598133u};
@ -63,7 +87,7 @@ TEST(Crypto, AesCtrState) {
ASSERT_EQ(answers1[i], td::crc32(t));
state.init(as_slice(key), as_slice(iv));
state.decrypt(t, t);
ASSERT_STREQ(s, t);
ASSERT_STREQ(td::base64_encode(s), td::base64_encode(t));
for (auto &c : iv.raw) {
c = 0xFF;
@ -76,6 +100,82 @@ TEST(Crypto, AesCtrState) {
}
}
TEST(Crypto, AesIgeState) {
td::vector<td::uint32> answers1{0u, 2045698207u, 2423540300u, 525522475u, 1545267325u};
std::size_t i = 0;
for (auto length : {0, 16, 32, 256, 1024}) {
td::uint32 seed = length;
td::string s(length, '\0');
for (auto &c : s) {
seed = seed * 123457567u + 987651241u;
c = static_cast<char>((seed >> 23) & 255);
}
td::UInt256 key;
for (auto &c : key.raw) {
seed = seed * 123457567u + 987651241u;
c = (seed >> 23) & 255;
}
td::UInt256 iv;
for (auto &c : iv.raw) {
seed = seed * 123457567u + 987651241u;
c = (seed >> 23) & 255;
}
td::AesIgeState state;
state.init(as_slice(key), as_slice(iv), true);
td::string t(length, '\0');
state.encrypt(s, t);
ASSERT_EQ(answers1[i], td::crc32(t));
state.init(as_slice(key), as_slice(iv), false);
state.decrypt(t, t);
ASSERT_STREQ(td::base64_encode(s), td::base64_encode(t));
i++;
}
}
TEST(Crypto, AesCbcState) {
td::vector<td::uint32> answers1{0u, 3617355989u, 3449188102u, 186999968u, 4244808847u};
std::size_t i = 0;
for (auto length : {0, 16, 32, 256, 1024}) {
td::uint32 seed = length;
td::string s(length, '\0');
for (auto &c : s) {
seed = seed * 123457567u + 987651241u;
c = static_cast<char>((seed >> 23) & 255);
}
td::UInt256 key;
for (auto &c : key.raw) {
seed = seed * 123457567u + 987651241u;
c = (seed >> 23) & 255;
}
td::UInt128 iv;
for (auto &c : iv.raw) {
seed = seed * 123457567u + 987651241u;
c = (seed >> 23) & 255;
}
td::AesCbcState state(as_slice(key), as_slice(iv));
//state.init(as_slice(key), as_slice(iv), true);
td::string t(length, '\0');
state.encrypt(s, t);
ASSERT_EQ(answers1[i], td::crc32(t));
//state.init(as_slice(key), as_slice(iv), false);
state = td::AesCbcState(as_slice(key), as_slice(iv));
state.decrypt(t, t);
ASSERT_STREQ(td::base64_encode(s), td::base64_encode(t));
i++;
}
}
#endif
TEST(Crypto, Sha256State) {
for (auto length : {0, 1, 31, 32, 33, 9999, 10000, 10001, 999999, 1000001}) {
auto s = td::rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), length);
@ -97,8 +197,8 @@ TEST(Crypto, Sha256State) {
}
TEST(Crypto, PBKDF) {
td::vector<td::string> passwords{"", "qwerty", std::string(1000, 'a')};
td::vector<td::string> salts{"", "qwerty", std::string(1000, 'a')};
td::vector<td::string> passwords{"", "qwerty", td::string(1000, 'a')};
td::vector<td::string> salts{"", "qwerty", td::string(1000, 'a')};
td::vector<int> iteration_counts{1, 2, 1000};
td::vector<td::Slice> answers{
"984LZT0tcqQQjPWr6RL/3Xd2Ftu7J6cOggTzri0Pb60=", "lzmEEdaupDp3rO+SImq4J41NsGaL0denanJfdoCsRcU=",
@ -199,7 +299,7 @@ TEST(Crypto, crc32c_benchmark) {
public:
explicit Crc32cExtendBenchmark(size_t chunk_size) : chunk_size_(chunk_size) {
}
std::string get_description() const override {
td::string get_description() const override {
return PSTRING() << "Crc32c with chunk_size=" << chunk_size_;
}
void start_up_n(int n) override {
@ -209,7 +309,7 @@ TEST(Crypto, crc32c_benchmark) {
} else {
cnt_ = 1;
}
data_ = std::string(n, 'a');
data_ = td::string(n, 'a');
}
void run(int n) override {
td::uint32 res = 0;
@ -226,7 +326,7 @@ TEST(Crypto, crc32c_benchmark) {
private:
size_t chunk_size_;
std::string data_;
td::string data_;
int cnt_;
};
bench(Crc32cExtendBenchmark(2));