forked from libtom/libtommath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbn_mp_fft_sqr.c
106 lines (98 loc) · 2.45 KB
/
bn_mp_fft_sqr.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
#include <tommath.h>
#ifdef BN_MP_FFT_SQR_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_sqr(mp_int *a,mp_int *c)
{
double *fa;
fa = NULL;
int length, e;
if (mp_iszero(a) == MP_YES) {
return MP_OKAY;
}
if (a->used == 1) {
if (a->dp[0] == 1) {
if ((e = mp_set_int(c,1)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}
/* rounds the edges of the stair a bit */
/*
il2 = ilog2((unsigned int)a->used);
if( a->used > powtwos[il2] && a->used < powtwos[il2]+powtwos[il2-2]){
return mp_toom_cook_5_sqr(a,c);
}
*/
if ((e = mp_dp_to_fft_single(a, &fa, &length)) != MP_OKAY) {
if (fa != NULL) free(fa);
return e;
}
if ((e = mp_fft_sqr_d(fa, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
return e;
}
if ((e = mp_fft_to_dp(fa, c, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
return e;
}
free(fa);
return MP_OKAY;
}
int mp_fft_sqr_float(mp_int *a,mp_int *c)
{
float *fa;
fa = NULL;
int length, e;
if (mp_iszero(a) == MP_YES) {
return MP_OKAY;
}
if (a->used == 1) {
if (a->dp[0] == 1) {
if ((e = mp_set_int(c,1)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}
/* rounds the edges of the stair a bit */
/*
il2 = ilog2((unsigned int)a->used);
if( a->used > powtwos[il2] && a->used < powtwos[il2]+powtwos[il2-2]){
return mp_toom_cook_5_sqr(a,c);
}
*/
if ((e = mp_dp_to_fft_single_float(a, &fa, &length)) != MP_OKAY) {
if (fa != NULL) free(fa);
return e;
}
if ((e = mp_fft_sqr_d_float(fa, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
return e;
}
if ((e = mp_fft_to_dp_float(fa, c, length)) != MP_OKAY) {
if (fa != NULL) free(fa);
return e;
}
free(fa);
return MP_OKAY;
}
#else
#error only for 28 bit digits for now
#endif
#endif