Skip to content

Commit f5cbf1e

Browse files
committed
Blake3 refactoring
1 parent bb05c6c commit f5cbf1e

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

core/src/main/java/org/bouncycastle/asn1/misc/MiscObjectIdentifiers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public interface MiscObjectIdentifiers
138138
ASN1ObjectIdentifier id_blake2s224 = blake2.branch("2.7");
139139
ASN1ObjectIdentifier id_blake2s256 = blake2.branch("2.8");
140140

141-
ASN1ObjectIdentifier blake3 = new ASN1ObjectIdentifier("1.3.6.1.4.1.1722.12.2.3");
141+
ASN1ObjectIdentifier blake3 = blake2.branch("3");
142142

143143
ASN1ObjectIdentifier blake3_256 = blake3.branch("8");
144144

core/src/main/java/org/bouncycastle/crypto/digests/Blake3Digest.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ public class Blake3Digest
164164
*/
165165
private static final byte[] SIGMA = {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8};
166166

167-
/**
168-
* Rotation constants.
169-
*/
170-
private static final byte[] ROTATE = {16, 12, 8, 7};
171-
172167
/**
173168
* Blake3 Initialization Vector.
174169
*/
@@ -308,7 +303,7 @@ public Blake3Digest(final Blake3Digest pSource)
308303
purpose = pSource.purpose;
309304

310305
/* Initialise from source */
311-
reset((Memoable)pSource);
306+
reset(pSource);
312307
}
313308

314309
public int getByteLength()
@@ -422,7 +417,7 @@ public void update(final byte[] pMessage,
422417
/* If there is sufficient space in the buffer */
423418
if (remainingLen >= pLen)
424419
{
425-
/* Copy data into byffer and return */
420+
/* Copy data into buffer and return */
426421
System.arraycopy(pMessage, pOffset, theBuffer, thePos, pLen);
427422
thePos += pLen;
428423
return;
@@ -697,17 +692,16 @@ private void compress()
697692
private void performRound()
698693
{
699694
/* Apply to columns of V */
700-
int idx = 0;
701-
mixG(idx++, CHAINING0, CHAINING4, IV0, COUNT0);
702-
mixG(idx++, CHAINING1, CHAINING5, IV1, COUNT1);
703-
mixG(idx++, CHAINING2, CHAINING6, IV2, DATALEN);
704-
mixG(idx++, CHAINING3, CHAINING7, IV3, FLAGS);
695+
mixG(0, CHAINING0, CHAINING4, IV0, COUNT0);
696+
mixG(1, CHAINING1, CHAINING5, IV1, COUNT1);
697+
mixG(2, CHAINING2, CHAINING6, IV2, DATALEN);
698+
mixG(3, CHAINING3, CHAINING7, IV3, FLAGS);
705699

706700
/* Apply to diagonals of V */
707-
mixG(idx++, CHAINING0, CHAINING5, IV2, FLAGS);
708-
mixG(idx++, CHAINING1, CHAINING6, IV3, COUNT0);
709-
mixG(idx++, CHAINING2, CHAINING7, IV0, COUNT1);
710-
mixG(idx, CHAINING3, CHAINING4, IV1, DATALEN);
701+
mixG(4, CHAINING0, CHAINING5, IV2, FLAGS);
702+
mixG(5, CHAINING1, CHAINING6, IV3, COUNT0);
703+
mixG(6, CHAINING2, CHAINING7, IV0, COUNT1);
704+
mixG(7, CHAINING3, CHAINING4, IV1, DATALEN);
711705
}
712706

713707
/**
@@ -771,17 +765,16 @@ private void mixG(final int msgIdx,
771765
{
772766
/* Determine indices */
773767
int msg = msgIdx << 1;
774-
int rot = 0;
775768

776769
/* Perform the Round */
777770
theV[posA] += theV[posB] + theM[theIndices[msg++]];
778-
theV[posD] = Integers.rotateRight(theV[posD] ^ theV[posA], ROTATE[rot++]);
771+
theV[posD] = Integers.rotateRight(theV[posD] ^ theV[posA], 16);
779772
theV[posC] += theV[posD];
780-
theV[posB] = Integers.rotateRight(theV[posB] ^ theV[posC], ROTATE[rot++]);
773+
theV[posB] = Integers.rotateRight(theV[posB] ^ theV[posC], 12);
781774
theV[posA] += theV[posB] + theM[theIndices[msg]];
782-
theV[posD] = Integers.rotateRight(theV[posD] ^ theV[posA], ROTATE[rot++]);
775+
theV[posD] = Integers.rotateRight(theV[posD] ^ theV[posA], 8);
783776
theV[posC] += theV[posD];
784-
theV[posB] = Integers.rotateRight(theV[posB] ^ theV[posC], ROTATE[rot]);
777+
theV[posB] = Integers.rotateRight(theV[posB] ^ theV[posC], 7);
785778
}
786779

787780
/**

0 commit comments

Comments
 (0)