forked from keybase/client
/
position.go
266 lines (218 loc) · 7.39 KB
/
position.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package merkletree2
import (
"math/big"
)
// Position represents the position of a node in the tree. When converted to
// bytes, a Position can be interpreted as a 1 followed (from left to right) by
// a sequence of log2(Config.ChildrenPerNode)-bit symbols, where each such
// symbol identifies which child to descend to in a path from the root to a
// node. The sequence is padded with 0s on the left to the nearest byte. For
// example, in a binary tree the root has position 0x01 (i.e. 0b00000001), and
// the second child of the first child of the root has position 0x05
// (0b00000101).
type Position big.Int
func (t *Config) GetRootPosition() *Position {
return (*Position)(big.NewInt(1))
}
func (t *Config) GetChild(p *Position, c ChildIndex) *Position {
var q big.Int
q.Lsh((*big.Int)(p), uint(t.BitsPerIndex))
q.Bits()[0] = q.Bits()[0] | big.Word(c)
return (*Position)(&q)
}
func (p *Position) GetBytes() []byte {
return (*big.Int)(p).Bytes()
}
func (p *Position) AsString() string {
return string(p.GetBytes())
}
func (p *Position) SetBytes(b []byte) {
(*big.Int)(p).SetBytes(b)
}
func NewPositionFromBytes(pos []byte) *Position {
var p big.Int
p.SetBytes(pos)
return (*Position)(&p)
}
// Set updates p to the value of q
func (p *Position) Set(q *Position) {
(*big.Int)(p).Set((*big.Int)(q))
}
// Clone returns a pointer to a deep copy of a position
func (p *Position) Clone() *Position {
var q Position
q.Set(p)
return &q
}
func (p *Position) isOnPathToKey(k Key) bool {
// If the Key is shorter than current prefix
if len(k)*8 < (*big.Int)(p).BitLen()-1 {
return false
}
var q big.Int
q.SetBytes([]byte(k))
q.SetBit(&q, len(k)*8, 1)
q.Rsh(&q, uint(q.BitLen()-(*big.Int)(p).BitLen()))
return (*big.Int)(p).Cmp(&q) == 0
}
func (p *Position) Equals(q *Position) bool {
return (*big.Int)(p).CmpAbs((*big.Int)(q)) == 0
}
// getParent return nil if the p is the root
func (t *Config) getParent(p *Position) *Position {
if (*big.Int)(p).BitLen() < 2 {
return nil
}
f := p.Clone()
t.updateToParent(f)
return f
}
func (t *Config) updateToParent(p *Position) {
((*big.Int)(p)).Rsh((*big.Int)(p), uint(t.BitsPerIndex))
}
// Behavior if p has no parent at the requested level is undefined.
func (t *Config) updateToParentAtLevel(p *Position, level uint) {
shift := (*big.Int)(p).BitLen() - 1 - int(t.BitsPerIndex)*int(level)
((*big.Int)(p)).Rsh((*big.Int)(p), uint(shift))
}
// updateToParentAndAllSiblings takes as input p and a slice of size
// t.cfg.ChildrenPerNode - 1. It populates the slice with the siblings of p, and
// updates p to be its parent.
func (t *Config) updateToParentAndAllSiblings(p *Position, sibs []Position) {
if (*big.Int)(p).BitLen() < 2 {
return
}
// Optimization for binary trees
if t.ChildrenPerNode == 2 {
sibs[0].Set(p)
lsBits := &(((*big.Int)(&sibs[0]).Bits())[0])
*lsBits = (*lsBits ^ 1)
} else {
pChildIndex := big.Word(t.getDeepestChildIndex(p))
mask := ^((big.Word)((1 << t.BitsPerIndex) - 1))
for i, j := uint(0), big.Word(0); j < big.Word(t.ChildrenPerNode); j++ {
if j == pChildIndex {
continue
}
sibs[i].Set(p)
// Set least significant bits to the j-th children
lsBits := &(((*big.Int)(&sibs[i]).Bits())[0])
*lsBits = (*lsBits & mask) | j
i++
}
}
t.updateToParent(p)
}
// getDeepestPositionForKey converts the key into the position the key would be
// stored at if the tree was full with only one key per leaf.
func (t *Config) getDeepestPositionForKey(k Key) (*Position, error) {
if len(k) != t.KeysByteLength {
return nil, NewInvalidKeyError()
}
var p Position
(*big.Int)(&p).SetBytes(k)
(*big.Int)(&p).SetBit((*big.Int)(&p), len(k)*8, 1)
return &p, nil
}
// Returns the lexicographically first key which could be found at any children
// of position p in the tree
func (t *Config) getMinKey(p *Position) Key {
var min big.Int
min.Set((*big.Int)(p))
n := uint(t.KeysByteLength*8 + 1 - min.BitLen())
min.Lsh(&min, n)
return min.Bytes()[1:]
}
func (t *Config) GetKeyIntervalUnderPosition(p *Position) (minKey, maxKey Key) {
var min, max big.Int
min.Set((*big.Int)(p))
n := uint(t.KeysByteLength*8 + 1 - min.BitLen())
min.Lsh(&min, n)
minKey = min.Bytes()[1:]
one := big.NewInt(1)
max.Lsh(one, n)
max.Sub(&max, one)
max.Or(&max, &min)
maxKey = max.Bytes()[1:]
return minKey, maxKey
}
// getDeepestPositionAtLevelAndSiblingsOnPathToKey returns a slice of positions,
// in descending order by level (siblings farther from the root come first) and
// in lexicographic order within each level. The first position in the slice is
// the position at level lastLevel on a path from the root to k (or the deepest
// possible position for such key if latLevel is greater than that). The
// following positions are all the siblings of the nodes on the longest possible
// path from the root to the key k with are at levels from lastLevel (excluded)
// to firstLevel (included).
// See TestGetDeepestPositionAtLevelAndSiblingsOnPathToKey for sample outputs.
func (t *Config) getDeepestPositionAtLevelAndSiblingsOnPathToKey(k Key, lastLevel int, firstLevel int) (sibs []Position) {
maxLevel := t.KeysByteLength * 8 / int(t.BitsPerIndex)
if lastLevel > maxLevel {
lastLevel = maxLevel
}
// first, shrink the key for efficiency
bytesNecessary := lastLevel * int(t.BitsPerIndex) / 8
if lastLevel*int(t.BitsPerIndex)%8 != 0 {
bytesNecessary++
}
k = k[:bytesNecessary]
var buf Position
p := &buf
(*big.Int)(p).SetBytes(k)
(*big.Int)(p).SetBit((*big.Int)(p), len(k)*8, 1)
t.updateToParentAtLevel(p, uint(lastLevel))
sibs = make([]Position, (lastLevel-firstLevel+1)*(t.ChildrenPerNode-1)+1)
sibs[0].Set(p)
for i, j := lastLevel, 0; i >= firstLevel; i-- {
sibsToFill := sibs[1+(t.ChildrenPerNode-1)*j : 1+(t.ChildrenPerNode-1)*(j+1)]
t.updateToParentAndAllSiblings(p, sibsToFill)
j++
}
return sibs
}
// getLevel returns the level of p. The root is at level 0, and each node has
// level 1 higher than its parent.
func (t *Config) getLevel(p *Position) int {
return ((*big.Int)(p).BitLen() - 1) / int(t.BitsPerIndex)
}
// getParentAtLevel returns nil if p is at a level lower than `level`. The root
// is at level 0, and each node has level 1 higher than its parent.
func (t *Config) getParentAtLevel(p *Position, level uint) *Position {
shift := (*big.Int)(p).BitLen() - 1 - int(t.BitsPerIndex)*int(level)
if (*big.Int)(p).BitLen() < 2 || shift < 0 {
return nil
}
f := p.Clone()
t.updateToParentAtLevel(f, level)
return f
}
// positionToChildIndexPath returns the list of childIndexes to navigate from the
// root to p (in reverse order).
func (t *Config) positionToChildIndexPath(p *Position) (path []ChildIndex) {
path = make([]ChildIndex, t.getLevel(p))
bitMask := big.Word(t.ChildrenPerNode - 1)
buff := p.Clone()
for i := range path {
path[i] = ChildIndex(((*big.Int)(buff)).Bits()[0] & bitMask)
((*big.Int)(buff)).Rsh((*big.Int)(buff), uint(t.BitsPerIndex))
}
return path
}
// getDeepestChildIndex returns the only ChildIndex i such that p is the i-th children of
// its parent. It returns 0 on the root.
func (t *Config) getDeepestChildIndex(p *Position) ChildIndex {
if (*big.Int)(p).BitLen() < 2 {
return ChildIndex(0)
}
return ChildIndex(((*big.Int)(p).Bits())[0] & ((1 << t.BitsPerIndex) - 1))
}
func (p *Position) CmpInMerkleProofOrder(p2 *Position) int {
lp := (*big.Int)(p).BitLen()
lp2 := (*big.Int)(p2).BitLen()
if lp > lp2 {
return -1
} else if lp < lp2 {
return 1
}
return (*big.Int)(p).CmpAbs((*big.Int)(p2))
}