You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an array or dictionary (which can also be deep) and a position in it, plus an hash function (default SHA3-512), produce a Merkle root hash of all the data below the pointer, recursively.
When I create the merkle root of '''
When I create the merkle root of '' at position ''
The 'position' may be searched as a string among the keys of the dictionary (slow and prone to errors) or be better a pointed address or also leverage pointer function defined in PR #752
Consequently make a verification statement that verifies the integrity of a data structure (possibly at position)
When I verify the merkle root '' of ''
When I verify the merkle root '' of '' at position ''
Mockup code in Lua:
localcrypto=require("crypto") -- Ensure you have a crypto library for hashing, such as luacrypto or similar-- Function to hash data using SHA-256localfunctionhash(data)
returncrypto.digest("sha256", data)
end-- Function to create a Merkle tree from a table of datalocalfunctioncreate_merkle_tree(data_table)
localtree= {}
-- Hash each piece of data and add to the treefor_, datainipairs(data_table) dotable.insert(tree, hash(data))
end-- Build the tree by hashing pairs of nodes until a single hash (the root) is obtainedwhile#tree>1dolocaltemp_tree= {}
fori=1, #tree, 2doifi+1<=#treethenlocalconcatenated_hashes=tree[i] ..tree[i+1]
table.insert(temp_tree, hash(concatenated_hashes))
elsetable.insert(temp_tree, tree[i])
endendtree=temp_treeendreturntree[1] -- The Merkle rootend-- Function to verify the integrity of a Merkle treelocalfunctionverify_merkle_tree(data_table, merkle_root)
localcomputed_root=create_merkle_tree(data_table)
returncomputed_root==merkle_rootend-- Example usagelocaldata= {
"data1",
"data2",
"data3",
"data4"
}
localmerkle_root=create_merkle_tree(data)
print("Merkle Root: " ..merkle_root)
localis_valid=verify_merkle_tree(data, merkle_root)
print("Is the Merkle tree valid? " ..tostring(is_valid))
-- Modify data to test verificationdata[2] ="tampered_data"localis_valid_after_tampering=verify_merkle_tree(data, merkle_root)
print("Is the Merkle tree valid after tampering? " ..tostring(is_valid_after_tampering))
The text was updated successfully, but these errors were encountered:
Given an array or dictionary (which can also be deep) and a position in it, plus an hash function (default SHA3-512), produce a Merkle root hash of all the data below the pointer, recursively.
The 'position' may be searched as a string among the keys of the dictionary (slow and prone to errors) or be better a pointed address or also leverage pointer function defined in PR #752
Consequently make a verification statement that verifies the integrity of a data structure (possibly at position)
Mockup code in Lua:
The text was updated successfully, but these errors were encountered: