From: couturie Date: Sun, 27 May 2018 14:18:30 +0000 (+0200) Subject: ida_gf64 X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/Cipher_code.git/commitdiff_plain/5ca765b420d772f256a9f7ee73a8e4b9398d0d77 ida_gf64 --- diff --git a/Arduino/sketch_AES/sketch_AES.ino b/Arduino/sketch_AES/sketch_AES.ino index dcd8883..998f763 100644 --- a/Arduino/sketch_AES/sketch_AES.ino +++ b/Arduino/sketch_AES/sketch_AES.ino @@ -6,7 +6,7 @@ AES aes ; byte *key = (unsigned char*)"0123456789010123"; -const int size_mesg=16; +const int size_mesg=64; diff --git a/IDA/Makefile b/IDA/Makefile index a2afb98..8d7d811 100644 --- a/IDA/Makefile +++ b/IDA/Makefile @@ -11,5 +11,9 @@ test_mat2: test_mat2.cpp ida: ida.cpp g++ -std=c++11 $(CPP_FLAGS) -o $@ $< $(LIBS) + +ida_gf64: ida_gf64.cpp + g++ -o $@ $< -std=c++11 -O3 -lm -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -g -O3 -Wall -I/home/couturie/ajeter/jerasure/include /home/couturie/ajeter/jerasure/src/.libs/jerasure.o /home/couturie/ajeter/jerasure/src/.libs/galois.o -lgf_complete -fpermissive + clean: - rm test_mat2 ida + rm test_mat2 ida ida_gf64 diff --git a/IDA/ida.cpp b/IDA/ida.cpp index dd7791a..e854c3a 100644 --- a/IDA/ida.cpp +++ b/IDA/ida.cpp @@ -1,6 +1,3 @@ -// 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 #include @@ -158,13 +155,16 @@ convert_vec250_to_vec256(vector vec250){ Mat readFullFile(int n, int k, long& sizeFile, int &lc) { // ifstream stream("lena.png", ios::in | ios::binary | ios::ate); - ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate); -// ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate); + ifstream stream("lena_small2.png", ios::in | ios::binary | ios::ate); +// ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate); + sizeFile=stream.tellg(); cout< readFullFile(int n, int k, long& sizeFile, int &lc) { tmpSize>>=8; } +//we rebuild the size in order to check that the routine is correct cout<<"rebuild"<=0;i--) { @@ -193,6 +194,14 @@ Mat readFullFile(int n, int k, long& sizeFile, int &lc) { // for(int i=0;i<8;i++) // contents.insert(i,mysize[i]); contents.insert (contents.begin(), mysize, mysize+8); + + cout<<"start of the vector"< contents2=convert_vec256_to_vec250(contents); cout<<"res contents2 "< matData(&contents2[0],1,contents2.size()); @@ -242,12 +251,9 @@ int main( int argc, char *argv[] ) { - int full=0; int n=8; int k=4; - int Tb=64; - int l=10;//399*Tb; @@ -292,15 +298,21 @@ int main( int argc, char *argv[] ) { cout<<"tatat"< C3; + + int off=0; //read k files among n - for(int i=0;i=0;i--) { + for(int i=0+off;i d2=readFile(str.c_str()); - C3.insert_rows(i,d2); + C3.insert_rows(l,d2); + l++; } @@ -311,6 +323,7 @@ int main( int argc, char *argv[] ) { Mat Cs=conv_to>::from(C3); cout< S2=conv_to>::from(SS2); S2=mod(S2,mm); -// cout< res =conv_to< vector >::from(S2.row(0)); cout<<"res size "< res2=convert_vec250_to_vec256(res); cout<<"res2 size "< +#include +#include +#include +#include +#include +#include +#include +#include // std::random_shuffle +#include // std::vector +extern "C" { + #include "jerasure.h" +} + +typedef unsigned long mylong; +#define LLUI (long long unsigned int) + + +using namespace std; + + +void display(mylong *mat, int r, int c) { + for(int i=0;imultiply.w64(gf,m1[i*c1+k], m2[k*c2+j]); + } + } + } + return product; +} + + + +int invert_matrix(gf_t *gf, mylong *mat, mylong *inv, int rows) +{ + int cols, i, j, k, x, rs2; + int row_start; + mylong tmp, inverse; + + cols = rows; + + k = 0; + for (i = 0; i < rows; i++) { + for (j = 0; j < cols; j++) { + inv[k] = (i == j) ? 1 : 0; + k++; + } + } +// display(inv, rows, rows); +// printf("\n"); + + /* First -- convert into upper triangular */ + for (i = 0; i < cols; i++) { + row_start = cols*i; + + /* Swap rows if we ave a zero i,i element. If we can't swap, then the + matrix was not invertible */ + + if (mat[row_start+i] == 0) { + for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ; + if (j == rows) return -1; + rs2 = j*cols; + for (k = 0; k < cols; k++) { + tmp = mat[row_start+k]; + mat[row_start+k] = mat[rs2+k]; + mat[rs2+k] = tmp; + tmp = inv[row_start+k]; + inv[row_start+k] = inv[rs2+k]; + inv[rs2+k] = tmp; + } + } + + /* Multiply the row by 1/element i,i */ + tmp = mat[row_start+i]; + if (tmp != 1) { + inverse = gf->divide.w64(gf,1, tmp); + for (j = 0; j < cols; j++) { + mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse); + inv[row_start+j] = gf->multiply.w64(gf,inv[row_start+j], inverse); + } + } + + /* Now for each j>i, add A_ji*Ai to Aj */ + k = row_start+i; + for (j = i+1; j != cols; j++) { + k += cols; + if (mat[k] != 0) { + if (mat[k] == 1) { + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= mat[row_start+x]; + inv[rs2+x] ^= inv[row_start+x]; + } + } else { + tmp = mat[k]; + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= gf->multiply.w64(gf,tmp, mat[row_start+x]); + inv[rs2+x] ^= gf->multiply.w64(gf,tmp, inv[row_start+x]); + } + } + } + } + } + + /* Now the matrix is upper triangular. Start at the top and multiply down */ + + for (i = rows-1; i >= 0; i--) { + row_start = i*cols; + for (j = 0; j < i; j++) { + rs2 = j*cols; + if (mat[rs2+i] != 0) { + tmp = mat[rs2+i]; + mat[rs2+i] = 0; + for (k = 0; k < cols; k++) { + inv[rs2+k] ^= gf->multiply.w64(gf,tmp, inv[row_start+k]); + } + } + } + } + +/* printf("mat\n"); + display(mat, rows, rows); + printf("\n"); + printf("inv\n"); + display(inv, rows, rows); + printf("\n"); +*/ + return 0; +} + + + + +int invertible_matrix(gf_t *gf, int *mat, int rows, int w) +{ + int cols, i, j, k, x, rs2; + int row_start; + mylong tmp, inverse; + + cols = rows; + + /* First -- convert into upper triangular */ + for (i = 0; i < cols; i++) { + row_start = cols*i; + + /* Swap rows if we ave a zero i,i element. If we can't swap, then the + matrix was not invertible */ + + if (mat[row_start+i] == 0) { + for (j = i+1; j < rows && mat[cols*j+i] == 0; j++) ; + if (j == rows) return 0; + rs2 = j*cols; + for (k = 0; k < cols; k++) { + tmp = mat[row_start+k]; + mat[row_start+k] = mat[rs2+k]; + mat[rs2+k] = tmp; + } + } + + /* Multiply the row by 1/element i,i */ + tmp = mat[row_start+i]; + if (tmp != 1) { + inverse = gf->divide.w64(gf,1, tmp); + for (j = 0; j < cols; j++) { + mat[row_start+j] = gf->multiply.w64(gf,mat[row_start+j], inverse); + } + } + + /* Now for each j>i, add A_ji*Ai to Aj */ + k = row_start+i; + for (j = i+1; j != cols; j++) { + k += cols; + if (mat[k] != 0) { + if (mat[k] == 1) { + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= mat[row_start+x]; + } + } else { + tmp = mat[k]; + rs2 = cols*j; + for (x = 0; x < cols; x++) { + mat[rs2+x] ^= gf->multiply.w64(gf,tmp,mat[row_start+x]); + } + } + } + } + } + return 1; +} + + + + + +mylong* readFullFile(int n, int t, mylong& sizeFile, mylong & padded_size) { + +// ifstream stream("lena.png", ios::in | ios::binary | ios::ate); +// ifstream stream("lena_small.png", ios::in | ios::binary | ios::ate); + ifstream stream("/home/couturie/Downloads/CARCARIASS.zip", ios::in | ios::binary | ios::ate); + + sizeFile=stream.tellg(); + std::cout << sizeFile << std::endl; + stream.seekg(0, ios::beg); + + + + + + + vector contents((istreambuf_iterator(stream)), istreambuf_iterator()); + + + + + + + + //make padding, we need to pad to be divisible by 8*t, we + if((sizeFile+8)%(8*t)!=0) { + cout<<(int)(sizeFile/(8*t))<(p_contents); + + padded_size=contents.size()/8; + + mylong *p_contents2=new mylong[padded_size]; + memcpy(p_contents2,p_contents,sizeof(mylong)*padded_size); + //mylong *p_contents2=(mylong*)p_contents; + + p_contents2[0]=sizeFile; + + + + +/* for(int i=0;i=0;i--) { + res<<=8; + res+=p_contents[i]; + } + + cout << "convert val " << (long)res << endl; + + res=0; + for(int i=16-1;i>=8;i--) { + res<<=8; + res+=p_contents[i]; + } + + cout << "convert val " << (long)res << endl; + */ + + return p_contents2; +} + + +void saveFile(uint8_t *data, const char *fileName,long size_file) { + cout<<"size file "< elapsed_seconds = end-start; + std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n"; + +// display(matC,t,t); + + + //Save trunks + for(int i=0;i myvector; + + // set some values: + for (int i=0; i=t;i--) { +// for(int i=0;i(&matS2[1]); + saveFile(reconstucted_data, "lena2.png",new_size); + return 0; +} + + diff --git a/IDA/lena_small.png b/IDA/lena_small.png deleted file mode 100644 index 872ef3a..0000000 Binary files a/IDA/lena_small.png and /dev/null differ diff --git a/IDA/lena_small2.png b/IDA/lena_small2.png deleted file mode 100644 index d02501f..0000000 Binary files a/IDA/lena_small2.png and /dev/null differ diff --git a/SboxAES/IOT/main.c b/SboxAES/IOT/main.c index 34d24c2..90f4369 100644 --- a/SboxAES/IOT/main.c +++ b/SboxAES/IOT/main.c @@ -4,13 +4,14 @@ #include #include #include +#include #include"aes.h" #define CTR 1 -const int size_mesg=64; +const int size_mesg=1024; typedef unsigned char byte; @@ -19,6 +20,22 @@ typedef unsigned char byte; +double TimeStart() +{ + struct timeval tstart; + gettimeofday(&tstart,0); + return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) ); +} + +double TimeStop(double t) +{ + struct timeval tend; + + gettimeofday(&tend,0); + t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t; + return (t); +} + @@ -63,26 +80,46 @@ int main(int argc, char** argv) { for(int i=0;i