-
Notifications
You must be signed in to change notification settings - Fork 63
/
util.h
149 lines (123 loc) · 3.66 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef UTIL_H
#define UTIL_H
#include "vdf_new.h"
// compiler-specific byte swap macros
#if defined(_MSC_VER)
#include <cstdlib>
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/byteswap-uint64-byteswap-ulong-byteswap-ushort?view=msvc-160
inline uint16_t bswap_16(uint16_t x) { return _byteswap_ushort(x); }
inline uint32_t bswap_32(uint32_t x) { return _byteswap_ulong(x); }
inline uint64_t bswap_64(uint64_t x) { return _byteswap_uint64(x); }
#elif defined(__clang__) || defined(__GNUC__)
inline uint16_t bswap_16(uint16_t x) { return __builtin_bswap16(x); }
inline uint32_t bswap_32(uint32_t x) { return __builtin_bswap32(x); }
inline uint64_t bswap_64(uint64_t x) { return __builtin_bswap64(x); }
#else
#error "unknown compiler, don't know how to swap bytes"
#endif
struct Segment {
uint64_t start;
uint64_t length;
form x;
form y;
form proof;
bool is_empty;
Segment() {
is_empty = true;
}
Segment(uint64_t start, uint64_t length, form& x, form& y) {
this->start = start;
this->length = length;
this->x = x;
this->y = y;
is_empty = false;
}
bool IsWorseThan(Segment& other) {
if (is_empty) {
if (!other.is_empty)
return true;
return false;
}
if (length > other.length)
return true;
if (length < other.length)
return false;
return start > other.start;
}
int GetSegmentBucket() {
uint64_t c_length = length;
length >>= 16;
int index = 0;
while (length > 1) {
index++;
if (length == 2 || length == 3) {
std::cout << "Warning: Invalid segment length.\n";
}
length >>= 2;
}
length = c_length;
return index;
}
};
void Int64ToBytes(uint8_t *result, uint64_t input)
{
uint64_t r = bswap_64(input);
memcpy(result, &r, sizeof(r));
}
void Int32ToBytes(uint8_t *result, uint32_t input)
{
uint32_t r = bswap_32(input);
memcpy(result, &r, sizeof(r));
}
uint64_t BytesToInt64(const uint8_t *bytes)
{
uint64_t i;
memcpy(&i, bytes, sizeof(i));
return bswap_64(i);
}
template<typename T>
void VectorAppend(std::vector<T> &dst, const std::vector<T> &src)
{
dst.insert(dst.end(), src.begin(), src.end());
}
template<typename T>
void VectorAppendArray(std::vector<T> &dst, const T *src, size_t len)
{
dst.insert(dst.end(), src, src + len);
}
std::string BytesToStr(const std::vector<unsigned char> &in)
{
std::vector<unsigned char>::const_iterator from = in.cbegin();
std::vector<unsigned char>::const_iterator to = in.cend();
std::ostringstream oss;
for (; from != to; ++from)
oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(*from);
return oss.str();
}
void ApproximateParameters(uint64_t T, uint32_t& L, uint32_t& K) {
double log_memory = 23.25349666;
double log_T = log2(T);
L = 1;
if (log_T - log_memory > 0.000001) {
L = ceil(pow(2, log_memory - 20));
}
double intermediate = T * (double)0.6931471 / (2.0 * L);
K = std::max(std::round(log(intermediate) - log(log(intermediate)) + 0.25), 1.0);
}
struct Proof {
Proof() {
}
Proof(std::vector<unsigned char> y, std::vector<unsigned char> proof) {
this->y = y;
this->proof = proof;
}
string hex() {
std::vector<unsigned char> bytes(y);
bytes.insert(bytes.end(), proof.begin(), proof.end());
return BytesToStr(bytes);
}
std::vector<unsigned char> y;
std::vector<unsigned char> proof;
uint8_t witness_type;
};
#endif // UTIL_H