Skip to main content

expert_ffi_math_library

🛠️ Integrating Ruby with GMP via Fiddle for Mega-scale Integers

When you need unrestricted integer sizes and blazing performance, bind Ruby to GMP (GNU MP) using Fiddle. You’ll get C‐level arithmetic speeds for huge numbers, ideal for cryptography or scientific computing.

require 'fiddle'
require 'fiddle/import'

module GMP
extend Fiddle::Importer
dlload 'libgmp.so' # ensure libgmp is installed
extern 'void mpz_init(void*)'
extern 'void mpz_set_str(void*, char*, int)'
extern 'char* mpz_get_str(char*, int, void*)'
end

# Allocate and initialize two big integers
a_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
b_ptr = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)

GMP.mpz_init(a_ptr)
GMP.mpz_init(b_ptr)
# Set from string (base 10)
GMP.mpz_set_str(a_ptr, '12345678901234567890', 10)
GMP.mpz_set_str(b_ptr, '98765432109876543210', 10)

# Use GMP functions (e.g., mpz_add) similarly
# Retrieve as string
result_str = GMP.mpz_get_str(nil, 10, a_ptr)
puts "Result: #{result_str}"