Kagome
Polkadot Runtime Engine in C++17
ed25519_provider_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include <random>
9 
10 extern "C" {
11 #include <schnorrkel/schnorrkel.h>
12 }
13 
16  switch (e) {
17  case E::VERIFICATION_FAILED:
18  return "Internal error during ed25519 signature verification";
19  case E::SIGN_FAILED:
20  return "Internal error during ed25519 signing";
21  }
22  return "Unknown error in ed25519 provider";
23 }
24 
25 namespace kagome::crypto {
26 
27  Ed25519ProviderImpl::Ed25519ProviderImpl(std::shared_ptr<CSPRNG> generator)
28  : generator_{std::move(generator)},
29  logger_{log::createLogger("Ed25519Provider", "ed25519")} {
30  BOOST_ASSERT(generator_ != nullptr);
31  }
32 
34  Ed25519Seed seed;
35  generator_->fillRandomly(seed);
36  return Ed25519KeypairAndSeed{generateKeypair(seed), seed};
37  }
38 
40  const Ed25519Seed &seed) const {
41  std::array<uint8_t, ED25519_KEYPAIR_LENGTH> kp_bytes{};
42  ed25519_keypair_from_seed(kp_bytes.data(), seed.data());
43  Ed25519Keypair kp;
44  std::copy_n(
45  kp_bytes.begin(), ED25519_SECRET_KEY_LENGTH, kp.secret_key.begin());
46  std::copy_n(kp_bytes.begin() + ED25519_SECRET_KEY_LENGTH,
47  ED25519_PUBLIC_KEY_LENGTH,
48  kp.public_key.begin());
49  return kp;
50  }
51 
52  outcome::result<Ed25519Signature> Ed25519ProviderImpl::sign(
53  const Ed25519Keypair &keypair, gsl::span<const uint8_t> message) const {
54  Ed25519Signature sig;
55  std::array<uint8_t, ED25519_KEYPAIR_LENGTH> keypair_bytes;
56  std::copy(keypair.secret_key.begin(),
57  keypair.secret_key.end(),
58  keypair_bytes.begin());
59  std::copy(keypair.public_key.begin(),
60  keypair.public_key.end(),
61  keypair_bytes.begin() + ED25519_SECRET_KEY_LENGTH);
62  auto res = ed25519_sign(
63  sig.data(), keypair_bytes.data(), message.data(), message.size_bytes());
64  if (res != ED25519_RESULT_OK) {
65  logger_->error("Error during ed25519 sign; error code: {}", res);
66  return Error::SIGN_FAILED;
67  }
68  return sig;
69  }
70  outcome::result<bool> Ed25519ProviderImpl::verify(
71  const Ed25519Signature &signature,
72  gsl::span<const uint8_t> message,
73  const Ed25519PublicKey &public_key) const {
74  auto res = ed25519_verify(signature.data(),
75  public_key.data(),
76  message.data(),
77  message.size_bytes());
78  if (res == ED25519_RESULT_OK) {
79  return true;
80  }
81  if (res == ED25519_RESULT_VERIFICATION_FAILED) {
82  return false;
83  }
84  logger_->error("Error verifying a signature; error code: {}", res);
86  }
87 } // namespace kagome::crypto
outcome::result< bool > verify(const Ed25519Signature &signature, gsl::span< const uint8_t > message, const Ed25519PublicKey &public_key) const override
OUTCOME_CPP_DEFINE_CATEGORY(kagome::crypto, Ed25519ProviderImpl::Error, e)
Ed25519KeypairAndSeed generateKeypair() const override
Ed25519ProviderImpl(std::shared_ptr< CSPRNG > generator)
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
outcome::result< Ed25519Signature > sign(const Ed25519Keypair &keypair, gsl::span< const uint8_t > message) const override