Skip to content

Instantly share code, notes, and snippets.

@afk11
Last active June 11, 2016 16:32
Show Gist options
  • Save afk11/4208b0fa895055ca2a226c856de4f112 to your computer and use it in GitHub Desktop.
Save afk11/4208b0fa895055ca2a226c856de4f112 to your computer and use it in GitHub Desktop.
PHPECC operation benchmarks
<?php
require "vendor/autoload.php";
$math = Mdanter\Ecc\EccFactory::getAdapter();
$g = \Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1();
/**
* @var \Mdanter\Ecc\Primitives\GeneratorPoint[]
*/
$generators = [
\Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator192(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator256(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator521(),
];
$results = [];
$runsPerCurve = 500;
$signer = new \Mdanter\Ecc\Crypto\Signature\Signer($math);
foreach ($generators as $g) {
$n = $g->getOrder();
$name = $g->getCurve()->getName();
$random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getRandomGenerator();
$keys = [];
for ($i = 0; $i < $runsPerCurve; $i++) {
$k = $random->generate($n);
$keys[] = [$k];
}
$start = microtime(true);
foreach ($keys as $key) {
list ($k) = $key;
$point = $g->mul($k);
}
$total = microtime(true) - $start;
$results[$name] = $total;
}
echo "# Scalar mult benchmarks\n";
echo "Number of curves tested: ".count($generators).PHP_EOL;
echo "Number of iterations per curve: ".$runsPerCurve.PHP_EOL;
foreach ($results as $name => $result) {
$avg = $result / $runsPerCurve;
echo $name . " --- Total: " . $result . " / Avg run: " . $avg . PHP_EOL;
}
<?php
require "vendor/autoload.php";
$math = Mdanter\Ecc\EccFactory::getAdapter();
$g = \Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1();
/**
* @var \Mdanter\Ecc\Primitives\GeneratorPoint[]
*/
$generators = [
\Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator192(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator256(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator521(),
];
$results = [];
$runsPerCurve = 500;
$signer = new \Mdanter\Ecc\Crypto\Signature\Signer($math);
foreach ($generators as $g) {
$n = $g->getOrder();
$name = $g->getCurve()->getName();
$random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getRandomGenerator();
$keys = [];
for ($i = 0; $i < $runsPerCurve; $i++) {
$keys[] = [$g->getPrivateKeyFrom($random->generate($n)), $random->generate($n), $random->generate($n)];
}
$start = microtime(true);
foreach ($keys as $key) {
list ($p, $hash, $k) = $key;
$signature = $signer->sign($p, $hash, $k);
}
$total = microtime(true) - $start;
$results[$name] = $total;
}
echo "# Signature creation results\n";
echo "Number of curves tested: ".count($generators).PHP_EOL;
echo "Number of iterations per curve: ".$runsPerCurve.PHP_EOL;
foreach ($results as $name => $result) {
if (substr($name, 0, strlen('inline')) == 'inline') {
continue;
}
$avg = $result / $runsPerCurve;
echo $name . " --- Total: " . $result . " / Avg run: " . $avg . PHP_EOL;
}
<?php
require "vendor/autoload.php";
$math = Mdanter\Ecc\EccFactory::getAdapter();
$g = \Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1();
/**
* @var \Mdanter\Ecc\Primitives\GeneratorPoint[]
*/
$generators = [
\Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator192(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator256(),
\Mdanter\Ecc\EccFactory::getNistCurves()->generator521(),
];
$results = [];
$runsPerCurve = 500;
$signer = new \Mdanter\Ecc\Crypto\Signature\Signer($math);
foreach ($generators as $g) {
$n = $g->getOrder();
$name = $g->getCurve()->getName();
$random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getRandomGenerator();
$keys = [];
for ($i = 0; $i < $runsPerCurve; $i++) {
$key = $g->getPrivateKeyFrom($random->generate($n));
$hash = $random->generate($n);
$k = $random->generate($n);
$public = $key->getPublicKey();
$sig = $signer->sign($key, $hash, $k);
$keys[] = [$sig, $public, $hash];
}
$start = microtime(true);
foreach ($keys as $key) {
list ($sig, $public, $hash) = $key;
$signature = $signer->verify($public, $sig, $hash);
}
$total = microtime(true) - $start;
$results[$name] = $total;
}
echo "# Signature verification benchmarks\n";
echo "Number of curves tested: ".count($generators).PHP_EOL;
echo "Number of iterations per curve: ".$runsPerCurve.PHP_EOL;
foreach ($results as $name => $result) {
$avg = $result / $runsPerCurve;
echo $name . " --- Total: " . $result . " / Avg run: " . $avg . PHP_EOL;
}

Without patch

Scalar mult benchmarks

Number of curves tested: 4
Number of iterations per curve: 500
secp256k1 --- Total: 23.179268121719 / Avg run: 0.046358536243439
nist-p192 --- Total: 15.343150854111 / Avg run: 0.030686301708221
nist-p256 --- Total: 23.433343887329 / Avg run: 0.046866687774658
nist-p521 --- Total: 73.456861019135 / Avg run: 0.14691372203827

With patch

Scalar mult benchmarks

Number of curves tested: 4
Number of iterations per curve: 500
secp256k1 --- Total: 8.6857531070709 / Avg run: 0.017371506214142
nist-p192 --- Total: 5.8396420478821 / Avg run: 0.011679284095764
nist-p256 --- Total: 8.7175660133362 / Avg run: 0.017435132026672
nist-p521 --- Total: 25.171487092972 / Avg run: 0.050342974185944

Without patch

Signature verification benchmarks

Number of curves tested: 4
Number of iterations per curve: 500
secp256k1 --- Total: 46.763059854507 / Avg run: 0.093526119709015
nist-p192 --- Total: 30.7817029953 / Avg run: 0.061563405990601
nist-p256 --- Total: 47.162358999252 / Avg run: 0.094324717998505
nist-p521 --- Total: 148.98546504974 / Avg run: 0.29797093009949

With patch

Signature verification benchmarks

Number of curves tested: 4
Number of iterations per curve: 500
secp256k1 --- Total: 17.863524913788 / Avg run: 0.035727049827576
nist-p192 --- Total: 12.153017997742 / Avg run: 0.024306035995483
nist-p256 --- Total: 18.767238140106 / Avg run: 0.037534476280212
nist-p521 --- Total: 52.01026892662 / Avg run: 0.10402053785324

Without patch

Signature creation results

Number of curves tested: 4
Number of iterations per curve: 500
secp256k1 --- Total: 23.446593999863 / Avg run: 0.046893187999725
nist-p192 --- Total: 15.967781066895 / Avg run: 0.031935562133789
nist-p256 --- Total: 24.600135803223 / Avg run: 0.049200271606445
nist-p521 --- Total: 75.563390016556 / Avg run: 0.15112678003311

With patch

Signature creation results

Number of curves tested: 4
Number of iterations per curve: 500
secp256k1 --- Total: 8.9722051620483 / Avg run: 0.017944410324097
nist-p192 --- Total: 5.8735201358795 / Avg run: 0.011747040271759
nist-p256 --- Total: 8.8211510181427 / Avg run: 0.017642302036285
nist-p521 --- Total: 25.292445898056 / Avg run: 0.050584891796112
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment