Skip to content

Commit eac8337

Browse files
committed
full test coverage of xonly tweak functions
1 parent 77bde8a commit eac8337

21 files changed

+632
-9
lines changed

secp256k1/tests/secp256k1_xonly_privkey_tweak_add_basic.phpt

-9
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ $ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN|SECP256K1_CONTEXT_VERIFY)
1111

1212
$pubkey1 = null;
1313
$pubkey2 = null;
14-
$pubKey1Out = null;
15-
$pubKey2Out = null;
16-
$tweakedPub = null;
1714
$tweakTwo = str_repeat("\x00", 31) . "\x02";
1815
$privKey1 = str_repeat("\x42", 32);
1916
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
2017
$privKey2 = str_repeat("\x42", 31) . "\x44";
2118

22-
$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
23-
echo $result . PHP_EOL;
24-
echo get_resource_type($pubkey1) . PHP_EOL;
25-
2619
$result = secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweakTwo);
2720
echo $result . PHP_EOL;
2821

@@ -35,6 +28,4 @@ if ($privKey1 === $privKey2) {
3528
?>
3629
--EXPECT--
3730
1
38-
secp256k1_xonly_pubkey
39-
1
4031
private keys equal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
secp256k1_xonly_privkey_tweak_add returns false with warning if parameter parsing fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
11+
12+
$result = secp256k1_xonly_privkey_tweak_add();
13+
echo $result . PHP_EOL;
14+
15+
?>
16+
--EXPECT--
17+
secp256k1_xonly_privkey_tweak_add() expects exactly 3 parameters, 0 given
18+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
secp256k1_xonly_privkey_tweak_add returns false with warning if context resource is wrong type
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+
$tweakTwo = str_repeat("\x00", 31) . "\x02";
15+
$privKey1 = str_repeat("\x42", 32);
16+
//02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619
17+
18+
$badCtx = tmpfile();
19+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
20+
$result = secp256k1_xonly_privkey_tweak_add($badCtx, $privKey1, $tweakTwo);
21+
echo $result . PHP_EOL;
22+
23+
?>
24+
--EXPECT--
25+
secp256k1_xonly_privkey_tweak_add(): supplied resource is not a valid secp256k1_context resource
26+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
secp256k1_xonly_privkey_tweak_add throws if seckey is wrong length
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+
$tweak32 = str_repeat("\x02", 32);
13+
$privKey1 = str_repeat("\x42", 31);
14+
$expecting = "secp256k1_xonly_privkey_tweak_add(): Parameter 2 should be 32 bytes";
15+
16+
try {
17+
secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweak32);
18+
} catch (\Exception $e) {
19+
if ($e->getMessage() !== $expecting) {
20+
echo "ERROR\n";
21+
}
22+
echo $e->getMessage() . "\n";
23+
}
24+
25+
?>
26+
--EXPECT--
27+
secp256k1_xonly_privkey_tweak_add(): Parameter 2 should be 32 bytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
secp256k1_xonly_privkey_tweak_add throws exception if tweak32 is not 32 bytes
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+
$tweakEmpty = "";
15+
$tweak31 = str_repeat("A", 31);
16+
$tweak33 = str_repeat("A", 31);
17+
18+
$privKey1 = str_repeat("\x42", 32);
19+
20+
$expecting = "secp256k1_xonly_privkey_tweak_add(): Parameter 3 should be 32 bytes";
21+
22+
try {
23+
secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweakEmpty);
24+
} catch (\Exception $e) {
25+
if ($e->getMessage() !== $expecting) {
26+
echo "ERROR\n";
27+
}
28+
echo $e->getMessage() . "\n";
29+
}
30+
31+
try {
32+
secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweak31);
33+
} catch (\Exception $e) {
34+
if ($e->getMessage() !== $expecting) {
35+
echo "ERROR\n";
36+
}
37+
echo $e->getMessage() . "\n";
38+
}
39+
40+
try {
41+
secp256k1_xonly_privkey_tweak_add($ctx, $privKey1, $tweak33);
42+
} catch (\Exception $e) {
43+
if ($e->getMessage() !== $expecting) {
44+
echo "ERROR\n";
45+
}
46+
echo $e->getMessage() . "\n";
47+
}
48+
49+
?>
50+
--EXPECT--
51+
secp256k1_xonly_privkey_tweak_add(): Parameter 3 should be 32 bytes
52+
secp256k1_xonly_privkey_tweak_add(): Parameter 3 should be 32 bytes
53+
secp256k1_xonly_privkey_tweak_add(): Parameter 3 should be 32 bytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_from_pubkey returns false with warning if parameter parsing fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
11+
12+
$result = secp256k1_xonly_pubkey_from_pubkey();
13+
echo $result . PHP_EOL;
14+
15+
?>
16+
--EXPECT--
17+
secp256k1_xonly_pubkey_from_pubkey() expects exactly 4 parameters, 0 given
18+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_from_pubkey returns false with warning if context is wrong resource type
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+
$badCtx = tmpfile();
24+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
25+
$result = secp256k1_xonly_pubkey_from_pubkey($badCtx, $xonlyPubKey, $sign, $pubkey);
26+
echo $result . PHP_EOL;
27+
28+
?>
29+
--EXPECT--
30+
1
31+
secp256k1_pubkey
32+
secp256k1_xonly_pubkey_from_pubkey(): supplied resource is not a valid secp256k1_context resource
33+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_from_pubkey returns false with warning if pubkey is wrong resource type
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+
$badPubKey = tmpfile();
16+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
17+
$result = secp256k1_xonly_pubkey_from_pubkey($ctx, $xonlyPubKey, $sign, $badPubKey);
18+
echo $result . PHP_EOL;
19+
20+
?>
21+
--EXPECT--
22+
secp256k1_xonly_pubkey_from_pubkey(): supplied resource is not a valid secp256k1_pubkey resource
23+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_to_pubkey returns false with warning if parameter parsing fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
11+
12+
$result = secp256k1_xonly_pubkey_to_pubkey();
13+
echo $result . PHP_EOL;
14+
15+
?>
16+
--EXPECT--
17+
secp256k1_xonly_pubkey_to_pubkey() expects exactly 4 parameters, 0 given
18+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_to_pubkey returns false with error if context is wrong resource type
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+
$badCtx = tmpfile();
22+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
23+
$result = secp256k1_xonly_pubkey_to_pubkey($badCtx, $pubkey, $xonlyPubKey, $sign);
24+
echo $result . PHP_EOL;
25+
26+
?>
27+
--EXPECT--
28+
1
29+
secp256k1_xonly_pubkey
30+
secp256k1_xonly_pubkey_to_pubkey(): supplied resource is not a valid secp256k1_context resource
31+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
$sign = 1;
13+
$pubkey = null;
14+
$badKey = tmpfile();
15+
16+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
17+
$result = secp256k1_xonly_pubkey_to_pubkey($ctx, $pubkey, $badKey, $sign);
18+
echo $result . PHP_EOL;
19+
20+
?>
21+
--EXPECT--
22+
secp256k1_xonly_pubkey_to_pubkey(): supplied resource is not a valid secp256k1_xonly_pubkey resource
23+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_tweak_add warns if parameter parsing fails
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
11+
12+
$result = secp256k1_xonly_pubkey_tweak_add();
13+
echo $result . PHP_EOL;
14+
15+
?>
16+
--EXPECT--
17+
secp256k1_xonly_pubkey_tweak_add() expects exactly 4 parameters, 0 given
18+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
secp256k1_xonly_pubkey_tweak_add fails with warning if context is wrong resource type
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+
$privKey1 = str_repeat("\x42", 32);
13+
14+
$pubkey1 = null;
15+
$tweakedPub = null;
16+
$tweakTwo = str_repeat("\x00", 31) . "\x02";
17+
$privKey2 = str_repeat("\x42", 31) . "\x44";
18+
19+
$result = secp256k1_xonly_pubkey_create($ctx, $pubkey1, $privKey1);
20+
echo $result . PHP_EOL;
21+
echo get_resource_type($pubkey1) . PHP_EOL;
22+
23+
$badCtx = tmpfile();
24+
25+
set_error_handler(function($code, $str) { echo $str . PHP_EOL; });
26+
27+
$result = secp256k1_xonly_pubkey_tweak_add($badCtx, $tweakedPub, $pubkey1, $tweakTwo);
28+
echo $result . PHP_EOL;
29+
30+
?>
31+
--EXPECT--
32+
1
33+
secp256k1_xonly_pubkey
34+
secp256k1_xonly_pubkey_tweak_add(): supplied resource is not a valid secp256k1_context resource
35+
0

0 commit comments

Comments
 (0)