Commit b4c42576d7e15f55973129879e7e21e3187ac5d7

Steffen Jaeckel 2019-11-05T19:40:30

Merge pull request #436 from fperrad/20191029_lint some linting

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
diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 648a654..edbecc0 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -304,7 +304,7 @@ static int mtest_opponent(void)
          DO(mp_read_radix(&c, buf, 64));
          DO(mp_invmod(&a, &b, &d));
          DO(mp_mulmod(&d, &a, &b, &e));
-         if (mp_cmp_d(&e, 1uL) != MP_EQ) {
+         if (mp_cmp_d(&e, 1u) != MP_EQ) {
             printf("inv [wrong value from MPI?!] failure\n");
             draw(&a);
             draw(&b);
diff --git a/demo/test.c b/demo/test.c
index 3b97873..cfe2e5a 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -43,12 +43,12 @@ static int64_t rand_int64(void)
 
 static uint32_t uabs32(int32_t x)
 {
-   return x > 0 ? (uint32_t)x : -(uint32_t)x;
+   return (x > 0) ? (uint32_t)x : -(uint32_t)x;
 }
 
 static uint64_t uabs64(int64_t x)
 {
-   return x > 0 ? (uint64_t)x : -(uint64_t)x;
+   return (x > 0) ? (uint64_t)x : -(uint64_t)x;
 }
 
 /* This function prototype is needed
@@ -115,20 +115,20 @@ static int test_trivial_stuff(void)
    DO(mp_abs(&a, &b));
    EXPECT(!mp_isneg(&b));
    /* a: -5-> b: -4 */
-   DO(mp_add_d(&a, 1uL, &b));
+   DO(mp_add_d(&a, 1u, &b));
    EXPECT(mp_isneg(&b));
    EXPECT(mp_get_i32(&b) == -4);
    EXPECT(mp_get_u32(&b) == (uint32_t)-4);
    EXPECT(mp_get_mag_u32(&b) == 4);
    /* a: -5-> b: 1 */
-   DO(mp_add_d(&a, 6uL, &b));
+   DO(mp_add_d(&a, 6u, &b));
    EXPECT(mp_get_u32(&b) == 1);
    /* a: -5-> a: 1 */
-   DO(mp_add_d(&a, 6uL, &a));
+   DO(mp_add_d(&a, 6u, &a));
    EXPECT(mp_get_u32(&a) == 1);
    mp_zero(&a);
    /* a: 0-> a: 6 */
-   DO(mp_add_d(&a, 6uL, &a));
+   DO(mp_add_d(&a, 6u, &a));
    EXPECT(mp_get_u32(&a) == 6);
 
    mp_set(&a, 42u);
@@ -223,9 +223,9 @@ static int test_mp_get_set_i64(void)
 
    DOR(mp_init(&a));
 
-   check_get_set_i64(&a, 0);
-   check_get_set_i64(&a, -1);
-   check_get_set_i64(&a, 1);
+   check_get_set_i64(&a, 0LL);
+   check_get_set_i64(&a, -1LL);
+   check_get_set_i64(&a, 1LL);
    check_get_set_i64(&a, INT64_MIN);
    check_get_set_i64(&a, INT64_MAX);
 
@@ -282,7 +282,7 @@ static int test_mp_rand(void)
       DO(mp_rand(&a, n));
       DO(mp_incr(&a));
       DO(mp_div_2d(&a, n * MP_DIGIT_BIT, &b, NULL));
-      if (mp_cmp_d(&b, 1) != MP_EQ) {
+      if (mp_cmp_d(&b, 1u) != MP_EQ) {
          ndraw(&a, "mp_rand() a");
          ndraw(&b, "mp_rand() b");
          e = MP_ERR;
@@ -292,7 +292,7 @@ static int test_mp_rand(void)
 LBL_ERR:
    mp_rand_source(s_mp_rand_jenkins);
    mp_clear_multi(&a, &b, NULL);
-   return e == MP_OKAY ? EXIT_SUCCESS : EXIT_FAILURE;
+   return (e == MP_OKAY) ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
 static int test_mp_kronecker(void)
@@ -729,7 +729,7 @@ static int test_mp_sqrt(void)
          printf("\nmp_sqrt() error!");
          goto LBL_ERR;
       }
-      DO(mp_root_u32(&a, 2uL, &c));
+      DO(mp_root_u32(&a, 2u, &c));
       if (mp_cmp_mag(&b, &c) != MP_EQ) {
          printf("mp_sqrt() bad result!\n");
          goto LBL_ERR;
@@ -770,7 +770,7 @@ static int test_mp_is_square(void)
       }
 
       /* test for false positives */
-      DO(mp_add_d(&a, 1uL, &a));
+      DO(mp_add_d(&a, 1u, &a));
       if (mp_is_square(&a, &res) != MP_OKAY) {
          printf("\nfp:mp_is_square() error!");
          goto LBL_ERR;
@@ -879,9 +879,9 @@ static int test_mp_prime_is_prime(void)
    }
    /* About the same size as Arnault's pseudoprime */
    printf("\rTesting mp_prime_is_prime() with certified prime 2^1119 + 53       ");
-   mp_set(&a, 1uL);
+   mp_set(&a, 1u);
    DO(mp_mul_2d(&a,1119,&a));
-   DO(mp_add_d(&a, 53uL, &a));
+   DO(mp_add_d(&a, 53u, &a));
    e = mp_prime_is_prime(&a, mp_prime_rabin_miller_trials(mp_count_bits(&a)), &cnt);
    /* small problem */
    if (e != MP_OKAY) {
@@ -912,7 +912,7 @@ static int test_mp_prime_is_prime(void)
          goto LBL_ERR;
       }
       /* let's see if it's really a safe prime */
-      DO(mp_sub_d(&a, 1uL, &b));
+      DO(mp_sub_d(&a, 1u, &b));
       DO(mp_div_2(&b, &b));
       e = mp_prime_is_prime(&b, mp_prime_rabin_miller_trials(mp_count_bits(&b)), &cnt);
       /* small problem */
@@ -1010,7 +1010,7 @@ static int test_mp_prime_next_prime(void)
       putchar('\n');
       goto LBL_ERR;
    }
-   mp_set(&a, 8);
+   mp_set(&a, 8u);
    DO(mp_prime_next_prime(&a, 5, true));
    if (mp_cmp_d(&a, 11u) != MP_EQ) {
       printf("mp_prime_next_prime: output should have been 11 but was: ");
@@ -1092,7 +1092,7 @@ static int test_mp_montgomery_reduce(void)
 
          /* now test a random reduction */
          for (ix = 0; ix < 100; ix++) {
-            DO(mp_rand(&c, 1 + abs(rand_int()) % (2*i)));
+            DO(mp_rand(&c, 1 + (abs(rand_int()) % (2*i))));
             DO(mp_copy(&c, &d));
             DO(mp_copy(&c, &e));
 
@@ -1183,7 +1183,7 @@ static int test_mp_cnt_lsb(void)
    mp_int a, b;
    DOR(mp_init_multi(&a, &b, NULL));
 
-   mp_set(&a, 1uL);
+   mp_set(&a, 1u);
    for (ix = 0; ix < 1024; ix++) {
       if (mp_cnt_lsb(&a) != ix) {
          printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a));
@@ -1212,7 +1212,7 @@ static int test_mp_reduce_2k(void)
       mp_digit tmp;
 
       DO(mp_2expt(&a, cnt));
-      DO(mp_sub_d(&a, 2uL, &a));  /* a = 2**cnt - 2 */
+      DO(mp_sub_d(&a, 2u, &a));  /* a = 2**cnt - 2 */
 
       printf("\r %4d bits", cnt);
       printf("(%d)", mp_reduce_is_2k(&a));
@@ -1223,10 +1223,10 @@ static int test_mp_reduce_2k(void)
             printf(".");
             fflush(stdout);
          }
-         DO(mp_rand(&b, (cnt / MP_DIGIT_BIT + 1) * 2));
+         DO(mp_rand(&b, ((cnt / MP_DIGIT_BIT) + 1) * 2));
          DO(mp_copy(&c, &b));
          DO(mp_mod(&c, &a, &c));
-         DO(mp_reduce_2k(&b, &a, 2uL));
+         DO(mp_reduce_2k(&b, &a, 2u));
          if (mp_cmp(&c, &b) != MP_EQ) {
             printf("FAILED\n");
             goto LBL_ERR;
@@ -1249,7 +1249,7 @@ static int test_mp_div_3(void)
    DOR(mp_init_multi(&a, &b, &c, &d, &e, NULL));
 
    /* test mp_div_3  */
-   mp_set(&d, 3uL);
+   mp_set(&d, 3u);
    for (cnt = 0; cnt < 10000;) {
       mp_digit r2;
 
@@ -1257,7 +1257,7 @@ static int test_mp_div_3(void)
          printf("\r %9d", cnt);
          fflush(stdout);
       }
-      DO(mp_rand(&a, abs(rand_int()) % 128 + 1));
+      DO(mp_rand(&a, (abs(rand_int()) % 128) + 1));
       DO(mp_div(&a, &d, &b, &e));
       DO(mp_div_3(&a, &c, &r2));
 
@@ -1306,7 +1306,7 @@ static int test_mp_dr_reduce(void)
             fflush(stdout);
          }
          DO(mp_sqr(&b, &b));
-         DO(mp_add_d(&b, 1uL, &b));
+         DO(mp_add_d(&b, 1u, &b));
          DO(mp_copy(&b, &c));
 
          DO(mp_mod(&b, &a, &b));
@@ -1370,10 +1370,10 @@ static int test_mp_reduce_2k_l(void)
    fflush(stdout);
    for (cnt = 0; cnt < (int)(1uL << 20); cnt++) {
       DO(mp_sqr(&b, &b));
-      DO(mp_add_d(&b, 1uL, &b));
+      DO(mp_add_d(&b, 1u, &b));
       DO(mp_reduce_2k_l(&b, &a, &d));
       DO(mp_sqr(&c, &c));
-      DO(mp_add_d(&c, 1uL, &c));
+      DO(mp_add_d(&c, 1u, &c));
       DO(mp_mod(&c, &a, &c));
       if (mp_cmp(&b, &c) != MP_EQ) {
          printf("mp_reduce_2k_l() failed at step %d\n", cnt);
@@ -1439,7 +1439,7 @@ static int test_mp_log_u32(void)
       0     x    MP_VAL
       1     x    MP_VAL
    */
-   mp_set(&a, 42uL);
+   mp_set(&a, 42u);
    base = 0u;
    if (mp_log_u32(&a, base, &lb) != MP_VAL) {
       goto LBL_ERR;
@@ -1520,8 +1520,8 @@ static int test_mp_log_u32(void)
 
    /*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10  */
    mp_set(&a, max_base);
-   DO(mp_expt_u32(&a, 10uL, &a));
-   DO(mp_add_d(&a, max_base / 2, &a));
+   DO(mp_expt_u32(&a, 10u, &a));
+   DO(mp_add_d(&a, max_base / 2u, &a));
    DO(mp_log_u32(&a, max_base, &lb));
    if (lb != 10u) {
       goto LBL_ERR;
@@ -1543,7 +1543,7 @@ static int test_mp_incr(void)
    /* Does it increment inside the limits of a MP_xBIT limb? */
    mp_set(&a, MP_MASK/2);
    DO(mp_incr(&a));
-   if (mp_cmp_d(&a, (MP_MASK/2uL) + 1uL) != MP_EQ) {
+   if (mp_cmp_d(&a, (MP_MASK/2u) + 1u) != MP_EQ) {
       goto LBL_ERR;
    }
 
@@ -1551,22 +1551,22 @@ static int test_mp_incr(void)
    mp_set(&a, MP_MASK);
    mp_set(&b, MP_MASK);
    DO(mp_incr(&a));
-   DO(mp_add_d(&b, 1uL, &b));
+   DO(mp_add_d(&b, 1u, &b));
    if (mp_cmp(&a, &b) != MP_EQ) {
       goto LBL_ERR;
    }
 
    /* Does it increment from -1 to 0? */
-   mp_set(&a, 1uL);
+   mp_set(&a, 1u);
    a.sign = MP_NEG;
    DO(mp_incr(&a));
-   if (mp_cmp_d(&a, 0uL) != MP_EQ) {
+   if (mp_cmp_d(&a, 0u) != MP_EQ) {
       goto LBL_ERR;
    }
 
    /* Does it increment from -(MP_MASK + 1) to -MP_MASK? */
    mp_set(&a, MP_MASK);
-   DO(mp_add_d(&a, 1uL, &a));
+   DO(mp_add_d(&a, 1u, &a));
    a.sign = MP_NEG;
    DO(mp_incr(&a));
    if (a.sign != MP_NEG) {
@@ -1593,13 +1593,13 @@ static int test_mp_decr(void)
    /* Does it decrement inside the limits of a MP_xBIT limb? */
    mp_set(&a, MP_MASK/2);
    DO(mp_decr(&a));
-   if (mp_cmp_d(&a, (MP_MASK/2uL) - 1uL) != MP_EQ) {
+   if (mp_cmp_d(&a, (MP_MASK/2u) - 1u) != MP_EQ) {
       goto LBL_ERR;
    }
 
    /* Does it decrement outside of the limits of a MP_xBIT limb? */
    mp_set(&a, MP_MASK);
-   DO(mp_add_d(&a, 1uL, &a));
+   DO(mp_add_d(&a, 1u, &a));
    DO(mp_decr(&a));
    if (mp_cmp_d(&a, MP_MASK) != MP_EQ) {
       goto LBL_ERR;
@@ -1610,7 +1610,7 @@ static int test_mp_decr(void)
    DO(mp_decr(&a));
    if (a.sign == MP_NEG) {
       a.sign = MP_ZPOS;
-      if (mp_cmp_d(&a, 1uL) != MP_EQ) {
+      if (mp_cmp_d(&a, 1u) != MP_EQ) {
          goto LBL_ERR;
       }
    } else {
@@ -1623,7 +1623,7 @@ static int test_mp_decr(void)
    a.sign = MP_NEG;
    mp_set(&b, MP_MASK);
    b.sign = MP_NEG;
-   DO(mp_sub_d(&b, 1uL, &b));
+   DO(mp_sub_d(&b, 1u, &b));
    DO(mp_decr(&a));
    if (mp_cmp(&a, &b) != MP_EQ) {
       goto LBL_ERR;
@@ -1903,7 +1903,7 @@ static int test_s_mp_mul_karatsuba(void)
    int size;
 
    DOR(mp_init_multi(&a, &b, &c, &d, NULL));
-   for (size = MP_MUL_KARATSUBA_CUTOFF; size < MP_MUL_KARATSUBA_CUTOFF + 20; size++) {
+   for (size = MP_MUL_KARATSUBA_CUTOFF; size < (MP_MUL_KARATSUBA_CUTOFF + 20); size++) {
       DO(mp_rand(&a, size));
       DO(mp_rand(&b, size));
       DO(s_mp_mul_karatsuba(&a, &b, &c));
@@ -1927,7 +1927,7 @@ static int test_s_mp_sqr_karatsuba(void)
    int size;
 
    DOR(mp_init_multi(&a, &b, &c, NULL));
-   for (size = MP_SQR_KARATSUBA_CUTOFF; size < MP_SQR_KARATSUBA_CUTOFF + 20; size++) {
+   for (size = MP_SQR_KARATSUBA_CUTOFF; size < (MP_SQR_KARATSUBA_CUTOFF + 20); size++) {
       DO(mp_rand(&a, size));
       DO(s_mp_sqr_karatsuba(&a, &b));
       DO(s_mp_sqr(&a, &c));
@@ -1976,7 +1976,7 @@ static int test_s_mp_mul_toom(void)
    }
 #endif
 
-   for (size = MP_MUL_TOOM_CUTOFF; size < MP_MUL_TOOM_CUTOFF + 20; size++) {
+   for (size = MP_MUL_TOOM_CUTOFF; size < (MP_MUL_TOOM_CUTOFF + 20); size++) {
       DO(mp_rand(&a, size));
       DO(mp_rand(&b, size));
       DO(s_mp_mul_toom(&a, &b, &c));
@@ -2000,7 +2000,7 @@ static int test_s_mp_sqr_toom(void)
    int size;
 
    DOR(mp_init_multi(&a, &b, &c, NULL));
-   for (size = MP_SQR_TOOM_CUTOFF; size < MP_SQR_TOOM_CUTOFF + 20; size++) {
+   for (size = MP_SQR_TOOM_CUTOFF; size < (MP_SQR_TOOM_CUTOFF + 20); size++) {
       DO(mp_rand(&a, size));
       DO(s_mp_sqr_toom(&a, &b));
       DO(s_mp_sqr(&a, &c));
@@ -2038,7 +2038,7 @@ static int test_mp_radix_size(void)
    DOR(mp_init(&a));
 
    /* number to result in a different size for every base: 67^(4 * 67) */
-   mp_set(&a, 67);
+   mp_set(&a, 67u);
    DO(mp_expt_u32(&a, 268u, &a));
 
    for (radix = 2; radix < 65; radix++) {
@@ -2075,7 +2075,7 @@ static int test_s_mp_div_recursive(void)
 
    DOR(mp_init_multi(&a, &b, &c_q, &c_r, &d_q, &d_r, NULL));
 
-   for (size = MP_MUL_KARATSUBA_CUTOFF; size < 3 * MP_MUL_KARATSUBA_CUTOFF; size += 10) {
+   for (size = MP_MUL_KARATSUBA_CUTOFF; size < (3 * MP_MUL_KARATSUBA_CUTOFF); size += 10) {
       printf("\rsizes = %d / %d", 10 * size, size);
       /* Relation 10:1 */
       DO(mp_rand(&a, 10 * size));
@@ -2254,7 +2254,7 @@ static int test_mp_pack_unpack(void)
    DOR(mp_init_multi(&a, &b, NULL));
    DO(mp_rand(&a, 15));
 
-   count = mp_pack_count(&a, 0, 1);
+   count = mp_pack_count(&a, 0uL, 1uL);
 
    buf = malloc(count);
    if (buf == NULL) {
@@ -2262,10 +2262,10 @@ static int test_mp_pack_unpack(void)
       goto LBL_ERR;
    }
 
-   DO(mp_pack((void *)buf, count, &written, order, 1,
-              endianess, 0, &a));
-   DO(mp_unpack(&b, count, order, 1,
-                endianess, 0, (const void *)buf));
+   DO(mp_pack((void *)buf, count, &written, order, 1uL,
+              endianess, 0uL, &a));
+   DO(mp_unpack(&b, count, order, 1uL,
+                endianess, 0uL, (const void *)buf));
 
    if (mp_cmp(&a, &b) != MP_EQ) {
       fprintf(stderr, "pack/unpack cycle failed\n");
@@ -2289,7 +2289,7 @@ static int unit_tests(int argc, char **argv)
    } test[] = {
 #define T0(n)           { #n, test_##n }
 #define T1(n, o)        { #n, MP_HAS(o) ? test_##n : NULL }
-#define T2(n, o1, o2)   { #n, MP_HAS(o1) && MP_HAS(o2) ? test_##n : NULL }
+#define T2(n, o1, o2)   { #n, (MP_HAS(o1) && MP_HAS(o2)) ? test_##n : NULL }
       T0(feature_detection),
       T0(trivial_stuff),
       T2(mp_get_set_i32, MP_GET_I32, MP_GET_MAG_U32),
@@ -2351,7 +2351,7 @@ static int unit_tests(int argc, char **argv)
    s_mp_rand_jenkins_init(t);
    mp_rand_source(s_mp_rand_jenkins);
 
-   for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) {
+   for (i = 0; i < (sizeof(test) / sizeof(test[0])); ++i) {
       if (argc > 1) {
          for (j = 1; j < argc; ++j) {
             if (strstr(test[i].name, argv[j]) != NULL) {
diff --git a/etc/tune.c b/etc/tune.c
index 9657910..0b73734 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -281,7 +281,7 @@ int main(int argc, char **argv)
    /*int preset[8];*/
    char *endptr, *str;
 
-   uint64_t seed = 0xdeadbeef;
+   uint64_t seed = 0xdeadbeefULL;
 
    int opt;
    struct cutoffs orig, updated;
diff --git a/tommath.h b/tommath.h
index d024a33..68a1592 100644
--- a/tommath.h
+++ b/tommath.h
@@ -375,10 +375,10 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
 mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
 
 /* Increment "a" by one like "a++". Changes input! */
-#define mp_incr(a) mp_add_d((a), 1, (a))
+#define mp_incr(a) mp_add_d((a), 1u, (a))
 
 /* Decrement "a" by one like "a--". Changes input! */
-#define mp_decr(a) mp_sub_d((a), 1, (a))
+#define mp_decr(a) mp_sub_d((a), 1u, (a))
 
 /* ---> single digit functions <--- */