7676public final class Blake3 {
7777
7878 private static final class ChunkState {
79+
7980 private int [] chainingValue ;
8081 private final long chunkCounter ;
8182 private final int flags ;
@@ -209,20 +210,20 @@ private void reset() {
209210 state = new ChunkState (key , 0 , flags );
210211 }
211212 }
213+
212214 /**
213215 * Represents the state just prior to either producing an eight word chaining value or any number of output bytes
214216 * when the ROOT flag is set.
215217 */
216218 private static final class Output {
219+
217220 private final int [] inputChainingValue ;
218221 private final int [] blockWords ;
219222 private final long counter ;
220223 private final int blockLength ;
221224 private final int flags ;
222225
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 ) {
226227 this .inputChainingValue = inputChainingValue ;
227228 this .blockWords = blockWords ;
228229 this .counter = counter ;
@@ -231,17 +232,15 @@ private Output(
231232 }
232233
233234 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 );
236236 }
237237
238238 private void rootOutputBytes (final byte [] out , int offset , int length ) {
239239 int outputBlockCounter = 0 ;
240240 while (length > 0 ) {
241241 int chunkLength = Math .min (OUT_LEN * 2 , length );
242242 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 );
245244 int wordCounter = 0 ;
246245 while (chunkLength > 0 ) {
247246 final int wordLength = Math .min (Integer .BYTES , chunkLength );
@@ -252,35 +251,33 @@ private void rootOutputBytes(final byte[] out, int offset, int length) {
252251 }
253252 }
254253 }
254+
255255 private static final int BLOCK_LEN = 64 ;
256256 private static final int BLOCK_INTS = BLOCK_LEN / Integer .BYTES ;
257257 private static final int KEY_LEN = 32 ;
258258 private static final int KEY_INTS = KEY_LEN / Integer .BYTES ;
259-
260259 private static final int OUT_LEN = 32 ;
261-
262260 private static final int CHUNK_LEN = 1024 ;
263261 private static final int CHAINING_VALUE_INTS = 8 ;
262+
264263 /**
265264 * Standard hash key used for plain hashes; same initialization vector as Blake2s.
266265 */
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+
269268 // domain flags
270269 private static final int CHUNK_START = 1 ;
271270 private static final int CHUNK_END = 1 << 1 ;
272271 private static final int PARENT = 1 << 2 ;
273272 private static final int ROOT = 1 << 3 ;
274-
275273 private static final int KEYED_HASH = 1 << 4 ;
276-
277274 private static final int DERIVE_KEY_CONTEXT = 1 << 5 ;
278-
279275 private static final int DERIVE_KEY_MATERIAL = 1 << 6 ;
280276
281277 /**
282278 * Pre-permuted for all 7 rounds; the second row (2,6,3,...) indicates the base permutation.
283279 */
280+ // @formatter:off
284281 private static final byte [][] MSG_SCHEDULE = {
285282 { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 },
286283 { 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) {
290287 { 9 , 14 , 11 , 5 , 8 , 12 , 15 , 1 , 13 , 3 , 0 , 10 , 2 , 6 , 4 , 7 },
291288 { 11 , 15 , 5 , 0 , 1 , 9 , 8 , 6 , 14 , 10 , 2 , 12 , 3 , 4 , 7 , 13 }
292289 };
290+ // @formatter:on
293291
294292 private static void checkBufferArgs (final byte [] buffer , final int offset , final int length ) {
295293 Objects .requireNonNull (buffer );
@@ -301,14 +299,11 @@ private static void checkBufferArgs(final byte[] buffer, final int offset, final
301299 }
302300 final int bufferLength = buffer .length ;
303301 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 );
306303 }
307304 }
308305
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 ) {
312307 final int [] state = Arrays .copyOf (chainingValue , BLOCK_INTS );
313308 System .arraycopy (IV , 0 , state , 8 , 4 );
314309 state [12 ] = (int ) counter ;
@@ -329,8 +324,7 @@ private static int[] compress(
329324 /**
330325 * The mixing function, G, which mixes either a column or a diagonal.
331326 */
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 ) {
334328 state [a ] += state [b ] + mx ;
335329 state [d ] = Integer .rotateRight (state [d ] ^ state [a ], 16 );
336330 state [c ] += state [d ];
@@ -414,13 +408,11 @@ private static void packInt(final int value, final byte[] dst, final int off, fi
414408 }
415409 }
416410
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 ) {
419412 return parentOutput (leftChildCV , rightChildCV , key , flags ).chainingValue ();
420413 }
421414
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 ) {
424416 final int [] blockWords = Arrays .copyOf (leftChildCV , BLOCK_INTS );
425417 System .arraycopy (rightChildCV , 0 , blockWords , 8 , CHAINING_VALUE_INTS );
426418 return new Output (key .clone (), blockWords , 0 , BLOCK_LEN , flags | PARENT );
0 commit comments