-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.js
More file actions
173 lines (143 loc) · 4.79 KB
/
functions.js
File metadata and controls
173 lines (143 loc) · 4.79 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const crypto = require("crypto");
const makeCanonicalPair = (l,r) => ({ l: maskLeft(l), r: maskRight(r) });
function append(digest, activeNodes, nodeCount, stopAtDepth = -1) {
const maxDepth = calculateMaxDepth(nodeCount + 1);
let currentDepth = stopAtDepth == -1 ? maxDepth - 1 : stopAtDepth;
let count = 0;
const nodes = [];
let partial = false;
let index = nodeCount;
let top = digest;
while (currentDepth > 0) {
if (!(index & 0x1)) {
if (!partial) nodes.push(top);
top = hashPair(makeCanonicalPair(top, top));
partial = true;
}
else {
var left_value = activeNodes[count];
count++;
if (partial) nodes.push(left_value);
top = hashPair(makeCanonicalPair(left_value, top));
}
currentDepth--;
index = index >> 1;
}
nodes.push(top);
return { activeNodes: nodes, root: top };
}
function hashPair(p){
var buffLeft = Buffer.from(p.l, "hex")
var buffRight = Buffer.from(p.r, "hex")
var buffFinal = Buffer.concat([buffLeft, buffRight]);
var finalHash = crypto.createHash("sha256").update(buffFinal).digest("hex");
return finalHash;
}
function annotateIncrementalMerkleTree(tree, log){
var npo2 = nextPowerOf2(tree.nodeCount);
var ppo2 = prevPowerOf2(tree.nodeCount+1);
if (log){
console.log("\nAnnotating tree for block : ", tree.nodeCount);
console.log(" -> next power of 2 : ", npo2);
console.log(" -> prev power of 2 : ", ppo2);
console.log("")
}
var accountedForLower = 0;
var accountedForUpper = 0;
let rootIntegers;
let blocksRequired = [];
let blockToEdit;
for (var i = tree.activeNodes.length-1; i>=0; i--){
tree.activeNodes[i] = { node : tree.activeNodes[i] };
if (i == tree.activeNodes.length-1 ){
if(log) console.log(" node ", tree.activeNodes[i].node, "root", " po2 : ", npo2);
rootIntegers = npo2.toString().length;
if (tree.activeNodes.length == 1) updateActiveNode(i)
}
else {
if (accountedForUpper+ppo2 >= tree.nodeCount+1){
do {
if(log) console.log(" no node ", " ".repeat(68), "po2 : ", ppo2);
ppo2/=2;
} while (accountedForUpper+ppo2 >= tree.nodeCount+1);
updateActiveNode(i, " partial ");
}
else updateActiveNode(i, " fully realized ");
}
}
function updateActiveNode(i, type){
accountedForLower = accountedForUpper + 1;
accountedForUpper+= ppo2;
ppo2/= 2;
const po2 = accountedForUpper - accountedForLower + 1;
tree.activeNodes[i].accountedForLower = accountedForLower;
tree.activeNodes[i].accountedForUpper = accountedForUpper;
tree.activeNodes[i].po2 = type ? po2 : ppo2;
if (type){
let aliveUntil;
tree.activeNodes[i].lifetime = accountedForUpper + po2;
tree.activeNodes[i].ttl = tree.activeNodes[i].lifetime - tree.nodeCount;
if (po2>1) {
aliveUntil = accountedForUpper + po2;
blocksRequired.push({blockNum: accountedForUpper +1, aliveUntil });
if(log) console.log(" node ", tree.activeNodes[i].node," ".repeat(5), " po2 : ", po2, " ".repeat(rootIntegers - po2.toString().length), "alive until", aliveUntil, "ttl", tree.activeNodes[i].ttl," ".repeat(rootIntegers - tree.activeNodes[i].ttl.toString().length), "(" + type +")", "accounts for leaves : ", accountedForLower, " to ", accountedForUpper);
}
else {
aliveUntil = accountedForLower+1;
if(log) console.log(" node ", tree.activeNodes[i].node," ".repeat(5), " po2 : ", 1, " ".repeat(rootIntegers-1), "alive until",aliveUntil, "ttl", 1," ".repeat(rootIntegers-1), "(" + type +")","accounts for leaves : ", accountedForLower);
}
blockToEdit = {blockNum: tree.nodeCount, aliveUntil }
}
}
return { blocksRequired, blockToEdit };
}
function nextPowerOf2(value) {
value -= 1;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value |= value >> 32;
value += 1;
return value;
}
function prevPowerOf2(value) {
value -= 1;
value |= (value >> 1);
value |= (value >> 2);
value |= (value >> 4);
value |= (value >> 8);
value |= (value >> 16);
value |= (value >> 32);
return value - (value >> 1);
}
function clzPower2( value) {
var count = 1;
for (var i = 0; i < 30; i++){
count*=2;
if (value == count) return i+1;
}
return 0;
}
function calculateMaxDepth( nodeCount) {
if (nodeCount == 0) return 0;
var impliedCount = nextPowerOf2(nodeCount);
return clzPower2(impliedCount) + 1;
}
function maskLeft(n){
var nn = Buffer.from(n, "hex");
nn[0] &= 0x7f;
if (nn[0] < 0) nn[0] += (1 << 30) * 4;
return nn;
}
function maskRight(n){
var nn = Buffer.from(n, "hex");
nn[0] |= 0x80;
if (nn[0] < 0) nn[0] += (1 << 30) * 4;
return nn;
}
module.exports = {
append,
annotateIncrementalMerkleTree
}