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, uchar * Sbox3, uchar *Sbox4, int *Pbox, int *Pbox2, int *Pbox3, int *Pbox4, 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]=xorshift64(Val[j]);
208 //Val[j]=xoroshiro128plus(&xoro[j])^Val[Pbox[j]];
209 //Val[j]=jsf(&ctx[j])^Val[Pbox[j]]; //good
210 //Val[j]=sfc(&sfcd[j])^Val[Pbox[j]]; //good
212 for(int j=0;j<bufsize;j++) {
213 Val[j]=Val[j]^Val[Pbox[j]]^Val[Pbox2[j]];
217 for(int j=0;j<bufsize;j++) {
218 cipher[nb*bufsize+j]=Val[j]^plain[nb*bufsize+j];
226 uchar *ptr=(uchar*)Val;
227 for(int j=0;j<bufsize*8;j++)
228 ptr[j]^=Sbox2[Sbox1[ptr[j]+DK[j&63]]];
230 for(int j=0;j<256;j++)
231 Sbox1[j]=Sbox3[Sbox1[j]];
233 for(int j=0;j<256;j++)
234 Sbox2[j]=Sbox4[Sbox2[j]];
236 for(int j=0;j<256;j++)
237 Sbox3[j]=Sbox4[Sbox3[j]];
240 for(int j=0;j<bufsize;j++)
241 Pbox[j]=Pbox3[Pbox[j]];
243 for(int j=0;j<bufsize;j++)
244 Pbox2[j]=Pbox4[Pbox2[j]];
246 for(int j=0;j<bufsize;j++)
247 Pbox3[j]=Pbox4[Pbox3[j]];
261 int main(int argc, char **argv) {
262 // printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
269 uchar *data_R, *data_G, *data_B;
277 for(int i=1; i<argc; i++){
278 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
279 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
280 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
281 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
291 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
292 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
293 imsize=width*height*3;
294 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
297 width=height=size_buf;
299 buffer=new uchar[imsize];
300 for(int i=0;i<imsize;i++) {
306 uchar* seq= new uchar[imsize];
307 uchar* seq2= new uchar[imsize];
309 int oneD=width*height;
311 for(int i=0;i<oneD;i++) {
313 seq[oneD+i]=data_G[i];
314 seq[2*oneD+i]=data_B[i];
318 for(int i=0;i<oneD;i++) {
323 uint64_t *SEQ=(uint64_t*)seq;
324 uint64_t *SEQ2=(uint64_t*)seq2;
334 for(int i=0;i<64;i++)
335 DK[i]=splitmix64_stateless(i);
342 rc4key(DK, Sbox1, 8);
343 rc4key(&DK[8], Sbox2, 8);
344 rc4key(&DK[16], Sbox3, 8);
345 rc4key(&DK[24], Sbox4, 8);
351 rc4keyperm(&DK[16], h, 1, Pbox, 16);
352 rc4keyperm(&DK[32], h, 1, Pbox2, 16);
353 rc4keyperm(&DK[8], h, 1, Pbox3, 16);
354 rc4keyperm(&DK[48], h, 1, Pbox4, 16);
357 // uint64_t plain[bufsize];
358 // uint64_t cipher[bufsize];
362 for(int i=0;i<h;i++) {
363 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
372 double t=TimeStart();
374 int nb_bloc=imsize/(h*8); //8 because we use 64bits numbers
378 for(uint iter=0;iter<nb_test;iter++) {
379 scprng(SEQ, SEQ2, h, nb_bloc, Val,Sbox1, Sbox2, Sbox3, Sbox4, Pbox, Pbox2, Pbox3, Pbox4, DK, delta);
382 double time=TimeStop(t);
383 printf("%e ",nb_test*nb_bloc*h*8/time);
386 for(int i=0;i<oneD;i++) {
388 data_G[i]=seq2[oneD+i];
389 data_B[i]=seq2[2*oneD+i];
391 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
395 //reinit of parameters
396 rc4key(DK, Sbox1, 8);
397 rc4key(&DK[8], Sbox2, 8);
399 rc4keyperm(&DK[16], h, 1, Pbox, 16);
400 rc4keyperm(&DK[32], h, 1, Pbox2, 16);
401 for(int i=0;i<h;i++) {
402 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
408 for(uint iter=0;iter<nb_test;iter++) {
409 scprng(SEQ2, SEQ, h, nb_bloc, Val,Sbox1, Sbox2, Sbox3, Sbox4, Pbox, Pbox2, Pbox3, Pbox4, DK, delta);
414 printf("%e\n",nb_test*nb_bloc*h*8/time);
417 for(int i=0;i<oneD;i++) {
419 data_G[i]=seq[oneD+i];
420 data_B[i]=seq[2*oneD+i];
422 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
426 for(int i=0;i<imsize;i++) {
427 //cout<<(int)buffer[i]<<endl;
428 if(buffer[i]!=seq[i]) {
432 // cout<<"RESULT CORRECT: "<<equal<<endl;