Skip to content

Commit 9168aab

Browse files
committed
Fix ristretto255 tests
Both tests were skipped because of a typo in the checked constant name. The scalarmult test was using illegal test vectors. The new test is based on: https://github.com/jedisct1/libsodium/blob/6d566070b48efd2fa099bbe9822914455150aba9/test/default/scalarmult_ristretto255.c The $L value contained one extra null byte. The number of "false" return values was too small. scalar_invert() doesn't return a valid point -- not sure on that one.
1 parent 5e997ec commit 9168aab

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

ext/sodium/libsodium.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ PHP_FUNCTION(sodium_crypto_scalarmult_ristretto255)
25882588
zend_throw_exception(sodium_exception_ce, "internal error", 0);
25892589
RETURN_THROWS();
25902590
}
2591-
ZSTR_VAL(q)[crypto_scalarmult_BYTES] = 0;
2591+
ZSTR_VAL(q)[crypto_scalarmult_ristretto255_BYTES] = 0;
25922592

25932593
RETURN_NEW_STR(q);
25942594
}

ext/sodium/tests/crypto_core_ristretto255.phpt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
--TEST--
2-
Check for libsodium scalarmult ristretto255
2+
Check for libsodium core ristretto255
33
--EXTENSIONS--
44
sodium
55
--SKIPIF--
66
<?php
7-
if (!defined('SODIUM_CRYPTO_SCALARMULT_RISTRETTO255_HASHBYTES')) print "skip libsodium without Ristretto255";
7+
if (!defined('SODIUM_CRYPTO_CORE_RISTRETTO255_HASHBYTES')) print "skip libsodium without Ristretto255";
88
?>
99
--FILE--
1010
<?php
@@ -69,11 +69,17 @@ $s0 = sodium_crypto_scalarmult_ristretto255_base($r);
6969
var_dump(sodium_crypto_core_ristretto255_is_valid_point($s0));
7070

7171
// Test that multiplying by the order of the curve fails:
72-
$L = "\xed\xd3\xf5\x5c\x1a\x63\x12\x58\xd6\x9c\xf7\xa2\xde\xf9\xde\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10";
72+
$L = "\xed\xd3\xf5\x5c\x1a\x63\x12\x58" .
73+
"\xd6\x9c\xf7\xa2\xde\xf9\xde\x14" .
74+
"\x00\x00\x00\x00\x00\x00\x00\x00" .
75+
"\x00\x00\x00\x00\x00\x00\x00\x10";
7376

7477
$s = sodium_crypto_core_ristretto255_random();
75-
$multL = sodium_crypto_scalarmult_ristretto255($s, $L);
76-
var_dump(sodium_crypto_core_ristretto255_is_valid_point($multL));
78+
try {
79+
$multL = sodium_crypto_scalarmult_ristretto255($s, $L);
80+
} catch (SodiumException $e) {
81+
echo $e->getMessage(), "\n";
82+
}
7783
$s2 = sodium_crypto_scalarmult_ristretto255($r, $s);
7884

7985
// _from_hash should produce a valid point
@@ -108,7 +114,7 @@ bool(false)
108114
bool(false)
109115
string(64) "3066f82a1a747d45120d1740f14358531a8f04bbffe6a819f86dfe50f44a0a46"
110116
bool(true)
111-
bool(false)
117+
internal error
112118
bool(true)
113119
bool(true)
114120
bool(true)

ext/sodium/tests/crypto_scalarmult_ristretto255.phpt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,46 @@ Check for libsodium scalarmult ristretto255
44
sodium
55
--SKIPIF--
66
<?php
7-
if (!defined('SODIUM_CRYPTO_SCALARMULT_RISTRETTO255_HASHBYTES')) print "skip libsodium without Ristretto255";
7+
if (!defined('SODIUM_CRYPTO_CORE_RISTRETTO255_HASHBYTES')) print "skip libsodium without Ristretto255";
88
?>
99
--FILE--
1010
<?php
11-
$n = sodium_hex2bin("94938bc8631c7d760f6a8b9d9c9c07569e65d9cf79dc809221186205fea3ec05");
12-
$p = sodium_hex2bin("edf2014b8a2ca9ec18e3ba4600c3c9c48d38acebba01601ad7b104a492035b06");
13-
$q = sodium_crypto_scalarmult_ristretto255($n, $p);
14-
$q2 = sodium_crypto_scalarmult_ristretto255_base($n);
11+
$b = sodium_hex2bin("e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76");
12+
$n = str_repeat("\0", SODIUM_CRYPTO_SCALARMULT_RISTRETTO255_SCALARBYTES);
13+
for ($i = 0; $i < 16; $i++, sodium_increment($n)) {
14+
try {
15+
$p = sodium_crypto_scalarmult_ristretto255_base($n);
16+
$p2 = sodium_crypto_scalarmult_ristretto255($n, $b);
17+
} catch (SodiumException $ex) {
18+
echo $ex->getMessage(), "\n";
19+
continue;
20+
}
21+
var_dump(sodium_bin2hex($p));
22+
assert($p === $p2);
23+
}
1524

16-
var_dump(sodium_bin2hex($q));
17-
var_dump(sodium_bin2hex($q2));
1825
try {
1926
sodium_crypto_scalarmult(substr($n, 1), $p);
2027
} catch (SodiumException $ex) {
21-
var_dump(true);
28+
echo $ex->getMessage(), "\n";
2229
}
30+
2331
?>
2432
--EXPECT--
25-
string(64) "2a684afd8de19c6964fffd28509294e2752fdbb79e13a58dec3aff51de65505e"
26-
string(64) "e08ec8d22c0901c1746da3844857e9bc25b77cfe14a412e7bcd2b4017aff0556"
27-
bool(true)
33+
internal error
34+
string(64) "e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76"
35+
string(64) "6a493210f7499cd17fecb510ae0cea23a110e8d5b901f8acadd3095c73a3b919"
36+
string(64) "94741f5d5d52755ece4f23f044ee27d5d1ea1e2bd196b462166b16152a9d0259"
37+
string(64) "da80862773358b466ffadfe0b3293ab3d9fd53c5ea6c955358f568322daf6a57"
38+
string(64) "e882b131016b52c1d3337080187cf768423efccbb517bb495ab812c4160ff44e"
39+
string(64) "f64746d3c92b13050ed8d80236a7f0007c3b3f962f5ba793d19a601ebb1df403"
40+
string(64) "44f53520926ec81fbd5a387845beb7df85a96a24ece18738bdcfa6a7822a176d"
41+
string(64) "903293d8f2287ebe10e2374dc1a53e0bc887e592699f02d077d5263cdd55601c"
42+
string(64) "02622ace8f7303a31cafc63f8fc48fdc16e1c8c8d234b2f0d6685282a9076031"
43+
string(64) "20706fd788b2720a1ed2a5dad4952b01f413bcf0e7564de8cdc816689e2db95f"
44+
string(64) "bce83f8ba5dd2fa572864c24ba1810f9522bc6004afe95877ac73241cafdab42"
45+
string(64) "e4549ee16b9aa03099ca208c67adafcafa4c3f3e4e5303de6026e3ca8ff84460"
46+
string(64) "aa52e000df2e16f55fb1032fc33bc42742dad6bd5a8fc0be0167436c5948501f"
47+
string(64) "46376b80f409b29dc2b5f6f0c52591990896e5716f41477cd30085ab7f10301e"
48+
string(64) "e0c418f7c8d9c4cdd7395b93ea124f3ad99021bb681dfc3302a9d99a2e53e64e"
49+
sodium_crypto_scalarmult(): Argument #1 ($n) must be SODIUM_CRYPTO_SCALARMULT_SCALARBYTES bytes long

0 commit comments

Comments
 (0)