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> readFile(int n, int k, int& sizeFile, int &lc) {
81 ifstream stream("/home/couturie/ajeter/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;
109 void buildFullRankIDA(Cube<byte>& IDAmat, Cube<short> &invIDAmat, int q, int n, byte* DK) {
116 prga(Sbox, q*n*n/4, RM1);
117 Cube<byte> rm1(RM1,1,1,q*n*n/4);
118 rm1.reshape(n/2,n/2,q);
126 // Construction of the IDA matrices
127 for(int i=0;i<q;i++) {
129 sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i);
130 sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye<Mat<byte>>(n/2,n/2);
131 sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye<Mat<byte>>(n/2,n/2);
132 sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i);
134 /* cout<<"sub "<<i<<endl;
138 mat toto=conv_to<mat>::from(sub);
139 cout<<det(toto)<<endl;
143 Mat<short> sub2(n,n);
144 Mat<short> rm2=conv_to<Mat<short>>::from(rm1.slice(i));
145 sub2.submat(0,0,n/2-1,n/2-1)=rm2;
146 sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye<Mat<short>>(n/2,n/2);
147 sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye<Mat<short>>(n/2,n/2);
148 sub2.submat(n/2,n/2,n-1,n-1)=rm2;
151 mat toto=conv_to<mat>::from(sub2);
152 cout<<det(toto)<<endl;
154 invIDAmat.slice(i)=sub2;
158 cout<<invIDAmat<<endl;
161 void buildKRankIDA(Cube<byte>& IDAmat, int q, int n, int k, byte* DK) {
168 prga(Sbox, q*n*k, RM1);
169 Cube<byte> rm1(RM1,1,1,q*n*k);
178 // Construction of the IDA matrices
179 for(int i=0;i<q;i++) {
181 sub.submat(0,0,n-1,k-1)=rm1.slice(i);
194 int main( int argc, char *argv[] ) {
210 byte Secretkey[key_size];
212 byte counter[key_size];
214 for(int i=0;i<key_size;i++) {
215 Secretkey[i]=lrand48()&0xFF;
216 counter[i]=lrand48()&0xFF;
221 for (int i = 0; i < key_size ; i++) {
222 DK[i]=Secretkey[i]^counter[i];
233 rc4keyperm(&DK[8], l/Tb, 1, PboxV, 16);
239 // Initialization of IDA and inverse IDA matrix
240 Cube<byte> IDAmat(n,n,q);
241 Cube<short> invIDAmat(n,n,q);
242 buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]);
247 Cube<byte> IDAmat(n,k,q);
248 buildKRankIDA(Cube<byte>& IDAmat, q, n, k, &DK[0]);
250 // rc4key(&DK[8], sc, 16);
257 arma_rng::set_seed(time(NULL));
262 Mat<byte> M=readFile(n,k,sizeFile,lc);
265 // data.load("/home/couturie/ajeter/lena.png");
267 /* Mat<byte> M=conv_to<Mat<byte>>::from(randi(k,l));
269 M=M - floor(M/255)*255;*/
278 Mat<byte> C=IDAmat.slice(0)*M;
279 M.save("M.bin",raw_binary);
280 // Mat<short> D=invIDAmat.slice(0)*C;
283 mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,k-1,n-1));
284 mat B=conv_to<mat>::from(C.submat(0,0,k-1,lc-1));
290 Mat<char> D2=conv_to<Mat<char>>::from(D);
293 // D2.save("D2.bin",raw_binary);
295 Row<char> d2=D2.row(0);
296 byte* vec=(byte*)d2.memptr();
298 FILE* pFile = fopen ("myfile.png", "wb");
299 fwrite (vec , sizeof(byte), sizeFile, pFile);