diff --git a/.gitignore b/.gitignore index 98f232e..fb2adb5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.o *.so *.class +/target/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..d40c3a8 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +#JNI Bindings for Blake3 +WIP + +C Bindings for the API of [Blake 3](https://github.com/BLAKE3-team/BLAKE3) cryptographic hash function + +Generating the JNI header +``` +javac *NativeBlake3.java -h src/main/resources/include/ +``` + +The library is cross compiled from linux + +for more platforms go here [here](https://github.com/BLAKE3-team/BLAKE3/tree/master/c) + +####Linux + +``` +cd src/main/c +gcc -fPIC -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux \ +-shared -o ../../../natives/linux_64/libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \ +blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S \ +blake3_avx512_x86-64_unix.S org_scash_NativeBLAKE3.c +``` + +####Windows 64 + +``` +cd src/main/c +x86_64-w64-mingw32-gcc -I/usr/lib/jvm/default-java/include -shared -o ../../../natives/windows_64/blake3.dll \ +blake3.c blake3_dispatch.c blake3_portable.c blake3_sse41_x86-64_windows_gnu.S blake3_avx512_x86-64_windows_gnu.S \ +blake3_avx2_x86-64_windows_gnu.S org_scash_NativeBLAKE3.c +``` + +This would result in the native library binaries to be inside the `/natives/` folder. The project will automatically detect the OS +and load the correct binary diff --git a/natives/windows_64/blake3.dll b/natives/windows_64/blake3.dll new file mode 100644 index 0000000..700d39c Binary files /dev/null and b/natives/windows_64/blake3.dll differ diff --git a/pom.xml b/pom.xml index 016e4ce..6ab25b0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,10 +3,20 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.scash BLAKE3jni 1.0-SNAPSHOT - - + + + org.scijava + native-lib-loader + 2.3.4 + + + junit + junit + 4.13 + test + + \ No newline at end of file diff --git a/src/main/resources/include/blake3.c b/src/main/c/blake3.c similarity index 100% rename from src/main/resources/include/blake3.c rename to src/main/c/blake3.c diff --git a/src/main/resources/include/blake3.h b/src/main/c/blake3.h similarity index 100% rename from src/main/resources/include/blake3.h rename to src/main/c/blake3.h diff --git a/src/main/resources/include/blake3_avx2.c b/src/main/c/blake3_avx2.c similarity index 100% rename from src/main/resources/include/blake3_avx2.c rename to src/main/c/blake3_avx2.c diff --git a/src/main/resources/include/blake3_avx2_x86-64_unix.S b/src/main/c/blake3_avx2_x86-64_unix.S similarity index 100% rename from src/main/resources/include/blake3_avx2_x86-64_unix.S rename to src/main/c/blake3_avx2_x86-64_unix.S diff --git a/src/main/resources/include/blake3_avx2_x86-64_windows_gnu.S b/src/main/c/blake3_avx2_x86-64_windows_gnu.S similarity index 100% rename from src/main/resources/include/blake3_avx2_x86-64_windows_gnu.S rename to src/main/c/blake3_avx2_x86-64_windows_gnu.S diff --git a/src/main/resources/include/blake3_avx2_x86-64_windows_msvc.asm b/src/main/c/blake3_avx2_x86-64_windows_msvc.asm similarity index 100% rename from src/main/resources/include/blake3_avx2_x86-64_windows_msvc.asm rename to src/main/c/blake3_avx2_x86-64_windows_msvc.asm diff --git a/src/main/resources/include/blake3_avx512.c b/src/main/c/blake3_avx512.c similarity index 100% rename from src/main/resources/include/blake3_avx512.c rename to src/main/c/blake3_avx512.c diff --git a/src/main/resources/include/blake3_avx512_x86-64_unix.S b/src/main/c/blake3_avx512_x86-64_unix.S similarity index 100% rename from src/main/resources/include/blake3_avx512_x86-64_unix.S rename to src/main/c/blake3_avx512_x86-64_unix.S diff --git a/src/main/resources/include/blake3_avx512_x86-64_windows_gnu.S b/src/main/c/blake3_avx512_x86-64_windows_gnu.S similarity index 100% rename from src/main/resources/include/blake3_avx512_x86-64_windows_gnu.S rename to src/main/c/blake3_avx512_x86-64_windows_gnu.S diff --git a/src/main/resources/include/blake3_avx512_x86-64_windows_msvc.asm b/src/main/c/blake3_avx512_x86-64_windows_msvc.asm similarity index 100% rename from src/main/resources/include/blake3_avx512_x86-64_windows_msvc.asm rename to src/main/c/blake3_avx512_x86-64_windows_msvc.asm diff --git a/src/main/resources/include/blake3_dispatch.c b/src/main/c/blake3_dispatch.c similarity index 100% rename from src/main/resources/include/blake3_dispatch.c rename to src/main/c/blake3_dispatch.c diff --git a/src/main/resources/include/blake3_impl.h b/src/main/c/blake3_impl.h similarity index 100% rename from src/main/resources/include/blake3_impl.h rename to src/main/c/blake3_impl.h diff --git a/src/main/resources/include/blake3_neon.c b/src/main/c/blake3_neon.c similarity index 100% rename from src/main/resources/include/blake3_neon.c rename to src/main/c/blake3_neon.c diff --git a/src/main/resources/include/blake3_portable.c b/src/main/c/blake3_portable.c similarity index 100% rename from src/main/resources/include/blake3_portable.c rename to src/main/c/blake3_portable.c diff --git a/src/main/resources/include/blake3_sse41.c b/src/main/c/blake3_sse41.c similarity index 100% rename from src/main/resources/include/blake3_sse41.c rename to src/main/c/blake3_sse41.c diff --git a/src/main/resources/include/blake3_sse41_x86-64_unix.S b/src/main/c/blake3_sse41_x86-64_unix.S similarity index 100% rename from src/main/resources/include/blake3_sse41_x86-64_unix.S rename to src/main/c/blake3_sse41_x86-64_unix.S diff --git a/src/main/resources/include/blake3_sse41_x86-64_windows_gnu.S b/src/main/c/blake3_sse41_x86-64_windows_gnu.S similarity index 100% rename from src/main/resources/include/blake3_sse41_x86-64_windows_gnu.S rename to src/main/c/blake3_sse41_x86-64_windows_gnu.S diff --git a/src/main/resources/include/blake3_sse41_x86-64_windows_msvc.asm b/src/main/c/blake3_sse41_x86-64_windows_msvc.asm similarity index 100% rename from src/main/resources/include/blake3_sse41_x86-64_windows_msvc.asm rename to src/main/c/blake3_sse41_x86-64_windows_msvc.asm diff --git a/src/main/c/org_scash_NativeBLAKE3.c b/src/main/c/org_scash_NativeBLAKE3.c new file mode 100644 index 0000000..68f3c5f --- /dev/null +++ b/src/main/c/org_scash_NativeBLAKE3.c @@ -0,0 +1,25 @@ +#include +#include +#include "blake3.h" +#include "org_scash_NativeBLAKE3.h" + +JNIEXPORT jlong JNICALL Java_org_scash_NativeBLAKE3_createHasher + (JNIEnv *env, jclass classObject) + { + blake3_hasher *hasher = malloc (sizeof (blake3_hasher)); + + (void)classObject;(void)env; + + return (uintptr_t)hasher; + } + +JNIEXPORT void JNICALL Java_org_scash_NativeBLAKE3_destroyHasher + (JNIEnv *env, jclass classObject, jlong lp) + { + blake3_hasher *hasher = (blake3_hasher*)(uintptr_t)lp; + if (hasher != NULL) { + free(hasher); + } + (void)classObject;(void)env; + return; + } diff --git a/src/main/c/org_scash_NativeBLAKE3.h b/src/main/c/org_scash_NativeBLAKE3.h new file mode 100644 index 0000000..9998bcd --- /dev/null +++ b/src/main/c/org_scash_NativeBLAKE3.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_scash_NativeBLAKE3 */ + +#ifndef _Included_org_scash_NativeBLAKE3 +#define _Included_org_scash_NativeBLAKE3 +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_scash_NativeBLAKE3 + * Method: createHasher + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_org_scash_NativeBLAKE3_createHasher + (JNIEnv *, jclass); + +/* + * Class: org_scash_NativeBLAKE3 + * Method: destroyHasher + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_org_scash_NativeBLAKE3_destroyHasher + (JNIEnv *, jclass, jlong); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/main/java/org/scash/NativeBLAKE3.java b/src/main/java/org/scash/NativeBLAKE3.java new file mode 100644 index 0000000..d1e7d3a --- /dev/null +++ b/src/main/java/org/scash/NativeBLAKE3.java @@ -0,0 +1,26 @@ +package org.scash; +import org.scijava.nativelib.NativeLoader; + +public class NativeBLAKE3 { + private static final boolean enabled; + + static { + boolean isEnabled = false; + try { + NativeLoader.loadLibrary("blake3"); + isEnabled = true; + } catch (java.io.IOException e) { + System.out.println("UnsatisfiedLinkError: " + e.toString()); + } finally { + enabled = isEnabled; + } + } + + public static boolean isEnabled() { + return enabled; + } + + private static native long createHasher(); + + private static native void destroyHasher(long hasher); +} diff --git a/src/main/java/org/scash/NativeBLAKE3Util.java b/src/main/java/org/scash/NativeBLAKE3Util.java new file mode 100644 index 0000000..8dcb4ca --- /dev/null +++ b/src/main/java/org/scash/NativeBLAKE3Util.java @@ -0,0 +1,17 @@ +package org.scash; + +public class NativeBLAKE3Util { + + public static void assertEquals(boolean val, boolean val2, String message) throws AssertFailException { + if (val != val2) + throw new AssertFailException("FAIL: " + message); + else + System.out.println("PASS: " + message); + } + + public static class AssertFailException extends Exception { + public AssertFailException(String message) { + super(message); + } + } +} diff --git a/src/test/java/org/scash/NativeBLAKE3Test.java b/src/test/java/org/scash/NativeBLAKE3Test.java new file mode 100644 index 0000000..94aa906 --- /dev/null +++ b/src/test/java/org/scash/NativeBLAKE3Test.java @@ -0,0 +1,13 @@ +package org.scash; + +import org.junit.Test; + +import static org.scash.NativeBLAKE3Util.assertEquals; +import static org.scash.NativeBLAKE3Util.AssertFailException; + +public class NativeBLAKE3Test { + @Test + public void testLibrary() throws AssertFailException { + assertEquals(NativeBLAKE3.isEnabled(), true, "isEnabled"); + } +}