annotate bn_mp_karatsuba_sqr.c @ 282:91fbc376f010 libtommath-orig libtommath-0.35

Import of libtommath 0.35 From ltm-0.35.tar.bz2 SHA1 of 3f193dbae9351e92d02530994fa18236f7fde01c
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 13:16:18 +0000
parents
children 97db060d0ef5
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 #include <tommath.h>
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 #ifdef BN_MP_KARATSUBA_SQR_C
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 *
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * LibTomMath is a library that provides multiple-precision
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * integer arithmetic as well as number theoretic functionality.
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 *
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * The library was designed directly after the MPI library by
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Michael Fromberger but has been written from scratch with
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * additional optimizations in place.
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 *
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * The library is free for all purposes without any express
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * guarantee it works.
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 *
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * Tom St Denis, [email protected], http://math.libtomcrypt.org
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
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 /* Karatsuba squaring, computes b = a*a using three
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 * half size squarings
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 * See comments of karatsuba_mul for details. It
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * is essentially the same algorithm but merely
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * tuned to perform recursive squarings.
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 int mp_karatsuba_sqr (mp_int * a, mp_int * b)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 mp_int x0, x1, t1, t2, x0x0, x1x1;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 int B, err;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 err = MP_MEM;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 /* min # of digits */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 B = a->used;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 /* now divide in two */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 B = B >> 1;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 /* init copy all the temps */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 if (mp_init_size (&x0, B) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 goto ERR;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 if (mp_init_size (&x1, a->used - B) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 goto X0;
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 /* init temps */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if (mp_init_size (&t1, a->used * 2) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 goto X1;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 if (mp_init_size (&t2, a->used * 2) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 goto T1;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if (mp_init_size (&x0x0, B * 2) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 goto T2;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 goto X0X0;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 register int x;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 register mp_digit *dst, *src;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 src = a->dp;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 /* now shift the digits */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 dst = x0.dp;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 for (x = 0; x < B; x++) {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 *dst++ = *src++;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 dst = x1.dp;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 for (x = B; x < a->used; x++) {
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 *dst++ = *src++;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 x0.used = B;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 x1.used = a->used - B;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 mp_clamp (&x0);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 /* now calc the products x0*x0 and x1*x1 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 if (mp_sqr (&x0, &x0x0) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 goto X1X1; /* x0x0 = x0*x0 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if (mp_sqr (&x1, &x1x1) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 goto X1X1; /* x1x1 = x1*x1 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 /* now calc (x1-x0)**2 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 if (mp_sub (&x1, &x0, &t1) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 goto X1X1; /* t1 = x1 - x0 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 if (mp_sqr (&t1, &t1) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 /* add x0y0 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 goto X1X1; /* t2 = x0x0 + x1x1 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 if (mp_sub (&t2, &t1, &t1) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 goto X1X1; /* t1 = x0x0 + x1x1 - (x1-x0)*(x1-x0) */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 /* shift by B */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 if (mp_lshd (&t1, B) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 goto X1X1; /* x1x1 = x1x1 << 2*B */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 if (mp_add (&x0x0, &t1, &t1) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 goto X1X1; /* t1 = x0x0 + t1 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 if (mp_add (&t1, &x1x1, b) != MP_OKAY)
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 err = MP_OKAY;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 X1X1:mp_clear (&x1x1);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 X0X0:mp_clear (&x0x0);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 T2:mp_clear (&t2);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 T1:mp_clear (&t1);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 X1:mp_clear (&x1);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 X0:mp_clear (&x0);
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 ERR:
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 return err;
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 }
91fbc376f010 Import of libtommath 0.35
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #endif