/
typeToIndex.ts
188 lines (175 loc) Β· 6.17 KB
/
typeToIndex.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
import assert from "assert";
import {
FullSSZType,
isBasicType,
Type,
Path,
PathElement,
} from "@chainsafe/ssz-type-schema";
import {
nextPowerOf2, bitLength,
} from "../../util/bigintMath";
import {itemLength, chunkCount} from "../../util/chunk";
import {bit, byte} from "../../util/types";
import {GeneralizedIndex} from "./types";
import {alphasort} from "./alphasort";
import { getPathIndices } from "./multiproof";
/**
* Return the type of the element of an object of the given type with the given index
* or member variable name (eg. `7` for `x[7]`, `"foo"` for `x.foo`)
*/
export function getElementType(type: FullSSZType, indexOrFieldName: PathElement): FullSSZType {
switch (type.type) {
case Type.bitList:
case Type.bitVector:
return bit;
case Type.byteList:
case Type.byteVector:
return byte;
case Type.list:
case Type.vector:
return type.elementType;
case Type.container:
return type.fields.find(([fieldName]) => indexOrFieldName === fieldName)[1];
default:
throw new Error("unsupported type");
}
}
/**
* Return three variables:
* (i) the index of the chunk in which the given element of the item is represented;
* (ii) the starting byte position within the chunk;
* (iii) the ending byte position within the chunk.
* For example: for a 6-item list of uint64 values, index=2 will return (0, 16, 24), index=5 will return (1, 8, 16)
*/
export function getItemPosition(type: FullSSZType, indexOrFieldName: PathElement): [number, number, number] {
let start: number,
elementItemLength: number;
switch (type.type) {
case Type.bitList:
case Type.bitVector:
case Type.byteList:
case Type.byteVector:
case Type.list:
case Type.vector:
indexOrFieldName = indexOrFieldName as number;
assert(Number.isSafeInteger(indexOrFieldName));
elementItemLength = itemLength(getElementType(type, indexOrFieldName));
start = indexOrFieldName * elementItemLength;
return [Math.floor(start / 32), start % 32, start % 32 + elementItemLength];
case Type.container:
indexOrFieldName = indexOrFieldName as string;
assert(typeof indexOrFieldName === "string");
return [
type.fields.map(([fieldName]) => fieldName).indexOf(indexOrFieldName),
0,
itemLength(getElementType(type, indexOrFieldName)),
];
default:
throw new Error("unsupported type");
}
}
function isListType(type: FullSSZType): boolean {
return [
Type.bitList,
Type.byteList,
Type.list
].includes(type.type);
}
/**
* Converts a path (eg. `[7, "foo", 3]` for `x[7].foo[3]`, `[12, "bar", "__len__"]` for
* `len(x[12].bar)`) into the generalized index representing its position in the Merkle tree.
*/
export function getGeneralizedIndex(type: FullSSZType, path: Path): GeneralizedIndex {
let root = 1n;
for (const p of path) {
assert(!isBasicType(type));
if (p === "__len__") {
assert(isListType(type));
return root * 2n + 1n;
} else {
if (isListType(type)) {
root *= 2n; // bit for length mix in
}
const [pos] = getItemPosition(type, p);
root = root * nextPowerOf2(BigInt(chunkCount(type))) + BigInt(pos);
type = getElementType(type, p);
}
}
return root;
}
/**
* Converts paths into generalized indices, representing positions in a merkle tree
* sorted in bit-alphabetical left-to-right order
*/
export function getGeneralizedIndices(type: FullSSZType, paths: Path[]): GeneralizedIndex[] {
return alphasort(new Set(paths.map((p) => getGeneralizedIndex(type, p))));
}
export function getPaths(type: FullSSZType, index: GeneralizedIndex): Path[] {
console.log(getPathIndices(index));
return _getPaths(type, index, getPathIndices(index), 1n, []);
}
import {BitsType, BytesType, ArrayType} from "@chainsafe/ssz-type-schema";
function maxLength(type: BitsType | BytesType | ArrayType): number {
switch (type.type) {
case Type.bitList:
case Type.byteList:
case Type.list:
return type.maxLength;
case Type.bitVector:
case Type.byteVector:
case Type.vector:
return type.length;
default:
throw new Error("");
}
}
function _getPaths(type: FullSSZType, index: GeneralizedIndex, pathIndices: GeneralizedIndex[], currentIndex: GeneralizedIndex, currentPath: Path): Path[] {
assert(!isBasicType(type));
const prevIndex = currentIndex;
const fullChunkCount = nextPowerOf2(BigInt(chunkCount(type)));
if (type.type === Type.container) {
currentIndex *= fullChunkCount;
for (const [fieldName, fieldType] of type.fields) {
const p = [...currentPath, fieldName];
const fieldIndex = currentIndex + BigInt(getItemPosition(type, fieldName)[0]);
if (fieldIndex === index) {
return [p];
}
if (pathIndices.includes(fieldIndex)) {
return _getPaths(fieldType, index, pathIndices, fieldIndex, p);
}
}
throw new Error("no valid paths");
} else {
let pathIndexDelta = bitLength(fullChunkCount - 1n);
if (isListType(type)) {
currentIndex = currentIndex * 2n;
pathIndexDelta += 1;
if (currentIndex + 1n === index) {
return [[...currentPath, "__len__"]];
}
}
currentIndex *= fullChunkCount;
const prevPathIndex = pathIndices.indexOf(prevIndex);
const currentPathIndex = pathIndices[prevPathIndex - pathIndexDelta];
const maxIndex = currentIndex + BigInt(getItemPosition(type, maxLength(type as ArrayType))[0]);
if (maxIndex < currentPathIndex) {
throw new Error("no valid paths");
}
const firstMatchedIndex = Number(currentPathIndex - currentIndex);
const elementType = getElementType(type, firstMatchedIndex);
const elementItemLength = itemLength(elementType);
if (isBasicType(elementType)) {
return Array.from({length: Math.floor(32 / elementItemLength)}, (_, i) => firstMatchedIndex + i)
.map((i) => ([...currentPath, i]));
} else {
const elementIndex = currentIndex + BigInt(getItemPosition(type, firstMatchedIndex)[0]);
const path = [...currentPath, firstMatchedIndex];
if (elementIndex === index) {
return [path];
}
return _getPaths(elementType, index, pathIndices, elementIndex, path);
}
}
}