-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
utils.ts
64 lines (57 loc) · 1.68 KB
/
utils.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
export const ZERO = '0x0000000000000000000000000000000000000000000000000000000000000000';
export const EMPTY = '0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
export const MAX_HEIGHT = 256;
export interface MapStore {
[key: string]: string;
}
/**
* Gets the bit at an offset from the most significant bit
*/
export function getBitAtFromMSB(data: string, position: number): number {
// if int(data[position / 8]) & (1 << (8 - 1 - uint(position) % 8)) > 0
// Slice off '0x'
const slicedData = data.slice(2);
// Get byte that contains the specified position
const byte = '0x'.concat(
slicedData.slice(Math.floor(position / 8) * 2, Math.floor(position / 8) * 2 + 2)
);
// Get bits from specified position within that byte
const bits = Number(byte) & (1 << (8 - 1 - (position % 8)));
// Bit at position = 0 IFF bits > 0.
if (bits > 0) {
return 1;
}
return 0;
}
/**
* Reverse the nodes position
*/
export function reverseSideNodes(sideNodes: string[]): string[] {
let left = 0;
let right = sideNodes.length - 1;
const reversedSideNodes: string[] = sideNodes;
while (left < right) {
[reversedSideNodes[left], reversedSideNodes[right]] = [
reversedSideNodes[right],
reversedSideNodes[left],
];
left += 1;
right -= 1;
}
return reversedSideNodes;
}
/**
* Counts the common bit at at an offset from the most significant bit
* between two inputs
*/
export function countCommonPrefix(data1: string, data2: string): number {
let count = 0;
for (let i = 0; i < MAX_HEIGHT; i += 1) {
if (getBitAtFromMSB(data1, i) === getBitAtFromMSB(data2, i)) {
count += 1;
} else {
break;
}
}
return count;
}