Skip to content

Commit 67f2a02

Browse files
authored
Merge pull request #9 from XDagger/develop
release 0.1.7
2 parents f72801a + 3cb9ff0 commit 67f2a02

File tree

11 files changed

+126
-89
lines changed

11 files changed

+126
-89
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mvn package
6262
<dependency>
6363
<groupId>io.xdag</groupId>
6464
<artifactId>xdagj-native-randomx</artifactId>
65-
<version>0.1.6</version>
65+
<version>0.1.7</version>
6666
</dependency>
6767
```
6868

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

7878
public class Example {
7979

80-
public static void main(String[] args) {
8180
String key = "hello xdagj-native-randomx";
8281
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
83-
82+
8483
// 1. build randomx jna wrapper
8584
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
86-
.flags(List.of(RandomXFlag.JIT))
87-
.fastInit(true)
85+
.flags(List.of(RandomXFlag.JIT, RandomXFlag.HARD_AES, RandomXFlag.ARGON2))
86+
.fastInit(false)
87+
.miningMode(false)
8888
.build();
89-
89+
90+
byte[] seed = new byte[]{(byte)1, (byte)2, (byte)3, (byte)4};
9091
// 2. init dataset or cache
91-
randomXWrapper.init(keyBytes);
92-
92+
randomXWrapper.init(seed);
93+
9394
// 3. create randomxVm
9495
RandomXVM randomxVm = randomXWrapper.createVM();
95-
96+
9697
// 4. calculate hash
9798
byte[] hash = randomxVm.getHash(keyBytes);
98-
99+
99100
// 5. print result
100101
HexFormat hex = HexFormat.of();
101102
System.out.println("message:" + key);
@@ -119,8 +120,8 @@ RAM :16 GB 2667 MHz DDR4
119120
```
120121

121122
| Benchmark | Mode | Cnt | Score | Error | Units |
122-
| :----------------------------: | :---: | :--: | :----: | :-----: | :---: |
123-
| RandomXJNAPerformance.testHash | thrpt | 25 | 70.130 | ± 2.128 | ops/s |
123+
| :----------------------------: | :---: | :--: |:------:| :-----: | :---: |
124+
| RandomXJNAPerformance.testHash | thrpt | 25 | 75.130 | ± 2.128 | ops/s |
124125
| RandomXJNAPerformance.testHash | avgt | 25 | 0.015 | ± 0.001 | s/op |
125126

126127

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.xdag</groupId>
88
<artifactId>xdagj-native-randomx</artifactId>
9-
<version>0.1.6</version>
9+
<version>0.1.7</version>
1010

1111
<name>xdagj-native-randomx</name>
1212
<description>A Java RandomX Library For XDAGJ</description>

src/main/java/io/xdag/crypto/randomx/Example.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ public static void main(String[] args) {
3535

3636
// 1. build randomx jna wrapper
3737
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
38-
.flags(List.of(RandomXFlag.JIT))
39-
.fastInit(true)
38+
.flags(List.of(RandomXFlag.JIT, RandomXFlag.HARD_AES, RandomXFlag.ARGON2))
39+
.fastInit(false)
40+
.miningMode(false)
4041
.build();
4142

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

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

src/main/java/io/xdag/crypto/randomx/RandomXJNA.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.sun.jna.Library;
2727
import com.sun.jna.NativeLong;
2828
import com.sun.jna.Pointer;
29-
import com.sun.jna.ptr.PointerByReference;
3029

3130
/**
3231
* RandomX JNA Interface
@@ -37,28 +36,28 @@ public interface RandomXJNA extends Library {
3736

3837
int randomx_get_flags();
3938

40-
PointerByReference randomx_alloc_cache(int flags);
39+
Pointer randomx_alloc_cache(int flags);
4140

42-
void randomx_init_cache(PointerByReference cache, Pointer key, NativeSize keySize);
41+
void randomx_init_cache(Pointer cache, Pointer key, NativeSize keySize);
4342

44-
void randomx_release_cache(PointerByReference cache);
43+
void randomx_release_cache(Pointer cache);
4544

46-
PointerByReference randomx_create_vm(int flags, PointerByReference cache, PointerByReference dataset);
45+
Pointer randomx_create_vm(int flags, Pointer cache, Pointer dataset);
4746

48-
void randomx_destroy_vm(PointerByReference machine);
47+
void randomx_destroy_vm(Pointer machine);
4948

50-
void randomx_vm_set_cache(PointerByReference machine, PointerByReference cache);
49+
void randomx_vm_set_cache(Pointer machine, Pointer cache);
5150

52-
void randomx_calculate_hash(PointerByReference machine, Pointer input, NativeSize inputSize, Pointer output);
51+
void randomx_calculate_hash(Pointer machine, Pointer input, NativeSize inputSize, Pointer output);
5352

54-
PointerByReference randomx_alloc_dataset(int flags);
53+
Pointer randomx_alloc_dataset(int flags);
5554

56-
void randomx_init_dataset(PointerByReference dataset, PointerByReference cache, NativeLong startItem, NativeLong itemCount);
55+
void randomx_init_dataset(Pointer dataset, Pointer cache, NativeLong startItem, NativeLong itemCount);
5756

58-
void randomx_vm_set_dataset(PointerByReference machine, PointerByReference dataset);
57+
void randomx_vm_set_dataset(Pointer machine, Pointer dataset);
5958

6059
NativeLong randomx_dataset_item_count();
6160

62-
void randomx_release_dataset(PointerByReference dataset);
61+
void randomx_release_dataset(Pointer dataset);
6362

6463
}

src/main/java/io/xdag/crypto/randomx/RandomXVM.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import com.sun.jna.Memory;
2727
import com.sun.jna.Pointer;
28-
import com.sun.jna.ptr.PointerByReference;
2928
import lombok.Builder;
3029
import lombok.ToString;
3130

@@ -36,7 +35,7 @@
3635
@ToString
3736
public final class RandomXVM {
3837

39-
PointerByReference pointer;
38+
Pointer pointer;
4039
RandomXWrapper parent;
4140

4241
/**
@@ -45,21 +44,20 @@ public final class RandomXVM {
4544
* @return the resulting hash
4645
*/
4746
public synchronized byte[] getHash(byte[] message) {
48-
Pointer msgPointer = new Memory(message.length);
47+
Memory msgPointer = new Memory(message.length);
4948
msgPointer.write(0, message, 0, message.length);
5049

51-
Pointer hashPointer = new Memory(RandomXUtils.HASH_SIZE);
50+
Memory hashPointer = new Memory(RandomXUtils.HASH_SIZE);
5251
RandomXJNA.INSTANCE.randomx_calculate_hash(pointer, msgPointer, new NativeSize(message.length), hashPointer);
5352

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

5655
msgPointer.clear(message.length);
5756
hashPointer.clear(RandomXUtils.HASH_SIZE);
58-
5957
return hash;
6058
}
6159

62-
PointerByReference getPointer() {
60+
Pointer getPointer() {
6361
return pointer;
6462
}
6563

src/main/java/io/xdag/crypto/randomx/RandomXWrapper.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.sun.jna.Memory;
2727
import com.sun.jna.NativeLong;
2828
import com.sun.jna.Pointer;
29-
import com.sun.jna.ptr.PointerByReference;
3029
import java.util.ArrayList;
3130
import java.util.Arrays;
3231
import java.util.List;
@@ -40,12 +39,13 @@
4039
@ToString
4140
public final class RandomXWrapper {
4241

43-
private PointerByReference cache;
44-
private PointerByReference dataset;
42+
private Pointer cache;
43+
private Pointer dataset;
4544

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

4847
private boolean fastInit;
48+
private boolean miningMode;
4949

5050
private Pointer memory;
5151
private int keySize;
@@ -58,10 +58,10 @@ public final class RandomXWrapper {
5858
* @param key The key to initialize randomX with. (generally a hash)
5959
*/
6060
public void init(byte[] key) {
61-
if(flags.contains(RandomXFlag.FULL_MEM)) {
61+
setCache(key);
62+
if(miningMode) {
63+
//Initialized cache is required to create dataset, first initialize it
6264
setDataset(key);
63-
} else {
64-
setCache(key);
6565
}
6666
}
6767

@@ -71,10 +71,19 @@ public void init(byte[] key) {
7171
* @return RandomX_VM an Object representing the resulting VM
7272
*/
7373
public RandomXVM createVM() {
74-
if (flags.contains(RandomXFlag.JIT)){
75-
flagsValue = RandomXFlag.JIT.getValue();
74+
if(flags.isEmpty()) {
75+
flagsValue = RandomXJNA.INSTANCE.randomx_get_flags();
76+
} else {
77+
for (RandomXFlag flag : flags) {
78+
flagsValue += flag.getValue();
79+
}
80+
81+
}
82+
Pointer pointer = RandomXJNA.INSTANCE.randomx_create_vm(flagsValue, cache, dataset);
83+
if(pointer == null) {
84+
throw new RuntimeException("create randomx vm error.");
7685
}
77-
RandomXVM vm = new RandomXVM(RandomXJNA.INSTANCE.randomx_create_vm(flagsValue, cache, dataset), this);
86+
RandomXVM vm = new RandomXVM(pointer, this);
7887
vms.add(vm);
7988
return vm;
8089
}
@@ -86,10 +95,12 @@ public RandomXVM createVM() {
8695
private void setCache(byte[] key) {
8796
if(this.memory != null && Arrays.equals(key, this.memory.getByteArray(0, keySize)))
8897
return;
89-
if (flags.contains(RandomXFlag.JIT)){
90-
flagsValue = RandomXFlag.JIT.getValue();
98+
99+
Pointer newCache = RandomXJNA.INSTANCE.randomx_alloc_cache(flagsValue);
100+
101+
if(newCache == null) {
102+
throw new RuntimeException("alloc cache error.");
91103
}
92-
PointerByReference newCache = RandomXJNA.INSTANCE.randomx_alloc_cache(flagsValue);
93104

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

116-
//Initialized cache is required to create dataset, first initialize it
117-
setCache(key);
118-
119-
PointerByReference newDataset;
120-
121127
//Allocate memory for dataset
122-
if(flags.contains(RandomXFlag.LARGE_PAGES)) {
123-
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(RandomXFlag.LARGE_PAGES.getValue());
124-
} else if (flags.contains(RandomXFlag.JIT)){
125-
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(RandomXFlag.JIT.getValue());
126-
} else {
127-
newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(0);
128+
Pointer newDataset = RandomXJNA.INSTANCE.randomx_alloc_dataset(flagsValue);
129+
130+
if(newDataset == null) {
131+
throw new RuntimeException("alloc dataset error.");
128132
}
129133

130134
if(fastInit) {
@@ -181,21 +185,20 @@ private void setDataset(byte[] key) {
181185
* @param key The key to initialize randomX with. (generally a hash)
182186
*/
183187
public void changeKey(byte[] key) {
184-
if(flags.contains(RandomXFlag.FULL_MEM)) {
188+
setCache(key);
189+
for(RandomXVM vm : vms) {
190+
if(vm.getPointer() != null) {
191+
RandomXJNA.INSTANCE.randomx_vm_set_cache(vm.getPointer(), cache);
192+
}
193+
}
194+
if(miningMode) {
185195
setDataset(key);
186196
for(RandomXVM vm : vms) {
187197
if(vm.getPointer() != null) {
188198
RandomXJNA.INSTANCE.randomx_vm_set_dataset(vm.getPointer(), dataset);
189199
}
190200

191201
}
192-
} else {
193-
setCache(key);
194-
for(RandomXVM vm : vms) {
195-
if(vm.getPointer() != null) {
196-
RandomXJNA.INSTANCE.randomx_vm_set_cache(vm.getPointer(), cache);
197-
}
198-
}
199202
}
200203
}
201204

-104 Bytes
Binary file not shown.

src/main/resources/librandomx.so

2.76 KB
Binary file not shown.

src/test/java/io/xdag/crypto/randomx/RandomXJNAPerfTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ public class RandomXJNAPerfTest {
5858
@Setup(Level.Trial)
5959
public void setup() {
6060
RandomXWrapper randomXWrapper = RandomXWrapper.builder()
61-
.flags(List.of(RandomXFlag.JIT))
61+
.flags(List.of(RandomXFlag.JIT, RandomXFlag.HARD_AES, RandomXFlag.ARGON2))
6262
.fastInit(true)
63+
.miningMode(false)
6364
.build();
6465
byte[] buffer = new byte[32];
6566
random.nextBytes(buffer);

0 commit comments

Comments
 (0)