Skip to content

Commit

Permalink
typehints for privkey tweak add and mul
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin authored and afk11 committed Jul 11, 2018
1 parent 89301f9 commit 32f3e05
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 102 deletions.
28 changes: 18 additions & 10 deletions secp256k1/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,14 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_ec_pubkey_negate, IS_LONG
ZEND_ARG_TYPE_INFO(1, ecPublicKey, IS_RESOURCE, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO(arginfo_secp256k1_ec_privkey_tweak_add, 0)
#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_ec_privkey_tweak_add, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_ec_privkey_tweak_add, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_INFO(1, seckey)
ZEND_ARG_INFO(0, tweak32)
ZEND_ARG_TYPE_INFO(1, seckey, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, tweak32, IS_STRING, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO(arginfo_secp256k1_ec_pubkey_tweak_add, 0)
Expand All @@ -191,10 +195,14 @@ ZEND_BEGIN_ARG_INFO(arginfo_secp256k1_ec_pubkey_tweak_add, 0)
ZEND_ARG_INFO(0, tweak32)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO(arginfo_secp256k1_ec_privkey_tweak_mul, 0)
#if (PHP_VERSION_ID >= 70000 && PHP_VERSION_ID <= 70200)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_ec_privkey_tweak_mul, IS_LONG, NULL, 0)
#else
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(arginfo_secp256k1_ec_privkey_tweak_mul, IS_LONG, 0)
#endif
ZEND_ARG_TYPE_INFO(0, context, IS_RESOURCE, 0)
ZEND_ARG_INFO(1, seckey)
ZEND_ARG_INFO(0, tweak32)
ZEND_ARG_TYPE_INFO(1, seckey, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, tweak32, IS_STRING, 0)
ZEND_END_ARG_INFO();

ZEND_BEGIN_ARG_INFO(arginfo_secp256k1_ec_pubkey_tweak_mul, 0)
Expand Down Expand Up @@ -999,11 +1007,11 @@ PHP_FUNCTION(secp256k1_ec_privkey_tweak_add)
int result = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/S", &zCtx, &zSecKey, &zTweak) == FAILURE) {
RETURN_FALSE;
RETURN_LONG(result);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_FALSE;
RETURN_LONG(result);
}

if (Z_TYPE_P(zSecKey) != IS_STRING) {
Expand Down Expand Up @@ -1074,11 +1082,11 @@ PHP_FUNCTION(secp256k1_ec_privkey_tweak_mul)
int result = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/S", &zCtx, &zSecKey, &zTweak) == FAILURE) {
RETURN_FALSE;
RETURN_LONG(result);
}

if ((ctx = php_get_secp256k1_context(zCtx)) == NULL) {
RETURN_FALSE;
RETURN_LONG(result);
}

if (Z_TYPE_P(zSecKey) != IS_STRING) {
Expand Down
6 changes: 2 additions & 4 deletions secp256k1/tests/secp256k1_ec_privkey_tweak_add_error2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ $keyTweak = str_repeat("A", 32);

$context = tmpfile();
$result = secp256k1_ec_privkey_tweak_add($context, $key, $keyTweak);
echo gettype($result) . PHP_EOL;
echo ($result ? "true" : "false") . PHP_EOL;
echo $result . PHP_EOL;

?>
--EXPECT--
secp256k1_ec_privkey_tweak_add(): supplied resource is not a valid secp256k1_context resource
boolean
false
0
6 changes: 2 additions & 4 deletions secp256k1/tests/secp256k1_ec_privkey_tweak_add_error3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ $key = str_repeat("A", 32);
$keyTweak = str_repeat("A", 32);

$result = secp256k1_ec_privkey_tweak_add();
echo gettype($result) . PHP_EOL;
echo ($result ? "true" : "false") . PHP_EOL;
echo $result . PHP_EOL;

?>
--EXPECT--
secp256k1_ec_privkey_tweak_add() expects exactly 3 parameters, 0 given
boolean
false
0
2 changes: 1 addition & 1 deletion secp256k1/tests/secp256k1_ec_privkey_tweak_add_error4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ try {

?>
--EXPECT--
secp256k1_ec_privkey_tweak_add(): Parameter 2 should be string
secp256k1_ec_privkey_tweak_add(): Parameter 2 should be 32 bytes
7 changes: 2 additions & 5 deletions secp256k1/tests/secp256k1_ec_privkey_tweak_mul_error2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ $keyTweak = str_repeat("A", 32);

$context = tmpfile();
$result = secp256k1_ec_privkey_tweak_mul($context, $key, $keyTweak);
echo gettype($result) . PHP_EOL;
echo ($result ? "true" : "false") . PHP_EOL;

echo $result . PHP_EOL;
?>
--EXPECT--
secp256k1_ec_privkey_tweak_mul(): supplied resource is not a valid secp256k1_context resource
boolean
false
0
6 changes: 2 additions & 4 deletions secp256k1/tests/secp256k1_ec_privkey_tweak_mul_error3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ if (!extension_loaded("secp256k1")) print "skip extension not loaded";
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$result = secp256k1_ec_privkey_tweak_mul();
echo gettype($result) . PHP_EOL;
echo ($result ? "true" : "false") . PHP_EOL;
echo $result . PHP_EOL;

?>
--EXPECT--
secp256k1_ec_privkey_tweak_mul() expects exactly 3 parameters, 0 given
boolean
false
0
4 changes: 1 addition & 3 deletions secp256k1/tests/secp256k1_ec_privkey_tweak_mul_error4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ if (!extension_loaded("secp256k1")) print "skip extension not loaded";
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);

$secKeyFirst = 1;
Expand All @@ -22,4 +20,4 @@ try {

?>
--EXPECT--
secp256k1_ec_privkey_tweak_mul(): Parameter 2 should be string
secp256k1_ec_privkey_tweak_mul(): Parameter 2 should be 32 bytes
36 changes: 0 additions & 36 deletions tests/Secp256k1PrivkeyTweakAddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,4 @@ public function testTweaksPrivateKeyAdd($context, $privkey, $tweak, $expectedTwe
$this->assertEquals($privkey, $expectedTweaked);

}

public function getErroneousTypeVectors()
{
$context = TestCase::getContext();
$tweak = $this->pack('0af79b2b747548d59a4a765fb73a72bc4208d00b43d0606c13d332d5c284b0ef');
$privateKey = $this->pack('0af79b2b747548d59a4a765fb73a72bc4208d00b43d0606c13d332d5c284b0ef');

$array = array();
$class = new self;
$resource = openssl_pkey_new();

return array(
array($context, $privateKey, $array),
array($context, $privateKey, $resource),
array($context, $privateKey, $class)
);
}

/**
* @dataProvider getErroneousTypeVectors
* @expectedException \PHPUnit\Framework\Error\Warning
*/
public function testErroneousTypes($context, $seckey, $tweak)
{
$r = \secp256k1_ec_privkey_tweak_add($context, $seckey, $tweak);
}/**/

/**
* @expectedException \Exception
*/
public function testEnforceZvalString()
{
$tweak = $this->pack('0af79b2b747548d59a4a765fb73a72bc4208d00b43d0606c13d332d5c284b0ef');
$privateKey = array();
\secp256k1_ec_privkey_tweak_add(TestCase::getContext(), $privateKey, $tweak);
}
}
35 changes: 0 additions & 35 deletions tests/Secp256k1PrivkeyTweakMulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,5 @@ public function testTweaksPrivatekeyMul($context, $privkey, $tweak, $expectedTwe
$result = secp256k1_ec_privkey_tweak_mul($context, $privkey, $tweak);
$this->assertEquals(1, $result);
$this->assertEquals($privkey, $expectedTweaked);

}


public function getErroneousTypeVectors()
{
$context = TestCase::getContext();
$tweak = $this->pack('0af79b2b747548d59a4a765fb73a72bc4208d00b43d0606c13d332d5c284b0ef');
$privateKey = $this->pack('0af79b2b747548d59a4a765fb73a72bc4208d00b43d0606c13d332d5c284b0ef');
$array = array();
$class = new self;
$resource = openssl_pkey_new();
return array(
// We only test the second parameter here, first is a Zval
array($context, $privateKey, $array),
array($context, $privateKey, $resource),
array($context, $privateKey, $class)
);
}
/**
* @dataProvider getErroneousTypeVectors
* @expectedException \PHPUnit\Framework\Error\Warning
*/
public function testErroneousTypes($context, $seckey, $tweak)
{
$r = \secp256k1_ec_privkey_tweak_mul($context, $seckey, $tweak);
}/**/
/**
* @expectedException \Exception
*/
public function testEnforceZvalString()
{
$tweak = $this->pack('0af79b2b747548d59a4a765fb73a72bc4208d00b43d0606c13d332d5c284b0ef');
$privateKey = array();
\secp256k1_ec_privkey_tweak_mul(TestCase::getContext(), $privateKey, $tweak);
}
}

0 comments on commit 32f3e05

Please sign in to comment.