+// 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
+
+
+
+#include <armadillo>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <iostream>
+#include <fstream>
+#include <vector>
+
+
+#include <iostream> // std::cout, std::fixed
+#include <iomanip>
+#include <math.h>
+
+using namespace arma;
+using namespace std;
+int key_size=256;
+
+typedef unsigned char byte;
+
+
+void rc4key(byte *key, byte *sc, int size_DK) {
+
+ for(int i=0;i<256;i++) {
+ sc[i]=i;
+ }
+
+
+ byte j0 = 0;
+ for(int i0=0; i0<256; i0++) {
+ j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
+ byte tmp = sc[i0];
+ sc[i0] = sc[j0 ];
+ sc[j0] = tmp;
+ }
+}
+
+
+void prga(byte *sc, int ldata, byte *r) {
+ byte i0=0;
+ byte j0=0;
+
+ for (int it=0; it<ldata; it++) {
+ i0 = ((i0+1)&0xFE); //%255);
+ j0 = (j0 + sc[i0])&0xFF;
+ byte tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ r[it]=sc[(sc[i0]+sc[j0])&0xFF]%254+1;
+ }
+}
+
+
+void rc4keyperm(byte *key,int len, int rp,byte *sc, int size_DK) {
+
+ //sc=1:len;
+
+
+
+ for (int i=0;i<len;i++) {
+ sc[i]=i;
+ }
+ for (int it = 0; it < rp; it++) {
+ int j0 = 1;
+ for(int i0 = 0; i0<len; i0++) {
+ j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
+ int tmp = sc[i0];
+ sc[i0] = sc[j0];
+ sc[j0] = tmp;
+ }
+
+ }
+}
+
+
+Mat<byte> readFile(int n, int k, int& sizeFile, int &lc) {
+
+ ifstream stream("/home/couturie/ajeter/lena.png", ios::in | ios::binary | ios::ate);
+// ifstream stream("/home/couturie/ajeter/lena_small.png", ios::in | ios::binary | ios::ate);
+ sizeFile=stream.tellg();
+ cout<<sizeFile<<endl;
+ stream.seekg(0, ios::beg);
+
+
+ lc=ceil(double(sizeFile)/k);
+ cout<<lc<<endl;
+
+ vector<uint8_t> contents2(n*lc,0);
+ vector<uint8_t> contents((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
+ copy ( contents.begin(), contents.end(), contents2.begin() );
+
+
+
+
+ Mat<byte> matData(&contents2[0],1,contents2.size());
+
+ cout << "file size: " << contents2.size() << endl;
+
+ matData.reshape(n,lc);
+ cout<<matData.n_rows<<" "<<matData.n_cols<<endl;
+ return matData;
+}
+
+
+
+void buildFullRankIDA(Cube<byte>& IDAmat, Cube<short> &invIDAmat, int q, int n, byte* DK) {
+
+ byte Sbox[256];
+ byte RM1[q*n*n/4];
+
+
+ rc4key(DK, Sbox, 8);
+ prga(Sbox, q*n*n/4, RM1);
+ Cube<byte> rm1(RM1,1,1,q*n*n/4);
+ rm1.reshape(n/2,n/2,q);
+
+ cout<<"rm1"<<endl;
+ cout<<rm1<<endl;
+
+
+
+
+// Construction of the IDA matrices
+ for(int i=0;i<q;i++) {
+ Mat<byte> sub(n,n);
+ sub.submat(0,0,n/2-1,n/2-1)=rm1.slice(i);
+ sub.submat(0,n/2,n/2-1,n-1)=rm1.slice(i)+eye<Mat<byte>>(n/2,n/2);
+ sub.submat(n/2,0,n-1,n/2-1)=rm1.slice(i)-eye<Mat<byte>>(n/2,n/2);
+ sub.submat(n/2,n/2,n-1,n-1)=rm1.slice(i);
+
+/* cout<<"sub "<<i<<endl;
+ cout<<sub<<endl;
+
+ cout<<"det"<<endl;
+ mat toto=conv_to<mat>::from(sub);
+ cout<<det(toto)<<endl;
+*/
+ IDAmat.slice(i)=sub;
+
+ Mat<short> sub2(n,n);
+ Mat<short> rm2=conv_to<Mat<short>>::from(rm1.slice(i));
+ sub2.submat(0,0,n/2-1,n/2-1)=rm2;
+ sub2.submat(0,n/2,n/2-1,n-1)=-rm2-eye<Mat<short>>(n/2,n/2);
+ sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye<Mat<short>>(n/2,n/2);
+ sub2.submat(n/2,n/2,n-1,n-1)=rm2;
+
+ cout<<"det"<<endl;
+ mat toto=conv_to<mat>::from(sub2);
+ cout<<det(toto)<<endl;
+
+ invIDAmat.slice(i)=sub2;
+
+ }
+ cout<<IDAmat<<endl;
+ cout<<invIDAmat<<endl;
+}
+
+
+int main( int argc, char *argv[] ) {
+
+
+ int q=2;
+ int n=4;
+ int k=4;
+ int Tb=64;
+ int l=10;//399*Tb;
+
+
+
+ int key_size=32;
+ int seed=time(NULL);
+ cout<<seed<<endl;
+ srand48(seed);
+
+ byte Secretkey[key_size];
+
+ byte counter[key_size];
+
+ for(int i=0;i<key_size;i++) {
+ Secretkey[i]=lrand48()&0xFF;
+ counter[i]=lrand48()&0xFF;
+ }
+
+
+ byte DK[key_size];
+ for (int i = 0; i < key_size ; i++) {
+ DK[i]=Secretkey[i]^counter[i];
+ }
+
+ byte sc[256];
+
+
+
+
+
+ byte PboxV[l/Tb];
+
+ rc4keyperm(&DK[8], l/Tb, 1, PboxV, 16);
+
+
+
+
+
+// Initialization of IDA and inverse IDA matrix
+ Cube<byte> IDAmat(n,n,q);
+ Cube<short> invIDAmat(n,n,q);
+ buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]);
+
+
+ // rc4key(&DK[8], sc, 16);
+
+
+
+
+
+
+ arma_rng::set_seed(time(NULL));
+
+
+ int lc;
+ int sizeFile;
+ Mat<byte> M=readFile(n,k,sizeFile,lc);
+
+// Ma<byte> data;
+// data.load("/home/couturie/ajeter/lena.png");
+
+ /* Mat<byte> M=conv_to<Mat<byte>>::from(randi(k,l));
+ M=abs(M);
+ M=M - floor(M/255)*255;*/
+
+/* cout<<"M"<<endl;
+ cout<<M<<endl;
+ cout<<"C"<<endl;
+ cout<<C<<endl;
+*/
+
+
+ Mat<byte> C=IDAmat.slice(0)*M;
+ M.save("M.bin",raw_binary);
+// Mat<short> D=invIDAmat.slice(0)*C;
+
+
+ mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,k-1,n-1));
+ mat B=conv_to<mat>::from(C.submat(0,0,k-1,lc-1));
+
+ mat D=solve(A,B);
+
+
+
+ Mat<char> D2=conv_to<Mat<char>>::from(D);
+// cout<<"D2"<<endl;
+// cout<<D2<<endl;
+// D2.save("D2.bin",raw_binary);
+ D2.reshape(1,n*lc);
+ Row<char> d2=D2.row(0);
+ byte* vec=(byte*)d2.memptr();
+
+ FILE* pFile = fopen ("myfile.png", "wb");
+ fwrite (vec , sizeof(byte), sizeFile, pFile);
+ fclose (pFile);
+}