Skip to content

Commit

Permalink
remove LegacyPreRctEnote; add bool is_pre_rct to LegacyEnoteV1
Browse files Browse the repository at this point in the history
  • Loading branch information
SNeedlewoods committed Jan 14, 2024
1 parent 352bf45 commit eabfd3a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 104 deletions.
49 changes: 12 additions & 37 deletions src/seraphis_core/legacy_enote_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ const rct::key& onetime_address_ref(const LegacyEnoteVariant &variant)
struct visitor final : public tools::variant_static_visitor<const rct::key&>
{
using variant_static_visitor::operator(); //for blank overload
const rct::key& operator()(const LegacyPreRctEnote &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV1 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV2 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV3 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV4 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV5 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV1 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV2 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV3 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV4 &enote) const { return enote.onetime_address; }
const rct::key& operator()(const LegacyEnoteV5 &enote) const { return enote.onetime_address; }
};

return variant.visit(visitor{});
Expand All @@ -67,46 +66,22 @@ rct::key amount_commitment_ref(const LegacyEnoteVariant &variant)
struct visitor final : public tools::variant_static_visitor<rct::key>
{
using variant_static_visitor::operator(); //for blank overload
rct::key operator()(const LegacyPreRctEnote &enote) const { return rct::zeroCommit(enote.amount); }
rct::key operator()(const LegacyEnoteV1 &enote) const { return rct::zeroCommit(enote.amount); }
rct::key operator()(const LegacyEnoteV2 &enote) const { return enote.amount_commitment; }
rct::key operator()(const LegacyEnoteV3 &enote) const { return enote.amount_commitment; }
rct::key operator()(const LegacyEnoteV4 &enote) const { return rct::zeroCommit(enote.amount); }
rct::key operator()(const LegacyEnoteV5 &enote) const { return enote.amount_commitment; }
rct::key operator()(const LegacyEnoteV1 &enote) const { return rct::zeroCommit(enote.amount); }
rct::key operator()(const LegacyEnoteV2 &enote) const { return enote.amount_commitment; }
rct::key operator()(const LegacyEnoteV3 &enote) const { return enote.amount_commitment; }
rct::key operator()(const LegacyEnoteV4 &enote) const { return rct::zeroCommit(enote.amount); }
rct::key operator()(const LegacyEnoteV5 &enote) const { return enote.amount_commitment; }
};

return variant.visit(visitor{});
}
//-------------------------------------------------------------------------------------------------------------------
rct::xmr_amount amount_ref(const LegacyEnoteVariant &variant)
{
struct visitor final : public tools::variant_static_visitor<rct::xmr_amount>
{
using variant_static_visitor::operator(); //for blank overload
rct::xmr_amount operator()(const LegacyPreRctEnote &enote) const { return enote.amount; }
rct::xmr_amount operator()(const LegacyEnoteV1 &enote) const { return 0; }
rct::xmr_amount operator()(const LegacyEnoteV2 &enote) const { return 0; }
rct::xmr_amount operator()(const LegacyEnoteV3 &enote) const { return 0; }
rct::xmr_amount operator()(const LegacyEnoteV4 &enote) const { return 0; }
rct::xmr_amount operator()(const LegacyEnoteV5 &enote) const { return 0; }
};

return variant.visit(visitor{});
}
//-------------------------------------------------------------------------------------------------------------------
LegacyPreRctEnote gen_legacy_pre_rct_enote()
{
LegacyPreRctEnote temp;
temp.onetime_address = rct::pkGen();
temp.amount = crypto::rand_idx<rct::xmr_amount>(0);
return temp;
}
//-------------------------------------------------------------------------------------------------------------------
LegacyEnoteV1 gen_legacy_enote_v1()
LegacyEnoteV1 gen_legacy_enote_v1(bool is_pre_rct)
{
LegacyEnoteV1 temp;
temp.onetime_address = rct::pkGen();
temp.amount = crypto::rand_idx<rct::xmr_amount>(0);
temp.is_pre_rct = is_pre_rct;
return temp;
}
//-------------------------------------------------------------------------------------------------------------------
Expand Down
30 changes: 6 additions & 24 deletions src/seraphis_core/legacy_enote_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,19 @@ namespace sp
{

////
// LegacyPreRctEnote (coinbase and regular)
// - onetime address
// - cleartext amount
///
struct LegacyPreRctEnote final
{
/// Ko
rct::key onetime_address;
/// a
rct::xmr_amount amount;
};

////
// LegacyEnoteV1 (coinbase-only post-ringct)
// LegacyEnoteV1 (regular & coinbase pre-RingCT, then coinbase-only post-RingCT)
// - onetime address
// - cleartext amount
// - differentitiate pre/post-RingCT
///
struct LegacyEnoteV1 final
{
/// Ko
rct::key onetime_address;
/// a
rct::xmr_amount amount;

bool is_pre_rct;
};

/// get size in bytes
Expand Down Expand Up @@ -164,23 +154,15 @@ inline std::size_t legacy_enote_v5_size_bytes() { return 2*32 + 8 + sizeof(crypt
// onetime_address_ref(): get the enote's onetime address
// amount_commitment_ref(): get the enote's amount commitment (this is a copy because V1 enotes need to
// compute the commitment)
// amount_ref(): get the enote's amount (returns 0 for enotes without cleartext amount,
// this is useful e.g. for m_legacy_amount_counts)
///
using LegacyEnoteVariant = tools::variant<LegacyPreRctEnote, LegacyEnoteV1, LegacyEnoteV2,
LegacyEnoteV3, LegacyEnoteV4, LegacyEnoteV5>;
using LegacyEnoteVariant = tools::variant<LegacyEnoteV1, LegacyEnoteV2, LegacyEnoteV3, LegacyEnoteV4, LegacyEnoteV5>;
const rct::key& onetime_address_ref(const LegacyEnoteVariant &variant);
rct::key amount_commitment_ref(const LegacyEnoteVariant &variant);
rct::xmr_amount amount_ref(const LegacyEnoteVariant &variant);

/**
* brief: gen_legacy_pre_rct_enote() - generate a legacy v1 pre-ringct enote (all random)
*/
LegacyPreRctEnote gen_legacy_pre_rct_enote();
/**
* brief: gen_legacy_enote_v1() - generate a legacy v1 enote (all random)
*/
LegacyEnoteV1 gen_legacy_enote_v1();
LegacyEnoteV1 gen_legacy_enote_v1(bool is_pre_rct);
/**
* brief: gen_legacy_enote_v2() - generate a legacy v2 enote (all random)
*/
Expand Down
6 changes: 5 additions & 1 deletion src/seraphis_core/legacy_enote_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void make_legacy_pre_rct_enote(const rct::key &destination_spendkey,
const rct::xmr_amount amount,
const std::uint64_t output_index,
const crypto::secret_key &enote_ephemeral_privkey,
LegacyPreRctEnote &enote_out)
LegacyEnoteV1 &enote_out)
{
// onetime address: K^o = Hn(r K^v, t) G + K^s
make_legacy_onetime_address(destination_spendkey,
Expand All @@ -75,6 +75,8 @@ void make_legacy_pre_rct_enote(const rct::key &destination_spendkey,

// amount: a
enote_out.amount = amount;

enote_out.is_pre_rct = true;
}
//-------------------------------------------------------------------------------------------------------------------
void make_legacy_enote_v1(const rct::key &destination_spendkey,
Expand All @@ -94,6 +96,8 @@ void make_legacy_enote_v1(const rct::key &destination_spendkey,

// amount: a
enote_out.amount = amount;

enote_out.is_pre_rct = false;
}
//-------------------------------------------------------------------------------------------------------------------
void make_legacy_enote_v2(const rct::key &destination_spendkey,
Expand Down
2 changes: 1 addition & 1 deletion src/seraphis_core/legacy_enote_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void make_legacy_pre_rct_enote(const rct::key &destination_spendkey,
const rct::xmr_amount amount,
const std::uint64_t output_index,
const crypto::secret_key &enote_ephemeral_privkey,
LegacyPreRctEnote &enote_out);
LegacyEnoteV1 &enote_out);
/**
* brief: make_legacy_enote_v1 - make a v1 legacy enote sending to an address or subaddress
* param: destination_spendkey - [address: K^s = k^s G] [subaddress: K^{s,i} = (Hn(k^v, i) + k^s) G]
Expand Down
42 changes: 6 additions & 36 deletions src/seraphis_main/enote_record_utils_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ static bool try_check_legacy_view_tag(const LegacyEnoteVariant &enote,
struct visitor final : public tools::variant_static_visitor<boost::optional<crypto::view_tag>>
{
using variant_static_visitor::operator(); //for blank overload
boost::optional<crypto::view_tag> operator()(const LegacyPreRctEnote &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV1 &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV2 &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV3 &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV4 &enote) const { return enote.view_tag; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV5 &enote) const { return enote.view_tag; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV1 &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV2 &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV3 &enote) const { return boost::none; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV4 &enote) const { return enote.view_tag; }
boost::optional<crypto::view_tag> operator()(const LegacyEnoteV5 &enote) const { return enote.view_tag; }
};

const boost::optional<crypto::view_tag> enote_view_tag{enote.visit(visitor{})};
Expand Down Expand Up @@ -224,13 +223,7 @@ static bool try_get_amount_commitment_information(const LegacyEnoteVariant &enot
rct::xmr_amount &amount_out,
crypto::secret_key &amount_blinding_factor_out)
{
if (const LegacyPreRctEnote *enote_ptr = enote.try_unwrap<LegacyPreRctEnote>())
{
return try_get_amount_commitment_information_v1(enote_ptr->amount,
amount_out,
amount_blinding_factor_out);
}
else if (const LegacyEnoteV1 *enote_ptr = enote.try_unwrap<LegacyEnoteV1>())
if (const LegacyEnoteV1 *enote_ptr = enote.try_unwrap<LegacyEnoteV1>())
{
return try_get_amount_commitment_information_v1(enote_ptr->amount,
amount_out,
Expand Down Expand Up @@ -429,28 +422,6 @@ static bool is_legacy_enote_v5(const cryptonote::transaction &tx, const cryptono
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static bool try_out_to_legacy_pre_rct_enote(const cryptonote::transaction &tx,
const size_t output_index,
sp::LegacyEnoteVariant &enote_out)
{
if (output_index >= tx.vout.size())
return false;
if (!is_legacy_enote_v1(tx, tx.vout[output_index]))
return false;

sp::LegacyPreRctEnote enote_v1;

/// Ko
crypto::public_key out_pub_key;
cryptonote::get_output_public_key(tx.vout[output_index], out_pub_key);
enote_v1.onetime_address = rct::pk2rct(out_pub_key);
/// a
enote_v1.amount = tx.vout[output_index].amount;

enote_out = std::move(enote_v1);
return true;
}//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static bool try_out_to_legacy_enote_v1(const cryptonote::transaction &tx,
const size_t output_index,
sp::LegacyEnoteVariant &enote_out)
Expand Down Expand Up @@ -783,7 +754,6 @@ void legacy_outputs_to_enotes(const cryptonote::transaction &tx, std::vector<Leg
for (size_t i = 0; i < tx.vout.size(); ++i)
{
enotes_out.emplace_back();
// TODO : here we need to know if legacy v1 is pre-rct, but this function actually doesn't get called anywhere
if (!try_out_to_legacy_enote_v1(tx, i, enotes_out.back())
&& !try_out_to_legacy_enote_v2(tx, i, enotes_out.back())
&& !try_out_to_legacy_enote_v3(tx, i, enotes_out.back())
Expand Down
6 changes: 4 additions & 2 deletions src/seraphis_mocks/mock_ledger_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ std::uint64_t MockLedgerContext::add_legacy_coinbase(const rct::key &tx_id,
++total_legacy_output_count;

// increment legacy amount count
rct::xmr_amount amount = amount_ref(enote);
const LegacyEnoteV1 *tmp_enote = enote.try_unwrap<LegacyEnoteV1>();
rct::xmr_amount amount = tmp_enote && tmp_enote->is_pre_rct ? tmp_enote->amount : 0;
++m_legacy_amount_counts[amount];
enote_same_amount_ledger_indices.emplace_back(m_legacy_amount_counts[amount]);
}
Expand Down Expand Up @@ -543,7 +544,8 @@ std::uint64_t MockLedgerContext::pop_chain_at_index(const std::uint64_t pop_inde
// enotes in tx
for (LegacyEnoteVariant enote : std::get<std::vector<LegacyEnoteVariant>>(tx_output_contents.second))
{
rct::xmr_amount amount = amount_ref(enote);
const LegacyEnoteV1 *tmp_enote = enote.try_unwrap<LegacyEnoteV1>();
rct::xmr_amount amount = tmp_enote && tmp_enote->is_pre_rct ? tmp_enote->amount : 0;
if (m_legacy_amount_counts[amount] > 1)
--m_legacy_amount_counts[amount];
else
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/seraphis_enote_scanning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2322,7 +2322,7 @@ TEST(seraphis_enote_scanning, legacy_pre_rct_1)

/// test

LegacyPreRctEnote enote_1; //to normal destination
LegacyEnoteV1 enote_1; //to normal destination
const crypto::secret_key enote_ephemeral_privkey_1{make_secret_key()};
const rct::key enote_ephemeral_pubkey_1{
rct::scalarmultBase(rct::sk2rct(enote_ephemeral_privkey_1))
Expand All @@ -2335,7 +2335,7 @@ TEST(seraphis_enote_scanning, legacy_pre_rct_1)
enote_ephemeral_privkey_1,
enote_1));

LegacyPreRctEnote enote_2; //to subaddress destination
LegacyEnoteV1 enote_2; //to subaddress destination
const crypto::secret_key enote_ephemeral_privkey_2{make_secret_key()};
const rct::key enote_ephemeral_pubkey_2{
rct::scalarmultKey(subaddr_spendkey, rct::sk2rct(enote_ephemeral_privkey_2))
Expand Down Expand Up @@ -2369,7 +2369,7 @@ TEST(seraphis_enote_scanning, legacy_pre_rct_1)
}
));

ASSERT_TRUE(ledger_context.get_legacy_amount_counts(1) == 2); // [ 2*LegacyPreRctEnote ]
ASSERT_TRUE(ledger_context.get_legacy_amount_counts(1) == 2); // [ 2*LegacyEnoteV1 ]

refresh_user_enote_store_legacy_full(legacy_keys.Ks,
legacy_subaddress_map,
Expand Down

0 comments on commit eabfd3a

Please sign in to comment.