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, float *toto)
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]);
82 int invert_matrix(gf_t *gf, mylong *mat, mylong *inv, int rows)
84 int cols, i, j, k, x, rs2;
91 for (i = 0; i < rows; i++) {
92 for (j = 0; j < cols; j++) {
93 inv[k] = (i == j) ? 1 : 0;
97 // display(inv, rows, rows);
100 /* First -- convert into upper triangular */
101 for (i = 0; i < cols; i++) {
104 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
105 matrix was not invertible */
107 if (mat[row_start+i] == 0) {
108 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
109 if (j == rows) return -1;
111 for (k = 0; k < cols; k++) {
112 tmp = mat[row_start+k];
113 mat[row_start+k] = mat[rs2+k];
115 tmp = inv[row_start+k];
116 inv[row_start+k] = inv[rs2+k];
121 /* Multiply the row by 1/element i,i */
122 tmp = mat[row_start+i];
124 inverse = gf->divide.w64(gf,1, tmp);
125 for (j = 0; j < cols; j++) {
126 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
127 inv[row_start+j] = gf->multiply.w64(gf,inv[row_start+j], inverse);
131 /* Now for each j>i, add A_ji*Ai to Aj */
133 for (j = i+1; j != cols; j++) {
138 for (x = 0; x < cols; x++) {
139 mat[rs2+x] ^= mat[row_start+x];
140 inv[rs2+x] ^= inv[row_start+x];
145 for (x = 0; x < cols; x++) {
146 mat[rs2+x] ^= gf->multiply.w64(gf,tmp, mat[row_start+x]);
147 inv[rs2+x] ^= gf->multiply.w64(gf,tmp, inv[row_start+x]);
154 /* Now the matrix is upper triangular. Start at the top and multiply down */
156 for (i = rows-1; i >= 0; i--) {
158 for (j = 0; j < i; j++) {
160 if (mat[rs2+i] != 0) {
163 for (k = 0; k < cols; k++) {
164 inv[rs2+k] ^= gf->multiply.w64(gf,tmp, inv[row_start+k]);
171 display(mat, rows, rows);
174 display(inv, rows, rows);
183 int invertible_matrix(gf_t *gf, int *mat, int rows, int w)
185 int cols, i, j, k, x, rs2;
191 /* First -- convert into upper triangular */
192 for (i = 0; i < cols; i++) {
195 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
196 matrix was not invertible */
198 if (mat[row_start+i] == 0) {
199 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
200 if (j == rows) return 0;
202 for (k = 0; k < cols; k++) {
203 tmp = mat[row_start+k];
204 mat[row_start+k] = mat[rs2+k];
209 /* Multiply the row by 1/element i,i */
210 tmp = mat[row_start+i];
212 inverse = gf->divide.w64(gf,1, tmp);
213 for (j = 0; j < cols; j++) {
214 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
218 /* Now for each j>i, add A_ji*Ai to Aj */
220 for (j = i+1; j != cols; j++) {
225 for (x = 0; x < cols; x++) {
226 mat[rs2+x] ^= mat[row_start+x];
231 for (x = 0; x < cols; x++) {
232 mat[rs2+x] ^= gf->multiply.w64(gf,tmp,mat[row_start+x]);
245 mylong* readFullFile(char* filename,int n, int t, mylong& sizeFile, mylong & padded_size) {
247 ifstream stream(filename, ios::in | ios::binary | ios::ate);
248 // ifstream stream("lena_small.png", ios::in | ios::binary | ios::ate);
249 // ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate);
251 sizeFile=stream.tellg();
252 //cout << sizeFile << std::endl;
253 stream.seekg(0, ios::beg);
260 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
268 //make padding, we need to pad to be divisible by 8*t, we
269 if((sizeFile+8)%(8*t)!=0) {
270 // cout<<(int)(sizeFile/(8*t))<<endl;
272 int remainder=(8*t)*(1+(int)((sizeFile+8)/(8*t)))-sizeFile-8;
273 //cout << "remainder " << remainder << endl;
274 uint8_t vec[remainder];
275 for(int i=0;i<remainder;i++)
277 //add remainder elements at the end
278 contents.insert(contents.end(),vec,vec+remainder);
279 //add 8 elements at the beginning for the size
280 contents.insert(contents.begin(),vec,vec+8);
285 // cout << "res contents " << contents.size() << endl;
287 // for(int i=0;i<contents.size();i++)
288 // cout << (int)contents[i] << " ";
291 uint8_t *p_contents=&contents[0];
292 uint8_t *p_temp=new uint8_t[sizeFile];
294 memcpy(p_temp,p_contents,sizeof(uint8_t)*sizeFile);
301 for(int i=0;i<sizeDK;i++) {
305 int *Pbox=new int[sizeFile];
306 rc4keyperm(DK, sizeFile, 1, Pbox, 64);
308 for(int i=0;i<sizeFile;i++) {
309 p_contents[i]=p_temp[Pbox[i]];
315 // mylong *p_contents2=reinterpret_cast<mylong*>(p_contents);
317 padded_size=contents.size()/8;
319 mylong *p_contents2=new mylong[padded_size];
320 memcpy(p_contents2,p_contents,sizeof(mylong)*padded_size);
321 //mylong *p_contents2=(mylong*)p_contents;
323 p_contents2[0]=sizeFile;
328 /* for(int i=0;i<padded_size;i++)
329 cout << p_contents2[i] << " ";
333 for(int i=8-1;i>=0;i--) {
338 cout << "convert val " << (long)res << endl;
341 for(int i=16-1;i>=8;i--) {
346 cout << "convert val " << (long)res << endl;
352 void sendChunk(string name,int cloud_id) {
354 ss <<"rclone copy "<<name<<" "<<cloud[cloud_id]<<":";
355 string str = ss.str();
366 void retrieveChunk(string name,int cloud_id) {
368 ss <<"rclone copy "<<cloud[cloud_id]<<":"<<name<<" .";
369 string str = ss.str();
375 void saveFile(uint8_t *data, const char *fileName,long size_file) {
376 // cout<<"size file "<<size_file<<endl;
377 FILE* pFile = fopen (fileName, "wb");
378 fwrite (data , sizeof(uint8_t), size_file, pFile);
384 void readFile(uint8_t *data, const char *fileName,long size_file) {
385 // cout<<"size file "<<size_file<<endl;
386 FILE* pFile = fopen (fileName, "rb");
388 fseek(pFile, 0L, SEEK_END);
389 int sz = ftell(pFile);
392 cout << "error : size of this chunk is not correct !! " << endl;
395 fread (data , sizeof(uint8_t), size_file, pFile);
401 void saveFile8(mylong *data, const char *fileName,long size_file) {
403 cout<<"size file 8 "<<size_file<<endl;
405 FILE* pFile = fopen (fileName, "wb");
409 fwrite (data , sizeof(mylong), size_file, pFile);
418 int main(int argc, char **argv)
429 char *filename=new char[1000];
435 for(int i=0;i<argc;i++) {
436 if(strncmp(argv[i],"t",1)==0)
437 t=atoi(&(argv[i][1]));
438 if(strncmp(argv[i],"n",1)==0)
439 n=atoi(&(argv[i][1]));
440 if(strncmp(argv[i],"f",1)==0)
441 strcpy(filename,&argv[i][1]);
446 cout<<"pb t>n"<<endl;
457 gf_init_easy(&gf, w);
461 matS=readFullFile(filename,n,t,sizeFile,padded_size);
463 // cout<<padded_size*8<<endl;
465 /* for(int i=0;i<padded_size;i++)
466 cout << matS[i] << " ";
470 int len=padded_size/t;
472 // cout<<" len "<<len<<" "<<padded_size<<endl;
474 // matS = malloc(sizeof(mylong)*t*len);
475 matG = malloc(sizeof(mylong)*t*n);
481 std::srand ( unsigned ( std::time(0) ) );
484 for(int i=0;i<t;i++) {
485 for(int j=0;j<len;j++) {
486 matS[i*len+j]=lrand48()<<32|lrand48();
491 for(int i=0;i<n;i++) {
492 for(int j=0;j<t;j++) {
493 matG[i*t+j]=lrand48()<<32|lrand48();
499 printf("Matrix S:\n");
500 // display(matS, len, t);
503 printf("Matrix G:\n");
508 auto start = std::chrono::system_clock::now();
509 matC=matrix_multiply(&gf, matG, matS, n, t, t, len, w,0);
510 auto end = std::chrono::system_clock::now();
511 std::chrono::duration<double> elapsed_seconds = end-start;
512 total_time+=elapsed_seconds.count();
513 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
515 // display(matC,t,t);
519 for(int i=0;i<n;i++) {
521 ss <<"file_"<<i<<".dat";
522 string str = ss.str();
523 saveFile((uint8_t*)&matC[i*len], str.c_str(),len*sizeof(mylong));
524 // sendChunk( str,i);
525 // th[i] = thread(sendChunk,str, i);
528 /* for(int i=0;i<n;i++) {
532 /* cout<<"sleep begin"<<endl;
534 cout<<"sleep end"<<endl;
538 mylong *matCs = malloc(sizeof(mylong)*t*len);
539 mylong *matGs = malloc(sizeof(mylong)*t*t);
542 std::vector<int> myvector;
545 for (int i=0; i<n; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
547 // using built-in random generator:
548 random_shuffle ( myvector.begin(), myvector.end() );
552 // std::cout << "random chunk" << std::endl;
557 for(int ii=0;ii<t;ii++) {
560 // std::cout << myvector[i] << " ";
564 ss <<"file_"<<i<<".dat";
565 string str = ss.str();
567 // retrieveChunk(str,i);
568 // th[ii] = thread(retrieveChunk,str, i);
574 for(int ii=0;ii<t;ii++) {
577 // std::cout << myvector[i] << " ";
581 ss <<"file_"<<i<<".dat";
582 string str = ss.str();
584 readFile((uint8_t*)&matCs[ind*len], str.c_str(),len*sizeof(mylong));
586 // display(&matCs[ind*len],1,1);
587 // display(&matC[i*len],1,1);
589 /*for(int j=0;j<len;j++) {
590 matCs[ind*len+j]=matC[i*len+j];
595 for(int j=0;j<t;j++) {
596 matGs[ind*t+j]=matG[i*t+j];
600 // std::cout << std::endl;
603 // printf("Matrix Gs:\n");
604 // display(matGs, t, t);
606 // printf("Matrix Cs:\n");
607 // display(matCs, t, len);
609 mylong* matGs_copy = malloc(sizeof(mylong)* t*t);
611 //WARNING invert changes the matrix
612 // invert = invertible_matrix(&gf,matGs, t, w);
613 // printf("\nInvertible Gs: %s\n", (invert == 1) ? "Yes" : "No");
615 memcpy(matGs_copy, matGs, sizeof(mylong)*t*t);
616 mylong *matInvGs = malloc(sizeof(mylong)*t*t);
617 invert_matrix(&gf, matGs_copy, matInvGs, t);
620 WARNING this changes matGs
621 identity=matrix_multiply(&gf, matInvGs, matGs, t, t, t, t, w);
622 printf("Identity:\n");
623 display(identity, t, t);
627 //printf("Matrix Gs:\n");
628 //display(matGs, t, t);
630 mylong *matS2 = malloc(sizeof(mylong)*t*len);
633 start = std::chrono::system_clock::now();
635 for(int i=0;i<1000;i++) {
637 matS2=matrix_multiply(&gf, matInvGs, matCs, t, t, t, len, w, toto);
640 end = std::chrono::system_clock::now();
641 elapsed_seconds = end-start;
642 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
643 total_time+=elapsed_seconds.count();
644 cout/*<<"TOTAL TIME : "*/<<total_time<<" "<<toto;
646 /* printf("Matrix S2:\n");
647 // display(matS2, len, t);
648 display(matS2, t, t);
651 for(int i=0;i<padded_size && equal;i++) {
652 equal=matS[i]==matS2[i];
657 // printf("EQUAL !!!\n");
660 mylong new_size=matS2[0];
661 // cout << "size of data " << new_size << endl;
664 //first elements that contains the size is removed
665 uint8_t *reconstucted_data=reinterpret_cast<uint8_t*>(&matS2[1]);
666 // saveFile(reconstucted_data, "file.dat",new_size);