libtommath/mp_exptmod.c

79 lines
2.1 KiB
C
Raw Permalink Normal View History

#include "tommath_private.h"
#ifdef MP_EXPTMOD_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2003-02-28 16:09:08 +00:00
/* this is a shell function that calls either the normal or Montgomery
2003-05-17 12:33:54 +00:00
* exptmod functions. Originally the call to the montgomery code was
2003-02-28 16:09:08 +00:00
* embedded in the normal function but that wasted alot of stack space
* for nothing (since 99% of the time the Montgomery code would be called)
*/
mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
2003-02-28 16:08:34 +00:00
{
2017-08-29 20:23:48 +00:00
int dr;
2003-05-17 12:33:54 +00:00
2017-08-29 20:23:48 +00:00
/* modulus P must be positive */
if (mp_isneg(P)) {
2017-08-29 20:23:48 +00:00
return MP_VAL;
}
2003-05-17 12:33:54 +00:00
2017-08-29 20:23:48 +00:00
/* if exponent X is negative we have to recurse */
if (mp_isneg(X)) {
2017-08-29 20:23:48 +00:00
mp_int tmpG, tmpX;
mp_err err;
2003-05-17 12:33:54 +00:00
2019-04-08 21:48:39 +00:00
if (!MP_HAS(MP_INVMOD)) {
return MP_VAL;
}
2019-09-02 16:32:33 +00:00
if ((err = mp_init_multi(&tmpG, &tmpX, NULL)) != MP_OKAY) {
2017-08-29 20:23:48 +00:00
return err;
}
2019-09-02 16:32:33 +00:00
/* first compute 1/G mod P */
2017-08-29 20:23:48 +00:00
if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
2019-05-29 10:23:08 +00:00
goto LBL_ERR;
2017-08-29 20:23:48 +00:00
}
2003-05-17 12:33:54 +00:00
2017-08-29 20:23:48 +00:00
/* now get |X| */
if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
2019-05-29 10:23:08 +00:00
goto LBL_ERR;
2017-08-29 20:23:48 +00:00
}
2003-05-17 12:33:54 +00:00
2017-08-29 20:23:48 +00:00
/* and now compute (1/G)**|X| instead of G**X [X < 0] */
err = mp_exptmod(&tmpG, &tmpX, P, Y);
2019-05-29 10:23:08 +00:00
LBL_ERR:
2017-08-29 20:23:48 +00:00
mp_clear_multi(&tmpG, &tmpX, NULL);
return err;
}
2003-05-17 12:33:54 +00:00
2017-08-29 20:23:48 +00:00
/* modified diminished radix reduction */
2019-04-08 21:48:39 +00:00
if (MP_HAS(MP_REDUCE_IS_2K_L) && MP_HAS(MP_REDUCE_2K_L) && MP_HAS(S_MP_EXPTMOD) &&
mp_reduce_is_2k_l(P)) {
2017-08-29 20:23:48 +00:00
return s_mp_exptmod(G, X, P, Y, 1);
}
2005-02-12 08:40:15 +00:00
2019-04-08 21:48:39 +00:00
/* is it a DR modulus? default to no */
dr = (MP_HAS(MP_DR_IS_MODULUS) && mp_dr_is_modulus(P)) ? 1 : 0;
2003-08-05 01:24:44 +00:00
2017-08-29 20:23:48 +00:00
/* if not, is it a unrestricted DR modulus? */
2019-09-02 16:23:32 +00:00
if (MP_HAS(MP_REDUCE_IS_2K) && (dr == 0)) {
dr = (mp_reduce_is_2k(P)) ? 2 : 0;
2017-08-29 20:23:48 +00:00
}
2017-08-30 03:51:11 +00:00
2017-08-29 20:23:48 +00:00
/* if the modulus is odd or dr != 0 use the montgomery method */
2019-10-24 15:43:31 +00:00
if (MP_HAS(S_MP_EXPTMOD_FAST) && (mp_isodd(P) || (dr != 0))) {
2019-04-12 12:56:29 +00:00
return s_mp_exptmod_fast(G, X, P, Y, dr);
2019-10-29 17:41:25 +00:00
}
/* otherwise use the generic Barrett reduction technique */
if (MP_HAS(S_MP_EXPTMOD)) {
2017-08-29 20:23:48 +00:00
return s_mp_exptmod(G, X, P, Y, 0);
}
2019-10-29 17:41:25 +00:00
/* no exptmod for evens */
return MP_VAL;
2003-02-28 16:09:08 +00:00
}
2004-10-29 22:07:18 +00:00
#endif