Repository navigation
Expand file tree
/
Copy pathKerl.java
More file actions
169 lines (119 loc) · 4.58 KB
/
Copy pathKerl.java
File metadata and controls
169 lines (119 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package jota.pow;
import org.bouncycastle.jcajce.provider.digest.Keccak;
import java.math.BigInteger;
import java.util.Arrays;
public class Kerl extends JCurl {
private static final int HASH_LENGTH = 243;
private static final int BIT_HASH_LENGTH = 384;
private static final int BYTE_HASH_LENGTH = BIT_HASH_LENGTH / 8;
private static final int RADIX = 3;
private static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE;
private final Keccak.Digest384 keccak;
private byte[] byte_state;
private int[] trit_state;
public Kerl() {
super();
this.keccak = new Keccak.Digest384();
this.byte_state = new byte[BYTE_HASH_LENGTH];
this.trit_state = new int[HASH_LENGTH];
}
public static BigInteger convertTritsToBigint(final int[] trits, final int offset, final int size) {
BigInteger value = BigInteger.ZERO;
for (int i = size; i-- > 0; ) {
value = value.multiply(BigInteger.valueOf(RADIX)).add(BigInteger.valueOf(trits[offset + i]));
}
return value;
}
private static BigInteger convertBytesToBigInt(final byte[] bytes, final int offset, final int size) {
return new BigInteger(Arrays.copyOfRange(bytes, offset, offset + size));
}
private static int[] convertBigintToTrits(final BigInteger value, int size) {
int[] destination = new int[size];
BigInteger absoluteValue = value.compareTo(BigInteger.ZERO) < 0 ? value.negate() : value;
for (int i = 0; i < size; i++) {
BigInteger[] divRemainder = absoluteValue.divideAndRemainder(BigInteger.valueOf(RADIX));
int remainder = divRemainder[1].intValue();
absoluteValue = divRemainder[0];
if (remainder > MAX_TRIT_VALUE) {
remainder = MIN_TRIT_VALUE;
absoluteValue = absoluteValue.add(BigInteger.ONE);
}
destination[i] = remainder;
}
if (value.compareTo(BigInteger.ZERO) < 0) {
for (int i = 0; i < size; i++) {
destination[i] = -destination[i];
}
}
return destination;
}
private static byte[] convertBigintToBytes(final BigInteger value, int size) {
final byte[] result = new byte[BYTE_HASH_LENGTH];
final byte[] bytes = value.toByteArray();
int i = 0;
while (i + bytes.length < BYTE_HASH_LENGTH) {
result[i++] = (byte) (bytes[0] < 0 ? -1 : 0);
}
for (int j = bytes.length; j-- > 0; ) {
result[i++] = bytes[bytes.length - 1 - j];
}
return result;
}
@Override
public Kerl reset() {
this.keccak.reset();
return this;
}
@Override
public Kerl absorb(final int[] trits, int offset, int length) {
if (length % 243 != 0) throw new RuntimeException("Illegal length: " + length);
do {
//copy trits[offset:offset+length]
System.arraycopy(trits, offset, trit_state, 0, HASH_LENGTH);
//convert to bits
trit_state[HASH_LENGTH - 1] = 0;
byte[] bytes = convertBigintToBytes(convertTritsToBigint(trit_state, 0, HASH_LENGTH), BYTE_HASH_LENGTH);
//run keccak
keccak.update(bytes);
offset += HASH_LENGTH;
} while ((length -= HASH_LENGTH) > 0);
return this;
}
@Override
public int[] squeeze(final int[] trits, int offset, int length) {
if (length % 243 != 0) throw new RuntimeException("Illegal length: " + length);
do {
byte_state = this.keccak.digest();
//convert to trits
trit_state = convertBigintToTrits(convertBytesToBigInt(byte_state, 0, BYTE_HASH_LENGTH), HASH_LENGTH);
//copy with offset
trit_state[HASH_LENGTH - 1] = 0;
System.arraycopy(trit_state, 0, trits, offset, HASH_LENGTH);
//calculate hash again
for (int i = byte_state.length; i-- > 0; ) {
byte_state[i] = (byte) (byte_state[i] ^ 0xFF);
}
keccak.update(byte_state);
offset += HASH_LENGTH;
} while ((length -= HASH_LENGTH) > 0);
return trits;
}
/**
* Squeezes the specified trits.
*
* @param trits The trits.
* @return The squeezes trits.
*/
public int[] squeeze(final int[] trits) {
return squeeze(trits, 0, trits.length);
}
/**
* Clones this instance.
*
* @return A new instance.
*/
@Override
public Kerl clone() {
return new Kerl();
}
}