Electroneum
crypto.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
33 
34 #include <cstddef>
35 #include <boost/thread/mutex.hpp>
36 #include <boost/thread/lock_guard.hpp>
37 #include <vector>
38 
39 #include "common/pod-class.h"
40 #include "generic-ops.h"
41 #include "hash.h"
42 
43 namespace crypto {
44 
45  extern "C" {
46 #include "random.h"
47  }
48 
49  extern boost::mutex random_lock;
50 
51 #pragma pack(push, 1)
53  char data[32];
54  };
55 
57  char data[32];
58  };
59 
61  friend class crypto_ops;
62  };
63 
65  friend class crypto_ops;
66  };
67 
69  std::vector<public_key> keys;
70  int rows;
71  };
72 
74  std::vector<secret_key> keys;
75  int rows;
76  };
77 
79  int cols;
80  int rows;
81  std::vector<secret_keyV> column_vectors;
82  };
83 
85  friend class crypto_ops;
86  };
87 
89  friend class crypto_ops;
90  };
91 
93  ec_scalar c, r;
94  friend class crypto_ops;
95  };
96 #pragma pack(pop)
97 
98  static_assert(sizeof(ec_point) == 32 && sizeof(ec_scalar) == 32 &&
99  sizeof(public_key) == 32 && sizeof(secret_key) == 32 &&
100  sizeof(key_derivation) == 32 && sizeof(key_image) == 32 &&
101  sizeof(signature) == 64, "Invalid structure size");
102 
103  class crypto_ops {
104  crypto_ops();
105  crypto_ops(const crypto_ops &);
106  void operator=(const crypto_ops &);
107  ~crypto_ops();
108 
109  static secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false);
110  friend secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover);
111  static bool check_key(const public_key &);
112  friend bool check_key(const public_key &);
113  static bool secret_key_to_public_key(const secret_key &, public_key &);
114  friend bool secret_key_to_public_key(const secret_key &, public_key &);
115  static bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &);
116  friend bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &);
117  static void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res);
118  friend void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res);
119  static bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &);
120  friend bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &);
121  static void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &);
122  friend void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &);
123  static void generate_signature(const hash &, const public_key &, const secret_key &, signature &);
124  friend void generate_signature(const hash &, const public_key &, const secret_key &, signature &);
125  static bool check_signature(const hash &, const public_key &, const signature &);
126  friend bool check_signature(const hash &, const public_key &, const signature &);
127  static void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &);
128  friend void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &);
129  static bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &);
130  friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &);
131  static void generate_key_image(const public_key &, const secret_key &, key_image &);
132  friend void generate_key_image(const public_key &, const secret_key &, key_image &);
133  static void generate_ring_signature(const hash &, const key_image &,
134  const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *);
135  friend void generate_ring_signature(const hash &, const key_image &,
136  const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *);
137  static bool check_ring_signature(const hash &, const key_image &,
138  const public_key *const *, std::size_t, const signature *);
139  friend bool check_ring_signature(const hash &, const key_image &,
140  const public_key *const *, std::size_t, const signature *);
141  };
142 
143  /* Generate N random bytes
144  */
145  inline void rand(size_t N, uint8_t *bytes) {
146  boost::lock_guard<boost::mutex> lock(random_lock);
148  }
149 
150  /* Generate a value filled with random bytes.
151  */
152  template<typename T>
153  typename std::enable_if<std::is_pod<T>::value, T>::type rand() {
154  typename std::remove_cv<T>::type res;
155  boost::lock_guard<boost::mutex> lock(random_lock);
157  return res;
158  }
159 
160  /* Generate a new key pair
161  */
162  inline secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key = secret_key(), bool recover = false) {
163  return crypto_ops::generate_keys(pub, sec, recovery_key, recover);
164  }
165 
166  /* Check a public key. Returns true if it is valid, false otherwise.
167  */
168  inline bool check_key(const public_key &key) {
169  return crypto_ops::check_key(key);
170  }
171 
172  /* Checks a private key and computes the corresponding public key.
173  */
174  inline bool secret_key_to_public_key(const secret_key &sec, public_key &pub) {
175  return crypto_ops::secret_key_to_public_key(sec, pub);
176  }
177 
178  /* To generate an ephemeral key used to send money to:
179  * * The sender generates a new key pair, which becomes the transaction key. The public transaction key is included in "extra" field.
180  * * Both the sender and the receiver generate key derivation from the transaction key, the receivers' "view" key and the output index.
181  * * The sender uses key derivation and the receivers' "spend" key to derive an ephemeral public key.
182  * * The receiver can either derive the public key (to check that the transaction is addressed to him) or the private key (to spend the money).
183  */
184  inline bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation) {
185  return crypto_ops::generate_key_derivation(key1, key2, derivation);
186  }
187  inline bool derive_public_key(const key_derivation &derivation, std::size_t output_index,
188  const public_key &base, public_key &derived_key) {
189  return crypto_ops::derive_public_key(derivation, output_index, base, derived_key);
190  }
191  inline void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res) {
192  return crypto_ops::derivation_to_scalar(derivation, output_index, res);
193  }
194  inline void derive_secret_key(const key_derivation &derivation, std::size_t output_index,
195  const secret_key &base, secret_key &derived_key) {
196  crypto_ops::derive_secret_key(derivation, output_index, base, derived_key);
197  }
198 
199  /* Generation and checking of a standard signature.
200  */
201  inline void generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) {
202  crypto_ops::generate_signature(prefix_hash, pub, sec, sig);
203  }
204  inline bool check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig) {
205  return crypto_ops::check_signature(prefix_hash, pub, sig);
206  }
207 
208  /* Generation and checking of a tx proof; given a tx pubkey R, the recipient's view pubkey A, and the key
209  * derivation D, the signature proves the knowledge of the tx secret key r such that R=r*G and D=r*A
210  */
211  inline void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const secret_key &r, signature &sig) {
212  crypto_ops::generate_tx_proof(prefix_hash, R, A, D, r, sig);
213  }
214  inline bool check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const signature &sig) {
215  return crypto_ops::check_tx_proof(prefix_hash, R, A, D, sig);
216  }
217 
218  /* To send money to a key:
219  * * The sender generates an ephemeral key and includes it in transaction output.
220  * * To spend the money, the receiver generates a key image from it.
221  * * Then he selects a bunch of outputs, including the one he spends, and uses them to generate a ring signature.
222  * To check the signature, it is necessary to collect all the keys that were used to generate it. To detect double spends, it is necessary to check that each key image is used at most once.
223  */
224  inline void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image) {
225  crypto_ops::generate_key_image(pub, sec, image);
226  }
227  inline void generate_ring_signature(const hash &prefix_hash, const key_image &image,
228  const public_key *const *pubs, std::size_t pubs_count,
229  const secret_key &sec, std::size_t sec_index,
230  signature *sig) {
231  crypto_ops::generate_ring_signature(prefix_hash, image, pubs, pubs_count, sec, sec_index, sig);
232  }
233  inline bool check_ring_signature(const hash &prefix_hash, const key_image &image,
234  const public_key *const *pubs, std::size_t pubs_count,
235  const signature *sig) {
236  return crypto_ops::check_ring_signature(prefix_hash, image, pubs, pubs_count, sig);
237  }
238 
239  /* Variants with vector<const public_key *> parameters.
240  */
241  inline void generate_ring_signature(const hash &prefix_hash, const key_image &image,
242  const std::vector<const public_key *> &pubs,
243  const secret_key &sec, std::size_t sec_index,
244  signature *sig) {
245  generate_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sec, sec_index, sig);
246  }
247  inline bool check_ring_signature(const hash &prefix_hash, const key_image &image,
248  const std::vector<const public_key *> &pubs,
249  const signature *sig) {
250  return check_ring_signature(prefix_hash, image, pubs.data(), pubs.size(), sig);
251  }
252 }
253 
void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res)
Definition: crypto.h:191
POD_CLASS ec_point
Definition: crypto.h:52
const uint32_t T[512]
Definition: groestl_tables.h:34
void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const secret_key &r, signature &sig)
Definition: crypto.h:211
#define CRYPTO_MAKE_COMPARABLE(type)
Definition: generic-ops.h:38
static bool derive_public_key(const key_derivation &, std::size_t, const public_key &, public_key &)
void derive_secret_key(const key_derivation &derivation, std::size_t output_index, const secret_key &base, secret_key &derived_key)
Definition: crypto.h:194
Definition: crypto.h:103
static bool secret_key_to_public_key(const secret_key &, public_key &)
Definition: crypto.cpp:128
static bool check_signature(const hash &, const public_key &, const signature &)
Definition: crypto.cpp:229
static void generate_key_image(const public_key &, const secret_key &, key_image &)
Definition: crypto.cpp:378
POD_CLASS key_derivation
Definition: crypto.h:82
static bool check_key(const public_key &)
Definition: crypto.cpp:123
bool check_ring_signature(const hash &prefix_hash, const key_image &image, const public_key *const *pubs, std::size_t pubs_count, const signature *sig)
Definition: crypto.h:233
void generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig)
Definition: crypto.h:201
crypto namespace.
Definition: crypto.cpp:47
POD_CLASS secret_keyV
Definition: crypto.h:73
#define POD_CLASS
Definition: pod-class.h:44
bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation)
Definition: crypto.h:184
void generate_random_bytes_not_thread_safe(size_t n, void *result)
Definition: random.c:117
boost::mutex random_lock
Definition: crypto.cpp:61
void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image)
Definition: crypto.h:224
void rand(size_t N, uint8_t *bytes)
Definition: crypto.h:145
POD_CLASS public_keyM
Definition: crypto.h:78
POD_CLASS ec_scalar
Definition: crypto.h:56
static void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &)
Definition: crypto.cpp:250
static secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key &recovery_key=secret_key(), bool recover=false)
Definition: crypto.cpp:101
void generate_ring_signature(const hash &prefix_hash, const key_image &image, const public_key *const *pubs, std::size_t pubs_count, const secret_key &sec, std::size_t sec_index, signature *sig)
Definition: crypto.h:227
friend class crypto_ops
Definition: crypto.h:94
static void derive_secret_key(const key_derivation &, std::size_t, const secret_key &, secret_key &)
secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key &recovery_key=secret_key(), bool recover=false)
Definition: crypto.h:162
static bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &)
Definition: crypto.cpp:300
POD_CLASS public_key
Definition: crypto.h:58
bool check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const signature &sig)
Definition: crypto.h:214
static bool generate_key_derivation(const public_key &, const secret_key &, key_derivation &)
Definition: crypto.cpp:138
Definition: base.py:1
POD_CLASS signature
Definition: crypto.h:92
POD_CLASS key_image
Definition: crypto.h:86
POD_CLASS secret_key
Definition: crypto.h:62
void operator=(const crypto_ops &)
bool derive_public_key(const key_derivation &derivation, std::size_t output_index, const public_key &base, public_key &derived_key)
Definition: crypto.h:187
bool check_key(const public_key &key)
Definition: crypto.h:168
static bool check_ring_signature(const hash &, const key_image &, const public_key *const *, std::size_t, const signature *)
POD_CLASS hash
Definition: hash.h:46
static void generate_ring_signature(const hash &, const key_image &, const public_key *const *, std::size_t, const secret_key &, std::size_t, signature *)
std::vector< secret_keyV > column_vectors
Definition: crypto.h:81
bool secret_key_to_public_key(const secret_key &sec, public_key &pub)
Definition: crypto.h:174
POD_CLASS public_keyV
Definition: crypto.h:68
static void generate_signature(const hash &, const public_key &, const secret_key &, signature &)
Definition: crypto.cpp:206
#define CRYPTO_MAKE_HASHABLE(type)
Definition: generic-ops.h:48
static void derivation_to_scalar(const key_derivation &derivation, size_t output_index, ec_scalar &res)
Definition: crypto.cpp:153
bool check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig)
Definition: crypto.h:204
int rows
Definition: crypto.h:70