11 #include <algorithm> // std::random_shuffle
12 #include <vector> // std::vector
21 typedef unsigned long mylong;
22 #define LLUI (long long unsigned int)
27 string cloud[5]={"dropboxida1","googleida1","onedriveida2","onedriveida1","pcloudida1"};
29 void rc4keyperm(uint8_t *key,int len, int rp,int *sc, int size_DK) {
35 for (int i=0;i<len;i++) {
38 for (int it = 0; it < rp; it++) {
40 for(int i0 = 0; i0<len; i0++) {
41 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
51 void display(mylong *mat, int r, int c) {
52 for(int i=0;i<r;i++) {
53 for(int j=0;j<c;j++) {
54 printf("%016llu ",LLUI mat[i*c+j]);
61 mylong *matrix_multiply(gf_t *gf, mylong *m1, mylong *m2, int r1, int c1, int r2, int c2, int w)
66 product = (mylong *) malloc(sizeof(mylong)*r1*c2);
67 for (i = 0; i < r1*c2; i++) product[i] = 0;
69 for (i = 0; i < r1; i++) {
70 for (j = 0; j < c2; j++) {
71 for (k = 0; k < r2; k++) {
72 product[i*c2+j] ^= gf->multiply.w64(gf,m1[i*c1+k], m2[k*c2+j]);
81 int invert_matrix(gf_t *gf, mylong *mat, mylong *inv, int rows)
83 int cols, i, j, k, x, rs2;
90 for (i = 0; i < rows; i++) {
91 for (j = 0; j < cols; j++) {
92 inv[k] = (i == j) ? 1 : 0;
96 // display(inv, rows, rows);
99 /* First -- convert into upper triangular */
100 for (i = 0; i < cols; i++) {
103 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
104 matrix was not invertible */
106 if (mat[row_start+i] == 0) {
107 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
108 if (j == rows) return -1;
110 for (k = 0; k < cols; k++) {
111 tmp = mat[row_start+k];
112 mat[row_start+k] = mat[rs2+k];
114 tmp = inv[row_start+k];
115 inv[row_start+k] = inv[rs2+k];
120 /* Multiply the row by 1/element i,i */
121 tmp = mat[row_start+i];
123 inverse = gf->divide.w64(gf,1, tmp);
124 for (j = 0; j < cols; j++) {
125 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
126 inv[row_start+j] = gf->multiply.w64(gf,inv[row_start+j], inverse);
130 /* Now for each j>i, add A_ji*Ai to Aj */
132 for (j = i+1; j != cols; j++) {
137 for (x = 0; x < cols; x++) {
138 mat[rs2+x] ^= mat[row_start+x];
139 inv[rs2+x] ^= inv[row_start+x];
144 for (x = 0; x < cols; x++) {
145 mat[rs2+x] ^= gf->multiply.w64(gf,tmp, mat[row_start+x]);
146 inv[rs2+x] ^= gf->multiply.w64(gf,tmp, inv[row_start+x]);
153 /* Now the matrix is upper triangular. Start at the top and multiply down */
155 for (i = rows-1; i >= 0; i--) {
157 for (j = 0; j < i; j++) {
159 if (mat[rs2+i] != 0) {
162 for (k = 0; k < cols; k++) {
163 inv[rs2+k] ^= gf->multiply.w64(gf,tmp, inv[row_start+k]);
170 display(mat, rows, rows);
173 display(inv, rows, rows);
182 int invertible_matrix(gf_t *gf, int *mat, int rows, int w)
184 int cols, i, j, k, x, rs2;
190 /* First -- convert into upper triangular */
191 for (i = 0; i < cols; i++) {
194 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
195 matrix was not invertible */
197 if (mat[row_start+i] == 0) {
198 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
199 if (j == rows) return 0;
201 for (k = 0; k < cols; k++) {
202 tmp = mat[row_start+k];
203 mat[row_start+k] = mat[rs2+k];
208 /* Multiply the row by 1/element i,i */
209 tmp = mat[row_start+i];
211 inverse = gf->divide.w64(gf,1, tmp);
212 for (j = 0; j < cols; j++) {
213 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
217 /* Now for each j>i, add A_ji*Ai to Aj */
219 for (j = i+1; j != cols; j++) {
224 for (x = 0; x < cols; x++) {
225 mat[rs2+x] ^= mat[row_start+x];
230 for (x = 0; x < cols; x++) {
231 mat[rs2+x] ^= gf->multiply.w64(gf,tmp,mat[row_start+x]);
244 mylong* readFullFile(char* filename,int n, int t, mylong& sizeFile, mylong & padded_size) {
246 ifstream stream(filename, ios::in | ios::binary | ios::ate);
247 // ifstream stream("lena_small.png", ios::in | ios::binary | ios::ate);
248 // ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate);
250 sizeFile=stream.tellg();
251 //cout << sizeFile << std::endl;
252 stream.seekg(0, ios::beg);
259 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
267 //make padding, we need to pad to be divisible by 8*t, we
268 if((sizeFile+8)%(8*t)!=0) {
269 // cout<<(int)(sizeFile/(8*t))<<endl;
271 int remainder=(8*t)*(1+(int)((sizeFile+8)/(8*t)))-sizeFile-8;
272 //cout << "remainder " << remainder << endl;
273 uint8_t vec[remainder];
274 for(int i=0;i<remainder;i++)
276 //add remainder elements at the end
277 contents.insert(contents.end(),vec,vec+remainder);
278 //add 8 elements at the beginning for the size
279 contents.insert(contents.begin(),vec,vec+8);
284 // cout << "res contents " << contents.size() << endl;
286 // for(int i=0;i<contents.size();i++)
287 // cout << (int)contents[i] << " ";
290 uint8_t *p_contents=&contents[0];
291 uint8_t *p_temp=new uint8_t[sizeFile];
293 memcpy(p_temp,p_contents,sizeof(uint8_t)*sizeFile);
300 for(int i=0;i<sizeDK;i++) {
304 int *Pbox=new int[sizeFile];
305 rc4keyperm(DK, sizeFile, 1, Pbox, 64);
307 for(int i=0;i<sizeFile;i++) {
308 p_contents[i]=p_temp[Pbox[i]];
314 // mylong *p_contents2=reinterpret_cast<mylong*>(p_contents);
316 padded_size=contents.size()/8;
318 mylong *p_contents2=new mylong[padded_size];
319 memcpy(p_contents2,p_contents,sizeof(mylong)*padded_size);
320 //mylong *p_contents2=(mylong*)p_contents;
322 p_contents2[0]=sizeFile;
327 /* for(int i=0;i<padded_size;i++)
328 cout << p_contents2[i] << " ";
332 for(int i=8-1;i>=0;i--) {
337 cout << "convert val " << (long)res << endl;
340 for(int i=16-1;i>=8;i--) {
345 cout << "convert val " << (long)res << endl;
351 void sendChunk(string name,int cloud_id) {
353 ss <<"rclone copy "<<name<<" "<<cloud[cloud_id]<<":";
354 string str = ss.str();
365 void retrieveChunk(string name,int cloud_id) {
367 ss <<"rclone copy "<<cloud[cloud_id]<<":"<<name<<" .";
368 string str = ss.str();
374 void saveFile(uint8_t *data, const char *fileName,long size_file) {
375 // cout<<"size file "<<size_file<<endl;
376 FILE* pFile = fopen (fileName, "wb");
377 fwrite (data , sizeof(uint8_t), size_file, pFile);
383 void readFile(uint8_t *data, const char *fileName,long size_file) {
384 // cout<<"size file "<<size_file<<endl;
385 FILE* pFile = fopen (fileName, "rb");
387 fseek(pFile, 0L, SEEK_END);
388 int sz = ftell(pFile);
391 cout << "error : size of this chunk is not correct !! " << endl;
394 fread (data , sizeof(uint8_t), size_file, pFile);
400 void saveFile8(mylong *data, const char *fileName,long size_file) {
402 cout<<"size file 8 "<<size_file<<endl;
404 FILE* pFile = fopen (fileName, "wb");
408 fwrite (data , sizeof(mylong), size_file, pFile);
416 int main(int argc, char **argv)
427 char *filename=new char[1000];
433 for(int i=0;i<argc;i++) {
434 if(strncmp(argv[i],"t",1)==0)
435 t=atoi(&(argv[i][1]));
436 if(strncmp(argv[i],"n",1)==0)
437 n=atoi(&(argv[i][1]));
438 if(strncmp(argv[i],"f",1)==0)
439 strcpy(filename,&argv[i][1]);
444 cout<<"pb t>n"<<endl;
455 gf_init_easy(&gf, w);
459 matS=readFullFile(filename,n,t,sizeFile,padded_size);
461 // cout<<padded_size*8<<endl;
463 /* for(int i=0;i<padded_size;i++)
464 cout << matS[i] << " ";
468 int len=padded_size/t;
470 // cout<<" len "<<len<<" "<<padded_size<<endl;
472 // matS = malloc(sizeof(mylong)*t*len);
473 matG = malloc(sizeof(mylong)*t*n);
479 std::srand ( unsigned ( std::time(0) ) );
482 for(int i=0;i<t;i++) {
483 for(int j=0;j<len;j++) {
484 matS[i*len+j]=lrand48()<<32|lrand48();
489 for(int i=0;i<n;i++) {
490 for(int j=0;j<t;j++) {
491 matG[i*t+j]=lrand48()<<32|lrand48();
497 printf("Matrix S:\n");
498 // display(matS, len, t);
501 printf("Matrix G:\n");
506 auto start = std::chrono::system_clock::now();
507 matC=matrix_multiply(&gf, matG, matS, n, t, t, len, w);
508 auto end = std::chrono::system_clock::now();
509 std::chrono::duration<double> elapsed_seconds = end-start;
510 total_time+=elapsed_seconds.count();
511 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
513 // display(matC,t,t);
517 for(int i=0;i<n;i++) {
519 ss <<"file_"<<i<<".dat";
520 string str = ss.str();
521 saveFile((uint8_t*)&matC[i*len], str.c_str(),len*sizeof(mylong));
522 // sendChunk( str,i);
523 // th[i] = thread(sendChunk,str, i);
526 /* for(int i=0;i<n;i++) {
530 /* cout<<"sleep begin"<<endl;
532 cout<<"sleep end"<<endl;
536 mylong *matCs = malloc(sizeof(mylong)*t*len);
537 mylong *matGs = malloc(sizeof(mylong)*t*t);
540 std::vector<int> myvector;
543 for (int i=0; i<n; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
545 // using built-in random generator:
546 random_shuffle ( myvector.begin(), myvector.end() );
550 // std::cout << "random chunk" << std::endl;
555 for(int ii=0;ii<t;ii++) {
558 // std::cout << myvector[i] << " ";
562 ss <<"file_"<<i<<".dat";
563 string str = ss.str();
565 // retrieveChunk(str,i);
566 // th[ii] = thread(retrieveChunk,str, i);
572 for(int ii=0;ii<t;ii++) {
575 // std::cout << myvector[i] << " ";
579 ss <<"file_"<<i<<".dat";
580 string str = ss.str();
582 readFile((uint8_t*)&matCs[ind*len], str.c_str(),len*sizeof(mylong));
584 // display(&matCs[ind*len],1,1);
585 // display(&matC[i*len],1,1);
587 /*for(int j=0;j<len;j++) {
588 matCs[ind*len+j]=matC[i*len+j];
593 for(int j=0;j<t;j++) {
594 matGs[ind*t+j]=matG[i*t+j];
598 // std::cout << std::endl;
601 // printf("Matrix Gs:\n");
602 // display(matGs, t, t);
604 // printf("Matrix Cs:\n");
605 // display(matCs, t, len);
607 mylong* matGs_copy = malloc(sizeof(mylong)* t*t);
609 //WARNING invert changes the matrix
610 // invert = invertible_matrix(&gf,matGs, t, w);
611 // printf("\nInvertible Gs: %s\n", (invert == 1) ? "Yes" : "No");
613 memcpy(matGs_copy, matGs, sizeof(mylong)*t*t);
614 mylong *matInvGs = malloc(sizeof(mylong)*t*t);
615 invert_matrix(&gf, matGs_copy, matInvGs, t);
618 WARNING this changes matGs
619 identity=matrix_multiply(&gf, matInvGs, matGs, t, t, t, t, w);
620 printf("Identity:\n");
621 display(identity, t, t);
625 //printf("Matrix Gs:\n");
626 //display(matGs, t, t);
628 mylong *matS2 = malloc(sizeof(mylong)*t*len);
631 start = std::chrono::system_clock::now();
632 for(int i=0;i<1000;i++) {
634 matS2=matrix_multiply(&gf, matInvGs, matCs, tt, tt, tt, len, w);
636 end = std::chrono::system_clock::now();
637 elapsed_seconds = end-start;
638 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
639 total_time+=elapsed_seconds.count();
640 cout/*<<"TOTAL TIME : "*/<<total_time;
642 /* printf("Matrix S2:\n");
643 // display(matS2, len, t);
644 display(matS2, t, t);
647 for(int i=0;i<padded_size && equal;i++) {
648 equal=matS[i]==matS2[i];
653 // printf("EQUAL !!!\n");
656 mylong new_size=matS2[0];
657 // cout << "size of data " << new_size << endl;
660 //first elements that contains the size is removed
661 uint8_t *reconstucted_data=reinterpret_cast<uint8_t*>(&matS2[1]);
662 // saveFile(reconstucted_data, "file.dat",new_size);