76
76
public final class Blake3 {
77
77
78
78
private static final class ChunkState {
79
+
79
80
private int [] chainingValue ;
80
81
private final long chunkCounter ;
81
82
private final int flags ;
@@ -209,20 +210,20 @@ private void reset() {
209
210
state = new ChunkState (key , 0 , flags );
210
211
}
211
212
}
213
+
212
214
/**
213
215
* Represents the state just prior to either producing an eight word chaining value or any number of output bytes
214
216
* when the ROOT flag is set.
215
217
*/
216
218
private static final class Output {
219
+
217
220
private final int [] inputChainingValue ;
218
221
private final int [] blockWords ;
219
222
private final long counter ;
220
223
private final int blockLength ;
221
224
private final int flags ;
222
225
223
- private Output (
224
- final int [] inputChainingValue , final int [] blockWords , final long counter , final int blockLength ,
225
- final int flags ) {
226
+ private Output (final int [] inputChainingValue , final int [] blockWords , final long counter , final int blockLength , final int flags ) {
226
227
this .inputChainingValue = inputChainingValue ;
227
228
this .blockWords = blockWords ;
228
229
this .counter = counter ;
@@ -231,17 +232,15 @@ private Output(
231
232
}
232
233
233
234
private int [] chainingValue () {
234
- return Arrays
235
- .copyOf (compress (inputChainingValue , blockWords , blockLength , counter , flags ), CHAINING_VALUE_INTS );
235
+ return Arrays .copyOf (compress (inputChainingValue , blockWords , blockLength , counter , flags ), CHAINING_VALUE_INTS );
236
236
}
237
237
238
238
private void rootOutputBytes (final byte [] out , int offset , int length ) {
239
239
int outputBlockCounter = 0 ;
240
240
while (length > 0 ) {
241
241
int chunkLength = Math .min (OUT_LEN * 2 , length );
242
242
length -= chunkLength ;
243
- final int [] words =
244
- compress (inputChainingValue , blockWords , blockLength , outputBlockCounter ++, flags | ROOT );
243
+ final int [] words = compress (inputChainingValue , blockWords , blockLength , outputBlockCounter ++, flags | ROOT );
245
244
int wordCounter = 0 ;
246
245
while (chunkLength > 0 ) {
247
246
final int wordLength = Math .min (Integer .BYTES , chunkLength );
@@ -252,35 +251,33 @@ private void rootOutputBytes(final byte[] out, int offset, int length) {
252
251
}
253
252
}
254
253
}
254
+
255
255
private static final int BLOCK_LEN = 64 ;
256
256
private static final int BLOCK_INTS = BLOCK_LEN / Integer .BYTES ;
257
257
private static final int KEY_LEN = 32 ;
258
258
private static final int KEY_INTS = KEY_LEN / Integer .BYTES ;
259
-
260
259
private static final int OUT_LEN = 32 ;
261
-
262
260
private static final int CHUNK_LEN = 1024 ;
263
261
private static final int CHAINING_VALUE_INTS = 8 ;
262
+
264
263
/**
265
264
* Standard hash key used for plain hashes; same initialization vector as Blake2s.
266
265
*/
267
- private static final int [] IV =
268
- { 0x6A09E667 , 0xBB67AE85 , 0x3C6EF372 , 0xA54FF53A , 0x510E527F , 0x9B05688C , 0x1F83D9AB , 0x5BE0CD19 };
266
+ private static final int [] IV = { 0x6A09E667 , 0xBB67AE85 , 0x3C6EF372 , 0xA54FF53A , 0x510E527F , 0x9B05688C , 0x1F83D9AB , 0x5BE0CD19 };
267
+
269
268
// domain flags
270
269
private static final int CHUNK_START = 1 ;
271
270
private static final int CHUNK_END = 1 << 1 ;
272
271
private static final int PARENT = 1 << 2 ;
273
272
private static final int ROOT = 1 << 3 ;
274
-
275
273
private static final int KEYED_HASH = 1 << 4 ;
276
-
277
274
private static final int DERIVE_KEY_CONTEXT = 1 << 5 ;
278
-
279
275
private static final int DERIVE_KEY_MATERIAL = 1 << 6 ;
280
276
281
277
/**
282
278
* Pre-permuted for all 7 rounds; the second row (2,6,3,...) indicates the base permutation.
283
279
*/
280
+ // @formatter:off
284
281
private static final byte [][] MSG_SCHEDULE = {
285
282
{ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 },
286
283
{ 2 , 6 , 3 , 10 , 7 , 0 , 4 , 13 , 1 , 11 , 12 , 5 , 9 , 14 , 15 , 8 },
@@ -290,6 +287,7 @@ private void rootOutputBytes(final byte[] out, int offset, int length) {
290
287
{ 9 , 14 , 11 , 5 , 8 , 12 , 15 , 1 , 13 , 3 , 0 , 10 , 2 , 6 , 4 , 7 },
291
288
{ 11 , 15 , 5 , 0 , 1 , 9 , 8 , 6 , 14 , 10 , 2 , 12 , 3 , 4 , 7 , 13 }
292
289
};
290
+ // @formatter:on
293
291
294
292
private static void checkBufferArgs (final byte [] buffer , final int offset , final int length ) {
295
293
Objects .requireNonNull (buffer );
@@ -301,14 +299,11 @@ private static void checkBufferArgs(final byte[] buffer, final int offset, final
301
299
}
302
300
final int bufferLength = buffer .length ;
303
301
if (offset > bufferLength - length ) {
304
- throw new IndexOutOfBoundsException (
305
- "Offset " + offset + " and length " + length + " out of bounds with buffer length " + bufferLength );
302
+ throw new IndexOutOfBoundsException ("Offset " + offset + " and length " + length + " out of bounds with buffer length " + bufferLength );
306
303
}
307
304
}
308
305
309
- private static int [] compress (
310
- final int [] chainingValue , final int [] blockWords , final int blockLength , final long counter ,
311
- final int flags ) {
306
+ private static int [] compress (final int [] chainingValue , final int [] blockWords , final int blockLength , final long counter , final int flags ) {
312
307
final int [] state = Arrays .copyOf (chainingValue , BLOCK_INTS );
313
308
System .arraycopy (IV , 0 , state , 8 , 4 );
314
309
state [12 ] = (int ) counter ;
@@ -329,8 +324,7 @@ private static int[] compress(
329
324
/**
330
325
* The mixing function, G, which mixes either a column or a diagonal.
331
326
*/
332
- private static void g (
333
- final int [] state , final int a , final int b , final int c , final int d , final int mx , final int my ) {
327
+ private static void g (final int [] state , final int a , final int b , final int c , final int d , final int mx , final int my ) {
334
328
state [a ] += state [b ] + mx ;
335
329
state [d ] = Integer .rotateRight (state [d ] ^ state [a ], 16 );
336
330
state [c ] += state [d ];
@@ -414,13 +408,11 @@ private static void packInt(final int value, final byte[] dst, final int off, fi
414
408
}
415
409
}
416
410
417
- private static int [] parentChainingValue (
418
- final int [] leftChildCV , final int [] rightChildCV , final int [] key , final int flags ) {
411
+ private static int [] parentChainingValue (final int [] leftChildCV , final int [] rightChildCV , final int [] key , final int flags ) {
419
412
return parentOutput (leftChildCV , rightChildCV , key , flags ).chainingValue ();
420
413
}
421
414
422
- private static Output parentOutput (
423
- final int [] leftChildCV , final int [] rightChildCV , final int [] key , final int flags ) {
415
+ private static Output parentOutput (final int [] leftChildCV , final int [] rightChildCV , final int [] key , final int flags ) {
424
416
final int [] blockWords = Arrays .copyOf (leftChildCV , BLOCK_INTS );
425
417
System .arraycopy (rightChildCV , 0 , blockWords , 8 , CHAINING_VALUE_INTS );
426
418
return new Output (key .clone (), blockWords , 0 , BLOCK_LEN , flags | PARENT );
0 commit comments