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);
240 int main(int argc, char **argv) {
241 // printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
248 uchar *data_R, *data_G, *data_B;
256 for(int i=1; i<argc; i++){
257 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
258 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
259 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
260 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
270 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
271 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
272 imsize=width*height*3;
273 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
276 width=height=size_buf;
278 buffer=new uchar[imsize];
279 for(int i=0;i<imsize;i++) {
285 uchar* seq= new uchar[imsize];
286 uchar* seq2= new uchar[imsize];
288 int oneD=width*height;
290 for(int i=0;i<oneD;i++) {
292 seq[oneD+i]=data_G[i];
293 seq[2*oneD+i]=data_B[i];
297 for(int i=0;i<oneD;i++) {
302 uint64_t *SEQ=(uint64_t*)seq;
303 uint64_t *SEQ2=(uint64_t*)seq2;
313 for(int i=0;i<64;i++)
314 DK[i]=splitmix64_stateless(i);
319 rc4key(DK, Sbox1, 8);
320 rc4key(&DK[8], Sbox2, 8);
324 rc4keyperm(&DK[16], h, 1, Pbox, 16);
325 rc4keyperm(&DK[32], h, 1, Pbox2, 16);
328 // uint64_t plain[bufsize];
329 // uint64_t cipher[bufsize];
333 for(int i=0;i<h;i++) {
334 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
343 double t=TimeStart();
345 int nb_bloc=imsize/(h*8); //8 because we use 64bits numbers
349 for(uint iter=0;iter<nb_test;iter++) {
350 scprng(SEQ, SEQ2, h, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
353 double time=TimeStop(t);
354 printf("%e ",nb_test*nb_bloc*h*8/time);
357 for(int i=0;i<oneD;i++) {
359 data_G[i]=seq2[oneD+i];
360 data_B[i]=seq2[2*oneD+i];
362 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
366 //reinit of parameters
367 rc4key(DK, Sbox1, 8);
368 rc4key(&DK[8], Sbox2, 8);
370 rc4keyperm(&DK[16], h, 1, Pbox, 16);
371 rc4keyperm(&DK[32], h, 1, Pbox2, 16);
372 for(int i=0;i<h;i++) {
373 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
379 for(uint iter=0;iter<nb_test;iter++) {
380 scprng(SEQ2, SEQ, h, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
385 printf("%e\n",nb_test*nb_bloc*h*8/time);
388 for(int i=0;i<oneD;i++) {
390 data_G[i]=seq[oneD+i];
391 data_B[i]=seq[2*oneD+i];
393 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
397 for(int i=0;i<imsize;i++) {
398 //cout<<(int)buffer[i]<<endl;
399 if(buffer[i]!=seq[i]) {
403 // cout<<"RESULT CORRECT: "<<equal<<endl;