Bump actions/upload-artifacts version
[libtommath.git] / etc / mont.c
blob3d844496e92dbc02472b4f34912824640e00e0bc
1 /* tests the montgomery routines */
2 #include <tommath.h>
3 #include <stdlib.h>
4 #include <time.h>
6 int main(void)
8 mp_int modulus, R, p, pp;
9 mp_digit mp;
10 mp_err err;
11 int x, y;
13 srand(time(NULL));
14 if ((err = mp_init_multi(&modulus, &R, &p, &pp, NULL)) != MP_OKAY) goto LTM_ERR;
16 /* loop through various sizes */
17 for (x = 4; x < 256; x++) {
18 printf("DIGITS == %3d...", x);
19 fflush(stdout);
21 /* make up the odd modulus */
22 if ((err = mp_rand(&modulus, x)) != MP_OKAY) goto LTM_ERR;
23 modulus.dp[0] |= 1uL;
25 /* now find the R value */
26 if ((err = mp_montgomery_calc_normalization(&R, &modulus)) != MP_OKAY) goto LTM_ERR;
27 if ((err = mp_montgomery_setup(&modulus, &mp)) != MP_OKAY) goto LTM_ERR;
29 /* now run through a bunch tests */
30 for (y = 0; y < 1000; y++) {
31 /* p = random */
32 if ((err = mp_rand(&p, x/2)) != MP_OKAY) goto LTM_ERR;
33 /* pp = R * p */
34 if ((err = mp_mul(&p, &R, &pp)) != MP_OKAY) goto LTM_ERR;
35 if ((err = mp_montgomery_reduce(&pp, &modulus, mp)) != MP_OKAY) goto LTM_ERR;
37 /* should be equal to p */
38 if (mp_cmp(&pp, &p) != MP_EQ) {
39 printf("FAILURE!\n");
40 exit(-1);
43 printf("PASSED\n");
46 LTM_ERR:
47 return 0;