Skip to content

Commit 8781b24

Browse files
czurniedensjaeckel
authored andcommitted
make both mp_log() variants testable and add CI jobs
1 parent ac3df4f commit 8781b24

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

.github/workflows/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ jobs:
4343
- { BUILDOPTIONS: '--symbols', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libtool-bin' }
4444
# Run always with valgrind (no sanitizer, but debug info)
4545
- { BUILDOPTIONS: '--with-cc=gcc --with-m64 --with-valgrind', SANITIZER: '', COMPILE_DEBUG: '1', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
46+
# Alternative big-int version of mp_log(_n)
47+
- { BUILDOPTIONS: '--with-cc=gcc --with-m64 --cflags=-DS_MP_WORD_TOO_SMALL_C="" --with-valgrind', SANITIZER: '', COMPILE_DEBUG: '1', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
4648
# Shared library build
4749
- { BUILDOPTIONS: '--with-cc=gcc --make-option=-f --make-option=makefile.shared', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '1', CONV_WARNINGS: '', OTHERDEPS: 'libtool-bin' }
4850
# GCC for the 32-bit architecture (no valgrind)
4951
- { BUILDOPTIONS: '--with-cc=gcc --with-m32', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libc6-dev-i386 gcc-multilib' }
52+
# Alternative big-int version of mp_log(_n) for the 32-bit architecture (no valgrind)
53+
- { BUILDOPTIONS: '--with-cc=gcc --with-m32 --cflags=-DS_MP_WORD_TOO_SMALL_C="" ', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'libc6-dev-i386 gcc-multilib' }
5054
# clang for the 32-bit architecture (no valgrind)
5155
- { BUILDOPTIONS: '--with-cc=clang-10 --with-m32', SANITIZER: '', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang-10 llvm-10 gcc-multilib' }
5256
# RSA superclass with tests (no sanitizer, but debug info)
@@ -103,6 +107,10 @@ jobs:
103107
# but testing all three in one run took to long and timed out.
104108
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_16BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
105109
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_32BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
110+
# Alternative big-int version of mp_log(_n)
111+
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_16BIT --cflags=-DS_MP_WORD_TOO_SMALL_C="" --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
112+
- { BUILDOPTIONS: '--with-cc=gcc --cflags=-DMP_32BIT --cflags=-DS_MP_WORD_TOO_SMALL_C="" --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: '' }
113+
106114
# clang for the x86-64 architecture with restricted limb sizes
107115
- { BUILDOPTIONS: '--with-cc=clang --cflags=-DMP_16BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang llvm' }
108116
- { BUILDOPTIONS: '--with-cc=clang --cflags=-DMP_32BIT --limit-valgrind', SANITIZER: '1', COMPILE_DEBUG: '0', COMPILE_LTO: '0', CONV_WARNINGS: '', OTHERDEPS: 'clang llvm' }

demo/test.c

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,38 +1425,20 @@ static mp_err s_rs(const mp_int *a, int radix, int *size)
14251425
return MP_OKAY;
14261426
}
14271427

1428-
/* The internal functions that compute the logarithm base two with MP_PRECISION_FIXED_LOG */
1429-
static int test_s_mp_fp_log(void)
1430-
{
1431-
/* s_mp_fp_log(const mp_int *a, mp_int *c) */
1432-
1433-
1434-
/*
1435-
"a" some large constants.
1436-
No random values because it would be quite involved to check the results
1437-
1438-
Some checks are made earlier so no tests with "a" a power of two are needed.
1439-
*/
1440-
1441-
1442-
return MP_OKAY;
1443-
}
1444-
static int test_s_mp_fp_log_d(void)
1445-
{
1446-
/* s_mp_fp_log_d(const mp_int *a, mp_word *c) */
1447-
/* See test_s_mp_fp_log() for details */
1448-
return MP_OKAY;
1449-
}
1450-
14511428

1452-
/* TODO: Cleanup (not everything is still needed) and construct (find) testvalues for each correction loop */
14531429
static int test_mp_log_n(void)
14541430
{
14551431
mp_int a;
14561432
mp_digit d;
1457-
int base, lb, size;
1433+
int base, lb, size, i;
14581434
const int max_base = MP_MIN(INT_MAX, MP_DIGIT_MAX);
14591435

1436+
if (MP_HAS(S_MP_WORD_TOO_SMALL)) {
1437+
fprintf(stderr, "Testing mp_log_n with restricted size of mp_word.\n");
1438+
} else {
1439+
fprintf(stderr, "Testing mp_log_n with normal size of mp_word.\n");
1440+
}
1441+
14601442
DOR(mp_init(&a));
14611443

14621444
/*
@@ -1509,25 +1491,32 @@ static int test_mp_log_n(void)
15091491
DO(mp_rand(&a, 10));
15101492
for (base = 2; base < 65; base++) {
15111493
DO(mp_log_n(&a, base, &lb));
1512-
DO(s_rs(&a,(int)base, &size));
1494+
DO(s_rs(&a,base, &size));
15131495
/* radix_size includes the memory needed for '\0', too*/
15141496
size -= 2;
15151497
EXPECT(lb == size);
15161498
}
15171499

15181500
/*
1519-
bases 2..64 with "a" a random small constant to
1520-
test the part of mp_ilogb that uses native types.
1501+
bases 2..64 with "a" a small constant and a small exponent "n" to test
1502+
in the range a^n - 10 .. a^n + 10. That will check the correction loops
1503+
and the test for perfect power.
1504+
For simplicity a = base and n = 23 (64^23 == 2^138 > 2^128)
15211505
*/
1522-
DO(mp_rand(&a, 1));
15231506
for (base = 2; base < 65; base++) {
1524-
DO(mp_log_n(&a, base, &lb));
1525-
DO(s_rs(&a,(int)base, &size));
1526-
size -= 2;
1527-
EXPECT(lb == size);
1507+
mp_set(&a,(mp_digit)base);
1508+
DO(mp_expt_n(&a, 23, &a));
1509+
DO(mp_sub_d(&a, 10u, &a));
1510+
for (i = 0; i < 20; i++) {
1511+
DO(mp_log_n(&a, base, &lb));
1512+
DO(s_rs(&a, base, &size));
1513+
size -= 2;
1514+
EXPECT(lb == size);
1515+
DO(mp_add_d(&a, 1u, &a));
1516+
}
15281517
}
15291518

1530-
/*Test upper edgecase with base UINT32_MAX and number (UINT32_MAX/2)*UINT32_MAX^10 */
1519+
/*Test base upper edgecase with base = UINT32_MAX and number = (UINT32_MAX/2)*UINT32_MAX^10 */
15311520
mp_set(&a, max_base);
15321521
DO(mp_expt_n(&a, 10uL, &a));
15331522
DO(mp_add_d(&a, max_base / 2, &a));
@@ -1546,6 +1535,12 @@ static int test_mp_log(void)
15461535
mp_int a, base, bn, t;
15471536
int lb, lb2, i, j;
15481537

1538+
if (MP_HAS(S_MP_WORD_TOO_SMALL)) {
1539+
fprintf(stdout, "Testing mp_log with restricted size of mp_word.\n");
1540+
} else {
1541+
fprintf(stdout, "Testing mp_log with normal size of mp_word.\n");
1542+
}
1543+
15491544
DOR(mp_init_multi(&a, &base, &bn, &t, NULL));
15501545

15511546
/*
@@ -2461,8 +2456,6 @@ static int unit_tests(int argc, char **argv)
24612456
T1(mp_get_u32, MP_GET_I32),
24622457
T1(mp_get_u64, MP_GET_I64),
24632458
T1(mp_get_ul, MP_GET_L),
2464-
T1(s_mp_fp_log_d, S_MP_FP_LOG_D),
2465-
T1(s_mp_fp_log, S_MP_FP_LOG),
24662459
T1(mp_log_n, MP_LOG_N),
24672460
T1(mp_log, MP_LOG),
24682461
T1(mp_incr, MP_ADD_D),

mp_log.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static mp_err s_approx_log(const mp_int *a, const mp_int *b, int *lb)
4848
return err;
4949
}
5050

51-
5251
mp_err mp_log(const mp_int *a, const mp_int *b, int *lb)
5352
{
5453
mp_int bn;
@@ -83,11 +82,10 @@ mp_err mp_log(const mp_int *a, const mp_int *b, int *lb)
8382
return MP_OKAY;
8483
}
8584

86-
87-
if (MP_HAS(S_MP_LOG_D_POSSIBLE)) {
88-
err = s_approx_log_d(a, b, &n);
89-
} else {
85+
if (MP_HAS(S_MP_WORD_TOO_SMALL)) {
9086
err = s_approx_log(a, b, &n);
87+
} else {
88+
err = s_approx_log_d(a, b, &n);
9189
}
9290
if (err != MP_OKAY) {
9391
return err;

tommath_private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ MP_STATIC_ASSERT(prec_geq_min_prec, MP_DEFAULT_DIGIT_COUNT >= MP_MIN_DIGIT_COUNT
182182
there is a really weird architecture we try to check for it. Not a 100% reliable
183183
test but it has a safe fallback.
184184
*/
185-
#if ((UINT_MAX == UINT32_MAX) && (MP_WORD_SIZE > 4)) \
186-
|| ((UINT_MAX == UINT16_MAX) && (MP_WORD_SIZE > 2))
187-
#define S_MP_FP_LOG_D_POSSIBLE_C
185+
#if !(((UINT_MAX == UINT32_MAX) && (MP_WORD_SIZE > 4)) \
186+
|| ((UINT_MAX == UINT16_MAX) && (MP_WORD_SIZE > 2)))
187+
#define S_MP_WORD_TOO_SMALL_C
188188
#endif
189189

190190
/* random number source */

0 commit comments

Comments
 (0)