This repository was archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathKerlTest.java
More file actions
257 lines (228 loc) · 10.9 KB
/
KerlTest.java
File metadata and controls
257 lines (228 loc) · 10.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//import com.iota.iri.model.Hash; //used for generate* tests - can be imported from IRI.
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Random;
/**
* Created by alon on 04/08/17.
*/
public class KerlTest {
final static Random seed = new Random();
Logger log = LoggerFactory.getLogger(CurlTest.class);
//Test conversion functions:
@Test
public void bytesFromBigInt() throws Exception {
int byte_size = 48;
BigInteger bigInteger = new BigInteger("13190295509826637194583200125168488859623001289643321872497025844241981297292953903419783680940401133507992851240799");
byte[] outBytes = Kerl.convertBigintToBytes(bigInteger,byte_size);
BigInteger out_bigInteger = Kerl.convertBytesToBigInt(outBytes,0,outBytes.length);
Assert.assertTrue(bigInteger.equals(out_bigInteger));
}
@Test
public void loopRandBytesFromBigInt() throws Exception {
//generate random bytes, turn them to trits and back
int byte_size = 48;
int trit_size = 243;
byte[] inBytes = new byte[byte_size];
int[] trits;
byte[] outBytes;
for (int i = 0; i<10_000; i++) {
seed.nextBytes(inBytes);
BigInteger in_bigInteger = Kerl.convertBytesToBigInt(inBytes,0,inBytes.length);
trits = Kerl.convertBigintToTrits(in_bigInteger, trit_size);
BigInteger out_bigInteger = Kerl.convertTritsToBigint(trits, 0, trit_size);
outBytes = Kerl.convertBigintToBytes(out_bigInteger,byte_size);
if(i % 1_000 == 0) {
System.out.println(String.format("%d iteration: %s",i, in_bigInteger ));
}
Assert.assertTrue(String.format("bigInt that failed: %s",in_bigInteger),Arrays.equals(inBytes,outBytes));
}
}
@Test
public void loopRandTritsFromBigInt() throws Exception {
//generate random bytes, turn them to trits and back
int byte_size = 48;
int trit_size = 243;
int[] inTrits;
byte[] bytes;
int[] outTrits;
for (int i = 0; i<10_000; i++) {
inTrits = getRandomTrits(trit_size);
inTrits[242] = 0;
BigInteger in_bigInteger = Kerl.convertTritsToBigint(inTrits, 0, trit_size);
bytes = Kerl.convertBigintToBytes(in_bigInteger,byte_size);
BigInteger out_bigInteger = Kerl.convertBytesToBigInt(bytes,0,bytes.length);
outTrits = Kerl.convertBigintToTrits(out_bigInteger, trit_size);
if(i % 1_000 == 0) {
System.out.println(String.format("%d iteration: %s",i, in_bigInteger ));
}
Assert.assertTrue(String.format("bigInt that failed: %s",in_bigInteger),Arrays.equals(inTrits,outTrits));
}
}
//@Test
public void generateBytesFromBigInt() throws Exception {
System.out.println("bigInteger,ByteArray");
for (int i = 0; i<100_000; i++) {
int byte_size = 48;
byte[] outBytes = new byte[byte_size];
seed.nextBytes(outBytes);
BigInteger out_bigInteger = new BigInteger(outBytes);
System.out.println(String.format("%s,%s", out_bigInteger, Arrays.toString(out_bigInteger.toByteArray())));
//Assert.assertTrue(bigInteger.equals(out_bigInteger));
}
}
@Test
public void kurlOneAbsorb() throws Exception {
int[] initial_value = trits("EMIDYNHBWMBCXVDEFOFWINXTERALUKYYPPHKP9JJFGJEIUY9MUDVNFZHMMWZUYUSWAIOWEVTHNWMHANBH");
Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
k.absorb(initial_value, 0, initial_value.length);
int[] hash_value = new int[Curl.HASH_LENGTH];
k.squeeze(hash_value, 0, hash_value.length);
String hash = trytes(hash_value);
Assert.assertEquals("EJEAOOZYSAWFPZQESYDHZCGYNSTWXUMVJOVDWUNZJXDGWCLUFGIMZRMGCAZGKNPLBRLGUNYWKLJTYEAQX", hash);
}
@Test
public void kurlMultiSqueeze() throws Exception {
int[] initial_value = trits("9MIDYNHBWMBCXVDEFOFWINXTERALUKYYPPHKP9JJFGJEIUY9MUDVNFZHMMWZUYUSWAIOWEVTHNWMHANBH");
Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
k.absorb(initial_value, 0, initial_value.length);
int[] hash_value = new int[Curl.HASH_LENGTH * 2];
k.squeeze(hash_value, 0, hash_value.length);
String hash = trytes(hash_value);
Assert.assertEquals("G9JYBOMPUXHYHKSNRNMMSSZCSHOFYOYNZRSZMAAYWDYEIMVVOGKPJBVBM9TDPULSFUNMTVXRKFIDOHUXXVYDLFSZYZTWQYTE9SPYYWYTXJYQ9IFGYOLZXWZBKWZN9QOOTBQMWMUBLEWUEEASRHRTNIQWJQNDWRYLCA", hash);
}
@Test
public void kurlMultiAbsorbMultiSqueeze() throws Exception {
int[] initial_value = trits("G9JYBOMPUXHYHKSNRNMMSSZCSHOFYOYNZRSZMAAYWDYEIMVVOGKPJBVBM9TDPULSFUNMTVXRKFIDOHUXXVYDLFSZYZTWQYTE9SPYYWYTXJYQ9IFGYOLZXWZBKWZN9QOOTBQMWMUBLEWUEEASRHRTNIQWJQNDWRYLCA");
Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
k.absorb(initial_value, 0, initial_value.length);
int[] hash_value = new int[Curl.HASH_LENGTH * 2];
k.squeeze(hash_value, 0, hash_value.length);
String hash = trytes(hash_value);
Assert.assertEquals("LUCKQVACOGBFYSPPVSSOXJEKNSQQRQKPZC9NXFSMQNRQCGGUL9OHVVKBDSKEQEBKXRNUJSRXYVHJTXBPDWQGNSCDCBAIRHAQCOWZEBSNHIJIGPZQITIBJQ9LNTDIBTCQ9EUWKHFLGFUVGGUWJONK9GBCDUIMAYMMQX", hash);
}
public static int[] getRandomTrits(int length) {
return Arrays.stream(new int[length]).map(i -> seed.nextInt(3)-1).toArray();
}
// public static int[] getRandomTransactionTrits() {
// return new Hash(getRandomTrits(Hash.SIZE_IN_TRITS));
// }
//
// //@Test
// public void generateTrytesAndHashes() throws Exception {
// System.out.println("trytes,Kerl_hash");
// for (int i = 0; i< 10000 ; i++) {
// Hash trytes = getRandomTransactionHash();
// int[] initial_value = trytes.trits();
// Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
// k.absorb(initial_value, 0, initial_value.length);
// int[] hash_value = new int[Curl.HASH_LENGTH];
// k.squeeze(hash_value, 0, hash_value.length);
// String hash = trytes(hash_value);
// System.out.println(String.format("%s,%s",trytes,hash));
// }
// }
//
// //@Test
// public void generateTrytesAndMultiSqueeze() throws Exception {
// System.out.println("trytes,Kerl_squeeze1,Kerl_squeeze2,Kerl_squeeze3");
// for (int i = 0; i< 10000 ; i++) {
// Hash trytes = getRandomTransactionHash();
// int[] initial_value = trytes.trits();
// Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
// k.absorb(initial_value, 0, initial_value.length);
// int[] hash_value = new int[Curl.HASH_LENGTH];
// k.squeeze(hash_value, 0, hash_value.length);
// String hash1 = trytes(hash_value);
// k.squeeze(hash_value, 0, hash_value.length);
// String hash2 = trytes(hash_value);
// k.squeeze(hash_value, 0, hash_value.length);
// String hash3 = trytes(hash_value);
// System.out.println(String.format("%s,%s,%s,%s",trytes,hash1,hash2,hash3));
// }
// }
//
// //@Test
// public void generateMultiTrytesAndHash() throws Exception {
// System.out.println("multiTrytes,Kerl_hash");
// for (int i = 0; i< 10000 ; i++) {
// String multi = String.format("%s%s%s",getRandomTransactionHash(),getRandomTransactionHash(),getRandomTransactionHash());
// int[] initial_value = trits(multi);
// Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
// k.absorb(initial_value, 0, initial_value.length);
// int[] hash_value = new int[Curl.HASH_LENGTH];
// k.squeeze(hash_value, 0, hash_value.length);
// String hash = trytes(hash_value);
// System.out.println(String.format("%s,%s",multi,hash));
// }
// }
//
//
// //@Test
// public void generateHashes() throws Exception {
// //System.out.println("trytes,Kerl_hash");
// for (int i = 0; i< 1_000_000 ; i++) {
// Hash trytes = getRandomTransactionHash();
// int[] initial_value = trytes.trits();
// Curl k = SpongeFactory.create(SpongeFactory.Mode.KERL);
// k.absorb(initial_value, 0, initial_value.length);
// int[] hash_value = new int[Curl.HASH_LENGTH];
// k.squeeze(hash_value, 0, hash_value.length);
// String hash = trytes(hash_value);
// //System.out.println(String.format("%s,%s",trytes,hash));
// System.out.println(String.format("%s",hash));
// }
// }
//Conversion helper function
public static final int RADIX = 3;
public static final int MAX_TRIT_VALUE = (RADIX - 1) / 2, MIN_TRIT_VALUE = -MAX_TRIT_VALUE;
public static final int NUMBER_OF_TRITS_IN_A_BYTE = 5;
public static final int NUMBER_OF_TRITS_IN_A_TRYTE = 3;
static final int[][] BYTE_TO_TRITS_MAPPINGS = new int[243][];
static final int[][] TRYTE_TO_TRITS_MAPPINGS = new int[27][];
public static final String TRYTE_ALPHABET = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static {
final int[] trits = new int[NUMBER_OF_TRITS_IN_A_BYTE];
for (int i = 0; i < 243; i++) {
BYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_BYTE);
increment(trits, NUMBER_OF_TRITS_IN_A_BYTE);
}
for (int i = 0; i < 27; i++) {
TRYTE_TO_TRITS_MAPPINGS[i] = Arrays.copyOf(trits, NUMBER_OF_TRITS_IN_A_TRYTE);
increment(trits, NUMBER_OF_TRITS_IN_A_TRYTE);
}
}
private static void increment(final int[] trits, final int size) {
for (int i = 0; i < size; i++) {
if (++trits[i] > MAX_TRIT_VALUE) {
trits[i] = MIN_TRIT_VALUE;
} else {
break;
}
}
}
public static int[] trits(final String trytes) {
final int[] trits = new int[trytes.length() * NUMBER_OF_TRITS_IN_A_TRYTE];
for (int i = 0; i < trytes.length(); i++) {
System.arraycopy(TRYTE_TO_TRITS_MAPPINGS[TRYTE_ALPHABET.indexOf(trytes.charAt(i))], 0, trits, i * NUMBER_OF_TRITS_IN_A_TRYTE, NUMBER_OF_TRITS_IN_A_TRYTE);
}
return trits;
}
public static String trytes(final int[] trits, final int offset, final int size) {
final StringBuilder trytes = new StringBuilder();
for (int i = 0; i < (size + NUMBER_OF_TRITS_IN_A_TRYTE - 1) / NUMBER_OF_TRITS_IN_A_TRYTE; i++) {
int j = trits[offset + i * 3] + trits[offset + i * 3 + 1] * 3 + trits[offset + i * 3 + 2] * 9;
if (j < 0) {
j += TRYTE_ALPHABET.length();
}
trytes.append(TRYTE_ALPHABET.charAt(j));
}
return trytes.toString();
}
public static String trytes(final int[] trits) {
return trytes(trits, 0, trits.length);
}
}