-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
150 lines (131 loc) · 6 KB
/
index.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
// IMPORTS
// ================================================================================================
import { AirModule, StarkLimits, AirModuleOptions, ComponentAnalysisResult } from '@guildofweavers/air-assembly';
import * as fs from 'fs';
import { AirSchema } from './lib/AirSchema';
import { AirComponent } from './lib/AirComponent';
import { lexer } from './lib/lexer';
import { parser } from './lib/parser';
import { instantiateModule } from './lib/jsGenerator';
import { analyzeProcedure } from './lib/analysis';
import { AssemblyError } from './lib/errors';
import { getCompositionFactor, isPowerOf2, sha256prng, validate } from './lib/utils';
// MODULE VARIABLES
// ================================================================================================
const DEFAULT_LIMITS: StarkLimits = {
maxTraceLength : 2**20,
maxTraceRegisters : 64,
maxStaticRegisters : 64,
maxConstraintCount : 1024,
maxConstraintDegree : 16
};
// RE-EXPORTS
// ================================================================================================
export { AirSchema } from './lib/AirSchema';
export { PrngSequence, PowerSequence} from './lib/registers';
export { AssemblyError } from './lib/errors';
export { ExpressionVisitor } from './lib/expressions/ExpressionVisitor';
export const prng = {
sha256 : sha256prng
};
// PUBLIC FUNCTIONS
// ================================================================================================
export function compile(sourceOrPath: Buffer | string, limits?: Partial<StarkLimits>): AirSchema {
let source: string;
if (Buffer.isBuffer(sourceOrPath)) {
source = sourceOrPath.toString('utf8');
}
else {
if (typeof sourceOrPath !== 'string')
throw new TypeError(`source path '${sourceOrPath}' is invalid`);
try {
source = fs.readFileSync(sourceOrPath, { encoding: 'utf8' });
}
catch (error) {
throw new AssemblyError([error]);
}
}
// tokenize input
const lexResult = lexer.tokenize(source);
if(lexResult.errors.length > 0) {
throw new AssemblyError(lexResult.errors);
}
// parse the tokens
parser.input = lexResult.tokens;
const schema = parser.module();
if (parser.errors.length > 0) {
throw new AssemblyError(parser.errors);
}
// if limits are specified, validate the schema against them
if (limits !== undefined) {
validateLimits(schema, { ...DEFAULT_LIMITS, ...limits });
}
return schema;
}
export function instantiate(schema: AirSchema, options?: Partial<AirModuleOptions>): AirModule;
export function instantiate(schema: AirSchema, component: string, options?: Partial<AirModuleOptions>): AirModule;
export function instantiate(schema: AirSchema, componentOrOptions?: string | Partial<AirModuleOptions>, options?: Partial<AirModuleOptions>): AirModule {
let component: AirComponent;
if (typeof componentOrOptions === 'string') {
component = schema.components.get(componentOrOptions)!;
validate(component, errors.componentNotFound(componentOrOptions));
options = options || {};
}
else {
component = schema.components.get('default')!;
validate(component, errors.noDefaultComponent());
options = componentOrOptions || {};
}
const compositionFactor = getCompositionFactor(component);
const vOptions = validateModuleOptions(options, compositionFactor);
validateLimits(schema, vOptions.limits as StarkLimits);
const module = instantiateModule(component, vOptions);
return module;
}
export function analyze(schema: AirSchema, componentName: string): ComponentAnalysisResult {
const component = schema.components.get(componentName)!;
validate(component, errors.componentNotFound(componentName));
const transition = analyzeProcedure(component.transitionFunction);
const evaluation = analyzeProcedure(component.constraintEvaluator);
return { transition, evaluation };
}
// HELPER FUNCTIONS
// ================================================================================================
function validateModuleOptions(options: Partial<AirModuleOptions>, compositionFactor: number): AirModuleOptions {
const minExtensionFactor = compositionFactor * 2;
const extensionFactor = options.extensionFactor || minExtensionFactor;
if (extensionFactor < minExtensionFactor) {
throw new Error(`extension factor cannot be smaller than ${minExtensionFactor}`);
}
else if (!isPowerOf2(extensionFactor)) {
throw new Error(`extension factor ${extensionFactor} is not a power of 2`)
}
return {
limits : { ...DEFAULT_LIMITS, ...options.limits },
wasmOptions : options.wasmOptions || false,
extensionFactor : extensionFactor
};
}
function validateLimits(schema: AirSchema, limits: StarkLimits): void {
try {
schema.components.forEach(component => {
if (component.traceRegisterCount > limits.maxTraceRegisters)
throw new Error(`number of state registers cannot exceed ${limits.maxTraceRegisters}`);
else if (component.staticRegisterCount > limits.maxStaticRegisters)
throw new Error(`number of static registers cannot exceed ${limits.maxStaticRegisters}`);
else if (component.constraintCount > limits.maxConstraintCount)
throw new Error(`number of transition constraints cannot exceed ${limits.maxConstraintCount}`);
else if (component.maxConstraintDegree > limits.maxConstraintDegree)
throw new Error(`max constraint degree cannot exceed ${limits.maxConstraintDegree}`);
});
}
catch (error) {
throw new AssemblyError([error]);
}
}
// ERRORS
// ================================================================================================
const errors = {
componentNotFound : (n: any) => `component with name '${n}' does not exist in the provided schema`,
noDefaultComponent : () => `provided schema does not contain a default component export`
};