Merge pull request #478 from MasterDuke17/make_exptmod_with_modulus_1_always_0

Give correct values for invmod with modulus of 1
This commit is contained in:
Steffen Jaeckel 2020-03-05 22:37:59 +01:00 committed by GitHub
commit fe382afd82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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