7 typedef unsigned char uchar;
16 typedef struct sfcctx { u8 a; u8 b; u8 c; u8 count; } sfcctx;
17 #define rot64(x,k) (((x)<<(k))|((x)>>(64-(k))))
20 #define right_shift 11
24 static inline u8 sfc( sfcctx *x ) {
25 u8 tmp = x->a + x->b + x->count++;
26 x->a = x->b ^ (x->b >> right_shift);
27 x->b = x->c + (x->c << left_shift);
28 x->c = rot64(x->c, rotation) + tmp;
32 static inline void sfcinit(sfcctx *x, uint64_t seed) {
43 typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx;
47 static inline u8 jsf( ranctx *x ) {
48 u8 e = x->a - rot64(x->b, 7);
49 x->a = x->b ^ rot64(x->c, 13);
50 x->b = x->c + rot64(x->d, 37);
56 static inline void jsfinit(ranctx *x, uint64_t seed) {
57 x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
68 inline uint64_t xorshift64( const uint64_t state)
77 static inline uint64_t splitmix64_stateless(uint64_t index) {
78 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
79 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
80 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
90 typedef struct sulong2 ulong2;
92 static inline uint64_t rotl(const uint64_t x, int k) {
93 return (x << k) | (x >> (64 - k));
96 // call this one before calling xoroshiro128plus
97 static inline void xoroshiro128plus_seed(ulong2 *xoro,uint64_t seed) {
98 xoro->x = splitmix64_stateless(seed);
99 xoro->y = splitmix64_stateless(seed + 1);
102 // returns random number, modifies xoroshiro128plus_s
103 static inline uint64_t xoroshiro128plus(ulong2 *xoro) {
104 const uint64_t s0 = xoro->x;
105 uint64_t s1 = xoro->y;
106 const uint64_t result = s0 + s1;
109 xoro->x = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
110 xoro->y = rotl(s1, 36); // c
122 struct timeval tstart;
123 gettimeofday(&tstart,0);
124 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
127 double TimeStop(double t)
131 gettimeofday(&tend,0);
132 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
137 uint xorshift32(const uint t)
139 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
151 void rc4key(uchar *key, uchar *sc, int size_DK) {
153 for(int i=0;i<256;i++) {
159 for(int i0=0; i0<256; i0++) {
160 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
168 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
174 for (int i=0;i<len;i++) {
177 for (int it = 0; it < rp; it++) {
179 for(int i0 = 0; i0<len; i0++) {
180 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
191 printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
200 for(int i=0;i<64;i++)
201 DK[i]=splitmix64_stateless(i);
206 rc4key(DK, Sbox1, 8);
207 rc4key(&DK[8], Sbox2, 8);
209 const int bufsize=128;
212 rc4keyperm(&DK[16], bufsize, 1, Pbox, 16);
213 rc4keyperm(&DK[32], bufsize, 1, Pbox2, 16);
219 ulong2 xoro[bufsize];
222 sfcctx sfcd[bufsize];
225 uint64_t Val[bufsize];
226 for(int i=0;i<bufsize;i++) {
227 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
229 xoroshiro128plus_seed(&xoro[i],Val[Pbox[i]]);
230 jsfinit(&ctx[i],1000+i*100+DK[i&63]);
231 sfcinit(&sfcd[i],1000+i*100+DK[i&63]);
240 double t=TimeStart();
242 for(int iter=0;iter<size;iter++) {
243 for(int j=0;j<bufsize;j++) {
244 //Val[j]=splitmix64_stateless(Val[j])^Val[Pbox[j]];
245 //Val[j]=xorshift64(Val[j])^Val[Pbox[j]]; //fail
246 Val[j]=xorshift64(Val[j])^Val[Pbox[j]]^Val[Pbox2[j]];
247 //Val[j]=xoroshiro128plus(&xoro[j])^Val[Pbox[j]];
248 //Val[j]=jsf(&ctx[j])^Val[Pbox[j]]; //good
249 //Val[j]=sfc(&sfcd[j])^Val[Pbox[j]]; //good
252 for(int j=0;j<bufsize;j++) {
253 cipher[j]=Val[j]^plain[j];
261 uchar *ptr=(uchar*)Val;
262 for(int j=0;j<bufsize*8;j++)
263 ptr[j]^=Sbox2[Sbox1[ptr[j]+DK[j&63]]];
264 rc4keyperm(ptr, bufsize, 1, Pbox, 64);
266 rc4keyperm(&ptr[32], bufsize, 1, Pbox2, 64);
269 rc4key(ptr, Sbox1, 64);
270 rc4key(&ptr[64], Sbox2, 64);
276 double time=TimeStop(t);
277 printf("time %e\n",size*bufsize*8/time);