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
This commit is contained in:
Daniel Mendler 2019-10-29 22:38:49 +01:00
parent 795cd2013f
commit bcda8fc696
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
22 changed files with 64 additions and 111 deletions

View File

@ -9,13 +9,12 @@
*/
mp_err mp_abs(const mp_int *a, mp_int *b)
{
/* copy a to b */
if (a != b) {
mp_err err;
/* copy a to b */
if ((err = mp_copy(a, b)) != MP_OKAY) {
return err;
}
}
/* force the sign of b to positive */
b->sign = MP_ZPOS;

View File

@ -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 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;

View File

@ -11,11 +11,9 @@ 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;
}
}
for (i = 0; i < used; i++) {
mp_digit x, y;

View File

@ -6,18 +6,17 @@
/* 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;
}
}
/* copy everything over and zero high digits */
s_mp_copy_digs(b->dp, a->dp, a->used);

View File

@ -6,15 +6,13 @@
/* 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;
}
}
oldused = b->used;
b->used = a->used;

View File

@ -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;
}
}
/* 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;

View File

@ -9,11 +9,9 @@ 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;
}
}
/* zero the int */
mp_zero(a);

View File

@ -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,12 +19,9 @@ 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;
}
}
/* increment the used by the shift amount then copy upwards */
a->used += b;

View File

@ -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,12 +23,9 @@ 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;
}
}
x->used = digs;
for (ix = 0; ix < n->used; ix++) {

View File

@ -6,16 +6,14 @@
/* 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;
}
}
oldused = b->used;
b->used = a->used;

View File

@ -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 (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;
}
}
/* 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;
}

View File

@ -11,11 +11,9 @@ 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;
}
}
/* get the original destinations used count */
oldused = c->used;

View File

@ -6,12 +6,10 @@
/* 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;
}
}
b->sign = mp_iszero(b) || b->sign == MP_NEG ? MP_ZPOS : MP_NEG;

View File

@ -11,11 +11,9 @@ 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;
}
}
for (i = 0; i < used; i++) {
mp_digit x, y;

View File

@ -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 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);

View File

@ -11,11 +11,9 @@ 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;
}
}
for (i = 0; i < used; i++) {
mp_digit x, y;

View File

@ -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,12 +21,9 @@ 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;
}
}
/* get old used digit count and set new one */
oldused = c->used;

View File

@ -25,11 +25,9 @@ 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;
}
}
/* first we have to get the digits of the input into
* an array of double precision words W[...]

View File

@ -27,11 +27,9 @@ 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;
}
}
/* number of output digits to produce */
pa = MP_MIN(digs, a->used + b->used);

View File

@ -21,11 +21,9 @@ 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;
}
}
/* number of output digits to produce */
pa = a->used + b->used;

View File

@ -18,15 +18,13 @@ 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;
}
}
/* number of output digits to produce */
W1 = 0;

View File

@ -8,14 +8,12 @@ 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;
}
}
c->used = max;