Bitcoin ABC 0.32.5
P2P Digital Currency
asmap.cpp
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <util/asmap.h>
6
7#include <clientversion.h>
8#include <crypto/common.h>
9#include <logging.h>
10#include <streams.h>
11#include <util/fs.h>
12
13#include <bit>
14#include <cassert>
15#include <map>
16#include <vector>
17
18namespace {
19
20constexpr uint32_t INVALID = 0xFFFFFFFF;
21
22uint32_t DecodeBits(std::vector<bool>::const_iterator &bitpos,
23 const std::vector<bool>::const_iterator &endpos,
24 uint8_t minval, const std::vector<uint8_t> &bit_sizes) {
25 uint32_t val = minval;
26 bool bit;
27 for (std::vector<uint8_t>::const_iterator bit_sizes_it = bit_sizes.begin();
28 bit_sizes_it != bit_sizes.end(); ++bit_sizes_it) {
29 if (bit_sizes_it + 1 != bit_sizes.end()) {
30 if (bitpos == endpos) {
31 break;
32 }
33 bit = *bitpos;
34 bitpos++;
35 } else {
36 bit = 0;
37 }
38 if (bit) {
39 val += (1 << *bit_sizes_it);
40 } else {
41 for (int b = 0; b < *bit_sizes_it; b++) {
42 if (bitpos == endpos) {
43 // Reached EOF in mantissa
44 return INVALID;
45 }
46 bit = *bitpos;
47 bitpos++;
48 val += bit << (*bit_sizes_it - 1 - b);
49 }
50 return val;
51 }
52 }
53 // Reached EOF in exponent
54 return INVALID;
55}
56
57enum class Instruction : uint32_t {
58 RETURN = 0,
59 JUMP = 1,
60 MATCH = 2,
61 DEFAULT = 3,
62};
63
64const std::vector<uint8_t> TYPE_BIT_SIZES{0, 0, 1};
65Instruction DecodeType(std::vector<bool>::const_iterator &bitpos,
66 const std::vector<bool>::const_iterator &endpos) {
67 return Instruction(DecodeBits(bitpos, endpos, 0, TYPE_BIT_SIZES));
68}
69
70const std::vector<uint8_t> ASN_BIT_SIZES{15, 16, 17, 18, 19,
71 20, 21, 22, 23, 24};
72uint32_t DecodeASN(std::vector<bool>::const_iterator &bitpos,
73 const std::vector<bool>::const_iterator &endpos) {
74 return DecodeBits(bitpos, endpos, 1, ASN_BIT_SIZES);
75}
76
77const std::vector<uint8_t> MATCH_BIT_SIZES{1, 2, 3, 4, 5, 6, 7, 8};
78uint32_t DecodeMatch(std::vector<bool>::const_iterator &bitpos,
79 const std::vector<bool>::const_iterator &endpos) {
80 return DecodeBits(bitpos, endpos, 2, MATCH_BIT_SIZES);
81}
82
83const std::vector<uint8_t> JUMP_BIT_SIZES{5, 6, 7, 8, 9, 10, 11, 12, 13,
84 14, 15, 16, 17, 18, 19, 20, 21, 22,
85 23, 24, 25, 26, 27, 28, 29, 30};
86uint32_t DecodeJump(std::vector<bool>::const_iterator &bitpos,
87 const std::vector<bool>::const_iterator &endpos) {
88 return DecodeBits(bitpos, endpos, 17, JUMP_BIT_SIZES);
89}
90
91} // namespace
92
93uint32_t Interpret(const std::vector<bool> &asmap,
94 const std::vector<bool> &ip) {
95 std::vector<bool>::const_iterator pos = asmap.begin();
96 const std::vector<bool>::const_iterator endpos = asmap.end();
97 uint8_t bits = ip.size();
98 uint32_t default_asn = 0;
99 uint32_t jump, match, matchlen;
100 Instruction opcode;
101 while (pos != endpos) {
102 opcode = DecodeType(pos, endpos);
103 if (opcode == Instruction::RETURN) {
104 default_asn = DecodeASN(pos, endpos);
105 if (default_asn == INVALID) {
106 // ASN straddles EOF
107 break;
108 }
109 return default_asn;
110 } else if (opcode == Instruction::JUMP) {
111 jump = DecodeJump(pos, endpos);
112 if (jump == INVALID) {
113 // Jump offset straddles EOF
114 break;
115 }
116 if (bits == 0) {
117 // No input bits left
118 break;
119 }
120 if (pos + jump < pos) {
121 // overflow
122 break;
123 }
124 if (pos + jump >= endpos) {
125 // Jumping past EOF
126 break;
127 }
128 if (ip[ip.size() - bits]) {
129 pos += jump;
130 }
131 bits--;
132 } else if (opcode == Instruction::MATCH) {
133 match = DecodeMatch(pos, endpos);
134 if (match == INVALID) {
135 // Match bits straddle EOF
136 break;
137 }
138 matchlen = std::bit_width(match) - 1;
139 if (bits < matchlen) {
140 // Not enough input bits
141 break;
142 }
143 for (uint32_t bit = 0; bit < matchlen; bit++) {
144 if ((ip[ip.size() - bits]) !=
145 ((match >> (matchlen - 1 - bit)) & 1)) {
146 return default_asn;
147 }
148 bits--;
149 }
150 } else if (opcode == Instruction::DEFAULT) {
151 default_asn = DecodeASN(pos, endpos);
152 if (default_asn == INVALID) {
153 // ASN straddles EOF
154 break;
155 }
156 } else {
157 // Instruction straddles EOF
158 break;
159 }
160 }
161
162 // Reached EOF without RETURN, or aborted (see any of the breaks above) -
163 // should have been caught by SanityCheckASMap below
164 assert(false);
165
166 // 0 is not a valid ASN
167 return 0;
168}
169
170bool SanityCheckASMap(const std::vector<bool> &asmap, int bits) {
171 const std::vector<bool>::const_iterator begin = asmap.begin(),
172 endpos = asmap.end();
173 std::vector<bool>::const_iterator pos = begin;
174 // All future positions we may jump to (bit offset in asmap -> bits to
175 // consume left)
176 std::vector<std::pair<uint32_t, int>> jumps;
177 jumps.reserve(bits);
178 Instruction prevopcode = Instruction::JUMP;
179 bool had_incomplete_match = false;
180 while (pos != endpos) {
181 uint32_t offset = pos - begin;
182 if (!jumps.empty() && offset >= jumps.back().first) {
183 // There was a jump into the middle of the previous instruction
184 return false;
185 }
186 Instruction opcode = DecodeType(pos, endpos);
187 if (opcode == Instruction::RETURN) {
188 if (prevopcode == Instruction::DEFAULT) {
189 // There should not be any RETURN immediately after a DEFAULT
190 // (could be combined into just RETURN)
191 return false;
192 }
193 uint32_t asn = DecodeASN(pos, endpos);
194 if (asn == INVALID) {
195 // ASN straddles EOF
196 return false;
197 }
198 if (jumps.empty()) {
199 // Nothing to execute anymore
200 if (endpos - pos > 7) {
201 // Excessive padding
202 return false;
203 }
204 while (pos != endpos) {
205 if (*pos) {
206 // Nonzero padding bit
207 return false;
208 }
209 ++pos;
210 }
211 // Sanely reached EOF
212 return true;
213 } else {
214 // Continue by pretending we jumped to the next instruction
215 offset = pos - begin;
216 if (offset != jumps.back().first) {
217 // Unreachable code
218 return false;
219 }
220 // Restore the number of bits we would have had left after this
221 // jump
222 bits = jumps.back().second;
223 jumps.pop_back();
224 prevopcode = Instruction::JUMP;
225 }
226 } else if (opcode == Instruction::JUMP) {
227 uint32_t jump = DecodeJump(pos, endpos);
228 if (jump == INVALID) {
229 // Jump offset straddles EOF
230 return false;
231 }
232 if (pos + jump < pos) {
233 // overflow
234 return false;
235 }
236 if (pos + jump > endpos) {
237 // Jump out of range
238 return false;
239 }
240 if (bits == 0) {
241 // Consuming bits past the end of the input
242 return false;
243 }
244 --bits;
245 uint32_t jump_offset = pos - begin + jump;
246 if (!jumps.empty() && jump_offset >= jumps.back().first) {
247 // Intersecting jumps
248 return false;
249 }
250 jumps.emplace_back(jump_offset, bits);
251 prevopcode = Instruction::JUMP;
252 } else if (opcode == Instruction::MATCH) {
253 uint32_t match = DecodeMatch(pos, endpos);
254 if (match == INVALID) {
255 // Match bits straddle EOF
256 return false;
257 }
258 int matchlen = std::bit_width(match) - 1;
259 if (prevopcode != Instruction::MATCH) {
260 had_incomplete_match = false;
261 }
262 if (matchlen < 8 && had_incomplete_match) {
263 // Within a sequence of matches only at most one should be
264 // incomplete
265 return false;
266 }
267 had_incomplete_match = (matchlen < 8);
268 if (bits < matchlen) {
269 // Consuming bits past the end of the input
270 return false;
271 }
272 bits -= matchlen;
273 prevopcode = Instruction::MATCH;
274 } else if (opcode == Instruction::DEFAULT) {
275 if (prevopcode == Instruction::DEFAULT) {
276 // There should not be two successive DEFAULTs (they could be
277 // combined into one)
278 return false;
279 }
280 uint32_t asn = DecodeASN(pos, endpos);
281 if (asn == INVALID) {
282 // ASN straddles EOF
283 return false;
284 }
285 prevopcode = Instruction::DEFAULT;
286 } else {
287 // Instruction straddles EOF
288 return false;
289 }
290 }
291 // Reached EOF without RETURN instruction
292 return false;
293}
294
295std::vector<bool> DecodeAsmap(fs::path path) {
296 std::vector<bool> bits;
297 FILE *filestr = fsbridge::fopen(path, "rb");
298 AutoFile file{filestr};
299 if (file.IsNull()) {
300 LogPrintf("Failed to open asmap file from disk\n");
301 return bits;
302 }
303 fseek(filestr, 0, SEEK_END);
304 int length = ftell(filestr);
305 LogPrintf("Opened asmap file %s (%d bytes) from disk\n",
306 fs::quoted(fs::PathToString(path)), length);
307 fseek(filestr, 0, SEEK_SET);
308 uint8_t cur_byte;
309 for (int i = 0; i < length; ++i) {
310 file >> cur_byte;
311 for (int bit = 0; bit < 8; ++bit) {
312 bits.push_back((cur_byte >> bit) & 1);
313 }
314 }
315 if (!SanityCheckASMap(bits, 128)) {
316 LogPrintf("Sanity check of asmap file %s failed\n",
318 return {};
319 }
320 return bits;
321}
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:93
std::vector< bool > DecodeAsmap(fs::path path)
Read asmap from provided binary file.
Definition: asmap.cpp:295
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:170
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:526
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
#define LogPrintf(...)
Definition: logging.h:424
static auto quoted(const std::string &s)
Definition: fs.h:112
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:147
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
assert(!tx.IsCoinBase())