-
Notifications
You must be signed in to change notification settings - Fork 0
/
merkle_tree.mjs
145 lines (108 loc) · 3.13 KB
/
merkle_tree.mjs
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
export class MerkleTree {
constructor(bottomLevel, hasher) {
this.bottomLevel = bottomLevel
this.hasher = hasher
// node -> index at level 0
this.nodeMap = {}
// [level, index] -> node
this.positionMap = {}
// level -> zero at the level
this.zeroes = []
this.nextAppendIndex = 0
let current = 0n
for(let currentLevel = this.bottomLevel; currentLevel >= 1; currentLevel--) {
this.zeroes[currentLevel] = current
current = this.hasher(current, current)
}
this.zeroes[0] = current
}
append(element, updateInternalState = false) {
this.insert(this.nextAppendIndex, element, updateInternalState)
this.nextAppendIndex++
}
appendAll(elements, updateInternalState = false) {
for(let element of elements) {
this.append(element, updateInternalState)
}
}
insert(bottomIndex, element, updateInternalState = false) {
if(updateInternalState) {
this.updateInternalState(bottomIndex)
}
this.nodeMap[element] = bottomIndex
this.positionMap[[this.bottomLevel, bottomIndex]] = element
}
remove(bottomIndex) {
let element = this.positionMap[[this.bottomLevel, bottomIndex]]
if(!element) {
return false
}
delete this.nodeMap[element]
delete this.positionMap[[this.bottomLevel, bottomIndex]]
this.updateInternalState(bottomIndex)
return true
}
updateInternalState(bottomIndex) {
let currentIndex = bottomIndex
for(let currentLevel = this.bottomLevel; currentLevel >= 0; currentLevel--) {
if(currentLevel != this.bottomLevel) {
delete this.positionMap[[currentLevel, currentIndex]]
}
currentIndex = Math.floor(currentIndex / 2)
}
}
getBottomIndex(element) {
return this.nodeMap[element]
}
getNode(level, index) {
if(index > ((this.nextAppendIndex - 1) >> (this.bottomLevel - level))) {
return this.zeroes[level]
}
let result = this.positionMap[[level, index]]
if(result == undefined) {
if(level < this.bottomLevel) {
result = this.hasher(this.getNode(level + 1, 2 * index), this.getNode(level + 1, (2 * index) + 1))
}
else {
return this.zeroes[level]
}
}
this.positionMap[[level, index]] = result
return result
}
getRoot() {
return this.getNode(0, 0)
}
getProof(bottomIndex) {
return this.getLevelProof(bottomIndex, 0)
}
getLevelProof(bottomIndex, topLevel) {
let siblings = []
let isLeft = []
let currentIndex = bottomIndex
for(let currentLevel = this.bottomLevel; currentLevel >= (topLevel + 1); currentLevel--) {
if(currentIndex & 0x1) {
siblings[currentLevel] = this.getNode(currentLevel, currentIndex - 1)
isLeft[currentLevel] = false
}
else {
siblings[currentLevel] = this.getNode(currentLevel, currentIndex + 1)
isLeft[currentLevel] = true
}
currentIndex = Math.floor(currentIndex / 2)
}
// For levels not asked, just place default values
for(let currentLevel = topLevel; currentLevel >= 1; currentLevel--) {
siblings[currentLevel] = 0n
isLeft[currentLevel] = false
}
let root = this.getNode(topLevel, currentIndex)
siblings.shift()
isLeft.shift()
console.log([root, siblings, isLeft])
return [root, siblings, isLeft]
}
size() {
return this.nextAppendIndex
}
}