Skip to content

Commit

Permalink
Whitespace, readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jun 9, 2017
1 parent b5477c5 commit 78cb755
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 106 deletions.
45 changes: 6 additions & 39 deletions src/Core/BLAKE2b.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ protected static function to64($num)
protected static function add64($x, $y)
{
$l = ($x[1] + $y[1]) & 0xffffffff;
return self::new64($x[0] + $y[0] + (($l < $x[1]) ? 1 : 0), $l);
return self::new64(
$x[0] + $y[0] + (
($l < $x[1]) ? 1 : 0
),
$l
);
}

/**
Expand Down Expand Up @@ -205,44 +210,6 @@ protected static function store64(SplFixedArray $x, $i, SplFixedArray $u)
}
$u[$uIdx] >>= 8;
}
/*
// OLD STYLE:
$x[$i] = ($u[1] & 0xff);
if ($maxLength <= $i) {
return;
}
$u[1] >>= 8;
$x[$i+1] = ($u[1] & 0xff);
if ($maxLength <= $i + 1) {
return;
}
$u[1] >>= 8;
$x[$i+2] = ($u[1] & 0xff);
if ($maxLength <= $i + 2) {
return;
}
$u[1] >>= 8;
$x[$i+3] = ($u[1] & 0xff);
if ($maxLength <= $i + 3) {
return;
}
$x[$i+4] = ($u[0] & 0xff);
if ($maxLength <= $i + 4) {
return;
}
$u[0] >>= 8;
$x[$i+5] = ($u[0] & 0xff);
if ($maxLength <= $i + 5) {
return;
}
$u[0] >>= 8;
$x[$i+6] = ($u[0] & 0xff);
if ($maxLength <= $i + 6) {
return;
}
$u[0] >>= 8;
$x[$i+7] = ($u[0] & 0xff);
*/
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Core/ChaCha20.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public static function rotate($v, $n)
$v &= 0xffffffff;
$n &= 31;
return 0xffffffff & (
($v << $n) | ($v >> (32 - $n))
($v << $n)
|
($v >> (32 - $n))
);
}

Expand Down
107 changes: 45 additions & 62 deletions src/Core/Curve25519.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,9 @@ abstract class ParagonIE_Sodium_Core_Curve25519 extends ParagonIE_Sodium_Core_Cu
*/
public static function fe_0()
{
return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(array(
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
));
return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
);
}

/**
Expand All @@ -47,18 +38,9 @@ public static function fe_0()
*/
public static function fe_1()
{
return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(array(
1,
0,
0,
0,
0,
0,
0,
0,
0,
0
));
return ParagonIE_Sodium_Core_Curve25519_Fe::fromArray(
array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
);
}

/**
Expand Down Expand Up @@ -493,46 +475,46 @@ public static function fe_mul(
$h8 = $f0g8 + $f1g7_2 + $f2g6 + $f3g5_2 + $f4g4 + $f5g3_2 + $f6g2 + $f7g1_2 + $f8g0 + $f9g9_38;
$h9 = $f0g9 + $f1g8 + $f2g7 + $f3g6 + $f4g5 + $f5g4 + $f6g3 + $f7g2 + $f8g1 + $f9g0 ;

$carry0 = ($h0 + (1<<25)) >> 26;
$carry0 = ($h0 + (1 << 25)) >> 26;
$h1 += $carry0;
$h0 -= $carry0 << 26;
$carry4 = ($h4 + (1<<25)) >> 26;
$carry4 = ($h4 + (1 << 25)) >> 26;
$h5 += $carry4;
$h4 -= $carry4 << 26;

$carry1 = ($h1 + (1<<24)) >> 25;
$carry1 = ($h1 + (1 << 24)) >> 25;
$h2 += $carry1;
$h1 -= $carry1 << 25;
$carry5 = ($h5 + (1<<24)) >> 25;
$carry5 = ($h5 + (1 << 24)) >> 25;
$h6 += $carry5;
$h5 -= $carry5 << 25;

$carry2 = ($h2 + (1<<25)) >> 26;
$carry2 = ($h2 + (1 << 25)) >> 26;
$h3 += $carry2;
$h2 -= $carry2 << 26;
$carry6 = ($h6 + (1<<25)) >> 26;
$carry6 = ($h6 + (1 << 25)) >> 26;
$h7 += $carry6;
$h6 -= $carry6 << 26;

$carry3 = ($h3 + (1<<24)) >> 25;
$carry3 = ($h3 + (1 << 24)) >> 25;
$h4 += $carry3;
$h3 -= $carry3 << 25;
$carry7 = ($h7 + (1<<24)) >> 25;
$carry7 = ($h7 + (1 << 24)) >> 25;
$h8 += $carry7;
$h7 -= $carry7 << 25;

$carry4 = ($h4 + (1<<25)) >> 26;
$carry4 = ($h4 + (1 << 25)) >> 26;
$h5 += $carry4;
$h4 -= $carry4 << 26;
$carry8 = ($h8 + (1<<25)) >> 26;
$carry8 = ($h8 + (1 << 25)) >> 26;
$h9 += $carry8;
$h8 -= $carry8 << 26;

$carry9 = ($h9 + (1<<24)) >> 25;
$carry9 = ($h9 + (1 << 24)) >> 25;
$h0 += self::mul($carry9, 19);
$h9 -= $carry9 << 25;

$carry0 = ($h0 + (1<<25)) >> 26;
$carry0 = ($h0 + (1 << 25)) >> 26;
$h1 += $carry0;
$h0 -= $carry0 << 26;

Expand Down Expand Up @@ -673,46 +655,46 @@ public static function fe_sq(ParagonIE_Sodium_Core_Curve25519_Fe $f)
$h8 = $f0f8_2 + $f1f7_4 + $f2f6_2 + $f3f5_4 + $f4f4 + $f9f9_38;
$h9 = $f0f9_2 + $f1f8_2 + $f2f7_2 + $f3f6_2 + $f4f5_2;

$carry0 = ($h0 + (1<<25)) >> 26;
$carry0 = ($h0 + (1 << 25)) >> 26;
$h1 += $carry0;
$h0 -= $carry0 << 26;
$carry4 = ($h4 + (1<<25)) >> 26;
$carry4 = ($h4 + (1 << 25)) >> 26;
$h5 += $carry4;
$h4 -= $carry4 << 26;

$carry1 = ($h1 + (1<<24)) >> 25;
$carry1 = ($h1 + (1 << 24)) >> 25;
$h2 += $carry1;
$h1 -= $carry1 << 25;
$carry5 = ($h5 + (1<<24)) >> 25;
$carry5 = ($h5 + (1 << 24)) >> 25;
$h6 += $carry5;
$h5 -= $carry5 << 25;

$carry2 = ($h2 + (1<<25)) >> 26;
$carry2 = ($h2 + (1 << 25)) >> 26;
$h3 += $carry2;
$h2 -= $carry2 << 26;
$carry6 = ($h6 + (1<<25)) >> 26;
$carry6 = ($h6 + (1 << 25)) >> 26;
$h7 += $carry6;
$h6 -= $carry6 << 26;

$carry3 = ($h3 + (1<<24)) >> 25;
$carry3 = ($h3 + (1 << 24)) >> 25;
$h4 += $carry3;
$h3 -= $carry3 << 25;
$carry7 = ($h7 + (1<<24)) >> 25;
$carry7 = ($h7 + (1 << 24)) >> 25;
$h8 += $carry7;
$h7 -= $carry7 << 25;

$carry4 = ($h4 + (1<<25)) >> 26;
$carry4 = ($h4 + (1 << 25)) >> 26;
$h5 += $carry4;
$h4 -= $carry4 << 26;
$carry8 = ($h8 + (1<<25)) >> 26;
$carry8 = ($h8 + (1 << 25)) >> 26;
$h9 += $carry8;
$h8 -= $carry8 << 26;

$carry9 = ($h9 + (1<<24)) >> 25;
$carry9 = ($h9 + (1 << 24)) >> 25;
$h0 += self::mul($carry9, 19);
$h9 -= $carry9 << 25;

$carry0 = ($h0 + (1<<25)) >> 26;
$carry0 = ($h0 + (1 << 25)) >> 26;
$h1 += $carry0;
$h0 -= $carry0 << 26;

Expand Down Expand Up @@ -847,46 +829,46 @@ public static function fe_sq2(ParagonIE_Sodium_Core_Curve25519_Fe $f)
$h8 = (int) ($h8 + $h8);
$h9 = (int) ($h9 + $h9);

$carry0 = ($h0 + (1<<25)) >> 26;
$carry0 = ($h0 + (1 << 25)) >> 26;
$h1 += $carry0;
$h0 -= $carry0 << 26;
$carry4 = ($h4 + (1<<25)) >> 26;
$carry4 = ($h4 + (1 << 25)) >> 26;
$h5 += $carry4;
$h4 -= $carry4 << 26;

$carry1 = ($h1 + (1<<24)) >> 25;
$carry1 = ($h1 + (1 << 24)) >> 25;
$h2 += $carry1;
$h1 -= $carry1 << 25;
$carry5 = ($h5 + (1<<24)) >> 25;
$carry5 = ($h5 + (1 << 24)) >> 25;
$h6 += $carry5;
$h5 -= $carry5 << 25;

$carry2 = ($h2 + (1<<25)) >> 26;
$carry2 = ($h2 + (1 << 25)) >> 26;
$h3 += $carry2;
$h2 -= $carry2 << 26;
$carry6 = ($h6 + (1<<25)) >> 26;
$carry6 = ($h6 + (1 << 25)) >> 26;
$h7 += $carry6;
$h6 -= $carry6 << 26;

$carry3 = ($h3 + (1<<24)) >> 25;
$carry3 = ($h3 + (1 << 24)) >> 25;
$h4 += $carry3;
$h3 -= $carry3 << 25;
$carry7 = ($h7 + (1<<24)) >> 25;
$carry7 = ($h7 + (1 << 24)) >> 25;
$h8 += $carry7;
$h7 -= $carry7 << 25;

$carry4 = ($h4 + (1<<25)) >> 26;
$carry4 = ($h4 + (1 << 25)) >> 26;
$h5 += $carry4;
$h4 -= $carry4 << 26;
$carry8 = ($h8 + (1<<25)) >> 26;
$carry8 = ($h8 + (1 << 25)) >> 26;
$h9 += $carry8;
$h8 -= $carry8 << 26;

$carry9 = ($h9 + (1<<24)) >> 25;
$carry9 = ($h9 + (1 << 24)) >> 25;
$h0 += self::mul($carry9, 19);
$h9 -= $carry9 << 25;

$carry0 = ($h0 + (1<<25)) >> 26;
$carry0 = ($h0 + (1 << 25)) >> 26;
$h1 += $carry0;
$h0 -= $carry0 << 26;

Expand Down Expand Up @@ -1138,7 +1120,7 @@ public static function ge_add(
$r->Y = self::fe_mul($r->Y, $q->YminusX);
$r->T = self::fe_mul($q->T2d, $p->T);
$r->X = self::fe_mul($p->Z, $q->Z);
$t0 = self::fe_add($r->X, $r->X);
$t0 = self::fe_add($r->X, $r->X);
$r->X = self::fe_sub($r->Z, $r->Y);
$r->Y = self::fe_add($r->Z, $r->Y);
$r->Z = self::fe_add($t0, $r->T);
Expand Down Expand Up @@ -1397,7 +1379,7 @@ public static function ge_p2_dbl(ParagonIE_Sodium_Core_Curve25519_Ge_P2 $p)
$r->Z = self::fe_sq($p->Y);
$r->T = self::fe_sq2($p->Z);
$r->Y = self::fe_add($p->X, $p->Y);
$t0 = self::fe_sq($r->Y);
$t0 = self::fe_sq($r->Y);
$r->Y = self::fe_add($r->Z, $r->X);
$r->Z = self::fe_sub($r->Z, $r->X);
$r->X = self::fe_sub($t0, $r->Y);
Expand Down Expand Up @@ -1882,6 +1864,7 @@ public static function sc_muladd($a, $b, $c)
$c10 = 2097151 & (self::load_3(self::substr($c, 26, 3)) >> 2);
$c11 = (self::load_4(self::substr($c, 28, 4)) >> 7);

/* Can't really avoid the pyramid here: */
$s0 = $c0 + self::mul($a0, $b0);
$s1 = $c1 + self::mul($a0, $b1) + self::mul($a1, $b0);
$s2 = $c2 + self::mul($a0, $b2) + self::mul($a1, $b1) + self::mul($a2, $b0);
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Ed25519.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public static function publickey_from_secretkey($sk)
public static function sk_to_pk($sk)
{
return self::ge_p3_tobytes(
self::ge_scalarmult_base(self::substr($sk, 0, 32))
self::ge_scalarmult_base(
self::substr($sk, 0, 32)
)
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Core/Salsa20.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public static function rotate($u, $c)
{
$u &= 0xffffffff;
$c %= 32;
return 0xffffffff & (($u << $c) | ($u >> (32 - $c)));
return 0xffffffff & (
($u << $c)
|
($u >> (32 - $c))
);
}
}
4 changes: 4 additions & 0 deletions src/Core/X25519.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public static function fe_mul121666(ParagonIE_Sodium_Core_Curve25519_Fe $f)
/**
* @internal You should not use this directly from another application
*
* Inline comments preceded by # are from libsodium's ref10 code.
*
* @param string $n
* @param string $p
* @return string
Expand Down Expand Up @@ -283,10 +285,12 @@ public static function crypto_scalarmult_curve25519_ref10_base($n)
{
# for (i = 0;i < 32;++i) e[i] = n[i];
$e = '' . $n;

# e[0] &= 248;
$e[0] = self::intToChr(
self::chrToInt($e[0]) & 248
);

# e[31] &= 127;
# e[31] |= 64;
$e[31] = self::intToChr(
Expand Down
5 changes: 4 additions & 1 deletion src/Core/XChaCha20.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public static function stream($len = 64, $nonce = '', $key = '')
}
return self::encryptBytes(
new ParagonIE_Sodium_Core_ChaCha20_Ctx(
self::hChaCha20(self::substr($nonce, 0, 16), $key),
self::hChaCha20(
self::substr($nonce, 0, 16),
$key
),
self::substr($nonce, 16, 8)
),
str_repeat("\x00", $len)
Expand Down
1 change: 0 additions & 1 deletion src/Core/Xsalsa20.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


if (class_exists('ParagonIE_Sodium_Core_Xsalsa20', false)) {
return;
}
Expand Down

0 comments on commit 78cb755

Please sign in to comment.