1 // g++ -std=c++11 -O4 -msse2 -msse3 -msse4 -fopenmp -O3 test_mat.cpp -o test_mat -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> readFile(int n, int k, int& sizeFile, int &lc) {
81 ifstream stream("/home/couturie/ajeter/lena.png", ios::in | ios::binary | ios::ate);
82 sizeFile=stream.tellg();
84 stream.seekg(0, ios::beg);
87 lc=ceil(double(sizeFile)/k);
90 vector<uint8_t> contents2(n*lc,0);
91 vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
92 copy ( contents.begin(), contents.end(), contents2.begin() );
97 Mat<byte> matData(&contents2[0],1,contents2.size());
99 cout << "file size: " << contents2.size() << endl;
101 matData.reshape(n,lc);
102 cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
107 int main( int argc, char *argv[] ) {
123 byte Secretkey[key_size];
125 byte counter[key_size];
127 for(int i=0;i<key_size;i++) {
128 Secretkey[i]=lrand48()&0xFF;
129 counter[i]=lrand48()&0xFF;
134 for (int i = 0; i < key_size ; i++) {
135 DK[i]=Secretkey[i]^counter[i];
144 prga(Sbox, q*n*n/4, RM1);
149 rc4keyperm(&DK[8], l/Tb, 1, PboxV, 16);
151 cout<<"rprree"<<endl;
152 Cube<byte> rm1(RM1,1,1,q*n*n/4);
153 rm1.reshape(n/2,n/2,q);
161 // Initialization of IDA and inverse IDA matrix
162 Cube<byte> IDAmat(n,n,q);
163 Cube<short> invIDAmat(n,n,q);
165 // Construction of the IDA matrices
166 for(int i=0;i<q;i++) {
168 sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i);
169 sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye<Mat<byte>>(n/2,n/2);
170 sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye<Mat<byte>>(n/2,n/2);
171 sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i);
173 /* cout<<"sub "<<i<<endl;
177 mat toto=conv_to<mat>::from(sub);
178 cout<<det(toto)<<endl;
182 Mat<short> sub2(n,n);
183 Mat<short> rm2=conv_to<Mat<short>>::from(rm1.slice(i));
184 sub2.submat(0,0,n/2-1,n/2-1)=rm2;
185 sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye<Mat<short>>(n/2,n/2);
186 sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye<Mat<short>>(n/2,n/2);
187 sub2.submat(n/2,n/2,n-1,n-1)=rm2;
190 mat toto=conv_to<mat>::from(sub2);
191 cout<<det(toto)<<endl;
193 invIDAmat.slice(i)=sub2;
198 cout<<invIDAmat<<endl;
199 // rc4key(&DK[8], sc, 16);
206 arma_rng::set_seed(time(NULL));
211 Mat<byte> M=readFile(n,k,sizeFile,lc);
214 // data.load("/home/couturie/ajeter/lena.png");
216 /* Mat<byte> M=conv_to<Mat<byte>>::from(randi(k,l));
218 M=M - floor(M/255)*255;*/
227 Mat<byte> C=IDAmat.slice(0)*M;
228 M.save("M.bin",raw_binary);
229 Mat<short> D=invIDAmat.slice(0)*C;
232 Mat<char> D2=conv_to<Mat<char>>::from(D);
235 // D2.save("D2.bin",raw_binary);
237 Row<char> d2=D2.row(0);
238 byte* vec=(byte*)d2.memptr();
240 FILE* pFile = fopen ("myfile.png", "wb");
241 fwrite (vec , sizeof(byte), sizeFile, pFile);