Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include(sources.cmake)
# Options
#-----------------------------------------------------------------------------
option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"ON\", default is static" OFF)
option(WITH_MP_FPRINTF "Build and link the rather large mp_fprintf if \"ON\"" OFF)

#-----------------------------------------------------------------------------
# Compose CFLAGS
Expand Down Expand Up @@ -181,6 +182,10 @@ if(BUILD_SHARED_LIBS)
)
endif()

if(WITH_MP_FPRINTF)
# Only the big-integers get their own treatment, the rest gets send to fprintf
set(LTM_C_FLAGS -DMP_WITH_MP_FPRINTF -Wno-switch-enum -Wno-format-nonliteral -Wno-format )
endif()
# generate package version file
write_basic_package_version_file(
${PROJECT_VERSION_FILE}
Expand Down
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ image:
- Visual Studio 2015
environment:
matrix:
- CFLAGS_VAR: ""
CFLAGS_VAR_DLL: "CFLAGS=\"/Ox /MD /DLTM_TEST_DYNAMIC\""
- CFLAGS_VAR: "CFLAGS=\"/Ox /DMP_WITH_MP_FPRINTF /wd4061 /wd4774\""
CFLAGS_VAR_DLL: "CFLAGS=\"/Ox /MD /DLTM_TEST_DYNAMIC /DMP_WITH_MP_FPRINTF /wd4061 /wd4774\""
build_script:
- cmd: >-
if "Visual Studio 2022"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
Expand All @@ -27,4 +27,4 @@ build_script:
nmake -f makefile.msvc test_dll.exe %CFLAGS_VAR_DLL%
test_script:
- cmd: test.exe
- cmd: test_dll.exe
- cmd: test_dll.exe
43 changes: 43 additions & 0 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,47 @@ static int test_mp_fread_fwrite(void)
mp_clear_multi(&a, &b, NULL);
return EXIT_FAILURE;
}
#ifdef MP_WITH_MP_FPRINTF
static int test_mp_fprintf(void)
{
mp_int a, b;
FILE *tmp = NULL;
int written;
size_t read;
char buf_fprintf[1024] = {0};
const char *expected_output =
"signed -123 unsigned 456 double 3.141593 bigint-16 >0x17B8F8FB141F0A40000000<bigint-64 >00000000000000000001Uu+FiK7mf00000<"
"bigint-2 0b1100000111000100111010000101111001110010000100011111101110100011010110010110100110101010110110001"
"Float: +0.6931471806 Percent: % Log: some logfi";

DOR(mp_init_multi(&a, &b, NULL));

mp_set_ul(&a, 123456uL);
mp_set_ul(&b, 654321uL);
DO(mp_expt_n(&a, 5, &a));
DO(mp_expt_n(&b, 5, &b));
tmp = tmpfile();
written = mp_fprintf(tmp,
"signed %d unsigned %u double %f bigint-16 >%-#Zx<bigint-64 >%0*ZK<"
"bigint-2 %#ZbFloat: %+.10Lf Percent: %% Log: %.10s",
-123, 456, 3.14159265, &a, &a, 35, &b,
0.69314718055994530941723212145817656807L, "some logfile entry blabla");

rewind(tmp);

read = fread(buf_fprintf, 1, sizeof(buf_fprintf), tmp);
EXPECT(written == (int)read);
EXPECT(strcmp(buf_fprintf, expected_output) == 0);

fclose(tmp);
mp_clear_multi(&a, &b, NULL);
return EXIT_SUCCESS;
LBL_ERR:
if (tmp != NULL) fclose(tmp);
mp_clear_multi(&a, &b, NULL);
return EXIT_FAILURE;
}
#endif
static mp_err very_random_source(void *out, size_t size)
{
memset(out, 0xff, size);
Expand Down Expand Up @@ -2567,6 +2607,9 @@ static int unit_tests(int argc, char **argv)
T1(mp_dr_reduce, MP_DR_REDUCE),
T2(mp_pack_unpack,MP_PACK, MP_UNPACK),
T2(mp_fread_fwrite, MP_FREAD, MP_FWRITE),
#ifdef MP_WITH_MP_FPRINTF
T1(mp_fprintf, MP_FPRINTF),
#endif
T1(mp_get_u32, MP_GET_I32),
T1(mp_get_u64, MP_GET_I64),
T1(mp_get_ul, MP_GET_L),
Expand Down
11 changes: 11 additions & 0 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2466,6 +2466,17 @@ \subsection{To ASCII}
mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream);
\end{alltt}


If \texttt{MP\_WITH\_MP\_FPRINTF} is defined a function \texttt{mp\_fprintf} is made available
that behaves like \texttt{fprintf(3)}. The modifier for the bigint is \texttt{Z}. there is also
the specifier \texttt{K} to print bigints with radix 64.
\index{mp\_fprintf}
\begin{alltt}
int mp_fprintf(FILE *stream, const char *s, ...)
\end{alltt}
Some of the options from C23 (ISO/IEC 9899:2024) are not implemented, the compiler support
is too sparse for now.

\subsection{From ASCII}
\index{mp\_read\_radix}
\begin{alltt}
Expand Down
18 changes: 18 additions & 0 deletions doc/tommath.3
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ Computes the extended Euclidean algorithm: \fBa * U1 + b * U2 = U3\fP
Set the argument for \fBU1, U2, U3\fP to \fBNULL\fP to ignore the respective output.
.in -1i

.LP
.BI "int mp_fprintf(FILE * " stream ", const char * " format ", " ... " )"
.in 1i
Behaves like \fBfprintf(3)\fP. The modifier for a bigint is \fBZ\fP. There is also the specifier
\fBK\fP to print mp_ints in radix 64.
.br
Example:
.in +4n
.EX
mp_fprintf(stdout,"The result number %d is %Zd\\n", result_idx, &bigint);
.EE
.in -4n
.br
Returns the number of characters written or a negative value in case of an error.
.br
Some of the options from C23 (ISO/IEC 9899:2024) are not implemented.
.in -1i

.LP
.BI "mp_err mp_fread(mp_int *" a ", int " radix ", FILE *" stream ")"
.in 1i
Expand Down
16 changes: 16 additions & 0 deletions libtommath_VS2008.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@
RelativePath="mp_exteuclid.c"
>
</File>
<File
RelativePath="mp_fprintf.c"
>
</File>
<File
RelativePath="mp_fread.c"
>
Expand Down Expand Up @@ -844,6 +848,10 @@
RelativePath="s_mp_fp_log_d.c"
>
</File>
<File
RelativePath="s_mp_fprint.c"
>
</File>
<File
RelativePath="s_mp_get_bit.c"
>
Expand Down Expand Up @@ -892,6 +900,10 @@
RelativePath="s_mp_mul_toom.c"
>
</File>
<File
RelativePath="s_mp_parse_printf_token.c"
>
</File>
<File
RelativePath="s_mp_prime_is_divisible.c"
>
Expand All @@ -916,6 +928,10 @@
RelativePath="s_mp_rand_source.c"
>
</File>
<File
RelativePath="s_mp_sprint.c"
>
</File>
<File
RelativePath="s_mp_sqr.c"
>
Expand Down
45 changes: 23 additions & 22 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,29 @@ LCOV_ARGS=--directory .
OBJECTS=mp_2expt.o mp_abs.o mp_add.o mp_add_d.o mp_addmod.o mp_and.o mp_clamp.o mp_clear.o mp_clear_multi.o \
mp_cmp.o mp_cmp_d.o mp_cmp_mag.o mp_cnt_lsb.o mp_complement.o mp_copy.o mp_count_bits.o mp_cutoffs.o \
mp_div.o mp_div_2.o mp_div_2d.o mp_div_d.o mp_dr_is_modulus.o mp_dr_reduce.o mp_dr_setup.o \
mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o mp_get_mag_u32.o \
mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o mp_init_i32.o mp_init_i64.o \
mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o \
mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \
mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \
mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \
mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \
mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \
mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o \
mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
mp_set_l.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_shrink.o mp_signed_rsh.o mp_sqrmod.o mp_sqrt.o \
mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o \
mp_unpack.o mp_warray_free.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_fp_log.o \
s_mp_fp_log_d.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log_2expt.o \
s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o \
s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o \
s_mp_radix_map.o s_mp_radix_size_overestimate.o s_mp_rand_platform.o s_mp_rand_source.o s_mp_sqr.o \
s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_warray.o s_mp_warray_get.o \
s_mp_warray_put.o s_mp_zero_buf.o s_mp_zero_digs.o
mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fprintf.o mp_fread.o \
mp_from_sbin.o mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o \
mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o \
mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o \
mp_init_u64.o mp_init_ul.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log.o mp_log_n.o mp_lshd.o \
mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o \
mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o \
mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o \
mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o \
mp_radix_size.o mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o \
mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o \
mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o \
mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_shrink.o mp_signed_rsh.o \
mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \
mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_warray_free.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o \
s_mp_div_3.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o \
s_mp_fp_log.o s_mp_fp_log_d.o s_mp_fprint.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o \
s_mp_log_2expt.o s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o \
s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_parse_printf_token.o \
s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_radix_size_overestimate.o \
s_mp_rand_platform.o s_mp_rand_source.o s_mp_sprint.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o \
s_mp_sqr_toom.o s_mp_sub.o s_mp_warray.o s_mp_warray_get.o s_mp_warray_put.o s_mp_zero_buf.o \
s_mp_zero_digs.o

#END_INS

Expand Down
45 changes: 23 additions & 22 deletions makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,29 @@ LIBMAIN_D =libtommath.dll
OBJECTS=mp_2expt.o mp_abs.o mp_add.o mp_add_d.o mp_addmod.o mp_and.o mp_clamp.o mp_clear.o mp_clear_multi.o \
mp_cmp.o mp_cmp_d.o mp_cmp_mag.o mp_cnt_lsb.o mp_complement.o mp_copy.o mp_count_bits.o mp_cutoffs.o \
mp_div.o mp_div_2.o mp_div_2d.o mp_div_d.o mp_dr_is_modulus.o mp_dr_reduce.o mp_dr_setup.o \
mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fread.o mp_from_sbin.o \
mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o mp_get_mag_u32.o \
mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o mp_init_i32.o mp_init_i64.o \
mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o \
mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \
mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \
mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \
mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \
mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \
mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o \
mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o \
mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o \
mp_set_l.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_shrink.o mp_signed_rsh.o mp_sqrmod.o mp_sqrt.o \
mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o mp_to_ubin.o mp_ubin_size.o \
mp_unpack.o mp_warray_free.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o s_mp_div_3.o \
s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o s_mp_fp_log.o \
s_mp_fp_log_d.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log_2expt.o \
s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o s_mp_mul_high.o \
s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o s_mp_prime_tab.o \
s_mp_radix_map.o s_mp_radix_size_overestimate.o s_mp_rand_platform.o s_mp_rand_source.o s_mp_sqr.o \
s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_warray.o s_mp_warray_get.o \
s_mp_warray_put.o s_mp_zero_buf.o s_mp_zero_digs.o
mp_error_to_string.o mp_exch.o mp_expt_n.o mp_exptmod.o mp_exteuclid.o mp_fprintf.o mp_fread.o \
mp_from_sbin.o mp_from_ubin.o mp_fwrite.o mp_gcd.o mp_get_double.o mp_get_i32.o mp_get_i64.o mp_get_l.o \
mp_get_mag_u32.o mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_hash.o mp_init.o mp_init_copy.o \
mp_init_i32.o mp_init_i64.o mp_init_l.o mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o \
mp_init_u64.o mp_init_ul.o mp_invmod.o mp_is_square.o mp_kronecker.o mp_lcm.o mp_log.o mp_log_n.o mp_lshd.o \
mp_mod.o mp_mod_2d.o mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o \
mp_mul.o mp_mul_2.o mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o \
mp_prime_fermat.o mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o \
mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o \
mp_radix_size.o mp_radix_size_overestimate.o mp_rand.o mp_rand_source.o mp_read_radix.o mp_reduce.o \
mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o mp_reduce_2k_setup_l.o mp_reduce_is_2k.o \
mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_n.o mp_rshd.o mp_sbin_size.o mp_set.o mp_set_double.o \
mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_u32.o mp_set_u64.o mp_set_ul.o mp_shrink.o mp_signed_rsh.o \
mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o mp_submod.o mp_to_radix.o mp_to_sbin.o \
mp_to_ubin.o mp_ubin_size.o mp_unpack.o mp_warray_free.o mp_xor.o mp_zero.o s_mp_add.o s_mp_copy_digs.o \
s_mp_div_3.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_small.o s_mp_exptmod.o s_mp_exptmod_fast.o \
s_mp_fp_log.o s_mp_fp_log_d.o s_mp_fprint.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o \
s_mp_log_2expt.o s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o \
s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_parse_printf_token.o \
s_mp_prime_is_divisible.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_radix_size_overestimate.o \
s_mp_rand_platform.o s_mp_rand_source.o s_mp_sprint.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o \
s_mp_sqr_toom.o s_mp_sub.o s_mp_warray.o s_mp_warray_get.o s_mp_warray_put.o s_mp_zero_buf.o \
s_mp_zero_digs.o

HEADERS_PUB=tommath.h
HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB)
Expand Down
Loading
Loading