security/nss/lib/freebl/mpi/tests/mptest-5.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * Simple test driver for MPI library
     3  *
     4  * Test 5: Other number theoretic functions
     5  *
     6  * This Source Code Form is subject to the terms of the Mozilla Public
     7  * License, v. 2.0. If a copy of the MPL was not distributed with this
     8  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    10 #include <stdio.h>
    11 #include <stdlib.h>
    12 #include <string.h>
    13 #include <ctype.h>
    14 #include <limits.h>
    16 #include "mpi.h"
    18 int main(int argc, char *argv[])
    19 {
    20   mp_int   a, b, c, x, y;
    22   if(argc < 3) {
    23     fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
    24     return 1;
    25   }
    27   printf("Test 5: Number theoretic functions\n\n");
    29   mp_init(&a);
    30   mp_init(&b);
    32   mp_read_radix(&a, argv[1], 10);
    33   mp_read_radix(&b, argv[2], 10);
    35   printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    36   printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    38   mp_init(&c);
    39   printf("\nc = (a, b)\n");
    41   mp_gcd(&a, &b, &c);
    42   printf("Euclid: c = "); mp_print(&c, stdout); fputc('\n', stdout);
    43 /*
    44   mp_bgcd(&a, &b, &c);
    45   printf("Binary: c = "); mp_print(&c, stdout); fputc('\n', stdout);
    46 */
    47   mp_init(&x);
    48   mp_init(&y);
    49   printf("\nc = (a, b) = ax + by\n");
    51   mp_xgcd(&a, &b, &c, &x, &y);
    52   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    53   printf("x = "); mp_print(&x, stdout); fputc('\n', stdout);
    54   printf("y = "); mp_print(&y, stdout); fputc('\n', stdout);
    56   printf("\nc = a^-1 (mod b)\n");
    57   if(mp_invmod(&a, &b, &c) == MP_UNDEF) {
    58     printf("a has no inverse mod b\n");
    59   } else {
    60     printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    61   }
    63   mp_clear(&y);
    64   mp_clear(&x);
    65   mp_clear(&c);
    66   mp_clear(&b);
    67   mp_clear(&a);
    69   return 0;
    70 }

mercurial