-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathfeature-flags.ts
189 lines (171 loc) · 5.77 KB
/
feature-flags.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
import { MlFeatureFlags, Gate, GateType } from '../../snarky.js';
import { MlBool, MlOption, MlArrayOptionalElements } from '../ml/base.js';
import type { analyzeMethod } from './zkprogram.js';
// public API
export { FeatureFlags };
// internal API
export { featureFlagsToMlOption, featureFlagsFromGates };
type AnalysableProgram = {
analyzeMethods: () => Promise<{
[I in keyof any]: Awaited<ReturnType<typeof analyzeMethod>>;
}>;
};
type FeatureFlags = {
rangeCheck0: boolean | undefined;
rangeCheck1: boolean | undefined;
foreignFieldAdd: boolean | undefined;
foreignFieldMul: boolean | undefined;
xor: boolean | undefined;
rot: boolean | undefined;
lookup: boolean | undefined;
runtimeTables: boolean | undefined;
};
/**
* Feature flags indicate what custom gates are used in a proof of circuit.
* Side loading, for example, requires a set of feature flags in advance (at compile time) in order to verify and side load proofs.
* If the side loaded proofs and verification keys do not match the specified feature flag configurations, the verification will fail.
* Flags specified as `undefined` are considered as `maybe` by Pickles. This means, proofs can be sided loaded that can, but don't have to, use a specific custom gate.
* _Note:_ `Maybe` feature flags incur a proving overhead.
*/
const FeatureFlags = {
/**
* Returns a feature flag configuration where all flags are set to false.
*/
allNone: {
rangeCheck0: false,
rangeCheck1: false,
foreignFieldAdd: false,
foreignFieldMul: false,
xor: false,
rot: false,
lookup: false,
runtimeTables: false,
},
/**
* Returns a feature flag configuration where all flags are optional.
*/
allMaybe: {
rangeCheck0: undefined,
rangeCheck1: undefined,
foreignFieldAdd: undefined,
foreignFieldMul: undefined,
xor: undefined,
rot: undefined,
lookup: undefined,
runtimeTables: undefined,
},
/**
* Given a list of gates, returns the feature flag configuration that the gates use.
*/
fromGates: featureFlagsFromGates,
/**
* Given a ZkProgram, return the feature flag configuration that fits the given program.
* This function considers all methods of the specified ZkProgram and finds a configuration that fits all.
*/
fromZkProgram: async (program: AnalysableProgram) =>
await fromZkProgramList([program]),
/**
* Given a list of ZkPrograms, return the feature flag configuration that fits the given set of programs.
* This function considers all methods of all specified ZkPrograms and finds a configuration that fits all.
*/
fromZkProgramList,
};
async function fromZkProgramList(programs: Array<AnalysableProgram>) {
let flatMethodIntfs: Array<Awaited<ReturnType<typeof analyzeMethod>>> = [];
for (const program of programs) {
let methodInterface = await program.analyzeMethods();
flatMethodIntfs.push(...Object.values(methodInterface));
}
return featureFlagsfromFlatMethodIntfs(flatMethodIntfs);
}
async function featureFlagsfromFlatMethodIntfs(
methodIntfs: Array<Awaited<ReturnType<typeof analyzeMethod>>>
): Promise<FeatureFlags> {
// compute feature flags that belong to each method
let flags = methodIntfs.map(({ gates }) => {
return featureFlagsFromGates(gates);
});
if (flags.length === 0)
throw Error(
'The ZkProgram has no methods, in order to calculate feature flags, please attach a method to your ZkProgram.'
);
// initialize feature flags to all false
let globalFlags: Record<string, boolean | undefined> = {
rangeCheck0: false,
rangeCheck1: false,
foreignFieldAdd: false,
foreignFieldMul: false,
xor: false,
rot: false,
lookup: false,
runtimeTables: false,
};
// if there's only one method that means it defines the feature flags for the entire program
if (flags.length === 1) return flags[0];
// calculating the crossover between all methods, compute the shared feature flag set
flags.forEach((featureFlags, i) => {
for (const [flagType, currentFlag] of Object.entries(featureFlags)) {
if (i === 0) {
// initialize first iteration of flags freely
globalFlags[flagType] = currentFlag;
} else if (globalFlags[flagType] != currentFlag) {
// if flags conflict, set them to undefined to account for both cases (true and false) ^= maybe
// otherwise side loading couldn't verify some proofs of some method branches!
globalFlags[flagType] = undefined;
}
}
});
return globalFlags as FeatureFlags;
}
// what feature flags to set to enable certain gate types
const gateToFlag: Partial<Record<GateType, keyof FeatureFlags>> = {
RangeCheck0: 'rangeCheck0',
RangeCheck1: 'rangeCheck1',
ForeignFieldAdd: 'foreignFieldAdd',
ForeignFieldMul: 'foreignFieldMul',
Xor16: 'xor',
Rot64: 'rot',
Lookup: 'lookup',
};
function featureFlagsFromGates(gates: Gate[]): FeatureFlags {
let flags: FeatureFlags = {
rangeCheck0: false,
rangeCheck1: false,
foreignFieldAdd: false,
foreignFieldMul: false,
xor: false,
rot: false,
lookup: false,
runtimeTables: false,
};
for (let gate of gates) {
let flag = gateToFlag[gate.type];
if (flag !== undefined) flags[flag] = true;
}
return flags;
}
function featureFlagsToMlOption(
flags: FeatureFlags
): MlArrayOptionalElements<MlFeatureFlags> {
const {
rangeCheck0,
rangeCheck1,
foreignFieldAdd,
foreignFieldMul,
xor,
rot,
lookup,
runtimeTables,
} = flags;
return [
0,
MlOption.mapTo(rangeCheck0, MlBool),
MlOption.mapTo(rangeCheck1, MlBool),
MlOption.mapTo(foreignFieldAdd, MlBool),
MlOption.mapTo(foreignFieldMul, MlBool),
MlOption.mapTo(xor, MlBool),
MlOption.mapTo(rot, MlBool),
MlOption.mapTo(lookup, MlBool),
MlOption.mapTo(runtimeTables, MlBool),
];
}