Skip to content

Commit

Permalink
add rust src lib and makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Oct 19, 2024
1 parent ca735fe commit 9c7876f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions rust_src/keccaklib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions rust_src/keccaklib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 12 additions & 2 deletions src/keccak.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 9c7876f

Please sign in to comment.