From 4de50e2390a2779a10ef850a2a5d3d7243af409f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 13:51:00 +0200 Subject: [PATCH 1/7] removed redundant multiplication functions --- src/ntru_decrypt.c | 17 +++-------------- src/ntru_decrypt.h | 2 -- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index d7562a2..cfe08f4 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -20,6 +20,7 @@ */ #include "ntru_decrypt.h" +#include "poly.h" /* * Legend @@ -38,7 +39,7 @@ int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ // toDo q = ?, p = ?, fp = ? - pb_poly *a = first_multiply(private_key, encr_msg, q);// StarMultiply(f, e, a, N, q) + //toDO StarMultiply(f, e, a, N, q) for(int i = 0, i < N, i++){ if(a[i] < 0 ) { @@ -48,20 +49,8 @@ int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, ch a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) } } - char* d = second_multiply(a, fp, p)// StarMultiply(a, Fp , d, N, p) + //toDo StarMultiply(a, Fp , d, N, p) // {Decode returns the decrypted message, d, through the argument list.} return d; } - -// toDo fix header file definition and types if needed! -pb_poly* first_multiply(pb_poly *private_key, char *encr_msg, int q) { - // toDo a= f*e mod q - return NULL; -} - -// toDo fix header file definition and types if needed! -char* second_multiply(pb_poly *a, pb_poly *fp, int p) { - //toDo a*Fp mod p - return NULL; -} diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 92ac915..9da81cc 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -26,7 +26,5 @@ #include "context.h" int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); -pb_poly* first_multiply(pb_poly*, char*, int); -char* first_multiply(pb_poly*, pb_poly*, int); #endif /* NTRU_DECRYPT */ From 7349c7e8bede89ecc888dcca66b74c9928f4458f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 14:03:03 +0200 Subject: [PATCH 2/7] used pb_starmultiply for the first multiplication and added p, q, N --- src/ntru_decrypt.c | 10 ++++++++-- src/ntru_decrypt.h | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index cfe08f4..5e048db 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -21,11 +21,12 @@ #include "ntru_decrypt.h" #include "poly.h" +#include "context.h" /* * Legend * - * N : highest degree of the polynom + * N : maximal degree of the polynom * q : "is given" (... mod q) * p : "is given" (... mod p) * f : private key @@ -36,10 +37,15 @@ * */ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. -int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ +int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ // toDo q = ?, p = ?, fp = ? + unsigned int q = *context->q; + unsigned int p = *context->p; + unsigned int N = *context->N; //toDO StarMultiply(f, e, a, N, q) + pb_poly *a; + pb_starmultiply(private_key, encr_msg, a, context, q); for(int i = 0, i < N, i++){ if(a[i] < 0 ) { diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 9da81cc..2d89c13 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,6 +25,6 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(char *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); +int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); #endif /* NTRU_DECRYPT */ From b4a7870e9767ee65842ab18698770b19e1d14f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 14:07:12 +0200 Subject: [PATCH 3/7] fixed pointer arithmetical mistakes --- src/ntru_decrypt.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 5e048db..9f3bae4 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -39,11 +39,12 @@ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ // toDo q = ?, p = ?, fp = ? - unsigned int q = *context->q; - unsigned int p = *context->p; - unsigned int N = *context->N; - //toDO StarMultiply(f, e, a, N, q) + unsigned int q = context->q; + unsigned int p = context->p; + unsigned int N = context->N; + + // StarMultiply(f, e, a, N, q) pb_poly *a; pb_starmultiply(private_key, encr_msg, a, context, q); From fa97531eacfb14f729e3be031eb97ca233aaa5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 14:57:05 +0200 Subject: [PATCH 4/7] implemented the coefficient shift into a range of -q/2 and q/2 --- src/ntru_decrypt.c | 24 ++++++++++++++++++------ src/ntru_decrypt.h | 5 ++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 9f3bae4..9815587 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -38,22 +38,34 @@ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ - // toDo q = ?, p = ?, fp = ? + // toDo fp = ? unsigned int q = context->q; unsigned int p = context->p; unsigned int N = context->N; // StarMultiply(f, e, a, N, q) - pb_poly *a; + pb_poly *a = build_polynom(NULL, N, context); pb_starmultiply(private_key, encr_msg, a, context, q); + mp_int mp_q; + mp_int mp_qdiv2; + mp_int zero; + + init_integer(&mp_q); + init_integer(&mp_qdiv2); + init_integer(&zero); + + MP_SET_INT(&mp_q, q); + mp_div_2(&mp_q, mp_qdiv2); + mp_zero(&zero); + for(int i = 0, i < N, i++){ - if(a[i] < 0 ) { - a[i] = a[i] + q; // Make all coefficients positive + if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { // Make all coefficients positive + a->terms[i] = a->terms[i] + q; } - if(a[i] > q/2) { - a[i] = a[i] - q // Shift coefficients of a into range (−q/2, q/2) + if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) { // Shift coefficients of a into range (−q/2, q/2) + a->terms[i] = a->terms[i] - mp_q; } } //toDo StarMultiply(a, Fp , d, N, p) diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index 2d89c13..ca22f4e 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,6 +25,9 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg); +int ntru_decrypt(pb_poly *encr_msg, + pb_poly *private_key, + ntru_context *context, + char ** decr_msg); #endif /* NTRU_DECRYPT */ From 5254cf93f3d59ecf5aafe6f71d6cf7658e5ff2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 16:02:09 +0200 Subject: [PATCH 5/7] changed return type of ntru_decrypt, implemented second starmultiply, fixed numerical operations to mp_operations --- src/ntru_decrypt.c | 15 ++++++++++----- src/ntru_decrypt.h | 9 +++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 9815587..e50daa5 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -37,8 +37,7 @@ * */ // Require: N , q, p, secret key f , inverse polynomial Fp , and encrypted message e. -int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, char ** decr_msg){ - // toDo fp = ? +pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru_context *context, char ** decr_msg){ unsigned int q = context->q; unsigned int p = context->p; @@ -62,13 +61,19 @@ int ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, ntru_context *context, for(int i = 0, i < N, i++){ if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { // Make all coefficients positive - a->terms[i] = a->terms[i] + q; + //a->terms[i] = a->terms[i] + q; + mp_add((&a->terms[i]),&mp_q,(&a->terms[i])); } if(mp_cmp(&(a->terms[i]), &mp_qdiv2) == MP_GT) { // Shift coefficients of a into range (−q/2, q/2) - a->terms[i] = a->terms[i] - mp_q; + //a->terms[i] = a->terms[i] - mp_q; + mp_sub((&a->terms[i]),&mp_q,(&a->terms[i])); } } - //toDo StarMultiply(a, Fp , d, N, p) + + pb_poly *d = build_polynom(NULL, N, context); + + // StarMultiply(a, Fp , d, N, p) + pb_starmultiply(a, Fp, d, N, p); // {Decode returns the decrypted message, d, through the argument list.} return d; diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index ca22f4e..5f44005 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -25,9 +25,10 @@ #include "poly.h" #include "context.h" -int ntru_decrypt(pb_poly *encr_msg, - pb_poly *private_key, - ntru_context *context, - char ** decr_msg); +pb_poly* ntru_decrypt(pb_poly*, + pb_poly*, + pb_poly*, + ntru_context*, + char**); #endif /* NTRU_DECRYPT */ From 4e6bb38260dd18ba3866f16a29c16ec4c1a0baa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Sat, 3 May 2014 17:18:23 +0200 Subject: [PATCH 6/7] numerous fixes --- src/Makefile | 4 ++-- src/ntru_decrypt.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Makefile b/src/Makefile index 30f657e..8bea853 100644 --- a/src/Makefile +++ b/src/Makefile @@ -37,8 +37,8 @@ endif LIBS += -L. # objects -PQC_OBJS = rand.o poly.o mem.o -PQC_HEADERS = err.h rand.h poly.h context.h +PQC_OBJS = rand.o poly.o mem.o ntru_decrypt.o +PQC_HEADERS = err.h rand.h poly.h context.h ntru_decrypt.h # CUNIT_OBJS = cunit.o # includes diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index e50daa5..abd55df 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -20,8 +20,6 @@ */ #include "ntru_decrypt.h" -#include "poly.h" -#include "context.h" /* * Legend @@ -42,11 +40,13 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru unsigned int q = context->q; unsigned int p = context->p; unsigned int N = context->N; + unsigned int i; // StarMultiply(f, e, a, N, q) pb_poly *a = build_polynom(NULL, N, context); pb_starmultiply(private_key, encr_msg, a, context, q); - + printf("%s\n", "Nach dem StarMultiply: "); + draw_polynom(a); mp_int mp_q; mp_int mp_qdiv2; mp_int zero; @@ -56,10 +56,10 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru init_integer(&zero); MP_SET_INT(&mp_q, q); - mp_div_2(&mp_q, mp_qdiv2); + mp_div_2(&mp_q, &mp_qdiv2); mp_zero(&zero); - for(int i = 0, i < N, i++){ + for(i = 0; i < N; i++){ if(mp_cmp(&(a->terms[i]),&zero) == MP_LT) { // Make all coefficients positive //a->terms[i] = a->terms[i] + q; mp_add((&a->terms[i]),&mp_q,(&a->terms[i])); @@ -73,7 +73,7 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru pb_poly *d = build_polynom(NULL, N, context); // StarMultiply(a, Fp , d, N, p) - pb_starmultiply(a, Fp, d, N, p); + pb_starmultiply(a, Fp, d, context, p); // {Decode returns the decrypted message, d, through the argument list.} return d; From 74a3fa88c78458440fd1dde178228a88d4e1dd0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Pr=C3=BC=C3=9Fner?= Date: Mon, 5 May 2014 13:58:53 +0200 Subject: [PATCH 7/7] moved some lines --- src/ntru_decrypt.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index abd55df..5594f3e 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -45,8 +45,7 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru // StarMultiply(f, e, a, N, q) pb_poly *a = build_polynom(NULL, N, context); pb_starmultiply(private_key, encr_msg, a, context, q); - printf("%s\n", "Nach dem StarMultiply: "); - draw_polynom(a); + mp_int mp_q; mp_int mp_qdiv2; mp_int zero; @@ -70,6 +69,9 @@ pb_poly* ntru_decrypt(pb_poly *encr_msg, pb_poly *private_key, pb_poly *Fp, ntru } } + printf("%s\np:%d", "Nach dem StarMultiply: ", p); + draw_polynom(a); + pb_poly *d = build_polynom(NULL, N, context); // StarMultiply(a, Fp , d, N, p)