From 6c0f94435e6cc5e4515b24b2e07c21b94dc270c5 Mon Sep 17 00:00:00 2001 From: hasufell Date: Sun, 20 Apr 2014 19:55:30 +0200 Subject: [PATCH] POLY: introduce delete_polynom_multi() Just a wrapper around delete_polynom() to handle multiple args. Must be called with NULL as last argument! --- src/poly.c | 31 ++++++++++++++++++++++++++----- src/poly.h | 3 +++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/poly.c b/src/poly.c index f7cacec..7f5ffcc 100644 --- a/src/poly.c +++ b/src/poly.c @@ -24,6 +24,7 @@ #include "mem.h" #include "poly.h" +#include #include #include #include @@ -170,6 +171,30 @@ void delete_polynom(pb_poly *poly) free(poly); } +/** + * This deletes the internal structure of all polynomials, + * and frees the pointers. Don't call this on stack variables, + * this is intended for use after ntru_ functions, that + * return a polynomial pointer. + * You must call this with NULL as last argument! + * + * @param poly the polynomial to delete + * @param ... follow up polynomials + */ +void delete_polynom_multi(pb_poly *poly, ...) +{ + pb_poly *next_poly; + va_list args; + + next_poly = poly; + va_start(args, poly); + while (next_poly != NULL) { + delete_polynom(next_poly); + next_poly = va_arg(args, pb_poly*); + } + va_end(args); +} + /** * Starmultiplication, as follows: * c = a * b mod (x^N − 1) @@ -353,11 +378,7 @@ OUT_OF_LOOP: mp_clear(&mp_tmp); } - delete_polynom(a_tmp); - delete_polynom(b); - delete_polynom(c); - delete_polynom(f); - delete_polynom(g); + delete_polynom_multi(a_tmp, b, c, f, g, NULL); /* TODO: check if the f * Fq = 1 (mod p) condition holds true */ diff --git a/src/poly.h b/src/poly.h index 4d9aa04..77a9b54 100644 --- a/src/poly.h +++ b/src/poly.h @@ -28,6 +28,7 @@ #include #include +#include #include #define MP_SET(...) mp_set(__VA_ARGS__) @@ -164,6 +165,8 @@ void erase_polynom(pb_poly *poly, size_t len); void delete_polynom(pb_poly *new_poly); +void delete_polynom_multi(pb_poly *poly, ...); + void pb_starmultiply(pb_poly *a, pb_poly *b, pb_poly *c,