Skip to content

Toom/FFT multiplication #227

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions bn_mp_mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,42 +43,60 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
GO_ON:
#endif

/* use Toom-Cook? */
#ifdef BN_S_MP_TOOM_MUL_C
if (MP_MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
res = s_mp_toom_mul(a, b, c);
#ifdef BN_S_MP_FFT_MUL_C
if ((MP_MIN(a->used, b->used) >= FFT_MUL_LOWER_CO) && (MP_MAX(a->used, b->used) < FFT_MUL_UPPER_CO)) {
res = s_mp_fft_mul(a, b, c);
} else
#endif
#ifdef BN_S_MP_KARATSUBA_MUL_C
/* use Karatsuba? */
if (MP_MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
res = s_mp_karatsuba_mul(a, b, c);

#ifdef BN_S_MP_TOOM_COOK_5_MUL_C

if (MP_MIN(a->used, b->used) >= TOOM_COOK_5_MUL_CO) {
res = s_mp_toom_cook_5_mul(a, b, c);
} else
#endif
{
/* can we use the fast multiplier?
*
* The fast multiplier can be used if the output will
* have less than MP_WARRAY digits and the number of
* digits won't affect carry propagation
*/
int digs = a->used + b->used + 1;

#ifdef BN_S_MP_MUL_DIGS_FAST_C
if ((digs < (int)MP_WARRAY) &&
(MP_MIN(a->used, b->used) <=
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
res = s_mp_mul_digs_fast(a, b, c, digs);
#ifdef BN_S_MP_TOOM_COOK_4_MUL_C
if (MP_MIN(a->used, b->used) >= TOOM_COOK_4_MUL_CO) {
res = s_mp_toom_cook_4_mul(a, b, c);
} else
#endif
{
/* use Toom-Cook? */
#ifdef BN_S_MP_TOOM_MUL_C
if (MP_MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
res = s_mp_toom_mul(a, b, c);
} else
#endif
#ifdef BN_S_MP_KARATSUBA_MUL_C
/* use Karatsuba? */
if (MP_MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
res = s_mp_karatsuba_mul(a, b, c);
} else
#endif
{
/* can we use the fast multiplier?
*
* The fast multiplier can be used if the output will
* have less than MP_WARRAY digits and the number of
* digits won't affect carry propagation
*/
int digs = a->used + b->used + 1;

#ifdef BN_S_MP_MUL_DIGS_FAST_C
if ((digs < (int)MP_WARRAY) &&
(MP_MIN(a->used, b->used) <=
(int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT))))) {
res = s_mp_mul_digs_fast(a, b, c, digs);
} else
#endif
{
#ifdef BN_S_MP_MUL_DIGS_C
res = s_mp_mul_digs(a, b, c, a->used + b->used + 1);
res = s_mp_mul_digs(a, b, c, a->used + b->used + 1);
#else
res = MP_VAL;
res = MP_VAL;
#endif
}
}
}
}
END:
c->sign = (c->used > 0) ? neg : MP_ZPOS;
return res;
Expand Down
58 changes: 38 additions & 20 deletions bn_mp_sqr.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,53 @@ int mp_sqr(const mp_int *a, mp_int *b)
{
int res;

#ifdef BN_S_MP_TOOM_SQR_C
/* use Toom-Cook? */
if (a->used >= TOOM_SQR_CUTOFF) {
res = s_mp_toom_sqr(a, b);
/* Karatsuba? */
#ifdef BN_S_MP_FFT_SQR_C
if ((a->used >= FFT_SQR_LOWER_CO) && (a->used < FFT_SQR_UPPER_CO)) {
res = s_mp_fft_sqr(a, b);
} else
#endif
#ifdef BN_S_MP_KARATSUBA_SQR_C
if (a->used >= KARATSUBA_SQR_CUTOFF) {
res = s_mp_karatsuba_sqr(a, b);

#ifdef BN_S_MP_TOOM_COOK_5_SQR_C
if (a->used >= TOOM_COOK_5_SQR_CO) {
res = s_mp_toom_cook_5_sqr(a, b);
} else
#endif
{
#ifdef BN_S_MP_SQR_FAST_C
/* can we use the fast comba multiplier? */
if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
(a->used <
(int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT)) - 1u)))) {
res = s_mp_sqr_fast(a, b);

#ifdef BN_S_MP_TOOM_COOK_4_SQR_C
if (a->used >= TOOM_COOK_4_SQR_CO) {
res = s_mp_toom_cook_4_sqr(a, b);
} else
#endif
{

#ifdef BN_S_MP_TOOM_SQR_C
/* use Toom-Cook? */
if (a->used >= TOOM_SQR_CUTOFF) {
res = s_mp_toom_sqr(a, b);
/* Karatsuba? */
} else
#endif
#ifdef BN_S_MP_KARATSUBA_SQR_C
if (a->used >= KARATSUBA_SQR_CUTOFF) {
res = s_mp_karatsuba_sqr(a, b);
} else
#endif
{
#ifdef BN_S_MP_SQR_FAST_C
/* can we use the fast comba multiplier? */
if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
(a->used <
(int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)MP_DIGIT_BIT)) - 1u)))) {
res = s_mp_sqr_fast(a, b);
} else
#endif
{
#ifdef BN_S_MP_SQR_C
res = s_mp_sqr(a, b);
res = s_mp_sqr(a, b);
#else
res = MP_VAL;
res = MP_VAL;
#endif
}
}
}
}
b->sign = MP_ZPOS;
return res;
}
Expand Down
Loading