simplify mod functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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