Skip to content

Commit fa8f3cf

Browse files
czurniedensjaeckel
authored andcommitted
Deny 2k-reduce if lsd is zero
1 parent 8314bde commit fa8f3cf

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

demo/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,11 +1229,20 @@ static int test_mp_cnt_lsb(void)
12291229
static int test_mp_reduce_2k(void)
12301230
{
12311231
int ix, cnt;
1232+
bool is2k;
12321233

12331234
mp_int a, b, c, d;
12341235
DOR(mp_init_multi(&a, &b, &c, &d, NULL));
12351236

12361237
/* test mp_reduce_2k */
1238+
1239+
/* Algorithm as implemented does not work if the least significant digit is zero */
1240+
DO(mp_2expt(&a, 100));
1241+
DO(mp_sub_d(&a, 1, &a));
1242+
DO(mp_sub_d(&a, MP_MASK, &a));
1243+
is2k = mp_reduce_is_2k(&a);
1244+
EXPECT(!is2k);
1245+
12371246
for (cnt = 3; cnt <= 128; ++cnt) {
12381247
mp_digit tmp;
12391248

mp_reduce_is_2k.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ bool mp_reduce_is_2k(const mp_int *a)
1111
} else if (a->used == 1) {
1212
return true;
1313
} else if (a->used > 1) {
14-
int ix, iy = mp_count_bits(a), iw = 1;
15-
mp_digit iz = 1;
14+
int ix, iy, iw = 1;
15+
mp_digit iz;
16+
/* Algorithm as implemented does not work if the least significant digit is zero */
17+
iz = a->dp[0] & MP_MASK;
18+
if (iz == 0u) {
19+
return false;
20+
}
1621

22+
iy = mp_count_bits(a);
23+
iz = 1;
1724
/* Test every bit from the second digit up, must be 1 */
1825
for (ix = MP_DIGIT_BIT; ix < iy; ix++) {
1926
if ((a->dp[iw] & iz) == 0u) {

0 commit comments

Comments
 (0)