Skip to content

Commit

Permalink
crypto: update eclib interface to implement ed25519 (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB committed Jul 14, 2023
1 parent 9c67966 commit 62fbd49
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ set(crypto_sources
crypto-ops-data.c
crypto-ops.c
crypto.cpp
eclib_ed25519.cpp
eclib_interface.cpp
eclib_test.cpp
generators.cpp
groestl.c
hash-extra-blake.c
Expand Down
12 changes: 9 additions & 3 deletions src/crypto/eclib_test.cpp → src/crypto/eclib_ed25519.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include "eclib_test.h"
#include "eclib_ed25519.h"

extern "C"
{
#include "crypto-ops.h"
}
#include "crypto.h"

namespace crypto
{
//-------------------------------------------------------------------------------------------------------------------
void eclib_test::core_func(const eclib_test::key &k, eclib_test::key &key_out)
void eclib_ed25519::test_func(const eclib_ed25519::secret_key &k, eclib_ed25519::secret_key &key_out)
{
key_out = k*10;
key_out = k;
}
//-------------------------------------------------------------------------------------------------------------------
} //namespace crypto
35 changes: 30 additions & 5 deletions src/crypto/eclib_test.h → src/crypto/eclib_ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,45 @@

#pragma once

extern "C"
{
#include "crypto-ops.h"
}
#include "crypto.h"
#include "eclib_utils.h"

namespace crypto
{

struct eclib_test final
struct eclib_ed25519 final
{
/// crypto utils
using utils = eclib_utils<eclib_ed25519>;

/// core group element types
using ge_deserialized = ge_p3;
using ge_intermediate1 = ge_p2;
using ge_intermediate2 = ge_p1p1;
using ge_precomp = ge_precomp;
using ge_cached = ge_cached;

/// crypto types
using scalar = crypto::ec_scalar;
using secret_key = crypto::secret_key;
using public_key = crypto::public_key;
using key_image = crypto::key_image;
using key_derivation = crypto::key_derivation;

using utils = eclib_utils<eclib_test>;
/// byte access
static inline unsigned char* to_bytes(scalar &sc) { return ::to_bytes(sc); }
static inline const unsigned char* to_bytes(const scalar &sc) { return ::to_bytes(sc); }
static inline unsigned char* to_bytes(public_key &pk) { return ::to_bytes(pk); }
static inline const unsigned char* to_bytes(const public_key &pk) { return ::to_bytes(pk); }

using key = unsigned char;
/// crypto ops

static void core_func(const key &k, key &key_out);
static void test_func(const secret_key &k, secret_key &key_out);

}; //eclib_test
}; //eclib_ed25519

} //namespace crypto
65 changes: 54 additions & 11 deletions src/crypto/eclib_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include "eclib_test.h"
#include "eclib_ed25519.h"
#include "eclib_utils.h"
#include "eclib_utils.inl" //should be included in ONLY this file

Expand All @@ -38,33 +38,76 @@ namespace crypto
template <typename LIBT>
static void eclib_interface()
{
// dummy variables
unsigned char* uchar_ptr = nullptr;
const unsigned char* const_uchar_ptr = nullptr;
bool boolean = false;

// core group element types
typename LIBT::ge_deserialized GE_DESERIALIZED{};
const typename LIBT::ge_deserialized CONST_GE_DESERIALIZED{};

typename LIBT::ge_intermediate1 GE_INTERMEDIATE1{};
const typename LIBT::ge_intermediate1 CONST_GE_INTERMEDIATE1{};

typename LIBT::ge_intermediate2 GE_INTERMEDIATE2{};
const typename LIBT::ge_intermediate2 CONST_GE_INTERMEDIATE2{};

typename LIBT::ge_precomp GE_PRECOMP{};
const typename LIBT::ge_precomp CONST_GE_PRECOMP{};

typename LIBT::ge_cached GE_CACHED{};
const typename LIBT::ge_cached CONST_GE_CACHED{};

// eclib types
typename LIBT::key KEY{};
const typename LIBT::key CONST_KEY{};
typename LIBT::scalar SCALAR{};
const typename LIBT::scalar CONST_SCALAR{};

typename LIBT::secret_key SECRET_KEY{};
const typename LIBT::secret_key CONST_SECRET_KEY{};

typename LIBT::public_key PUBLIC_KEY{};
const typename LIBT::public_key CONST_PUBLIC_KEY{};

typename LIBT::key_image KEY_IMAGE{};
const typename LIBT::key_image CONST_KEY_IMAGE{};

typename LIBT::key_derivation KEY_DERIVATION{};
const typename LIBT::key_derivation CONST_KEY_DERIVATION{};

// operators that should be implemented
boolean = PUBLIC_KEY < PUBLIC_KEY;
boolean = PUBLIC_KEY > PUBLIC_KEY;
boolean = KEY_IMAGE < KEY_IMAGE;
boolean = KEY_IMAGE > KEY_IMAGE;

// byte access
uchar_ptr = LIBT::to_bytes(SCALAR);
const_uchar_ptr = LIBT::to_bytes(CONST_SCALAR);
uchar_ptr = LIBT::to_bytes(SECRET_KEY);
const_uchar_ptr = LIBT::to_bytes(CONST_SECRET_KEY);
uchar_ptr = LIBT::to_bytes(PUBLIC_KEY);
const_uchar_ptr = LIBT::to_bytes(CONST_PUBLIC_KEY);

// eclib functions
LIBT::core_func(CONST_KEY, KEY);
LIBT::test_func(CONST_SECRET_KEY, SECRET_KEY);

// eclib::utils functions
LIBT::utils::util_func(CONST_KEY, KEY);
LIBT::utils::util_test_func(CONST_SECRET_KEY, SECRET_KEY);
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------------------------------------
// instantiate the utils for each eclib type
//-------------------------------------------------------------------------------------------------------------------
template struct eclib_utils<eclib_test>;
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------

template struct eclib_utils<eclib_ed25519>;
//-------------------------------------------------------------------------------------------------------------------
// expect the interface to compile for each eclib type
//-------------------------------------------------------------------------------------------------------------------
void eclib_interfaces_impl()
{
eclib_interface<eclib_test>();
eclib_interface<eclib_ed25519>();
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
} //namespace crypto
6 changes: 1 addition & 5 deletions src/crypto/eclib_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ namespace crypto
template <typename LIBT>
struct eclib_utils
{
static void util_func(const typename LIBT::key &k, typename LIBT::key &key_inout);
static void util_test_func(const typename LIBT::secret_key &k, typename LIBT::secret_key &key_out);
};


//template <typename LIBT>
//void eclib_utils<LIBT>::util_func(const typename LIBT::key &k, typename LIBT::key &key_inout);

} //namespace crypto
4 changes: 2 additions & 2 deletions src/crypto/eclib_utils.inl
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ namespace crypto
{
//-------------------------------------------------------------------------------------------------------------------
template <typename LIBT>
void eclib_utils<LIBT>::util_func(const typename LIBT::key &k, typename LIBT::key &key_inout)
void eclib_utils<LIBT>::util_test_func(const typename LIBT::secret_key &k, typename LIBT::secret_key &key_out)
{
key_inout = k + 20;
key_out = k;
}
//-------------------------------------------------------------------------------------------------------------------
} //namespace crypto
16 changes: 8 additions & 8 deletions tests/unit_tests/seraphis_crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extern "C"
{
#include "crypto/crypto-ops.h"
}
#include "crypto/eclib_test.h"
#include "crypto/eclib_ed25519.h"
#include "crypto/generators.h"
#include "crypto/x25519.h"
#include "device/device.hpp"
Expand Down Expand Up @@ -296,14 +296,14 @@ TEST(seraphis_crypto, multiexp_utility)
//-------------------------------------------------------------------------------------------------------------------
TEST(seraphis_crypto, eclib_test)
{
using eclib = crypto::eclib_test;
using eclib = crypto::eclib_ed25519;

const eclib::key constant{20};
eclib::key temp;
const eclib::secret_key constant{};
eclib::secret_key temp;

eclib::core_func(constant, temp);
EXPECT_TRUE(temp == 200);
eclib::utils::util_func(constant, temp);
EXPECT_TRUE(temp == 40);
eclib::test_func(constant, temp);
EXPECT_TRUE(temp == constant);
eclib::utils::util_test_func(constant, temp);
EXPECT_TRUE(temp == constant);
}
//-------------------------------------------------------------------------------------------------------------------

0 comments on commit 62fbd49

Please sign in to comment.