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
12 #include <iostream> // std::cout, std::fixed
20 typedef unsigned char byte;
23 void rc4key(byte *key, byte *sc, int size_DK) {
25 for(int i=0;i<256;i++) {
31 for(int i0=0; i0<256; i0++) {
32 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
40 void prga(byte *sc, int ldata, byte *r) {
44 for (int it=0; it<ldata; it++) {
45 i0 = ((i0+1)&0xFE); //%255);
46 j0 = (j0 + sc[i0])&0xFF;
50 r[it]=sc[(sc[i0]+sc[j0])&0xFF]%254+1;
55 void rc4keyperm(byte *key,int len, int rp,byte *sc, int size_DK) {
61 for (int i=0;i<len;i++) {
64 for (int it = 0; it < rp; it++) {
66 for(int i0 = 0; i0<len; i0++) {
67 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
77 Mat<byte> readFullFile(int n, int k, int& sizeFile, int &lc) {
79 // ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
80 ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate);
81 sizeFile=stream.tellg();
83 stream.seekg(0, ios::beg);
86 lc=ceil(double(sizeFile)/k);
89 vector<uint8_t> contents2(n*lc,0);
90 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
91 copy ( contents.begin(), contents.end(), contents2.begin() );
96 Mat<byte> matData(&contents2[0],1,contents2.size());
98 cout << "file size: " << contents2.size() << endl;
100 matData.reshape(n,lc);
101 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
106 Mat<byte> readPartialFile(int n, int k, int& sizeFile, int &lc) {
108 // ifstream stream("lena.png", ios::in | ios::binary | ios::ate);
109 ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate);
110 sizeFile=stream.tellg();
111 cout<<sizeFile<<endl;
112 stream.seekg(0, ios::beg);
115 lc=ceil(double(sizeFile)/k);
118 vector<uint8_t> contents2(k*lc,0);
119 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
120 copy ( contents.begin(), contents.end(), contents2.begin() );
125 Mat<byte> matData(&contents2[0],1,contents2.size());
127 cout << "file size: " << contents2.size() << endl;
129 matData.reshape(k,lc);
130 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
138 void buildFullRankIDA(Cube<byte>& IDAmat, Cube<short> &invIDAmat, int q, int n, byte* DK) {
145 prga(Sbox, q*n*n/4, RM1);
146 Cube<byte> rm1(RM1,1,1,q*n*n/4);
147 rm1.reshape(n/2,n/2,q);
149 /* cout<<"rm1"<<endl;
155 // Construction of the IDA matrices
156 for(int i=0;i<q;i++) {
158 sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i);
159 sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye<Mat<byte>>(n/2,n/2);
160 sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye<Mat<byte>>(n/2,n/2);
161 sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i);
163 /* cout<<"sub "<<i<<endl;
167 mat toto=conv_to<mat>::from(sub);
168 cout<<det(toto)<<endl;
172 Mat<short> sub2(n,n);
173 Mat<short> rm2=conv_to<Mat<short>>::from(rm1.slice(i));
174 sub2.submat(0,0,n/2-1,n/2-1)=rm2;
175 sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye<Mat<short>>(n/2,n/2);
176 sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye<Mat<short>>(n/2,n/2);
177 sub2.submat(n/2,n/2,n-1,n-1)=rm2;
180 mat toto=conv_to<mat>::from(sub2);
181 cout<<det(toto)<<endl;
183 invIDAmat.slice(i)=sub2;
186 //cout<<IDAmat<<endl;
187 //cout<<invIDAmat<<endl;
190 void buildPartialRankIDA(Cube<byte>& IDAmat, int q, int n, int k, byte* DK) {
197 prga(Sbox, q*n*k, RM1);
198 Cube<byte> rm1(RM1,1,1,q*n*k);
208 // Construction of the IDA matrices
209 for(int i=0;i<q;i++) {
211 sub.submat(0,0,n-1,k-1)=rm1.slice(i);
217 cout<<"det "<<i<<endl;
218 mat toto=conv_to<mat>::from(sub);
219 cout<<det(toto)<<endl;
223 // cout<<IDAmat<<endl;
229 int main( int argc, char *argv[] ) {
248 byte Secretkey[key_size];
250 byte counter[key_size];
252 for(int i=0;i<key_size;i++) {
253 Secretkey[i]=lrand48()&0xFF;
254 counter[i]=lrand48()&0xFF;
259 for (int i = 0; i < key_size ; i++) {
260 DK[i]=Secretkey[i]^counter[i];
271 rc4keyperm(&DK[8], l/Tb, 1, PboxV, 16);
282 M=readFullFile(n,k,sizeFile,lc);
284 M=readPartialFile(n,k,sizeFile,lc);
290 arma_rng::set_seed(time(NULL));
292 // Initialization of IDA and inverse IDA matrix
295 Cube<short> invIDAmat;
300 IDAmat.resize(n,n,q);
301 invIDAmat.resize(n,n,q);
302 buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]);
305 IDAmat.resize(n,k,q);
306 buildPartialRankIDA(IDAmat, q, n, k, &DK[0]);
314 // data.load("/home/couturie/ajeter/lena.png");
316 /* Mat<byte> M=conv_to<Mat<byte>>::from(randi(k,l));
318 M=M - floor(M/255)*255;*/
330 // C=IDAmat.slice(0)*M;
333 C=IDAmat.slice(0).submat(0,0,n-1,k-1)*M;
335 M.save("M.bin",raw_binary);
338 Mat<short> D=invIDAmat.slice(0)*C;
341 /*mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,n-1,n-1));
342 mat B=conv_to<mat>::from(C.submat(0,0,n-1,lc-1));
347 D2=conv_to<Mat<char>>::from(D);
352 IDAmat.resize(k,k,q);
353 invIDAmat.resize(k,k,q);
354 buildFullRankIDA(IDAmat, invIDAmat, q, k, &DK[0]);
357 mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,k-1,k-1));
359 mat B=conv_to<mat>::from(C.submat(0,0,k-1,lc-1));
365 // Mat<short> D=invIDAmat.slice(0)*C.submat(0,0,k-1,lc-1);
367 D2=conv_to<Mat<char>>::from(D);
376 // D2.save("D2.bin",raw_binary);
378 Row<char> d2=D2.row(0);
379 byte* vec=(byte*)d2.memptr();
381 FILE* pFile = fopen ("myfile.png", "wb");
382 fwrite (vec , sizeof(byte), sizeFile, pFile);