xref: /minix3/crypto/external/bsd/heimdal/dist/lib/hcrypto/libtommath/etc/mont.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: mont.c,v 1.1.1.2 2014/04/24 12:45:39 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /* tests the montgomery routines */
4ebfedea0SLionel Sambuc #include <tommath.h>
5ebfedea0SLionel Sambuc 
main(void)6ebfedea0SLionel Sambuc int main(void)
7ebfedea0SLionel Sambuc {
8ebfedea0SLionel Sambuc    mp_int modulus, R, p, pp;
9ebfedea0SLionel Sambuc    mp_digit mp;
10ebfedea0SLionel Sambuc    long x, y;
11ebfedea0SLionel Sambuc 
12ebfedea0SLionel Sambuc    srand(time(NULL));
13ebfedea0SLionel Sambuc    mp_init_multi(&modulus, &R, &p, &pp, NULL);
14ebfedea0SLionel Sambuc 
15ebfedea0SLionel Sambuc    /* loop through various sizes */
16ebfedea0SLionel Sambuc    for (x = 4; x < 256; x++) {
17ebfedea0SLionel Sambuc        printf("DIGITS == %3ld...", x); fflush(stdout);
18ebfedea0SLionel Sambuc 
19ebfedea0SLionel Sambuc        /* make up the odd modulus */
20ebfedea0SLionel Sambuc        mp_rand(&modulus, x);
21ebfedea0SLionel Sambuc        modulus.dp[0] |= 1;
22ebfedea0SLionel Sambuc 
23ebfedea0SLionel Sambuc        /* now find the R value */
24ebfedea0SLionel Sambuc        mp_montgomery_calc_normalization(&R, &modulus);
25ebfedea0SLionel Sambuc        mp_montgomery_setup(&modulus, &mp);
26ebfedea0SLionel Sambuc 
27ebfedea0SLionel Sambuc        /* now run through a bunch tests */
28ebfedea0SLionel Sambuc        for (y = 0; y < 1000; y++) {
29ebfedea0SLionel Sambuc            mp_rand(&p, x/2);        /* p = random */
30ebfedea0SLionel Sambuc            mp_mul(&p, &R, &pp);     /* pp = R * p */
31ebfedea0SLionel Sambuc            mp_montgomery_reduce(&pp, &modulus, mp);
32ebfedea0SLionel Sambuc 
33ebfedea0SLionel Sambuc            /* should be equal to p */
34ebfedea0SLionel Sambuc            if (mp_cmp(&pp, &p) != MP_EQ) {
35ebfedea0SLionel Sambuc               printf("FAILURE!\n");
36ebfedea0SLionel Sambuc               exit(-1);
37ebfedea0SLionel Sambuc            }
38ebfedea0SLionel Sambuc        }
39ebfedea0SLionel Sambuc        printf("PASSED\n");
40ebfedea0SLionel Sambuc     }
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc     return 0;
43ebfedea0SLionel Sambuc }
44ebfedea0SLionel Sambuc 
45ebfedea0SLionel Sambuc 
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc 
49ebfedea0SLionel Sambuc 
50ebfedea0SLionel Sambuc /* Source: /cvs/libtom/libtommath/etc/mont.c,v  */
51ebfedea0SLionel Sambuc /* Revision: 1.2  */
52ebfedea0SLionel Sambuc /* Date: 2005/05/05 14:38:47  */
53