7 typedef unsigned char uchar;
15 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
16 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
22 typedef struct sfcctx { u8 a; u8 b; u8 c; u8 count; } sfcctx;
23 #define rot64(x,k) (((x)<<(k))|((x)>>(64-(k))))
26 #define right_shift 11
30 static inline u8 sfc( sfcctx *x ) {
31 u8 tmp = x->a + x->b + x->count++;
32 x->a = x->b ^ (x->b >> right_shift);
33 x->b = x->c + (x->c << left_shift);
34 x->c = rot64(x->c, rotation) + tmp;
38 static inline void sfcinit(sfcctx *x, uint64_t seed) {
49 typedef struct ranctx { u8 a; u8 b; u8 c; u8 d; } ranctx;
53 static inline u8 jsf( ranctx *x ) {
54 u8 e = x->a - rot64(x->b, 7);
55 x->a = x->b ^ rot64(x->c, 13);
56 x->b = x->c + rot64(x->d, 37);
62 static inline void jsfinit(ranctx *x, uint64_t seed) {
63 x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
74 inline uint64_t xorshift64( const uint64_t state)
83 static inline uint64_t splitmix64_stateless(uint64_t index) {
84 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
85 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
86 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
96 typedef struct sulong2 ulong2;
98 static inline uint64_t rotl(const uint64_t x, int k) {
99 return (x << k) | (x >> (64 - k));
102 // call this one before calling xoroshiro128plus
103 static inline void xoroshiro128plus_seed(ulong2 *xoro,uint64_t seed) {
104 xoro->x = splitmix64_stateless(seed);
105 xoro->y = splitmix64_stateless(seed + 1);
108 // returns random number, modifies xoroshiro128plus_s
109 static inline uint64_t xoroshiro128plus(ulong2 *xoro) {
110 const uint64_t s0 = xoro->x;
111 uint64_t s1 = xoro->y;
112 const uint64_t result = s0 + s1;
115 xoro->x = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
116 xoro->y = rotl(s1, 36); // c
128 struct timeval tstart;
129 gettimeofday(&tstart,0);
130 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
133 double TimeStop(double t)
137 gettimeofday(&tend,0);
138 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
143 uint xorshift32(const uint t)
145 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
157 void rc4key(uchar *key, uchar *sc, int size_DK) {
159 for(int i=0;i<256;i++) {
165 for(int i0=0; i0<256; i0++) {
166 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
174 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
180 for (int i=0;i<len;i++) {
183 for (int it = 0; it < rp; it++) {
185 for(int i0 = 0; i0<len; i0++) {
186 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
196 void scprng(uint64_t *plain, uint64_t* cipher, int bufsize, int nb_bloc, uint64_t *Val, uchar *Sbox1, uchar *Sbox2, int *Pbox, int *Pbox2, uchar *DK, int delta) {
200 for(int nb=0;nb<nb_bloc;nb++) {
202 for(int j=0;j<bufsize;j++) {
203 //Val[j]=splitmix64_stateless(Val[j])^Val[Pbox[j]];
204 //Val[j]=xorshift64(Val[j])^Val[Pbox[j]]; //fail
205 Val[j]=xorshift64(Val[j])^Val[Pbox[j]]^Val[Pbox2[j]];
206 //Val[j]=xoroshiro128plus(&xoro[j])^Val[Pbox[j]];
207 //Val[j]=jsf(&ctx[j])^Val[Pbox[j]]; //good
208 //Val[j]=sfc(&sfcd[j])^Val[Pbox[j]]; //good
211 for(int j=0;j<bufsize;j++) {
212 cipher[nb*bufsize+j]=Val[j]^plain[nb*bufsize+j];
220 uchar *ptr=(uchar*)Val;
221 for(int j=0;j<bufsize*8;j++)
222 ptr[j]^=Sbox2[Sbox1[ptr[j]+DK[j&63]]];
223 rc4keyperm(ptr, bufsize, 1, Pbox, 64);
225 rc4keyperm(&ptr[32], bufsize, 1, Pbox2, 64);
228 rc4key(ptr, Sbox1, 64);
229 rc4key(&ptr[64], Sbox2, 64);
241 printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
246 uchar *data_R, *data_G, *data_B;
255 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
256 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
257 imsize=width*height*3;
258 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
261 width=height=size_buf;
263 buffer=new uchar[imsize];
264 for(int i=0;i<imsize;i++) {
270 uchar* seq= new uchar[imsize];
271 uchar* seq2= new uchar[imsize];
273 int oneD=width*height;
275 for(int i=0;i<oneD;i++) {
277 seq[oneD+i]=data_G[i];
278 seq[2*oneD+i]=data_B[i];
282 for(int i=0;i<oneD;i++) {
287 uint64_t *SEQ=(uint64_t*)seq;
288 uint64_t *SEQ2=(uint64_t*)seq2;
298 for(int i=0;i<64;i++)
299 DK[i]=splitmix64_stateless(i);
304 rc4key(DK, Sbox1, 8);
305 rc4key(&DK[8], Sbox2, 8);
307 const int bufsize=128;
310 rc4keyperm(&DK[16], bufsize, 1, Pbox, 16);
311 rc4keyperm(&DK[32], bufsize, 1, Pbox2, 16);
314 // uint64_t plain[bufsize];
315 // uint64_t cipher[bufsize];
317 ulong2 xoro[bufsize];
320 sfcctx sfcd[bufsize];
323 uint64_t Val[bufsize];
324 for(int i=0;i<bufsize;i++) {
325 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
334 double t=TimeStart();
336 int nb_bloc=imsize/(bufsize*8); //8 because we use 64bits numbers
340 for(uint iter=0;iter<nb_test;iter++) {
341 scprng(SEQ, SEQ2, bufsize, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
344 double time=TimeStop(t);
345 printf("time %e\n",nb_test*nb_bloc*bufsize*8/time);
348 for(int i=0;i<oneD;i++) {
350 data_G[i]=seq2[oneD+i];
351 data_B[i]=seq2[2*oneD+i];
353 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
357 //reinit of parameters
358 rc4key(DK, Sbox1, 8);
359 rc4key(&DK[8], Sbox2, 8);
361 rc4keyperm(&DK[16], bufsize, 1, Pbox, 16);
362 rc4keyperm(&DK[32], bufsize, 1, Pbox2, 16);
363 for(int i=0;i<bufsize;i++) {
364 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
368 for(uint iter=0;iter<nb_test;iter++) {
369 scprng(SEQ2, SEQ, bufsize, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
374 printf("time %e\n",nb_test*nb_bloc*bufsize*8/time);
377 for(int i=0;i<oneD;i++) {
379 data_G[i]=seq[oneD+i];
380 data_B[i]=seq[2*oneD+i];
382 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
386 for(int i=0;i<imsize;i++) {
387 //cout<<(int)buffer[i]<<endl;
388 if(buffer[i]!=seq[i]) {
392 // cout<<"RESULT CORRECT: "<<equal<<endl;