From 9c7876f025258ab43c363b82289479baed24b097 Mon Sep 17 00:00:00 2001 From: z80 Date: Fri, 18 Oct 2024 20:52:11 -0400 Subject: [PATCH] add rust src lib and makefile --- Makefile | 40 +++++++++++++++++++++++++++++++++++ rust_src/keccaklib/Cargo.toml | 11 ++++++++++ rust_src/keccaklib/src/lib.rs | 10 +++++++++ src/keccak.rkt | 14 ++++++++++-- 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 rust_src/keccaklib/Cargo.toml create mode 100644 rust_src/keccaklib/src/lib.rs diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf77185 --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +## +# Puff - a Huff compiler in Racket +# +# @file +# @version 0.1 + +KECCAK_LIB_DIR = rust_src/keccaklib +KECCAK_LIB_TARGET_DIR = $(KECCAK_LIB_DIR)/target/release +LIB_NAME = libkeccak_lib +LIB_DIR = lib + +ifeq ($(OS),Windows_NT) + LIB_EXT = .dll +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S),Linux) + LIB_EXT = .so + endif + ifeq ($(UNAME_S),Darwin) + LIB_EXT = .dylib + endif +endif + +deps: build_rust copy_lib + +build_rust: + @echo "Building Rust library..." + cd $(KECCAK_LIB_DIR) && cargo build --release + +copy_lib: + @echo "Copying library to $(LIB_DIR) directory..." + mkdir -p $(LIB_DIR) + cp $(KECCAK_LIB_TARGET_DIR)/$(LIB_NAME)$(LIB_EXT) $(LIB_DIR)/$(LIB_NAME)$(LIB_EXT) + +clean: + @echo "Cleaning up..." + cd $(KECCAK_LIB_DIR) && cargo clean + rm -f $(LIB_DIR)/$(LIB_NAME)$(LIB_EXT) + +# end diff --git a/rust_src/keccaklib/Cargo.toml b/rust_src/keccaklib/Cargo.toml new file mode 100644 index 0000000..0e77954 --- /dev/null +++ b/rust_src/keccaklib/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "keccaklib" +version = "0.1.0" +edition = "2021" + +[lib] +name = "keccak_lib" +crate-type = ["cdylib"] + +[dependencies] +alloy-primitives = "0.8.8" diff --git a/rust_src/keccaklib/src/lib.rs b/rust_src/keccaklib/src/lib.rs new file mode 100644 index 0000000..7450993 --- /dev/null +++ b/rust_src/keccaklib/src/lib.rs @@ -0,0 +1,10 @@ +use alloy_primitives::utils::keccak256 as keccak; + +#[no_mangle] +pub extern "C" fn keccak256(data_ptr: *const u8, data_len: usize, result_ptr: *mut u8) { + let data = unsafe { std::slice::from_raw_parts(data_ptr, data_len) }; + let hash = keccak(&data); + let hash = hash.as_slice(); + let result = unsafe { std::slice::from_raw_parts_mut(result_ptr, 32) }; + result.copy_from_slice(hash); +} diff --git a/src/keccak.rkt b/src/keccak.rkt index 959ff41..720bcce 100644 --- a/src/keccak.rkt +++ b/src/keccak.rkt @@ -3,9 +3,19 @@ (require ffi/unsafe ffi/unsafe/define "assembler.rkt" threading) (require racket/runtime-path) -(define-runtime-path libkeccak "../lib/libkeccak_lib.so") +(define-runtime-path libdir "../lib") -(define-ffi-definer define-keccak (ffi-lib libkeccak)) +;; define-runtime-path to each library file depending on system +(define lib-path + (cond + [(eq? (system-type 'os) 'unix) (build-path libdir "libkeccak_lib.so")] + [(eq? (system-type 'os) 'windows) (build-path libdir "libkeccak_lib.dll")] + [(eq? (system-type 'os) 'macosx) (build-path libdir "libkeccak_lib.dylib")] + [else (error "Unsupported system type")])) + +;;(define-runtime-path libkeccak "../lib/libkeccak_lib") + +(define-ffi-definer define-keccak (ffi-lib lib-path)) (define-keccak keccak256 (_fun _pointer _size _pointer -> _void))