Skip to content

Commit 00102ec

Browse files
committed
basic tests for xonly_[pubkey|privkey]_tweak_add, and xonly_pubkey_tweak_verify
1 parent 5ac425e commit 00102ec

9 files changed

+329
-2
lines changed

secp256k1/secp256k1.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_add)
20312031
zend_string *zTweak;
20322032
int result;
20332033

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

@@ -2069,7 +2069,7 @@ PHP_FUNCTION(secp256k1_xonly_pubkey_tweak_verify)
20692069
zend_string *zTweak32;
20702070
int result;
20712071

2072-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak32) == FAILURE) {
2072+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrrS", &zCtx, &zOutputPubKey, &zInternalPubKey, &zTweak32) == FAILURE) {
20732073
RETURN_LONG(0);
20742074
}
20752075

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
secp256k1_xonly_privkey_tweak_add works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY);
11+
12+
$pubkey1 = null;
13+
$pubkey2 = null;
14+
$pubKey1Out = null;
15+
$pubKey2Out = null;
16+
$tweakedPub = null;
17+
$tweakTwo = str_repeat("\x00", 31) . "\x02";
18+
$privKey1 = str_repeat("\x42", 32);
19+
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
20+
$privKey2 = str_repeat("\x42", 31) . "\x44";
21+
22+
$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
23+
echo $result . PHP_EOL;
24+
echo get_resource_type($pubkey1) . PHP_EOL;
25+
26+
$result = secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweakTwo);
27+
echo $result . PHP_EOL;
28+
29+
if ($privKey1 === $privKey2) {
30+
echo "private keys equal";
31+
} else {
32+
echo bin2hex($privKey1)." !== ".bin2hex($privKey2)."\n";
33+
}
34+
35+
?>
36+
--EXPECT--
37+
1
38+
secp256k1_xonly_pubkey
39+
1
40+
private keys equal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_from_pubkey works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
11+
12+
$privKey = str_repeat("\x41", 32);
13+
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
14+
15+
$pubkey = null;
16+
$xonlyPubKey = null;
17+
$sign = null;
18+
$pubkey2 = null;
19+
$result = secp256k1_ec_pubkey_create($ctx, $pubkey, $privKey);
20+
echo $result . PHP_EOL;
21+
echo get_resource_type($pubkey) . PHP_EOL;
22+
23+
$result = secp256k1_xonly_pubkey_from_pubkey($ctx, $xonlyPubKey, $sign, $pubkey);
24+
echo $result . PHP_EOL;
25+
26+
echo "sign: $sign\n";
27+
28+
$serialized = null;
29+
$result = secp256k1_xonly_pubkey_serialize($ctx, $serialized, $xonlyPubKey);
30+
echo $result . PHP_EOL;
31+
32+
echo bin2hex($serialized) . PHP_EOL;
33+
34+
?>
35+
--EXPECT--
36+
1
37+
secp256k1_pubkey
38+
1
39+
sign: 1
40+
1
41+
eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_from_pubkey works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
11+
12+
$privKey = str_repeat("\x42", 32);
13+
//0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c
14+
15+
$pubkey = null;
16+
$xonlyPubKey = null;
17+
$sign = null;
18+
$pubkey2 = null;
19+
$result = secp256k1_ec_pubkey_create($ctx, $pubkey, $privKey);
20+
echo $result . PHP_EOL;
21+
echo get_resource_type($pubkey) . PHP_EOL;
22+
23+
$result = secp256k1_xonly_pubkey_from_pubkey($ctx, $xonlyPubKey, $sign, $pubkey);
24+
echo $result . PHP_EOL;
25+
26+
echo "sign: $sign\n";
27+
28+
$serialized = null;
29+
$result = secp256k1_xonly_pubkey_serialize($ctx, $serialized, $xonlyPubKey);
30+
echo $result . PHP_EOL;
31+
32+
echo bin2hex($serialized) . PHP_EOL;
33+
34+
?>
35+
--EXPECT--
36+
1
37+
secp256k1_pubkey
38+
1
39+
sign: 0
40+
1
41+
24653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_to_pubkey works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
11+
12+
$pubKeyIn = hex2bin("eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619");
13+
$sign = 1;
14+
$pubkey = null;
15+
$xonlyPubKey = null;
16+
17+
$result = secp256k1_xonly_pubkey_parse($ctx, $xonlyPubKey, $pubKeyIn);
18+
echo $result . PHP_EOL;
19+
echo get_resource_type($xonlyPubKey) . PHP_EOL;
20+
21+
$result = secp256k1_xonly_pubkey_to_pubkey($ctx, $pubkey, $xonlyPubKey, $sign);
22+
echo $result . PHP_EOL;
23+
24+
$serialized = null;
25+
$result = secp256k1_ec_pubkey_serialize($ctx, $serialized, $pubkey, SECP256K1_EC_COMPRESSED);
26+
echo $result . PHP_EOL;
27+
28+
echo bin2hex($serialized) . PHP_EOL;
29+
30+
?>
31+
--EXPECT--
32+
1
33+
secp256k1_xonly_pubkey
34+
1
35+
1
36+
02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_to_pubkey works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
11+
12+
$pubKeyIn = hex2bin("24653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c");
13+
$sign = 0;
14+
$pubkey = null;
15+
$xonlyPubKey = null;
16+
17+
$result = secp256k1_xonly_pubkey_parse($ctx, $xonlyPubKey, $pubKeyIn);
18+
echo $result . PHP_EOL;
19+
echo get_resource_type($xonlyPubKey) . PHP_EOL;
20+
21+
$result = secp256k1_xonly_pubkey_to_pubkey($ctx, $pubkey, $xonlyPubKey, $sign);
22+
echo $result . PHP_EOL;
23+
24+
$serialized = null;
25+
$result = secp256k1_ec_pubkey_serialize($ctx, $serialized, $pubkey, SECP256K1_EC_COMPRESSED);
26+
echo $result . PHP_EOL;
27+
28+
echo bin2hex($serialized) . PHP_EOL;
29+
30+
?>
31+
--EXPECT--
32+
1
33+
secp256k1_xonly_pubkey
34+
1
35+
1
36+
0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_tweak_add works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY);
11+
12+
$pubkey1 = null;
13+
$pubkey2 = null;
14+
$pubKey1Out = null;
15+
$pubKey2Out = null;
16+
$tweakedPub = null;
17+
$tweakTwo = str_repeat("\x00", 31) . "\x02";
18+
$privKey1 = str_repeat("\x42", 32);
19+
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
20+
$privKey2 = str_repeat("\x42", 31) . "\x44";
21+
22+
$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
23+
echo $result . PHP_EOL;
24+
echo get_resource_type($pubkey1) . PHP_EOL;
25+
26+
$result = secp256k1_xonly_pubkey_create($ctx, $pubkey2, $privKey2);
27+
echo $result . PHP_EOL;
28+
echo get_resource_type($pubkey2) . PHP_EOL;
29+
30+
31+
$result = secp256k1_xonly_pubkey_tweak_add($ctx, $tweakedPub, $pubkey1, $tweakTwo);
32+
echo $result . PHP_EOL;
33+
34+
$result = secp256k1_ec_pubkey_serialize($ctx, $pubKey1Out, $tweakedPub, SECP256K1_EC_COMPRESSED);
35+
echo $result . PHP_EOL;
36+
37+
$result = secp256k1_xonly_pubkey_serialize($ctx, $pubKey2Out, $pubkey2);
38+
echo $result . PHP_EOL;
39+
40+
if (substr($pubKey1Out, 1) === $pubKey2Out) {
41+
echo "public keys equal";
42+
} else {
43+
echo bin2hex($pubKey1Out)." !== ".bin2hex($pubKey2Out)."\n";
44+
}
45+
46+
?>
47+
--EXPECT--
48+
1
49+
secp256k1_xonly_pubkey
50+
1
51+
secp256k1_xonly_pubkey
52+
1
53+
1
54+
1
55+
public keys equal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_tweak_verify works
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY);
11+
12+
$pubkey1 = null;
13+
$pubKey1Out = null;
14+
$tweakedPub = null;
15+
$tweak = hex2bin("3e10c475efefd59fc14d56706902ef73d5759191065a4c286d4054ed5b6f8258");
16+
$tweakInvalid = hex2bin("1c5bcae8f25cfff40a3cfb29bc59ed97d67e870f60c424aea12ea109696d373a");
17+
$privKey1 = str_repeat("\x42", 32);
18+
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
19+
20+
$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
21+
echo $result . PHP_EOL;
22+
echo get_resource_type($pubkey1) . PHP_EOL;
23+
24+
$result = secp256k1_xonly_pubkey_tweak_add($ctx, $tweakedPub, $pubkey1, $tweak);
25+
echo $result . PHP_EOL;
26+
27+
$result = secp256k1_xonly_pubkey_tweak_verify($ctx, $tweakedPub, $pubkey1, $tweakInvalid);
28+
echo $result.PHP_EOL;
29+
30+
$result = secp256k1_xonly_pubkey_tweak_verify($ctx, $tweakedPub, $pubkey1, $tweak);
31+
echo $result.PHP_EOL;
32+
33+
?>
34+
--EXPECT--
35+
1
36+
secp256k1_xonly_pubkey
37+
1
38+
0
39+
1

stubs/functions.php

+39
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,45 @@ function secp256k1_xonly_pubkey_parse($context, &$pubkey, string $input32): int
324324
* @return int
325325
*/
326326
function secp256k1_xonly_pubkey_serialize($context, ?string &$output32, $pubkey): int {}
327+
/**
328+
* @param resource $context
329+
* @param resource|null $xonly_pubkey
330+
* @param int|null $sign
331+
* @param resource $pubkey
332+
* @return int
333+
*/
334+
function secp256k1_xonly_pubkey_from_pubkey($context, &$xonly_pubkey, ?int &$sign, $pubkey): int {}
335+
/**
336+
* @param resource $context
337+
* @param resource|null $pubkey
338+
* @param resource $xonly_pubkey
339+
* @param int $sign
340+
* @return int
341+
*/
342+
function secp256k1_xonly_pubkey_to_pubkey($context, &$pubkey, $xonly_pubkey, int $sign): int {}
343+
/**
344+
* @param resource $context
345+
* @param string $seckey
346+
* @param string $tweak32
347+
* @return int
348+
*/
349+
function secp256k1_xonly_privkey_tweak_add($context, string &$seckey, string $tweak32): int {}
350+
/**
351+
* @param resource $context
352+
* @param resource|null $outputPubKey
353+
* @param resource $internalPubKey
354+
* @param string $tweak32
355+
* @return int
356+
*/
357+
function secp256k1_xonly_pubkey_tweak_add($context, &$outputPubKey, $internalPubKey, string $tweak32): int {}
358+
/**
359+
* @param resource $context
360+
* @param resource $outputPubKey
361+
* @param resource $internalPubKey
362+
* @param string $tweak32
363+
* @return int
364+
*/
365+
function secp256k1_xonly_pubkey_tweak_verify($context, $outputPubKey, $internalPubKey, string $tweak32): int {}
327366
/**
328367
* Parse a compact ECDSA signature (64 bytes + recovery id).
329368
*

0 commit comments

Comments
 (0)