Kagome
Polkadot Runtime Engine in C++17
crypto_suites.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CRYPTO_SUITES_HPP
7 #define KAGOME_CRYPTO_SUITES_HPP
8 
13 
14 namespace kagome::crypto {
15 
19  template <typename PublicKeyT,
20  typename PrivateKeyT,
21  typename KeypairT,
22  typename KeypairAndSeedT,
23  typename SeedT>
24  struct CryptoSuite {
25  using PublicKey = PublicKeyT;
26  using PrivateKey = PrivateKeyT;
27  using Keypair = KeypairT;
28  using KeypairAndSeed = KeypairAndSeedT;
29  using Seed = SeedT;
30 
31  virtual ~CryptoSuite() = default;
32 
37  virtual outcome::result<Keypair> generateKeypair(
38  const Seed &seed) const noexcept = 0;
39 
44  virtual outcome::result<KeypairAndSeed> generateRandomKeypair()
45  const noexcept = 0;
46 
53  virtual Keypair composeKeypair(PublicKey pub,
54  PrivateKey priv) const noexcept = 0;
59  virtual std::pair<PublicKey, PrivateKey> decomposeKeypair(
60  const Keypair &kp) const noexcept = 0;
61 
65  virtual outcome::result<PublicKey> toPublicKey(
66  gsl::span<const uint8_t> bytes) const noexcept = 0;
67 
71  virtual outcome::result<Seed> toSeed(
72  gsl::span<const uint8_t> bytes) const noexcept = 0;
73  };
74 
75  class EcdsaSuite : public CryptoSuite<EcdsaPublicKey,
76  EcdsaPrivateKey,
79  EcdsaSeed> {
80  public:
81  explicit EcdsaSuite(std::shared_ptr<EcdsaProvider> ecdsa_provider)
82  : ecdsa_provider_{std::move(ecdsa_provider)} {
83  BOOST_ASSERT(ecdsa_provider_ != nullptr);
84  }
85 
86  ~EcdsaSuite() override = default;
87 
88  outcome::result<EcdsaKeypairAndSeed> generateRandomKeypair()
89  const noexcept override {
90  return ecdsa_provider_->generate();
91  }
92 
93  outcome::result<EcdsaKeypair> generateKeypair(
94  const EcdsaSeed &seed) const noexcept override {
95  OUTCOME_TRY(public_key, ecdsa_provider_->derive(seed));
96  return composeKeypair(public_key, EcdsaPrivateKey{seed});
97  }
98 
99  EcdsaKeypair composeKeypair(PublicKey pub,
100  PrivateKey priv) const noexcept override {
101  return EcdsaKeypair{.secret_key = std::move(priv),
102  .public_key = std::move(pub)};
103  }
104 
105  std::pair<PublicKey, PrivateKey> decomposeKeypair(
106  const EcdsaKeypair &kp) const noexcept override {
107  return {kp.public_key, kp.secret_key};
108  }
109 
110  outcome::result<PublicKey> toPublicKey(
111  gsl::span<const uint8_t> bytes) const noexcept override {
112  OUTCOME_TRY(blob, EcdsaPublicKey::fromSpan(bytes));
113  return EcdsaPublicKey{blob};
114  }
115 
116  outcome::result<Seed> toSeed(
117  gsl::span<const uint8_t> bytes) const noexcept override {
118  return EcdsaSeed::fromSpan(bytes);
119  }
120 
121  private:
122  std::shared_ptr<EcdsaProvider> ecdsa_provider_;
123  };
124 
125  class Ed25519Suite : public CryptoSuite<Ed25519PublicKey,
126  Ed25519PrivateKey,
127  Ed25519Keypair,
128  Ed25519KeypairAndSeed,
129  Ed25519Seed> {
130  public:
131  explicit Ed25519Suite(std::shared_ptr<Ed25519Provider> ed_provider)
132  : ed_provider_{std::move(ed_provider)} {
133  BOOST_ASSERT(ed_provider_ != nullptr);
134  }
135 
136  ~Ed25519Suite() override = default;
137 
138  outcome::result<Ed25519KeypairAndSeed> generateRandomKeypair()
139  const noexcept override {
140  return ed_provider_->generateKeypair();
141  }
142 
143  outcome::result<Ed25519Keypair> generateKeypair(
144  const Ed25519Seed &seed) const noexcept override {
145  return ed_provider_->generateKeypair(seed);
146  }
147 
149  PrivateKey priv) const noexcept override {
150  return Ed25519Keypair{.secret_key = std::move(priv),
151  .public_key = std::move(pub)};
152  }
153 
154  std::pair<PublicKey, PrivateKey> decomposeKeypair(
155  const Ed25519Keypair &kp) const noexcept override {
156  return {kp.public_key, kp.secret_key};
157  }
158 
159  outcome::result<PublicKey> toPublicKey(
160  gsl::span<const uint8_t> bytes) const noexcept override {
161  OUTCOME_TRY(blob, Ed25519PublicKey::fromSpan(bytes));
162  return Ed25519PublicKey{blob};
163  }
164 
165  outcome::result<Seed> toSeed(
166  gsl::span<const uint8_t> bytes) const noexcept override {
167  return Ed25519Seed::fromSpan(bytes);
168  }
169 
170  private:
171  std::shared_ptr<Ed25519Provider> ed_provider_;
172  };
173 
174  class Sr25519Suite : public CryptoSuite<Sr25519PublicKey,
175  Sr25519SecretKey,
176  Sr25519Keypair,
177  Sr25519KeypairAndSeed,
178  Sr25519Seed> {
179  public:
180  explicit Sr25519Suite(std::shared_ptr<Sr25519Provider> sr_provider)
181  : sr_provider_{std::move(sr_provider)} {
182  BOOST_ASSERT(sr_provider_ != nullptr);
183  }
184 
185  ~Sr25519Suite() override = default;
186 
187  outcome::result<Sr25519KeypairAndSeed> generateRandomKeypair()
188  const noexcept override {
189  return sr_provider_->generateKeypair();
190  }
191 
192  outcome::result<Sr25519Keypair> generateKeypair(
193  const Sr25519Seed &seed) const noexcept override {
194  return sr_provider_->generateKeypair(seed);
195  }
196 
198  PrivateKey priv) const noexcept override {
199  return Sr25519Keypair{.secret_key = std::move(priv),
200  .public_key = std::move(pub)};
201  }
202 
203  std::pair<PublicKey, PrivateKey> decomposeKeypair(
204  const Sr25519Keypair &kp) const noexcept override {
205  return {kp.public_key, kp.secret_key};
206  }
207 
208  outcome::result<PublicKey> toPublicKey(
209  gsl::span<const uint8_t> bytes) const noexcept override {
210  OUTCOME_TRY(blob, Sr25519PublicKey::fromSpan(bytes));
211  return Sr25519PublicKey{std::move(blob)};
212  }
213 
214  outcome::result<Seed> toSeed(
215  gsl::span<const uint8_t> bytes) const noexcept override {
216  return Sr25519Seed::fromSpan(bytes);
217  }
218 
219  private:
220  std::shared_ptr<Sr25519Provider> sr_provider_;
221  };
222 
223 } // namespace kagome::crypto
224 
225 #endif // KAGOME_CRYPTO_SUITES_HPP
EcdsaSuite(std::shared_ptr< EcdsaProvider > ecdsa_provider)
outcome::result< Sr25519Keypair > generateKeypair(const Sr25519Seed &seed) const noexceptoverride
Sr25519Keypair composeKeypair(PublicKey pub, PrivateKey priv) const noexceptoverride
std::shared_ptr< EcdsaProvider > ecdsa_provider_
Sr25519Suite(std::shared_ptr< Sr25519Provider > sr_provider)
Ed25519Keypair composeKeypair(PublicKey pub, PrivateKey priv) const noexceptoverride
STL namespace.
virtual outcome::result< Seed > toSeed(gsl::span< const uint8_t > bytes) const noexcept=0
virtual outcome::result< KeypairAndSeed > generateRandomKeypair() const noexcept=0
outcome::result< Seed > toSeed(gsl::span< const uint8_t > bytes) const noexceptoverride
virtual outcome::result< PublicKey > toPublicKey(gsl::span< const uint8_t > bytes) const noexcept=0
virtual Keypair composeKeypair(PublicKey pub, PrivateKey priv) const noexcept=0
outcome::result< Ed25519KeypairAndSeed > generateRandomKeypair() const noexceptoverride
outcome::result< Ed25519Keypair > generateKeypair(const Ed25519Seed &seed) const noexceptoverride
std::shared_ptr< Sr25519Provider > sr_provider_
static outcome::result< Blob< size_ > > fromSpan(const gsl::span< const uint8_t > &span)
Definition: blob.hpp:208
outcome::result< Seed > toSeed(gsl::span< const uint8_t > bytes) const noexceptoverride
outcome::result< PublicKey > toPublicKey(gsl::span< const uint8_t > bytes) const noexceptoverride
Ed25519Suite(std::shared_ptr< Ed25519Provider > ed_provider)
std::shared_ptr< Ed25519Provider > ed_provider_
std::pair< PublicKey, PrivateKey > decomposeKeypair(const EcdsaKeypair &kp) const noexceptoverride
outcome::result< Sr25519KeypairAndSeed > generateRandomKeypair() const noexceptoverride
outcome::result< EcdsaKeypair > generateKeypair(const EcdsaSeed &seed) const noexceptoverride
outcome::result< PublicKey > toPublicKey(gsl::span< const uint8_t > bytes) const noexceptoverride
outcome::result< PublicKey > toPublicKey(gsl::span< const uint8_t > bytes) const noexceptoverride
virtual outcome::result< Keypair > generateKeypair(const Seed &seed) const noexcept=0
outcome::result< Seed > toSeed(gsl::span< const uint8_t > bytes) const noexceptoverride
std::pair< PublicKey, PrivateKey > decomposeKeypair(const Ed25519Keypair &kp) const noexceptoverride
outcome::result< EcdsaKeypairAndSeed > generateRandomKeypair() const noexceptoverride
virtual std::pair< PublicKey, PrivateKey > decomposeKeypair(const Keypair &kp) const noexcept=0
virtual ~CryptoSuite()=default
EcdsaKeypair composeKeypair(PublicKey pub, PrivateKey priv) const noexceptoverride
std::pair< PublicKey, PrivateKey > decomposeKeypair(const Sr25519Keypair &kp) const noexceptoverride