Skip to content

Commit 4500d06

Browse files
committed
also no MP_WUR in mtest_opponent()
1 parent b250ec4 commit 4500d06

File tree

3 files changed

+81
-80
lines changed

3 files changed

+81
-80
lines changed

demo/mtest_opponent.c

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define MP_WUR /* TODO: result checks disabled for now, T.b.d. if it should even be done here */
21
#include "shared.h"
32

43
#ifdef LTM_MTEST_REAL_RAND
@@ -47,27 +46,27 @@ static int mtest_opponent(void)
4746
switch (abs(rand()) % 7) {
4847
case 0:
4948
mp_clear(&a);
50-
mp_init(&a);
49+
DO(mp_init(&a));
5150
break;
5251
case 1:
5352
mp_clear(&b);
54-
mp_init(&b);
53+
DO(mp_init(&b));
5554
break;
5655
case 2:
5756
mp_clear(&c);
58-
mp_init(&c);
57+
DO(mp_init(&c));
5958
break;
6059
case 3:
6160
mp_clear(&d);
62-
mp_init(&d);
61+
DO(mp_init(&d));
6362
break;
6463
case 4:
6564
mp_clear(&e);
66-
mp_init(&e);
65+
DO(mp_init(&e));
6766
break;
6867
case 5:
6968
mp_clear(&f);
70-
mp_init(&f);
69+
DO(mp_init(&f));
7170
break;
7271
case 6:
7372
break; /* don't clear any */
@@ -84,13 +83,13 @@ static int mtest_opponent(void)
8483
if (strcmp(cmd, "mul2d") == 0) {
8584
++mul2d_n;
8685
FGETS(buf, 4095, stdin);
87-
mp_read_radix(&a, buf, 64);
86+
DO(mp_read_radix(&a, buf, 64));
8887
FGETS(buf, 4095, stdin);
8988
sscanf(buf, "%u", &rr);
9089
FGETS(buf, 4095, stdin);
91-
mp_read_radix(&b, buf, 64);
90+
DO(mp_read_radix(&b, buf, 64));
9291

93-
mp_mul_2d(&a, (int)rr, &a);
92+
DO(mp_mul_2d(&a, (int)rr, &a));
9493
a.sign = b.sign;
9594
if (mp_cmp(&a, &b) != MP_EQ) {
9695
printf("mul2d failed, rr == %u\n", rr);
@@ -101,13 +100,13 @@ static int mtest_opponent(void)
101100
} else if (strcmp(cmd, "div2d") == 0) {
102101
++div2d_n;
103102
FGETS(buf, 4095, stdin);
104-
mp_read_radix(&a, buf, 64);
103+
DO(mp_read_radix(&a, buf, 64));
105104
FGETS(buf, 4095, stdin);
106105
sscanf(buf, "%u", &rr);
107106
FGETS(buf, 4095, stdin);
108-
mp_read_radix(&b, buf, 64);
107+
DO(mp_read_radix(&b, buf, 64));
109108

110-
mp_div_2d(&a, (int)rr, &a, &e);
109+
DO(mp_div_2d(&a, (int)rr, &a, &e));
111110
a.sign = b.sign;
112111
if ((a.used == b.used) && (a.used == 0)) {
113112
a.sign = b.sign = MP_ZPOS;
@@ -121,13 +120,13 @@ static int mtest_opponent(void)
121120
} else if (strcmp(cmd, "add") == 0) {
122121
++add_n;
123122
FGETS(buf, 4095, stdin);
124-
mp_read_radix(&a, buf, 64);
123+
DO(mp_read_radix(&a, buf, 64));
125124
FGETS(buf, 4095, stdin);
126-
mp_read_radix(&b, buf, 64);
125+
DO(mp_read_radix(&b, buf, 64));
127126
FGETS(buf, 4095, stdin);
128-
mp_read_radix(&c, buf, 64);
129-
mp_copy(&a, &d);
130-
mp_add(&d, &b, &d);
127+
DO(mp_read_radix(&c, buf, 64));
128+
DO(mp_copy(&a, &d));
129+
DO(mp_add(&d, &b, &d));
131130
if (mp_cmp(&c, &d) != MP_EQ) {
132131
printf("add %lu failure!\n", add_n);
133132
draw(&a);
@@ -140,9 +139,9 @@ static int mtest_opponent(void)
140139
/* test the sign/unsigned storage functions */
141140

142141
rr = (unsigned)mp_sbin_size(&c);
143-
mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
142+
DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
144143
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
145-
mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr);
144+
DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr));
146145
if (mp_cmp(&c, &d) != MP_EQ) {
147146
printf("mp_signed_bin failure!\n");
148147
draw(&c);
@@ -151,9 +150,9 @@ static int mtest_opponent(void)
151150
}
152151

153152
rr = (unsigned)mp_ubin_size(&c);
154-
mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
153+
DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
155154
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
156-
mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr);
155+
DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr));
157156
if (mp_cmp_mag(&c, &d) != MP_EQ) {
158157
printf("mp_unsigned_bin failure!\n");
159158
draw(&c);
@@ -164,13 +163,13 @@ static int mtest_opponent(void)
164163
} else if (strcmp(cmd, "sub") == 0) {
165164
++sub_n;
166165
FGETS(buf, 4095, stdin);
167-
mp_read_radix(&a, buf, 64);
166+
DO(mp_read_radix(&a, buf, 64));
168167
FGETS(buf, 4095, stdin);
169-
mp_read_radix(&b, buf, 64);
168+
DO(mp_read_radix(&b, buf, 64));
170169
FGETS(buf, 4095, stdin);
171-
mp_read_radix(&c, buf, 64);
172-
mp_copy(&a, &d);
173-
mp_sub(&d, &b, &d);
170+
DO(mp_read_radix(&c, buf, 64));
171+
DO(mp_copy(&a, &d));
172+
DO(mp_sub(&d, &b, &d));
174173
if (mp_cmp(&c, &d) != MP_EQ) {
175174
printf("sub %lu failure!\n", sub_n);
176175
draw(&a);
@@ -182,13 +181,13 @@ static int mtest_opponent(void)
182181
} else if (strcmp(cmd, "mul") == 0) {
183182
++mul_n;
184183
FGETS(buf, 4095, stdin);
185-
mp_read_radix(&a, buf, 64);
184+
DO(mp_read_radix(&a, buf, 64));
186185
FGETS(buf, 4095, stdin);
187-
mp_read_radix(&b, buf, 64);
186+
DO(mp_read_radix(&b, buf, 64));
188187
FGETS(buf, 4095, stdin);
189-
mp_read_radix(&c, buf, 64);
190-
mp_copy(&a, &d);
191-
mp_mul(&d, &b, &d);
188+
DO(mp_read_radix(&c, buf, 64));
189+
DO(mp_copy(&a, &d));
190+
DO(mp_mul(&d, &b, &d));
192191
if (mp_cmp(&c, &d) != MP_EQ) {
193192
printf("mul %lu failure!\n", mul_n);
194193
draw(&a);
@@ -200,15 +199,15 @@ static int mtest_opponent(void)
200199
} else if (strcmp(cmd, "div") == 0) {
201200
++div_n;
202201
FGETS(buf, 4095, stdin);
203-
mp_read_radix(&a, buf, 64);
202+
DO(mp_read_radix(&a, buf, 64));
204203
FGETS(buf, 4095, stdin);
205-
mp_read_radix(&b, buf, 64);
204+
DO(mp_read_radix(&b, buf, 64));
206205
FGETS(buf, 4095, stdin);
207-
mp_read_radix(&c, buf, 64);
206+
DO(mp_read_radix(&c, buf, 64));
208207
FGETS(buf, 4095, stdin);
209-
mp_read_radix(&d, buf, 64);
208+
DO(mp_read_radix(&d, buf, 64));
210209

211-
mp_div(&a, &b, &e, &f);
210+
DO(mp_div(&a, &b, &e, &f));
212211
if ((mp_cmp(&c, &e) != MP_EQ) || (mp_cmp(&d, &f) != MP_EQ)) {
213212
printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e),
214213
mp_cmp(&d, &f));
@@ -224,11 +223,11 @@ static int mtest_opponent(void)
224223
} else if (strcmp(cmd, "sqr") == 0) {
225224
++sqr_n;
226225
FGETS(buf, 4095, stdin);
227-
mp_read_radix(&a, buf, 64);
226+
DO(mp_read_radix(&a, buf, 64));
228227
FGETS(buf, 4095, stdin);
229-
mp_read_radix(&b, buf, 64);
230-
mp_copy(&a, &c);
231-
mp_sqr(&c, &c);
228+
DO(mp_read_radix(&b, buf, 64));
229+
DO(mp_copy(&a, &c));
230+
DO(mp_sqr(&c, &c));
232231
if (mp_cmp(&b, &c) != MP_EQ) {
233232
printf("sqr %lu failure!\n", sqr_n);
234233
draw(&a);
@@ -239,13 +238,13 @@ static int mtest_opponent(void)
239238
} else if (strcmp(cmd, "gcd") == 0) {
240239
++gcd_n;
241240
FGETS(buf, 4095, stdin);
242-
mp_read_radix(&a, buf, 64);
241+
DO(mp_read_radix(&a, buf, 64));
243242
FGETS(buf, 4095, stdin);
244-
mp_read_radix(&b, buf, 64);
243+
DO(mp_read_radix(&b, buf, 64));
245244
FGETS(buf, 4095, stdin);
246-
mp_read_radix(&c, buf, 64);
247-
mp_copy(&a, &d);
248-
mp_gcd(&d, &b, &d);
245+
DO(mp_read_radix(&c, buf, 64));
246+
DO(mp_copy(&a, &d));
247+
DO(mp_gcd(&d, &b, &d));
249248
d.sign = c.sign;
250249
if (mp_cmp(&c, &d) != MP_EQ) {
251250
printf("gcd %lu failure!\n", gcd_n);
@@ -258,13 +257,13 @@ static int mtest_opponent(void)
258257
} else if (strcmp(cmd, "lcm") == 0) {
259258
++lcm_n;
260259
FGETS(buf, 4095, stdin);
261-
mp_read_radix(&a, buf, 64);
260+
DO(mp_read_radix(&a, buf, 64));
262261
FGETS(buf, 4095, stdin);
263-
mp_read_radix(&b, buf, 64);
262+
DO(mp_read_radix(&b, buf, 64));
264263
FGETS(buf, 4095, stdin);
265-
mp_read_radix(&c, buf, 64);
266-
mp_copy(&a, &d);
267-
mp_lcm(&d, &b, &d);
264+
DO(mp_read_radix(&c, buf, 64));
265+
DO(mp_copy(&a, &d));
266+
DO(mp_lcm(&d, &b, &d));
268267
d.sign = c.sign;
269268
if (mp_cmp(&c, &d) != MP_EQ) {
270269
printf("lcm %lu failure!\n", lcm_n);
@@ -277,15 +276,15 @@ static int mtest_opponent(void)
277276
} else if (strcmp(cmd, "expt") == 0) {
278277
++expt_n;
279278
FGETS(buf, 4095, stdin);
280-
mp_read_radix(&a, buf, 64);
279+
DO(mp_read_radix(&a, buf, 64));
281280
FGETS(buf, 4095, stdin);
282-
mp_read_radix(&b, buf, 64);
281+
DO(mp_read_radix(&b, buf, 64));
283282
FGETS(buf, 4095, stdin);
284-
mp_read_radix(&c, buf, 64);
283+
DO(mp_read_radix(&c, buf, 64));
285284
FGETS(buf, 4095, stdin);
286-
mp_read_radix(&d, buf, 64);
287-
mp_copy(&a, &e);
288-
mp_exptmod(&e, &b, &c, &e);
285+
DO(mp_read_radix(&d, buf, 64));
286+
DO(mp_copy(&a, &e));
287+
DO(mp_exptmod(&e, &b, &c, &e));
289288
if (mp_cmp(&d, &e) != MP_EQ) {
290289
printf("expt %lu failure!\n", expt_n);
291290
draw(&a);
@@ -298,32 +297,32 @@ static int mtest_opponent(void)
298297
} else if (strcmp(cmd, "invmod") == 0) {
299298
++inv_n;
300299
FGETS(buf, 4095, stdin);
301-
mp_read_radix(&a, buf, 64);
300+
DO(mp_read_radix(&a, buf, 64));
302301
FGETS(buf, 4095, stdin);
303-
mp_read_radix(&b, buf, 64);
302+
DO(mp_read_radix(&b, buf, 64));
304303
FGETS(buf, 4095, stdin);
305-
mp_read_radix(&c, buf, 64);
306-
mp_invmod(&a, &b, &d);
307-
mp_mulmod(&d, &a, &b, &e);
304+
DO(mp_read_radix(&c, buf, 64));
305+
DO(mp_invmod(&a, &b, &d));
306+
DO(mp_mulmod(&d, &a, &b, &e));
308307
if (mp_cmp_d(&e, 1uL) != MP_EQ) {
309308
printf("inv [wrong value from MPI?!] failure\n");
310309
draw(&a);
311310
draw(&b);
312311
draw(&c);
313312
draw(&d);
314313
draw(&e);
315-
mp_gcd(&a, &b, &e);
314+
DO(mp_gcd(&a, &b, &e));
316315
draw(&e);
317316
goto LBL_ERR;
318317
}
319318

320319
} else if (strcmp(cmd, "div2") == 0) {
321320
++div2_n;
322321
FGETS(buf, 4095, stdin);
323-
mp_read_radix(&a, buf, 64);
322+
DO(mp_read_radix(&a, buf, 64));
324323
FGETS(buf, 4095, stdin);
325-
mp_read_radix(&b, buf, 64);
326-
mp_div_2(&a, &c);
324+
DO(mp_read_radix(&b, buf, 64));
325+
DO(mp_div_2(&a, &c));
327326
if (mp_cmp(&c, &b) != MP_EQ) {
328327
printf("div_2 %lu failure\n", div2_n);
329328
draw(&a);
@@ -334,10 +333,10 @@ static int mtest_opponent(void)
334333
} else if (strcmp(cmd, "mul2") == 0) {
335334
++mul2_n;
336335
FGETS(buf, 4095, stdin);
337-
mp_read_radix(&a, buf, 64);
336+
DO(mp_read_radix(&a, buf, 64));
338337
FGETS(buf, 4095, stdin);
339-
mp_read_radix(&b, buf, 64);
340-
mp_mul_2(&a, &c);
338+
DO(mp_read_radix(&b, buf, 64));
339+
DO(mp_mul_2(&a, &c));
341340
if (mp_cmp(&c, &b) != MP_EQ) {
342341
printf("mul_2 %lu failure\n", mul2_n);
343342
draw(&a);
@@ -348,12 +347,12 @@ static int mtest_opponent(void)
348347
} else if (strcmp(cmd, "add_d") == 0) {
349348
++add_d_n;
350349
FGETS(buf, 4095, stdin);
351-
mp_read_radix(&a, buf, 64);
350+
DO(mp_read_radix(&a, buf, 64));
352351
FGETS(buf, 4095, stdin);
353352
sscanf(buf, "%d", &ix);
354353
FGETS(buf, 4095, stdin);
355-
mp_read_radix(&b, buf, 64);
356-
mp_add_d(&a, (mp_digit)ix, &c);
354+
DO(mp_read_radix(&b, buf, 64));
355+
DO(mp_add_d(&a, (mp_digit)ix, &c));
357356
if (mp_cmp(&b, &c) != MP_EQ) {
358357
printf("add_d %lu failure\n", add_d_n);
359358
draw(&a);
@@ -365,12 +364,12 @@ static int mtest_opponent(void)
365364
} else if (strcmp(cmd, "sub_d") == 0) {
366365
++sub_d_n;
367366
FGETS(buf, 4095, stdin);
368-
mp_read_radix(&a, buf, 64);
367+
DO(mp_read_radix(&a, buf, 64));
369368
FGETS(buf, 4095, stdin);
370369
sscanf(buf, "%d", &ix);
371370
FGETS(buf, 4095, stdin);
372-
mp_read_radix(&b, buf, 64);
373-
mp_sub_d(&a, (mp_digit)ix, &c);
371+
DO(mp_read_radix(&b, buf, 64));
372+
DO(mp_sub_d(&a, (mp_digit)ix, &c));
374373
if (mp_cmp(&b, &c) != MP_EQ) {
375374
printf("sub_d %lu failure\n", sub_d_n);
376375
draw(&a);

demo/shared.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@
1616

1717
#include "tommath_private.h"
1818

19+
20+
#define EXPECT(a) do { if (!(a)) { fprintf(stderr, "%d: EXPECT(%s) failed\n", __LINE__, #a); goto LBL_ERR; } } while(0)
21+
#define DO_WHAT(a, what) do { mp_err err; if ((err = (a)) != MP_OKAY) { fprintf(stderr, "%d: DO(%s) failed: %s\n", __LINE__, #a, mp_error_to_string(err)); what; } } while(0)
22+
#define DO(a) DO_WHAT(a, goto LBL_ERR)
23+
#define DOR(a) DO_WHAT(a, return EXIT_FAILURE)
24+
25+
1926
extern void ndraw(const mp_int* a, const char* name);
2027
extern void print_header(void);

demo/test.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ static int test_feature_detection(void)
9494
return EXIT_SUCCESS;
9595
}
9696

97-
#define EXPECT(a) do { if (!(a)) { fprintf(stderr, "%d: EXPECT(%s) failed\n", __LINE__, #a); goto LBL_ERR; } } while(0)
98-
#define DO_WHAT(a, what) do { mp_err err; if ((err = (a)) != MP_OKAY) { fprintf(stderr, "%d: DO(%s) failed: %s\n", __LINE__, #a, mp_error_to_string(err)); what; } } while(0)
99-
#define DO(a) DO_WHAT(a, goto LBL_ERR)
100-
#define DOR(a) DO_WHAT(a, return EXIT_FAILURE)
101-
10297
static int test_trivial_stuff(void)
10398
{
10499
mp_int a, b, c, d;

0 commit comments

Comments
 (0)