Give correct values for invmod with modulus of 1

This commit is contained in:
Daniel Green 2020-02-23 09:46:12 -05:00
parent ce4e6ae114
commit ea65456607
3 changed files with 8 additions and 4 deletions

View File

@ -135,10 +135,7 @@ static int test_trivial_stuff(void)
mp_set(&b, 1u);
DO(mp_neg(&b, &b));
mp_set(&c, 1u);
/* I expected this works, but somehow the computer sez no
* DO(mp_exptmod(&a, &b, &c, &d));
*/
EXPECT(mp_exptmod(&a, &b, &c, &d) != MP_OKAY);
DO(mp_exptmod(&a, &b, &c, &d));
mp_set(&c, 7u);
/* same here */

View File

@ -6,6 +6,12 @@
/* hac 14.61, pp608 */
mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
{
/* for all n in N and n > 0, n = 0 mod 1 */
if (!mp_isneg(a) && mp_cmp_d(b, 1uL) == MP_EQ) {
mp_zero(c);
return MP_OKAY;
}
/* b cannot be negative and has to be >1 */
if (mp_isneg(b) || (mp_cmp_d(b, 1uL) != MP_GT)) {
return MP_VAL;

View File

@ -440,6 +440,7 @@
#if defined(MP_INVMOD_C)
# define MP_CMP_D_C
# define MP_ZERO_C
# define S_MP_INVMOD_C
# define S_MP_INVMOD_ODD_C
#endif