Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasMLK committed Nov 19, 2022
2 parents a0e303a + 03dccfb commit 3d49bae
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "randomx"]
path = randomx
url = https://github.com/tevador/RandomX.git
url = https://github.com/tevador/RandomX
95 changes: 72 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,92 @@ Maven : v3.8.3
### 2. compile
### 2.1 checkout
```shell
git clone https://github.com/XDagger/xdagj-native-randomx.git
cd xdagj-native-randomx
git submodule init
git submodule update
```
### 2.2 compile randomx lib

#### 2.2.1 change cmake setting

```
cd randomx
vim CMakeLists.txt
```
modify CMakeList.txt in randomx folder.
change "add_library(randomx ${randomx_sources})" at line 178 to
"add_library(randomx SHARED ${randomx_sources})"

#### 2.2.2 compile c++ lib
```
mkdir build && cd build
cmake -DARCH=native ..
make
cp -i librandomx.dylib ../../src/main/resources
overwrite ../../src/main/resources/librandomx.dylib? (y/n [n]) y
```

#### 2.2.3 compile java lib

```
cd ../../
mvn package
```

### 3. example
### 3. maven system dependency

```xml
<dependency>
<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.1.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/xdagj-native-randomx-0.1.3.jar</systemPath>
</dependency>
```

### 4. example

```java
String key = "hello xdagj";
byte[] keyBytes = key.getBytes();
package io.xdag.crypto.randomx;

import java.nio.charset.StandardCharsets;
import com.google.common.collect.Lists;
import com.google.common.io.BaseEncoding;

public class Example {

public static void main(String[] args) {
String key = "hello xdagj-native-randomx";
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);

// 1. set flages
int flags = RandomXJNA.INSTANCE.randomx_get_flags();
// 1. build randomx jna wrapper
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
.flags(Lists.newArrayList(RandomXWrapper.Flag.JIT))
.fastInit(true)
.build();

// 2. alloc cache and dataset
PointerByReference cache = RandomXJNA.INSTANCE.randomx_alloc_cache(flags);
PointerByReference dataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(flags);
// 2. init dataset or cache
randomXWrapper.init(keyBytes);

// 3. init cache and dataset
Memory memory = new Memory(keyBytes.length);
memory.write(0, keyBytes, 0, keyBytes.length);
RandomXJNA.INSTANCE.randomx_init_cache(cache, memory, new NativeSize(keyBytes.length));
RandomXJNA.INSTANCE.randomx_init_dataset(dataset, cache, new NativeLong(0), RandomXJNA.INSTANCE.randomx_dataset_item_count());
// 3. create randomxVm
RandomXVM randomxVm = randomXWrapper.createVM();

// 4. alloc memory and set value
Pointer msgPointer = new Memory(keyBytes.length);
Pointer hashPointer = new Memory(RandomXUtils.HASH_SIZE);
msgPointer.write(0, keyBytes, 0, keyBytes.length);
// 4. calculate hash
byte[] hash = randomxVm.getHash(keyBytes);

// 5. create vm and calculate hash
RandomXVM vm = createVM(flags, cache, dataset);
RandomXJNA.INSTANCE.randomx_calculate_hash(vm.getPointer(), msgPointer, new NativeSize(keyBytes.length), hashPointer);
// 5. print result
System.out.println("message:" + key);
System.out.println("hash:" + BaseEncoding.base16().lowerCase().encode(hash));
}
}

// 6. get hash value from memory
byte[] hash = hashPointer.getByteArray(0, RandomXUtils.HASH_SIZE);
```

### 4. benchmark
### 5. benchmark

JMH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM

Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.1.2</version>
<version>0.1.3</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -129,6 +129,13 @@
<version>31.1-jre</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
1 change: 1 addition & 0 deletions randomx
Submodule randomx added at 261d58
32 changes: 32 additions & 0 deletions src/main/java/io/xdag/crypto/randomx/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.xdag.crypto.randomx;

import java.nio.charset.StandardCharsets;
import com.google.common.collect.Lists;
import com.google.common.io.BaseEncoding;

public class Example {

public static void main(String[] args) {
String key = "hello xdagj-native-randomx";
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);

// 1. build randomx jna wrapper
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
.flags(Lists.newArrayList(RandomXWrapper.Flag.JIT))
.fastInit(true)
.build();

// 2. init dataset or cache
randomXWrapper.init(keyBytes);

// 3. create randomxVm
RandomXVM randomxVm = randomXWrapper.createVM();

// 4. calculate hash
byte[] hash = randomxVm.getHash(keyBytes);

// 5. print result
System.out.println("message:" + key);
System.out.println("hash:" + BaseEncoding.base16().lowerCase().encode(hash));
}
}
15 changes: 8 additions & 7 deletions src/main/java/io/xdag/crypto/randomx/RandomXJNAPerformance.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.profile.LinuxPerfProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.google.common.collect.Lists;

@State(Scope.Benchmark)
@Threads(value = 1)
@BenchmarkMode({Mode.Throughput, Mode.AverageTime})
Expand All @@ -54,12 +55,12 @@ public class RandomXJNAPerformance {

@Setup(Level.Trial)
public void setup() {
RandomXWrapper.Builder builder = new RandomXWrapper.Builder();
builder.flag(RandomXWrapper.Flag.JIT);
builder.fastInit(true);
RandomXWrapper randomX = builder.build();
randomX.init(RandomUtils.nextBytes(32));
randomxVm = randomX.createVM();
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
.flags(Lists.newArrayList(RandomXWrapper.Flag.JIT))
.fastInit(true)
.build();
randomXWrapper.init(RandomUtils.nextBytes(32));
randomxVm = randomXWrapper.createVM();
}

@Benchmark
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/io/xdag/crypto/randomx/RandomXVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;

import lombok.Builder;

/**
* RandomX VM JNA Interface
*/
public class RandomXVM {
@Builder
public final class RandomXVM {

PointerByReference pointer;
RandomXWrapper parent;

public RandomXVM(PointerByReference pointer, RandomXWrapper parent) {
this.pointer = pointer;
this.parent = parent;
}

/**
* Calculate hash of given message
* @param message the message to get the hash of
Expand Down
Loading

0 comments on commit 3d49bae

Please sign in to comment.