Electroneum
main.cpp File Reference
#include <cstddef>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include "warnings.h"
#include "misc_log_ex.h"
#include "crypto/crypto.h"
#include "crypto/hash.h"
#include "crypto-tests.h"
#include "../io.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Typedefs

typedef crypto::hash chash
 

Functions

bool operator!= (const ec_scalar &a, const ec_scalar &b)
 
bool operator!= (const ec_point &a, const ec_point &b)
 
bool operator!= (const key_derivation &a, const key_derivation &b)
 
int main (int argc, char *argv[])
 

Typedef Documentation

◆ chash

Definition at line 47 of file main.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 63 of file main.cpp.

63  {
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
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
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
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
POD_CLASS public_key
Definition: crypto.h:76
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
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

◆ operator!=() [1/3]

bool operator!= ( const ec_scalar &  a,
const ec_scalar &  b 
)

Definition at line 49 of file main.cpp.

49  {
50  return 0 != memcmp(&a, &b, sizeof(ec_scalar));
51 }
POD_CLASS ec_scalar
Definition: crypto.h:74
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124

◆ operator!=() [2/3]

bool operator!= ( const ec_point &  a,
const ec_point &  b 
)

Definition at line 53 of file main.cpp.

53  {
54  return 0 != memcmp(&a, &b, sizeof(ec_point));
55 }
POD_CLASS ec_point
Definition: crypto.h:70
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124

◆ operator!=() [3/3]

bool operator!= ( const key_derivation &  a,
const key_derivation &  b 
)

Definition at line 57 of file main.cpp.

57  {
58  return 0 != memcmp(&a, &b, sizeof(key_derivation));
59 }
POD_CLASS key_derivation
Definition: crypto.h:98
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124