forked from libtom/libtommath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbn_mp_fft_mul.c
164 lines (148 loc) · 3.6 KB
/
bn_mp_fft_mul.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <tommath.h>
#ifdef BN_MP_FFT_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, [email protected], http://libtom.org
*/
/* Multiplication with FHT convolution */
#if defined (MP_28BIT) || defined (MP_64BIT)
int mp_fft_mul(mp_int *a, mp_int *b, mp_int *c)
{
double *fa, *fb;
fa = NULL;
fb = NULL;
int length, e,k;
mp_int r;
if (mp_iszero(a) == MP_YES || mp_iszero(b) == MP_YES) {
return MP_OKAY;
}
if (a->used == 1) {
if (a->dp[0] == 1) {
if ((e = mp_copy(b,c)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}
if (b->used == 1) {
if (b->dp[0] == 1) {
if ((e = mp_copy(a,c)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}
/* rounds the edges of the stair a bit (good for equal sized multipliers) */
/* il2 = ilog2((unsigned int)MAX(a->used,b->used));
extra = (il2<12)?powtwos[il2-2]:powtwos[8];
if( a->used < powtwos[il2]+extra ){
return mp_toom_cook_5_mul(a,b,c);
}
*/
if ((e = mp_dp_to_fft(a, &fa, b, &fb, &length)) != MP_OKAY) {
if (fa != NULL) free(fa);
if (fb != NULL) free(fb);
return e;
}
if ((e = mp_fft(fa, fb, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
if (fb != NULL) free(fb);
return e;
}
/*
for(k=0;k< length;k++){
printf("%f,",fa[k]);
}
puts("");
for(k=0;k< length;k++){
printf("%f,",fb[k]);
}
puts("");
*/
if( (e = mp_init(&r) ) != MP_OKAY){
return e;
}
if ((e = mp_fft_to_dp(fb, &r, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
if (fb != NULL) free(fb);
mp_clear(&r);
return e;
}
free(fa);
free(fb);
mp_exch(&r,c);
mp_clear(&r);
return MP_OKAY;
}
int mp_fft_mul_float(mp_int *a, mp_int *b, mp_int *c)
{
float *fa, *fb;
fa = NULL;
fb = NULL;
int length, e,k;
mp_int r;
if (mp_iszero(a) == MP_YES || mp_iszero(b) == MP_YES) {
return MP_OKAY;
}
if (a->used == 1) {
if (a->dp[0] == 1) {
if ((e = mp_copy(b,c)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}
if (b->used == 1) {
if (b->dp[0] == 1) {
if ((e = mp_copy(a,c)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}
/* rounds the edges of the stair a bit (good for equal sized multipliers) */
/* il2 = ilog2((unsigned int)MAX(a->used,b->used));
extra = (il2<12)?powtwos[il2-2]:powtwos[8];
if( a->used < powtwos[il2]+extra ){
return mp_toom_cook_5_mul(a,b,c);
}
*/
if ((e = mp_dp_to_fft_float(a, &fa, b, &fb, &length)) != MP_OKAY) {
if (fa != NULL) free(fa);
if (fb != NULL) free(fb);
return e;
}
if ((e = mp_fft_float(fa, fb, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
if (fb != NULL) free(fb);
return e;
}
if( (e = mp_init(&r) ) != MP_OKAY){
return e;
}
if ((e = mp_fft_to_dp_float(fb, &r, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
if (fb != NULL) free(fb);
mp_clear(&r);
return e;
}
free(fa);
free(fb);
mp_exch(&r,c);
mp_clear(&r);
return MP_OKAY;
}
#else
#error only for 28 bit digits for now
#endif
#endif