-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
tree.ts
734 lines (644 loc) · 22.3 KB
/
tree.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
import {zeroNode} from "./zeroNode";
import {Gindex, GindexBitstring, convertGindexToBitstring} from "./gindex";
import {Node, LeafNode, BranchNode} from "./node";
import {createNodeFromProof, createProof, Proof, ProofInput} from "./proof";
import {createSingleProof} from "./proof/single";
export type Hook = (newRootNode: Node) => void;
/**
* Binary merkle tree
*
* Wrapper around immutable `Node` to support mutability.
*
* Mutability between a parent tree and subtree is achieved by maintaining a `hook` callback, which updates the parent when the subtree is updated.
*/
export class Tree {
private _rootNode: Node;
private hook?: Hook | WeakRef<Hook>;
constructor(node: Node, hook?: Hook) {
this._rootNode = node;
if (hook) {
if (typeof WeakRef === "undefined") {
this.hook = hook;
} else {
this.hook = new WeakRef(hook);
}
}
}
/**
* Create a `Tree` from a `Proof` object
*/
static createFromProof(proof: Proof): Tree {
return new Tree(createNodeFromProof(proof));
}
/**
* The root node of the tree
*/
get rootNode(): Node {
return this._rootNode;
}
/**
*
* Setting the root node will trigger a call to the tree's `hook` if it exists.
*/
set rootNode(newRootNode: Node) {
this._rootNode = newRootNode;
if (this.hook) {
// WeakRef should not change status during a program's execution
// So, use WeakRef feature detection to assume the type of this.hook
// to minimize the memory footprint of Tree
if (typeof WeakRef === "undefined") {
(this.hook as Hook)(newRootNode);
} else {
const hookVar = (this.hook as WeakRef<Hook>).deref();
if (hookVar) {
hookVar(newRootNode);
} else {
// Hook has been garbage collected, no need to keep the hookRef
this.hook = undefined;
}
}
}
}
/**
* The root hash of the tree
*/
get root(): Uint8Array {
return this.rootNode.root;
}
/**
* Return a copy of the tree
*/
clone(): Tree {
return new Tree(this.rootNode);
}
/**
* Return the subtree at the specified gindex.
*
* Note: The returned subtree will have a `hook` attached to the parent tree.
* Updates to the subtree will result in updates to the parent.
*/
getSubtree(index: Gindex | GindexBitstring): Tree {
return new Tree(this.getNode(index), (node) => this.setNode(index, node));
}
/**
* Return the node at the specified gindex.
*/
getNode(gindex: Gindex | GindexBitstring): Node {
return getNode(this.rootNode, gindex);
}
/**
* Return the node at the specified depth and index.
*
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
getNodeAtDepth(depth: number, index: number): Node {
return getNodeAtDepth(this.rootNode, depth, index);
}
/**
* Return the hash at the specified gindex.
*/
getRoot(index: Gindex | GindexBitstring): Uint8Array {
return this.getNode(index).root;
}
/**
* Set the node at at the specified gindex.
*/
setNode(gindex: Gindex | GindexBitstring, n: Node): void {
this.rootNode = setNode(this.rootNode, gindex, n);
}
/**
* Traverse to the node at the specified gindex,
* then apply the function to get a new node and set the node at the specified gindex with the result.
*
* This is a convenient method to avoid traversing the tree 2 times to
* get and set.
*/
setNodeWithFn(gindex: Gindex | GindexBitstring, getNewNode: (node: Node) => Node): void {
this.rootNode = setNodeWithFn(this.rootNode, gindex, getNewNode);
}
/**
* Set the node at the specified depth and index.
*
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
setNodeAtDepth(depth: number, index: number, node: Node): void {
this.rootNode = setNodeAtDepth(this.rootNode, depth, index, node);
}
/**
* Set the hash at the specified gindex.
*
* Note: This will set a new `LeafNode` at the specified gindex.
*/
setRoot(index: Gindex | GindexBitstring, root: Uint8Array): void {
this.setNode(index, LeafNode.fromRoot(root));
}
/**
* Fast read-only iteration
* In-order traversal of nodes at `depth`
* starting from the `startIndex`-indexed node
* iterating through `count` nodes
*
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
getNodesAtDepth(depth: number, startIndex: number, count: number): Node[] {
return getNodesAtDepth(this.rootNode, depth, startIndex, count);
}
/**
* Fast read-only iteration
* In-order traversal of nodes at `depth`
* starting from the `startIndex`-indexed node
* iterating through `count` nodes
*
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
iterateNodesAtDepth(depth: number, startIndex: number, count: number): IterableIterator<Node> {
return iterateNodesAtDepth(this.rootNode, depth, startIndex, count);
}
/**
* Return a merkle proof for the node at the specified gindex.
*/
getSingleProof(index: Gindex): Uint8Array[] {
return createSingleProof(this.rootNode, index)[1];
}
/**
* Return a merkle proof for the proof input.
*
* This method can be used to create multiproofs.
*/
getProof(input: ProofInput): Proof {
return createProof(this.rootNode, input);
}
}
/**
* Return the node at the specified gindex.
*/
export function getNode(rootNode: Node, gindex: Gindex | GindexBitstring): Node {
const gindexBitstring = convertGindexToBitstring(gindex);
let node = rootNode;
for (let i = 1; i < gindexBitstring.length; i++) {
if (node.isLeaf()) {
throw new Error(`Invalid tree - found leaf at depth ${i}`);
}
// If bit is set, means navigate right
node = gindexBitstring[i] === "1" ? node.right : node.left;
}
return node;
}
/**
* Set the node at at the specified gindex.
* Returns the new root node.
*/
export function setNode(rootNode: Node, gindex: Gindex | GindexBitstring, n: Node): Node {
// Pre-compute entire bitstring instead of using an iterator (25% faster)
const gindexBitstring = convertGindexToBitstring(gindex);
const parentNodes = getParentNodes(rootNode, gindexBitstring);
return rebindNodeToRoot(gindexBitstring, parentNodes, n);
}
/**
* Traverse to the node at the specified gindex,
* then apply the function to get a new node and set the node at the specified gindex with the result.
*
* This is a convenient method to avoid traversing the tree 2 times to
* get and set.
*
* Returns the new root node.
*/
export function setNodeWithFn(
rootNode: Node,
gindex: Gindex | GindexBitstring,
getNewNode: (node: Node) => Node
): Node {
// Pre-compute entire bitstring instead of using an iterator (25% faster)
const gindexBitstring = convertGindexToBitstring(gindex);
const parentNodes = getParentNodes(rootNode, gindexBitstring);
const lastParentNode = parentNodes[parentNodes.length - 1];
const lastBit = gindexBitstring[gindexBitstring.length - 1];
const oldNode = lastBit === "1" ? lastParentNode.right : lastParentNode.left;
const newNode = getNewNode(oldNode);
return rebindNodeToRoot(gindexBitstring, parentNodes, newNode);
}
/**
* Traverse the tree from root node, ignore the last bit to get all parent nodes
* of the specified bitstring.
*/
function getParentNodes(rootNode: Node, bitstring: GindexBitstring): Node[] {
let node = rootNode;
// Keep a list of all parent nodes of node at gindex `index`. Then walk the list
// backwards to rebind them "recursively" with the new nodes without using functions
const parentNodes: Node[] = [rootNode];
// Ignore the first bit, left right directions are at bits [1,..]
// Ignore the last bit, no need to push the target node to the parentNodes array
for (let i = 1; i < bitstring.length - 1; i++) {
// Compare to string directly to prevent unnecessary type conversions
if (bitstring[i] === "1") {
node = node.right;
} else {
node = node.left;
}
parentNodes.push(node);
}
return parentNodes;
}
/**
* Build a new tree structure from bitstring, parentNodes and a new node.
* Returns the new root node.
*/
function rebindNodeToRoot(bitstring: GindexBitstring, parentNodes: Node[], newNode: Node): Node {
let node = newNode;
// Ignore the first bit, left right directions are at bits [1,..]
// Iterate the list backwards including the last bit, but offset the parentNodes array
// by one since the first bit in bitstring was ignored in the previous loop
for (let i = bitstring.length - 1; i >= 1; i--) {
if (bitstring[i] === "1") {
node = new BranchNode(parentNodes[i - 1].left, node);
} else {
node = new BranchNode(node, parentNodes[i - 1].right);
}
}
return node;
}
/**
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
export function getNodeAtDepth(rootNode: Node, depth: number, index: number): Node {
if (depth === 0) {
return rootNode;
}
if (depth === 1) {
return index === 0 ? rootNode.left : rootNode.right;
}
// Ignore first bit "1", then substract 1 to get to the parent
const depthiRoot = depth - 1;
const depthiParent = 0;
let node = rootNode;
for (let d = depthiRoot; d >= depthiParent; d--) {
node = isLeftNode(d, index) ? node.left : node.right;
}
return node;
}
/**
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
export function setNodeAtDepth(rootNode: Node, nodesDepth: number, index: number, nodeChanged: Node): Node {
// TODO: OPTIMIZE (if necessary)
return setNodesAtDepth(rootNode, nodesDepth, [index], [nodeChanged]);
}
/**
* Set multiple nodes in batch, editing and traversing nodes strictly once.
*
* - gindexes MUST be sorted in ascending order beforehand.
* - All gindexes must be at the exact same depth.
* - Depth must be > 0, if 0 just replace the root node.
*
* Strategy: for each gindex in `gindexes` navigate to the depth of its parent,
* and create a new parent. Then calculate the closest common depth with the next
* gindex and navigate upwards creating or caching nodes as necessary. Loop and repeat.
*
* Supports index up to `Number.MAX_SAFE_INTEGER`.
*/
export function setNodesAtDepth(rootNode: Node, nodesDepth: number, indexes: number[], nodes: Node[]): Node {
// depth depthi gindexes indexes
// 0 1 1 0
// 1 0 2 3 0 1
// 2 - 4 5 6 7 0 1 2 3
// '10' means, at depth 1, node is at the left
//
// For index N check if the bit at position depthi is set to navigate right at depthi
// ```
// mask = 1 << depthi
// goRight = (N & mask) == mask
// ```
// If depth is 0 there's only one node max and the optimization below will cause a navigation error.
// For this case, check if there's a new root node and return it, otherwise the current rootNode.
if (nodesDepth === 0) {
return nodes.length > 0 ? nodes[0] : rootNode;
}
/**
* Contiguous filled stack of parent nodes. It get filled in the first descent
* Indexed by depthi
*/
const parentNodeStack = new Array<Node>(nodesDepth);
/**
* Temp stack of left parent nodes, index by depthi.
* Node leftParentNodeStack[depthi] is a node at d = depthi - 1, such that:
* ```
* parentNodeStack[depthi].left = leftParentNodeStack[depthi]
* ```
*/
const leftParentNodeStack = new Array<Node | undefined>(nodesDepth);
// Ignore first bit "1", then substract 1 to get to the parent
const depthiRoot = nodesDepth - 1;
const depthiParent = 0;
let depthi = depthiRoot;
let node = rootNode;
// Insert root node to make the loop below general
parentNodeStack[depthiRoot] = rootNode;
// TODO: Iterate to depth 32 to allow using bit ops
// for (; depthi >= 32; depthi--) {
// node = node.left;
// }
for (let i = 0; i < indexes.length; i++) {
const index = indexes[i];
// Navigate down until parent depth, and store the chain of nodes
//
// Starts from latest common depth, so node is the parent node at `depthi`
// When persisting the next node, store at the `d - 1` since its the child of node at `depthi`
//
// Stops at the level above depthiParent. For the re-binding routing below node must be at depthiParent
for (let d = depthi; d > depthiParent; d--) {
node = isLeftNode(d, index) ? node.left : node.right;
parentNodeStack[d - 1] = node;
}
depthi = depthiParent;
// If this is the left node, check first it the next node is on the right
//
// - If both nodes exist, create new
// / \
// x x
//
// - If only the left node exists, rebind left
// / \
// x -
//
// - If this is the right node, only the right node exists, rebind right
// / \
// - x
// d = 0, mask = 1 << d = 1
const isLeftLeafNode = (index & 1) !== 1;
if (isLeftLeafNode) {
// Next node is the very next to the right of current node
if (index + 1 === indexes[i + 1]) {
node = new BranchNode(nodes[i], nodes[i + 1]);
// Move pointer one extra forward since node has consumed two nodes
i++;
} else {
node = new BranchNode(nodes[i], node.right);
}
} else {
node = new BranchNode(node.left, nodes[i]);
}
// Here `node` is the new BranchNode at depthi `depthiParent`
// Now climb upwards until finding the common node with the next index
// For the last iteration, climb to the root at `depthiRoot`
const isLastIndex = i >= indexes.length - 1;
const diffDepthi = isLastIndex ? depthiRoot : findDiffDepthi(index, indexes[i + 1]);
// When climbing up from a left node there are two possible paths
// 1. Go to the right of the parent: Store left node to rebind latter
// 2. Go another level up: Will never visit the left node again, so must rebind now
// 🡼 \ Rebind left only, will never visit this node again
// 🡽 /\
//
// / 🡽 Rebind left only (same as above)
// 🡽 /\
//
// 🡽 /\ 🡾 Store left node to rebind the entire node when returning
//
// 🡼 \ Rebind right with left if exists, will never visit this node again
// /\ 🡼
//
// / 🡽 Rebind right with left if exists (same as above)
// /\ 🡼
for (let d = depthiParent + 1; d <= diffDepthi; d++) {
// If node is on the left, store for latter
// If node is on the right merge with stored left node
if (isLeftNode(d, index)) {
if (isLastIndex || d !== diffDepthi) {
// If it's last index, bind with parent since it won't navigate to the right anymore
// Also, if still has to move upwards, rebind since the node won't be visited anymore
node = new BranchNode(node, parentNodeStack[d].right);
} else {
// Only store the left node if it's at d = diffDepth
leftParentNodeStack[d] = node;
node = parentNodeStack[d];
}
} else {
const leftNode = leftParentNodeStack[d];
if (leftNode !== undefined) {
node = new BranchNode(leftNode, node);
leftParentNodeStack[d] = undefined;
} else {
node = new BranchNode(parentNodeStack[d].left, node);
}
}
}
// Prepare next loop
// Go to the parent of the depth with diff, to switch branches to the right
depthi = diffDepthi;
}
// Done, return new root node
return node;
}
/**
* Fast read-only iteration
* In-order traversal of nodes at `depth`
* starting from the `startIndex`-indexed node
* iterating through `count` nodes
*
* **Strategy**
* 1. Navigate down to parentDepth storing a stack of parents
* 2. At target level push current node
* 3. Go up to the first level that navigated left
* 4. Repeat (1) for next index
*/
export function getNodesAtDepth(rootNode: Node, depth: number, startIndex: number, count: number): Node[] {
// Optimized paths for short trees (x20 times faster)
if (depth === 0) {
return startIndex === 0 && count > 0 ? [rootNode] : [];
} else if (depth === 1) {
if (count === 0) {
return [];
} else if (count === 1) {
return startIndex === 0 ? [rootNode.left] : [rootNode.right];
} else {
return [rootNode.left, rootNode.right];
}
}
// Ignore first bit "1", then substract 1 to get to the parent
const depthiRoot = depth - 1;
const depthiParent = 0;
let depthi = depthiRoot;
let node = rootNode;
// Contiguous filled stack of parent nodes. It get filled in the first descent
// Indexed by depthi
const parentNodeStack = new Array<Node>(depth);
const isLeftStack = new Array<boolean>(depth);
const nodes = new Array<Node>(count);
// Insert root node to make the loop below general
parentNodeStack[depthiRoot] = rootNode;
for (let i = 0; i < count; i++) {
for (let d = depthi; d >= depthiParent; d--) {
if (d !== depthi) {
parentNodeStack[d] = node;
}
const isLeft = isLeftNode(d, startIndex + i);
isLeftStack[d] = isLeft;
node = isLeft ? node.left : node.right;
}
nodes[i] = node;
// Find the first depth where navigation when left.
// Store that height and go right from there
for (let d = depthiParent; d <= depthiRoot; d++) {
if (isLeftStack[d] === true) {
depthi = d;
break;
}
}
node = parentNodeStack[depthi];
}
return nodes;
}
/**
* @see getNodesAtDepth but instead of pushing to an array, it yields
*/
export function* iterateNodesAtDepth(
rootNode: Node,
depth: number,
startIndex: number,
count: number
): IterableIterator<Node> {
const endIndex = startIndex + count;
// Ignore first bit "1", then substract 1 to get to the parent
const depthiRoot = depth - 1;
const depthiParent = 0;
let depthi = depthiRoot;
let node = rootNode;
// Contiguous filled stack of parent nodes. It get filled in the first descent
// Indexed by depthi
const parentNodeStack = new Array<Node>(depth);
const isLeftStack = new Array<boolean>(depth);
// Insert root node to make the loop below general
parentNodeStack[depthiRoot] = rootNode;
for (let index = startIndex; index < endIndex; index++) {
for (let d = depthi; d >= depthiParent; d--) {
if (d !== depthi) {
parentNodeStack[d] = node;
}
const isLeft = isLeftNode(d, index);
isLeftStack[d] = isLeft;
node = isLeft ? node.left : node.right;
}
yield node;
// Find the first depth where navigation when left.
// Store that height and go right from there
for (let d = depthiParent; d <= depthiRoot; d++) {
if (isLeftStack[d] === true) {
depthi = d;
break;
}
}
node = parentNodeStack[depthi];
}
}
/**
* Zero's all nodes right of index with constant depth of `nodesDepth`.
*
* For example, zero-ing this tree at depth 2 after index 0
* ```
* X X
* X X -> X 0
* X X X X X 0 0 0
* ```
*
* Or, zero-ing this tree at depth 3 after index 2
* ```
* X X
* X X X 0
* X X X X -> X X 0 0
* X X X X X X X X X X X 0 0 0 0 0
* ```
*
* The strategy is to first navigate down to `nodesDepth` and `index` and keep a stack of parents.
* Then navigate up re-binding:
* - If navigated to the left rebind with zeroNode()
* - If navigated to the right rebind with parent.left from the stack
*/
export function treeZeroAfterIndex(rootNode: Node, nodesDepth: number, index: number): Node {
// depth depthi gindexes indexes
// 0 1 1 0
// 1 0 2 3 0 1
// 2 - 4 5 6 7 0 1 2 3
// '10' means, at depth 1, node is at the left
//
// For index N check if the bit at position depthi is set to navigate right at depthi
// ```
// mask = 1 << depthi
// goRight = (N & mask) == mask
// ```
// Degenerate case where tree is zero after a negative index (-1).
// All positive indexes are zero, so the entire tree is zero. Return cached zero node as root.
if (index < 0) {
return zeroNode(nodesDepth);
}
/**
* Contiguous filled stack of parent nodes. It get filled in the first descent
* Indexed by depthi
*/
const parentNodeStack = new Array<Node>(nodesDepth);
// Ignore first bit "1", then substract 1 to get to the parent
const depthiRoot = nodesDepth - 1;
const depthiParent = 0;
let depthi = depthiRoot;
let node = rootNode;
// Insert root node to make the loop below general
parentNodeStack[depthiRoot] = rootNode;
// Navigate down until parent depth, and store the chain of nodes
//
// Stops at the depthiParent level. To rebind below down to `nodesDepth`
for (let d = depthi; d >= depthiParent; d--) {
node = isLeftNode(d, index) ? node.left : node.right;
parentNodeStack[d - 1] = node;
}
depthi = depthiParent;
// Now climb up re-binding with either zero of existing tree.
for (let d = depthiParent; d <= depthiRoot; d++) {
if (isLeftNode(d, index)) {
// If navigated to the left, then all the child nodes of the right node are NOT part of the new tree.
// So re-bind new `node` with a zeroNode at the current depth.
node = new BranchNode(node, zeroNode(d));
} else {
// If navigated to the right, then all the child nodes of the left node are part of the new tree.
// So re-bind new `node` with the existing left node of the parent.
node = new BranchNode(parentNodeStack[d].left, node);
}
}
// Done, return new root node
return node;
}
/**
* Returns true if the `index` at `depth` is a left node, false if it is a right node.
*
* Supports index up to `Number.MAX_SAFE_INTEGER`.
* In Eth2 case the biggest tree's index is 2**40 (VALIDATOR_REGISTRY_LIMIT)
*/
function isLeftNode(depthi: number, index: number): boolean {
if (depthi > 31) {
// Javascript can only do bitwise ops with 32 bit numbers.
// Shifting left 1 by 32 wraps around and becomes 1.
// Get the high part of `index` and adjust depthi
const indexHi = (index / 2 ** 32) >>> 0;
const mask = 1 << (depthi - 32);
return (indexHi & mask) !== mask;
}
const mask = 1 << depthi;
return (index & mask) !== mask;
}
/**
* depth depthi gindexes indexes
* 0 1 1 0
* 1 0 2 3 0 1
* 2 - 4 5 6 7 0 1 2 3
*
* **Conditions**:
* - `from` and `to` must not be equal
*
* @param from Index
* @param to Index
*/
function findDiffDepthi(from: number, to: number): number {
return (
// (0,0) -> 0 | (0,1) -> 1 | (0,2) -> 2
Math.ceil(Math.log2(-~(from ^ to))) -
// Must offset by one to match the depthi scale
1
);
}