carrot_impl compilation

This commit is contained in:
akildemir
2025-05-07 13:52:52 +03:00
parent 44e70b4765
commit a6733fcf2b
14 changed files with 170 additions and 264 deletions
+39 -20
View File
@@ -71,22 +71,6 @@ namespace crypto {
using secret_key = epee::mlocked<tools::scrubbed<ec_scalar>>;
POD_CLASS public_keyV {
std::vector<public_key> keys;
int rows;
};
POD_CLASS secret_keyV {
std::vector<secret_key> keys;
int rows;
};
POD_CLASS public_keyM {
int cols;
int rows;
std::vector<secret_keyV> column_vectors;
};
POD_CLASS key_derivation: ec_point {
friend class crypto_ops;
};
@@ -95,6 +79,10 @@ namespace crypto {
friend class crypto_ops;
};
POD_CLASS key_image_y: ec_point {
friend class crypto_ops;
};
POD_CLASS signature {
ec_scalar c, r;
friend class crypto_ops;
@@ -110,7 +98,7 @@ namespace crypto {
static_assert(sizeof(ec_point) == 32 && sizeof(ec_scalar) == 32 &&
sizeof(public_key) == 32 && sizeof(public_key_memsafe) == 32 && sizeof(secret_key) == 32 &&
sizeof(key_derivation) == 32 && sizeof(key_image) == 32 &&
sizeof(key_derivation) == 32 && sizeof(key_image) == 32 && sizeof(key_image_y) == 32 &&
sizeof(signature) == 64 && sizeof(view_tag) == 1, "Invalid structure size");
class crypto_ops {
@@ -145,6 +133,8 @@ namespace crypto {
friend void generate_tx_proof_v1(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const secret_key &, signature &);
static bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &, const int);
friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const boost::optional<public_key> &, const public_key &, const signature &, const int);
static void derive_key_image_generator(const public_key &, ec_point &);
friend void derive_key_image_generator(const public_key &, ec_point &);
static void generate_key_image(const public_key &, const secret_key &, key_image &);
friend void generate_key_image(const public_key &, const secret_key &, key_image &);
static void generate_ring_signature(const hash &, const key_image &,
@@ -157,6 +147,10 @@ namespace crypto {
const public_key *const *, std::size_t, const signature *);
static void derive_view_tag(const key_derivation &, std::size_t, view_tag &);
friend void derive_view_tag(const key_derivation &, std::size_t, view_tag &);
static bool key_image_to_y(const key_image &, key_image_y &);
friend bool key_image_to_y(const key_image &, key_image_y &);
static void key_image_from_y(const key_image_y &, const bool, key_image &);
friend void key_image_from_y(const key_image_y &, const bool, key_image &);
};
void generate_random_bytes_thread_safe(size_t N, uint8_t *bytes);
@@ -172,8 +166,8 @@ namespace crypto {
*/
template<typename T>
T rand() {
static_assert(std::is_standard_layout<T>(), "cannot write random bytes into non-standard layout type");
static_assert(std::is_trivially_copyable<T>(), "cannot write random bytes into non-trivially copyable type");
static_assert(std::is_standard_layout_v<T>, "cannot write random bytes into non-standard layout type");
static_assert(std::is_trivially_copyable_v<T>, "cannot write random bytes into non-trivially copyable type");
typename std::remove_cv<T>::type res;
generate_random_bytes_thread_safe(sizeof(T), (uint8_t*)&res);
return res;
@@ -270,6 +264,10 @@ namespace crypto {
return crypto_ops::check_tx_proof(prefix_hash, R, A, B, D, sig, version);
}
inline void derive_key_image_generator(const public_key &pub, ec_point &ki_gen) {
crypto_ops::derive_key_image_generator(pub, ki_gen);
}
/* To send money to a key:
* * The sender generates an ephemeral key and includes it in transaction output.
* * To spend the money, the receiver generates a key image from it.
@@ -313,6 +311,21 @@ namespace crypto {
crypto_ops::derive_view_tag(derivation, output_index, vt);
}
/** Clear the sign bit on the key image (i.e. get just the y coordinate).
* Return true if the sign bit is set, false if not.
* Since fcmp's allow construction of key images with sign bit cleared, while
* the same key image with sign bit set may already exist in the chain, we
* prevent double spends by converting all existing key images in the chain to
* their y coordinate and preventing duplicate key image y's.
*/
inline bool key_image_to_y(const key_image &ki, key_image_y &ki_y) {
return crypto_ops::key_image_to_y(ki, ki_y);
}
inline void key_image_from_y(const key_image_y &ki_y, const bool sign, key_image &ki) {
return crypto_ops::key_image_from_y(ki_y, sign, ki);
}
inline std::ostream &operator <<(std::ostream &o, const crypto::public_key &v) {
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
}
@@ -331,6 +344,9 @@ namespace crypto {
inline std::ostream &operator <<(std::ostream &o, const crypto::key_image &v) {
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
}
inline std::ostream &operator <<(std::ostream &o, const crypto::key_image_y &v) {
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
}
inline std::ostream &operator <<(std::ostream &o, const crypto::signature &v) {
epee::to_hex::formatted(o, epee::as_byte_span(v)); return o;
}
@@ -345,6 +361,8 @@ namespace crypto {
inline bool operator>(const public_key &p1, const public_key &p2) { return p2 < p1; }
inline bool operator<(const key_image &p1, const key_image &p2) { return memcmp(&p1, &p2, sizeof(key_image)) < 0; }
inline bool operator>(const key_image &p1, const key_image &p2) { return p2 < p1; }
inline bool operator<(const key_image_y &p1, const key_image_y &p2) { return memcmp(&p1, &p2, sizeof(key_image_y)) < 0; }
inline bool operator>(const key_image_y &p1, const key_image_y &p2) { return p2 < p1; }
}
// type conversions for easier calls to sc_add(), sc_sub(), hash functions
@@ -357,5 +375,6 @@ CRYPTO_MAKE_HASHABLE(public_key)
CRYPTO_MAKE_HASHABLE_CONSTANT_TIME(secret_key)
CRYPTO_MAKE_HASHABLE_CONSTANT_TIME(public_key_memsafe)
CRYPTO_MAKE_HASHABLE(key_image)
CRYPTO_MAKE_HASHABLE(key_image_y)
CRYPTO_MAKE_COMPARABLE(signature)
CRYPTO_MAKE_COMPARABLE(view_tag)
CRYPTO_MAKE_COMPARABLE(view_tag)