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);
415 #define cli() __asm volatile( "cli" ::: "memory" )
416 #define sei() __asm volatile( "sei" ::: "memory" )
419 int main(int argc, char **argv)
430 char *filename=new char[1000];
436 for(int i=0;i<argc;i++) {
437 if(strncmp(argv[i],"t",1)==0)
438 t=atoi(&(argv[i][1]));
439 if(strncmp(argv[i],"n",1)==0)
440 n=atoi(&(argv[i][1]));
441 if(strncmp(argv[i],"f",1)==0)
442 strcpy(filename,&argv[i][1]);
447 cout<<"pb t>n"<<endl;
458 gf_init_easy(&gf, w);
462 matS=readFullFile(filename,n,t,sizeFile,padded_size);
464 // cout<<padded_size*8<<endl;
466 /* for(int i=0;i<padded_size;i++)
467 cout << matS[i] << " ";
471 int len=padded_size/t;
473 // cout<<" len "<<len<<" "<<padded_size<<endl;
475 // matS = malloc(sizeof(mylong)*t*len);
476 matG = malloc(sizeof(mylong)*t*n);
482 std::srand ( unsigned ( std::time(0) ) );
485 for(int i=0;i<t;i++) {
486 for(int j=0;j<len;j++) {
487 matS[i*len+j]=lrand48()<<32|lrand48();
492 for(int i=0;i<n;i++) {
493 for(int j=0;j<t;j++) {
494 matG[i*t+j]=lrand48()<<32|lrand48();
500 printf("Matrix S:\n");
501 // display(matS, len, t);
504 printf("Matrix G:\n");
509 auto start = std::chrono::system_clock::now();
510 matC=matrix_multiply(&gf, matG, matS, n, t, t, len, w);
511 auto end = std::chrono::system_clock::now();
512 std::chrono::duration<double> elapsed_seconds = end-start;
513 total_time+=elapsed_seconds.count();
514 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
516 // display(matC,t,t);
520 for(int i=0;i<n;i++) {
522 ss <<"file_"<<i<<".dat";
523 string str = ss.str();
524 saveFile((uint8_t*)&matC[i*len], str.c_str(),len*sizeof(mylong));
525 // sendChunk( str,i);
526 // th[i] = thread(sendChunk,str, i);
529 /* for(int i=0;i<n;i++) {
533 /* cout<<"sleep begin"<<endl;
535 cout<<"sleep end"<<endl;
539 mylong *matCs = malloc(sizeof(mylong)*t*len);
540 mylong *matGs = malloc(sizeof(mylong)*t*t);
543 std::vector<int> myvector;
546 for (int i=0; i<n; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
548 // using built-in random generator:
549 random_shuffle ( myvector.begin(), myvector.end() );
553 // std::cout << "random chunk" << std::endl;
558 for(int ii=0;ii<t;ii++) {
561 // std::cout << myvector[i] << " ";
565 ss <<"file_"<<i<<".dat";
566 string str = ss.str();
568 // retrieveChunk(str,i);
569 // th[ii] = thread(retrieveChunk,str, i);
575 for(int ii=0;ii<t;ii++) {
578 // std::cout << myvector[i] << " ";
582 ss <<"file_"<<i<<".dat";
583 string str = ss.str();
585 readFile((uint8_t*)&matCs[ind*len], str.c_str(),len*sizeof(mylong));
587 // display(&matCs[ind*len],1,1);
588 // display(&matC[i*len],1,1);
590 /*for(int j=0;j<len;j++) {
591 matCs[ind*len+j]=matC[i*len+j];
596 for(int j=0;j<t;j++) {
597 matGs[ind*t+j]=matG[i*t+j];
601 // std::cout << std::endl;
604 // printf("Matrix Gs:\n");
605 // display(matGs, t, t);
607 // printf("Matrix Cs:\n");
608 // display(matCs, t, len);
610 mylong* matGs_copy = malloc(sizeof(mylong)* t*t);
612 //WARNING invert changes the matrix
613 // invert = invertible_matrix(&gf,matGs, t, w);
614 // printf("\nInvertible Gs: %s\n", (invert == 1) ? "Yes" : "No");
616 memcpy(matGs_copy, matGs, sizeof(mylong)*t*t);
617 mylong *matInvGs = malloc(sizeof(mylong)*t*t);
618 invert_matrix(&gf, matGs_copy, matInvGs, t);
621 WARNING this changes matGs
622 identity=matrix_multiply(&gf, matInvGs, matGs, t, t, t, t, w);
623 printf("Identity:\n");
624 display(identity, t, t);
628 //printf("Matrix Gs:\n");
629 //display(matGs, t, t);
631 mylong *matS2 = malloc(sizeof(mylong)*t*len);
634 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);
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;
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);