add randomx registers

This commit is contained in:
moneroexamples 2019-06-04 18:54:08 +08:00
parent d32cc0df05
commit dcc4641157
2 changed files with 102 additions and 35 deletions

View file

@ -235,6 +235,70 @@ using namespace std;
using epee::string_tools::pod_to_hex;
using epee::string_tools::hex_to_pod;
template< typename T >
std::string as_hex(T i)
{
std::stringstream ss;
ss << "0x" << setfill ('0') << setw(sizeof(T)*2)
<< hex << i;
return ss.str();
}
struct randomx_status
{
randomx::Program prog;
randomx::RegisterFile reg_file;
randomx::AssemblyGeneratorX86
get_asm()
{
randomx::AssemblyGeneratorX86 asmX86;
asmX86.generateProgram(prog);
return asmX86;
}
mstch::map
get_mstch()
{
auto asmx86 = get_asm();
stringstream ss1, ss2;
ss1 << prog;
asmx86.printCode(ss2);
mstch::map rx_map {
{"rx_code" , ss1.str()},
{"rx_code_asm", ss2.str()}
};
for (size_t i = 0; i < randomx::RegistersCount; ++i)
{
rx_map["r"+std::to_string(i)] = as_hex(reg_file.r[i]);
}
for (size_t i = 0; i < randomx::RegistersCount/2; ++i)
{
rx_map["f"+std::to_string(i)] = rx_float_as_str(reg_file.f[i]);
rx_map["e"+std::to_string(i)] = rx_float_as_str(reg_file.e[i]);
rx_map["a"+std::to_string(i)] = rx_float_as_str(reg_file.a[i]);
}
return rx_map;
}
string
rx_float_as_str(randomx::fpu_reg_t fpu)
{
uint64_t* lo = reinterpret_cast<uint64_t*>(&fpu.lo);
uint64_t* hi = reinterpret_cast<uint64_t*>(&fpu.hi);
return "{" + as_hex(*lo) + ", " + as_hex(*hi)+ "}";
}
};
/**
* @brief The tx_details struct
*
@ -1397,12 +1461,9 @@ show_randomx(uint64_t _blk_height)
for (auto& rxc: rx_code)
{
rx_code_str.push_back(
mstch::map {
{"rx_code_idx", code_idx++},
{"rx_code", std::move(rxc.first)},
{"rx_code_asm", std::move(rxc.second)}
});
mstch::map rx_map = rxc.get_mstch();
rx_map["rx_code_idx"] = code_idx++;
rx_code_str.push_back(rx_map);
}
mstch::map context {
@ -7129,14 +7190,14 @@ get_tx(string const& tx_hash_str,
return true;
}
vector<std::pair<string, string>>
vector<randomx_status>
get_randomx_code(uint64_t blk_height,
block const& blk,
crypto::hash const& blk_hash)
{
static std::mutex mtx;
vector<std::pair<string, string>> rx_code;
vector<randomx_status> rx_code;
blobdata bd = get_block_hashing_blob(blk);
@ -7167,47 +7228,33 @@ get_randomx_code(uint64_t blk_height,
// randomx::Program* prg
// = reinterpret_cast<randomx::Program*>(
// reinterpret_cast<char*>(rx_vm) + 64);
//
randomx::Program prg;
randomx::RegisterFile reg;
for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain)
{
rx_vm->run(&tempHash);
blake2b(tempHash, sizeof(tempHash),
rx_vm->getRegisterFile(),
sizeof(randomx::RegisterFile), nullptr, 0);
prg = rx_vm->getProgram();
reg = *(rx_vm->getRegisterFile());
rx_code.push_back({});
stringstream ss, ss2;
ss << prg;
randomx::AssemblyGeneratorX86 asmX86;
asmX86.generateProgram(prg);
asmX86.printCode(ss2);
rx_code.emplace_back(ss.str(), ss2.str());
rx_code.back().prog = rx_vm->getProgram();
rx_code.back().reg_file = *(rx_vm->getRegisterFile());
}
rx_vm->run(&tempHash);
prg = rx_vm->getProgram();
rx_code.push_back({});
stringstream ss, ss2;
ss << prg;
rx_code.back().prog = rx_vm->getProgram();
rx_code.back().reg_file = *(rx_vm->getRegisterFile());
randomx::AssemblyGeneratorX86 asmX86;
asmX86.generateProgram(prg);
asmX86.printCode(ss2);
rx_code.emplace_back(ss.str(), ss2.str());
// crypto::hash res2;
// rx_vm->getFinalResult(res2.data, RANDOMX_HASH_SIZE);
// crypto::hash res2;
// rx_vm->getFinalResult(res2.data, RANDOMX_HASH_SIZE);
// cout << "pow2: " << pod_to_hex(res2) << endl;

View file

@ -3,13 +3,33 @@
<H4>Block hash (height): {{blk_hash}} ({{blk_height}})</H4>
<h4>
Eight RandomX and correspoding assembly x86 programs used
to calcualte PoW hash of the block.<br/>The RandomX programs
are executed on RandomX virtual machine.
to calcualte the PoW hash of the block.<br/>
The RandomX programs are executed on RandomX virtual machine. <br/>
Initial values of the integer and floating point registers in the machine are also provided.
</h4>
{{#rx_codes}}
<h3> Program #{{rx_code_idx}}</h3>
<table class="center" style="width: 50%; margin-top:10px">
<table class="center" style="width: 60%; margin-top:10px">
<tr>
<td colspan="2">
Integer registers R:<br/><br/>
r0: {{r0}}, r1: {{r1}}, r2: {{r2}}, r3: {{r3}}<br/>
r4: {{r4}}, r5: {{r5}}, r6: {{r6}}, r7: {{r7}}<br/><br/>
Floating point registers F {lo, hi}:<br/><br/>
f0: {{f0}}, f1: {{f1}}<br/>
f2: {{f2}}, f3: {{f3}}<br/><br/>
Floating point registers E {lo, hi}:<br/><br/>
e0: {{e0}}, e1: {{e1}}<br/>
e2: {{e2}}, e3: {{e3}}<br/><br/>
Floating point registers A {lo, hi}:<br/><br/>
a0: {{a0}}, a1: {{a1}}<br/>
a2: {{a2}}, a3: {{a3}}<br/><br/>
<br/>
</td>
</tr>
<tr>
<td style="text-align: left;">RandomX code</td>
<td style="text-align: left;">ASM x86</td>