Kagome
Polkadot Runtime Engine in C++17
crypto_extension.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CRYPTO_EXTENSION_HPP
7 #define KAGOME_CRYPTO_EXTENSION_HPP
8 
9 #include <future>
10 #include <optional>
11 #include <queue>
12 
14 #include "crypto/crypto_store.hpp"
15 #include "log/logger.hpp"
17 #include "runtime/types.hpp"
18 
19 namespace kagome::crypto {
20  class Sr25519Provider;
21  class EcdsaProvider;
22  class Ed25519Provider;
23  class Secp256k1Provider;
24  class Hasher;
25  class Bip39Provider;
26  class CryptoStore;
27 } // namespace kagome::crypto
28 
29 namespace kagome::host_api {
34  public:
35  static constexpr uint32_t kVerifyBatchSuccess = 1;
36  static constexpr uint32_t kVerifyBatchFail = 0;
37  static constexpr uint32_t kVerifySuccess = 1;
38  static constexpr uint32_t kVerifyFail = 0;
39 
41  std::shared_ptr<const runtime::MemoryProvider> memory_provider,
42  std::shared_ptr<const crypto::Sr25519Provider> sr25519_provider,
43  std::shared_ptr<const crypto::EcdsaProvider> ecdsa_provider,
44  std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
45  std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider,
46  std::shared_ptr<const crypto::Hasher> hasher,
47  std::shared_ptr<crypto::CryptoStore> crypto_store,
48  std::shared_ptr<const crypto::Bip39Provider> bip39_provider);
49 
50  // -------------------- hashing methods v1 --------------------
51 
55  runtime::WasmPointer ext_hashing_keccak_256_version_1(
56  runtime::WasmSpan data);
57 
61  runtime::WasmPointer ext_hashing_sha2_256_version_1(runtime::WasmSpan data);
62 
66  runtime::WasmPointer ext_hashing_blake2_128_version_1(
67  runtime::WasmSpan data);
68 
72  runtime::WasmPointer ext_hashing_blake2_256_version_1(
73  runtime::WasmSpan data);
74 
78  runtime::WasmPointer ext_hashing_twox_64_version_1(runtime::WasmSpan data);
79 
83  runtime::WasmPointer ext_hashing_twox_128_version_1(runtime::WasmSpan data);
84 
88  runtime::WasmPointer ext_hashing_twox_256_version_1(runtime::WasmSpan data);
89 
90  // -------------------- crypto methods v1 --------------------
91 
92  void ext_crypto_start_batch_verify_version_1();
93 
94  [[nodiscard]] runtime::WasmSize ext_crypto_finish_batch_verify_version_1();
95 
99  runtime::WasmSpan ext_crypto_ed25519_public_keys_version_1(
100  runtime::WasmSize key_type);
101 
105  runtime::WasmPointer ext_crypto_ed25519_generate_version_1(
106  runtime::WasmSize key_type, runtime::WasmSpan seed);
107 
111  runtime::WasmSpan ext_crypto_ed25519_sign_version_1(
112  runtime::WasmSize key_type,
114  runtime::WasmSpan msg);
115 
119  runtime::WasmSize ext_crypto_ed25519_verify_version_1(
121  runtime::WasmSpan msg,
122  runtime::WasmPointer pubkey_data);
123 
127  runtime::WasmSpan ext_crypto_sr25519_public_keys_version_1(
128  runtime::WasmSize key_type);
129 
133  runtime::WasmPointer ext_crypto_sr25519_generate_version_1(
134  runtime::WasmSize key_type, runtime::WasmSpan seed);
135 
139  runtime::WasmSpan ext_crypto_sr25519_sign_version_1(
140  runtime::WasmSize key_type,
142  runtime::WasmSpan msg);
143 
147  int32_t ext_crypto_sr25519_verify_version_1(
149  runtime::WasmSpan msg,
150  runtime::WasmPointer pubkey_data);
151 
152  int32_t ext_crypto_sr25519_verify_version_2(
154  runtime::WasmSpan msg,
155  runtime::WasmPointer pubkey_data);
156 
160  runtime::WasmSpan ext_crypto_secp256k1_ecdsa_recover_version_1(
162 
166  runtime::WasmSpan ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(
168 
172  runtime::WasmSpan ext_crypto_ecdsa_public_keys_version_1(
173  runtime::WasmSize key_type);
174 
178  runtime::WasmSpan ext_crypto_ecdsa_sign_version_1(
179  runtime::WasmSize key_type,
181  runtime::WasmSpan msg);
182 
186  runtime::WasmSpan ext_crypto_ecdsa_sign_prehashed_version_1(
187  runtime::WasmSize key_type,
189  runtime::WasmSpan msg);
190 
194  runtime::WasmPointer ext_crypto_ecdsa_generate_version_1(
195  runtime::WasmSize key_type_id, runtime::WasmSpan seed) const;
196 
200  int32_t ext_crypto_ecdsa_verify_version_1(runtime::WasmPointer sig,
201  runtime::WasmSpan msg,
202  runtime::WasmPointer key) const;
203 
207  int32_t ext_crypto_ecdsa_verify_prehashed_version_1(
209  runtime::WasmSpan msg,
210  runtime::WasmPointer key) const;
211 
212  private:
213  common::Blob<32> deriveSeed(std::string_view content);
214 
216  return memory_provider_->getCurrentMemory()->get();
217  }
218 
219  std::shared_ptr<const runtime::MemoryProvider> memory_provider_;
220  std::shared_ptr<const crypto::Sr25519Provider> sr25519_provider_;
221  std::shared_ptr<const crypto::EcdsaProvider> ecdsa_provider_;
222  std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider_;
223  std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider_;
224  std::shared_ptr<const crypto::Hasher> hasher_;
225  std::shared_ptr<crypto::CryptoStore> crypto_store_;
226  std::shared_ptr<const crypto::Bip39Provider> bip39_provider_;
228  };
229 } // namespace kagome::host_api
230 
231 #endif // KAGOME_CRYPTO_EXTENSION_HPP
uint32_t WasmSize
Size type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:35
std::shared_ptr< const crypto::Ed25519Provider > ed25519_provider_
std::shared_ptr< const crypto::Secp256k1Provider > secp256k1_provider_
runtime::Memory & getMemory() const
std::shared_ptr< crypto::CryptoStore > crypto_store_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
std::shared_ptr< const crypto::Sr25519Provider > sr25519_provider_
uint32_t WasmPointer
type of wasm memory is 32 bit integer
Definition: types.hpp:26
std::shared_ptr< const crypto::Bip39Provider > bip39_provider_
std::shared_ptr< const crypto::EcdsaProvider > ecdsa_provider_
std::shared_ptr< const runtime::MemoryProvider > memory_provider_
std::shared_ptr< const crypto::Hasher > hasher_