fcmp++: rename "PreWeiX" -> "EdYDerivatives" + small touchups

Naming suggestion from @kayabaNerve
This commit is contained in:
j-berman 2024-09-17 10:36:00 -07:00
parent da710fcaaf
commit a74f7aeada
7 changed files with 39 additions and 35 deletions

View File

@ -3920,7 +3920,7 @@ int ge_p3_is_point_at_infinity_vartime(const ge_p3 *p) {
}
// https://www.ietf.org/archive/id/draft-ietf-lwig-curve-representations-02.pdf E.2
void fe_to_wei_x(unsigned char *wei_x, const fe inv_one_minus_y, const fe one_plus_y)
void fe_ed_y_derivatives_to_wei_x(unsigned char *wei_x, const fe inv_one_minus_y, const fe one_plus_y)
{
// (1/(1-y))*(1+y)
fe inv_one_minus_y_mul_one_plus_y;

View File

@ -173,4 +173,4 @@ void fe_1(fe h);
int ge_p3_is_point_at_infinity_vartime(const ge_p3 *p);
void fe_to_wei_x(unsigned char *wei_x, const fe inv_one_minus_y, const fe one_plus_y);
void fe_ed_y_derivatives_to_wei_x(unsigned char *wei_x, const fe inv_one_minus_y, const fe one_plus_y);

View File

@ -736,12 +736,12 @@ static PreLeafTuple output_to_pre_leaf_tuple(const OutputPair &output_pair)
crypto::derive_key_image_generator(output_pubkey, I);
PreLeafTuple plt;
if (!fcmp_pp::point_to_pre_wei_x(O, plt.O_pre_x))
throw std::runtime_error("failed to get pre wei x from O");
if (!fcmp_pp::point_to_pre_wei_x(rct::pt2rct(I), plt.I_pre_x))
throw std::runtime_error("failed to get pre wei x from I");
if (!fcmp_pp::point_to_pre_wei_x(C, plt.C_pre_x))
throw std::runtime_error("failed to get pre wei x from C");
if (!fcmp_pp::point_to_ed_y_derivatives(O, plt.O_pre_x))
throw std::runtime_error("failed to get ed y derivatives from O");
if (!fcmp_pp::point_to_ed_y_derivatives(rct::pt2rct(I), plt.I_pre_x))
throw std::runtime_error("failed to get ed y derivatives from I");
if (!fcmp_pp::point_to_ed_y_derivatives(C, plt.C_pre_x))
throw std::runtime_error("failed to get ed y derivatives from C");
return plt;
}
@ -755,9 +755,9 @@ CurveTrees<Helios, Selene>::LeafTuple CurveTrees<Helios, Selene>::leaf_tuple(con
const auto plt = output_to_pre_leaf_tuple(output_pair);
rct::key O_x, I_x, C_x;
fcmp_pp::pre_wei_x_to_wei_x(plt.O_pre_x, O_x);
fcmp_pp::pre_wei_x_to_wei_x(plt.I_pre_x, I_x);
fcmp_pp::pre_wei_x_to_wei_x(plt.C_pre_x, C_x);
fcmp_pp::ed_y_derivatives_to_wei_x(plt.O_pre_x, O_x);
fcmp_pp::ed_y_derivatives_to_wei_x(plt.I_pre_x, I_x);
fcmp_pp::ed_y_derivatives_to_wei_x(plt.C_pre_x, C_x);
return LeafTuple{
.O_x = tower_cycle::selene_scalar_from_bytes(O_x),
@ -1019,7 +1019,8 @@ void CurveTrees<C1, C2>::set_valid_leaves(
tools::threadpool& tpool = tools::threadpool::getInstanceForCompute();
tools::threadpool::waiter waiter(tpool);
// Step 1. Multithreaded convert valid outputs into pre-Wei x coords
// Step 1. Multithreaded convert valid outputs into Edwards y derivatives needed to get Wei x coordinates
// TODO: investigate batched threading (as opposed to small tasks)
std::vector<PreLeafTuple> pre_leaves;
pre_leaves.resize(new_outputs.size());
for (std::size_t i = 0; i < new_outputs.size(); ++i)
@ -1047,15 +1048,17 @@ void CurveTrees<C1, C2>::set_valid_leaves(
);
}
CHECK_AND_ASSERT_THROW_MES(waiter.wait(), "failed to convert outputs to pre wei x coords");
CHECK_AND_ASSERT_THROW_MES(waiter.wait(), "failed to convert outputs to ed y derivatives");
// Step 2. Collect valid pre-Wei x coords
// Step 2. Collect valid Edwards y derivatives
const std::size_t n_valid_outputs = std::count(valid_outputs.begin(), valid_outputs.end(), True);
const std::size_t n_valid_leaf_elems = n_valid_outputs * LEAF_TUPLE_SIZE;
// Collecting (1+y)'s
fe *one_plus_y_vec = (fe *) malloc(n_valid_leaf_elems * sizeof(fe));
CHECK_AND_ASSERT_THROW_MES(one_plus_y_vec, "failed malloc one_plus_y_vec");
// Collecting (1-y)'s
fe *one_minus_y_vec = (fe *) malloc(n_valid_leaf_elems * sizeof(fe));
CHECK_AND_ASSERT_THROW_MES(one_minus_y_vec, "failed malloc one_minus_y_vec");
@ -1090,14 +1093,15 @@ void CurveTrees<C1, C2>::set_valid_leaves(
CHECK_AND_ASSERT_THROW_MES(n_valid_leaf_elems == valid_i, "unexpected end valid_i");
// Step 3. Get batch inverse of valid pre-Wei x (1-y)'s
// Step 3. Get batch inverse of all valid (1-y)'s
// - Batch inversion is significantly faster than inverting 1 at a time
fe *inv_one_minus_y_vec = (fe *) malloc(n_valid_leaf_elems * sizeof(fe));
CHECK_AND_ASSERT_THROW_MES(inv_one_minus_y_vec, "failed malloc inv_one_minus_y_vec");
CHECK_AND_ASSERT_THROW_MES(fe_batch_invert(inv_one_minus_y_vec, one_minus_y_vec, n_valid_leaf_elems) == 0,
"failed to batch invert");
// Step 4. Multithreaded get Wei x coords and convert to Selene scalars
// Step 4. Multithreaded get Wei x's and convert to Selene scalars
// TODO: investigate batched threading (as opposed to small tasks)
flattened_leaves_out.resize(n_valid_leaf_elems);
for (std::size_t i = 0; i < n_valid_leaf_elems; ++i)
{
@ -1110,7 +1114,7 @@ void CurveTrees<C1, C2>::set_valid_leaves(
]()
{
rct::key wei_x;
fe_to_wei_x(wei_x.bytes, inv_one_minus_y_vec[i], one_plus_y_vec[i]);
fe_ed_y_derivatives_to_wei_x(wei_x.bytes, inv_one_minus_y_vec[i], one_plus_y_vec[i]);
flattened_leaves_out[i] = tower_cycle::selene_scalar_from_bytes(wei_x);
},
true
@ -1119,7 +1123,7 @@ void CurveTrees<C1, C2>::set_valid_leaves(
CHECK_AND_ASSERT_THROW_MES(waiter.wait(), "failed to convert outputs to wei x coords");
// Step 5. Set valid tuples
// Step 5. Set valid tuples to be stored in the db
tuples_out.clear();
tuples_out.reserve(n_valid_outputs);
for (std::size_t i = 0; i < valid_outputs.size(); ++i)

View File

@ -159,9 +159,9 @@ using OutputsByUnlockBlock = std::unordered_map<uint64_t, std::vector<OutputCont
// Struct composed of ec elems needed to get a full-fledged leaf tuple
struct PreLeafTuple final
{
fcmp_pp::PreWeiX O_pre_x;
fcmp_pp::PreWeiX I_pre_x;
fcmp_pp::PreWeiX C_pre_x;
fcmp_pp::EdYDerivatives O_pre_x;
fcmp_pp::EdYDerivatives I_pre_x;
fcmp_pp::EdYDerivatives C_pre_x;
};
//----------------------------------------------------------------------------------------------------------------------

View File

@ -48,7 +48,7 @@ bool clear_torsion(const rct::key &k, rct::key &k_out) {
return true;
}
//----------------------------------------------------------------------------------------------------------------------
bool point_to_pre_wei_x(const rct::key &pub, PreWeiX &pre_wei_x) {
bool point_to_ed_y_derivatives(const rct::key &pub, EdYDerivatives &ed_y_derivatives) {
if (pub == rct::I)
return false;
fe y;
@ -57,22 +57,22 @@ bool point_to_pre_wei_x(const rct::key &pub, PreWeiX &pre_wei_x) {
fe one;
fe_1(one);
// (1+y),(1-y)
fe_add(pre_wei_x.one_plus_y, one, y);
fe_sub(pre_wei_x.one_minus_y, one, y);
fe_add(ed_y_derivatives.one_plus_y, one, y);
fe_sub(ed_y_derivatives.one_minus_y, one, y);
return true;
}
//----------------------------------------------------------------------------------------------------------------------
void pre_wei_x_to_wei_x(const PreWeiX pre_wei_x, rct::key &wei_x) {
void ed_y_derivatives_to_wei_x(const EdYDerivatives pre_wei_x, rct::key &wei_x) {
fe inv_one_minus_y;
fe_invert(inv_one_minus_y, pre_wei_x.one_minus_y);
fe_to_wei_x(wei_x.bytes, inv_one_minus_y, pre_wei_x.one_plus_y);
fe_ed_y_derivatives_to_wei_x(wei_x.bytes, inv_one_minus_y, pre_wei_x.one_plus_y);
}
//----------------------------------------------------------------------------------------------------------------------
bool point_to_wei_x(const rct::key &pub, rct::key &wei_x) {
PreWeiX pre_wei_x;
if (!point_to_pre_wei_x(pub, pre_wei_x))
EdYDerivatives ed_y_derivatives;
if (!point_to_ed_y_derivatives(pub, ed_y_derivatives))
return false;
pre_wei_x_to_wei_x(pre_wei_x, wei_x);
ed_y_derivatives_to_wei_x(ed_y_derivatives, wei_x);
return true;
}
//----------------------------------------------------------------------------------------------------------------------

View File

@ -38,7 +38,7 @@ namespace fcmp_pp
{
//----------------------------------------------------------------------------------------------------------------------
// Field elems needed to get wei x coord
struct PreWeiX final
struct EdYDerivatives final
{
fe one_plus_y;
fe one_minus_y;
@ -46,8 +46,8 @@ struct PreWeiX final
//----------------------------------------------------------------------------------------------------------------------
// TODO: tests for these functions
bool clear_torsion(const rct::key &k, rct::key &k_out);
bool point_to_pre_wei_x(const rct::key &pub, PreWeiX &pre_wei_x);
void pre_wei_x_to_wei_x(const PreWeiX pre_wei_x, rct::key &wei_x);
bool point_to_ed_y_derivatives(const rct::key &pub, EdYDerivatives &ed_y_derivatives);
void ed_y_derivatives_to_wei_x(const EdYDerivatives ed_y_derivatives, rct::key &wei_x);
bool point_to_wei_x(const rct::key &pub, rct::key &wei_x);
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------

View File

@ -372,7 +372,7 @@ TEST(Crypto, key_image_y)
TEST(Crypto, batch_inversion)
{
std::size_t MAX_TEST_ELEMS = 1000;
const std::size_t MAX_TEST_ELEMS = 1000;
// Memory allocator
auto alloc = [](const std::size_t n) -> fe*
@ -384,8 +384,8 @@ TEST(Crypto, batch_inversion)
};
// Init test elems and individual inversions
fe *init_elems = alloc(MAX_TEST_ELEMS);
fe *norm_inverted = alloc(MAX_TEST_ELEMS);
fe *init_elems = alloc(MAX_TEST_ELEMS);
fe *norm_inverted = alloc(MAX_TEST_ELEMS);
for (std::size_t i = 0; i < MAX_TEST_ELEMS; ++i)
{
const cryptonote::keypair kp = cryptonote::keypair::generate(hw::get_device("default"));