EllipticBench.php 1.7 KB

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