-
+ 5EB23A86515CC3CB7E47FCDB287828FA70F21A62026AB715BE5121F7793ABD90F5929FBA94F6DAA9B3FDB62E159D3319920903DFC7183B2890F492265C96E081
smg_comms/mpi/tests/test_mpi.c
(0 . 0)(1 . 145)
8395 #include "mpi.h"
8396 #include "mpi-internal.h" /* for BITS_PER_MPI_LIMB */
8397 #include <stdlib.h>
8398
8399 void err(char *msg)
8400 {
8401 fprintf(stderr, "%s\n", msg);
8402 exit(1);
8403 }
8404
8405 void terpri(FILE *fp)
8406 {
8407 fprintf(fp, "\n");
8408 }
8409
8410 void print_results(MPI in, MPI out, char * title)
8411 {
8412 fprintf(stdout, "******** %s ********", title);
8413 terpri(stdout);
8414
8415 fprintf(stdout, "input : ");
8416 mpi_print(stdout, in, 1);
8417 terpri(stdout);
8418
8419 fprintf(stdout, "output: ");
8420 mpi_print(stdout, out, 1);
8421 terpri(stdout);
8422
8423 terpri(stdout);
8424 fflush(stdout);
8425 }
8426
8427 /*
8428 * Test that will fail on original code and will pass after EuCrypt fix is applied.
8429 */
8430 void test_rshift()
8431 {
8432 MPI out, in, copy_in;
8433 out = mpi_alloc(0);
8434 in = mpi_alloc(0);
8435 copy_in = mpi_alloc(0);
8436
8437 mpi_fromstr(out, "0x20E92FE28E1929"); /* some value */
8438 mpi_fromstr(in, "0x2000000010000001000000002");
8439 mpi_fromstr(copy_in, "0x2000000010000001000000002"); /* to make sure the actual input is print, since call can modify in */
8440
8441 /* print value of BITS_PER_MPI_LIMB */
8442 fprintf(stdout, "BITS_PER_MPI_LIMB is %d\n", BITS_PER_MPI_LIMB);
8443
8444 /* shift by 0 */
8445 mpi_tdiv_q_2exp(out, in, 0);
8446 print_results(copy_in, out, "TEST: right shift by 0");
8447
8448 /* shift by multiple of BITS_PER_MPI_LIMB */
8449 mpi_fromstr(in, "0x2000000010000001000000002");
8450
8451 mpi_tdiv_q_2exp(out, in, BITS_PER_MPI_LIMB);
8452 print_results(copy_in, out, "TEST: right shift by BITS_PER_MPI_LIMB");
8453
8454 /* shift by non-multiple of BITS_PER_MPI_LIMB */
8455 mpi_fromstr(in, "0x2000000010000001000000002");
8456 mpi_tdiv_q_2exp(out, in, BITS_PER_MPI_LIMB - 3);
8457 print_results(copy_in, out, "TEST: right shift by BITS_PER_MPI_LIMB - 3");
8458
8459 mpi_free(copy_in);
8460 mpi_free(out);
8461 mpi_free(in);
8462 }
8463
8464 void test_highbit()
8465 {
8466 MPI in, set_out, clear_out;
8467
8468 in = mpi_alloc(0);
8469 set_out = mpi_alloc(0);
8470 clear_out = mpi_alloc(0);
8471
8472 mpi_fromstr(in, "0x2000000010000002000000004");
8473 mpi_fromstr(set_out, "0x2000000010000002000000004");
8474 mpi_fromstr(clear_out, "0x2000000010000002000000004");
8475
8476 mpi_set_highbit(set_out, 91);
8477 print_results(in, set_out, "TEST: mpi_set_highbit(in, 91)");
8478
8479 mpi_fromstr(set_out, "0x2000000010000002000000004");
8480 mpi_set_highbit(set_out, 96);
8481 print_results(in, set_out, "TEST: mpi_set_highbit(in, 96)");
8482
8483
8484 mpi_clear_highbit(clear_out, 96);
8485 print_results(in, clear_out, "TEST: mpi_clear_highbit(in, 96)");
8486
8487 mpi_fromstr(clear_out, "0x2000000010000002000000004");
8488 mpi_clear_highbit(clear_out, 1);
8489 print_results(in, clear_out, "TEST: mpi_clear_highbit(in, 1)");
8490
8491 mpi_free(in);
8492 mpi_free(set_out);
8493 mpi_free(clear_out);
8494 }
8495
8496 void test_get_nbits()
8497 {
8498 MPI m;
8499 int nbits;
8500
8501 m = mpi_alloc(0);
8502 mpi_fromstr(m, "0x0");
8503 nbits = mpi_get_nbits(m);
8504 print_results(m, m, "TEST: get_nbits");
8505 fprintf(stdout, "nbits: %d\n", nbits);
8506 mpi_free(m);
8507 }
8508
8509 int main(int ac, char **av)
8510 {
8511 MPI a, b, y;
8512 int r;
8513
8514 test_rshift();
8515 test_highbit();
8516 test_get_nbits();
8517
8518 r = secmem_init(1000);
8519 if (r==0) err("secmem init");
8520
8521 a = mpi_alloc_secure(0);
8522 b = mpi_alloc_secure(0);
8523 y = mpi_alloc_secure(0);
8524 mpi_fromstr(a, "0x1B0B206C488601");
8525 mpi_fromstr(b, "0x20E92FE28E1929");
8526 mpi_mul(y, a, b);
8527 mpi_free(a);
8528 mpi_free(b);
8529
8530 fprintf(stdout, "******** TEST: mpi_mul, see README ********");
8531 terpri(stdout);
8532 mpi_print(stdout, y, 1);
8533 mpi_free(y);
8534
8535 terpri(stdout);
8536 secmem_term();
8537
8538 return 0;
8539 }