Skip to content

Commit c306afd

Browse files
author
Christoph Zurnieden
committed
fix for b == 0
1 parent 537dd2b commit c306afd

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

demo/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,15 @@ static int test_mp_root_n(void)
19311931
}
19321932
}
19331933

1934+
/* 0^(1/x) = 0 with x != 0 is allowed, test */
1935+
mp_set(&a, 0);
1936+
DO(mp_root_n(&a, 2, &c));
1937+
EXPECT(mp_cmp_d(&c, 0) == MP_EQ);
1938+
1939+
/* Not allowed: division by zero */
1940+
mp_set(&a, 2);
1941+
EXPECT(mp_root_n(&a, 0, &c) == MP_VAL);
1942+
19341943
/* root^base == input with small input and base */
19351944
mp_set(&a, 4);
19361945
DO(mp_root_n(&a, 2, &c));

mp_root_n.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ mp_err mp_root_n(const mp_int *a, int b, mp_int *c)
147147
int ilog2;
148148
mp_err err;
149149

150+
if (b == 0) {
151+
mp_set(c, 0);
152+
return MP_VAL;
153+
}
154+
155+
/* 0^(1/x) = 0 with x != 0 is allowed */
156+
if (mp_iszero(a)) {
157+
mp_set(c, 0);
158+
return MP_OKAY;
159+
}
160+
150161
if (b < 0 || (unsigned)b > (unsigned)MP_DIGIT_MAX) {
151162
return MP_VAL;
152163
}

0 commit comments

Comments
 (0)