Skip to content

Commit 878b86b

Browse files
Remove clutter and pointless overhead
1 parent 46206f9 commit 878b86b

File tree

11 files changed

+50
-938
lines changed

11 files changed

+50
-938
lines changed

src/Core/BLAKE2b.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public static function rotr64(SplFixedArray $x, int $c): SplFixedArray
165165
}
166166

167167
$l0 = 0;
168+
/** @var int $c */
168169
$c = 64 - $c;
169170

170-
/** @var int $c */
171171
if ($c < 32) {
172172
$h0 = ((int) ($x[0]) << $c) | (
173173
(
@@ -211,8 +211,6 @@ protected static function flatten64(SplFixedArray $x): int
211211
* @param SplFixedArray $x
212212
* @param int $i
213213
* @return SplFixedArray
214-
*
215-
* @throws SodiumException
216214
*/
217215
protected static function load64(SplFixedArray $x, int $i): SplFixedArray
218216
{

src/Core/ChaCha20.php

+8-100
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,6 @@ public static function encryptBytes(
8484
): string {
8585
$bytes = self::strlen($message);
8686

87-
/*
88-
j0 = ctx->input[0];
89-
j1 = ctx->input[1];
90-
j2 = ctx->input[2];
91-
j3 = ctx->input[3];
92-
j4 = ctx->input[4];
93-
j5 = ctx->input[5];
94-
j6 = ctx->input[6];
95-
j7 = ctx->input[7];
96-
j8 = ctx->input[8];
97-
j9 = ctx->input[9];
98-
j10 = ctx->input[10];
99-
j11 = ctx->input[11];
100-
j12 = ctx->input[12];
101-
j13 = ctx->input[13];
102-
j14 = ctx->input[14];
103-
j15 = ctx->input[15];
104-
*/
10587
$j0 = (int) $ctx[0];
10688
$j1 = (int) $ctx[1];
10789
$j2 = (int) $ctx[2];
@@ -144,48 +126,16 @@ public static function encryptBytes(
144126

145127
# for (i = 20; i > 0; i -= 2) {
146128
for ($i = 20; $i > 0; $i -= 2) {
147-
# QUARTERROUND( x0, x4, x8, x12)
148-
list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
129+
[$x0, $x4, $x8, $x12] = self::quarterRound($x0, $x4, $x8, $x12);
130+
[$x1, $x5, $x9, $x13] = self::quarterRound($x1, $x5, $x9, $x13);
131+
[$x2, $x6, $x10, $x14] = self::quarterRound($x2, $x6, $x10, $x14);
132+
[$x3, $x7, $x11, $x15] = self::quarterRound($x3, $x7, $x11, $x15);
149133

150-
# QUARTERROUND( x1, x5, x9, x13)
151-
list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);
152-
153-
# QUARTERROUND( x2, x6, x10, x14)
154-
list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);
155-
156-
# QUARTERROUND( x3, x7, x11, x15)
157-
list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);
158-
159-
# QUARTERROUND( x0, x5, x10, x15)
160-
list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);
161-
162-
# QUARTERROUND( x1, x6, x11, x12)
163-
list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);
164-
165-
# QUARTERROUND( x2, x7, x8, x13)
166-
list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);
167-
168-
# QUARTERROUND( x3, x4, x9, x14)
169-
list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
134+
[$x0, $x5, $x10, $x15] = self::quarterRound($x0, $x5, $x10, $x15);
135+
[$x1, $x6, $x11, $x12] = self::quarterRound($x1, $x6, $x11, $x12);
136+
[$x2, $x7, $x8, $x13] = self::quarterRound($x2, $x7, $x8, $x13);
137+
[$x3, $x4, $x9, $x14] = self::quarterRound($x3, $x4, $x9, $x14);
170138
}
171-
/*
172-
x0 = PLUS(x0, j0);
173-
x1 = PLUS(x1, j1);
174-
x2 = PLUS(x2, j2);
175-
x3 = PLUS(x3, j3);
176-
x4 = PLUS(x4, j4);
177-
x5 = PLUS(x5, j5);
178-
x6 = PLUS(x6, j6);
179-
x7 = PLUS(x7, j7);
180-
x8 = PLUS(x8, j8);
181-
x9 = PLUS(x9, j9);
182-
x10 = PLUS(x10, j10);
183-
x11 = PLUS(x11, j11);
184-
x12 = PLUS(x12, j12);
185-
x13 = PLUS(x13, j13);
186-
x14 = PLUS(x14, j14);
187-
x15 = PLUS(x15, j15);
188-
*/
189139
$x0 = ($x0 & 0xffffffff) + $j0;
190140
$x1 = ($x1 & 0xffffffff) + $j1;
191141
$x2 = ($x2 & 0xffffffff) + $j2;
@@ -203,24 +153,6 @@ public static function encryptBytes(
203153
$x14 = ($x14 & 0xffffffff) + $j14;
204154
$x15 = ($x15 & 0xffffffff) + $j15;
205155

206-
/*
207-
x0 = XOR(x0, LOAD32_LE(m + 0));
208-
x1 = XOR(x1, LOAD32_LE(m + 4));
209-
x2 = XOR(x2, LOAD32_LE(m + 8));
210-
x3 = XOR(x3, LOAD32_LE(m + 12));
211-
x4 = XOR(x4, LOAD32_LE(m + 16));
212-
x5 = XOR(x5, LOAD32_LE(m + 20));
213-
x6 = XOR(x6, LOAD32_LE(m + 24));
214-
x7 = XOR(x7, LOAD32_LE(m + 28));
215-
x8 = XOR(x8, LOAD32_LE(m + 32));
216-
x9 = XOR(x9, LOAD32_LE(m + 36));
217-
x10 = XOR(x10, LOAD32_LE(m + 40));
218-
x11 = XOR(x11, LOAD32_LE(m + 44));
219-
x12 = XOR(x12, LOAD32_LE(m + 48));
220-
x13 = XOR(x13, LOAD32_LE(m + 52));
221-
x14 = XOR(x14, LOAD32_LE(m + 56));
222-
x15 = XOR(x15, LOAD32_LE(m + 60));
223-
*/
224156
$x0 ^= self::load_4(self::substr($message, 0, 4));
225157
$x1 ^= self::load_4(self::substr($message, 4, 4));
226158
$x2 ^= self::load_4(self::substr($message, 8, 4));
@@ -238,35 +170,11 @@ public static function encryptBytes(
238170
$x14 ^= self::load_4(self::substr($message, 56, 4));
239171
$x15 ^= self::load_4(self::substr($message, 60, 4));
240172

241-
/*
242-
j12 = PLUSONE(j12);
243-
if (!j12) {
244-
j13 = PLUSONE(j13);
245-
}
246-
*/
247173
++$j12;
248174
if ($j12 & 0xf0000000) {
249175
throw new SodiumException('Overflow');
250176
}
251177

252-
/*
253-
STORE32_LE(c + 0, x0);
254-
STORE32_LE(c + 4, x1);
255-
STORE32_LE(c + 8, x2);
256-
STORE32_LE(c + 12, x3);
257-
STORE32_LE(c + 16, x4);
258-
STORE32_LE(c + 20, x5);
259-
STORE32_LE(c + 24, x6);
260-
STORE32_LE(c + 28, x7);
261-
STORE32_LE(c + 32, x8);
262-
STORE32_LE(c + 36, x9);
263-
STORE32_LE(c + 40, x10);
264-
STORE32_LE(c + 44, x11);
265-
STORE32_LE(c + 48, x12);
266-
STORE32_LE(c + 52, x13);
267-
STORE32_LE(c + 56, x14);
268-
STORE32_LE(c + 60, x15);
269-
*/
270178
$block = self::store32_le(($x0 & 0xffffffff)) .
271179
self::store32_le(($x1 & 0xffffffff)) .
272180
self::store32_le(($x2 & 0xffffffff)) .

0 commit comments

Comments
 (0)