Skip to content

Commit

Permalink
Merge pull request #9 from XDagger/develop
Browse files Browse the repository at this point in the history
release 0.1.7
  • Loading branch information
LucasMLK committed Aug 31, 2023
2 parents f72801a + 3cb9ff0 commit 67f2a02
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 89 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mvn package
<dependency>
<groupId>io.xdag</groupId>
<artifactId>xdagj-native-randomx</artifactId>
<version>0.1.6</version>
<version>0.1.7</version>
</dependency>
```

Expand All @@ -77,25 +77,26 @@ import java.util.List;

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(List.of(RandomXFlag.JIT))
.fastInit(true)
.flags(List.of(RandomXFlag.JIT, RandomXFlag.HARD_AES, RandomXFlag.ARGON2))
.fastInit(false)
.miningMode(false)
.build();


byte[] seed = new byte[]{(byte)1, (byte)2, (byte)3, (byte)4};
// 2. init dataset or cache
randomXWrapper.init(keyBytes);

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

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

// 5. print result
HexFormat hex = HexFormat.of();
System.out.println("message:" + key);
Expand All @@ -119,8 +120,8 @@ RAM :16 GB 2667 MHz DDR4
```

| Benchmark | Mode | Cnt | Score | Error | Units |
| :----------------------------: | :---: | :--: | :----: | :-----: | :---: |
| RandomXJNAPerformance.testHash | thrpt | 25 | 70.130 | ± 2.128 | ops/s |
| :----------------------------: | :---: | :--: |:------:| :-----: | :---: |
| RandomXJNAPerformance.testHash | thrpt | 25 | 75.130 | ± 2.128 | ops/s |
| RandomXJNAPerformance.testHash | avgt | 25 | 0.015 | ± 0.001 | s/op |


Expand Down
2 changes: 1 addition & 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.6</version>
<version>0.1.7</version>

<name>xdagj-native-randomx</name>
<description>A Java RandomX Library For XDAGJ</description>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/io/xdag/crypto/randomx/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ public static void main(String[] args) {

// 1. build randomx jna wrapper
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
.flags(List.of(RandomXFlag.JIT))
.fastInit(true)
.flags(List.of(RandomXFlag.JIT, RandomXFlag.HARD_AES, RandomXFlag.ARGON2))
.fastInit(false)
.miningMode(false)
.build();

byte[] seed = new byte[]{(byte)1, (byte)2, (byte)3, (byte)4};
// 2. init dataset or cache
randomXWrapper.init(keyBytes);
randomXWrapper.init(seed);

// 3. create randomxVm
RandomXVM randomxVm = randomXWrapper.createVM();
Expand Down
23 changes: 11 additions & 12 deletions src/main/java/io/xdag/crypto/randomx/RandomXJNA.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.sun.jna.Library;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;

/**
* RandomX JNA Interface
Expand All @@ -37,28 +36,28 @@ public interface RandomXJNA extends Library {

int randomx_get_flags();

PointerByReference randomx_alloc_cache(int flags);
Pointer randomx_alloc_cache(int flags);

void randomx_init_cache(PointerByReference cache, Pointer key, NativeSize keySize);
void randomx_init_cache(Pointer cache, Pointer key, NativeSize keySize);

void randomx_release_cache(PointerByReference cache);
void randomx_release_cache(Pointer cache);

PointerByReference randomx_create_vm(int flags, PointerByReference cache, PointerByReference dataset);
Pointer randomx_create_vm(int flags, Pointer cache, Pointer dataset);

void randomx_destroy_vm(PointerByReference machine);
void randomx_destroy_vm(Pointer machine);

void randomx_vm_set_cache(PointerByReference machine, PointerByReference cache);
void randomx_vm_set_cache(Pointer machine, Pointer cache);

void randomx_calculate_hash(PointerByReference machine, Pointer input, NativeSize inputSize, Pointer output);
void randomx_calculate_hash(Pointer machine, Pointer input, NativeSize inputSize, Pointer output);

PointerByReference randomx_alloc_dataset(int flags);
Pointer randomx_alloc_dataset(int flags);

void randomx_init_dataset(PointerByReference dataset, PointerByReference cache, NativeLong startItem, NativeLong itemCount);
void randomx_init_dataset(Pointer dataset, Pointer cache, NativeLong startItem, NativeLong itemCount);

void randomx_vm_set_dataset(PointerByReference machine, PointerByReference dataset);
void randomx_vm_set_dataset(Pointer machine, Pointer dataset);

NativeLong randomx_dataset_item_count();

void randomx_release_dataset(PointerByReference dataset);
void randomx_release_dataset(Pointer dataset);

}
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 @@ -25,7 +25,6 @@

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import lombok.Builder;
import lombok.ToString;

Expand All @@ -36,7 +35,7 @@
@ToString
public final class RandomXVM {

PointerByReference pointer;
Pointer pointer;
RandomXWrapper parent;

/**
Expand All @@ -45,21 +44,20 @@ public final class RandomXVM {
* @return the resulting hash
*/
public synchronized byte[] getHash(byte[] message) {
Pointer msgPointer = new Memory(message.length);
Memory msgPointer = new Memory(message.length);
msgPointer.write(0, message, 0, message.length);

Pointer hashPointer = new Memory(RandomXUtils.HASH_SIZE);
Memory hashPointer = new Memory(RandomXUtils.HASH_SIZE);
RandomXJNA.INSTANCE.randomx_calculate_hash(pointer, msgPointer, new NativeSize(message.length), hashPointer);

byte[] hash = hashPointer.getByteArray(0, RandomXUtils.HASH_SIZE);

msgPointer.clear(message.length);
hashPointer.clear(RandomXUtils.HASH_SIZE);

return hash;
}

PointerByReference getPointer() {
Pointer getPointer() {
return pointer;
}

Expand Down
65 changes: 34 additions & 31 deletions src/main/java/io/xdag/crypto/randomx/RandomXWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.sun.jna.Memory;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -40,12 +39,13 @@
@ToString
public final class RandomXWrapper {

private PointerByReference cache;
private PointerByReference dataset;
private Pointer cache;
private Pointer dataset;

final List<RandomXVM> vms = new ArrayList<>();

private boolean fastInit;
private boolean miningMode;

private Pointer memory;
private int keySize;
Expand All @@ -58,10 +58,10 @@ public final class RandomXWrapper {
* @param key The key to initialize randomX with. (generally a hash)
*/
public void init(byte[] key) {
if(flags.contains(RandomXFlag.FULL_MEM)) {
setCache(key);
if(miningMode) {
//Initialized cache is required to create dataset, first initialize it
setDataset(key);
} else {
setCache(key);
}
}

Expand All @@ -71,10 +71,19 @@ public void init(byte[] key) {
* @return RandomX_VM an Object representing the resulting VM
*/
public RandomXVM createVM() {
if (flags.contains(RandomXFlag.JIT)){
flagsValue = RandomXFlag.JIT.getValue();
if(flags.isEmpty()) {
flagsValue = RandomXJNA.INSTANCE.randomx_get_flags();
} else {
for (RandomXFlag flag : flags) {
flagsValue += flag.getValue();
}

}
Pointer pointer = RandomXJNA.INSTANCE.randomx_create_vm(flagsValue, cache, dataset);
if(pointer == null) {
throw new RuntimeException("create randomx vm error.");
}
RandomXVM vm = new RandomXVM(RandomXJNA.INSTANCE.randomx_create_vm(flagsValue, cache, dataset), this);
RandomXVM vm = new RandomXVM(pointer, this);
vms.add(vm);
return vm;
}
Expand All @@ -86,10 +95,12 @@ public RandomXVM createVM() {
private void setCache(byte[] key) {
if(this.memory != null && Arrays.equals(key, this.memory.getByteArray(0, keySize)))
return;
if (flags.contains(RandomXFlag.JIT)){
flagsValue = RandomXFlag.JIT.getValue();

Pointer newCache = RandomXJNA.INSTANCE.randomx_alloc_cache(flagsValue);

if(newCache == null) {
throw new RuntimeException("alloc cache error.");
}
PointerByReference newCache = RandomXJNA.INSTANCE.randomx_alloc_cache(flagsValue);

this.memory = new Memory(key.length);
this.memory.write(0, key, 0, key.length);
Expand All @@ -113,18 +124,11 @@ private void setDataset(byte[] key) {
return;
}

//Initialized cache is required to create dataset, first initialize it
setCache(key);

PointerByReference newDataset;

//Allocate memory for dataset
if(flags.contains(RandomXFlag.LARGE_PAGES)) {
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(RandomXFlag.LARGE_PAGES.getValue());
} else if (flags.contains(RandomXFlag.JIT)){
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(RandomXFlag.JIT.getValue());
} else {
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(0);
Pointer newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(flagsValue);

if(newDataset == null) {
throw new RuntimeException("alloc dataset error.");
}

if(fastInit) {
Expand Down Expand Up @@ -181,21 +185,20 @@ private void setDataset(byte[] key) {
* @param key The key to initialize randomX with. (generally a hash)
*/
public void changeKey(byte[] key) {
if(flags.contains(RandomXFlag.FULL_MEM)) {
setCache(key);
for(RandomXVM vm : vms) {
if(vm.getPointer() != null) {
RandomXJNA.INSTANCE.randomx_vm_set_cache(vm.getPointer(), cache);
}
}
if(miningMode) {
setDataset(key);
for(RandomXVM vm : vms) {
if(vm.getPointer() != null) {
RandomXJNA.INSTANCE.randomx_vm_set_dataset(vm.getPointer(), dataset);
}

}
} else {
setCache(key);
for(RandomXVM vm : vms) {
if(vm.getPointer() != null) {
RandomXJNA.INSTANCE.randomx_vm_set_cache(vm.getPointer(), cache);
}
}
}
}

Expand Down
Binary file modified src/main/resources/librandomx.dylib
Binary file not shown.
Binary file modified src/main/resources/librandomx.so
Binary file not shown.
3 changes: 2 additions & 1 deletion src/test/java/io/xdag/crypto/randomx/RandomXJNAPerfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public class RandomXJNAPerfTest {
@Setup(Level.Trial)
public void setup() {
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
.flags(List.of(RandomXFlag.JIT))
.flags(List.of(RandomXFlag.JIT, RandomXFlag.HARD_AES, RandomXFlag.ARGON2))
.fastInit(true)
.miningMode(false)
.build();
byte[] buffer = new byte[32];
random.nextBytes(buffer);
Expand Down

0 comments on commit 67f2a02

Please sign in to comment.