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(int n, int t, mylong& sizeFile, mylong & padded_size) {
225 ifstream stream("lena.png", 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 std::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)
390 gf_init_easy(&gf, w);
394 matS=readFullFile(n,t,sizeFile,padded_size);
396 cout<<padded_size*8<<endl;
398 /* for(int i=0;i<padded_size;i++)
399 cout << matS[i] << " ";
403 int len=padded_size/t;
405 cout<<" len "<<len<<" "<<padded_size<<endl;
407 // matS = malloc(sizeof(mylong)*t*len);
408 matG = malloc(sizeof(mylong)*t*n);
414 std::srand ( unsigned ( std::time(0) ) );
417 for(int i=0;i<t;i++) {
418 for(int j=0;j<len;j++) {
419 matS[i*len+j]=lrand48()<<32|lrand48();
424 for(int i=0;i<n;i++) {
425 for(int j=0;j<t;j++) {
426 matG[i*t+j]=lrand48()<<32|lrand48();
432 printf("Matrix S:\n");
433 // display(matS, len, t);
436 printf("Matrix G:\n");
440 auto start = std::chrono::system_clock::now();
441 matC=matrix_multiply(&gf, matG, matS, n, t, t, len, w);
442 auto end = std::chrono::system_clock::now();
443 std::chrono::duration<double> elapsed_seconds = end-start;
444 std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
446 // display(matC,t,t);
450 for(int i=0;i<n;i++) {
452 ss <<"lena_"<<i<<".png";
453 string str = ss.str();
454 saveFile((uint8_t*)&matC[i*len], str.c_str(),len*sizeof(mylong));
455 // sendChunk( str,i);
456 th[i] = thread(sendChunk,str, i);
459 for(int i=0;i<n;i++) {
463 /* cout<<"sleep begin"<<endl;
465 cout<<"sleep end"<<endl;
469 mylong *matCs = malloc(sizeof(mylong)*t*len);
470 mylong *matGs = malloc(sizeof(mylong)*t*t);
473 std::vector<int> myvector;
476 for (int i=0; i<n; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
478 // using built-in random generator:
479 random_shuffle ( myvector.begin(), myvector.end() );
483 std::cout << "random chunk" << std::endl;
488 for(int ii=0;ii<t;ii++) {
491 std::cout << myvector[i] << " ";
495 ss <<"lena_"<<i<<".png";
496 string str = ss.str();
498 // retrieveChunk(str,i);
499 th[ii] = thread(retrieveChunk,str, i);
505 for(int ii=0;ii<t;ii++) {
508 std::cout << myvector[i] << " ";
512 ss <<"lena_"<<i<<".png";
513 string str = ss.str();
515 readFile((uint8_t*)&matCs[ind*len], str.c_str(),len*sizeof(mylong));
517 // display(&matCs[ind*len],1,1);
518 // display(&matC[i*len],1,1);
520 /*for(int j=0;j<len;j++) {
521 matCs[ind*len+j]=matC[i*len+j];
526 for(int j=0;j<t;j++) {
527 matGs[ind*t+j]=matG[i*t+j];
531 std::cout << std::endl;
534 printf("Matrix Gs:\n");
535 display(matGs, t, t);
537 // printf("Matrix Cs:\n");
538 // display(matCs, t, len);
540 mylong* matGs_copy = malloc(sizeof(mylong)* t*t);
542 //WARNING invert changes the matrix
543 // invert = invertible_matrix(&gf,matGs, t, w);
544 // printf("\nInvertible Gs: %s\n", (invert == 1) ? "Yes" : "No");
546 memcpy(matGs_copy, matGs, sizeof(mylong)*t*t);
547 mylong *matInvGs = malloc(sizeof(mylong)*t*t);
548 invert_matrix(&gf, matGs_copy, matInvGs, t);
551 WARNING this changes matGs
552 identity=matrix_multiply(&gf, matInvGs, matGs, t, t, t, t, w);
553 printf("Identity:\n");
554 display(identity, t, t);
558 //printf("Matrix Gs:\n");
559 //display(matGs, t, t);
561 mylong *matS2 = malloc(sizeof(mylong)*t*len);
564 start = std::chrono::system_clock::now();
565 matS2=matrix_multiply(&gf, matInvGs, matCs, t, t, t, len, w);
566 end = std::chrono::system_clock::now();
567 elapsed_seconds = end-start;
568 std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
571 printf("Matrix S2:\n");
572 // display(matS2, len, t);
573 display(matS2, t, t);
576 for(int i=0;i<padded_size && equal;i++) {
577 equal=matS[i]==matS2[i];
582 printf("EQUAL !!!\n");
585 mylong new_size=matS2[0];
586 cout << "size of data " << new_size << endl;
589 //first elements that contains the size is removed
590 uint8_t *reconstucted_data=reinterpret_cast<uint8_t*>(&matS2[1]);
591 saveFile(reconstucted_data, "lena2.png",new_size);