当前位置:网站首页>Merkle tree existential function modified for the first time
Merkle tree existential function modified for the first time
2022-07-29 06:36:00 【Dtsuv】
Merkle Tree The existential function is modified for the first time
Blockchain learning notes ( Two )
Determine whether a certain data is on the tree
How to verify that a certain data is on the tree , You can build a tree by , Sort the hash values before building .
If some data is in the leaves , Then connect the hash value of the corresponding sibling node with its own hash value , Then get the new hash value through the hash algorithm , And so on , Finally, we get a hash value without siblings , Compare it with the root , If the same , Then it proves that the data exists .
If some data is not in the leaves , By comparing the size of the hash value , Then get the hash value of two adjacent nodes of the hash value corresponding to the data , Prove that these two hash values are adjacent in the tree .
( It is more convincing than simple traversal )
The relevant code implementation is as follows :
void tree::buildBaseLeafes(vector<string> base_leafs) // Create a leaf node list
{
cout << " The hash value corresponding to the data : " << endl;
for (int i = 0; i < base_leafs.size(); i++) {
cout << base_leafs[i] << " : ";
base_leafs[i] = sha256::hash256_hex_string(base_leafs[i]);
cout << base_leafs[i] << endl;
}
cout << endl << endl;
sort(base_leafs.begin(), base_leafs.end());
cout << " After the hash value is sorted :" << endl;
for (int i = 0; i < base_leafs.size(); i++) {
cout << base_leafs[i] << endl;
}
cout << endl << endl;
vector<node*> new_nodes;
for (auto leaf : base_leafs) // Create corresponding nodes for each string , And set the hash value through this string
{
node* new_node = new node;
new_node->setHash(leaf);
new_nodes.push_back(new_node);
}
base.push_back(new_nodes);
cout << endl;
}
int tree::verify(string hash)
{
node* el_node = nullptr;
node* el_node_t_1 = nullptr;
node* el_node_t_2 = nullptr;
string act_hash = hash; // The hash value of the leaf node you want to verify
int t = 0;
// If base[0] That is, there is one node in the leaf node hash Value is equal to it
for (int i = 0; i < base[0].size(); i++)
{
if (base[0][i]->getHash() == act_hash)
{
el_node = base[0][i];// Point to the node
break;
}
if (i < base[0].size() - 1) {
if (act_hash > base[0][i]->getHash() && act_hash < base[0][i + 1]->getHash()) {
el_node_t_1 = base[0][i];
el_node_t_2 = base[0][i + 1];
t = i;
}
}
}
if (el_node == nullptr)
{
cout << endl;
cout << " The hash value used :" << endl;
cout << endl;
cout << " Leaf nodes adjacent to hash values :" << endl;
cout << el_node_t_1->getHash() << endl;
cout << el_node_t_2->getHash() << endl;
cout << endl;
if (t % 2 == 0) {
cout << " The node where the above two hash values are located has the same parent node ..." << endl;
el_node_t_1 = el_node_t_1->getParent();
do {
if (el_node_t_1->checkDir() == 0)
{
std::cout << el_node_t_1->getSibling()->getHash() << endl;
}
else
{
std::cout << el_node_t_1->getSibling()->getHash() << endl;
}
el_node_t_1 = el_node_t_1->getParent();
} while ((el_node_t_1->getParent()) != NULL);
}
else {
cout << " The node where the above two hash values are located does not have the same parent node ..." << endl;
string act_hash_t_1 = el_node_t_1->getHash();
string act_hash_t_2 = el_node_t_2->getHash();
int cor = 0;// Judge whether the two nodes converge to the same parent node
if (el_node_t_1->checkDir() == 0)
{
act_hash_t_1 = sha256::hash256_hex_string(act_hash_t_1 + el_node_t_1->getSibling()->getHash());
}
else
{
act_hash_t_1 = sha256::hash256_hex_string(el_node_t_1->getSibling()->getHash() + act_hash_t_1);
}
if (el_node_t_2->checkDir() == 0)
{
act_hash_t_2 = sha256::hash256_hex_string(act_hash_t_2 + el_node_t_2->getSibling()->getHash());
}
else
{
act_hash_t_2 = sha256::hash256_hex_string(el_node_t_2->getSibling()->getHash() + act_hash_t_2);
}
do {
if (act_hash_t_1 != act_hash_t_2) {
std::cout << el_node_t_1->getSibling()->getHash() << endl;
el_node_t_1 = el_node_t_1->getParent();
std::cout << el_node_t_2->getSibling()->getHash() << endl;
el_node_t_2 = el_node_t_2->getParent();
if (el_node_t_1->checkDir() == 0)
{
act_hash_t_1 = sha256::hash256_hex_string(act_hash_t_1 + el_node_t_1->getSibling()->getHash());
}
else
{
act_hash_t_1 = sha256::hash256_hex_string(el_node_t_1->getSibling()->getHash() + act_hash_t_1);
}
if (el_node_t_2->checkDir() == 0)
{
act_hash_t_2 = sha256::hash256_hex_string(act_hash_t_2 + el_node_t_2->getSibling()->getHash());
}
else
{
act_hash_t_2 = sha256::hash256_hex_string(el_node_t_2->getSibling()->getHash() + act_hash_t_2);
}
}
else {
cor = 1;
el_node_t_1 = el_node_t_1->getParent();
}
} while ((el_node_t_1->getParent()) != NULL && cor == 0);
do {
std::cout << el_node_t_1->getSibling()->getHash() << endl;
el_node_t_1 = el_node_t_1->getParent();
} while ((el_node_t_1->getParent()) != NULL);
}
return 0;
}
else {
cout << " The hash value used :" << endl;
cout << act_hash << endl;
do // verification merkle tree Have you changed
{
// The hash of the parent node is the hash of the left child string+ Hash of right child string
// If el_node The left node of the parent node of is el_node
if (el_node->checkDir() == 0)
{
// It's the left child Left child's hash string+ Hash of right child string
act_hash = sha256::hash256_hex_string(act_hash + el_node->getSibling()->getHash());
}
else
{
act_hash = sha256::hash256_hex_string(el_node->getSibling()->getHash() + act_hash);
}
std::cout << act_hash << endl;
el_node = el_node->getParent();
} while ((el_node->getParent()) != NULL); // Reach the root node
return act_hash == merkleRoot ? 1 : 0;
}
}
At the end
Tips : Please refer to the previous article for the source code : Merkle Tree structure (C++ Realization )
This code is for reference only , If you have any professional questions, please leave a message in the comment area .
边栏推荐
- day10_ Exception handling & enumeration
- 多线程服务器编程
- Those vulnerability attacks on app
- 关于DDoS的几个误区
- FIR filter design (1) -- using the FDATool toolbox of MATLAB to design FIR filter parameters
- day10_异常处理&枚举
- Official tutorial redshift 08 light
- day13_ Under multithreading
- The difference between DDoS attack and CC attack
- What is DNS amplification attack
猜你喜欢

虹科分享 | FPGA 实现的直通与存储转发切换延迟

EtherCAT主站掉线后,如何保证目标系统免受故障影响?

Personal views on time complexity

软件测试职业发展:软件测试人员该何去何从

Vivado IP核之复数浮点数乘法 Floating-point

day03_1_流程控制

day15_泛型

day09_ Static & Final & code block & abstract class & Interface & internal class
![[leetcode skimming] array 2 - binary search](/img/50/c006cbe5a91774c99eb782d9203fa0.png)
[leetcode skimming] array 2 - binary search

Ue5 light shadow basic shadow full resolution sawtooth shadow solution lumen
随机推荐
Design and simulation code of 4-bit subtracter based on FPGA
IGMP协议软件开发实验
如何判断业务被DDoS攻击?又会造成哪些危害?
day06_类与对象
Joint use skills of joiner.on and stream().Map
Hog+svm for pedestrian detection
盘点 | 全球关键信息基础设施网络安全大事件
虹科分享 | 带您全面认识“CAN总线错误”(一)——CAN总线错误与错误帧
Day16 set
Ue5 landscape conversion Nanite conversion method and it does not support the use method of starting dynamic mesh with lumen and lumen
JVM memory structure
Solution for website being suspended
FTP的两种模式详解
Vivado IP核之浮点数乘除法 Floating-point
Advanced socket programming (options and control information)
NoClassDefFoundError processing
不安全的第三方组件的漏洞如何做前置规避?
一文看懂网络安全五年之巨变
Leetcode - Tips
IGMP protocol software development experiment