Skip to content

Commit 143e037

Browse files
committed
simplifications: basic arithmetic functions
1 parent e60149d commit 143e037

12 files changed

+176
-271
lines changed

mp_add_d.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
/* single digit addition */
77
mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
88
{
9-
mp_err err;
10-
int ix, oldused;
11-
mp_digit *tmpa, *tmpc;
9+
int oldused;
1210

1311
/* fast path for a == c */
1412
if (a == c) {
@@ -27,13 +25,15 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
2725

2826
/* grow c as required */
2927
if (c->alloc < (a->used + 1)) {
28+
mp_err err;
3029
if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
3130
return err;
3231
}
3332
}
3433

3534
/* if a is negative and |a| >= b, call c = |a| - b */
3635
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
36+
mp_err err;
3737
mp_int a_ = *a;
3838
/* temporarily fix sign of a */
3939
a_.sign = MP_ZPOS;
@@ -53,49 +53,34 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
5353
/* old number of used digits in c */
5454
oldused = c->used;
5555

56-
/* source alias */
57-
tmpa = a->dp;
58-
59-
/* destination alias */
60-
tmpc = c->dp;
61-
6256
/* if a is positive */
6357
if (a->sign == MP_ZPOS) {
6458
/* add digits, mu is carry */
59+
int i;
6560
mp_digit mu = b;
66-
for (ix = 0; ix < a->used; ix++) {
67-
*tmpc = *tmpa++ + mu;
68-
mu = *tmpc >> MP_DIGIT_BIT;
69-
*tmpc++ &= MP_MASK;
61+
for (i = 0; i < a->used; i++) {
62+
c->dp[i] = a->dp[i] + mu;
63+
mu = c->dp[i] >> MP_DIGIT_BIT;
64+
c->dp[i] &= MP_MASK;
7065
}
7166
/* set final carry */
72-
ix++;
73-
*tmpc++ = mu;
67+
c->dp[i] = mu;
7468

7569
/* setup size */
7670
c->used = a->used + 1;
7771
} else {
7872
/* a was negative and |a| < b */
79-
c->used = 1;
73+
c->used = 1;
8074

8175
/* the result is a single digit */
82-
if (a->used == 1) {
83-
*tmpc++ = b - a->dp[0];
84-
} else {
85-
*tmpc++ = b;
86-
}
87-
88-
/* setup count so the clearing of oldused
89-
* can fall through correctly
90-
*/
91-
ix = 1;
76+
c->dp[0] = (a->used == 1) ? b - a->dp[0] : b;
9277
}
9378

9479
/* sign always positive */
9580
c->sign = MP_ZPOS;
9681

9782
/* now zero to oldused */
98-
MP_ZERO_DIGITS(tmpc, oldused - ix);
83+
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
9984
mp_clamp(c);
10085

10186
return MP_OKAY;

mp_div_2.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
/* b = a/2 */
77
mp_err mp_div_2(const mp_int *a, mp_int *b)
88
{
9-
int x, oldused;
10-
mp_digit r, rr, *tmpa, *tmpb;
11-
mp_err err;
9+
int x, oldused;
10+
mp_digit r;
1211

13-
/* copy */
1412
if (b->alloc < a->used) {
13+
mp_err err;
1514
if ((err = mp_grow(b, a->used)) != MP_OKAY) {
1615
return err;
1716
}
@@ -20,20 +19,14 @@ mp_err mp_div_2(const mp_int *a, mp_int *b)
2019
oldused = b->used;
2120
b->used = a->used;
2221

23-
/* source alias */
24-
tmpa = a->dp + b->used - 1;
25-
26-
/* dest alias */
27-
tmpb = b->dp + b->used - 1;
28-
2922
/* carry */
3023
r = 0;
31-
for (x = b->used - 1; x >= 0; x--) {
24+
for (x = b->used; x --> 0;) {
3225
/* get the carry for the next iteration */
33-
rr = *tmpa & 1u;
26+
mp_digit rr = a->dp[x] & 1u;
3427

3528
/* shift the current digit, add in carry and store */
36-
*tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1));
29+
b->dp[x] = (a->dp[x] >> 1) | (r << (MP_DIGIT_BIT - 1));
3730

3831
/* forward carry to next iteration */
3932
r = rr;

mp_div_2d.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@
66
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
77
mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
88
{
9-
mp_digit D, r, rr;
10-
int x;
119
mp_err err;
1210

13-
/* if the shift count is <= 0 then we do no work */
14-
if (b <= 0) {
15-
err = mp_copy(a, c);
16-
if (d != NULL) {
17-
mp_zero(d);
18-
}
19-
return err;
11+
if (b < 0) {
12+
return MP_VAL;
2013
}
2114

22-
/* copy */
2315
if ((err = mp_copy(a, c)) != MP_OKAY) {
2416
return err;
2517
}
18+
2619
/* 'a' should not be used after here - it might be the same as d */
2720

2821
/* get the remainder */
@@ -38,28 +31,25 @@ mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
3831
}
3932

4033
/* shift any bit count < MP_DIGIT_BIT */
41-
D = (mp_digit)(b % MP_DIGIT_BIT);
42-
if (D != 0u) {
43-
mp_digit *tmpc, mask, shift;
34+
b %= MP_DIGIT_BIT;
35+
if (b != 0u) {
36+
int x;
37+
mp_digit r, mask, shift;
4438

4539
/* mask */
46-
mask = ((mp_digit)1 << D) - 1uL;
40+
mask = ((mp_digit)1 << b) - 1uL;
4741

4842
/* shift for lsb */
49-
shift = (mp_digit)MP_DIGIT_BIT - D;
50-
51-
/* alias */
52-
tmpc = c->dp + (c->used - 1);
43+
shift = (mp_digit)(MP_DIGIT_BIT - b);
5344

5445
/* carry */
5546
r = 0;
56-
for (x = c->used - 1; x >= 0; x--) {
47+
for (x = c->used; x --> 0;) {
5748
/* get the lower bits of this word in a temp */
58-
rr = *tmpc & mask;
49+
mp_digit rr = c->dp[x] & mask;
5950

6051
/* shift the current word and mix in the carry bits from the previous word */
61-
*tmpc = (*tmpc >> D) | (r << shift);
62-
--tmpc;
52+
c->dp[x] = (c->dp[x] >> b) | (r << shift);
6353

6454
/* set the carry to the carry bits of the current word found above */
6555
r = rr;

mp_div_d.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
88
{
99
mp_int q;
1010
mp_word w;
11-
mp_digit t;
1211
mp_err err;
1312
int ix;
1413

@@ -56,14 +55,12 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
5655
q.used = a->used;
5756
q.sign = a->sign;
5857
w = 0;
59-
for (ix = a->used - 1; ix >= 0; ix--) {
58+
for (ix = a->used; ix --> 0;) {
59+
mp_digit t = 0;
6060
w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix];
61-
6261
if (w >= b) {
6362
t = (mp_digit)(w / b);
6463
w -= (mp_word)t * (mp_word)b;
65-
} else {
66-
t = 0;
6764
}
6865
q.dp[ix] = t;
6966
}
@@ -78,7 +75,7 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
7875
}
7976
mp_clear(&q);
8077

81-
return err;
78+
return MP_OKAY;
8279
}
8380

8481
#endif

mp_mul.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
88
{
99
mp_err err;
10-
int min_len = MP_MIN(a->used, b->used),
11-
max_len = MP_MAX(a->used, b->used),
10+
int min = MP_MIN(a->used, b->used),
11+
max = MP_MAX(a->used, b->used),
1212
digs = a->used + b->used + 1;
1313
mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
1414

@@ -20,16 +20,16 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
2020
* Using it to cut the input into slices small enough for s_mp_mul_digs_fast
2121
* was actually slower on the author's machine, but YMMV.
2222
*/
23-
(min_len >= MP_KARATSUBA_MUL_CUTOFF) &&
24-
((max_len / 2) >= MP_KARATSUBA_MUL_CUTOFF) &&
23+
(min >= MP_KARATSUBA_MUL_CUTOFF) &&
24+
((max / 2) >= MP_KARATSUBA_MUL_CUTOFF) &&
2525
/* Not much effect was observed below a ratio of 1:2, but again: YMMV. */
26-
(max_len >= (2 * min_len))) {
26+
(max >= (2 * min))) {
2727
err = s_mp_balance_mul(a,b,c);
2828
} else if (MP_HAS(S_MP_TOOM_MUL) &&
29-
(min_len >= MP_TOOM_MUL_CUTOFF)) {
29+
(min >= MP_TOOM_MUL_CUTOFF)) {
3030
err = s_mp_toom_mul(a, b, c);
3131
} else if (MP_HAS(S_MP_KARATSUBA_MUL) &&
32-
(min_len >= MP_KARATSUBA_MUL_CUTOFF)) {
32+
(min >= MP_KARATSUBA_MUL_CUTOFF)) {
3333
err = s_mp_karatsuba_mul(a, b, c);
3434
} else if (MP_HAS(S_MP_MUL_DIGS_FAST) &&
3535
/* can we use the fast multiplier?
@@ -39,7 +39,7 @@ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
3939
* digits won't affect carry propagation
4040
*/
4141
(digs < MP_WARRAY) &&
42-
(min_len <= MP_MAXFAST)) {
42+
(min <= MP_MAXFAST)) {
4343
err = s_mp_mul_digs_fast(a, b, c, digs);
4444
} else if (MP_HAS(S_MP_MUL_DIGS)) {
4545
err = s_mp_mul_digs(a, b, c, digs);

mp_mul_2.c

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
/* b = a*2 */
77
mp_err mp_mul_2(const mp_int *a, mp_int *b)
88
{
9-
int x, oldused;
10-
mp_err err;
9+
int x, oldused;
10+
mp_digit r;
1111

1212
/* grow to accomodate result */
1313
if (b->alloc < (a->used + 1)) {
14+
mp_err err;
1415
if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
1516
return err;
1617
}
@@ -19,45 +20,35 @@ mp_err mp_mul_2(const mp_int *a, mp_int *b)
1920
oldused = b->used;
2021
b->used = a->used;
2122

22-
{
23-
mp_digit r, rr, *tmpa, *tmpb;
23+
/* carry */
24+
r = 0;
25+
for (x = 0; x < a->used; x++) {
2426

25-
/* alias for source */
26-
tmpa = a->dp;
27-
28-
/* alias for dest */
29-
tmpb = b->dp;
30-
31-
/* carry */
32-
r = 0;
33-
for (x = 0; x < a->used; x++) {
27+
/* get what will be the *next* carry bit from the
28+
* MSB of the current digit
29+
*/
30+
mp_digit rr = a->dp[x] >> (mp_digit)(MP_DIGIT_BIT - 1);
3431

35-
/* get what will be the *next* carry bit from the
36-
* MSB of the current digit
37-
*/
38-
rr = *tmpa >> (mp_digit)(MP_DIGIT_BIT - 1);
32+
/* now shift up this digit, add in the carry [from the previous] */
33+
b->dp[x] = ((a->dp[x] << 1uL) | r) & MP_MASK;
3934

40-
/* now shift up this digit, add in the carry [from the previous] */
41-
*tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
35+
/* copy the carry that would be from the source
36+
* digit into the next iteration
37+
*/
38+
r = rr;
39+
}
4240

43-
/* copy the carry that would be from the source
44-
* digit into the next iteration
45-
*/
46-
r = rr;
47-
}
41+
/* new leading digit? */
42+
if (r != 0u) {
43+
/* add a MSB which is always 1 at this point */
44+
b->dp[b->used++] = 1;
45+
}
4846

49-
/* new leading digit? */
50-
if (r != 0u) {
51-
/* add a MSB which is always 1 at this point */
52-
*tmpb = 1;
53-
++(b->used);
54-
}
47+
/* now zero any excess digits on the destination
48+
* that we didn't write to
49+
*/
50+
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
5551

56-
/* now zero any excess digits on the destination
57-
* that we didn't write to
58-
*/
59-
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
60-
}
6152
b->sign = a->sign;
6253
return MP_OKAY;
6354
}

0 commit comments

Comments
 (0)