public static function getContext() { if (self::$context == null) { self::$context = \secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); } return self::$context; }
/** * @param null $flags * @return resource */ public static function getSecp256k1Context($flags = null) { if (!extension_loaded('secp256k1')) { throw new \RuntimeException('Secp256k1 not installed'); } if (self::$context === null) { self::$context = secp256k1_context_create($flags ?: SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); } return self::$context; }
/** * @return array */ public function getPkVectors() { $parser = new Yaml(); $data = $parser->parse(__DIR__ . '/data/pubkey_create.yml'); $fixtures = array(); $context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); foreach ($data['vectors'] as $c => $vector) { $fixtures[] = array($context, $vector['seckey'], $c % 2 == 0); } return $fixtures; }
public function testContext() { $ctx = \secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); $clone = \secp256k1_context_clone($ctx); // We should have two resources of type secp256k1_context_t $this->assertInternalType('resource', $ctx); $this->assertInternalType('resource', $ctx); $this->assertEquals(SECP256K1_TYPE_CONTEXT, get_resource_type($ctx)); $this->assertEquals(SECP256K1_TYPE_CONTEXT, get_resource_type($clone)); // We should be able to destroy it (without affecting the other), and see it's type is now unknown. $this->assertTrue(\secp256k1_context_destroy($ctx)); $this->assertEquals('Unknown', get_resource_type($ctx)); $this->assertEquals(SECP256K1_TYPE_CONTEXT, get_resource_type($clone)); }
function generateKeyPair() { // Create a context: $ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); do { // Generate random str: $privkey = mcrypt_create_iv(32, \MCRYPT_DEV_URANDOM); // Attempt to verify that it's a valid private key: } while (!(bool) secp256k1_ec_seckey_verify($ctx, $privkey)); $pubkey = null; $pubkeyRef = null; // Create the public key (note: For additional safety, check this equals 1): secp256k1_ec_pubkey_create($ctx, $privkey, $pubkeyRef); // Serialise it: secp256k1_ec_pubkey_serialize($ctx, $pubkeyRef, false, $pubkey); // Done: return array('private' => $privkey, 'public' => $pubkey); }
<?php $context = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); $context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); $msg32 = hash('sha256', 'this is a message!', true); $signatureRaw = pack("H*", "3044022055ef6953afd139d917d947ba7823ab5dfb9239ba8a26295a218cad88fb7299ef022057147cf4233ff3b87fa64d82a0b9a327e9b6d5d0070ab3f671b795934c4f2074"); $publicKeyRaw = pack("H*", '04fae8f5e64c9997749ef65c5db9f0ec3e121dc6901096c30da0f105a13212b6db4315e65a2d63cc667c034fac05cdb3c7bc1abfc2ad90f7f97321613f901758c9'); // Load up the public key from its bytes (into $publicKey): /** @var resource $publicKey */ $publicKey = ''; secp256k1_ec_pubkey_parse($context, $publicKey, $publicKeyRaw); // Load up the signature from its bytes (into $signature): /** @var resource $signature */ $signature = ''; secp256k1_ecdsa_signature_parse_der($context, $signature, $signatureRaw); // Verify: for ($i = 0; $i < 10000; $i++) { $result = secp256k1_ecdsa_verify($context, $signature, $msg32, $publicKey); }