Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit f3a32d4

Browse files
committed
switched from curl to kerl
1 parent 6241a01 commit f3a32d4

16 files changed

+367
-59
lines changed

dependency-reduced-pom.xml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>org.iota</groupId>
55
<artifactId>jota</artifactId>
66
<name>JOTA</name>
7-
<version>0.9.0-RC1</version>
7+
<version>0.9.2</version>
88
<description>JOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface.</description>
99
<licenses>
1010
<license>
@@ -41,7 +41,7 @@
4141
<artifactId>maven-surefire-plugin</artifactId>
4242
<version>2.12.4</version>
4343
<configuration>
44-
<skipTests>true</skipTests>
44+
<skipTests>${skipTests}</skipTests>
4545
</configuration>
4646
</plugin>
4747
</plugins>
@@ -53,7 +53,6 @@
5353
<plugins>
5454
<plugin>
5555
<artifactId>maven-source-plugin</artifactId>
56-
<version>3.0.1</version>
5756
<executions>
5857
<execution>
5958
<id>attach-sources</id>
@@ -72,11 +71,6 @@
7271
<goals>
7372
<goal>jar</goal>
7473
</goals>
75-
<configuration>
76-
<show>private</show>
77-
<nohelp>true</nohelp>
78-
<additionalparam>-Xdoclint:none</additionalparam>
79-
</configuration>
8074
</execution>
8175
</executions>
8276
<configuration>
@@ -121,6 +115,7 @@
121115
</dependency>
122116
</dependencies>
123117
<properties>
118+
<skipTests>true</skipTests>
124119
<java-version>1.7</java-version>
125120
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
126121
</properties>

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.iota</groupId>
66
<artifactId>jota</artifactId>
7-
<version>0.9.1</version>
7+
<version>0.9.2</version>
88
<name>JOTA</name>
99
<description>JOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface.</description>
1010

@@ -24,6 +24,13 @@
2424

2525
<dependencies>
2626

27+
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
28+
<dependency>
29+
<groupId>org.bouncycastle</groupId>
30+
<artifactId>bcprov-jdk15on</artifactId>
31+
<version>1.57</version>
32+
</dependency>
33+
2734
<!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit -->
2835
<dependency>
2936
<groupId>com.squareup.retrofit2</groupId>

src/main/java/jota/IotaAPI.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package jota;
22

3+
import com.google.gson.Gson;
34
import jota.dto.response.*;
45
import jota.error.*;
56
import jota.model.*;
67
import jota.pow.ICurl;
7-
import jota.pow.JCurl;
8+
import jota.pow.SpongeFactory;
89
import jota.utils.*;
910
import org.apache.commons.lang3.StringUtils;
1011
import org.slf4j.Logger;
@@ -252,6 +253,7 @@ public List<Transaction> sendTrytes(final String[] trytes, final int depth, fina
252253
final GetTransactionsToApproveResponse txs = getTransactionsToApprove(depth);
253254

254255
// attach to tangle - do pow
256+
System.out.println(new Gson().toJson(txs));
255257
final GetAttachToTangleResponse res = attachToTangle(txs.getTrunkTransaction(), txs.getBranchTransaction(), minWeightMagnitude, trytes);
256258

257259
try {
@@ -641,7 +643,7 @@ public GetBundleResponse getBundle(String transaction) throws ArgumentException,
641643
long totalSum = 0;
642644
String bundleHash = bundle.getTransactions().get(0).getBundle();
643645

644-
ICurl curl = new JCurl();
646+
ICurl curl = SpongeFactory.create(SpongeFactory.Mode.KERL);
645647
curl.reset();
646648

647649
List<Signature> signaturesToValidate = new ArrayList<>();
@@ -1108,7 +1110,7 @@ public List<String> addRemainder(final String seed,
11081110
}
11091111

11101112
public static class Builder extends IotaAPICore.Builder<Builder> {
1111-
private ICurl customCurl = new JCurl();
1113+
private ICurl customCurl = SpongeFactory.create(SpongeFactory.Mode.KERL);
11121114

11131115
public Builder withCustomCurl(ICurl curl) {
11141116
customCurl = curl;

src/main/java/jota/model/Bundle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package jota.model;
22

33
import jota.pow.ICurl;
4-
import jota.pow.JCurl;
4+
import jota.pow.SpongeFactory;
55
import jota.utils.Converter;
66
import org.apache.commons.lang3.StringUtils;
77

@@ -90,7 +90,7 @@ public void addEntry(int signatureMessageLength, String address, long value, Str
9090
*/
9191
public void finalize(ICurl customCurl) {
9292

93-
ICurl curl = customCurl == null ? new JCurl() : customCurl;
93+
ICurl curl = customCurl == null ? SpongeFactory.create(SpongeFactory.Mode.KERL) : customCurl;
9494
curl.reset();
9595

9696
for (int i = 0; i < this.getTransactions().size(); i++) {

src/main/java/jota/model/Transaction.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package jota.model;
22

33
import jota.pow.ICurl;
4-
import jota.pow.JCurl;
4+
import jota.pow.SpongeFactory;
55
import jota.utils.Converter;
66
import org.apache.commons.lang3.StringUtils;
77
import org.apache.commons.lang3.builder.ToStringBuilder;
@@ -18,8 +18,9 @@
1818
*/
1919
public class Transaction {
2020

21-
private static final Logger log = LoggerFactory.getLogger(Transaction.class);
22-
private ICurl customCurl;
21+
private static final transient Logger log = LoggerFactory.getLogger(Transaction.class);
22+
23+
private transient ICurl customCurl;
2324

2425
private String hash;
2526
private String signatureFragments;
@@ -391,7 +392,7 @@ public void transactionObject(final String trytes) {
391392
int[] transactionTrits = Converter.trits(trytes);
392393
int[] hash = new int[243];
393394

394-
final ICurl curl = customCurl == null ? new JCurl() : customCurl; // we need a fluent JCurl.
395+
final ICurl curl = customCurl == null ? SpongeFactory.create(SpongeFactory.Mode.KERL) : customCurl; // we need a fluent JCurl.
395396

396397
// generate the correct transaction hash
397398
curl.reset();

src/main/java/jota/pow/JCurl.java

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package jota.pow;
22

3+
import jota.utils.Converter;
4+
import jota.utils.Pair;
5+
6+
import java.util.Arrays;
7+
38
/**
49
* (c) 2016 Come-from-Beyond
510
* <p>
@@ -14,10 +19,30 @@ public class JCurl implements ICurl {
1419
private static final int STATE_LENGTH = 3 * HASH_LENGTH;
1520

1621
private static final int NUMBER_OF_ROUNDS = 27;
17-
private static final int[] TRUTH_TABLE = {1, 0, -1, 1, -1, 0, -1, 1, 0};
18-
19-
private int[] state = new int[STATE_LENGTH];
22+
private static final int[] TRUTH_TABLE = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0};
23+
private final long[] stateLow;
24+
private final long[] stateHigh;
25+
private final int[] scratchpad = new int[STATE_LENGTH];
26+
private int[] state;
27+
private boolean pair;
28+
29+
public JCurl() {
30+
this(false);
31+
}
2032

33+
public JCurl(boolean pair) {
34+
this.pair = pair;
35+
if (pair) {
36+
stateHigh = new long[STATE_LENGTH];
37+
stateLow = new long[STATE_LENGTH];
38+
state = null;
39+
set();
40+
} else {
41+
state = new int[STATE_LENGTH];
42+
stateHigh = null;
43+
stateLow = null;
44+
}
45+
}
2146
/**
2247
* Absorbs the specified trits.
2348
*
@@ -54,14 +79,21 @@ public JCurl absorb(final int[] trits) {
5479
*/
5580
public JCurl transform() {
5681

57-
final int[] scratchpad = new int[STATE_LENGTH];
5882
int scratchpadIndex = 0;
83+
int prev_scratchpadIndex = 0;
5984
for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
6085
System.arraycopy(state, 0, scratchpad, 0, STATE_LENGTH);
6186
for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
62-
state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndex] + scratchpad[scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365)] * 3 + 4];
87+
prev_scratchpadIndex = scratchpadIndex;
88+
if (scratchpadIndex < 365) {
89+
scratchpadIndex += 364;
90+
} else {
91+
scratchpadIndex += -365;
92+
}
93+
state[stateIndex] = TRUTH_TABLE[scratchpad[prev_scratchpadIndex] + (scratchpad[scratchpadIndex] << 2) + 5];
6394
}
6495
}
96+
6597
return this;
6698
}
6799

@@ -124,13 +156,59 @@ public void setState(int[] state) {
124156
this.state = state;
125157
}
126158

159+
private void set() {
160+
Arrays.fill(stateLow, Converter.HIGH_LONG_BITS);
161+
Arrays.fill(stateHigh, Converter.HIGH_LONG_BITS);
162+
}
163+
164+
private void pairTransform() {
165+
final long[] curlScratchpadLow = new long[STATE_LENGTH];
166+
final long[] curlScratchpadHigh = new long[STATE_LENGTH];
167+
int curlScratchpadIndex = 0;
168+
for (int round = 27; round-- > 0; ) {
169+
System.arraycopy(stateLow, 0, curlScratchpadLow, 0, STATE_LENGTH);
170+
System.arraycopy(stateHigh, 0, curlScratchpadHigh, 0, STATE_LENGTH);
171+
for (int curlStateIndex = 0; curlStateIndex < STATE_LENGTH; curlStateIndex++) {
172+
final long alpha = curlScratchpadLow[curlScratchpadIndex];
173+
final long beta = curlScratchpadHigh[curlScratchpadIndex];
174+
final long gamma = curlScratchpadHigh[curlScratchpadIndex += (curlScratchpadIndex < 365 ? 364 : -365)];
175+
final long delta = (alpha | (~gamma)) & (curlScratchpadLow[curlScratchpadIndex] ^ beta);
176+
stateLow[curlStateIndex] = ~delta;
177+
stateHigh[curlStateIndex] = (alpha ^ gamma) | delta;
178+
}
179+
}
180+
}
181+
182+
public void absorb(final Pair<long[], long[]> pair, int offset, int length) {
183+
int o = offset, l = length, i = 0;
184+
do {
185+
System.arraycopy(pair.low, o, stateLow, 0, l < HASH_LENGTH ? l : HASH_LENGTH);
186+
System.arraycopy(pair.hi, o, stateHigh, 0, l < HASH_LENGTH ? l : HASH_LENGTH);
187+
pairTransform();
188+
o += HASH_LENGTH;
189+
} while ((l -= HASH_LENGTH) > 0);
190+
}
191+
192+
public Pair<long[], long[]> squeeze(Pair<long[], long[]> pair, int offset, int length) {
193+
int o = offset, l = length, i = 0;
194+
long[] low = pair.low;
195+
long[] hi = pair.hi;
196+
do {
197+
System.arraycopy(stateLow, 0, low, o, l < HASH_LENGTH ? l : HASH_LENGTH);
198+
System.arraycopy(stateHigh, 0, hi, o, l < HASH_LENGTH ? l : HASH_LENGTH);
199+
pairTransform();
200+
o += HASH_LENGTH;
201+
} while ((l -= HASH_LENGTH) > 0);
202+
return new Pair<>(low, hi);
203+
}
204+
127205
/**
128206
* Clones this instance.
129207
*
130208
* @return A new instance.
131209
*/
132210
@Override
133211
public ICurl clone() {
134-
return new JCurl();
212+
return new JCurl(pair);
135213
}
136214
}

0 commit comments

Comments
 (0)