Line data Source code
1 : #include "tommath_private.h"
2 : #ifdef BN_MP_ROOT_U32_C
3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 : /* SPDX-License-Identifier: Unlicense */
5 :
6 : /* find the n'th root of an integer
7 : *
8 : * Result found such that (c)**b <= a and (c+1)**b > a
9 : *
10 : * This algorithm uses Newton's approximation
11 : * x[i+1] = x[i] - f(x[i])/f'(x[i])
12 : * which will find the root in log(N) time where
13 : * each step involves a fair bit.
14 : */
15 0 : mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c)
16 : {
17 : mp_int t1, t2, t3, a_;
18 : mp_ord cmp;
19 : int ilog2;
20 : mp_err err;
21 :
22 : /* input must be positive if b is even */
23 0 : if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
24 0 : return MP_VAL;
25 : }
26 :
27 0 : if ((err = mp_init_multi(&t1, &t2, &t3, NULL)) != MP_OKAY) {
28 0 : return err;
29 : }
30 :
31 : /* if a is negative fudge the sign but keep track */
32 0 : a_ = *a;
33 0 : a_.sign = MP_ZPOS;
34 :
35 : /* Compute seed: 2^(log_2(n)/b + 2)*/
36 0 : ilog2 = mp_count_bits(a);
37 :
38 : /*
39 : If "b" is larger than INT_MAX it is also larger than
40 : log_2(n) because the bit-length of the "n" is measured
41 : with an int and hence the root is always < 2 (two).
42 : */
43 0 : if (b > (uint32_t)(INT_MAX/2)) {
44 0 : mp_set(c, 1uL);
45 0 : c->sign = a->sign;
46 0 : err = MP_OKAY;
47 0 : goto LBL_ERR;
48 : }
49 :
50 : /* "b" is smaller than INT_MAX, we can cast safely */
51 0 : if (ilog2 < (int)b) {
52 0 : mp_set(c, 1uL);
53 0 : c->sign = a->sign;
54 0 : err = MP_OKAY;
55 0 : goto LBL_ERR;
56 : }
57 0 : ilog2 = ilog2 / ((int)b);
58 0 : if (ilog2 == 0) {
59 0 : mp_set(c, 1uL);
60 0 : c->sign = a->sign;
61 0 : err = MP_OKAY;
62 0 : goto LBL_ERR;
63 : }
64 : /* Start value must be larger than root */
65 0 : ilog2 += 2;
66 0 : if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY) goto LBL_ERR;
67 : do {
68 : /* t1 = t2 */
69 0 : if ((err = mp_copy(&t2, &t1)) != MP_OKAY) goto LBL_ERR;
70 :
71 : /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
72 :
73 : /* t3 = t1**(b-1) */
74 0 : if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) goto LBL_ERR;
75 :
76 : /* numerator */
77 : /* t2 = t1**b */
78 0 : if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) goto LBL_ERR;
79 :
80 : /* t2 = t1**b - a */
81 0 : if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) goto LBL_ERR;
82 :
83 : /* denominator */
84 : /* t3 = t1**(b-1) * b */
85 0 : if ((err = mp_mul_d(&t3, b, &t3)) != MP_OKAY) goto LBL_ERR;
86 :
87 : /* t3 = (t1**b - a)/(b * t1**(b-1)) */
88 0 : if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) goto LBL_ERR;
89 :
90 0 : if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) goto LBL_ERR;
91 :
92 : /*
93 : Number of rounds is at most log_2(root). If it is more it
94 : got stuck, so break out of the loop and do the rest manually.
95 : */
96 0 : if (ilog2-- == 0) {
97 0 : break;
98 : }
99 0 : } while (mp_cmp(&t1, &t2) != MP_EQ);
100 :
101 : /* result can be off by a few so check */
102 : /* Loop beneath can overshoot by one if found root is smaller than actual root */
103 : for (;;) {
104 0 : if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
105 0 : cmp = mp_cmp(&t2, &a_);
106 0 : if (cmp == MP_EQ) {
107 0 : err = MP_OKAY;
108 0 : goto LBL_ERR;
109 : }
110 0 : if (cmp == MP_LT) {
111 0 : if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
112 : } else {
113 0 : break;
114 : }
115 : }
116 : /* correct overshoot from above or from recurrence */
117 : for (;;) {
118 0 : if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR;
119 0 : if (mp_cmp(&t2, &a_) == MP_GT) {
120 0 : if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR;
121 : } else {
122 0 : break;
123 : }
124 : }
125 :
126 : /* set the result */
127 0 : mp_exch(&t1, c);
128 :
129 : /* set the sign of the result */
130 0 : c->sign = a->sign;
131 :
132 0 : err = MP_OKAY;
133 :
134 0 : LBL_ERR:
135 0 : mp_clear_multi(&t1, &t2, &t3, NULL);
136 0 : return err;
137 : }
138 :
139 : #endif
|