|
| 1 | + |
| 2 | +#include <sys/types.h> |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdint> |
| 5 | +#include <cstdlib> |
| 6 | +#include <iostream> |
| 7 | +#include <ostream> |
| 8 | +#include <string> |
| 9 | +#include <vector> |
| 10 | +#include <limits> |
| 11 | +#include "util.h" |
| 12 | + |
| 13 | +using std::vector; |
| 14 | + |
| 15 | +const std::string kAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 16 | + |
| 17 | +class Matrix { |
| 18 | + vector<vector<int32_t>> matrix_; |
| 19 | + size_t rows_num_, cols_num_; |
| 20 | + |
| 21 | +public: |
| 22 | + Matrix(size_t rows_num, size_t cols_num) |
| 23 | + : matrix_(rows_num, vector<int32_t>(cols_num, 0)), |
| 24 | + rows_num_(rows_num), |
| 25 | + cols_num_(cols_num) { |
| 26 | + } |
| 27 | + |
| 28 | + explicit Matrix(size_t rows_num) : Matrix(rows_num, rows_num) { |
| 29 | + } |
| 30 | + |
| 31 | + size_t Rows() const { |
| 32 | + return rows_num_; |
| 33 | + } |
| 34 | + |
| 35 | + size_t Columns() const { |
| 36 | + return cols_num_; |
| 37 | + } |
| 38 | + |
| 39 | + int32_t& operator()(size_t row_idx, size_t col_idx) { |
| 40 | + return matrix_[row_idx][col_idx]; |
| 41 | + } |
| 42 | + |
| 43 | + int32_t operator()(size_t row_idx, size_t col_idx) const { |
| 44 | + return matrix_[row_idx][col_idx]; |
| 45 | + } |
| 46 | + |
| 47 | + Matrix& operator+=(const Matrix& rhs) { |
| 48 | + for (size_t i = 0; i < rows_num_; ++i) { |
| 49 | + for (size_t j = 0; j < cols_num_; ++j) { |
| 50 | + matrix_[i][j] += rhs.matrix_[i][j]; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return *this; |
| 55 | + } |
| 56 | + |
| 57 | + Matrix operator-() const { |
| 58 | + Matrix negative(rows_num_, cols_num_); |
| 59 | + for (size_t i = 0; i < negative.rows_num_; ++i) { |
| 60 | + for (size_t j = 0; j < negative.cols_num_; ++j) { |
| 61 | + negative.matrix_[i][j] = -matrix_[i][j]; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return negative; |
| 66 | + } |
| 67 | + |
| 68 | + Matrix& operator-=(const Matrix& rhs) { |
| 69 | + return (*this) += -rhs; |
| 70 | + } |
| 71 | + |
| 72 | + friend Matrix operator*(const Matrix& lhs, const Matrix& rhs); |
| 73 | + |
| 74 | + Matrix& operator*=(const Matrix& rhs) { |
| 75 | + return *this = *this * rhs; |
| 76 | + } |
| 77 | + |
| 78 | + Matrix Pow(size_t pow) const; |
| 79 | + |
| 80 | + void Print() const { |
| 81 | + std::cout << "["; |
| 82 | + for (size_t i = 0; i < rows_num_; ++i) { |
| 83 | + std::cout << "["; |
| 84 | + for (size_t j = 0; j < cols_num_; ++j) { |
| 85 | + std::cout << matrix_[i][j]; |
| 86 | + if (j + 1 != cols_num_) { |
| 87 | + std::cout << ", "; |
| 88 | + } |
| 89 | + } |
| 90 | + std::cout << "]"; |
| 91 | + if (i + 1 != rows_num_) { |
| 92 | + std::cout << std::endl; |
| 93 | + } |
| 94 | + } |
| 95 | + std::cout << "]" << std::endl; |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +Matrix operator+(Matrix lhs, const Matrix& rhs) { |
| 100 | + return lhs += rhs; |
| 101 | +} |
| 102 | + |
| 103 | +Matrix operator-(Matrix lhs, const Matrix& rhs) { |
| 104 | + return lhs -= rhs; |
| 105 | +} |
| 106 | + |
| 107 | +Matrix operator*(const Matrix& lhs, const Matrix& rhs) { |
| 108 | + Matrix dot(lhs.rows_num_, rhs.cols_num_); |
| 109 | + for (size_t i = 0; i < dot.rows_num_; ++i) { |
| 110 | + for (size_t j = 0; j < dot.cols_num_; ++j) { |
| 111 | + int32_t sum = 0; |
| 112 | + for (size_t z = 0; z < lhs.cols_num_; ++z) { |
| 113 | + sum += static_cast<uint32_t>(lhs.matrix_[i][z] * rhs.matrix_[z][j]); |
| 114 | + } |
| 115 | + dot.matrix_[i][j] = sum; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return dot; |
| 120 | +} |
| 121 | + |
| 122 | +Matrix Transpose(const Matrix& m) { |
| 123 | + Matrix result(m.Columns(), m.Rows()); |
| 124 | + for (size_t i = 0; i < result.Rows(); ++i) { |
| 125 | + for (size_t j = 0; j < result.Columns(); ++j) { |
| 126 | + result(i, j) = m(j, i); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return result; |
| 131 | +} |
| 132 | + |
| 133 | +Matrix Identity(size_t rows_num) { |
| 134 | + Matrix result(rows_num); |
| 135 | + for (size_t i = 0; i < result.Rows(); ++i) { |
| 136 | + result(i, i) = 1; |
| 137 | + } |
| 138 | + |
| 139 | + return result; |
| 140 | +} |
| 141 | + |
| 142 | +Matrix Matrix::Pow(size_t pow) const { |
| 143 | + Matrix res = Identity(rows_num_); |
| 144 | + Matrix m = *this; |
| 145 | + do { |
| 146 | + if (pow & 1) { |
| 147 | + res *= m; |
| 148 | + } |
| 149 | + m *= m; |
| 150 | + pow = pow >> 1; |
| 151 | + } while (pow); |
| 152 | + |
| 153 | + return res; |
| 154 | +} |
| 155 | + |
| 156 | +void GenString(std::string* s) { |
| 157 | + for (auto& c : *s) { |
| 158 | + c = kAlphabet[std::rand() % kAlphabet.size()]; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +Matrix MakeState(uint32_t seed) { |
| 163 | + Matrix state(1, 31); |
| 164 | + state(0, 0) = seed; |
| 165 | + |
| 166 | + int32_t word = seed; |
| 167 | + for (size_t i = 1; i < 31; ++i) { |
| 168 | + int64_t hi = word / 127773; |
| 169 | + int64_t lo = word % 127773; |
| 170 | + word = 16807 * lo - 2836 * hi; |
| 171 | + if (word < 0) { |
| 172 | + word += 2147483647; |
| 173 | + } |
| 174 | + state(0, i) = word; |
| 175 | + } |
| 176 | + |
| 177 | + return state; |
| 178 | +} |
| 179 | + |
| 180 | +Matrix MakeTransformMatrix() { |
| 181 | + size_t size = 31; |
| 182 | + Matrix transform = Identity(size); |
| 183 | + size_t shift = 3; |
| 184 | + for (size_t j = 0; j < size; ++j) { |
| 185 | + size_t col_idx = (j + shift) % size; |
| 186 | + for (size_t i = 0; i < size; ++i) { |
| 187 | + transform(i, col_idx) += transform(i, j); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + return transform; |
| 192 | +} |
| 193 | + |
| 194 | +std::string GenPassword(int32_t seed) { |
| 195 | + static constexpr std::string kIdealPassword = "NLXGI4NoAp"; |
| 196 | + |
| 197 | + Matrix state = MakeState(seed); |
| 198 | + |
| 199 | + const static Matrix kTransform = MakeTransformMatrix(); |
| 200 | + |
| 201 | + static constexpr size_t kIter = 100'000'001; |
| 202 | + |
| 203 | + static constexpr size_t kPow = (kIter - 1) / 31 + 1 + 10; |
| 204 | + static constexpr size_t kPos = ((kIter - 1) % 31 + 3) % 31; |
| 205 | + |
| 206 | + static const Matrix kRandomTransform = kTransform.Pow(kPow); |
| 207 | + Matrix result = state * kRandomTransform; |
| 208 | + |
| 209 | + std::string password; |
| 210 | + for (size_t i = 0; i < 10; ++i) { |
| 211 | + uint32_t u_random_value = result(0, kPos + i); |
| 212 | + int32_t random_value = u_random_value >> 1; |
| 213 | + char letter = kAlphabet[random_value % kAlphabet.size()]; |
| 214 | + password.push_back(letter); |
| 215 | + // if (letter != kIdealPassword[i]) { |
| 216 | + // return false; |
| 217 | + // } |
| 218 | + } |
| 219 | + |
| 220 | + return password; |
| 221 | +} |
| 222 | + |
1 | 223 | int main() { |
| 224 | + const auto current_dir = GetFileDir(__FILE__); |
| 225 | + std::filesystem::path input_file(current_dir / "../seed.txt"); |
| 226 | + std::ofstream myfile(input_file); |
| 227 | + |
| 228 | + // size_t checked_cnt = 0; |
| 229 | + // for (int32_t seed = std::numeric_limits<int32_t>::min(); seed <= std::numeric_limits<int32_t>::max(); ++seed) { |
| 230 | + // if (seed != 0) { |
| 231 | + // if (GenPassword(seed)) { |
| 232 | + // std::cout << "Ideal seed: " << seed << std::endl; |
| 233 | + // myfile << seed << std::endl; |
| 234 | + // myfile.close(); |
| 235 | + // } |
| 236 | + // ++checked_cnt; |
| 237 | + // } |
| 238 | + |
| 239 | + // if (checked_cnt % 1000000 == 0) { |
| 240 | + // std::cout << "Checked: " << checked_cnt << std::endl; |
| 241 | + // } |
| 242 | + // } |
| 243 | + |
| 244 | + std::cout << "My password: " << GenPassword(1543750888) << std::endl; |
2 | 245 | } |
0 commit comments