Repository navigation
Expand file tree
/
Copy pathJCurl.java
More file actions
215 lines (189 loc) · 6.22 KB
/
Copy pathJCurl.java
File metadata and controls
215 lines (189 loc) · 6.22 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package jota.pow;
import jota.utils.Converter;
import jota.utils.Pair;
import java.util.Arrays;
/**
* (c) 2016 Come-from-Beyond
* <p>
* JCurl belongs to the sponge function family.
*/
public class JCurl implements ICurl {
/**
* The hash length.
*/
public static final int HASH_LENGTH = 243;
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, 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.
*
* @param trits The trits.
* @param offset The offset to start from.
* @param length The length.
* @return The ICurl instance (used for method chaining).
*/
public JCurl absorb(final int[] trits, int offset, int length) {
do {
System.arraycopy(trits, offset, state, 0, length < HASH_LENGTH ? length : HASH_LENGTH);
transform();
offset += HASH_LENGTH;
} while ((length -= HASH_LENGTH) > 0);
return this;
}
/**
* Absorbs the specified trits.
*
* @param trits The trits.
* @return The ICurl instance (used for method chaining).
*/
public JCurl absorb(final int[] trits) {
return absorb(trits, 0, trits.length);
}
/**
* Transforms this instance.
*
* @return The ICurl instance (used for method chaining).
*/
public JCurl transform() {
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++) {
prev_scratchpadIndex = scratchpadIndex;
if (scratchpadIndex < 365) {
scratchpadIndex += 364;
} else {
scratchpadIndex += -365;
}
state[stateIndex] = TRUTH_TABLE[scratchpad[prev_scratchpadIndex] + (scratchpad[scratchpadIndex] << 2) + 5];
}
}
return this;
}
/**
* Resets this state.
*
* @return The ICurl instance (used for method chaining).
*/
public JCurl reset() {
for (int stateIndex = 0; stateIndex < STATE_LENGTH; stateIndex++) {
state[stateIndex] = 0;
}
return this;
}
/**
* Squeezes the specified trits.
*
* @param trits The trits.
* @param offset The offset to start from.
* @param length The length.
* @return The squeezes trits.
*/
public int[] squeeze(final int[] trits, int offset, int length) {
do {
System.arraycopy(state, 0, trits, offset, length < HASH_LENGTH ? length : HASH_LENGTH);
transform();
offset += HASH_LENGTH;
} while ((length -= HASH_LENGTH) > 0);
return state;
}
/**
* Squeezes the specified trits.
*
* @param trits The trits.
* @return The squeezes trits.
*/
public int[] squeeze(final int[] trits) {
return squeeze(trits, 0, trits.length);
}
/**
* Gets the states.
*
* @return The state.
*/
public int[] getState() {
return state;
}
/**
* Sets the state.
*
* @param state The states.
*/
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(pair);
}
}