obfs, rand: Add seed parameter for xorshiftplus

This commit is contained in:
revsic
2019-04-07 01:09:37 +09:00
parent 0752d4e2e5
commit 7f4cff179e
2 changed files with 20 additions and 20 deletions
+10 -10
View File
@@ -176,22 +176,22 @@ namespace obfs {
namespace obfs {
using size_t = decltype(sizeof(void*));
constexpr char TIME[] = __TIME__;
constexpr int digit(char c) {
return c - '0';
}
constexpr int SEED = digit(TIME[7]) +
constexpr size_t SEED = digit(TIME[7]) +
digit(TIME[6]) * 10 +
digit(TIME[4]) * 60 +
digit(TIME[3]) * 600 +
digit(TIME[1]) * 3600 +
digit(TIME[0]) * 36000;
using size_t = decltype(sizeof(void*));
template <size_t Idx>
template <size_t Seed, size_t Idx>
struct Xorshiftplus {
using prev = Xorshiftplus<Idx - 1>;
using prev = Xorshiftplus<Seed, Idx - 1>;
constexpr static size_t update() {
constexpr size_t x = prev::state0 ^ (prev::state0 << 23);
@@ -205,15 +205,15 @@ namespace obfs {
constexpr static size_t value = state0 + state1;
};
template <>
struct Xorshiftplus<0> {
constexpr static size_t state0 = static_cast<size_t>(SEED);
constexpr static size_t state1 = static_cast<size_t>(SEED << 1);
template <size_t Seed>
struct Xorshiftplus<Seed, 0> {
constexpr static size_t state0 = Seed;
constexpr static size_t state1 = Seed << 1;
constexpr static size_t value = state0 + state1;
};
template <size_t Idx, size_t Mod>
constexpr size_t RAND_VAL = Xorshiftplus<Idx>::value % Mod;
constexpr size_t RAND_VAL = Xorshiftplus<SEED, Idx>::value % Mod;
}
+10 -10
View File
@@ -2,22 +2,22 @@
#define COMPILE_TIME_RANDOM
namespace obfs {
using size_t = decltype(sizeof(void*));
constexpr char TIME[] = __TIME__;
constexpr int digit(char c) {
return c - '0';
}
constexpr int SEED = digit(TIME[7]) +
constexpr size_t SEED = digit(TIME[7]) +
digit(TIME[6]) * 10 +
digit(TIME[4]) * 60 +
digit(TIME[3]) * 600 +
digit(TIME[1]) * 3600 +
digit(TIME[0]) * 36000;
using size_t = decltype(sizeof(void*));
template <size_t Idx>
template <size_t Seed, size_t Idx>
struct Xorshiftplus {
using prev = Xorshiftplus<Idx - 1>;
using prev = Xorshiftplus<Seed, Idx - 1>;
constexpr static size_t update() {
constexpr size_t x = prev::state0 ^ (prev::state0 << 23);
@@ -31,15 +31,15 @@ namespace obfs {
constexpr static size_t value = state0 + state1;
};
template <>
struct Xorshiftplus<0> {
constexpr static size_t state0 = static_cast<size_t>(SEED);
constexpr static size_t state1 = static_cast<size_t>(SEED << 1);
template <size_t Seed>
struct Xorshiftplus<Seed, 0> {
constexpr static size_t state0 = Seed;
constexpr static size_t state1 = Seed << 1;
constexpr static size_t value = state0 + state1;
};
template <size_t Idx, size_t Mod>
constexpr size_t RAND_VAL = Xorshiftplus<Idx>::value % Mod;
constexpr size_t RAND_VAL = Xorshiftplus<SEED, Idx>::value % Mod;
}
#define MAKE_RAND_VAL(MOD) RAND_VAL<__LINE__, MOD>