1 // g++ -std=c++11 -O4 -msse2 -msse3 -msse4 -fopenmp -O3 test_mat2.cpp -o test_mat2 -I /home/couturie/tools/armadillo-6.200.5/include/ -lc -lm -lpthread -lgfortran -DMAX_STACK_ALLOC=2048 -Wall -m64 -DF_INTERFACE_GFORT -fPIC -DSMP_SERVER -DNO_WARMUP -DMAX_CPU_NUMBER=8 -DNO_AFFINITY -UCOMPLEX -DDOUBLE -I/home/couturie/tools/openblas/include -I/home/couturie/Downloads/OpenBLAS-0.2.15/ /home/couturie/tools/openblas/lib/libopenblas_haswellp-r0.2.15.a
14 #include <iostream> // std::cout, std::fixed
22 typedef unsigned char byte;
25 void rc4key(byte *key, byte *sc, int size_DK) {
27 for(int i=0;i<256;i++) {
33 for(int i0=0; i0<256; i0++) {
34 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
42 void prga(byte *sc, int ldata, byte *r) {
46 for (int it=0; it<ldata; it++) {
47 i0 = ((i0+1)&0xFE); //%255);
48 j0 = (j0 + sc[i0])&0xFF;
52 r[it]=sc[(sc[i0]+sc[j0])&0xFF]%254+1;
57 void rc4keyperm(byte *key,int len, int rp,byte *sc, int size_DK) {
63 for (int i=0;i<len;i++) {
66 for (int it = 0; it < rp; it++) {
68 for(int i0 = 0; i0<len; i0++) {
69 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
79 Mat<byte> readFullFile(int n, int k, int& sizeFile, int &lc) {
81 // ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
82 ifstream stream("/home/couturie/ajeter/lena_small.png", ios::in | ios::binary | ios::ate);
83 sizeFile=stream.tellg();
85 stream.seekg(0, ios::beg);
88 lc=ceil(double(sizeFile)/k);
91 vector<uint8_t> contents2(n*lc,0);
92 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
93 copy ( contents.begin(), contents.end(), contents2.begin() );
98 Mat<byte> matData(&contents2[0],1,contents2.size());
100 cout << "file size: " << contents2.size() << endl;
102 matData.reshape(n,lc);
103 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
108 Mat<byte> readPartialFile(int n, int k, int& sizeFile, int &lc) {
110 // ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
111 ifstream stream("/home/couturie/ajeter/lena_small.png", ios::in | ios::binary | ios::ate);
112 sizeFile=stream.tellg();
113 cout<<sizeFile<<endl;
114 stream.seekg(0, ios::beg);
117 lc=ceil(double(sizeFile)/k);
120 vector<uint8_t> contents2(k*lc,0);
121 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
122 copy ( contents.begin(), contents.end(), contents2.begin() );
127 Mat<byte> matData(&contents2[0],1,contents2.size());
129 cout << "file size: " << contents2.size() << endl;
131 matData.reshape(k,lc);
132 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
140 void buildFullRankIDA(Cube<byte>& IDAmat, Cube<short> &invIDAmat, int q, int n, byte* DK) {
147 prga(Sbox, q*n*n/4, RM1);
148 Cube<byte> rm1(RM1,1,1,q*n*n/4);
149 rm1.reshape(n/2,n/2,q);
157 // Construction of the IDA matrices
158 for(int i=0;i<q;i++) {
160 sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i);
161 sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye<Mat<byte>>(n/2,n/2);
162 sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye<Mat<byte>>(n/2,n/2);
163 sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i);
165 /* cout<<"sub "<<i<<endl;
169 mat toto=conv_to<mat>::from(sub);
170 cout<<det(toto)<<endl;
174 Mat<short> sub2(n,n);
175 Mat<short> rm2=conv_to<Mat<short>>::from(rm1.slice(i));
176 sub2.submat(0,0,n/2-1,n/2-1)=rm2;
177 sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye<Mat<short>>(n/2,n/2);
178 sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye<Mat<short>>(n/2,n/2);
179 sub2.submat(n/2,n/2,n-1,n-1)=rm2;
182 mat toto=conv_to<mat>::from(sub2);
183 cout<<det(toto)<<endl;
185 invIDAmat.slice(i)=sub2;
189 cout<<invIDAmat<<endl;
192 void buildPartialRankIDA(Cube<byte>& IDAmat, int q, int n, int k, byte* DK) {
199 prga(Sbox, q*n*k, RM1);
200 Cube<byte> rm1(RM1,1,1,q*n*k);
210 // Construction of the IDA matrices
211 for(int i=0;i<q;i++) {
213 sub.submat(0,0,n-1,k-1)=rm1.slice(i);
219 cout<<"det "<<i<<endl;
220 mat toto=conv_to<mat>::from(sub);
221 cout<<det(toto)<<endl;
231 int main( int argc, char *argv[] ) {
250 byte Secretkey[key_size];
252 byte counter[key_size];
254 for(int i=0;i<key_size;i++) {
255 Secretkey[i]=lrand48()&0xFF;
256 counter[i]=lrand48()&0xFF;
261 for (int i = 0; i < key_size ; i++) {
262 DK[i]=Secretkey[i]^counter[i];
273 rc4keyperm(&DK[8], l/Tb, 1, PboxV, 16);
284 M=readFullFile(n,k,sizeFile,lc);
286 M=readPartialFile(n,k,sizeFile,lc);
292 arma_rng::set_seed(time(NULL));
294 // Initialization of IDA and inverse IDA matrix
297 Cube<short> invIDAmat;
301 IDAmat.resize(n,n,q);
302 invIDAmat.resize(n,n,q);
303 buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]);
306 IDAmat.resize(n,k,q);
307 buildPartialRankIDA(IDAmat, q, n, k, &DK[0]);
315 // data.load("/home/couturie/ajeter/lena.png");
317 /* Mat<byte> M=conv_to<Mat<byte>>::from(randi(k,l));
319 M=M - floor(M/255)*255;*/
331 M.save("M.bin",raw_binary);
334 //Mat<short> D=invIDAmat.slice(0)*C;
337 mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,n-1,n-1));
338 mat B=conv_to<mat>::from(C.submat(0,0,n-1,lc-1));
343 D2=conv_to<Mat<char>>::from(D);
346 mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,n-1,k-1));
347 mat B=conv_to<mat>::from(C.submat(0,0,n-1,lc-1));
351 D2=conv_to<Mat<char>>::from(D);
359 // D2.save("D2.bin",raw_binary);
361 Row<char> d2=D2.row(0);
362 byte* vec=(byte*)d2.memptr();
364 FILE* pFile = fopen ("myfile.png", "wb");
365 fwrite (vec , sizeof(byte), sizeFile, pFile);