Skip to content

Commit 795cd20

Browse files
committed
simplifications: add s_mp_zero_(digs|buf) and s_mp_copy_digs
Originally I made those as macros. However we have many other small functions like mp_clamp, mp_exch which are also not implemented as macros right now. If we would use c99, I would implement them as private static inline functions. And mp_exch would be a public static inline function. But since we are bound to c89, we simply use normal functions. To achieve optimal performance one should either use link time optimization or amalgamation.
1 parent b1f9bff commit 795cd20

33 files changed

+148
-150
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ matrix:
144144
# clang for x86-64 architecture (64-bit longs and 64-bit pointers)
145145
- env: SANITIZER=1 CONV_WARNINGS=relaxed BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-travis-valgrind'
146146
- env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-travis-valgrind'
147+
- env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --cflags=-DMP_USE_MEMOPS --with-m64 --with-travis-valgrind'
147148
- env: SANITIZER=1 CONV_WARNINGS=strict BUILDOPTIONS='--with-cc=clang-7 --c89 --with-m64 --with-travis-valgrind'
148149
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --with-m64 --with-travis-valgrind --cflags=-DMP_PREC=MP_MIN_PREC'
149150
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-6.0 --with-m64 --with-travis-valgrind'

etc/tune.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ int main(int argc, char **argv)
292292
s_number_of_test_loops = 64;
293293
s_stabilization_extra = 3;
294294

295-
MP_ZERO_BUFFER(&args, sizeof(args));
295+
s_mp_zero_buf(&args, sizeof(args));
296296

297297
args.testmode = 0;
298298
args.verbose = 0;

mp_add_d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ mp_err mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
8080
c->sign = MP_ZPOS;
8181

8282
/* now zero to oldused */
83-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
83+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
8484
mp_clamp(c);
8585

8686
return MP_OKAY;

mp_clear.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void mp_clear(mp_int *a)
99
/* only do anything if a hasn't been freed previously */
1010
if (a->dp != NULL) {
1111
/* free ram */
12-
MP_FREE_DIGITS(a->dp, a->alloc);
12+
MP_FREE_DIGS(a->dp, a->alloc);
1313

1414
/* reset members to make debugging easier */
1515
a->dp = NULL;

mp_copy.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
/* copy, b = a */
77
mp_err mp_copy(const mp_int *a, mp_int *b)
88
{
9-
int n;
10-
119
/* if dst == src do nothing */
1210
if (a == b) {
1311
return MP_OKAY;
@@ -21,19 +19,12 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
2119
}
2220
}
2321

24-
/* zero b and copy the parameters over */
25-
26-
/* copy all the digits */
27-
for (n = 0; n < a->used; n++) {
28-
b->dp[n] = a->dp[n];
29-
}
30-
31-
/* clear high digits */
32-
MP_ZERO_DIGITS(b->dp + a->used, b->used - a->used);
33-
34-
/* copy used count and sign */
22+
/* copy everything over and zero high digits */
23+
s_mp_copy_digs(b->dp, a->dp, a->used);
24+
s_mp_zero_digs(b->dp + a->used, b->used - a->used);
3525
b->used = a->used;
3626
b->sign = a->sign;
27+
3728
return MP_OKAY;
3829
}
3930
#endif

mp_div_2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ mp_err mp_div_2(const mp_int *a, mp_int *b)
3333
}
3434

3535
/* zero excess digits */
36-
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
36+
s_mp_zero_digs(b->dp + b->used, oldused - b->used);
3737

3838
b->sign = a->sign;
3939
mp_clamp(b);

mp_dr_reduce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
4949
x->dp[i] = mu;
5050

5151
/* zero words above m */
52-
MP_ZERO_DIGITS(x->dp + m + 1, (x->used - m) - 1);
52+
s_mp_zero_digs(x->dp + m + 1, (x->used - m) - 1);
5353

5454
/* clamp, sub and return */
5555
mp_clamp(x);

mp_fwrite.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
2525
}
2626
}
2727

28-
MP_FREE_BUFFER(buf, size);
28+
MP_FREE_BUF(buf, size);
2929
return err;
3030
}
3131
#endif

mp_grow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mp_err mp_grow(mp_int *a, int size)
2626
a->dp = dp;
2727

2828
/* zero excess digits */
29-
MP_ZERO_DIGITS(a->dp + a->alloc, size - a->alloc);
29+
s_mp_zero_digs(a->dp + a->alloc, size - a->alloc);
3030
a->alloc = size;
3131
}
3232
return MP_OKAY;

mp_lshd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mp_err mp_lshd(mp_int *a, int b)
3737
}
3838

3939
/* zero the lower digits */
40-
MP_ZERO_DIGITS(a->dp, b);
40+
s_mp_zero_digs(a->dp, b);
4141

4242
return MP_OKAY;
4343
}

mp_mod_2d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
2929

3030
/* zero digits above the last digit of the modulus */
3131
x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1);
32-
MP_ZERO_DIGITS(c->dp + x, c->used - x);
32+
s_mp_zero_digs(c->dp + x, c->used - x);
3333

3434
/* clear the digit that is not completely outside/inside the modulus */
3535
c->dp[b / MP_DIGIT_BIT] &=

mp_mul_2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mp_err mp_mul_2(const mp_int *a, mp_int *b)
4747
/* now zero any excess digits on the destination
4848
* that we didn't write to
4949
*/
50-
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
50+
s_mp_zero_digs(b->dp + b->used, oldused - b->used);
5151

5252
b->sign = a->sign;
5353
return MP_OKAY;

mp_mul_d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
4545
c->used = a->used + 1;
4646

4747
/* now zero digits above the top */
48-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
48+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
4949

5050
mp_clamp(c);
5151

mp_prime_rand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags)
116116

117117
err = MP_OKAY;
118118
LBL_ERR:
119-
MP_FREE_BUFFER(tmp, (size_t)bsize);
119+
MP_FREE_BUF(tmp, (size_t)bsize);
120120
return err;
121121
}
122122

mp_rshd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void mp_rshd(mp_int *a, int b)
3535
}
3636

3737
/* zero the top digits */
38-
MP_ZERO_DIGITS(a->dp + a->used - b, b);
38+
s_mp_zero_digs(a->dp + a->used - b, b);
3939

4040
/* remove excess digits */
4141
a->used -= b;

mp_set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ void mp_set(mp_int *a, mp_digit b)
1010
a->dp[0] = b & MP_MASK;
1111
a->sign = MP_ZPOS;
1212
a->used = (a->dp[0] != 0u) ? 1 : 0;
13-
MP_ZERO_DIGITS(a->dp + a->used, oldused - a->used);
13+
s_mp_zero_digs(a->dp + a->used, oldused - a->used);
1414
}
1515
#endif

mp_sub_d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mp_err mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
7272
}
7373

7474
/* zero excess digits */
75-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
75+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
7676

7777
mp_clamp(c);
7878
return MP_OKAY;

mp_zero.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
void mp_zero(mp_int *a)
88
{
99
a->sign = MP_ZPOS;
10-
MP_ZERO_DIGITS(a->dp, a->used);
10+
s_mp_zero_digs(a->dp, a->used);
1111
a->used = 0;
1212
}
1313
#endif

s_mp_add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c)
6464
c->dp[i] = u;
6565

6666
/* clear digits above oldused */
67-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
67+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
6868

6969
mp_clamp(c);
7070
return MP_OKAY;

s_mp_balance_mul.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
88
{
99
mp_int a0, tmp, r;
1010
mp_err err;
11-
int i, j, count,
11+
int i, j,
1212
nblocks = MP_MAX(a->used, b->used) / MP_MIN(a->used, b->used),
1313
bsize = MP_MIN(a->used, b->used);
1414

@@ -27,12 +27,11 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
2727

2828
for (i = 0, j=0; i < nblocks; i++) {
2929
/* Cut a slice off of a */
30-
a0.used = 0;
31-
for (count = 0; count < bsize; count++) {
32-
a0.dp[count] = a->dp[ j++ ];
33-
a0.used++;
34-
}
30+
a0.used = bsize;
31+
s_mp_copy_digs(a0.dp, a->dp + j, a0.used);
32+
j += a0.used;
3533
mp_clamp(&a0);
34+
3635
/* Multiply with b */
3736
if ((err = mp_mul(&a0, b, &tmp)) != MP_OKAY) {
3837
goto LBL_ERR;
@@ -48,12 +47,11 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
4847
}
4948
/* The left-overs; there are always left-overs */
5049
if (j < a->used) {
51-
a0.used = 0;
52-
for (count = 0; j < a->used; count++) {
53-
a0.dp[count] = a->dp[ j++ ];
54-
a0.used++;
55-
}
50+
a0.used = a->used - j;
51+
s_mp_copy_digs(a0.dp, a->dp + j, a0.used);
52+
j += a0.used;
5653
mp_clamp(&a0);
54+
5755
if ((err = mp_mul(&a0, b, &tmp)) != MP_OKAY) {
5856
goto LBL_ERR;
5957
}

s_mp_copy_digs.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "tommath_private.h"
2+
#ifdef S_MP_COPY_DIGS_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
#ifdef MP_USE_MEMOPS
7+
# include <string.h>
8+
#endif
9+
10+
void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits)
11+
{
12+
#ifdef MP_USE_MEMOPS
13+
if (digits > 0) {
14+
memcpy(d, s, (size_t)digits * sizeof(mp_digit));
15+
}
16+
#else
17+
while (digits-- > 0) {
18+
*d++ = *s++;
19+
}
20+
#endif
21+
}
22+
23+
#endif

s_mp_karatsuba_mul.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
3636
{
3737
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
38-
int B, i;
38+
int B;
3939
mp_err err;
4040

4141
/* min # of digits */
@@ -77,16 +77,10 @@ mp_err s_mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
7777
/* we copy the digits directly instead of using higher level functions
7878
* since we also need to shift the digits
7979
*/
80-
for (i = 0; i < B; i++) {
81-
x0.dp[i] = a->dp[i];
82-
y0.dp[i] = b->dp[i];
83-
}
84-
for (i = B; i < a->used; i++) {
85-
x1.dp[i - B] = a->dp[i];
86-
}
87-
for (i = B; i < b->used; i++) {
88-
y1.dp[i - B] = b->dp[i];
89-
}
80+
s_mp_copy_digs(x0.dp, a->dp, x0.used);
81+
s_mp_copy_digs(y0.dp, b->dp, y0.used);
82+
s_mp_copy_digs(x1.dp, a->dp + B, x1.used);
83+
s_mp_copy_digs(y1.dp, b->dp + B, y1.used);
9084

9185
/* only need to clamp the lower words since by definition the
9286
* upper words x1/y1 must have a known number of digits

s_mp_karatsuba_sqr.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b)
1414
{
1515
mp_int x0, x1, t1, t2, x0x0, x1x1;
16-
int B, x;
16+
int B;
1717
mp_err err;
1818

1919
/* min # of digits */
@@ -39,16 +39,10 @@ mp_err s_mp_karatsuba_sqr(const mp_int *a, mp_int *b)
3939
goto X0X0;
4040

4141
/* now shift the digits */
42-
for (x = 0; x < B; x++) {
43-
x0.dp[x] = a->dp[x];
44-
}
45-
for (x = B; x < a->used; x++) {
46-
x1.dp[x - B] = a->dp[x];
47-
}
48-
4942
x0.used = B;
5043
x1.used = a->used - B;
51-
44+
s_mp_copy_digs(x0.dp, a->dp, x0.used);
45+
s_mp_copy_digs(x1.dp, a->dp + B, x1.used);
5246
mp_clamp(&x0);
5347

5448
/* now calc the products x0*x0 and x1*x1 */

s_mp_montgomery_reduce_fast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
4242

4343
/* zero the high words of W[a->used..m->used*2] */
4444
if (ix < ((n->used * 2) + 1)) {
45-
MP_ZERO_BUFFER(W + x->used, sizeof(mp_word) * (size_t)(((n->used * 2) + 1) - ix));
45+
s_mp_zero_buf(W + x->used, sizeof(mp_word) * (size_t)(((n->used * 2) + 1) - ix));
4646
}
4747

4848
/* now we proceed to zero successive digits
@@ -108,7 +108,7 @@ mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho)
108108
/* zero oldused digits, if the input a was larger than
109109
* m->used+1 we'll have to clear the digits
110110
*/
111-
MP_ZERO_DIGITS(x->dp + x->used, oldused - x->used);
111+
s_mp_zero_digs(x->dp + x->used, oldused - x->used);
112112

113113
mp_clamp(x);
114114

s_mp_mul_digs_fast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mp_err s_mp_mul_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int digs)
7272
}
7373

7474
/* clear unused digits [that existed in the old copy of c] */
75-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
75+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
7676

7777
mp_clamp(c);
7878
return MP_OKAY;

s_mp_mul_high_digs_fast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ mp_err s_mp_mul_high_digs_fast(const mp_int *a, const mp_int *b, mp_int *c, int
6464
}
6565

6666
/* clear unused digits [that existed in the old copy of c] */
67-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
67+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
6868

6969
mp_clamp(c);
7070
return MP_OKAY;

s_mp_sqr_fast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mp_err s_mp_sqr_fast(const mp_int *a, mp_int *b)
8181
}
8282

8383
/* clear unused digits [that existed in the old copy of c] */
84-
MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
84+
s_mp_zero_digs(b->dp + b->used, oldused - b->used);
8585

8686
mp_clamp(b);
8787
return MP_OKAY;

s_mp_sub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
4949
}
5050

5151
/* clear digits above used (since we may not have grown result above) */
52-
MP_ZERO_DIGITS(c->dp + c->used, oldused - c->used);
52+
s_mp_zero_digs(c->dp + c->used, oldused - c->used);
5353

5454
mp_clamp(c);
5555
return MP_OKAY;

0 commit comments

Comments
 (0)