Skip to content

Commit b36662a

Browse files
committed
fcmp++ crypto: compare normalized fe byte reprs
1 parent 7ca251c commit b36662a

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/fcmp_pp/fcmp_pp_crypto.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@
5151
// printf("\n");
5252
// }
5353

54+
static bool fe_compare(const fe a, const fe b)
55+
{
56+
unsigned char a_bytes[32];
57+
unsigned char b_bytes[32];
58+
59+
fe_tobytes(a_bytes, a);
60+
fe_tobytes(b_bytes, b);
61+
62+
return memcmp(a_bytes, b_bytes, sizeof(a_bytes)) == 0;
63+
}
64+
5465
static bool sqrt_ext(fe y, const fe x)
5566
{
5667
fe y_res;
@@ -67,7 +78,7 @@ static bool sqrt_ext(fe y, const fe x)
6778
fe c;
6879
fe_mul(c, x2, b_sq);
6980

70-
if (memcmp(c, fe_one, sizeof(fe)) == 0 || memcmp(c, fe_m1, sizeof(fe)) == 0)
81+
if (fe_compare(c, fe_one) || fe_compare(c, fe_m1))
7182
{
7283
fe_0(c);
7384
c[0] = 3;
@@ -85,7 +96,7 @@ static bool sqrt_ext(fe y, const fe x)
8596

8697
fe y_sq;
8798
fe_sq(y_sq, y_res);
88-
bool r = memcmp(x, y_sq, sizeof(fe)) == 0;
99+
bool r = fe_compare(x, y_sq);
89100

90101
fe_copy(y, y_res);
91102
return r;
@@ -127,7 +138,7 @@ static void inv_psi1(fe e_out, fe u_out, fe w_out, const fe e, const fe u, const
127138
fe neg_u_dbl;
128139
fe_dbl(neg_u_dbl, u);
129140
fe_neg(neg_u_dbl, neg_u_dbl);
130-
if (memcmp(tt_sq, neg_u_dbl, sizeof(fe)) == 0) {
141+
if (fe_compare(tt_sq, neg_u_dbl)) {
131142
fe_mul(tt, tt, fe_sqrtm1);
132143
}
133144

@@ -221,7 +232,7 @@ static bool check_e_u_w(const fe e, const fe u, const fe w)
221232
fe_add(sum, sum, B_mul_e_sq_sq);
222233
fe_reduce(sum, sum);
223234

224-
if (memcmp(u_w_sq, sum, sizeof(fe)) != 0) {
235+
if (!fe_compare(u_w_sq, sum)) {
225236
return false;
226237
}
227238

tests/unit_tests/crypto.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -474,17 +474,43 @@ TEST(Crypto, fe_constants)
474474
ASSERT_TRUE(memcmp(fe_sqrtm1, sqrtm1, sizeof(fe)) == 0);
475475
}
476476

477-
TEST(Crypto, torsion_check_pass)
477+
TEST(Crypto, torsion_check_pass_random)
478478
{
479-
const cryptonote::keypair kp = cryptonote::keypair::generate(hw::get_device("default"));
480-
ge_p3 x;
481-
ASSERT_EQ(ge_frombytes_vartime(&x, (const unsigned char*)kp.pub.data), 0);
482-
const rct::key k = rct::pk2rct(kp.pub);
483-
ASSERT_TRUE(rct::isInMainSubgroup(k));
484-
ASSERT_FALSE(fcmp_pp::mul8_is_identity(x));
485-
ASSERT_TRUE(fcmp_pp::torsion_check_vartime(x));
486-
const rct::key cleared = fcmp_pp::clear_torsion(x);
487-
ASSERT_EQ(k, cleared);
479+
for (int i = 0; i < 1000; ++i)
480+
{
481+
const cryptonote::keypair kp = cryptonote::keypair::generate(hw::get_device("default"));
482+
ge_p3 x;
483+
ASSERT_EQ(ge_frombytes_vartime(&x, (const unsigned char*)kp.pub.data), 0);
484+
const rct::key k = rct::pk2rct(kp.pub);
485+
ASSERT_TRUE(rct::isInMainSubgroup(k));
486+
ASSERT_FALSE(fcmp_pp::mul8_is_identity(x));
487+
ASSERT_TRUE(fcmp_pp::torsion_check_vartime(x));
488+
const rct::key cleared = fcmp_pp::clear_torsion(x);
489+
ASSERT_EQ(k, cleared);
490+
}
491+
}
492+
493+
TEST(Crypto, torsion_check_pass_hardcoded)
494+
{
495+
static constexpr const char *torsion_free_points[] = {
496+
// Fails in check_e_u_w without correctly implemented fe_compare
497+
"785eda585dca4f3d27976106008ccfbca13146c8b21b8c7e4909032639a776e1",
498+
// Fails in inv_psi2 without correctly implemented fe_compare
499+
"9a7b10563aa266032cd075f4e347f348a3841ae4f41572633351a97dd44066b4"
500+
};
501+
502+
for (const auto point : torsion_free_points)
503+
{
504+
rct::key k;
505+
epee::string_tools::hex_to_pod(point, k);
506+
ge_p3 x;
507+
ASSERT_EQ(ge_frombytes_vartime(&x, k.bytes), 0);
508+
ASSERT_TRUE(rct::isInMainSubgroup(k));
509+
ASSERT_FALSE(fcmp_pp::mul8_is_identity(x));
510+
ASSERT_TRUE(fcmp_pp::torsion_check_vartime(x));
511+
const rct::key cleared = fcmp_pp::clear_torsion(x);
512+
ASSERT_EQ(k, cleared);
513+
}
488514
}
489515

490516
TEST(Crypto, torsion_check_torsioned_point)

0 commit comments

Comments
 (0)