#include <iostream>
#include <fstream>
#include <vector>
-
-
#include <iostream> // std::cout, std::fixed
#include <iomanip>
#include <math.h>
}
-Mat<byte> readFile(int n, int k, int& sizeFile, int &lc) {
+Mat<byte> 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<<sizeFile<<endl;
stream.seekg(0, ios::beg);
}
+Mat<byte> 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<<sizeFile<<endl;
+ stream.seekg(0, ios::beg);
+
+
+ lc=ceil(double(sizeFile)/k);
+ cout<<lc<<endl;
+
+ vector<uint8_t> contents2(k*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(k,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) {
Cube<byte> rm1(RM1,1,1,q*n*n/4);
rm1.reshape(n/2,n/2,q);
- cout<<"rm1"<<endl;
+ /* cout<<"rm1"<<endl;
cout<<rm1<<endl;
-
+ */
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;
+ /*cout<<"det"<<endl;
mat toto=conv_to<mat>::from(sub2);
cout<<det(toto)<<endl;
-
+ */
invIDAmat.slice(i)=sub2;
}
- cout<<IDAmat<<endl;
- cout<<invIDAmat<<endl;
+ //cout<<IDAmat<<endl;
+ //cout<<invIDAmat<<endl;
}
-void buildKRankIDA(Cube<byte>& IDAmat, int q, int n, int k, byte* DK) {
+void buildPartialRankIDA(Cube<byte>& IDAmat, int q, int n, int k, byte* DK) {
byte Sbox[256];
byte RM1[q*n*k];
prga(Sbox, q*n*k, RM1);
Cube<byte> rm1(RM1,1,1,q*n*k);
rm1.reshape(n,k,q);
-
+ IDAmat=rm1;
+/*
cout<<"rm1"<<endl;
cout<<rm1<<endl;
IDAmat.slice(i)=sub;
+ cout<<"det "<<i<<endl;
+ mat toto=conv_to<mat>::from(sub);
+ cout<<det(toto)<<endl;
+
}
- cout<<IDAmat<<endl;
+*/
+ // cout<<IDAmat<<endl;
}
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;
-/*
-// 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]);
-*/
-
- Cube<byte> IDAmat(n,k,q);
- buildKRankIDA(Cube<byte>& IDAmat, q, n, k, &DK[0]);
+ int lc;
+ int sizeFile;
+ Mat<byte> M;
+ if(full)
+ M=readFullFile(n,k,sizeFile,lc);
+ else
+ M=readPartialFile(n,k,sizeFile,lc);
- // rc4key(&DK[8], sc, 16);
-
+ cout<<"M "<<endl;
+ cout<<M<<endl;
-
-
-
arma_rng::set_seed(time(NULL));
+// Initialization of IDA and inverse IDA matrix
+
+ Cube<byte> IDAmat;
+ Cube<short> 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<byte> M=readFile(n,k,sizeFile,lc);
// Ma<byte> data;
// data.load("/home/couturie/ajeter/lena.png");
cout<<C<<endl;
*/
+
+ Mat<byte> C;
+ Mat<char> D2;
+ //full
+// C=IDAmat.slice(0)*M;
+
+//partial
+ C=IDAmat.slice(0).submat(0,0,n-1,k-1)*M;
- Mat<byte> C=IDAmat.slice(0)*M;
M.save("M.bin",raw_binary);
-// Mat<short> D=invIDAmat.slice(0)*C;
+
+ if(full) {
+ Mat<short> D=invIDAmat.slice(0)*C;
+
+
+ /*mat A=conv_to<mat>::from(IDAmat.slice(0).submat(0,0,n-1,n-1));
+ mat B=conv_to<mat>::from(C.submat(0,0,n-1,lc-1));
+ mat D=solve(A,B);
+ */
+
+ D2=conv_to<Mat<char>>::from(D);
+ cout<<D2<<endl;
+ }
+ else {
- 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));
+ 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<mat>::from(IDAmat.slice(0).submat(0,0,k-1,k-1));
+
+ mat B=conv_to<mat>::from(C.submat(0,0,k-1,lc-1));
+ mat D=solve(A,B);
+// mat D=inv(A)*B;
+
+// cout<<D<<endl;
+
+// Mat<short> D=invIDAmat.slice(0)*C.submat(0,0,k-1,lc-1);
+
+ D2=conv_to<Mat<char>>::from(D);
+ cout<<D2<<endl;
+ }
- Mat<char> D2=conv_to<Mat<char>>::from(D);
+
// cout<<"D2"<<endl;
// cout<<D2<<endl;
// D2.save("D2.bin",raw_binary);