Electroneum
main.cpp
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, 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 #include <cstddef>
33 #include <cstring>
34 #include <fstream>
35 #include <string>
36 #include <vector>
37 
38 #include "warnings.h"
39 #include "misc_log_ex.h"
40 #include "crypto/crypto.h"
41 #include "crypto/hash.h"
42 #include "crypto-tests.h"
43 #include "../io.h"
44 
45 using namespace std;
46 using namespace crypto;
48 
49 bool operator !=(const ec_scalar &a, const ec_scalar &b) {
50  return 0 != memcmp(&a, &b, sizeof(ec_scalar));
51 }
52 
53 bool operator !=(const ec_point &a, const ec_point &b) {
54  return 0 != memcmp(&a, &b, sizeof(ec_point));
55 }
56 
57 bool operator !=(const key_derivation &a, const key_derivation &b) {
58  return 0 != memcmp(&a, &b, sizeof(key_derivation));
59 }
60 
61 DISABLE_GCC_WARNING(maybe-uninitialized)
62 
63 int main(int argc, char *argv[]) {
64  TRY_ENTRY();
65  fstream input;
66  string cmd;
67  size_t test = 0;
68  bool error = false;
69  setup_random();
70  if (argc != 2) {
71  cerr << "invalid arguments" << endl;
72  return 1;
73  }
74  input.open(argv[1], ios_base::in);
75  for (;;) {
76  ++test;
77  input.exceptions(ios_base::badbit);
78  if (!(input >> cmd)) {
79  break;
80  }
81  input.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit);
82  if (cmd == "check_scalar") {
83  ec_scalar scalar;
84  bool expected, actual;
85  get(input, scalar, expected);
86  actual = check_scalar(scalar);
87  if (expected != actual) {
88  goto error;
89  }
90  } else if (cmd == "random_scalar") {
91  ec_scalar expected, actual;
92  get(input, expected);
93  crypto::random_scalar(actual);
94  if (expected != actual) {
95  goto error;
96  }
97  } else if (cmd == "hash_to_scalar") {
98  vector<char> data;
99  ec_scalar expected, actual;
100  get(input, data, expected);
101  crypto::hash_to_scalar(data.data(), data.size(), actual);
102  if (expected != actual) {
103  goto error;
104  }
105  } else if (cmd == "generate_keys") {
106  public_key expected1, actual1;
107  secret_key expected2, actual2;
108  get(input, expected1, expected2);
109  generate_keys(actual1, actual2);
110  if (expected1 != actual1 || expected2 != actual2) {
111  goto error;
112  }
113  } else if (cmd == "check_key") {
114  public_key key;
115  bool expected, actual;
116  get(input, key, expected);
117  actual = check_key(key);
118  if (expected != actual) {
119  goto error;
120  }
121  } else if (cmd == "secret_key_to_public_key") {
122  secret_key sec;
123  bool expected1, actual1;
124  public_key expected2, actual2;
125  get(input, sec, expected1);
126  if (expected1) {
127  get(input, expected2);
128  }
129  actual1 = secret_key_to_public_key(sec, actual2);
130  if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
131  goto error;
132  }
133  } else if (cmd == "generate_key_derivation") {
134  public_key key1;
135  secret_key key2;
136  bool expected1, actual1;
137  key_derivation expected2, actual2;
138  get(input, key1, key2, expected1);
139  if (expected1) {
140  get(input, expected2);
141  }
142  actual1 = generate_key_derivation(key1, key2, actual2);
143  if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
144  goto error;
145  }
146  } else if (cmd == "derive_public_key") {
147  key_derivation derivation;
148  size_t output_index;
149  public_key base;
150  bool expected1, actual1;
151  public_key expected2, actual2;
152  get(input, derivation, output_index, base, expected1);
153  if (expected1) {
154  get(input, expected2);
155  }
156  actual1 = derive_public_key(derivation, output_index, base, actual2);
157  if (expected1 != actual1 || (expected1 && expected2 != actual2)) {
158  goto error;
159  }
160  } else if (cmd == "derive_secret_key") {
161  key_derivation derivation;
162  size_t output_index;
163  secret_key base;
164  secret_key expected, actual;
165  get(input, derivation, output_index, base, expected);
166  derive_secret_key(derivation, output_index, base, actual);
167  if (expected != actual) {
168  goto error;
169  }
170  } else if (cmd == "generate_signature") {
171  chash prefix_hash;
172  public_key pub;
173  secret_key sec;
174  signature expected, actual;
175  get(input, prefix_hash, pub, sec, expected);
176  generate_signature(prefix_hash, pub, sec, actual);
177  if (expected != actual) {
178  goto error;
179  }
180  } else if (cmd == "check_signature") {
181  chash prefix_hash;
182  public_key pub;
183  signature sig;
184  bool expected, actual;
185  get(input, prefix_hash, pub, sig, expected);
186  actual = check_signature(prefix_hash, pub, sig);
187  if (expected != actual) {
188  goto error;
189  }
190  } else if (cmd == "hash_to_point") {
191  chash h;
192  ec_point expected, actual;
193  get(input, h, expected);
194  hash_to_point(h, actual);
195  if (expected != actual) {
196  goto error;
197  }
198  } else if (cmd == "hash_to_ec") {
199  public_key key;
200  ec_point expected, actual;
201  get(input, key, expected);
202  hash_to_ec(key, actual);
203  if (expected != actual) {
204  goto error;
205  }
206  } else if (cmd == "generate_key_image") {
207  public_key pub;
208  secret_key sec;
209  key_image expected, actual;
210  get(input, pub, sec, expected);
211  generate_key_image(pub, sec, actual);
212  if (expected != actual) {
213  goto error;
214  }
215  } else if (cmd == "generate_ring_signature") {
216  chash prefix_hash;
217  key_image image;
218  vector<public_key> vpubs;
219  vector<const public_key *> pubs;
220  size_t pubs_count;
221  secret_key sec;
222  size_t sec_index;
223  vector<signature> expected, actual;
224  size_t i;
225  get(input, prefix_hash, image, pubs_count);
226  vpubs.resize(pubs_count);
227  pubs.resize(pubs_count);
228  for (i = 0; i < pubs_count; i++) {
229  get(input, vpubs[i]);
230  pubs[i] = &vpubs[i];
231  }
232  get(input, sec, sec_index);
233  expected.resize(pubs_count);
234  getvar(input, pubs_count * sizeof(signature), expected.data());
235  actual.resize(pubs_count);
236  generate_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sec, sec_index, actual.data());
237  if (expected != actual) {
238  goto error;
239  }
240  } else if (cmd == "check_ring_signature") {
241  chash prefix_hash;
242  key_image image;
243  vector<public_key> vpubs;
244  vector<const public_key *> pubs;
245  size_t pubs_count;
246  vector<signature> sigs;
247  bool expected, actual;
248  size_t i;
249  get(input, prefix_hash, image, pubs_count);
250  vpubs.resize(pubs_count);
251  pubs.resize(pubs_count);
252  for (i = 0; i < pubs_count; i++) {
253  get(input, vpubs[i]);
254  pubs[i] = &vpubs[i];
255  }
256  sigs.resize(pubs_count);
257  getvar(input, pubs_count * sizeof(signature), sigs.data());
258  get(input, expected);
259  actual = check_ring_signature(prefix_hash, image, pubs.data(), pubs_count, sigs.data());
260  if (expected != actual) {
261  goto error;
262  }
263  } else {
264  throw ios_base::failure("Unknown function: " + cmd);
265  }
266  continue;
267 error:
268  cerr << "Wrong result on test " << test << endl;
269  error = true;
270  }
271  return error ? 1 : 0;
272  CATCH_ENTRY_L0("main", 1);
273 }
POD_CLASS ec_point
Definition: crypto.h:70
#define DISABLE_GCC_WARNING
Definition: warnings.h:24
void derive_secret_key(const key_derivation &derivation, std::size_t output_index, const secret_key &base, secret_key &derived_key)
Definition: crypto.h:282
int main(int argc, char *argv[])
Definition: main.cpp:63
POD_CLASS key_derivation
Definition: crypto.h:98
void hash_to_ec(const crypto::public_key &key, crypto::ec_point &res)
Definition: crypto.cpp:48
crypto::hash chash
Definition: main.cpp:47
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:333
void generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig)
Definition: crypto.h:292
const char * key
Definition: hmac_keccak.cpp:39
crypto namespace.
Definition: crypto.cpp:58
STL namespace.
bool generate_key_derivation(const public_key &key1, const secret_key &key2, key_derivation &derivation)
Definition: crypto.h:272
void hash_to_point(const crypto::hash &h, crypto::ec_point &res)
Definition: crypto.cpp:496
void generate_key_image(const public_key &pub, const secret_key &sec, key_image &image)
Definition: crypto.h:324
void setup_random(void)
void getvar(std::istream &input, std::size_t length, void *res)
Definition: io.h:80
#define TRY_ENTRY()
Definition: misc_log_ex.h:151
POD_CLASS ec_scalar
Definition: crypto.h:74
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:327
bool check_scalar(const crypto::ec_scalar &scalar)
Definition: crypto.cpp:36
secret_key generate_keys(public_key &pub, secret_key &sec, const secret_key &recovery_key=secret_key(), bool recover=false)
Definition: crypto.h:250
bool operator!=(const ec_scalar &a, const ec_scalar &b)
Definition: main.cpp:49
POD_CLASS public_key
Definition: crypto.h:76
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
POD_CLASS signature
Definition: crypto.h:108
POD_CLASS key_image
Definition: crypto.h:102
bool derive_public_key(const key_derivation &derivation, std::size_t output_index, const public_key &base, public_key &derived_key)
Definition: crypto.h:275
bool check_key(const public_key &key)
Definition: crypto.h:256
void hash_to_scalar(const void *data, size_t length, ec_scalar &res)
Definition: crypto.cpp:126
POD_CLASS hash
Definition: hash.h:50
bool secret_key_to_public_key(const secret_key &sec, public_key &pub)
Definition: crypto.h:262
#define CATCH_ENTRY_L0(lacation, return_val)
Definition: misc_log_ex.h:165
error
Tracks LMDB error codes.
Definition: error.h:44
bool check_signature(const hash &prefix_hash, const public_key &pub, const signature &sig)
Definition: crypto.h:295
void random_scalar(ec_scalar &res)
Definition: crypto.cpp:122