Skip to content

Commit a05cc6b

Browse files
winne42dghgit
authored andcommitted
Blake3Digest compression now works in local variables, roughly 2.8x faster, relates to github #2471.
1 parent b64c278 commit a05cc6b

3 files changed

Lines changed: 175 additions & 88 deletions

File tree

‎CONTRIBUTORS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,4 @@ We also wish to acknowledge financial and collaborative support from [CISCO](htt
553553
- Carsten Hammer \<https://github.com/carstenartur\> - deriving the c6 and c7 constants of the generic RFC 9380 sqrt_ratio calculator from a shared z^c3, saving a modular exponentiation per instance, with tests comparing the stored constants against the direct powers (PR #2455).
554554
- pyj kor \<pyjkor&#064;gmail.com\> - report of the RFC 5990 RSA-KTS CMS recipient deriving to the keyLength declared in the message rather than the length the key-wrapping algorithm of its data encapsulation mechanism fixes, with the crafted message and the reproduction that showed what the declared length was worth.
555555
- Will Childs-Klein \<https://github.com/WillChilds-Klein\> - initial implementation of ML-KEM key conversion for BCJSSE when another provider supplies the keys, with the diagnosis and reproduction of the resulting handshake failure (PR #2466).
556+
- Winfried Gerlach \<https://github.com/winne42\> - restructured Blake3Digest compression to work in local variables, with the differential testing and benchmarks behind it (PR #2472).

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

Lines changed: 172 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ public class Blake3Digest
160160
*/
161161
private static final int FLAGS = 15;
162162

163-
/**
164-
* Message word permutations.
165-
*/
166-
private static final byte[] SIGMA = {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8};
167-
168163
/**
169164
* Blake3 Initialization Vector.
170165
*/
@@ -197,11 +192,6 @@ public class Blake3Digest
197192
*/
198193
private final int[] theM = new int[NUMWORDS << 1];
199194

200-
/**
201-
* The indices.
202-
*/
203-
private final byte[] theIndices = new byte[NUMWORDS << 1];
204-
205195
/**
206196
* The chainingStack.
207197
*/
@@ -668,40 +658,185 @@ private void processStack()
668658
}
669659

670660
/**
671-
* Perform compression.
661+
* Perform compression: the seven rounds of the BLAKE3 compression function over theV, with the message words in
662+
* theM, followed by the output transformation.
663+
* <p>
664+
* The state and message are worked on in local variables, with the fixed message permutation applied to them
665+
* between rounds, so that the JIT can keep them in registers; theV receives the final state and theM is left
666+
* unchanged.
667+
* </p>
672668
*/
673669
private void compress()
674670
{
675-
/* Initialise the buffers */
676-
initIndices();
677-
678-
/* Loop through the rounds */
679-
for (int round = 0; round < ROUNDS - 1; round++)
671+
final int[] v = theV;
672+
final int[] m = theM;
673+
674+
int v0 = v[0];
675+
int v1 = v[1];
676+
int v2 = v[2];
677+
int v3 = v[3];
678+
int v4 = v[4];
679+
int v5 = v[5];
680+
int v6 = v[6];
681+
int v7 = v[7];
682+
int v8 = v[8];
683+
int v9 = v[9];
684+
int v10 = v[10];
685+
int v11 = v[11];
686+
int v12 = v[12];
687+
int v13 = v[13];
688+
int v14 = v[14];
689+
int v15 = v[15];
690+
691+
int m0 = m[0];
692+
int m1 = m[1];
693+
int m2 = m[2];
694+
int m3 = m[3];
695+
int m4 = m[4];
696+
int m5 = m[5];
697+
int m6 = m[6];
698+
int m7 = m[7];
699+
int m8 = m[8];
700+
int m9 = m[9];
701+
int m10 = m[10];
702+
int m11 = m[11];
703+
int m12 = m[12];
704+
int m13 = m[13];
705+
int m14 = m[14];
706+
int m15 = m[15];
707+
708+
for (int round = 0; round < ROUNDS; round++)
680709
{
681-
/* Perform the round and permuteM */
682-
performRound();
683-
permuteIndices();
710+
/* Apply to columns of V */
711+
v0 += v4 + m0;
712+
v12 = Integers.rotateRight(v12 ^ v0, 16);
713+
v8 += v12;
714+
v4 = Integers.rotateRight(v4 ^ v8, 12);
715+
v0 += v4 + m1;
716+
v12 = Integers.rotateRight(v12 ^ v0, 8);
717+
v8 += v12;
718+
v4 = Integers.rotateRight(v4 ^ v8, 7);
719+
720+
v1 += v5 + m2;
721+
v13 = Integers.rotateRight(v13 ^ v1, 16);
722+
v9 += v13;
723+
v5 = Integers.rotateRight(v5 ^ v9, 12);
724+
v1 += v5 + m3;
725+
v13 = Integers.rotateRight(v13 ^ v1, 8);
726+
v9 += v13;
727+
v5 = Integers.rotateRight(v5 ^ v9, 7);
728+
729+
v2 += v6 + m4;
730+
v14 = Integers.rotateRight(v14 ^ v2, 16);
731+
v10 += v14;
732+
v6 = Integers.rotateRight(v6 ^ v10, 12);
733+
v2 += v6 + m5;
734+
v14 = Integers.rotateRight(v14 ^ v2, 8);
735+
v10 += v14;
736+
v6 = Integers.rotateRight(v6 ^ v10, 7);
737+
738+
v3 += v7 + m6;
739+
v15 = Integers.rotateRight(v15 ^ v3, 16);
740+
v11 += v15;
741+
v7 = Integers.rotateRight(v7 ^ v11, 12);
742+
v3 += v7 + m7;
743+
v15 = Integers.rotateRight(v15 ^ v3, 8);
744+
v11 += v15;
745+
v7 = Integers.rotateRight(v7 ^ v11, 7);
746+
747+
/* Apply to diagonals of V */
748+
v0 += v5 + m8;
749+
v15 = Integers.rotateRight(v15 ^ v0, 16);
750+
v10 += v15;
751+
v5 = Integers.rotateRight(v5 ^ v10, 12);
752+
v0 += v5 + m9;
753+
v15 = Integers.rotateRight(v15 ^ v0, 8);
754+
v10 += v15;
755+
v5 = Integers.rotateRight(v5 ^ v10, 7);
756+
757+
v1 += v6 + m10;
758+
v12 = Integers.rotateRight(v12 ^ v1, 16);
759+
v11 += v12;
760+
v6 = Integers.rotateRight(v6 ^ v11, 12);
761+
v1 += v6 + m11;
762+
v12 = Integers.rotateRight(v12 ^ v1, 8);
763+
v11 += v12;
764+
v6 = Integers.rotateRight(v6 ^ v11, 7);
765+
766+
v2 += v7 + m12;
767+
v13 = Integers.rotateRight(v13 ^ v2, 16);
768+
v8 += v13;
769+
v7 = Integers.rotateRight(v7 ^ v8, 12);
770+
v2 += v7 + m13;
771+
v13 = Integers.rotateRight(v13 ^ v2, 8);
772+
v8 += v13;
773+
v7 = Integers.rotateRight(v7 ^ v8, 7);
774+
775+
v3 += v4 + m14;
776+
v14 = Integers.rotateRight(v14 ^ v3, 16);
777+
v9 += v14;
778+
v4 = Integers.rotateRight(v4 ^ v9, 12);
779+
v3 += v4 + m15;
780+
v14 = Integers.rotateRight(v14 ^ v3, 8);
781+
v9 += v14;
782+
v4 = Integers.rotateRight(v4 ^ v9, 7);
783+
784+
/*
785+
* Permute the message words for the next round: m'[i] = m[SIGMA[i]], with the BLAKE3 message
786+
* permutation SIGMA = {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8}.
787+
*/
788+
final int t0 = m0;
789+
final int t1 = m1;
790+
final int t2 = m2;
791+
final int t3 = m3;
792+
final int t4 = m4;
793+
final int t5 = m5;
794+
final int t6 = m6;
795+
final int t7 = m7;
796+
final int t8 = m8;
797+
final int t9 = m9;
798+
final int t10 = m10;
799+
final int t11 = m11;
800+
final int t12 = m12;
801+
final int t13 = m13;
802+
final int t14 = m14;
803+
final int t15 = m15;
804+
m0 = t2;
805+
m1 = t6;
806+
m2 = t3;
807+
m3 = t10;
808+
m4 = t7;
809+
m5 = t0;
810+
m6 = t4;
811+
m7 = t13;
812+
m8 = t1;
813+
m9 = t11;
814+
m10 = t12;
815+
m11 = t5;
816+
m12 = t9;
817+
m13 = t14;
818+
m14 = t15;
819+
m15 = t8;
684820
}
685-
performRound();
686-
adjustChaining();
687-
}
688821

689-
/**
690-
* Perform a round.
691-
*/
692-
private void performRound()
693-
{
694-
/* Apply to columns of V */
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);
822+
v[0] = v0;
823+
v[1] = v1;
824+
v[2] = v2;
825+
v[3] = v3;
826+
v[4] = v4;
827+
v[5] = v5;
828+
v[6] = v6;
829+
v[7] = v7;
830+
v[8] = v8;
831+
v[9] = v9;
832+
v[10] = v10;
833+
v[11] = v11;
834+
v[12] = v12;
835+
v[13] = v13;
836+
v[14] = v14;
837+
v[15] = v15;
699838

700-
/* Apply to diagonals of V */
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);
839+
adjustChaining();
705840
}
706841

707842
/**
@@ -748,57 +883,6 @@ private void adjustChaining()
748883
}
749884
}
750885

751-
/**
752-
* Mix function G.
753-
*
754-
* @param msgIdx the message index
755-
* @param posA position A in V
756-
* @param posB position B in V
757-
* @param posC position C in V
758-
* @param posD poistion D in V
759-
*/
760-
private void mixG(final int msgIdx,
761-
final int posA,
762-
final int posB,
763-
final int posC,
764-
final int posD)
765-
{
766-
/* Determine indices */
767-
int msg = msgIdx << 1;
768-
769-
/* Perform the Round */
770-
theV[posA] += theV[posB] + theM[theIndices[msg++]];
771-
theV[posD] = Integers.rotateRight(theV[posD] ^ theV[posA], 16);
772-
theV[posC] += theV[posD];
773-
theV[posB] = Integers.rotateRight(theV[posB] ^ theV[posC], 12);
774-
theV[posA] += theV[posB] + theM[theIndices[msg]];
775-
theV[posD] = Integers.rotateRight(theV[posD] ^ theV[posA], 8);
776-
theV[posC] += theV[posD];
777-
theV[posB] = Integers.rotateRight(theV[posB] ^ theV[posC], 7);
778-
}
779-
780-
/**
781-
* initialise the indices.
782-
*/
783-
private void initIndices()
784-
{
785-
for (byte i = 0; i < theIndices.length; i++)
786-
{
787-
theIndices[i] = i;
788-
}
789-
}
790-
791-
/**
792-
* PermuteIndices.
793-
*/
794-
private void permuteIndices()
795-
{
796-
for (byte i = 0; i < theIndices.length; i++)
797-
{
798-
theIndices[i] = SIGMA[theIndices[i]];
799-
}
800-
}
801-
802886
/**
803887
* Initialise null key.
804888
*/

‎docs/releasenotes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Date: 2026, TBD
9898

9999
- The BCJSSE provider adds an org.bouncycastle.jsse.BCSSLContext interface exposing extended functionality of its SSLContext, obtained with org.bouncycastle.jsse.util.ContextUtil.getBCSSLContext() by way of the new BCSSLSessionContext interface the context's session contexts implement. Its getDefaultParameters(boolean) and getSupportedParameters(boolean) return the context's default and supported parameters as a BCSSLParameters for either client or server mode, including the BC-specific properties, where SSLContext.getDefaultSSLParameters() and getSupportedSSLParameters() report client mode only and cannot carry those properties. A BCSSLContext describes the initialization of the SSLContext it was obtained from, and is not updated if the SSLContext is re-initialized.
100100

101+
- Blake3Digest (and with it Blake3Mac and the provider's BLAKE3 digests) now runs its compression function in local variables rather than through its state arrays, making it roughly 2.7 to 2.9 times faster. The output is unchanged (github #2471).
102+
101103
- The BC provider's ML-DSA, ML-KEM and SLH-DSA keys now implement getParams(), which keys inherit from java.security.AsymmetricKey from Java 22, returning a java.security.spec.NamedParameterSpec naming the parameter set (for example "ML-DSA-65") on Java 11 and later, and null before. The JDK's own ML-DSA and ML-KEM keys return only the family name, "ML-DSA" or "ML-KEM", from getAlgorithm() and report the parameter set this way, so comparing getParams() rather than getAlgorithm() now identifies the parameter set whichever provider made the key. The BCPQC provider's keys do the same for every family with named parameter sets - AIMer, BIKE, FAEST, Falcon, HAETAE, HQC, MAYO, MQOM, NTRU, NTRU+, NTRU LPRime, Streamlined NTRU Prime, QR-UOV, SABER, SDitH, SMAUG-T, SNOVA, SQIsign and UOV - with the name their ParameterSpec reports, which the family's KeyPairGenerator accepts back. getAlgorithm() on BC and BCPQC keys is unchanged (github #2467).
102104

103105
- The provider adds ECDHwithSHA256HKDF, ECDHwithSHA384HKDF and ECDHwithSHA512HKDF KeyAgreement algorithms: ECDH followed by HKDF (RFC 5869), the counterparts for elliptic curve keys of the XDHwithSHA\*HKDF agreements used for the RFC 8418 dhSinglePass-stdDH-hkdf schemes. As with those, a UserKeyingMaterialSpec supplies the HKDF info and, optionally, its salt. They are registered by name only; the RFC 8418 scheme object identifiers remain registered to the XDH agreements.

0 commit comments

Comments
 (0)