-
Notifications
You must be signed in to change notification settings - Fork 0
/
MerkleDAGNode.m
77 lines (68 loc) · 2.95 KB
/
MerkleDAGNode.m
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
classdef MerkleDAGNode < handle
properties
Data % Data associated with the node
Hash % Stores the cryptographic hash of either the provided data or directly assigned hash
Children % Array of child nodes
Parent % Parent node
HashAlgorithm % Hashing algorithm (default: 'SHA-256')
end
methods
function obj = MerkleDAGNode(data, hashAlgorithm, hash)
if nargin > 0
if nargin < 2 || isempty(hashAlgorithm)
hashAlgorithm = 'SHA-256'; % Default to SHA-256 if not specified
end
obj.HashAlgorithm = hashAlgorithm;
if isempty(data)
obj.Data = [];
obj.Hash = hash;
else
obj.Data = data;
obj.Hash = obj.computeHash(data);
end
obj.Children = {};
obj.Parent = [];
end
end
%% Add a child node to the current node
function addChild(obj, childNode)
obj.Children{end+1} = childNode;
childNode.Parent = obj;
obj.updateHash(); % Update hash of the current node after adding child
obj.propagateHashUpdate(); % Propagate hash update up the graph
end
%% Update hash of the current node based on its data and children
function updateHash(obj)
if isempty(obj.Children)
obj.Hash = obj.computeHash(obj.Data); % Recompute hash if no children
else
childHashes = cellfun(@(child) child.Hash, obj.Children, 'UniformOutput', false);
combinedHash = obj.computeHash([obj.Hash, childHashes{:}]);
obj.Hash = combinedHash;
end
end
%% Propagate hash update: recursively updates the hash of the current node's parent
function propagateHashUpdate(obj)
parent = obj.Parent;
while ~isempty(parent)
parent.updateHash();
parent = parent.Parent;
end
end
%% Compute the cryptographic hash of the data
function hash = computeHash(obj, data)
persistent hasher;
if isempty(hasher) || ~isequal(hasher.getAlgorithm(), obj.HashAlgorithm)
hasher = java.security.MessageDigest.getInstance(obj.HashAlgorithm);
end
% Convert to uint8
if isnumeric(data)
serializedData = typecast(data(:), 'uint8');
else
serializedData = uint8(data(:)');
end
hasher.update(serializedData(:));
hash = char(reshape(dec2hex(typecast(hasher.digest(), 'uint8'))', 1, []));
end
end
end