#include #include #include #include typedef unsigned char uchar; typedef uint64_t u8; typedef struct sfcctx { u8 a; u8 b; u8 c; u8 count; } sfcctx; #define rot64(x,k) (((x)<<(k))|((x)>>(64-(k)))) #define rotation 24 #define right_shift 11 #define left_shift 3 static inline u8 sfc( sfcctx *x ) { u8 tmp = x->a + x->b + x->count++; x->a = x->b ^ (x->b >> right_shift); x->b = x->c + (x->c << left_shift); x->c = rot64(x->c, rotation) + tmp; return tmp; } static inline void sfcinit(sfcctx *x, uint64_t seed) { x->a = seed; x->b = seed; x->c = seed; x->count = 1; for(int i=0;i<12;i++) sfc(x); } typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx; static inline u8 jsf( ranctx *x ) { u8 e = x->a - rot64(x->b, 7); x->a = x->b ^ rot64(x->c, 13); x->b = x->c + rot64(x->d, 37); x->c = x->d + e; x->d = e + x->a; return x->d; } static inline void jsfinit(ranctx *x, uint64_t seed) { x->a = 0xf1ea5eed, x->b = x->c = x->d = seed; } inline uint64_t xorshift64( const uint64_t state) { uint64_t x = state; x^= x << 13; x^= x >> 7; x^= x << 17; return x; } static inline uint64_t splitmix64_stateless(uint64_t index) { uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15)); z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); return z ^ (z >> 31); } struct sulong2 { uint64_t x; uint64_t y; }; typedef struct sulong2 ulong2; static inline uint64_t rotl(const uint64_t x, int k) { return (x << k) | (x >> (64 - k)); } // call this one before calling xoroshiro128plus static inline void xoroshiro128plus_seed(ulong2 *xoro,uint64_t seed) { xoro->x = splitmix64_stateless(seed); xoro->y = splitmix64_stateless(seed + 1); } // returns random number, modifies xoroshiro128plus_s static inline uint64_t xoroshiro128plus(ulong2 *xoro) { const uint64_t s0 = xoro->x; uint64_t s1 = xoro->y; const uint64_t result = s0 + s1; s1 ^= s0; xoro->x = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b xoro->y = rotl(s1, 36); // c return result; } double TimeStart() { struct timeval tstart; gettimeofday(&tstart,0); return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) ); } double TimeStop(double t) { struct timeval tend; gettimeofday(&tend,0); t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t; return (t); } uint xorshift32(const uint t) { /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ uint x = t; x ^= x << 13; x ^= x >> 17; x ^= x << 5; return x; } void rc4key(uchar *key, uchar *sc, int size_DK) { for(int i=0;i<256;i++) { sc[i]=i; } uchar j0 = 0; for(int i0=0; i0<256; i0++) { j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF; uchar tmp = sc[i0]; sc[i0] = sc[j0 ]; sc[j0] = tmp; } } void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) { //sc=1:len; for (int i=0;i