Skip to content

Commit c4a2ee1

Browse files
author
Christoph Zurnieden
committed
Add 1 (one) unconditionally to the start value
1 parent 9d04b28 commit c4a2ee1

1 file changed

Lines changed: 2 additions & 20 deletions

File tree

mp_root_n.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,21 @@ static mp_err s_mp_fp_exp2(const mp_int *a, mp_int *c)
108108
if ((err = mp_init_multi(&tmp, &int_part, &fract_part, NULL)) != MP_OKAY) {
109109
return err;
110110
}
111-
112111
/* Cut fore and aft to get the integer part */
113112
if ((err = mp_div_2d(a, MP_PRECISION_FIXED_LOG, &int_part, NULL)) != MP_OKAY) goto LBL_ERR;
114113
if ((err = mp_mul_2d(&int_part, MP_PRECISION_FIXED_LOG, &int_part)) != MP_OKAY) goto LBL_ERR;
115-
116114
/* Fractional part */
117115
if ((err = mp_sub(a, &int_part, &fract_part)) != MP_OKAY) goto LBL_ERR;
118-
119116
/* 2^{fractional part} */
120117
if ((err = s_mp_fp_exp2_fract(&fract_part, &fract_part)) != MP_OKAY) goto LBL_ERR;
121-
122118
/* A = 1 << normalized integer part */
123119
mp_set(&tmp,1);
124120
if ((err = mp_div_2d(&int_part, MP_PRECISION_FIXED_LOG, &int_part, NULL)) != MP_OKAY) goto LBL_ERR;
125121
if ((err = mp_mul_2d(&tmp, (int)mp_get_l(&int_part), &int_part)) != MP_OKAY) goto LBL_ERR;
126-
127122
/* B = A * fractional part */
128123
if ((err = mp_mul(&int_part, &fract_part, &tmp)) != MP_OKAY) goto LBL_ERR;
129-
130124
/* normalize B */
131125
if ((err = mp_div_2d(&tmp, MP_PRECISION_FIXED_LOG, &tmp, NULL)) != MP_OKAY) goto LBL_ERR;
132-
133126
/* C = 2^a (Roughly. Very, very roughly) */
134127
if (c != NULL) {
135128
mp_exch(&tmp, c);
@@ -219,38 +212,27 @@ mp_err mp_root_n(const mp_int *a, int b, mp_int *c)
219212
if ((err = mp_mul_2d(&t2,MP_PRECISION_FIXED_LOG, &t2)) != MP_OKAY) goto LBL_ERR;
220213
if ((err = mp_div(&t2, &t1, &t2,NULL)) != MP_OKAY) goto LBL_ERR;
221214

222-
223215
/* 2^k, the start-value */
224216
if ((err = s_mp_fp_exp2(&t2, &t2)) != MP_OKAY) goto LBL_ERR;
225-
if (mp_isone(&t2)) {
226-
if ((err = mp_incr(&t2)) != MP_OKAY) goto LBL_ERR;
227-
}
217+
/* Add 1 (one) unconditionally for the small roots. */
218+
if ((err = mp_incr(&t2)) != MP_OKAY) goto LBL_ERR;
228219
do {
229-
230220
/* t1 = t2 */
231221
if ((err = mp_copy(&t2, &t1)) != MP_OKAY) goto LBL_ERR;
232-
233222
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
234-
235223
/* t3 = t1**(b-1) */
236224
if ((err = mp_expt_n(&t1, b - 1, &t3)) != MP_OKAY) goto LBL_ERR;
237-
238225
/* numerator */
239226
/* t2 = t1**b */
240227
if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) goto LBL_ERR;
241-
242228
/* t2 = t1**b - a */
243229
if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) goto LBL_ERR;
244-
245230
/* denominator */
246231
/* t3 = t1**(b-1) * b */
247232
if ((err = mp_mul_d(&t3, (mp_digit)b, &t3)) != MP_OKAY) goto LBL_ERR;
248-
249233
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
250234
if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) goto LBL_ERR;
251-
252235
if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) goto LBL_ERR;
253-
254236
/*
255237
Number of rounds is at most log_2(root). If it is more it
256238
got stuck, so break out of the loop and do the rest manually.

0 commit comments

Comments
 (0)