Exemple #1
0
function verify($signatureB64, $data, $publicKeyRaw)
{
    if ($publicKeyRaw == null) {
        global $thisEntity;
        // Load the public key if needed (from a hex string):
        if (!isset($thisEntity['PublicKeyBytes'])) {
            // Load it now:
            $thisEntity['PublicKeyBytes'] = hex2bin($thisEntity['Key']);
        }
        // Grab the raw public key (bytes):
        $publicKeyRaw = $thisEntity['PublicKeyBytes'];
    }
    // Decode the signature from base64:
    $decodedSignature = base64_decode($signatureB64, true);
    if ($decodedSignature === false) {
        // Invalid signature.
        error('field/invalid', 'signature');
    }
    // Get the double hash of the data:
    $msg32 = hash('sha256', hash('sha256', $data, true), true);
    // Create a context:
    $ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
    // Load up the public key from its bytes (into $publicKey):
    $publicKey;
    secp256k1_ec_pubkey_parse($ctx, $publicKeyRaw, $publicKey);
    // Load up the signature from its bytes (into $signature):
    $signature;
    secp256k1_ecdsa_signature_parse_der($ctx, $decodedSignature, $signature);
    if ($signature == null) {
        // Not a valid signature.
        error('field/invalid', 'signature');
    }
    // Verify:
    return secp256k1_ecdsa_verify($ctx, $msg32, $signature, $publicKey);
}
 /**
  * @dataProvider getErroneousTypeVectors
  * @expectedException \PHPUnit_Framework_Error_Warning
  */
 public function testErroneousTypes($context, $msg32, $sig, $public)
 {
     $s = '';
     $p = '';
     secp256k1_ecdsa_signature_parse_der($context, $sig, $s);
     secp256k1_ec_pubkey_parse($context, $public, $p);
     \secp256k1_ecdsa_verify($context, $msg32, $s, $p);
 }
 /**
  * @param \BitWasp\Buffertools\BufferInterface|string $data
  * @return PublicKey
  */
 public function parse($data)
 {
     $buffer = (new Parser($data))->getBuffer();
     $binary = $buffer->getBinary();
     $pubkey_t = '';
     /** @var resource $pubkey_t */
     if (!secp256k1_ec_pubkey_parse($this->ecAdapter->getContext(), $binary, $pubkey_t)) {
         throw new \RuntimeException('Secp256k1 failed to parse public key');
     }
     return new PublicKey($this->ecAdapter, $pubkey_t, $buffer->getSize() === 33);
 }
 /**
  * @param $publicKey
  * @param $tweak
  * @param $expectedPublicKey
  * @param $eMul
  */
 private function genericTest($context, $publicKey, $tweak, $expectedPublicKey, $eMul, $compressed)
 {
     $publicKey = $this->toBinary32($publicKey);
     $tweak = $this->toBinary32($tweak);
     $p = '';
     secp256k1_ec_pubkey_parse($context, $publicKey, $p);
     $result = secp256k1_ec_pubkey_tweak_mul($context, $p, $tweak);
     $this->assertEquals($eMul, $result);
     $ser = '';
     secp256k1_ec_pubkey_serialize($context, $p, $compressed, $ser);
     $this->assertEquals($expectedPublicKey, bin2hex($ser));
 }
 /**
  * @param $publicKey
  * @param $tweak
  * @param $expectedPublicKey
  * @param $eAdd
  */
 private function genericTest($context, $publicKey, $tweak, $expectedPublicKey, $eAdd, $compressed)
 {
     $publicKey = $this->toBinary32($publicKey);
     /** @var resource $p */
     $p = '';
     secp256k1_ec_pubkey_parse($context, $p, $publicKey);
     $tweak = $this->toBinary32($tweak);
     $result = secp256k1_ec_pubkey_tweak_add($context, $p, $tweak);
     $this->assertEquals($eAdd, $result);
     $pSer = '';
     secp256k1_ec_pubkey_serialize($context, $pSer, $p, $compressed);
     $this->assertEquals(bin2hex($pSer), $expectedPublicKey);
 }
Exemple #6
0
 /**
  * @return resource
  * @throws \Exception
  */
 private function clonePubkey()
 {
     $context = $this->ecAdapter->getContext();
     /** @var resource $serialized */
     $serialized = '';
     if (1 !== secp256k1_ec_pubkey_serialize($context, $this->pubkey_t, $this->compressed, $serialized)) {
         throw new \Exception('Secp256k1: pubkey serialize');
     }
     /** @var resource $clone */
     $clone = '';
     if (1 !== secp256k1_ec_pubkey_parse($context, $serialized, $clone)) {
         throw new \Exception('Secp256k1 pubkey parse');
     }
     return $clone;
 }
Exemple #7
0
<?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);
}