Commit 61ebe7c047dc74297b55c1369c78cc08d73f67c0

Daniel Mendler 2019-11-07T01:00:13

simplify mod functions

diff --git a/mp_addmod.c b/mp_addmod.c
index beda872..91e2087 100644
--- a/mp_addmod.c
+++ b/mp_addmod.c
@@ -6,20 +6,10 @@
 /* d = a + b (mod c) */
 mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
 {
-   mp_err  err;
-   mp_int  t;
-
-   if ((err = mp_init(&t)) != MP_OKAY) {
+   mp_err err;
+   if ((err = mp_add(a, b, d)) != MP_OKAY) {
       return err;
    }
-
-   if ((err = mp_add(a, b, &t)) != MP_OKAY) {
-      goto LBL_ERR;
-   }
-   err = mp_mod(&t, c, d);
-
-LBL_ERR:
-   mp_clear(&t);
-   return err;
+   return mp_mod(d, c, d);
 }
 #endif
diff --git a/mp_mod.c b/mp_mod.c
index 3eced35..beae13e 100644
--- a/mp_mod.c
+++ b/mp_mod.c
@@ -6,26 +6,10 @@
 /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
 mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   mp_int  t;
-   mp_err  err;
-
-   if ((err = mp_init_size(&t, b->used)) != MP_OKAY) {
+   mp_err err;
+   if ((err = mp_div(a, b, NULL, c)) != MP_OKAY) {
       return err;
    }
-
-   if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) {
-      goto LBL_ERR;
-   }
-
-   if (mp_iszero(&t) || (t.sign == b->sign)) {
-      err = MP_OKAY;
-      mp_exch(&t, c);
-   } else {
-      err = mp_add(b, &t, c);
-   }
-
-LBL_ERR:
-   mp_clear(&t);
-   return err;
+   return mp_iszero(c) || (c->sign == b->sign) ? MP_OKAY : mp_add(b, c, c);
 }
 #endif
diff --git a/mp_mulmod.c b/mp_mulmod.c
index 55b635b..e158693 100644
--- a/mp_mulmod.c
+++ b/mp_mulmod.c
@@ -7,19 +7,9 @@
 mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
 {
    mp_err err;
-   mp_int t;
-
-   if ((err = mp_init_size(&t, c->used)) != MP_OKAY) {
+   if ((err = mp_mul(a, b, d)) != MP_OKAY) {
       return err;
    }
-
-   if ((err = mp_mul(a, b, &t)) != MP_OKAY) {
-      goto LBL_ERR;
-   }
-   err = mp_mod(&t, c, d);
-
-LBL_ERR:
-   mp_clear(&t);
-   return err;
+   return mp_mod(d, c, d);
 }
 #endif
diff --git a/mp_sqrmod.c b/mp_sqrmod.c
index f1a92ef..bce2af0 100644
--- a/mp_sqrmod.c
+++ b/mp_sqrmod.c
@@ -6,20 +6,10 @@
 /* c = a * a (mod b) */
 mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c)
 {
-   mp_err  err;
-   mp_int  t;
-
-   if ((err = mp_init(&t)) != MP_OKAY) {
+   mp_err err;
+   if ((err = mp_sqr(a, c)) != MP_OKAY) {
       return err;
    }
-
-   if ((err = mp_sqr(a, &t)) != MP_OKAY) {
-      goto LBL_ERR;
-   }
-   err = mp_mod(&t, b, c);
-
-LBL_ERR:
-   mp_clear(&t);
-   return err;
+   return mp_mod(c, b, c);
 }
 #endif
diff --git a/mp_submod.c b/mp_submod.c
index c5dc401..6e4d4f7 100644
--- a/mp_submod.c
+++ b/mp_submod.c
@@ -7,19 +7,9 @@
 mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
 {
    mp_err err;
-   mp_int t;
-
-   if ((err = mp_init(&t)) != MP_OKAY) {
+   if ((err = mp_sub(a, b, d)) != MP_OKAY) {
       return err;
    }
-
-   if ((err = mp_sub(a, b, &t)) != MP_OKAY) {
-      goto LBL_ERR;
-   }
-   err = mp_mod(&t, c, d);
-
-LBL_ERR:
-   mp_clear(&t);
-   return err;
+   return mp_mod(d, c, d);
 }
 #endif
diff --git a/tommath_class.h b/tommath_class.h
index 0f95a93..b11c574 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -196,8 +196,6 @@
 
 #if defined(MP_ADDMOD_C)
 #   define MP_ADD_C
-#   define MP_CLEAR_C
-#   define MP_INIT_C
 #   define MP_MOD_C
 #endif
 
@@ -519,10 +517,7 @@
 
 #if defined(MP_MOD_C)
 #   define MP_ADD_C
-#   define MP_CLEAR_C
 #   define MP_DIV_C
-#   define MP_EXCH_C
-#   define MP_INIT_SIZE_C
 #endif
 
 #if defined(MP_MOD_2D_C)
@@ -580,8 +575,6 @@
 #endif
 
 #if defined(MP_MULMOD_C)
-#   define MP_CLEAR_C
-#   define MP_INIT_SIZE_C
 #   define MP_MOD_C
 #   define MP_MUL_C
 #endif
@@ -888,8 +881,6 @@
 #endif
 
 #if defined(MP_SQRMOD_C)
-#   define MP_CLEAR_C
-#   define MP_INIT_C
 #   define MP_MOD_C
 #   define MP_SQR_C
 #endif
@@ -938,8 +929,6 @@
 #endif
 
 #if defined(MP_SUBMOD_C)
-#   define MP_CLEAR_C
-#   define MP_INIT_C
 #   define MP_MOD_C
 #   define MP_SUB_C
 #endif