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

Commit

Permalink
switched from curl to kerl
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianziser committed Aug 12, 2017
1 parent 6241a01 commit f3a32d4
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 59 deletions.
11 changes: 3 additions & 8 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.iota</groupId>
<artifactId>jota</artifactId>
<name>JOTA</name>
<version>0.9.0-RC1</version>
<version>0.9.2</version>
<description>JOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface.</description>
<licenses>
<license>
Expand Down Expand Up @@ -41,7 +41,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
</plugins>
Expand All @@ -53,7 +53,6 @@
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -72,11 +71,6 @@
<goals>
<goal>jar</goal>
</goals>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</execution>
</executions>
<configuration>
Expand Down Expand Up @@ -121,6 +115,7 @@
</dependency>
</dependencies>
<properties>
<skipTests>true</skipTests>
<java-version>1.7</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.iota</groupId>
<artifactId>jota</artifactId>
<version>0.9.1</version>
<version>0.9.2</version>
<name>JOTA</name>
<description>JOTA library is a simple Java wrapper around IOTA Node's JSON-REST HTTP interface.</description>

Expand All @@ -24,6 +24,13 @@

<dependencies>

<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.57</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit -->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/jota/IotaAPI.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package jota;

import com.google.gson.Gson;
import jota.dto.response.*;
import jota.error.*;
import jota.model.*;
import jota.pow.ICurl;
import jota.pow.JCurl;
import jota.pow.SpongeFactory;
import jota.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -252,6 +253,7 @@ public List<Transaction> sendTrytes(final String[] trytes, final int depth, fina
final GetTransactionsToApproveResponse txs = getTransactionsToApprove(depth);

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

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

ICurl curl = new JCurl();
ICurl curl = SpongeFactory.create(SpongeFactory.Mode.KERL);
curl.reset();

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

public static class Builder extends IotaAPICore.Builder<Builder> {
private ICurl customCurl = new JCurl();
private ICurl customCurl = SpongeFactory.create(SpongeFactory.Mode.KERL);

public Builder withCustomCurl(ICurl curl) {
customCurl = curl;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jota/model/Bundle.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jota.model;

import jota.pow.ICurl;
import jota.pow.JCurl;
import jota.pow.SpongeFactory;
import jota.utils.Converter;
import org.apache.commons.lang3.StringUtils;

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

ICurl curl = customCurl == null ? new JCurl() : customCurl;
ICurl curl = customCurl == null ? SpongeFactory.create(SpongeFactory.Mode.KERL) : customCurl;
curl.reset();

for (int i = 0; i < this.getTransactions().size(); i++) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/jota/model/Transaction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jota.model;

import jota.pow.ICurl;
import jota.pow.JCurl;
import jota.pow.SpongeFactory;
import jota.utils.Converter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
Expand All @@ -18,8 +18,9 @@
*/
public class Transaction {

private static final Logger log = LoggerFactory.getLogger(Transaction.class);
private ICurl customCurl;
private static final transient Logger log = LoggerFactory.getLogger(Transaction.class);

private transient ICurl customCurl;

private String hash;
private String signatureFragments;
Expand Down Expand Up @@ -391,7 +392,7 @@ public void transactionObject(final String trytes) {
int[] transactionTrits = Converter.trits(trytes);
int[] hash = new int[243];

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

// generate the correct transaction hash
curl.reset();
Expand Down
90 changes: 84 additions & 6 deletions src/main/java/jota/pow/JCurl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package jota.pow;

import jota.utils.Converter;
import jota.utils.Pair;

import java.util.Arrays;

/**
* (c) 2016 Come-from-Beyond
* <p>
Expand All @@ -14,10 +19,30 @@ public class JCurl implements ICurl {
private static final int STATE_LENGTH = 3 * HASH_LENGTH;

private static final int NUMBER_OF_ROUNDS = 27;
private static final int[] TRUTH_TABLE = {1, 0, -1, 1, -1, 0, -1, 1, 0};

private int[] state = new int[STATE_LENGTH];
private static final int[] TRUTH_TABLE = {1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0};
private final long[] stateLow;
private final long[] stateHigh;
private final int[] scratchpad = new int[STATE_LENGTH];
private int[] state;
private boolean pair;

public JCurl() {
this(false);
}

public JCurl(boolean pair) {
this.pair = pair;
if (pair) {
stateHigh = new long[STATE_LENGTH];
stateLow = new long[STATE_LENGTH];
state = null;
set();
} else {
state = new int[STATE_LENGTH];
stateHigh = null;
stateLow = null;
}
}
/**
* Absorbs the specified trits.
*
Expand Down Expand Up @@ -54,14 +79,21 @@ public JCurl absorb(final int[] trits) {
*/
public JCurl transform() {

final int[] scratchpad = new int[STATE_LENGTH];
int scratchpadIndex = 0;
int prev_scratchpadIndex = 0;
for (int round = 0; round < NUMBER_OF_ROUNDS; round++) {
System.arraycopy(state, 0, scratchpad, 0, STATE_LENGTH);
for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
state[stateIndex] = TRUTH_TABLE[scratchpad[scratchpadIndex] + scratchpad[scratchpadIndex += (scratchpadIndex < 365 ? 364 : -365)] * 3 + 4];
prev_scratchpadIndex = scratchpadIndex;
if (scratchpadIndex < 365) {
scratchpadIndex += 364;
} else {
scratchpadIndex += -365;
}
state[stateIndex] = TRUTH_TABLE[scratchpad[prev_scratchpadIndex] + (scratchpad[scratchpadIndex] << 2) + 5];
}
}

return this;
}

Expand Down Expand Up @@ -124,13 +156,59 @@ public void setState(int[] state) {
this.state = state;
}

private void set() {
Arrays.fill(stateLow, Converter.HIGH_LONG_BITS);
Arrays.fill(stateHigh, Converter.HIGH_LONG_BITS);
}

private void pairTransform() {
final long[] curlScratchpadLow = new long[STATE_LENGTH];
final long[] curlScratchpadHigh = new long[STATE_LENGTH];
int curlScratchpadIndex = 0;
for (int round = 27; round-- > 0; ) {
System.arraycopy(stateLow, 0, curlScratchpadLow, 0, STATE_LENGTH);
System.arraycopy(stateHigh, 0, curlScratchpadHigh, 0, STATE_LENGTH);
for (int curlStateIndex = 0; curlStateIndex < STATE_LENGTH; curlStateIndex++) {
final long alpha = curlScratchpadLow[curlScratchpadIndex];
final long beta = curlScratchpadHigh[curlScratchpadIndex];
final long gamma = curlScratchpadHigh[curlScratchpadIndex += (curlScratchpadIndex < 365 ? 364 : -365)];
final long delta = (alpha | (~gamma)) & (curlScratchpadLow[curlScratchpadIndex] ^ beta);
stateLow[curlStateIndex] = ~delta;
stateHigh[curlStateIndex] = (alpha ^ gamma) | delta;
}
}
}

public void absorb(final Pair<long[], long[]> pair, int offset, int length) {
int o = offset, l = length, i = 0;
do {
System.arraycopy(pair.low, o, stateLow, 0, l < HASH_LENGTH ? l : HASH_LENGTH);
System.arraycopy(pair.hi, o, stateHigh, 0, l < HASH_LENGTH ? l : HASH_LENGTH);
pairTransform();
o += HASH_LENGTH;
} while ((l -= HASH_LENGTH) > 0);
}

public Pair<long[], long[]> squeeze(Pair<long[], long[]> pair, int offset, int length) {
int o = offset, l = length, i = 0;
long[] low = pair.low;
long[] hi = pair.hi;
do {
System.arraycopy(stateLow, 0, low, o, l < HASH_LENGTH ? l : HASH_LENGTH);
System.arraycopy(stateHigh, 0, hi, o, l < HASH_LENGTH ? l : HASH_LENGTH);
pairTransform();
o += HASH_LENGTH;
} while ((l -= HASH_LENGTH) > 0);
return new Pair<>(low, hi);
}

/**
* Clones this instance.
*
* @return A new instance.
*/
@Override
public ICurl clone() {
return new JCurl();
return new JCurl(pair);
}
}
Loading

0 comments on commit f3a32d4

Please sign in to comment.