simplify mod functions

This commit is contained in:
Daniel Mendler 2019-11-07 01:00:13 +01:00
parent cc77fadb85
commit 61ebe7c047
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
6 changed files with 13 additions and 80 deletions

View File

@ -6,20 +6,10 @@
/* d = a + b (mod c) */
mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
{
mp_err err;
mp_int t;
if ((err = mp_init(&t)) != MP_OKAY) {
mp_err err;
if ((err = mp_add(a, b, d)) != MP_OKAY) {
return err;
}
if ((err = mp_add(a, b, &t)) != MP_OKAY) {
goto LBL_ERR;
}
err = mp_mod(&t, c, d);
LBL_ERR:
mp_clear(&t);
return err;
return mp_mod(d, c, d);
}
#endif

View File

@ -6,26 +6,10 @@
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
{
mp_int t;
mp_err err;
if ((err = mp_init_size(&t, b->used)) != MP_OKAY) {
mp_err err;
if ((err = mp_div(a, b, NULL, c)) != MP_OKAY) {
return err;
}
if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) {
goto LBL_ERR;
}
if (mp_iszero(&t) || (t.sign == b->sign)) {
err = MP_OKAY;
mp_exch(&t, c);
} else {
err = mp_add(b, &t, c);
}
LBL_ERR:
mp_clear(&t);
return err;
return mp_iszero(c) || (c->sign == b->sign) ? MP_OKAY : mp_add(b, c, c);
}
#endif

View File

@ -7,19 +7,9 @@
mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
{
mp_err err;
mp_int t;
if ((err = mp_init_size(&t, c->used)) != MP_OKAY) {
if ((err = mp_mul(a, b, d)) != MP_OKAY) {
return err;
}
if ((err = mp_mul(a, b, &t)) != MP_OKAY) {
goto LBL_ERR;
}
err = mp_mod(&t, c, d);
LBL_ERR:
mp_clear(&t);
return err;
return mp_mod(d, c, d);
}
#endif

View File

@ -6,20 +6,10 @@
/* c = a * a (mod b) */
mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c)
{
mp_err err;
mp_int t;
if ((err = mp_init(&t)) != MP_OKAY) {
mp_err err;
if ((err = mp_sqr(a, c)) != MP_OKAY) {
return err;
}
if ((err = mp_sqr(a, &t)) != MP_OKAY) {
goto LBL_ERR;
}
err = mp_mod(&t, b, c);
LBL_ERR:
mp_clear(&t);
return err;
return mp_mod(c, b, c);
}
#endif

View File

@ -7,19 +7,9 @@
mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
{
mp_err err;
mp_int t;
if ((err = mp_init(&t)) != MP_OKAY) {
if ((err = mp_sub(a, b, d)) != MP_OKAY) {
return err;
}
if ((err = mp_sub(a, b, &t)) != MP_OKAY) {
goto LBL_ERR;
}
err = mp_mod(&t, c, d);
LBL_ERR:
mp_clear(&t);
return err;
return mp_mod(d, c, d);
}
#endif

View File

@ -196,8 +196,6 @@
#if defined(MP_ADDMOD_C)
# define MP_ADD_C
# define MP_CLEAR_C
# define MP_INIT_C
# define MP_MOD_C
#endif
@ -519,10 +517,7 @@
#if defined(MP_MOD_C)
# define MP_ADD_C
# define MP_CLEAR_C
# define MP_DIV_C
# define MP_EXCH_C
# define MP_INIT_SIZE_C
#endif
#if defined(MP_MOD_2D_C)
@ -580,8 +575,6 @@
#endif
#if defined(MP_MULMOD_C)
# define MP_CLEAR_C
# define MP_INIT_SIZE_C
# define MP_MOD_C
# define MP_MUL_C
#endif
@ -888,8 +881,6 @@
#endif
#if defined(MP_SQRMOD_C)
# define MP_CLEAR_C
# define MP_INIT_C
# define MP_MOD_C
# define MP_SQR_C
#endif
@ -938,8 +929,6 @@
#endif
#if defined(MP_SUBMOD_C)
# define MP_CLEAR_C
# define MP_INIT_C
# define MP_MOD_C
# define MP_SUB_C
#endif