Skip to content

Commit

Permalink
basic tests for xonly_[pubkey|privkey]_tweak_add, and xonly_pubkey_tw…
Browse files Browse the repository at this point in the history
…eak_verify
  • Loading branch information
afk11 committed Oct 10, 2019
1 parent 5ac425e commit 00102ec
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 2 deletions.
4 changes: 2 additions & 2 deletions secp256k1/secp256k1.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_add)
zend_string *zTweak;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/rS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak) == FAILURE) {
RETURN_LONG(0);
}

Expand Down Expand Up @@ -2069,7 +2069,7 @@ PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_verify)
zend_string *zTweak32;
int result;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak32) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak32) == FAILURE) {
RETURN_LONG(0);
}

Expand Down
40 changes: 40 additions & 0 deletions secp256k1/tests/secp256k1_xonly_privkey_tweak_add_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
secp256k1_xonly_privkey_tweak_add works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY);

$pubkey1 = null;
$pubkey2 = null;
$pubKey1Out = null;
$pubKey2Out = null;
$tweakedPub = null;
$tweakTwo = str_repeat("\x00", 31) . "\x02";
$privKey1 = str_repeat("\x42", 32);
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
$privKey2 = str_repeat("\x42", 31) . "\x44";

$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
echo $result . PHP_EOL;
echo get_resource_type($pubkey1) . PHP_EOL;

$result = secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweakTwo);
echo $result . PHP_EOL;

if ($privKey1 === $privKey2) {
echo "private keys equal";
} else {
echo bin2hex($privKey1)." !== ".bin2hex($privKey2)."\n";
}

?>
--EXPECT--
1
secp256k1_xonly_pubkey
1
private keys equal
41 changes: 41 additions & 0 deletions secp256k1/tests/secp256k1_xonly_pubkey_from_pubkey_basic1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
secp256k1_xonly_pubkey_from_pubkey works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);

$privKey = str_repeat("\x41", 32);
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619

$pubkey = null;
$xonlyPubKey = null;
$sign = null;
$pubkey2 = null;
$result = secp256k1_ec_pubkey_create($ctx, $pubkey, $privKey);
echo $result . PHP_EOL;
echo get_resource_type($pubkey) . PHP_EOL;

$result = secp256k1_xonly_pubkey_from_pubkey($ctx, $xonlyPubKey, $sign, $pubkey);
echo $result . PHP_EOL;

echo "sign: $sign\n";

$serialized = null;
$result = secp256k1_xonly_pubkey_serialize($ctx, $serialized, $xonlyPubKey);
echo $result . PHP_EOL;

echo bin2hex($serialized) . PHP_EOL;

?>
--EXPECT--
1
secp256k1_pubkey
1
sign: 1
1
eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
41 changes: 41 additions & 0 deletions secp256k1/tests/secp256k1_xonly_pubkey_from_pubkey_basic2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
secp256k1_xonly_pubkey_from_pubkey works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);

$privKey = str_repeat("\x42", 32);
//0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c

$pubkey = null;
$xonlyPubKey = null;
$sign = null;
$pubkey2 = null;
$result = secp256k1_ec_pubkey_create($ctx, $pubkey, $privKey);
echo $result . PHP_EOL;
echo get_resource_type($pubkey) . PHP_EOL;

$result = secp256k1_xonly_pubkey_from_pubkey($ctx, $xonlyPubKey, $sign, $pubkey);
echo $result . PHP_EOL;

echo "sign: $sign\n";

$serialized = null;
$result = secp256k1_xonly_pubkey_serialize($ctx, $serialized, $xonlyPubKey);
echo $result . PHP_EOL;

echo bin2hex($serialized) . PHP_EOL;

?>
--EXPECT--
1
secp256k1_pubkey
1
sign: 0
1
24653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c
36 changes: 36 additions & 0 deletions secp256k1/tests/secp256k1_xonly_pubkey_to_pubkey_basic1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
secp256k1_xonly_pubkey_to_pubkey works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);

$pubKeyIn = hex2bin("eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619");
$sign = 1;
$pubkey = null;
$xonlyPubKey = null;

$result = secp256k1_xonly_pubkey_parse($ctx, $xonlyPubKey, $pubKeyIn);
echo $result . PHP_EOL;
echo get_resource_type($xonlyPubKey) . PHP_EOL;

$result = secp256k1_xonly_pubkey_to_pubkey($ctx, $pubkey, $xonlyPubKey, $sign);
echo $result . PHP_EOL;

$serialized = null;
$result = secp256k1_ec_pubkey_serialize($ctx, $serialized, $pubkey, SECP256K1_EC_COMPRESSED);
echo $result . PHP_EOL;

echo bin2hex($serialized) . PHP_EOL;

?>
--EXPECT--
1
secp256k1_xonly_pubkey
1
1
02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
36 changes: 36 additions & 0 deletions secp256k1/tests/secp256k1_xonly_pubkey_to_pubkey_basic2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
secp256k1_xonly_pubkey_to_pubkey works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);

$pubKeyIn = hex2bin("24653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c");
$sign = 0;
$pubkey = null;
$xonlyPubKey = null;

$result = secp256k1_xonly_pubkey_parse($ctx, $xonlyPubKey, $pubKeyIn);
echo $result . PHP_EOL;
echo get_resource_type($xonlyPubKey) . PHP_EOL;

$result = secp256k1_xonly_pubkey_to_pubkey($ctx, $pubkey, $xonlyPubKey, $sign);
echo $result . PHP_EOL;

$serialized = null;
$result = secp256k1_ec_pubkey_serialize($ctx, $serialized, $pubkey, SECP256K1_EC_COMPRESSED);
echo $result . PHP_EOL;

echo bin2hex($serialized) . PHP_EOL;

?>
--EXPECT--
1
secp256k1_xonly_pubkey
1
1
0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c
55 changes: 55 additions & 0 deletions secp256k1/tests/secp256k1_xonly_pubkey_tweak_add_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
secp256k1_xonly_pubkey_tweak_add works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY);

$pubkey1 = null;
$pubkey2 = null;
$pubKey1Out = null;
$pubKey2Out = null;
$tweakedPub = null;
$tweakTwo = str_repeat("\x00", 31) . "\x02";
$privKey1 = str_repeat("\x42", 32);
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
$privKey2 = str_repeat("\x42", 31) . "\x44";

$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
echo $result . PHP_EOL;
echo get_resource_type($pubkey1) . PHP_EOL;

$result = secp256k1_xonly_pubkey_create($ctx, $pubkey2, $privKey2);
echo $result . PHP_EOL;
echo get_resource_type($pubkey2) . PHP_EOL;


$result = secp256k1_xonly_pubkey_tweak_add($ctx, $tweakedPub, $pubkey1, $tweakTwo);
echo $result . PHP_EOL;

$result = secp256k1_ec_pubkey_serialize($ctx, $pubKey1Out, $tweakedPub, SECP256K1_EC_COMPRESSED);
echo $result . PHP_EOL;

$result = secp256k1_xonly_pubkey_serialize($ctx, $pubKey2Out, $pubkey2);
echo $result . PHP_EOL;

if (substr($pubKey1Out, 1) === $pubKey2Out) {
echo "public keys equal";
} else {
echo bin2hex($pubKey1Out)." !== ".bin2hex($pubKey2Out)."\n";
}

?>
--EXPECT--
1
secp256k1_xonly_pubkey
1
secp256k1_xonly_pubkey
1
1
1
public keys equal
39 changes: 39 additions & 0 deletions secp256k1/tests/secp256k1_xonly_pubkey_tweak_verify.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
secp256k1_xonly_pubkey_tweak_verify works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY);

$pubkey1 = null;
$pubKey1Out = null;
$tweakedPub = null;
$tweak = hex2bin("3e10c475efefd59fc14d56706902ef73d5759191065a4c286d4054ed5b6f8258");
$tweakInvalid = hex2bin("1c5bcae8f25cfff40a3cfb29bc59ed97d67e870f60c424aea12ea109696d373a");
$privKey1 = str_repeat("\x42", 32);
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619

$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
echo $result . PHP_EOL;
echo get_resource_type($pubkey1) . PHP_EOL;

$result = secp256k1_xonly_pubkey_tweak_add($ctx, $tweakedPub, $pubkey1, $tweak);
echo $result . PHP_EOL;

$result = secp256k1_xonly_pubkey_tweak_verify($ctx, $tweakedPub, $pubkey1, $tweakInvalid);
echo $result.PHP_EOL;

$result = secp256k1_xonly_pubkey_tweak_verify($ctx, $tweakedPub, $pubkey1, $tweak);
echo $result.PHP_EOL;

?>
--EXPECT--
1
secp256k1_xonly_pubkey
1
0
1
39 changes: 39 additions & 0 deletions stubs/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,45 @@ function secp256k1_xonly_pubkey_parse($context, &$pubkey, string $input32): int
* @return int
*/
function secp256k1_xonly_pubkey_serialize($context, ?string &$output32, $pubkey): int {}
/**
* @param resource $context
* @param resource|null $xonly_pubkey
* @param int|null $sign
* @param resource $pubkey
* @return int
*/
function secp256k1_xonly_pubkey_from_pubkey($context, &$xonly_pubkey, ?int &$sign, $pubkey): int {}
/**
* @param resource $context
* @param resource|null $pubkey
* @param resource $xonly_pubkey
* @param int $sign
* @return int
*/
function secp256k1_xonly_pubkey_to_pubkey($context, &$pubkey, $xonly_pubkey, int $sign): int {}
/**
* @param resource $context
* @param string $seckey
* @param string $tweak32
* @return int
*/
function secp256k1_xonly_privkey_tweak_add($context, string &$seckey, string $tweak32): int {}
/**
* @param resource $context
* @param resource|null $outputPubKey
* @param resource $internalPubKey
* @param string $tweak32
* @return int
*/
function secp256k1_xonly_pubkey_tweak_add($context, &$outputPubKey, $internalPubKey, string $tweak32): int {}
/**
* @param resource $context
* @param resource $outputPubKey
* @param resource $internalPubKey
* @param string $tweak32
* @return int
*/
function secp256k1_xonly_pubkey_tweak_verify($context, $outputPubKey, $internalPubKey, string $tweak32): int {}
/**
* Parse a compact ECDSA signature (64 bytes + recovery id).
*
Expand Down

0 comments on commit 00102ec

Please sign in to comment.