libtommath bignum for Win-64
#1
this is header translation for 64-bit QB64 on Windows x64 of LibTomMath https://www.libtom.net/
LibTomMath is in the public domain, no strings attached

LibTomMath.bi
Code: (Select All)
$If 64BIT Then
    Const MP_28BIT = 0
    Const MP_64BIT = 1
    '    Type _unsigned _integer64
    '        As _Unsigned _Integer64 mpdigit
    '    End Type
    Type private_mp_word
        As String * 128 mpword
    End Type
    Const MP_DIGIT_BIT = 60
$Else
        Type _unsigned _integer64
        mpdigit As _Unsigned Long
        End Type
        Type private_mp_word
        mpword As _Unsigned _Integer64
        End Type
        Const _unsigned _integer64_BIT = 28
        Const MP_28BIT = 1
        Const MP_64BIT = 0
$End If

Type mp_int
    used As Long
    alloc As Long
    sign As Long
    dp As _Offset
End Type

Declare Dynamic Library "libtommath"
    Function mp_init& (a As mp_int) ' as mp_err
    Function mp_init_size& (a As mp_int, Byval size As Long) ' as mp_err
    Function mp_init_i32& (a As mp_int, Byval b As Long) ' as mp_err
    Function mp_init_l& (a As mp_int, Byval b As Long) ' ' as mp_err
    Function mp_init_u32& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
    Function mp_init_ul& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
    Function mp_init_i64& (a As mp_int, Byval b As _Integer64) ' as mp_err
    Function mp_init_ll& (a As mp_int, Byval b As _Integer64) ' as mp_err
    Function mp_init_u64& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
    Function mp_init_ull& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
    Function mp_init_set& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
    Function mp_init_set_int& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
    Function mp_init_copy (a As mp_int, b As mp_int) ' as mp_err
    Sub mp_clear (a As mp_int)
    Sub mp_exch (a As mp_int, b As mp_int)
    Function mp_shrink& (a As mp_int) ' as mp_err
    Function mp_grow& (a As mp_int, Byval size As Long) ' as mp_err
    Function mp_iseven& (a As mp_int) ' as long
    Function mp_isodd& (a As mp_int) ' as long
    Sub mp_zero (a As mp_int)
    Function mp_get_double# (a As mp_int) ' as double
    Function mp_set_double& (a As mp_int, Byval b As Double) ' as mp_err
    Function mp_get_i32& (a As mp_int) ' as long
    Function mp_get_l& (a As mp_int) ' as long
    Function mp_get_int~& (a As mp_int) ' as ulong
    Function mp_get_long~& (a As mp_int) ' as ulong
    Function mp_get_i64&& (a As mp_int) ' as longint
    Function mp_get_ll&& (a As mp_int) ' as longint
    Function mp_get_long_long~&& (a As mp_int) ' as ulongint
    Sub mp_set_i32 (a As mp_int, Byval b As Long)
    Sub mp_set_l (a As mp_int, Byval b As Long)
    Function mp_set_long& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
    Sub mp_set_u32 (a As mp_int, Byval b As _Unsigned Long)
    Sub mp_set_ul (a As mp_int, Byval b As _Unsigned Long)
    Function mp_set_int& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
    Sub mp_set_i64 (a As mp_int, Byval b As _Integer64)
    Function mp_set_long_long& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
    Sub mp_set_ll (a As mp_int, Byval b As _Integer64)
    Sub mp_set_u64 (a As mp_int, Byval b As _Unsigned _Integer64)
    Sub mp_set_ull (a As mp_int, Byval b As _Unsigned _Integer64)
    Sub mp_set (a As mp_int, Byval b As _Unsigned _Integer64)
    Function mp_get_mag_u32~& (a As mp_int) ' as ulong
    Function mp_get_mag_ul~& (a As mp_int) ' as ulong
    Function mp_get_mag_u64~&& (a As mp_int) ' as ulongint
    Function mp_get_mag_ull~&& (a As mp_int) ' as ulongint
    Function mp_copy (a As mp_int, b As mp_int) ' as mp_err
    Sub mp_clamp (a As mp_int)
    Function mp_export& (rop As _Offset, countp As _Unsigned Long, Byval order As Long, Byval size As _Unsigned Long, Byval endian As Long, Byval nails As _Unsigned Long, op As mp_int) ' as mp_err
    Function mp_import& (rop As mp_int, Byval count As _Unsigned Long, Byval order As Long, Byval size As _Unsigned Long, Byval endian As Long, Byval nails As _Unsigned Long, Byval op As _Offset) ' as mp_err
    Function mp_unpack& (rop As mp_int, Byval count As _Unsigned Long, Byval order As Long, Byval size As _Unsigned Long, Byval endian As Long, Byval nails As _Unsigned Long, op As _Offset) ' as mp_err
    Function mp_pack_count~& (a As mp_int, Byval nails As _Unsigned Long, Byval size As _Unsigned Long) ' as uinteger
    Function mp_pack& (rop As _Offset, Byval maxcount As _Unsigned Long, written As _Unsigned Long, Byval order As Long, Byval size As _Unsigned Long, Byval endian As Long, Byval nails As _Unsigned Long, op As mp_int) ' as mp_err
    Sub mp_rshd (a As mp_int, Byval b As Long)
    Function mp_lshd& (a As mp_int, Byval b As Long) ' as mp_err
    Function mp_div_2d& (a As mp_int, Byval b As Long, c As mp_int, d As mp_int) ' as mp_err
    Function mp_div_2& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_div_3& (a As mp_int, c As mp_int, d As _Unsigned _Integer64) ' as mp_err
    Function mp_mul_2d& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
    Function mp_mul_2& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_mod_2d& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
    Function mp_2expt& (a As mp_int, Byval b As Long) ' as mp_err
    Function mp_cnt_lsb& (a As mp_int) ' as long
    Function mp_rand& (a As mp_int, Byval digits As Long) ' as mp_err
    Function mp_rand_digit& (r As _Unsigned _Integer64) ' as mp_err
    Function mp_get_bit& (a As mp_int, Byval b As Long) ' as long
    Function mp_tc_xor& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_xor& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_tc_or& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_or& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_tc_and& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_and& (a As mp_int, b As mp_int, c As mp_int) 'as mp_err
    Function mp_complement& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_tc_div_2d& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
    Function mp_signed_rsh& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
    Function mp_neg (a As mp_int, b As mp_int) ' as mp_err
    Function mp_abs& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_cmp& (a As mp_int, b As mp_int) 'as mp_ord
    Function mp_cmp_mag& (a As mp_int, b As mp_int) ' as mp_ord
    Function mp_add& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_sub& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_mul& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_sqr& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_div& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
    Function mp_mod& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_incr& (a As mp_int) ' as mp_err
    Function mp_decr& (a As mp_int) ' as mp_err
    Function mp_cmp_d& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_ord
    Function mp_add_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
    Function mp_sub_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
    Function mp_mul_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
    Function mp_div_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int, d As _Unsigned _Integer64) ' as mp_err
    Function mp_mod_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As _Unsigned _Integer64) ' as mp_err
    Function mp_addmod& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
    Function mp_submod& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
    Function mp_mulmod& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
    Function mp_sqrmod& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_invmod& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_gcd& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_exteuclid& (a As mp_int, b As mp_int, U1 As mp_int, U2 As mp_int, U3 As mp_int) ' as mp_err
    Function mp_lcm& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
    Function mp_root_u32& (a As mp_int, Byval b As _Unsigned Long, c As mp_int) ' as mp_err
    Function mp_n_root& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
    Function mp_n_root_ex& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int, Byval fast As Long) ' as mp_err
    Function mp_sqrt& (arg As mp_int, ret As mp_int) ' as mp_err
    Function mp_sqrtmod_prime& (n As mp_int, prime As mp_int, ret As mp_int) ' as mp_err
    Function mp_is_square& (arg As mp_int, ret As Long) ' as mp_err
    Function mp_jacobi& (a As mp_int, n As mp_int, c As Long) ' as mp_err
    Function mp_kronecker& (a As mp_int, p As mp_int, c As Long) ' as mp_err
    Function mp_reduce_setup& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_reduce& (x As mp_int, m As mp_int, mu As mp_int) ' as mp_err
    Function mp_montgomery_setup& (n As mp_int, rho As _Unsigned _Integer64) ' as mp_err
    Function mp_montgomery_calc_normalization& (a As mp_int, b As mp_int) ' as mp_err
    Function mp_montgomery_reduce& (x As mp_int, n As mp_int, Byval rho As _Unsigned _Integer64) ' as mp_err
    Function mp_dr_is_modulus& (a As mp_int) ' as mp_bool
    Sub mp_dr_setup (a As mp_int, d As _Unsigned _Integer64)
    Function mp_dr_reduce& (x As mp_int, n As mp_int, Byval k As _Unsigned _Integer64) ' as mp_err
    Function mp_reduce_is_2k& (a As mp_int) ' as mp_bool
    Function mp_reduce_2k_setup& (a As mp_int, d As _Unsigned _Integer64) ' as mp_err
    Function mp_reduce_2k& (a As mp_int, n As mp_int, Byval d As _Unsigned _Integer64) ' as mp_err
    Function mp_reduce_is_2k_l& (a As mp_int) ' as mp_bool
    Function mp_reduce_2k_setup_l& (a As mp_int, d As mp_int) ' as mp_err
    Function mp_reduce_2k_l& (a As mp_int, n As mp_int, d As mp_int) ' as mp_err
    Function mp_exptmod& (G As mp_int, X As mp_int, P As mp_int, Y As mp_int) ' as mp_err
    Function mp_prime_is_divisible& (a As mp_int, result As Long) ' as mp_err
    Function mp_prime_fermat& (a As mp_int, b As mp_int, result As Long) ' as mp_err
    Function mp_prime_miller_rabin& (a As mp_int, b As mp_int, result As Long) ' as mp_err
    Function mp_prime_rabin_miller_trials& (ByVal size As Long) ' as long
    Function mp_prime_strong_lucas_selfridge& (a As mp_int, result As Long) ' as mp_err
    Function mp_prime_frobenius_underwood& (N As mp_int, result As Long) ' as mp_err
    Function mp_prime_is_prime& (a As mp_int, Byval t As Long, result As Long) ' as mp_err
    Function mp_prime_next_prime& (a As mp_int, Byval t As Long, Byval bbs_style As Long) ' as mp_err
    Function mp_prime_rand& (a As mp_int, Byval t As Long, Byval size As Long, Byval flags As Long) ' as mp_err
    Function mp_log_u32& (a As mp_int, Byval base As _Unsigned Long, c As _Unsigned Long) ' as mp_err
    Function mp_expt_u32& (a As mp_int, Byval b As _Unsigned Long, c As mp_int) ' as mp_err
    Function mp_expt_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
    Function mp_expt_d_ex& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int, Byval fast As Long) ' as mp_err
    Function mp_count_bits& (a As mp_int) ' as long
    Function mp_unsigned_bin_size& (a As mp_int) ' as long
    Function mp_read_unsigned_bin& (a As mp_int, Byval b As _Offset, Byval c As Long) ' as mp_err
    Function mp_to_unsigned_bin& (a As mp_int, Byval b As _Offset) ' as mp_err
    Function mp_to_unsigned_bin_n& (a As mp_int, Byval b As _Offset, outlen As _Unsigned Long) ' as mp_err
    Function mp_signed_bin_size& (a As mp_int) ' as long
    Function mp_read_signed_bin& (a As mp_int, Byval b As _Offset, Byval c As Long) ' as mp_err
    Function mp_to_signed_bin& (a As mp_int, Byval b As _Offset) ' as mp_err
    Function mp_to_signed_bin_n& (a As mp_int, Byval b As _Offset, outlen As _Unsigned Long) ' as mp_err
    Function mp_ubin_size~&& (a As mp_int) ' as uinteger
    Function mp_from_ubin& (a As mp_int, Byval buf As _Offset, Byval size As _Unsigned _Integer64) ' as mp_err
    Function mp_to_ubin& (a As mp_int, Byval buf As _Offset, Byval maxlen As _Unsigned _Integer64, written As _Unsigned _Integer64) ' as mp_err
    Function mp_sbin_size~&& (a As mp_int) ' as uinteger
    Function mp_from_sbin& (a As mp_int, Byval buf As _Offset, Byval size As _Unsigned _Integer64) ' as mp_err
    Function mp_to_sbin& (a As mp_int, Byval buf As _Offset, Byval maxlen As _Unsigned _Integer64, written As _Unsigned _Integer64) ' as mp_err
    Function mp_to_radix& (a As mp_int, str As String, Byval maxlen As _Unsigned _Integer64, written As _Unsigned _Integer64, Byval radix As Long) ' as mp_err
    Function mp_radix_size& (a As mp_int, Byval radix As Long, size As Long) ' as mp_err
    Function mp_read_radix& (a As mp_int, str As String, Byval radix As Long) ' as mp_err
    Function mp_toradix (a As mp_int, str As String, Byval radix As Long) ' as mp_err
    Function mp_toradix_n& (a As mp_int, str As String, Byval radix As Long, Byval maxlen As Long) ' as mp_err
End Declare

LibTomMath.bm
Code: (Select All)
Function mp_str$ (n As mp_int, radix As Long)
    Dim sresult As String
    Dim As Long status, size
    status = mp_radix_size&(n, radix, size)
    sresult = Space$(size) + Chr$(0)
    status = mp_toradix_n(n, sresult, radix, size)
    If status = 0 Then
        mp_str$ = _Trim$(sresult)
    Else
        mp_str$ = "error in mp_toradix"
    End If
End Function

Sub mp_val (s As String, n As mp_int, radix As Long)
    Dim value As String
    Dim status As Long
    Dim As Long ok
    value = s + Chr$(0)
    status = mp_read_radix(n, value, radix)
    If status <> 0 Then Print "could not read number"
End Sub

testTomMath.bas
Code: (Select All)
'$include: 'LibTomMath.bi'

Dim As mp_int n, m, r
Dim As Long ok
If mp_init(n) <> 0 Then Print "failed to initialize"
If mp_init(m) <> 0 Then Print "failed to initialize"
If mp_init(r) <> 0 Then Print "failed to initialize"
mp_val "2" + String$(100, "0"), n, 10
ok = mp_n_root&(n, 2, r)
Print mp_str(r, 10)
ok = mp_sqrt&(n, m)
Print mp_str(m, 10)


mp_clear r
mp_clear m
mp_clear n

'$include: 'LibTomMath.bm'


get the 64-bit dll
.zip   libtommath.zip (Size: 59.37 KB / Downloads: 62)
Reply
#2
in case you can't download the attachment you can get the dll from https://u.pcloud.link/publink/show?code=...gid0c0gecV
Reply




Users browsing this thread: 2 Guest(s)