12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include "tommath_private.h"
- #ifdef BN_MP_EXPTMOD_C
- mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
- {
- int dr;
-
- if (P->sign == MP_NEG) {
- return MP_VAL;
- }
-
- if (X->sign == MP_NEG) {
- mp_int tmpG, tmpX;
- mp_err err;
- if (!MP_HAS(MP_INVMOD)) {
- return MP_VAL;
- }
- if ((err = mp_init_multi(&tmpG, &tmpX, NULL)) != MP_OKAY) {
- return err;
- }
-
- if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
- goto LBL_ERR;
- }
-
- if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
- goto LBL_ERR;
- }
-
- err = mp_exptmod(&tmpG, &tmpX, P, Y);
- LBL_ERR:
- mp_clear_multi(&tmpG, &tmpX, NULL);
- return err;
- }
-
- 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) == MP_YES)) {
- return s_mp_exptmod(G, X, P, Y, 1);
- }
-
- dr = (MP_HAS(MP_DR_IS_MODULUS) && (mp_dr_is_modulus(P) == MP_YES)) ? 1 : 0;
-
- if (MP_HAS(MP_REDUCE_IS_2K) && (dr == 0)) {
- dr = (mp_reduce_is_2k(P) == MP_YES) ? 2 : 0;
- }
-
- if (MP_HAS(S_MP_EXPTMOD_FAST) && (MP_IS_ODD(P) || (dr != 0))) {
- return s_mp_exptmod_fast(G, X, P, Y, dr);
- } else if (MP_HAS(S_MP_EXPTMOD)) {
-
- return s_mp_exptmod(G, X, P, Y, 0);
- } else {
-
- return MP_VAL;
- }
- }
- #endif
|