diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c index 0960b49..775c76f 100644 --- a/bn_fast_s_mp_sqr.c +++ b/bn_fast_s_mp_sqr.c @@ -66,7 +66,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) * we halve the distance since they approach at a rate of 2x * and we have to round because odd cases need to be executed */ - iy = MIN(iy, (ty-tx+1)>>1); + iy = MIN(iy, ((ty-tx)+1)>>1); /* execute loop */ for (iz = 0; iz < iy; iz++) { diff --git a/bn_mp_div.c b/bn_mp_div.c index db5fffb..3ca5d7f 100644 --- a/bn_mp_div.c +++ b/bn_mp_div.c @@ -190,7 +190,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) /* step 3.1 if xi == yt then set q{i-t-1} to b-1, * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ if (x.dp[i] == y.dp[t]) { - q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); + q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); } else { mp_word tmp; tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); @@ -199,7 +199,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) if (tmp > (mp_word) MP_MASK) { tmp = MP_MASK; } - q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); + q.dp[(i - t) - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); } /* while (q{i-t-1} * (yt * b + y{t-1})) > @@ -207,16 +207,16 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) do q{i-t-1} -= 1; */ - q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; + q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK; do { - q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK; + q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK; /* find left hand */ mp_zero (&t1); t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1]; t1.dp[1] = y.dp[t]; t1.used = 2; - if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) { + if ((res = mp_mul_d (&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { goto LBL_Y; } @@ -228,11 +228,11 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } while (mp_cmp_mag(&t1, &t2) == MP_GT); /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ - if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) { + if ((res = mp_mul_d (&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { goto LBL_Y; } - if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { + if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) { goto LBL_Y; } @@ -245,14 +245,14 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) if ((res = mp_copy (&y, &t1)) != MP_OKAY) { goto LBL_Y; } - if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { + if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) { goto LBL_Y; } if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { goto LBL_Y; } - q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; + q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK; } } diff --git a/bn_mp_export.c b/bn_mp_export.c index fd31301..2455fc5 100644 --- a/bn_mp_export.c +++ b/bn_mp_export.c @@ -54,8 +54,8 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, for (j = 0; j < size; ++j) { unsigned char* byte = ( (unsigned char*)rop + - (((order == -1) ? i : (count - 1 - i)) * size) + - ((endian == -1) ? j : (size - 1 - j)) + (((order == -1) ? i : ((count - 1) - i)) * size) + + ((endian == -1) ? j : ((size - 1) - j)) ); if (j >= (size - nail_bytes)) { @@ -63,9 +63,9 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, continue; } - *byte = (unsigned char)((j == (size - nail_bytes - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF)); + *byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF)); - if ((result = mp_div_2d(&t, ((j == (size - nail_bytes - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) { + if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) { mp_clear(&t); return result; } diff --git a/bn_mp_import.c b/bn_mp_import.c index 2f97880..ca2a5e9 100644 --- a/bn_mp_import.c +++ b/bn_mp_import.c @@ -47,8 +47,8 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size, for (j = 0; j < (size - nail_bytes); ++j) { unsigned char byte = *( (unsigned char*)op + - (((order == 1) ? i : (count - 1 - i)) * size) + - ((endian == 1) ? (j + nail_bytes) : (size - 1 - j - nail_bytes)) + (((order == 1) ? i : ((count - 1) - i)) * size) + + ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)) ); if ( diff --git a/bn_mp_lshd.c b/bn_mp_lshd.c index 88bffe0..0e0ea0a 100644 --- a/bn_mp_lshd.c +++ b/bn_mp_lshd.c @@ -42,7 +42,7 @@ int mp_lshd (mp_int * a, int b) top = a->dp + a->used - 1; /* base */ - bottom = a->dp + a->used - 1 - b; + bottom = (a->dp + a->used - 1) - b; /* much like mp_rshd this is implemented using a sliding window * except the window goes the otherway around. Copying from diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c index 10c6a5b..ad2099b 100644 --- a/bn_mp_sqr.c +++ b/bn_mp_sqr.c @@ -38,7 +38,7 @@ mp_sqr (mp_int * a, mp_int * b) /* can we use the fast comba multiplier? */ if ((((a->used * 2) + 1) < MP_WARRAY) && (a->used < - (1 << ((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT) - 1)))) { + (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) { res = fast_s_mp_sqr (a, b); } else #endif diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c index 2dc7743..55260fe 100644 --- a/bn_s_mp_sub.c +++ b/bn_s_mp_sub.c @@ -47,7 +47,7 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c) u = 0; for (i = 0; i < min; i++) { /* T[i] = A[i] - B[i] - U */ - *tmpc = *tmpa++ - *tmpb++ - u; + *tmpc = (*tmpa++ - *tmpb++) - u; /* U = carry bit of T[i] * Note this saves performing an AND operation since