Author Topic: libtommath for Win-64  (Read 5715 times)

0 Members and 1 Guest are viewing this topic.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
libtommath for Win-64
« on: April 05, 2022, 08:37:27 pm »
made a win64 dll of LibTomMath https://www.libtom.net/
Code: QB64: [Select]
  1.  
  2. Const MP_64BIT = 1
  3. Type private_mp_word
  4.     As String * 128 mpword
  5. Const MP_DIGIT_BIT = 60
  6.  
  7. Type mp_int
  8.     used As Long
  9.     alloc As Long
  10.     sign As Long
  11.     dp As _Offset
  12.  
  13. Declare Dynamic Library "libtommath"
  14.     Function mp_init& (a As mp_int) ' as mp_err
  15.     Function mp_init_size& (a As mp_int, Byval size As Long) ' as mp_err
  16.     Function mp_init_i32& (a As mp_int, Byval b As Long) ' as mp_err
  17.     Function mp_init_l& (a As mp_int, Byval b As Long) ' ' as mp_err
  18.     Function mp_init_u32& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
  19.     Function mp_init_ul& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
  20.     Function mp_init_i64& (a As mp_int, Byval b As _Integer64) ' as mp_err
  21.     Function mp_init_ll& (a As mp_int, Byval b As _Integer64) ' as mp_err
  22.     Function mp_init_u64& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
  23.     Function mp_init_ull& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
  24.     Function mp_init_set& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
  25.     Function mp_init_set_int& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
  26.     Function mp_init_copy (a As mp_int, b As mp_int) ' as mp_err
  27.     Sub mp_clear (a As mp_int)
  28.     Sub mp_exch (a As mp_int, b As mp_int)
  29.     Function mp_shrink& (a As mp_int) ' as mp_err
  30.     Function mp_grow& (a As mp_int, Byval size As Long) ' as mp_err
  31.     Function mp_iseven& (a As mp_int) ' as long
  32.     Function mp_isodd& (a As mp_int) ' as long
  33.     Sub mp_zero (a As mp_int)
  34.     Function mp_get_double# (a As mp_int) ' as double
  35.     Function mp_set_double& (a As mp_int, Byval b As Double) ' as mp_err
  36.     Function mp_get_i32& (a As mp_int) ' as long
  37.     Function mp_get_l& (a As mp_int) ' as long
  38.     Function mp_get_int~& (a As mp_int) ' as ulong
  39.     Function mp_get_long~& (a As mp_int) ' as ulong
  40.     Function mp_get_i64&& (a As mp_int) ' as longint
  41.     Function mp_get_ll&& (a As mp_int) ' as longint
  42.     Function mp_get_long_long~&& (a As mp_int) ' as ulongint
  43.     Sub mp_set_i32 (a As mp_int, Byval b As Long)
  44.     Sub mp_set_l (a As mp_int, Byval b As Long)
  45.     Function mp_set_long& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
  46.     Sub mp_set_u32 (a As mp_int, Byval b As _Unsigned Long)
  47.     Sub mp_set_ul (a As mp_int, Byval b As _Unsigned Long)
  48.     Function mp_set_int& (a As mp_int, Byval b As _Unsigned Long) ' as mp_err
  49.     Sub mp_set_i64 (a As mp_int, Byval b As _Integer64)
  50.     Function mp_set_long_long& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_err
  51.     Sub mp_set_ll (a As mp_int, Byval b As _Integer64)
  52.     Sub mp_set_u64 (a As mp_int, Byval b As _Unsigned _Integer64)
  53.     Sub mp_set_ull (a As mp_int, Byval b As _Unsigned _Integer64)
  54.     Sub mp_set (a As mp_int, Byval b As _Unsigned _Integer64)
  55.     Function mp_get_mag_u32~& (a As mp_int) ' as ulong
  56.     Function mp_get_mag_ul~& (a As mp_int) ' as ulong
  57.     Function mp_get_mag_u64~&& (a As mp_int) ' as ulongint
  58.     Function mp_get_mag_ull~&& (a As mp_int) ' as ulongint
  59.     Function mp_copy (a As mp_int, b As mp_int) ' as mp_err
  60.     Sub mp_clamp (a As mp_int)
  61.     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
  62.     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
  63.     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
  64.     Function mp_pack_count~& (a As mp_int, Byval nails As _Unsigned Long, Byval size As _Unsigned Long) ' as uinteger
  65.     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
  66.     Sub mp_rshd (a As mp_int, Byval b As Long)
  67.     Function mp_lshd& (a As mp_int, Byval b As Long) ' as mp_err
  68.     Function mp_div_2d& (a As mp_int, Byval b As Long, c As mp_int, d As mp_int) ' as mp_err
  69.     Function mp_div_2& (a As mp_int, b As mp_int) ' as mp_err
  70.     Function mp_div_3& (a As mp_int, c As mp_int, d As _Unsigned _Integer64) ' as mp_err
  71.     Function mp_mul_2d& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
  72.     Function mp_mul_2& (a As mp_int, b As mp_int) ' as mp_err
  73.     Function mp_mod_2d& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
  74.     Function mp_2expt& (a As mp_int, Byval b As Long) ' as mp_err
  75.     Function mp_cnt_lsb& (a As mp_int) ' as long
  76.     Function mp_rand& (a As mp_int, Byval digits As Long) ' as mp_err
  77.     Function mp_rand_digit& (r As _Unsigned _Integer64) ' as mp_err
  78.     Function mp_get_bit& (a As mp_int, Byval b As Long) ' as long
  79.     Function mp_tc_xor& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  80.     Function mp_xor& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  81.     Function mp_tc_or& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  82.     Function mp_or& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  83.     Function mp_tc_and& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  84.     Function mp_and& (a As mp_int, b As mp_int, c As mp_int) 'as mp_err
  85.     Function mp_complement& (a As mp_int, b As mp_int) ' as mp_err
  86.     Function mp_tc_div_2d& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
  87.     Function mp_signed_rsh& (a As mp_int, Byval b As Long, c As mp_int) ' as mp_err
  88.     Function mp_neg (a As mp_int, b As mp_int) ' as mp_err
  89.     Function mp_abs& (a As mp_int, b As mp_int) ' as mp_err
  90.     Function mp_cmp& (a As mp_int, b As mp_int) 'as mp_ord
  91.     Function mp_cmp_mag& (a As mp_int, b As mp_int) ' as mp_ord
  92.     Function mp_add& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  93.     Function mp_sub& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  94.     Function mp_mul& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  95.     Function mp_sqr& (a As mp_int, b As mp_int) ' as mp_err
  96.     Function mp_div& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
  97.     Function mp_mod& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  98.     Function mp_incr& (a As mp_int) ' as mp_err
  99.     Function mp_decr& (a As mp_int) ' as mp_err
  100.     Function mp_cmp_d& (a As mp_int, Byval b As _Unsigned _Integer64) ' as mp_ord
  101.     Function mp_add_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
  102.     Function mp_sub_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
  103.     Function mp_mul_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
  104.     Function mp_div_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int, d As _Unsigned _Integer64) ' as mp_err
  105.     Function mp_mod_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As _Unsigned _Integer64) ' as mp_err
  106.     Function mp_addmod& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
  107.     Function mp_submod& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
  108.     Function mp_mulmod& (a As mp_int, b As mp_int, c As mp_int, d As mp_int) ' as mp_err
  109.     Function mp_sqrmod& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  110.     Function mp_invmod& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  111.     Function mp_gcd& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  112.     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
  113.     Function mp_lcm& (a As mp_int, b As mp_int, c As mp_int) ' as mp_err
  114.     Function mp_root_u32& (a As mp_int, Byval b As _Unsigned Long, c As mp_int) ' as mp_err
  115.     Function mp_n_root& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
  116.     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
  117.     Function mp_sqrt& (arg As mp_int, ret As mp_int) ' as mp_err
  118.     Function mp_sqrtmod_prime& (n As mp_int, prime As mp_int, ret As mp_int) ' as mp_err
  119.     Function mp_is_square& (arg As mp_int, ret As Long) ' as mp_err
  120.     Function mp_jacobi& (a As mp_int, n As mp_int, c As Long) ' as mp_err
  121.     Function mp_kronecker& (a As mp_int, p As mp_int, c As Long) ' as mp_err
  122.     Function mp_reduce_setup& (a As mp_int, b As mp_int) ' as mp_err
  123.     Function mp_reduce& (x As mp_int, m As mp_int, mu As mp_int) ' as mp_err
  124.     Function mp_montgomery_setup& (n As mp_int, rho As _Unsigned _Integer64) ' as mp_err
  125.     Function mp_montgomery_calc_normalization& (a As mp_int, b As mp_int) ' as mp_err
  126.     Function mp_montgomery_reduce& (x As mp_int, n As mp_int, Byval rho As _Unsigned _Integer64) ' as mp_err
  127.     Function mp_dr_is_modulus& (a As mp_int) ' as mp_bool
  128.     Sub mp_dr_setup (a As mp_int, d As _Unsigned _Integer64)
  129.     Function mp_dr_reduce& (x As mp_int, n As mp_int, Byval k As _Unsigned _Integer64) ' as mp_err
  130.     Function mp_reduce_is_2k& (a As mp_int) ' as mp_bool
  131.     Function mp_reduce_2k_setup& (a As mp_int, d As _Unsigned _Integer64) ' as mp_err
  132.     Function mp_reduce_2k& (a As mp_int, n As mp_int, Byval d As _Unsigned _Integer64) ' as mp_err
  133.     Function mp_reduce_is_2k_l& (a As mp_int) ' as mp_bool
  134.     Function mp_reduce_2k_setup_l& (a As mp_int, d As mp_int) ' as mp_err
  135.     Function mp_reduce_2k_l& (a As mp_int, n As mp_int, d As mp_int) ' as mp_err
  136.     Function mp_exptmod& (G As mp_int, X As mp_int, P As mp_int, Y As mp_int) ' as mp_err
  137.     Function mp_prime_is_divisible& (a As mp_int, result As Long) ' as mp_err
  138.     Function mp_prime_fermat& (a As mp_int, b As mp_int, result As Long) ' as mp_err
  139.     Function mp_prime_miller_rabin& (a As mp_int, b As mp_int, result As Long) ' as mp_err
  140.     Function mp_prime_rabin_miller_trials& (ByVal size As Long) ' as long
  141.     Function mp_prime_strong_lucas_selfridge& (a As mp_int, result As Long) ' as mp_err
  142.     Function mp_prime_frobenius_underwood& (N As mp_int, result As Long) ' as mp_err
  143.     Function mp_prime_is_prime& (a As mp_int, Byval t As Long, result As Long) ' as mp_err
  144.     Function mp_prime_next_prime& (a As mp_int, Byval t As Long, Byval bbs_style As Long) ' as mp_err
  145.     Function mp_prime_rand& (a As mp_int, Byval t As Long, Byval size As Long, Byval flags As Long) ' as mp_err
  146.     Function mp_log_u32& (a As mp_int, Byval base As _Unsigned Long, c As _Unsigned Long) ' as mp_err
  147.     Function mp_expt_u32& (a As mp_int, Byval b As _Unsigned Long, c As mp_int) ' as mp_err
  148.     Function mp_expt_d& (a As mp_int, Byval b As _Unsigned _Integer64, c As mp_int) ' as mp_err
  149.     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
  150.     Function mp_count_bits& (a As mp_int) ' as long
  151.     Function mp_unsigned_bin_size& (a As mp_int) ' as long
  152.     Function mp_read_unsigned_bin& (a As mp_int, Byval b As _Offset, Byval c As Long) ' as mp_err
  153.     Function mp_to_unsigned_bin& (a As mp_int, Byval b As _Offset) ' as mp_err
  154.     Function mp_to_unsigned_bin_n& (a As mp_int, Byval b As _Offset, outlen As _Unsigned Long) ' as mp_err
  155.     Function mp_signed_bin_size& (a As mp_int) ' as long
  156.     Function mp_read_signed_bin& (a As mp_int, Byval b As _Offset, Byval c As Long) ' as mp_err
  157.     Function mp_to_signed_bin& (a As mp_int, Byval b As _Offset) ' as mp_err
  158.     Function mp_to_signed_bin_n& (a As mp_int, Byval b As _Offset, outlen As _Unsigned Long) ' as mp_err
  159.     Function mp_ubin_size~&& (a As mp_int) ' as uinteger
  160.     Function mp_from_ubin& (a As mp_int, Byval buf As _Offset, Byval size As _Unsigned _Integer64) ' as mp_err
  161.     Function mp_to_ubin& (a As mp_int, Byval buf As _Offset, Byval maxlen As _Unsigned _Integer64, written As _Unsigned _Integer64) ' as mp_err
  162.     Function mp_sbin_size~&& (a As mp_int) ' as uinteger
  163.     Function mp_from_sbin& (a As mp_int, Byval buf As _Offset, Byval size As _Unsigned _Integer64) ' as mp_err
  164.     Function mp_to_sbin& (a As mp_int, Byval buf As _Offset, Byval maxlen As _Unsigned _Integer64, written As _Unsigned _Integer64) ' as mp_err
  165.     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
  166.     Function mp_radix_size& (a As mp_int, Byval radix As Long, size As Long) ' as mp_err
  167.     Function mp_read_radix& (a As mp_int, str As String, Byval radix As Long) ' as mp_err
  168.     Function mp_toradix (a As mp_int, str As String, Byval radix As Long) ' as mp_err
  169.     Function mp_toradix_n& (a As mp_int, str As String, Byval radix As Long, Byval maxlen As Long) ' as mp_err
  170.  
  171. Dim As mp_int n, m, q, r
  172. Dim As Long ok, k
  173. t = Timer(.0001)
  174. If mp_init(n) <> 0 Then Print "failed to initialize"
  175. If mp_init(m) <> 0 Then Print "failed to initiali"
  176. If mp_init(q) <> 0 Then Print "failed to initiali"
  177. If mp_init(r) <> 0 Then Print "failed to initiali"
  178. 'mp_val "2" + String$(100, "0"), n, 10
  179. 'ok = mp_n_root&(n, 2, r)
  180. 'Print mp_str(r, 10)
  181. 'ok = mp_sqrt&(n, m)
  182. 'Print mp_str(m, 10)
  183. mp_set_i32 n, 2 '4784969
  184. ok = mp_expt_u32(n, 1024, n)
  185. 'mp_set_i32 m, 10
  186. 'ok = mp_expt_u32(m, 1000000, m)
  187. 'fib_mod n, m, r
  188. 'mp_set_i32 m, 10
  189. 'ok = mp_expt_u32(m, 999960, m)
  190. 'ok = mp_div(r, m, q, r)
  191. 'Print mp_str(q, 10)
  192. mp_set_i32 m, 10
  193. ok = mp_expt_u32(m, 40, m)
  194. fib_mod n, m, r
  195. Print mp_str(r, 10)
  196. t = Timer(.0001) - t
  197. mp_clear r
  198. mp_clear q
  199. mp_clear m
  200. mp_clear n
  201.  
  202. Sub fib_mod (k As mp_int, m As mp_int, result As mp_int)
  203.     Dim As mp_int x, y, xx, temp, tmp
  204.     Dim As Long ok, cmp, i, bit_length
  205.  
  206.     If mp_init(x) <> 0 Then
  207.         Print "failed to initialize"
  208.         Exit Sub
  209.     End If
  210.     If mp_init(y) <> 0 Then
  211.         Print "failed to initialize"
  212.         Exit Sub
  213.     End If
  214.     If mp_init(xx) <> 0 Then
  215.         Print "failed to initialize"
  216.         Exit Sub
  217.     End If
  218.     If mp_init(temp) <> 0 Then
  219.         Print "failed to initialize"
  220.         Exit Sub
  221.     End If
  222.     If mp_init(tmp) <> 0 Then
  223.         Print "failed to initialize"
  224.         Exit Sub
  225.     End If
  226.  
  227.     mp_set_i32 tmp, 1
  228.     If mp_cmp(k, tmp) <= 0 Then
  229.         Print "mp_cmp(k, tmp) <= 0"
  230.         ok = mp_copy(k, result)
  231.         Exit Sub
  232.     End If
  233.  
  234.     mp_set_i32 x, 1
  235.     mp_set_i32 y, 0
  236.     ok = mp_log_u32(k, 2, bit_length)
  237.     Print "bit_length="; bit_length
  238.     For i = bit_length - 1 To 0 Step -1
  239.         ok = mp_sqr(x, tmp)
  240.         ok = mp_mod(tmp, m, xx)
  241.         ok = mp_mul(x, y, tmp)
  242.         ok = mp_add(tmp, tmp, tmp)
  243.         ok = mp_add(xx, tmp, tmp)
  244.         ok = mp_mod(tmp, m, x)
  245.         ok = mp_sqr(y, tmp)
  246.         ok = mp_add(xx, tmp, tmp)
  247.         ok = mp_mod(tmp, m, y)
  248.         If mp_get_bit(k, i) Then
  249.             ok = mp_copy(x, temp)
  250.             ok = mp_add(x, y, tmp)
  251.             ok = mp_mod(tmp, m, x)
  252.             ok = mp_copy(temp, y)
  253.         End If
  254.     Next
  255.     ok = mp_copy(x, result)
  256.     mp_clear tmp
  257.     mp_clear temp
  258.     mp_clear xx
  259.     mp_clear y
  260.     mp_clear x
  261.  
  262. Function mp_str$ (n As mp_int, radix As Long)
  263.     Dim sresult As String
  264.     Dim As Long status, size
  265.     status = mp_radix_size&(n, radix, size)
  266.     sresult = Space$(size) + Chr$(0)
  267.     status = mp_toradix_n(n, sresult, radix, size)
  268.     If status = 0 Then
  269.         mp_str$ = _Trim$(sresult)
  270.     Else
  271.         mp_str$ = "error in mp_toradix"
  272.     End If
  273.  
  274. Sub mp_val (s As String, n As mp_int, radix As Long)
  275.     Dim value As String
  276.     Dim status As Long
  277.     Dim As Long ok
  278.     value = s + Chr$(0)
  279.     status = mp_read_radix(n, value, radix)
  280.     If status <> 0 Then Print "could not read number"
  281.  
« Last Edit: April 07, 2022, 01:56:00 pm by jack »