simplifications: remove unnecessary optimization * these double checks are not necessary * the compiler will move the early return outside of the called function, basically the functions is partially inlined * however lto/amalgamation needed for the optimization
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
diff --git a/mp_abs.c b/mp_abs.c
index 902279e..a87cc0c 100644
--- a/mp_abs.c
+++ b/mp_abs.c
@@ -9,12 +9,11 @@
*/
mp_err mp_abs(const mp_int *a, mp_int *b)
{
+ mp_err err;
+
/* copy a to b */
- if (a != b) {
- mp_err err;
- if ((err = mp_copy(a, b)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_copy(a, b)) != MP_OKAY) {
+ return err;
}
/* force the sign of b to positive */
diff --git a/mp_add_d.c b/mp_add_d.c
index 9ef4475..de935bb 100644
--- a/mp_add_d.c
+++ b/mp_add_d.c
@@ -6,6 +6,7 @@
/* single digit addition */
mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
{
+ mp_err err;
int oldused;
/* fast path for a == c */
@@ -24,16 +25,12 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
}
/* grow c as required */
- if (c->alloc < (a->used + 1)) {
- mp_err err;
- if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
+ return err;
}
/* if a is negative and |a| >= b, call c = |a| - b */
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
- mp_err err;
mp_int a_ = *a;
/* temporarily fix sign of a */
a_.sign = MP_ZPOS;
diff --git a/mp_and.c b/mp_and.c
index 92e6aed..a865ae0 100644
--- a/mp_and.c
+++ b/mp_and.c
@@ -11,10 +11,8 @@ mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
mp_digit ac = 1, bc = 1, cc = 1;
mp_sign csign = ((a->sign == MP_NEG) && (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
- if (c->alloc < used) {
- if ((err = mp_grow(c, used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
for (i = 0; i < used; i++) {
diff --git a/mp_copy.c b/mp_copy.c
index cf93b04..d79e2b8 100644
--- a/mp_copy.c
+++ b/mp_copy.c
@@ -6,17 +6,16 @@
/* copy, b = a */
mp_err mp_copy(const mp_int *a, mp_int *b)
{
+ mp_err err;
+
/* if dst == src do nothing */
if (a == b) {
return MP_OKAY;
}
/* grow dest */
- if (b->alloc < a->used) {
- mp_err err;
- if ((err = mp_grow(b, a->used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, a->used)) != MP_OKAY) {
+ return err;
}
/* copy everything over and zero high digits */
diff --git a/mp_div_2.c b/mp_div_2.c
index b15391e..8ab9bcb 100644
--- a/mp_div_2.c
+++ b/mp_div_2.c
@@ -6,14 +6,12 @@
/* b = a/2 */
mp_err mp_div_2(const mp_int *a, mp_int *b)
{
+ mp_err err;
int x, oldused;
mp_digit r;
- if (b->alloc < a->used) {
- mp_err err;
- if ((err = mp_grow(b, a->used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, a->used)) != MP_OKAY) {
+ return err;
}
oldused = b->used;
diff --git a/mp_dr_reduce.c b/mp_dr_reduce.c
index 1b97a1d..f0f6f35 100644
--- a/mp_dr_reduce.c
+++ b/mp_dr_reduce.c
@@ -19,22 +19,20 @@
*/
mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
{
+ mp_err err;
+
/* m = digits in modulus */
int m = n->used;
/* ensure that "x" has at least 2m digits */
- if (x->alloc < (m + m)) {
- mp_err err;
- if ((err = mp_grow(x, m + m)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(x, m + m)) != MP_OKAY) {
+ return err;
}
/* top of loop, this is where the code resumes if
* another reduction pass is required.
*/
for (;;) {
- mp_err err;
int i;
mp_digit mu = 0;
diff --git a/mp_from_ubin.c b/mp_from_ubin.c
index ae79be3..8272185 100644
--- a/mp_from_ubin.c
+++ b/mp_from_ubin.c
@@ -9,10 +9,8 @@ mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size)
mp_err err;
/* make sure there are at least two digits */
- if (a->alloc < 2) {
- if ((err = mp_grow(a, 2)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(a, 2)) != MP_OKAY) {
+ return err;
}
/* zero the int */
diff --git a/mp_lshd.c b/mp_lshd.c
index 2f56e5d..bfa8af8 100644
--- a/mp_lshd.c
+++ b/mp_lshd.c
@@ -6,6 +6,7 @@
/* shift left a certain amount of digits */
mp_err mp_lshd(mp_int *a, int b)
{
+ mp_err err;
int x;
/* if its less than zero return */
@@ -18,11 +19,8 @@ mp_err mp_lshd(mp_int *a, int b)
}
/* grow to fit the new digits */
- if (a->alloc < (a->used + b)) {
- mp_err err;
- if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
+ return err;
}
/* increment the used by the shift amount then copy upwards */
diff --git a/mp_montgomery_reduce.c b/mp_montgomery_reduce.c
index 6a5be26..0a8a1a5 100644
--- a/mp_montgomery_reduce.c
+++ b/mp_montgomery_reduce.c
@@ -6,6 +6,7 @@
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
{
+ mp_err err;
int ix, digs;
/* can the fast reduction [comba] method be used?
@@ -22,11 +23,8 @@ mp_err mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
}
/* grow the input as required */
- if (x->alloc < digs) {
- mp_err err;
- if ((err = mp_grow(x, digs)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(x, digs)) != MP_OKAY) {
+ return err;
}
x->used = digs;
diff --git a/mp_mul_2.c b/mp_mul_2.c
index 9e549c9..7d7084b 100644
--- a/mp_mul_2.c
+++ b/mp_mul_2.c
@@ -6,15 +6,13 @@
/* b = a*2 */
mp_err mp_mul_2(const mp_int *a, mp_int *b)
{
+ mp_err err;
int x, oldused;
mp_digit r;
/* grow to accomodate result */
- if (b->alloc < (a->used + 1)) {
- mp_err err;
- if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
+ return err;
}
oldused = b->used;
diff --git a/mp_mul_2d.c b/mp_mul_2d.c
index f1016ea..e458137 100644
--- a/mp_mul_2d.c
+++ b/mp_mul_2d.c
@@ -6,27 +6,22 @@
/* shift left by a certain bit count */
mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c)
{
+ mp_err err;
+
if (b < 0) {
return MP_VAL;
}
- if (a != c) {
- mp_err err;
- if ((err = mp_copy(a, c)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_copy(a, c)) != MP_OKAY) {
+ return err;
}
- if (c->alloc < (c->used + (b / MP_DIGIT_BIT) + 1)) {
- mp_err err;
- if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, c->used + (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
+ return err;
}
/* shift by as many digits in the bit count */
if (b >= MP_DIGIT_BIT) {
- mp_err err;
if ((err = mp_lshd(c, b / MP_DIGIT_BIT)) != MP_OKAY) {
return err;
}
diff --git a/mp_mul_d.c b/mp_mul_d.c
index 2be366f..30d6c93 100644
--- a/mp_mul_d.c
+++ b/mp_mul_d.c
@@ -11,10 +11,8 @@ mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
int ix, oldused;
/* make sure c is big enough to hold a*b */
- if (c->alloc < (a->used + 1)) {
- if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
+ return err;
}
/* get the original destinations used count */
diff --git a/mp_neg.c b/mp_neg.c
index f54ef3e..bfb6eb9 100644
--- a/mp_neg.c
+++ b/mp_neg.c
@@ -6,11 +6,9 @@
/* b = -a */
mp_err mp_neg(const mp_int *a, mp_int *b)
{
- if (a != b) {
- mp_err err;
- if ((err = mp_copy(a, b)) != MP_OKAY) {
- return err;
- }
+ mp_err err;
+ if ((err = mp_copy(a, b)) != MP_OKAY) {
+ return err;
}
b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG;
diff --git a/mp_or.c b/mp_or.c
index 7fa1375..5cf5255 100644
--- a/mp_or.c
+++ b/mp_or.c
@@ -11,10 +11,8 @@ mp_err mp_or(const mp_int *a, const mp_int *b, mp_int *c)
mp_digit ac = 1, bc = 1, cc = 1;
mp_sign csign = ((a->sign == MP_NEG) || (b->sign == MP_NEG)) ? MP_NEG : MP_ZPOS;
- if (c->alloc < used) {
- if ((err = mp_grow(c, used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
for (i = 0; i < used; i++) {
diff --git a/mp_sub_d.c b/mp_sub_d.c
index 91437f8..e80df3d 100644
--- a/mp_sub_d.c
+++ b/mp_sub_d.c
@@ -6,6 +6,7 @@
/* single digit subtraction */
mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
{
+ mp_err err;
int oldused;
/* fast path for a == c */
@@ -23,18 +24,14 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
}
/* grow c as required */
- if (c->alloc < (a->used + 1)) {
- mp_err err;
- if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
+ return err;
}
/* if a is negative just do an unsigned
* addition [with fudged signs]
*/
if (a->sign == MP_NEG) {
- mp_err err;
mp_int a_ = *a;
a_.sign = MP_ZPOS;
err = mp_add_d(&a_, b, c);
diff --git a/mp_xor.c b/mp_xor.c
index ca2c2f1..2fe8618 100644
--- a/mp_xor.c
+++ b/mp_xor.c
@@ -11,10 +11,8 @@ mp_err mp_xor(const mp_int *a, const mp_int *b, mp_int *c)
mp_digit ac = 1, bc = 1, cc = 1;
mp_sign csign = (a->sign != b->sign) ? MP_NEG : MP_ZPOS;
- if (c->alloc < used) {
- if ((err = mp_grow(c, used)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, used)) != MP_OKAY) {
+ return err;
}
for (i = 0; i < used; i++) {
diff --git a/s_mp_add.c b/s_mp_add.c
index 1d799b7..2bda2fe 100644
--- a/s_mp_add.c
+++ b/s_mp_add.c
@@ -8,6 +8,7 @@ mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
{
int oldused, min, max, i;
mp_digit u;
+ mp_err err;
/* find sizes, we let |a| <= |b| which means we have to sort
* them. "x" will point to the input with the most digits
@@ -20,11 +21,8 @@ mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
max = a->used;
/* init result */
- if (c->alloc < (max + 1)) {
- mp_err err;
- if ((err = mp_grow(c, max + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, max + 1)) != MP_OKAY) {
+ return err;
}
/* get old used digit count and set new one */
diff --git a/s_mp_montgomery_reduce_fast.c b/s_mp_montgomery_reduce_fast.c
index 9b08115..751d7fa 100644
--- a/s_mp_montgomery_reduce_fast.c
+++ b/s_mp_montgomery_reduce_fast.c
@@ -25,10 +25,8 @@ mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
oldused = x->used;
/* grow a as required */
- if (x->alloc < (n->used + 1)) {
- if ((err = mp_grow(x, n->used + 1)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(x, n->used + 1)) != MP_OKAY) {
+ return err;
}
/* first we have to get the digits of the input into
diff --git a/s_mp_mul_digs_fast.c b/s_mp_mul_digs_fast.c
index 3928d04..4f882f1 100644
--- a/s_mp_mul_digs_fast.c
+++ b/s_mp_mul_digs_fast.c
@@ -27,10 +27,8 @@ mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
mp_word _W;
/* grow the destination as required */
- if (c->alloc < digs) {
- if ((err = mp_grow(c, digs)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, digs)) != MP_OKAY) {
+ return err;
}
/* number of output digits to produce */
diff --git a/s_mp_mul_high_digs_fast.c b/s_mp_mul_high_digs_fast.c
index 01335a5..36bc69a 100644
--- a/s_mp_mul_high_digs_fast.c
+++ b/s_mp_mul_high_digs_fast.c
@@ -21,10 +21,8 @@ mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int
/* grow the destination as required */
pa = a->used + b->used;
- if (c->alloc < pa) {
- if ((err = mp_grow(c, pa)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, pa)) != MP_OKAY) {
+ return err;
}
/* number of output digits to produce */
diff --git a/s_mp_sqr_fast.c b/s_mp_sqr_fast.c
index daf4214..aebc61b 100644
--- a/s_mp_sqr_fast.c
+++ b/s_mp_sqr_fast.c
@@ -18,14 +18,12 @@ mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b)
int oldused, pa, ix;
mp_digit W[MP_WARRAY];
mp_word W1;
+ mp_err err;
/* grow the destination as required */
pa = a->used + a->used;
- if (b->alloc < pa) {
- mp_err err;
- if ((err = mp_grow(b, pa)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(b, pa)) != MP_OKAY) {
+ return err;
}
/* number of output digits to produce */
diff --git a/s_mp_sub.c b/s_mp_sub.c
index ead0b51..b1a749e 100644
--- a/s_mp_sub.c
+++ b/s_mp_sub.c
@@ -8,13 +8,11 @@ mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
{
int oldused = c->used, min = b->used, max = a->used, i;
mp_digit u;
+ mp_err err;
/* init result */
- if (c->alloc < max) {
- mp_err err;
- if ((err = mp_grow(c, max)) != MP_OKAY) {
- return err;
- }
+ if ((err = mp_grow(c, max)) != MP_OKAY) {
+ return err;
}
c->used = max;