carrot_core: separate payment_id_t and encrypted_payment_id_t

This commit is contained in:
jeffro256
2025-05-19 14:44:24 -05:00
committed by akildemir
parent 4942f848f8
commit daffdd764b
4 changed files with 47 additions and 6 deletions

View File

@@ -102,6 +102,16 @@ payment_id_t operator^(const payment_id_t &a, const payment_id_t &b)
return xor_bytes(a, b);
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const encrypted_payment_id_t &a, const encrypted_payment_id_t &b)
{
return memcmp(&a, &b, sizeof(encrypted_payment_id_t)) == 0;
}
//-------------------------------------------------------------------------------------------------------------------
encrypted_payment_id_t operator^(const encrypted_payment_id_t &a, const encrypted_payment_id_t &b)
{
return xor_bytes(a, b);
}
//-------------------------------------------------------------------------------------------------------------------
bool operator==(const input_context_t &a, const input_context_t &b)
{
return memcmp(&a, &b, sizeof(input_context_t)) == 0;
@@ -119,7 +129,17 @@ janus_anchor_t gen_janus_anchor()
//-------------------------------------------------------------------------------------------------------------------
payment_id_t gen_payment_id()
{
return crypto::rand<payment_id_t>();
while (true)
{
const payment_id_t res = crypto::rand<payment_id_t>();
if (res != null_payment_id)
return res;
}
}
//-------------------------------------------------------------------------------------------------------------------
encrypted_payment_id_t gen_encrypted_payment_id()
{
return crypto::rand<encrypted_payment_id_t>();
}
//-------------------------------------------------------------------------------------------------------------------
view_tag_t gen_view_tag()

View File

@@ -78,7 +78,10 @@ struct payment_id_t final
static constexpr payment_id_t null_payment_id{{0}};
/// legacy encrypted payment ID
using encrypted_payment_id_t = payment_id_t;
struct encrypted_payment_id_t final
{
unsigned char bytes[PAYMENT_ID_BYTES];
};
/// carrot view tags
constexpr std::size_t VIEW_TAG_BYTES{3};
@@ -118,6 +121,11 @@ bool operator==(const payment_id_t &a, const payment_id_t &b);
static inline bool operator!=(const payment_id_t &a, const payment_id_t &b) { return !(a == b); }
payment_id_t operator^(const payment_id_t &a, const payment_id_t &b);
/// overloaded operators: encrypted payment ID
bool operator==(const encrypted_payment_id_t &a, const encrypted_payment_id_t &b);
static inline bool operator!=(const encrypted_payment_id_t &a, const encrypted_payment_id_t &b) { return !(a == b); }
encrypted_payment_id_t operator^(const encrypted_payment_id_t &a, const encrypted_payment_id_t &b);
/// overloaded operators: input context
bool operator==(const input_context_t &a, const input_context_t &b);
static inline bool operator!=(const input_context_t &a, const input_context_t &b) { return !(a == b); }
@@ -133,8 +141,10 @@ encrypted_return_pubkey_t operator^(const encrypted_return_pubkey_t &a, const en
/// generate a random janus anchor
janus_anchor_t gen_janus_anchor();
/// generate a random (non-zero) payment ID
/// generate a random (non-null) payment ID
payment_id_t gen_payment_id();
/// generate a random encrypted payment ID
encrypted_payment_id_t gen_encrypted_payment_id();
/// generate a random view tag
view_tag_t gen_view_tag();
/// generate a random input context

View File

@@ -89,6 +89,17 @@ static rct::xmr_amount dec_amount(const encrypted_amount_t &encrypted_amount, co
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
template <typename Pid,
typename OtherPid = std::conditional_t<std::is_same_v<Pid, payment_id_t>, encrypted_payment_id_t, payment_id_t>>
static OtherPid convert_payment_id(const Pid &v)
{
static_assert(sizeof(Pid) == PAYMENT_ID_BYTES);
OtherPid conv;
memcpy(&conv, &v, PAYMENT_ID_BYTES);
return conv;
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
void make_carrot_enote_ephemeral_privkey(const janus_anchor_t &anchor_norm,
const input_context_t &input_context,
const crypto::public_key &address_spend_pubkey,
@@ -389,7 +400,7 @@ encrypted_payment_id_t encrypt_legacy_payment_id(const payment_id_t payment_id,
make_carrot_payment_id_encryption_mask(s_sender_receiver, onetime_address, mask);
// pid_enc = pid XOR m_pid
return payment_id ^ mask;
return convert_payment_id(payment_id) ^ mask;
}
//-------------------------------------------------------------------------------------------------------------------
payment_id_t decrypt_legacy_payment_id(const encrypted_payment_id_t encrypted_payment_id,
@@ -401,7 +412,7 @@ payment_id_t decrypt_legacy_payment_id(const encrypted_payment_id_t encrypted_pa
make_carrot_payment_id_encryption_mask(s_sender_receiver, onetime_address, mask);
// pid = pid_enc XOR m_pid
return encrypted_payment_id ^ mask;
return convert_payment_id(encrypted_payment_id ^ mask);
}
//-------------------------------------------------------------------------------------------------------------------
void make_carrot_janus_anchor_special(const mx25519_pubkey &enote_ephemeral_pubkey,

View File

@@ -163,7 +163,7 @@ void get_output_enote_proposals(const std::vector<CarrotPaymentProposalV1> &norm
std::vector<std::pair<bool, std::size_t>> *payment_proposal_order_out)
{
output_enote_proposals_out.clear();
encrypted_payment_id_out = null_payment_id;
encrypted_payment_id_out = {{0}};
if (payment_proposal_order_out)
payment_proposal_order_out->clear();