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"};
30 void display(mylong *mat, int r, int c) {
31 for(int i=0;i<r;i++) {
32 for(int j=0;j<c;j++) {
33 printf("%016llu ",LLUI mat[i*c+j]);
40 mylong *matrix_multiply(gf_t *gf, mylong *m1, mylong *m2, int r1, int c1, int r2, int c2, int w)
45 product = (mylong *) malloc(sizeof(mylong)*r1*c2);
46 for (i = 0; i < r1*c2; i++) product[i] = 0;
48 for (i = 0; i < r1; i++) {
49 for (j = 0; j < c2; j++) {
50 for (k = 0; k < r2; k++) {
51 product[i*c2+j] ^= gf->multiply.w64(gf,m1[i*c1+k], m2[k*c2+j]);
60 int invert_matrix(gf_t *gf, mylong *mat, mylong *inv, int rows)
62 int cols, i, j, k, x, rs2;
69 for (i = 0; i < rows; i++) {
70 for (j = 0; j < cols; j++) {
71 inv[k] = (i == j) ? 1 : 0;
75 // display(inv, rows, rows);
78 /* First -- convert into upper triangular */
79 for (i = 0; i < cols; i++) {
82 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
83 matrix was not invertible */
85 if (mat[row_start+i] == 0) {
86 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
87 if (j == rows) return -1;
89 for (k = 0; k < cols; k++) {
90 tmp = mat[row_start+k];
91 mat[row_start+k] = mat[rs2+k];
93 tmp = inv[row_start+k];
94 inv[row_start+k] = inv[rs2+k];
99 /* Multiply the row by 1/element i,i */
100 tmp = mat[row_start+i];
102 inverse = gf->divide.w64(gf,1, tmp);
103 for (j = 0; j < cols; j++) {
104 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
105 inv[row_start+j] = gf->multiply.w64(gf,inv[row_start+j], inverse);
109 /* Now for each j>i, add A_ji*Ai to Aj */
111 for (j = i+1; j != cols; j++) {
116 for (x = 0; x < cols; x++) {
117 mat[rs2+x] ^= mat[row_start+x];
118 inv[rs2+x] ^= inv[row_start+x];
123 for (x = 0; x < cols; x++) {
124 mat[rs2+x] ^= gf->multiply.w64(gf,tmp, mat[row_start+x]);
125 inv[rs2+x] ^= gf->multiply.w64(gf,tmp, inv[row_start+x]);
132 /* Now the matrix is upper triangular. Start at the top and multiply down */
134 for (i = rows-1; i >= 0; i--) {
136 for (j = 0; j < i; j++) {
138 if (mat[rs2+i] != 0) {
141 for (k = 0; k < cols; k++) {
142 inv[rs2+k] ^= gf->multiply.w64(gf,tmp, inv[row_start+k]);
149 display(mat, rows, rows);
152 display(inv, rows, rows);
161 int invertible_matrix(gf_t *gf, int *mat, int rows, int w)
163 int cols, i, j, k, x, rs2;
169 /* First -- convert into upper triangular */
170 for (i = 0; i < cols; i++) {
173 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
174 matrix was not invertible */
176 if (mat[row_start+i] == 0) {
177 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
178 if (j == rows) return 0;
180 for (k = 0; k < cols; k++) {
181 tmp = mat[row_start+k];
182 mat[row_start+k] = mat[rs2+k];
187 /* Multiply the row by 1/element i,i */
188 tmp = mat[row_start+i];
190 inverse = gf->divide.w64(gf,1, tmp);
191 for (j = 0; j < cols; j++) {
192 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
196 /* Now for each j>i, add A_ji*Ai to Aj */
198 for (j = i+1; j != cols; j++) {
203 for (x = 0; x < cols; x++) {
204 mat[rs2+x] ^= mat[row_start+x];
209 for (x = 0; x < cols; x++) {
210 mat[rs2+x] ^= gf->multiply.w64(gf,tmp,mat[row_start+x]);
223 mylong* readFullFile(char* filename,int n, int t, mylong& sizeFile, mylong & padded_size) {
225 ifstream stream(filename, ios::in | ios::binary | ios::ate);
226 // ifstream stream("lena_small.png", ios::in | ios::binary | ios::ate);
227 // ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate);
229 sizeFile=stream.tellg();
230 //cout << sizeFile << std::endl;
231 stream.seekg(0, ios::beg);
238 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
246 //make padding, we need to pad to be divisible by 8*t, we
247 if((sizeFile+8)%(8*t)!=0) {
248 // cout<<(int)(sizeFile/(8*t))<<endl;
250 int remainder=(8*t)*(1+(int)((sizeFile+8)/(8*t)))-sizeFile-8;
251 //cout << "remainder " << remainder << endl;
252 uint8_t vec[remainder];
253 for(int i=0;i<remainder;i++)
255 //add remainder elements at the end
256 contents.insert(contents.end(),vec,vec+remainder);
257 //add 8 elements at the beginning for the size
258 contents.insert(contents.begin(),vec,vec+8);
263 // cout << "res contents " << contents.size() << endl;
265 // for(int i=0;i<contents.size();i++)
266 // cout << (int)contents[i] << " ";
269 uint8_t *p_contents=&contents[0];
270 // mylong *p_contents2=reinterpret_cast<mylong*>(p_contents);
272 padded_size=contents.size()/8;
274 mylong *p_contents2=new mylong[padded_size];
275 memcpy(p_contents2,p_contents,sizeof(mylong)*padded_size);
276 //mylong *p_contents2=(mylong*)p_contents;
278 p_contents2[0]=sizeFile;
283 /* for(int i=0;i<padded_size;i++)
284 cout << p_contents2[i] << " ";
288 for(int i=8-1;i>=0;i--) {
293 cout << "convert val " << (long)res << endl;
296 for(int i=16-1;i>=8;i--) {
301 cout << "convert val " << (long)res << endl;
307 void sendChunk(string name,int cloud_id) {
309 ss <<"rclone copy "<<name<<" "<<cloud[cloud_id]<<":";
310 string str = ss.str();
321 void retrieveChunk(string name,int cloud_id) {
323 ss <<"rclone copy "<<cloud[cloud_id]<<":"<<name<<" .";
324 string str = ss.str();
330 void saveFile(uint8_t *data, const char *fileName,long size_file) {
331 // cout<<"size file "<<size_file<<endl;
332 FILE* pFile = fopen (fileName, "wb");
333 fwrite (data , sizeof(uint8_t), size_file, pFile);
339 void readFile(uint8_t *data, const char *fileName,long size_file) {
340 // cout<<"size file "<<size_file<<endl;
341 FILE* pFile = fopen (fileName, "rb");
343 fseek(pFile, 0L, SEEK_END);
344 int sz = ftell(pFile);
347 cout << "error : size of this chunk is not correct !! " << endl;
350 fread (data , sizeof(uint8_t), size_file, pFile);
356 void saveFile8(mylong *data, const char *fileName,long size_file) {
358 cout<<"size file 8 "<<size_file<<endl;
360 FILE* pFile = fopen (fileName, "wb");
364 fwrite (data , sizeof(mylong), size_file, pFile);
372 int main(int argc, char **argv)
383 char *filename=new char[1000];
389 for(int i=0;i<argc;i++) {
390 if(strncmp(argv[i],"t",1)==0)
391 t=atoi(&(argv[i][1]));
392 if(strncmp(argv[i],"n",1)==0)
393 n=atoi(&(argv[i][1]));
394 if(strncmp(argv[i],"f",1)==0)
395 strcpy(filename,&argv[i][1]);
400 cout<<"pb t>n"<<endl;
411 gf_init_easy(&gf, w);
415 matS=readFullFile(filename,n,t,sizeFile,padded_size);
417 // cout<<padded_size*8<<endl;
419 /* for(int i=0;i<padded_size;i++)
420 cout << matS[i] << " ";
424 int len=padded_size/t;
426 // cout<<" len "<<len<<" "<<padded_size<<endl;
428 // matS = malloc(sizeof(mylong)*t*len);
429 matG = malloc(sizeof(mylong)*t*n);
435 std::srand ( unsigned ( std::time(0) ) );
438 for(int i=0;i<t;i++) {
439 for(int j=0;j<len;j++) {
440 matS[i*len+j]=lrand48()<<32|lrand48();
445 for(int i=0;i<n;i++) {
446 for(int j=0;j<t;j++) {
447 matG[i*t+j]=lrand48()<<32|lrand48();
453 printf("Matrix S:\n");
454 // display(matS, len, t);
457 printf("Matrix G:\n");
462 auto start = std::chrono::system_clock::now();
463 matC=matrix_multiply(&gf, matG, matS, n, t, t, len, w);
464 auto end = std::chrono::system_clock::now();
465 std::chrono::duration<double> elapsed_seconds = end-start;
466 total_time+=elapsed_seconds.count();
467 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
469 // display(matC,t,t);
473 for(int i=0;i<n;i++) {
475 ss <<"file_"<<i<<".dat";
476 string str = ss.str();
477 saveFile((uint8_t*)&matC[i*len], str.c_str(),len*sizeof(mylong));
478 // sendChunk( str,i);
479 // th[i] = thread(sendChunk,str, i);
482 /* for(int i=0;i<n;i++) {
486 /* cout<<"sleep begin"<<endl;
488 cout<<"sleep end"<<endl;
492 mylong *matCs = malloc(sizeof(mylong)*t*len);
493 mylong *matGs = malloc(sizeof(mylong)*t*t);
496 std::vector<int> myvector;
499 for (int i=0; i<n; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
501 // using built-in random generator:
502 random_shuffle ( myvector.begin(), myvector.end() );
506 // std::cout << "random chunk" << std::endl;
511 for(int ii=0;ii<t;ii++) {
514 // std::cout << myvector[i] << " ";
518 ss <<"file_"<<i<<".dat";
519 string str = ss.str();
521 // retrieveChunk(str,i);
522 // th[ii] = thread(retrieveChunk,str, i);
528 for(int ii=0;ii<t;ii++) {
531 // std::cout << myvector[i] << " ";
535 ss <<"file_"<<i<<".dat";
536 string str = ss.str();
538 readFile((uint8_t*)&matCs[ind*len], str.c_str(),len*sizeof(mylong));
540 // display(&matCs[ind*len],1,1);
541 // display(&matC[i*len],1,1);
543 /*for(int j=0;j<len;j++) {
544 matCs[ind*len+j]=matC[i*len+j];
549 for(int j=0;j<t;j++) {
550 matGs[ind*t+j]=matG[i*t+j];
554 // std::cout << std::endl;
557 // printf("Matrix Gs:\n");
558 // display(matGs, t, t);
560 // printf("Matrix Cs:\n");
561 // display(matCs, t, len);
563 mylong* matGs_copy = malloc(sizeof(mylong)* t*t);
565 //WARNING invert changes the matrix
566 // invert = invertible_matrix(&gf,matGs, t, w);
567 // printf("\nInvertible Gs: %s\n", (invert == 1) ? "Yes" : "No");
569 memcpy(matGs_copy, matGs, sizeof(mylong)*t*t);
570 mylong *matInvGs = malloc(sizeof(mylong)*t*t);
571 invert_matrix(&gf, matGs_copy, matInvGs, t);
574 WARNING this changes matGs
575 identity=matrix_multiply(&gf, matInvGs, matGs, t, t, t, t, w);
576 printf("Identity:\n");
577 display(identity, t, t);
581 //printf("Matrix Gs:\n");
582 //display(matGs, t, t);
584 mylong *matS2 = malloc(sizeof(mylong)*t*len);
587 start = std::chrono::system_clock::now();
588 matS2=matrix_multiply(&gf, matInvGs, matCs, t, t, t, len, w);
589 end = std::chrono::system_clock::now();
590 elapsed_seconds = end-start;
591 // cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
592 total_time+=elapsed_seconds.count();
593 cout/*<<"TOTAL TIME : "*/<<total_time;
595 /* printf("Matrix S2:\n");
596 // display(matS2, len, t);
597 display(matS2, t, t);
600 for(int i=0;i<padded_size && equal;i++) {
601 equal=matS[i]==matS2[i];
606 // printf("EQUAL !!!\n");
609 mylong new_size=matS2[0];
610 // cout << "size of data " << new_size << endl;
613 //first elements that contains the size is removed
614 uint8_t *reconstucted_data=reinterpret_cast<uint8_t*>(&matS2[1]);
615 // saveFile(reconstucted_data, "file.dat",new_size);