11 #include <algorithm> // std::random_shuffle
12 #include <vector> // std::vector
17 typedef unsigned long mylong;
18 #define LLUI (long long unsigned int)
23 string cloud[5]={"dropboxida1","googleida1","megaida1","onedriveida1","pcloudida1"};
26 void display(mylong *mat, int r, int c) {
27 for(int i=0;i<r;i++) {
28 for(int j=0;j<c;j++) {
29 printf("%016llu ",LLUI mat[i*c+j]);
36 mylong *matrix_multiply(gf_t *gf, mylong *m1, mylong *m2, int r1, int c1, int r2, int c2, int w)
41 product = (mylong *) malloc(sizeof(mylong)*r1*c2);
42 for (i = 0; i < r1*c2; i++) product[i] = 0;
44 for (i = 0; i < r1; i++) {
45 for (j = 0; j < c2; j++) {
46 for (k = 0; k < r2; k++) {
47 product[i*c2+j] ^= gf->multiply.w64(gf,m1[i*c1+k], m2[k*c2+j]);
56 int invert_matrix(gf_t *gf, mylong *mat, mylong *inv, int rows)
58 int cols, i, j, k, x, rs2;
65 for (i = 0; i < rows; i++) {
66 for (j = 0; j < cols; j++) {
67 inv[k] = (i == j) ? 1 : 0;
71 // display(inv, rows, rows);
74 /* First -- convert into upper triangular */
75 for (i = 0; i < cols; i++) {
78 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
79 matrix was not invertible */
81 if (mat[row_start+i] == 0) {
82 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
83 if (j == rows) return -1;
85 for (k = 0; k < cols; k++) {
86 tmp = mat[row_start+k];
87 mat[row_start+k] = mat[rs2+k];
89 tmp = inv[row_start+k];
90 inv[row_start+k] = inv[rs2+k];
95 /* Multiply the row by 1/element i,i */
96 tmp = mat[row_start+i];
98 inverse = gf->divide.w64(gf,1, tmp);
99 for (j = 0; j < cols; j++) {
100 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
101 inv[row_start+j] = gf->multiply.w64(gf,inv[row_start+j], inverse);
105 /* Now for each j>i, add A_ji*Ai to Aj */
107 for (j = i+1; j != cols; j++) {
112 for (x = 0; x < cols; x++) {
113 mat[rs2+x] ^= mat[row_start+x];
114 inv[rs2+x] ^= inv[row_start+x];
119 for (x = 0; x < cols; x++) {
120 mat[rs2+x] ^= gf->multiply.w64(gf,tmp, mat[row_start+x]);
121 inv[rs2+x] ^= gf->multiply.w64(gf,tmp, inv[row_start+x]);
128 /* Now the matrix is upper triangular. Start at the top and multiply down */
130 for (i = rows-1; i >= 0; i--) {
132 for (j = 0; j < i; j++) {
134 if (mat[rs2+i] != 0) {
137 for (k = 0; k < cols; k++) {
138 inv[rs2+k] ^= gf->multiply.w64(gf,tmp, inv[row_start+k]);
145 display(mat, rows, rows);
148 display(inv, rows, rows);
157 int invertible_matrix(gf_t *gf, int *mat, int rows, int w)
159 int cols, i, j, k, x, rs2;
165 /* First -- convert into upper triangular */
166 for (i = 0; i < cols; i++) {
169 /* Swap rows if we ave a zero i,i element. If we can't swap, then the
170 matrix was not invertible */
172 if (mat[row_start+i] == 0) {
173 for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ;
174 if (j == rows) return 0;
176 for (k = 0; k < cols; k++) {
177 tmp = mat[row_start+k];
178 mat[row_start+k] = mat[rs2+k];
183 /* Multiply the row by 1/element i,i */
184 tmp = mat[row_start+i];
186 inverse = gf->divide.w64(gf,1, tmp);
187 for (j = 0; j < cols; j++) {
188 mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse);
192 /* Now for each j>i, add A_ji*Ai to Aj */
194 for (j = i+1; j != cols; j++) {
199 for (x = 0; x < cols; x++) {
200 mat[rs2+x] ^= mat[row_start+x];
205 for (x = 0; x < cols; x++) {
206 mat[rs2+x] ^= gf->multiply.w64(gf,tmp,mat[row_start+x]);
219 mylong* readFullFile(int n, int t, mylong& sizeFile, mylong & padded_size) {
221 ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
222 // ifstream stream("lena_small.png", ios::in | ios::binary | ios::ate);
223 // ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate);
225 sizeFile=stream.tellg();
226 std::cout << sizeFile << std::endl;
227 stream.seekg(0, ios::beg);
234 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
242 //make padding, we need to pad to be divisible by 8*t, we
243 if((sizeFile+8)%(8*t)!=0) {
244 cout<<(int)(sizeFile/(8*t))<<endl;
246 int remainder=(8*t)*(1+(int)((sizeFile+8)/(8*t)))-sizeFile-8;
247 cout << "remainder " << remainder << endl;
248 uint8_t vec[remainder];
249 for(int i=0;i<remainder;i++)
251 //add remainder elements at the end
252 contents.insert(contents.end(),vec,vec+remainder);
253 //add 8 elements at the beginning for the size
254 contents.insert(contents.begin(),vec,vec+8);
259 cout << "res contents " << contents.size() << endl;
261 // for(int i=0;i<contents.size();i++)
262 // cout << (int)contents[i] << " ";
265 uint8_t *p_contents=&contents[0];
266 // mylong *p_contents2=reinterpret_cast<mylong*>(p_contents);
268 padded_size=contents.size()/8;
270 mylong *p_contents2=new mylong[padded_size];
271 memcpy(p_contents2,p_contents,sizeof(mylong)*padded_size);
272 //mylong *p_contents2=(mylong*)p_contents;
274 p_contents2[0]=sizeFile;
279 /* for(int i=0;i<padded_size;i++)
280 cout << p_contents2[i] << " ";
284 for(int i=8-1;i>=0;i--) {
289 cout << "convert val " << (long)res << endl;
292 for(int i=16-1;i>=8;i--) {
297 cout << "convert val " << (long)res << endl;
303 void sendChunk(string name,int cloud_id) {
305 ss <<"rclone copy "<<name<<" "<<cloud[cloud_id]<<":";
306 string str = ss.str();
317 void retrieveChunk(string name,int cloud_id) {
319 ss <<"rclone copy "<<cloud[cloud_id]<<":"<<name<<" .";
320 string str = ss.str();
326 void saveFile(uint8_t *data, const char *fileName,long size_file) {
327 cout<<"size file "<<size_file<<endl;
328 FILE* pFile = fopen (fileName, "wb");
329 fwrite (data , sizeof(uint8_t), size_file, pFile);
335 void readFile(uint8_t *data, const char *fileName,long size_file) {
336 cout<<"size file "<<size_file<<endl;
337 FILE* pFile = fopen (fileName, "rb");
339 fseek(pFile, 0L, SEEK_END);
340 int sz = ftell(pFile);
343 cout << "error : size of this chunk is not correct !! " << endl;
346 fread (data , sizeof(uint8_t), size_file, pFile);
352 void saveFile8(mylong *data, const char *fileName,long size_file) {
354 cout<<"size file 8 "<<size_file<<endl;
356 FILE* pFile = fopen (fileName, "wb");
360 fwrite (data , sizeof(mylong), size_file, pFile);
368 int main(int argc, char **argv)
386 gf_init_easy(&gf, w);
390 matS=readFullFile(n,t,sizeFile,padded_size);
392 cout<<padded_size*8<<endl;
394 /* for(int i=0;i<padded_size;i++)
395 cout << matS[i] << " ";
399 int len=padded_size/t;
401 cout<<" len "<<len<<" "<<padded_size<<endl;
403 // matS = malloc(sizeof(mylong)*t*len);
404 matG = malloc(sizeof(mylong)*t*n);
410 std::srand ( unsigned ( std::time(0) ) );
413 for(int i=0;i<t;i++) {
414 for(int j=0;j<len;j++) {
415 matS[i*len+j]=lrand48()<<32|lrand48();
420 for(int i=0;i<n;i++) {
421 for(int j=0;j<t;j++) {
422 matG[i*t+j]=lrand48()<<32|lrand48();
428 printf("Matrix S:\n");
429 // display(matS, len, t);
432 printf("Matrix G:\n");
436 auto start = std::chrono::system_clock::now();
437 matC=matrix_multiply(&gf, matG, matS, n, t, t, len, w);
438 auto end = std::chrono::system_clock::now();
439 std::chrono::duration<double> elapsed_seconds = end-start;
440 std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
442 // display(matC,t,t);
446 for(int i=0;i<n;i++) {
448 ss <<"lena_"<<i<<".png";
449 string str = ss.str();
450 saveFile((uint8_t*)&matC[i*len], str.c_str(),len*sizeof(mylong));
456 mylong *matCs = malloc(sizeof(mylong)*t*len);
457 mylong *matGs = malloc(sizeof(mylong)*t*t);
460 std::vector<int> myvector;
463 for (int i=0; i<n; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
465 // using built-in random generator:
466 random_shuffle ( myvector.begin(), myvector.end() );
470 std::cout << "random chunk" << std::endl;
472 for(int ii=0;ii<t;ii++) {
473 // for(int i=n-1;i>=t;i--) {
474 // for(int i=0;i<n;i+=2) {
477 std::cout << myvector[i] << " ";
481 ss <<"lena_"<<i<<".png";
482 string str = ss.str();
484 retrieveChunk(str,i);
485 readFile((uint8_t*)&matCs[ind*len], str.c_str(),len*sizeof(mylong));
487 // display(&matCs[ind*len],1,1);
488 // display(&matC[i*len],1,1);
490 /*for(int j=0;j<len;j++) {
491 matCs[ind*len+j]=matC[i*len+j];
496 for(int j=0;j<t;j++) {
497 matGs[ind*t+j]=matG[i*t+j];
501 std::cout << std::endl;
504 printf("Matrix Gs:\n");
505 display(matGs, t, t);
507 // printf("Matrix Cs:\n");
508 // display(matCs, t, len);
510 mylong* matGs_copy = malloc(sizeof(mylong)* t*t);
512 //WARNING invert changes the matrix
513 // invert = invertible_matrix(&gf,matGs, t, w);
514 // printf("\nInvertible Gs: %s\n", (invert == 1) ? "Yes" : "No");
516 memcpy(matGs_copy, matGs, sizeof(mylong)*t*t);
517 mylong *matInvGs = malloc(sizeof(mylong)*t*t);
518 invert_matrix(&gf, matGs_copy, matInvGs, t);
521 WARNING this changes matGs
522 identity=matrix_multiply(&gf, matInvGs, matGs, t, t, t, t, w);
523 printf("Identity:\n");
524 display(identity, t, t);
528 //printf("Matrix Gs:\n");
529 //display(matGs, t, t);
531 mylong *matS2 = malloc(sizeof(mylong)*t*len);
534 start = std::chrono::system_clock::now();
535 matS2=matrix_multiply(&gf, matInvGs, matCs, t, t, t, len, w);
536 end = std::chrono::system_clock::now();
537 elapsed_seconds = end-start;
538 std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
541 printf("Matrix S2:\n");
542 // display(matS2, len, t);
543 display(matS2, t, t);
546 for(int i=0;i<padded_size && equal;i++) {
547 equal=matS[i]==matS2[i];
552 printf("EQUAL !!!\n");
555 mylong new_size=matS2[0];
556 cout << "size of data " << new_size << endl;
559 //first elements that contains the size is removed
560 uint8_t *reconstucted_data=reinterpret_cast<uint8_t*>(&matS2[1]);
561 saveFile(reconstucted_data, "lena2.png",new_size);