Electroneum
Loading...
Searching...
No Matches
main.cpp File Reference
#include <cstddef>
#include <fstream>
#include <iomanip>
#include <ios>
#include <string>
#include <cfenv>
#include "misc_log_ex.h"
#include "warnings.h"
#include "crypto/hash.h"
#include "crypto/variant2_int_sqrt.h"
#include "../io.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  V4_Data
struct  hash_func

Functions

POP_WARNINGS typedef void hash_f (const void *, size_t, char *)
int test_variant2_int_sqrt ()
int test_variant2_int_sqrt_ref ()
int main (int argc, char *argv[])

Variables

struct hash_func hashes []

Function Documentation

◆ hash_f()

POP_WARNINGS typedef void hash_f ( const void * ,
size_t ,
char *  )
Here is the caller graph for this function:

◆ main()

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

Definition at line 92 of file main.cpp.

92 {
93 TRY_ENTRY();
94
95 hash_f *f;
96 hash_func *hf;
97 fstream input;
98 vector<char> data;
99 chash expected, actual;
100 size_t test = 0;
101 bool error = false;
102 if (argc != 3) {
103 if ((argc == 2) && (strcmp(argv[1], "variant2_int_sqrt") == 0)) {
104 if (test_variant2_int_sqrt_ref() != 0) {
105 return 1;
106 }
107 const int round_modes[3] = { FE_DOWNWARD, FE_TONEAREST, FE_UPWARD };
108 for (int i = 0; i < 3; ++i) {
109 std::fesetround(round_modes[i]);
110 const int result = test_variant2_int_sqrt();
111 if (result != 0) {
112 cerr << "FPU round mode was set to ";
113 switch (round_modes[i]) {
114 case FE_DOWNWARD:
115 cerr << "FE_DOWNWARD";
116 break;
117 case FE_TONEAREST:
118 cerr << "FE_TONEAREST";
119 break;
120 case FE_UPWARD:
121 cerr << "FE_UPWARD";
122 break;
123 default:
124 cerr << "unknown";
125 break;
126 }
127 cerr << endl;
128 return result;
129 }
130 }
131 return 0;
132 }
133 cerr << "Wrong number of arguments" << endl;
134 return 1;
135 }
136 for (hf = hashes;; hf++) {
137 if (hf >= &hashes[sizeof(hashes) / sizeof(hash_func)]) {
138 cerr << "Unknown function" << endl;
139 return 1;
140 }
141 if (argv[1] == hf->name) {
142 f = &hf->f;
143 break;
144 }
145 }
146 input.open(argv[2], ios_base::in);
147 for (;;) {
148 ++test;
149 input.exceptions(ios_base::badbit);
150 get(input, expected);
151 if (input.rdstate() & ios_base::eofbit) {
152 break;
153 }
154 input.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit);
155 input.clear(input.rdstate());
156 get(input, data);
157 if (f == cn_slow_hash_4) {
158 V4_Data d;
159 d.data = data.data();
160 d.length = data.size();
161 get(input, d.height);
162 f(&d, 0, (char *) &actual);
163 } else {
164 f(data.data(), data.size(), (char *) &actual);
165 }
166 if (expected != actual) {
167 size_t i;
168 cerr << "Hash mismatch on test " << test << endl << "Input: ";
169 if (data.size() == 0) {
170 cerr << "empty";
171 } else {
172 for (i = 0; i < data.size(); i++) {
173 cerr << setbase(16) << setw(2) << setfill('0') << int(static_cast<unsigned char>(data[i]));
174 }
175 }
176 cerr << endl << "Expected hash: ";
177 for (i = 0; i < 32; i++) {
178 cerr << setbase(16) << setw(2) << setfill('0') << int(reinterpret_cast<unsigned char *>(&expected)[i]);
179 }
180 cerr << endl << "Actual hash: ";
181 for (i = 0; i < 32; i++) {
182 cerr << setbase(16) << setw(2) << setfill('0') << int(reinterpret_cast<unsigned char *>(&actual)[i]);
183 }
184 cerr << endl;
185 error = true;
186 }
187 }
188 return error ? 1 : 0;
189 CATCH_ENTRY_L0("main", 1);
190}
void get(std::istream &input, bool &res)
Definition io.h:62
#define CATCH_ENTRY_L0(lacation, return_val)
#define TRY_ENTRY()
error
Tracks LMDB error codes.
Definition error.h:45
uint64_t height
Definition main.cpp:52
size_t length
Definition main.cpp:51
const void * data
Definition main.cpp:50
hash_f & f
Definition main.cpp:83
const string name
Definition main.cpp:82
crypto::hash chash
Definition main.cpp:47
struct hash_func hashes[]
int test_variant2_int_sqrt()
Definition main.cpp:264
int test_variant2_int_sqrt_ref()
Definition main.cpp:319
POP_WARNINGS typedef void hash_f(const void *, size_t, char *)
Here is the call graph for this function:

◆ test_variant2_int_sqrt()

int test_variant2_int_sqrt ( )

Definition at line 264 of file main.cpp.

265{
266 if (!test_variant2_int_sqrt(0, 0)) {
267 return 1;
268 }
269 if (!test_variant2_int_sqrt(1ULL << 63, 1930543745UL)) {
270 return 1;
271 }
272 if (!test_variant2_int_sqrt(uint64_t(-1), 3558067407UL)) {
273 return 1;
274 }
275
276 for (uint64_t i = 1; i <= 3558067407UL; ++i) {
277 // "i" is integer part of "sqrt(2^64 + n) * 2 - 2^33"
278 // n = (i/2 + 2^32)^2 - 2^64
279
280 const uint64_t i0 = i >> 1;
281 uint64_t n1;
282 if ((i & 1) == 0) {
283 // n = (i/2 + 2^32)^2 - 2^64
284 // n = i^2/4 + 2*2^32*i/2 + 2^64 - 2^64
285 // n = i^2/4 + 2^32*i
286 // i is even, so i^2 is divisible by 4:
287 // n = (i^2 >> 2) + (i << 32)
288
289 // int_sqrt_v2(i^2/4 + 2^32*i - 1) must be equal to i - 1
290 // int_sqrt_v2(i^2/4 + 2^32*i) must be equal to i
291 n1 = i0 * i0 + (i << 32) - 1;
292 }
293 else {
294 // n = (i/2 + 2^32)^2 - 2^64
295 // n = i^2/4 + 2*2^32*i/2 + 2^64 - 2^64
296 // n = i^2/4 + 2^32*i
297 // i is odd, so i = i0*2+1 (i0 = i >> 1)
298 // n = (i0*2+1)^2/4 + 2^32*i
299 // n = (i0^2*4+i0*4+1)/4 + 2^32*i
300 // n = i0^2+i0+1/4 + 2^32*i
301 // i0^2+i0 + 2^32*i < n < i0^2+i0+1 + 2^32*i
302
303 // int_sqrt_v2(i0^2+i0 + 2^32*i) must be equal to i - 1
304 // int_sqrt_v2(i0^2+i0+1 + 2^32*i) must be equal to i
305 n1 = i0 * i0 + i0 + (i << 32);
306 }
307
308 if (!test_variant2_int_sqrt(n1, i - 1)) {
309 return 1;
310 }
311 if (!test_variant2_int_sqrt(n1 + 1, i)) {
312 return 1;
313 }
314 }
315
316 return 0;
317}
unsigned __int64 uint64_t
Definition hash.h:137
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_variant2_int_sqrt_ref()

int test_variant2_int_sqrt_ref ( )

Definition at line 319 of file main.cpp.

320{
321 if (!test_variant2_int_sqrt_ref(0, 0)) {
322 return 1;
323 }
324 if (!test_variant2_int_sqrt_ref(1ULL << 63, 1930543745UL)) {
325 return 1;
326 }
327 if (!test_variant2_int_sqrt_ref(uint64_t(-1), 3558067407UL)) {
328 return 1;
329 }
330
331 // Reference version is slow, so we test only every 83th edge case
332 // "i += 83" because 1 + 83 * 42868282 = 3558067407
333 for (uint64_t i = 1; i <= 3558067407UL; i += 83) {
334 const uint64_t i0 = i >> 1;
335 uint64_t n1;
336 if ((i & 1) == 0) {
337 n1 = i0 * i0 + (i << 32) - 1;
338 }
339 else {
340 n1 = i0 * i0 + i0 + (i << 32);
341 }
342
343 if (!test_variant2_int_sqrt_ref(n1, i - 1)) {
344 return 1;
345 }
346 if (!test_variant2_int_sqrt_ref(n1 + 1, i)) {
347 return 1;
348 }
349 }
350
351 return 0;
352}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ hashes

struct hash_func hashes[]
Initial value:
= {{"fast", cn_fast_hash}, {"slow", cn_slow_hash_0}, {"tree", hash_tree},
{"extra-blake", hash_extra_blake}, {"extra-groestl", hash_extra_groestl},
{"extra-jh", hash_extra_jh}, {"extra-skein", hash_extra_skein},
{"slow-1", cn_slow_hash_1}, {"slow-2", cn_slow_hash_2}, {"slow-4", cn_slow_hash_4}}
void cn_fast_hash(const void *data, size_t length, char *hash)
void hash_extra_blake(const void *data, size_t length, char *hash)
void hash_extra_groestl(const void *data, size_t length, char *hash)
void hash_extra_skein(const void *data, size_t length, char *hash)
void hash_extra_jh(const void *data, size_t length, char *hash)