New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proof of Concept] - Hypothetical Version 2.x #154
Conversation
The \Sodium\foo() API is no longer present in v2.x The rest of the PHP ecosystem has largely moved onto sodium_foo() instead.
We don't need the old libsodium source code in comments anymore. We're confident our implementation is correct.
Q: Does strict-typing impact performance?Here's a dumb benchmarking script: <?php
require_once __DIR__ . '/autoload-phpunit.php';
ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true;
$stop = $start = 0.0;
$i = 0;
$start = microtime(true);
while ($i < 1000) {
$p = sodium_crypto_core_ristretto255_scalar_random();
$q = sodium_crypto_core_ristretto255_scalar_random();
$r = sodium_crypto_core_ristretto255_scalar_add($p, $q);
$p = sodium_crypto_core_ristretto255_scalar_sub($r, $q);
$z = sodium_crypto_core_ristretto255_scalar_mul($p, $q);
$comp = sodium_crypto_core_ristretto255_scalar_complement($p);
$inv = sodium_crypto_core_ristretto255_scalar_invert($p);
$neg1 = sodium_crypto_core_ristretto255_scalar_negate($p);
$bytes = random_bytes(ParagonIE_Sodium_Compat::CRYPTO_CORE_RISTRETTO255_NONREDUCEDSCALARBYTES);
$red = sodium_crypto_core_ristretto255_scalar_reduce($bytes);
$alice = sodium_crypto_box_keypair();
$alice_s = sodium_crypto_box_secretkey($alice);
$alice_p = sodium_crypto_box_publickey($alice);
$bob = sodium_crypto_box_keypair();
$bob_s = sodium_crypto_box_secretkey($bob);
$bob_p = sodium_crypto_box_publickey($bob);
$a2b = sodium_crypto_scalarmult($alice_s, $bob_p);
$b2a = sodium_crypto_scalarmult($bob_s, $alice_p);
++$i;
}
$stop = microtime(true);
var_dump($stop - $start); Results (dev-master)
Results (v2.x branch)
Results (v2.x branch with Strict Typing)For this test, I added
Summary
It seems that any performance penalty incurred by strict types is negligible at best. |
I forgot to disable ext-sodium, so here's a better benchmarking script. <?php
declare(strict_types=1);
require_once __DIR__ . '/autoload-phpunit.php';
ParagonIE_Sodium_Compat::$disableFallbackForUnitTests = true;
$stop = $start = 0.0;
$i = 0;
$start = microtime(true);
while ($i < 5) {
$p = ParagonIE_Sodium_Compat::ristretto255_scalar_random();
$q = ParagonIE_Sodium_Compat::ristretto255_scalar_random();
$r = ParagonIE_Sodium_Compat::ristretto255_scalar_add($p, $q);
$p = ParagonIE_Sodium_Compat::ristretto255_scalar_sub($r, $q);
$z = ParagonIE_Sodium_Compat::ristretto255_scalar_mul($p, $q);
$comp = ParagonIE_Sodium_Compat::ristretto255_scalar_complement($p);
$inv = ParagonIE_Sodium_Compat::ristretto255_scalar_invert($p);
$neg = ParagonIE_Sodium_Compat::ristretto255_scalar_negate($p);
$bytes = random_bytes(ParagonIE_Sodium_Compat::CRYPTO_CORE_RISTRETTO255_NONREDUCEDSCALARBYTES);
$red = ParagonIE_Sodium_Compat::ristretto255_scalar_reduce($bytes);
$alice = ParagonIE_Sodium_Compat::crypto_box_keypair();
$alice_s = ParagonIE_Sodium_Compat::crypto_box_secretkey($alice);
$alice_p = ParagonIE_Sodium_Compat::crypto_box_publickey($alice);
$bob = ParagonIE_Sodium_Compat::crypto_box_keypair();
$bob_s = ParagonIE_Sodium_Compat::crypto_box_secretkey($bob);
$bob_p = ParagonIE_Sodium_Compat::crypto_box_publickey($bob);
$a2b = ParagonIE_Sodium_Compat::crypto_scalarmult($alice_s, $bob_p);
$b2a = ParagonIE_Sodium_Compat::crypto_scalarmult($bob_s, $alice_p);
++$i;
}
$stop = microtime(true);
var_dump($stop - $start); Updated Results
From v2's perspective, there is a small (1.97%) performance hit for using strict_types. However, the savings from dev-master are still larger (3.64% slower). If we anchor on dev-master, the total run1time is 94.61% (v2) and 96.48% (v2-strict) what we were seeing before. Overall, performance is improved with v2.x. |
I'm closing this pull request. If we decide to continue work on v2.x, the current branch will serve as the basis for this work. |
See #137 for background
This PoC exists to answer the following questions:
I may follow up with another branch that pins the minimum to 8.1 and uses FFI for scrypt/Argon2 support.
Note: This will never be merged into the
master
branch (indefinite PHP 5 support is a goal of that branch). Additionally, no tags/releases will be issued from this branch until we're settled on what the minimum version constraint for a sodium_compat v2.x should even be.What's Been Removed?
\Sodium\*()
APIWhat's Been Changed?
Not much else, really. You should be able to test these changes against your own code without modifying anything since v1.19.0.
How Do These Changes Affect PHP 8.2?
dev-master
This Branch
Some tests were removed (due to removed support).