EllipticBench.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. require __DIR__ . "/../vendor/autoload.php";
  3. /**
  4. * @BeforeMethods({"init"})
  5. *
  6. * @Iterations(5)
  7. * @Revs(50)
  8. * @OutputTimeUnit("seconds")
  9. * @OutputMode("throughput")
  10. */
  11. class EllipticBench {
  12. private $ec;
  13. private $keys;
  14. private $hash;
  15. static $msg = [ 0xB, 0xE, 0xE, 0xF ];
  16. public function init() {
  17. $this->ec = new \Elliptic\EC('secp256k1');
  18. $this->priv = $this->ec->genKeyPair();
  19. $this->pub = $this->priv->getPublic();
  20. $this->hash = hash('sha256', 'hello world');
  21. $this->sign = $this->priv->sign($this->hash);
  22. $this->priv2 = $this->ec->genKeyPair();
  23. $this->pub2 = $this->priv2->getPublic();
  24. $this->ed25519 = new \Elliptic\EdDSA('ed25519');
  25. $secret = array_fill(0, 32, 0);
  26. $this->edkey = $this->ed25519->keyFromSecret($secret);
  27. $this->edsig = $this->edkey->sign(self::$msg);
  28. }
  29. public function benchGenKeyPair() {
  30. $this->ec->genKeyPair();
  31. }
  32. public function benchGenKeyPairWithPublicKey() {
  33. $priv = $this->ec->genKeyPair();
  34. $pub = $priv->getPublic();
  35. }
  36. public function benchSign() {
  37. $this->priv->sign($this->hash);
  38. }
  39. public function benchVerify() {
  40. if ( !$this->ec->verify($this->hash, $this->sign, $this->pub) )
  41. throw new \Exception("unexpected");
  42. }
  43. public function benchDH() {
  44. $this->priv->derive($this->pub2);
  45. }
  46. public function benchEdDSASign() {
  47. $this->edkey->sign(self::$msg);
  48. }
  49. public function benchEdDSAVerify() {
  50. if ( !$this->edkey->verify(self::$msg, $this->edsig) )
  51. throw new \Exception("unexpected");
  52. }
  53. }