annotate etc/mont.c @ 386:97db060d0ef5 libtommath-orig libtommath-0.40

Update to LibTomMath 0.40
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 03:11:15 +0000
parents 91fbc376f010
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
282
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* tests the montgomery routines */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #include <tommath.h>
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 int main(void)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 mp_int modulus, R, p, pp;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 mp_digit mp;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 long x, y;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 srand(time(NULL));
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 mp_init_multi(&modulus, &R, &p, &pp, NULL);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /* loop through various sizes */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 for (x = 4; x < 256; x++) {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 printf("DIGITS == %3ld...", x); fflush(stdout);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 /* make up the odd modulus */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 mp_rand(&modulus, x);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 modulus.dp[0] |= 1;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 /* now find the R value */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 mp_montgomery_calc_normalization(&R, &modulus);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 mp_montgomery_setup(&modulus, &mp);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 /* now run through a bunch tests */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 for (y = 0; y < 1000; y++) {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 mp_rand(&p, x/2); /* p = random */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 mp_mul(&p, &R, &pp); /* pp = R * p */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 mp_montgomery_reduce(&pp, &modulus, mp);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 /* should be equal to p */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 if (mp_cmp(&pp, &p) != MP_EQ) {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 printf("FAILURE!\n");
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 exit(-1);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 printf("PASSED\n");
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 return 0;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
386
97db060d0ef5 Update to LibTomMath 0.40
Matt Johnston <matt@ucc.asn.au>
parents: 282
diff changeset
47
97db060d0ef5 Update to LibTomMath 0.40
Matt Johnston <matt@ucc.asn.au>
parents: 282
diff changeset
48 /* $Source: /cvs/libtom/libtommath/etc/mont.c,v $ */
97db060d0ef5 Update to LibTomMath 0.40
Matt Johnston <matt@ucc.asn.au>
parents: 282
diff changeset
49 /* $Revision: 1.2 $ */
97db060d0ef5 Update to LibTomMath 0.40
Matt Johnston <matt@ucc.asn.au>
parents: 282
diff changeset
50 /* $Date: 2005/05/05 14:38:47 $ */