refactor with mp_init_multi

This commit is contained in:
Francois Perrad 2019-05-20 05:36:08 +02:00
parent d185c1d7bd
commit c1f5b06d4b
3 changed files with 33 additions and 59 deletions

View File

@ -24,18 +24,10 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
return MP_VAL;
}
if ((res = mp_init(&t1)) != MP_OKAY) {
if ((res = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
return res;
}
if ((res = mp_init(&t2)) != MP_OKAY) {
goto LBL_T1;
}
if ((res = mp_init(&t3)) != MP_OKAY) {
goto LBL_T2;
}
/* if a is negative fudge the sign but keep track */
a_ = *a;
a_.sign = MP_ZPOS;
@ -60,7 +52,7 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
mp_set(c, 1uL);
c->sign = a->sign;
res = MP_OKAY;
goto LBL_T3;
goto LBL_ERR;
}
}
#endif
@ -69,56 +61,56 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
mp_set(c, 1uL);
c->sign = a->sign;
res = MP_OKAY;
goto LBL_T3;
goto LBL_ERR;
}
ilog2 = ilog2 / ((int)b);
if (ilog2 == 0) {
mp_set(c, 1uL);
c->sign = a->sign;
res = MP_OKAY;
goto LBL_T3;
goto LBL_ERR;
}
/* Start value must be larger than root */
ilog2 += 2;
if ((res = mp_2expt(&t2,ilog2)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
do {
/* t1 = t2 */
if ((res = mp_copy(&t2, &t1)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
/* t3 = t1**(b-1) */
if ((res = mp_expt_d_ex(&t1, b - 1u, &t3, fast)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
/* numerator */
/* t2 = t1**b */
if ((res = mp_mul(&t3, &t1, &t2)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
/* t2 = t1**b - a */
if ((res = mp_sub(&t2, &a_, &t2)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
/* denominator */
/* t3 = t1**(b-1) * b */
if ((res = mp_mul_d(&t3, b, &t3)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
if ((res = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
if ((res = mp_sub(&t1, &t3, &t2)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
/*
Number of rounds is at most log_2(root). If it is more it
@ -133,16 +125,16 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
/* Loop beneath can overshoot by one if found root is smaller than actual root */
for (;;) {
if ((res = mp_expt_d_ex(&t1, b, &t2, fast)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
cmp = mp_cmp(&t2, &a_);
if (cmp == MP_EQ) {
res = MP_OKAY;
goto LBL_T3;
goto LBL_ERR;
}
if (cmp == MP_LT) {
if ((res = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
} else {
break;
@ -151,11 +143,11 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
/* correct overshoot from above or from recurrence */
for (;;) {
if ((res = mp_expt_d_ex(&t1, b, &t2, fast)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
if (mp_cmp(&t2, &a_) == MP_GT) {
if ((res = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) {
goto LBL_T3;
goto LBL_ERR;
}
} else {
break;
@ -170,12 +162,8 @@ mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
res = MP_OKAY;
LBL_T3:
mp_clear(&t3);
LBL_T2:
mp_clear(&t2);
LBL_T1:
mp_clear(&t1);
LBL_ERR:
mp_clear_multi(&t1, &t2, &t3, NULL);
return res;
}
#endif

View File

@ -3881,7 +3881,9 @@ BN_MP_NEG_C
BN_MP_N_ROOT_C
+--->BN_MP_N_ROOT_EX_C
| +--->BN_MP_INIT_C
| +--->BN_MP_INIT_MULTI_C
| | +--->BN_MP_INIT_C
| | +--->BN_MP_CLEAR_C
| +--->BN_MP_COUNT_BITS_C
| +--->BN_MP_SET_C
| | +--->BN_MP_ZERO_C
@ -3897,8 +3899,6 @@ BN_MP_N_ROOT_C
| | +--->BN_MP_MUL_C
| | | +--->BN_S_MP_BALANCE_MUL_C
| | | | +--->BN_MP_INIT_SIZE_C
| | | | +--->BN_MP_INIT_MULTI_C
| | | | | +--->BN_MP_CLEAR_C
| | | | +--->BN_MP_CLEAR_C
| | | | +--->BN_MP_LSHD_C
| | | | | +--->BN_MP_GROW_C
@ -3913,8 +3913,6 @@ BN_MP_N_ROOT_C
| | | | +--->BN_MP_EXCH_C
| | | | +--->BN_MP_CLEAR_MULTI_C
| | | +--->BN_S_MP_TOOM_MUL_C
| | | | +--->BN_MP_INIT_MULTI_C
| | | | | +--->BN_MP_CLEAR_C
| | | | +--->BN_MP_MOD_2D_C
| | | | | +--->BN_MP_ZERO_C
| | | | | +--->BN_MP_CLAMP_C
@ -3982,7 +3980,6 @@ BN_MP_N_ROOT_C
| | +--->BN_MP_CLEAR_C
| | +--->BN_MP_SQR_C
| | | +--->BN_S_MP_TOOM_SQR_C
| | | | +--->BN_MP_INIT_MULTI_C
| | | | +--->BN_MP_MOD_2D_C
| | | | | +--->BN_MP_ZERO_C
| | | | | +--->BN_MP_CLAMP_C
@ -4044,8 +4041,6 @@ BN_MP_N_ROOT_C
| +--->BN_MP_MUL_C
| | +--->BN_S_MP_BALANCE_MUL_C
| | | +--->BN_MP_INIT_SIZE_C
| | | +--->BN_MP_INIT_MULTI_C
| | | | +--->BN_MP_CLEAR_C
| | | +--->BN_MP_CLEAR_C
| | | +--->BN_MP_LSHD_C
| | | | +--->BN_MP_GROW_C
@ -4060,8 +4055,6 @@ BN_MP_N_ROOT_C
| | | +--->BN_MP_EXCH_C
| | | +--->BN_MP_CLEAR_MULTI_C
| | +--->BN_S_MP_TOOM_MUL_C
| | | +--->BN_MP_INIT_MULTI_C
| | | | +--->BN_MP_CLEAR_C
| | | +--->BN_MP_MOD_2D_C
| | | | +--->BN_MP_ZERO_C
| | | | +--->BN_MP_CLAMP_C
@ -4140,8 +4133,6 @@ BN_MP_N_ROOT_C
| +--->BN_MP_DIV_C
| | +--->BN_MP_CMP_MAG_C
| | +--->BN_MP_ZERO_C
| | +--->BN_MP_INIT_MULTI_C
| | | +--->BN_MP_CLEAR_C
| | +--->BN_MP_ABS_C
| | +--->BN_MP_MUL_2D_C
| | | +--->BN_MP_GROW_C
@ -4164,6 +4155,7 @@ BN_MP_N_ROOT_C
| | +--->BN_MP_CLEAR_MULTI_C
| | | +--->BN_MP_CLEAR_C
| | +--->BN_MP_INIT_SIZE_C
| | +--->BN_MP_INIT_C
| | +--->BN_MP_INIT_COPY_C
| | | +--->BN_MP_CLEAR_C
| | +--->BN_MP_LSHD_C
@ -4182,11 +4174,14 @@ BN_MP_N_ROOT_C
| | +--->BN_MP_GROW_C
| | +--->BN_MP_CLAMP_C
| +--->BN_MP_EXCH_C
| +--->BN_MP_CLEAR_C
| +--->BN_MP_CLEAR_MULTI_C
| | +--->BN_MP_CLEAR_C
BN_MP_N_ROOT_EX_C
+--->BN_MP_INIT_C
+--->BN_MP_INIT_MULTI_C
| +--->BN_MP_INIT_C
| +--->BN_MP_CLEAR_C
+--->BN_MP_COUNT_BITS_C
+--->BN_MP_SET_C
| +--->BN_MP_ZERO_C
@ -4202,8 +4197,6 @@ BN_MP_N_ROOT_EX_C
| +--->BN_MP_MUL_C
| | +--->BN_S_MP_BALANCE_MUL_C
| | | +--->BN_MP_INIT_SIZE_C
| | | +--->BN_MP_INIT_MULTI_C
| | | | +--->BN_MP_CLEAR_C
| | | +--->BN_MP_CLEAR_C
| | | +--->BN_MP_LSHD_C
| | | | +--->BN_MP_GROW_C
@ -4218,8 +4211,6 @@ BN_MP_N_ROOT_EX_C
| | | +--->BN_MP_EXCH_C
| | | +--->BN_MP_CLEAR_MULTI_C
| | +--->BN_S_MP_TOOM_MUL_C
| | | +--->BN_MP_INIT_MULTI_C
| | | | +--->BN_MP_CLEAR_C
| | | +--->BN_MP_MOD_2D_C
| | | | +--->BN_MP_ZERO_C
| | | | +--->BN_MP_CLAMP_C
@ -4287,7 +4278,6 @@ BN_MP_N_ROOT_EX_C
| +--->BN_MP_CLEAR_C
| +--->BN_MP_SQR_C
| | +--->BN_S_MP_TOOM_SQR_C
| | | +--->BN_MP_INIT_MULTI_C
| | | +--->BN_MP_MOD_2D_C
| | | | +--->BN_MP_ZERO_C
| | | | +--->BN_MP_CLAMP_C
@ -4349,8 +4339,6 @@ BN_MP_N_ROOT_EX_C
+--->BN_MP_MUL_C
| +--->BN_S_MP_BALANCE_MUL_C
| | +--->BN_MP_INIT_SIZE_C
| | +--->BN_MP_INIT_MULTI_C
| | | +--->BN_MP_CLEAR_C
| | +--->BN_MP_CLEAR_C
| | +--->BN_MP_LSHD_C
| | | +--->BN_MP_GROW_C
@ -4365,8 +4353,6 @@ BN_MP_N_ROOT_EX_C
| | +--->BN_MP_EXCH_C
| | +--->BN_MP_CLEAR_MULTI_C
| +--->BN_S_MP_TOOM_MUL_C
| | +--->BN_MP_INIT_MULTI_C
| | | +--->BN_MP_CLEAR_C
| | +--->BN_MP_MOD_2D_C
| | | +--->BN_MP_ZERO_C
| | | +--->BN_MP_CLAMP_C
@ -4445,8 +4431,6 @@ BN_MP_N_ROOT_EX_C
+--->BN_MP_DIV_C
| +--->BN_MP_CMP_MAG_C
| +--->BN_MP_ZERO_C
| +--->BN_MP_INIT_MULTI_C
| | +--->BN_MP_CLEAR_C
| +--->BN_MP_ABS_C
| +--->BN_MP_MUL_2D_C
| | +--->BN_MP_GROW_C
@ -4469,6 +4453,7 @@ BN_MP_N_ROOT_EX_C
| +--->BN_MP_CLEAR_MULTI_C
| | +--->BN_MP_CLEAR_C
| +--->BN_MP_INIT_SIZE_C
| +--->BN_MP_INIT_C
| +--->BN_MP_INIT_COPY_C
| | +--->BN_MP_CLEAR_C
| +--->BN_MP_LSHD_C
@ -4487,7 +4472,8 @@ BN_MP_N_ROOT_EX_C
| +--->BN_MP_GROW_C
| +--->BN_MP_CLAMP_C
+--->BN_MP_EXCH_C
+--->BN_MP_CLEAR_C
+--->BN_MP_CLEAR_MULTI_C
| +--->BN_MP_CLEAR_C
BN_MP_OR_C

View File

@ -610,7 +610,7 @@
#endif
#if defined(BN_MP_N_ROOT_EX_C)
# define BN_MP_INIT_C
# define BN_MP_INIT_MULTI_C
# define BN_MP_COUNT_BITS_C
# define BN_MP_SET_C
# define BN_MP_2EXPT_C
@ -624,7 +624,7 @@
# define BN_MP_ADD_D_C
# define BN_MP_SUB_D_C
# define BN_MP_EXCH_C
# define BN_MP_CLEAR_C
# define BN_MP_CLEAR_MULTI_C
#endif
#if defined(BN_MP_NEG_C)