X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/blobdiff_plain/70b995d30a4c75869c4223dbb931802246f52d94..d8de23027e190ab94ece38a9411fefb995bab460:/IDA/test_mat2.cpp?ds=inline diff --git a/IDA/test_mat2.cpp b/IDA/test_mat2.cpp index e09acbd..474c572 100644 --- a/IDA/test_mat2.cpp +++ b/IDA/test_mat2.cpp @@ -9,8 +9,6 @@ #include #include #include - - #include // std::cout, std::fixed #include #include @@ -76,10 +74,10 @@ void rc4keyperm(byte *key,int len, int rp,byte *sc, int size_DK) { } -Mat readFile(int n, int k, int& sizeFile, int &lc) { +Mat readFullFile(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); +// ifstream stream("lena.png", ios::in | ios::binary | ios::ate); + ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate); sizeFile=stream.tellg(); cout< readFile(int n, int k, int& sizeFile, int &lc) { } +Mat readPartialFile(int n, int k, int& sizeFile, int &lc) { + +// ifstream stream("lena.png", ios::in | ios::binary | ios::ate); + ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate); + sizeFile=stream.tellg(); + cout< contents2(k*lc,0); + vector contents((istreambuf_iterator(stream)), istreambuf_iterator()); + copy ( contents.begin(), contents.end(), contents2.begin() ); + + + + + Mat matData(&contents2[0],1,contents2.size()); + + cout << "file size: " << contents2.size() << endl; + + matData.reshape(k,lc); + cout<& IDAmat, Cube &invIDAmat, int q, int n, byte* DK) { @@ -117,9 +146,9 @@ void buildFullRankIDA(Cube& IDAmat, Cube &invIDAmat, int q, int n, Cube rm1(RM1,1,1,q*n*n/4); rm1.reshape(n/2,n/2,q); - cout<<"rm1"<& IDAmat, Cube &invIDAmat, int q, int n, sub2.submat(n/2,0,n-1,n/2-1)=-rm2+eye>(n/2,n/2); sub2.submat(n/2,n/2,n-1,n-1)=rm2; - cout<<"det"<::from(sub2); cout<& IDAmat, int q, int n, int k, byte* DK) { +void buildPartialRankIDA(Cube& IDAmat, int q, int n, int k, byte* DK) { byte Sbox[256]; byte RM1[q*n*k]; @@ -168,7 +197,8 @@ void buildKRankIDA(Cube& IDAmat, int q, int n, int k, byte* DK) { prga(Sbox, q*n*k, RM1); Cube rm1(RM1,1,1,q*n*k); rm1.reshape(n,k,q); - + IDAmat=rm1; +/* cout<<"rm1"<& IDAmat, int q, int n, int k, byte* DK) { IDAmat.slice(i)=sub; + cout<<"det "<::from(sub); + cout<& IDAmat, int q, int n, int k, byte* DK) { int main( int argc, char *argv[] ) { + + int full=0; + int q=2; - int n=4; + int n=8; int k=4; int Tb=64; int l=10;//399*Tb; @@ -235,31 +273,42 @@ int main( int argc, char *argv[] ) { -/* -// Initialization of IDA and inverse IDA matrix - Cube IDAmat(n,n,q); - Cube invIDAmat(n,n,q); - buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]); -*/ - - Cube IDAmat(n,k,q); - buildKRankIDA(Cube& IDAmat, q, n, k, &DK[0]); + int lc; + int sizeFile; + Mat M; + if(full) + M=readFullFile(n,k,sizeFile,lc); + else + M=readPartialFile(n,k,sizeFile,lc); - // rc4key(&DK[8], sc, 16); - + cout<<"M "< IDAmat; + Cube invIDAmat; + + + if(full) + { + IDAmat.resize(n,n,q); + invIDAmat.resize(n,n,q); + buildFullRankIDA(IDAmat, invIDAmat, q, n, &DK[0]); + } + else { + IDAmat.resize(n,k,q); + buildPartialRankIDA(IDAmat, q, n, k, &DK[0]); + } + + + - int lc; - int sizeFile; - Mat M=readFile(n,k,sizeFile,lc); // Ma data; // data.load("/home/couturie/ajeter/lena.png"); @@ -274,20 +323,54 @@ int main( int argc, char *argv[] ) { cout< C; + Mat D2; + //full +// C=IDAmat.slice(0)*M; + +//partial + C=IDAmat.slice(0).submat(0,0,n-1,k-1)*M; - Mat C=IDAmat.slice(0)*M; M.save("M.bin",raw_binary); -// Mat D=invIDAmat.slice(0)*C; + + if(full) { + Mat D=invIDAmat.slice(0)*C; + + + /*mat A=conv_to::from(IDAmat.slice(0).submat(0,0,n-1,n-1)); + mat B=conv_to::from(C.submat(0,0,n-1,lc-1)); + mat D=solve(A,B); + */ + + D2=conv_to>::from(D); + cout<::from(IDAmat.slice(0).submat(0,0,k-1,n-1)); - mat B=conv_to::from(C.submat(0,0,k-1,lc-1)); + IDAmat.resize(k,k,q); + invIDAmat.resize(k,k,q); + buildFullRankIDA(IDAmat, invIDAmat, q, k, &DK[0]); - mat D=solve(A,B); + + mat A=conv_to::from(IDAmat.slice(0).submat(0,0,k-1,k-1)); + + mat B=conv_to::from(C.submat(0,0,k-1,lc-1)); + mat D=solve(A,B); +// mat D=inv(A)*B; + +// cout< D=invIDAmat.slice(0)*C.submat(0,0,k-1,lc-1); + + D2=conv_to>::from(D); + cout< D2=conv_to>::from(D); + // cout<<"D2"<