Created
February 16, 2021 10:12
-
-
Save dahlia/0bd3957da1e690c9bfed30556f153593 to your computer and use it in GitHub Desktop.
RandomX API example (Python port): https://github.com/tevador/RandomX/blob/v1.1.8/src/tests/api-example1.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A Python port of: | |
# https://github.com/tevador/RandomX/blob/v1.1.8/src/tests/api-example1.c | |
# | |
# You can build librandomx.{so,dylib} by the following command: | |
# cmake -DARCH=native -DBUILD_SHARED_LIBS=ON | |
from ctypes import * | |
from ctypes.util import find_library | |
librandomx = cdll.LoadLibrary(find_library('librandomx')) | |
randomx_get_flags = librandomx.randomx_get_flags | |
randomx_get_flags.argtypes = [] | |
randomx_get_flags.restype = c_int | |
randomx_alloc_cache = librandomx.randomx_alloc_cache | |
randomx_alloc_cache.argtypes = [c_int] | |
randomx_alloc_cache.restype = c_void_p | |
randomx_release_cache = librandomx.randomx_release_cache | |
randomx_release_cache.argtypes = [c_void_p] | |
randomx_release_cache.restype = None | |
randomx_init_cache = librandomx.randomx_init_cache | |
randomx_init_cache.argtypes = [c_void_p, c_void_p, c_size_t] | |
randomx_init_cache.restype = None | |
randomx_create_vm = librandomx.randomx_create_vm | |
randomx_create_vm.argtypes = [c_int, c_void_p, c_void_p] | |
randomx_create_vm.restype = c_void_p | |
randomx_destroy_vm = librandomx.randomx_destroy_vm | |
randomx_destroy_vm.argtypes = [c_void_p] | |
randomx_destroy_vm.restype = None | |
randomx_calculate_hash = librandomx.randomx_calculate_hash | |
randomx_calculate_hash.argtypes = [c_void_p, c_void_p, c_size_t, c_void_p] | |
randomx_calculate_hash.restype = None | |
my_key = create_string_buffer(b'RandomX example key') | |
my_input = create_string_buffer(b"RandomX example input") | |
hash = create_string_buffer(32) | |
flags = randomx_get_flags() | |
my_cache = randomx_alloc_cache(flags) | |
assert my_cache is not None | |
try: | |
randomx_init_cache(my_cache, addressof(my_key), sizeof(my_key)) | |
my_machine = randomx_create_vm(flags, my_cache, None) | |
assert my_machine is not None | |
try: | |
randomx_calculate_hash( | |
my_machine, | |
addressof(my_input), | |
sizeof(my_input), | |
hash | |
) | |
finally: | |
randomx_destroy_vm(my_machine) | |
finally: | |
randomx_release_cache(my_cache) | |
print(hash.raw.hex()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment