Skip to content

Commit

Permalink
Remove clutter and pointless overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Apr 19, 2024
1 parent 46206f9 commit 878b86b
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 938 deletions.
4 changes: 1 addition & 3 deletions src/Core/BLAKE2b.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public static function rotr64(SplFixedArray $x, int $c): SplFixedArray
}

$l0 = 0;
/** @var int $c */
$c = 64 - $c;

/** @var int $c */
if ($c < 32) {
$h0 = ((int) ($x[0]) << $c) | (
(
Expand Down Expand Up @@ -211,8 +211,6 @@ protected static function flatten64(SplFixedArray $x): int
* @param SplFixedArray $x
* @param int $i
* @return SplFixedArray
*
* @throws SodiumException
*/
protected static function load64(SplFixedArray $x, int $i): SplFixedArray
{
Expand Down
108 changes: 8 additions & 100 deletions src/Core/ChaCha20.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,6 @@ public static function encryptBytes(
): string {
$bytes = self::strlen($message);

/*
j0 = ctx->input[0];
j1 = ctx->input[1];
j2 = ctx->input[2];
j3 = ctx->input[3];
j4 = ctx->input[4];
j5 = ctx->input[5];
j6 = ctx->input[6];
j7 = ctx->input[7];
j8 = ctx->input[8];
j9 = ctx->input[9];
j10 = ctx->input[10];
j11 = ctx->input[11];
j12 = ctx->input[12];
j13 = ctx->input[13];
j14 = ctx->input[14];
j15 = ctx->input[15];
*/
$j0 = (int) $ctx[0];
$j1 = (int) $ctx[1];
$j2 = (int) $ctx[2];
Expand Down Expand Up @@ -144,48 +126,16 @@ public static function encryptBytes(

# for (i = 20; i > 0; i -= 2) {
for ($i = 20; $i > 0; $i -= 2) {
# QUARTERROUND( x0, x4, x8, x12)
list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12);
[$x0, $x4, $x8, $x12] = self::quarterRound($x0, $x4, $x8, $x12);
[$x1, $x5, $x9, $x13] = self::quarterRound($x1, $x5, $x9, $x13);
[$x2, $x6, $x10, $x14] = self::quarterRound($x2, $x6, $x10, $x14);
[$x3, $x7, $x11, $x15] = self::quarterRound($x3, $x7, $x11, $x15);

# QUARTERROUND( x1, x5, x9, x13)
list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13);

# QUARTERROUND( x2, x6, x10, x14)
list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14);

# QUARTERROUND( x3, x7, x11, x15)
list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15);

# QUARTERROUND( x0, x5, x10, x15)
list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15);

# QUARTERROUND( x1, x6, x11, x12)
list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12);

# QUARTERROUND( x2, x7, x8, x13)
list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13);

# QUARTERROUND( x3, x4, x9, x14)
list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14);
[$x0, $x5, $x10, $x15] = self::quarterRound($x0, $x5, $x10, $x15);
[$x1, $x6, $x11, $x12] = self::quarterRound($x1, $x6, $x11, $x12);
[$x2, $x7, $x8, $x13] = self::quarterRound($x2, $x7, $x8, $x13);
[$x3, $x4, $x9, $x14] = self::quarterRound($x3, $x4, $x9, $x14);
}
/*
x0 = PLUS(x0, j0);
x1 = PLUS(x1, j1);
x2 = PLUS(x2, j2);
x3 = PLUS(x3, j3);
x4 = PLUS(x4, j4);
x5 = PLUS(x5, j5);
x6 = PLUS(x6, j6);
x7 = PLUS(x7, j7);
x8 = PLUS(x8, j8);
x9 = PLUS(x9, j9);
x10 = PLUS(x10, j10);
x11 = PLUS(x11, j11);
x12 = PLUS(x12, j12);
x13 = PLUS(x13, j13);
x14 = PLUS(x14, j14);
x15 = PLUS(x15, j15);
*/
$x0 = ($x0 & 0xffffffff) + $j0;
$x1 = ($x1 & 0xffffffff) + $j1;
$x2 = ($x2 & 0xffffffff) + $j2;
Expand All @@ -203,24 +153,6 @@ public static function encryptBytes(
$x14 = ($x14 & 0xffffffff) + $j14;
$x15 = ($x15 & 0xffffffff) + $j15;

/*
x0 = XOR(x0, LOAD32_LE(m + 0));
x1 = XOR(x1, LOAD32_LE(m + 4));
x2 = XOR(x2, LOAD32_LE(m + 8));
x3 = XOR(x3, LOAD32_LE(m + 12));
x4 = XOR(x4, LOAD32_LE(m + 16));
x5 = XOR(x5, LOAD32_LE(m + 20));
x6 = XOR(x6, LOAD32_LE(m + 24));
x7 = XOR(x7, LOAD32_LE(m + 28));
x8 = XOR(x8, LOAD32_LE(m + 32));
x9 = XOR(x9, LOAD32_LE(m + 36));
x10 = XOR(x10, LOAD32_LE(m + 40));
x11 = XOR(x11, LOAD32_LE(m + 44));
x12 = XOR(x12, LOAD32_LE(m + 48));
x13 = XOR(x13, LOAD32_LE(m + 52));
x14 = XOR(x14, LOAD32_LE(m + 56));
x15 = XOR(x15, LOAD32_LE(m + 60));
*/
$x0 ^= self::load_4(self::substr($message, 0, 4));
$x1 ^= self::load_4(self::substr($message, 4, 4));
$x2 ^= self::load_4(self::substr($message, 8, 4));
Expand All @@ -238,35 +170,11 @@ public static function encryptBytes(
$x14 ^= self::load_4(self::substr($message, 56, 4));
$x15 ^= self::load_4(self::substr($message, 60, 4));

/*
j12 = PLUSONE(j12);
if (!j12) {
j13 = PLUSONE(j13);
}
*/
++$j12;
if ($j12 & 0xf0000000) {
throw new SodiumException('Overflow');
}

/*
STORE32_LE(c + 0, x0);
STORE32_LE(c + 4, x1);
STORE32_LE(c + 8, x2);
STORE32_LE(c + 12, x3);
STORE32_LE(c + 16, x4);
STORE32_LE(c + 20, x5);
STORE32_LE(c + 24, x6);
STORE32_LE(c + 28, x7);
STORE32_LE(c + 32, x8);
STORE32_LE(c + 36, x9);
STORE32_LE(c + 40, x10);
STORE32_LE(c + 44, x11);
STORE32_LE(c + 48, x12);
STORE32_LE(c + 52, x13);
STORE32_LE(c + 56, x14);
STORE32_LE(c + 60, x15);
*/
$block = self::store32_le(($x0 & 0xffffffff)) .
self::store32_le(($x1 & 0xffffffff)) .
self::store32_le(($x2 & 0xffffffff)) .
Expand Down
Loading

0 comments on commit 878b86b

Please sign in to comment.