improve error handling * handle buffer full case * display error reason of `s_mp_get_token()` * display name of variables when `draw()`ing on error
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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 7cdad2c..25d9b5b 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -6,17 +6,14 @@
#define LTM_MTEST_RAND_SEED 23
#endif
-static void draw(const mp_int *a)
-{
- ndraw(a, "");
-}
+#define DRAW(a) do{ ndraw(&(a), #a); }while(0)
/*
Get tokens. It is just a very(!) simple fgets(3) that does not keep line endings.
Implementation follows along "man 3 fgets", some of which is quoted.
*/
-static char *s_mp_get_token(char *s, int size, FILE *stream)
+static int s_mp_get_token(char *s, int size, FILE *stream)
{
char *s_bar = s;
int c;
@@ -28,7 +25,7 @@ static char *s_mp_get_token(char *s, int size, FILE *stream)
if ((c = fgetc(stream)) == EOF) {
/* "Returns [...] NULL on error or when end of file occurs while no characters have been read" */
if ((s_bar == s) || (ferror(stream) != 0)) {
- return NULL;
+ return -1;
}
break;
}
@@ -45,16 +42,20 @@ static char *s_mp_get_token(char *s, int size, FILE *stream)
}
*s_bar++ = c;
}
+ if (size == 0) return -2;
/* "A terminating null byte ('\0') is stored after the last character in the buffer" */
*s_bar = '\0';
- return s;
+ return 0;
}
-#define GET_TOKEN(str, size, stream) \
- { \
- char *ret = s_mp_get_token((str), (size), (stream)); \
- if (!ret) { fprintf(stderr, "\n%d: s_mp_get_token failed\n", __LINE__); goto LBL_ERR; } \
- }
+#define TMP_(r,l) r ## _ ## l
+#define TMP(r,l) TMP_(r,l)
+
+#define GET_TOKEN(str, stream) \
+ do { \
+ int TMP(ret,__LINE__) = s_mp_get_token((str), sizeof(str), (stream)); \
+ if (TMP(ret,__LINE__) < 0) { fprintf(stderr, "\n%d: s_mp_get_token failed with error %d\n", __LINE__, TMP(ret,__LINE__)); goto LBL_ERR; } \
+ } while(0)
static int mtest_opponent(void)
{
@@ -115,33 +116,33 @@ static int mtest_opponent(void)
printf("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ",
add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n,
expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n);
- GET_TOKEN(cmd, 4095, stdin);
+ GET_TOKEN(cmd, stdin);
printf("%-6s ]\r", cmd);
fflush(stdout);
if (strcmp(cmd, "mul2d") == 0) {
++mul2d_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
sscanf(buf, "%u", &rr);
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_mul_2d(&a, (int)rr, &a));
a.sign = b.sign;
if (mp_cmp(&a, &b) != MP_EQ) {
printf("mul2d failed, rr == %u\n", rr);
- draw(&a);
- draw(&b);
+ DRAW(a);
+ DRAW(b);
goto LBL_ERR;
}
} else if (strcmp(cmd, "div2d") == 0) {
++div2d_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
sscanf(buf, "%u", &rr);
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_div_2d(&a, (int)rr, &a, &e));
@@ -151,26 +152,26 @@ static int mtest_opponent(void)
}
if (mp_cmp(&a, &b) != MP_EQ) {
printf("div2d failed, rr == %u\n", rr);
- draw(&a);
- draw(&b);
+ DRAW(a);
+ DRAW(b);
goto LBL_ERR;
}
} else if (strcmp(cmd, "add") == 0) {
++add_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_add(&d, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("add %lu failure!\n", add_n);
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
@@ -182,8 +183,8 @@ static int mtest_opponent(void)
DO(mp_from_sbin(&d, (uint8_t *) cmd, (size_t)rr));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("mp_signed_bin failure!\n");
- draw(&c);
- draw(&d);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
@@ -193,226 +194,226 @@ static int mtest_opponent(void)
DO(mp_from_ubin(&d, (uint8_t *) cmd, (size_t)rr));
if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("mp_unsigned_bin failure!\n");
- draw(&c);
- draw(&d);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
} else if (strcmp(cmd, "sub") == 0) {
++sub_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_sub(&d, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("sub %lu failure!\n", sub_n);
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
} else if (strcmp(cmd, "mul") == 0) {
++mul_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_mul(&d, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("mul %lu failure!\n", mul_n);
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
} else if (strcmp(cmd, "div") == 0) {
++div_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&d, buf, 64));
DO(mp_div(&a, &b, &e, &f));
if ((mp_cmp(&c, &e) != MP_EQ) || (mp_cmp(&d, &f) != MP_EQ)) {
printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e),
mp_cmp(&d, &f));
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
- draw(&e);
- draw(&f);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
+ DRAW(e);
+ DRAW(f);
goto LBL_ERR;
}
} else if (strcmp(cmd, "sqr") == 0) {
++sqr_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_copy(&a, &c));
DO(mp_sqr(&c, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
printf("sqr %lu failure!\n", sqr_n);
- draw(&a);
- draw(&b);
- draw(&c);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
goto LBL_ERR;
}
} else if (strcmp(cmd, "gcd") == 0) {
++gcd_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_gcd(&d, &b, &d));
d.sign = c.sign;
if (mp_cmp(&c, &d) != MP_EQ) {
printf("gcd %lu failure!\n", gcd_n);
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
} else if (strcmp(cmd, "lcm") == 0) {
++lcm_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_lcm(&d, &b, &d));
d.sign = c.sign;
if (mp_cmp(&c, &d) != MP_EQ) {
printf("lcm %lu failure!\n", lcm_n);
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
goto LBL_ERR;
}
} else if (strcmp(cmd, "expt") == 0) {
++expt_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&c, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&d, buf, 64));
DO(mp_copy(&a, &e));
DO(mp_exptmod(&e, &b, &c, &e));
if (mp_cmp(&d, &e) != MP_EQ) {
printf("expt %lu failure!\n", expt_n);
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
- draw(&e);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
+ DRAW(e);
goto LBL_ERR;
}
} else if (strcmp(cmd, "invmod") == 0) {
++inv_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
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, 1u) != MP_EQ) {
printf("inv [wrong value from MPI?!] failure\n");
- draw(&a);
- draw(&b);
- draw(&c);
- draw(&d);
- draw(&e);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
+ DRAW(d);
+ DRAW(e);
DO(mp_gcd(&a, &b, &e));
- draw(&e);
+ DRAW(e);
goto LBL_ERR;
}
} else if (strcmp(cmd, "div2") == 0) {
++div2_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_div_2(&a, &c));
if (mp_cmp(&c, &b) != MP_EQ) {
printf("div_2 %lu failure\n", div2_n);
- draw(&a);
- draw(&b);
- draw(&c);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
goto LBL_ERR;
}
} else if (strcmp(cmd, "mul2") == 0) {
++mul2_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_mul_2(&a, &c));
if (mp_cmp(&c, &b) != MP_EQ) {
printf("mul_2 %lu failure\n", mul2_n);
- draw(&a);
- draw(&b);
- draw(&c);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
goto LBL_ERR;
}
} else if (strcmp(cmd, "add_d") == 0) {
++add_d_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
sscanf(buf, "%d", &ix);
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_add_d(&a, (mp_digit)ix, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
printf("add_d %lu failure\n", add_d_n);
- draw(&a);
- draw(&b);
- draw(&c);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
printf("d == %d\n", ix);
goto LBL_ERR;
}
} else if (strcmp(cmd, "sub_d") == 0) {
++sub_d_n;
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&a, buf, 64));
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
sscanf(buf, "%d", &ix);
- GET_TOKEN(buf, 4095, stdin);
+ GET_TOKEN(buf, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_sub_d(&a, (mp_digit)ix, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
printf("sub_d %lu failure\n", sub_d_n);
- draw(&a);
- draw(&b);
- draw(&c);
+ DRAW(a);
+ DRAW(b);
+ DRAW(c);
printf("d == %d\n", ix);
goto LBL_ERR;
}