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]=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]]];
229 rc4keyperm(ptr, bufsize, 1, Pbox, 64);
231 rc4keyperm(&ptr[32], bufsize, 1, Pbox2, 64);
234 rc4key(ptr, Sbox1, 64);
235 rc4key(&ptr[64], Sbox2, 64);
246 int main(int argc, char **argv) {
247 // printf("%d %d \n",sizeof(__uint64_t),sizeof(ulong));
254 uchar *data_R, *data_G, *data_B;
262 for(int i=1; i<argc; i++){
263 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
264 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
265 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
266 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
276 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
277 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
278 imsize=width*height*3;
279 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
282 width=height=size_buf;
284 buffer=new uchar[imsize];
285 for(int i=0;i<imsize;i++) {
291 uchar* seq= new uchar[imsize];
292 uchar* seq2= new uchar[imsize];
294 int oneD=width*height;
296 for(int i=0;i<oneD;i++) {
298 seq[oneD+i]=data_G[i];
299 seq[2*oneD+i]=data_B[i];
303 for(int i=0;i<oneD;i++) {
308 uint64_t *SEQ=(uint64_t*)seq;
309 uint64_t *SEQ2=(uint64_t*)seq2;
319 for(int i=0;i<64;i++)
320 DK[i]=splitmix64_stateless(i);
325 rc4key(DK, Sbox1, 8);
326 rc4key(&DK[8], Sbox2, 8);
330 rc4keyperm(&DK[16], h, 1, Pbox, 16);
331 rc4keyperm(&DK[32], h, 1, Pbox2, 16);
334 // uint64_t plain[bufsize];
335 // uint64_t cipher[bufsize];
339 for(int i=0;i<h;i++) {
340 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
349 double t=TimeStart();
351 int nb_bloc=imsize/(h*8); //8 because we use 64bits numbers
355 for(uint iter=0;iter<nb_test;iter++) {
356 scprng(SEQ, SEQ2, h, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
359 double time=TimeStop(t);
360 printf("%e ",nb_test*nb_bloc*h*8/time);
363 for(int i=0;i<oneD;i++) {
365 data_G[i]=seq2[oneD+i];
366 data_B[i]=seq2[2*oneD+i];
368 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
372 //reinit of parameters
373 rc4key(DK, Sbox1, 8);
374 rc4key(&DK[8], Sbox2, 8);
376 rc4keyperm(&DK[16], h, 1, Pbox, 16);
377 rc4keyperm(&DK[32], h, 1, Pbox2, 16);
378 for(int i=0;i<h;i++) {
379 Val[Pbox[i]]=splitmix64_stateless(i+DK[i&63]);
385 for(uint iter=0;iter<nb_test;iter++) {
386 scprng(SEQ2, SEQ, h, nb_bloc, Val,Sbox1, Sbox2, Pbox, Pbox2, DK, delta);
391 printf("%e\n",nb_test*nb_bloc*h*8/time);
394 for(int i=0;i<oneD;i++) {
396 data_G[i]=seq[oneD+i];
397 data_B[i]=seq[2*oneD+i];
399 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
403 for(int i=0;i<imsize;i++) {
404 //cout<<(int)buffer[i]<<endl;
405 if(buffer[i]!=seq[i]) {
409 // cout<<"RESULT CORRECT: "<<equal<<endl;