s4:torture: Adapt KDC canon test to Heimdal upstream changes
[samba.git] / source4 / heimdal / lib / hcrypto / libtommath / etc / mont.c
1 /* tests the montgomery routines */
2 #include <tommath.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 int main(void)
7 {
8    mp_int modulus, R, p, pp;
9    mp_digit mp;
10    int x, y;
11
12    srand(time(NULL));
13    mp_init_multi(&modulus, &R, &p, &pp, NULL);
14
15    /* loop through various sizes */
16    for (x = 4; x < 256; x++) {
17       printf("DIGITS == %3d...", x);
18       fflush(stdout);
19
20       /* make up the odd modulus */
21       mp_rand(&modulus, x);
22       modulus.dp[0] |= 1uL;
23
24       /* now find the R value */
25       mp_montgomery_calc_normalization(&R, &modulus);
26       mp_montgomery_setup(&modulus, &mp);
27
28       /* now run through a bunch tests */
29       for (y = 0; y < 1000; y++) {
30          mp_rand(&p, x/2);        /* p = random */
31          mp_mul(&p, &R, &pp);     /* pp = R * p */
32          mp_montgomery_reduce(&pp, &modulus, mp);
33
34          /* should be equal to p */
35          if (mp_cmp(&pp, &p) != MP_EQ) {
36             printf("FAILURE!\n");
37             exit(-1);
38          }
39       }
40       printf("PASSED\n");
41    }
42
43    return 0;
44 }