summaryrefslogtreecommitdiffstats
path: root/third_party/heimdal/lib/hcrypto/libtommath/etc/mont.c
blob: 4652410d0c091d76dfd341d9504f26084453d21b (plain)
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
/* tests the montgomery routines */
#include <tommath.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
   mp_int modulus, R, p, pp;
   mp_digit mp;
   int x, y;

   srand(time(NULL));
   mp_init_multi(&modulus, &R, &p, &pp, NULL);

   /* loop through various sizes */
   for (x = 4; x < 256; x++) {
      printf("DIGITS == %3d...", x);
      fflush(stdout);

      /* make up the odd modulus */
      mp_rand(&modulus, x);
      modulus.dp[0] |= 1uL;

      /* now find the R value */
      mp_montgomery_calc_normalization(&R, &modulus);
      mp_montgomery_setup(&modulus, &mp);

      /* now run through a bunch tests */
      for (y = 0; y < 1000; y++) {
         mp_rand(&p, x/2);        /* p = random */
         mp_mul(&p, &R, &pp);     /* pp = R * p */
         mp_montgomery_reduce(&pp, &modulus, mp);

         /* should be equal to p */
         if (mp_cmp(&pp, &p) != MP_EQ) {
            printf("FAILURE!\n");
            exit(-1);
         }
      }
      printf("PASSED\n");
   }

   return 0;
}