-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathair-assembly.d.ts
627 lines (503 loc) · 24.3 KB
/
air-assembly.d.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
declare module '@guildofweavers/air-assembly' {
// IMPORTS AND RE-EXPORTS
// --------------------------------------------------------------------------------------------
import { FiniteField, Vector, Matrix, WasmOptions } from '@guildofweavers/galois';
export { FiniteField, Vector, Matrix, WasmOptions } from '@guildofweavers/galois';
// COMMON INTERFACES
// --------------------------------------------------------------------------------------------
export interface StarkLimits {
/** Maximum number of steps in an execution trace; defaults to 2^20 */
maxTraceLength: number;
/** Maximum number of state registers; defaults to 64 */
maxTraceRegisters: number;
/** Maximum number of static registers; defaults to 64 */
maxStaticRegisters: number;
/** Maximum number of transition constraints; defaults to 1024 */
maxConstraintCount: number;
/** Highest allowed degree of transition constraints; defaults to 16 */
maxConstraintDegree: number;
}
export interface AirModuleOptions {
/** Limits to be imposed on the instantiated module. */
limits: Partial<StarkLimits>;
/**
* Options for finite fields which can take advantage of WebAssembly optimization. Can also
* be set to a boolean value to turn the optimization on or off
*/
wasmOptions: Partial<WasmOptions> | boolean;
/**
* Number by which the execution trace is to be "stretched." Must be a power of 2 at least
* 2x of the highest constraint degree. This property is optional, the default is the
* smallest power of 2 that is greater than 2x of the highest constraint degree.
*/
extensionFactor: number;
}
// PUBLIC FUNCTIONS
// --------------------------------------------------------------------------------------------
/**
* Parses and compiles AirAssembly source code into an AirSchema object
* @param path Path to the file containing AirAssembly source code
* @param limits StarkLimits object against which the schema should be validated
*/
export function compile(path: string, limits?: Partial<StarkLimits>): AirSchema;
/**
* * Parses and compiles AirAssembly source code into an AirSchema object
* @param source Buffer with AirAssembly source code (encoded in UTF8)
* @param limits StarkLimits object against which the schema should be validated
*/
export function compile(source: Buffer, limits?: Partial<StarkLimits>): AirSchema;
/**
* Creates an AirModule object for the defaulted schema component
* @param schema Schema containing the component to instantiate
* @param options Additional options for the AirModule
*/
export function instantiate(schema: AirSchema, options?: Partial<AirModuleOptions>): AirModule;
/**
* Creates an AirModule object for the specified schema component
* @param schema Schema containing the component to instantiate
* @param component Name of the component to instantiate
* @param options Additional options for the AirModule
*/
export function instantiate(schema: AirSchema, component: string, options?: Partial<AirModuleOptions>): AirModule;
/**
* Performs basic analysis of the specified schema to infer such things as degree of transition
* constraints, number of additions and multiplications needed to evaluate transition function etc.
* @param schema Schema containing the component to analyze
* @param component Name of the component to analyze
*/
export function analyze(schema: AirSchema, component: string): ComponentAnalysisResult;
// AIR SCHEMA
// --------------------------------------------------------------------------------------------
export class AirSchema {
/** A finite field defined for the module */
readonly field: FiniteField;
/** Constants defined for the module */
readonly constants: ReadonlyArray<Constant>;
/** Functions defined for the module */
readonly functions: ReadonlyArray<AirFunction>;
/** Components defined for the module exposed as a map keyed by component name */
readonly components: ReadonlyMap<string, AirComponent>;
/**
* Creates a new AirSchema object
* @param fieldType Type of the finite field
* @param fieldModulus Modules of the prime filed
*/
constructor(fieldType: 'prime', fieldModulus: bigint);
/**
* Adds a constant to the module
* @param value Value of the constant
* @param handle Optional constant handle
*/
addConstant(value: bigint | bigint[] | bigint[][], handle?: string): void;
/**
* Creates a new function context from the current state of the schema
* @param resultType Dimensions of the expected function return value
* @param handle Optional function handle
*/
createFunctionContext(resultType: Dimensions, handle?: string): FunctionContext;
/**
* Adds a function to the module
* @param context Function context, including parameters and local variables
* @param statements A list of store operations within the function body
* @param result The return expression of the function
*/
addFunction(context: FunctionContext, statements: StoreOperation[], result: Expression): void;
/**
* Creates a component for a new computation within the module
* @param name Name of the component
* @param registers Number of dynamic registers expected in the computation
* @param constraints Number of constraints expected in the computation
* @param steps Minimal cycle length possible for the computation
*/
createComponent(name: string, registers: number, constraints: number, steps: number): AirComponent;
/**
* Adds a component to the module
* @param component Component to add to the module
*/
addComponent(component: AirComponent): void;
}
export interface AirComponent {
/** Name of the computation */
readonly name: string;
/** Base cycle length of the computation */
readonly cycleLength: number;
/** Static registers defined for the computation */
readonly staticRegisters: ReadonlyArray<StaticRegister>;
/** Number of secret input registers defined for the computation */
readonly secretInputCount: number;
/** Trace initialization procedure defined for the computation */
readonly traceInitializer: AirProcedure;
/** Transition function procedure defined for the computation */
readonly transitionFunction: AirProcedure;
/** Transition constraint evaluator procedure defined for the computation. */
readonly constraintEvaluator: AirProcedure;
/** Transition constraint properties */
readonly constraints: ConstraintDescriptor[];
/** Highest degree of transition constraints defined for the computation. */
readonly maxConstraintDegree: number;
addInputRegister(scope: string, binary: boolean, master?: InputRegisterMaster, steps?: number, offset?: number): void;
addMaskRegister(sourceIdx: number, inverted: boolean): void;
addCyclicRegister(values: bigint[] | ValueSequence): void;
/**
* Creates a new procedure context from the current state of the component
* @param name Name of the procedure
*/
createProcedureContext(name: ProcedureName): ProcedureContext;
setTraceInitializer(context: ProcedureContext, statements: StoreOperation[], result: Expression): void;
setTransitionFunction(context: ProcedureContext, statements: StoreOperation[], result: Expression): void;
setConstraintEvaluator(context: ProcedureContext, statements: StoreOperation[], result: Expression): void;
}
// FUNCTIONS AND PROCEDURES
// --------------------------------------------------------------------------------------------
export interface AirFunction {
readonly handle? : string;
readonly params : ReadonlyArray<Parameter>;
readonly locals : ReadonlyArray<LocalVariable>;
readonly statements : ReadonlyArray<StoreOperation>;
readonly result : Expression;
}
export type ProcedureName = 'init' | 'transition' | 'evaluation';
export interface AirProcedure {
readonly name : ProcedureName;
readonly params : ReadonlyArray<Parameter>
readonly locals : ReadonlyArray<LocalVariable>;
readonly statements : ReadonlyArray<StoreOperation>;
readonly result : Expression;
}
export interface Constant {
readonly dimensions : Dimensions;
readonly value : LiteralValue;
readonly handle? : string;
}
export interface Parameter {
readonly dimensions : Dimensions;
readonly handle? : string;
}
export interface LocalVariable {
readonly dimensions : Dimensions;
readonly handle? : string;
}
export interface StoreOperation {
readonly target : number;
readonly handle? : string;
readonly expression : Expression;
readonly dimensions : Dimensions;
}
export interface ExecutionContext {
readonly field : FiniteField;
readonly constants : ReadonlyArray<Constant>;
readonly functions : ReadonlyArray<AirFunction>;
readonly params : ReadonlyArray<Parameter>;
readonly locals : ReadonlyArray<LocalVariable>;
addParam(dimensions: Dimensions, handle?: string): void;
addLocal(dimensions: Dimensions, handle?: string): void;
buildLiteralValue(value: bigint | bigint[] | bigint[]): LiteralValue;
buildBinaryOperation(operation: string, lhs: Expression, rhs: Expression): BinaryOperation;
buildUnaryOperation(operation: string, operand: Expression): UnaryOperation;
buildMakeVectorExpression(elements: Expression[]): MakeVector;
buildGetVectorElementExpression(source: Expression, index: number): GetVectorElement;
buildSliceVectorExpression(source: Expression, start: number, end: number): SliceVector;
buildMakeMatrixExpression(elements: Expression[][]): MakeMatrix;
buildLoadExpression(operation: string, indexOrHandle: number | string): LoadExpression;
buildStoreOperation(indexOrHandle: number | string, value: Expression): StoreOperation;
buildCallExpression(indexOrHandle: number | string, params: Expression[]): CallExpression;
}
export interface FunctionContext extends ExecutionContext{
readonly result : Dimensions;
readonly handle? : string;
}
export interface ProcedureContext extends ExecutionContext {
readonly name : ProcedureName;
readonly traceRegisters : any;
readonly staticRegisters: any;
readonly width : number;
}
// STATIC REGISTERS
// --------------------------------------------------------------------------------------------
export interface StaticRegister {}
export interface InputRegister extends StaticRegister {
readonly secret : boolean;
readonly rank : number;
readonly binary : boolean;
readonly offset : number;
readonly master? : InputRegisterMaster;
readonly steps? : number;
}
export type InputRegisterRelation = 'childof' | 'peerof';
export interface InputRegisterMaster {
readonly index : number;
readonly relation : InputRegisterRelation;
}
export interface MaskRegister extends StaticRegister {
readonly source : number;
readonly inverted : boolean;
}
export interface CyclicRegister extends StaticRegister {
readonly cycleLength: number;
getValues(): bigint[];
readonly values : bigint[] | ValueSequence; // TODO: replace with something else?
}
export interface ValueSequence {
readonly length : number;
getValues(field: FiniteField): bigint[];
}
export class PrngSequence implements ValueSequence {
readonly method : 'sha256';
readonly seed : Buffer;
readonly length : number;
constructor(method: string, seed: bigint, count: number);
getValues(field: FiniteField): bigint[];
}
export class PowerSequence implements ValueSequence {
readonly base : bigint;
readonly length : number;
constructor(base: bigint, count: number);
getValues(field: FiniteField): bigint[];
}
// EXPRESSIONS
// --------------------------------------------------------------------------------------------
export type Dimensions = [number, number];
export type Degree = bigint | bigint[] | bigint[][];
export type LoadSource = 'const' | 'trace' | 'static' | 'param' | 'local'; // TODO: rename?
export type BinaryOperationType = 'add' | 'sub' | 'mul' | 'div' | 'exp' | 'prod';
export type UnaryOperationType = 'neg' | 'inv';
export interface Expression {
readonly dimensions : Dimensions;
readonly children : Expression[];
readonly isScalar : boolean;
readonly isVector : boolean;
readonly isMatrix : boolean;
readonly isStatic : boolean;
}
export interface LiteralValue extends Expression {
readonly value : bigint | bigint[] | bigint[][];
}
export interface BinaryOperation extends Expression {
readonly operation : BinaryOperationType;
readonly lhs : Expression;
readonly rhs : Expression;
}
export interface UnaryOperation extends Expression {
readonly operation : UnaryOperationType;
readonly operand : Expression;
}
export interface MakeVector extends Expression {
readonly elements : Expression[];
}
export interface GetVectorElement extends Expression {
readonly source : Expression;
readonly index : number;
}
export interface SliceVector extends Expression {
readonly source : Expression;
readonly start : number;
readonly end : number;
}
export interface MakeMatrix extends Expression {
readonly elements : Expression[][];
}
export interface LoadExpression extends Expression {
readonly source : LoadSource;
readonly index : number;
}
export interface CallExpression extends Expression {
readonly func : AirFunction;
readonly index : number;
readonly params : Expression[];
}
export class ExpressionVisitor<T> {
constructor();
visit(expression: Expression, options?: any): T;
}
// ANALYSIS
// --------------------------------------------------------------------------------------------
export interface ProcedureAnalysisResult {
readonly results: {
readonly degree : bigint;
readonly traceRefs : number[];
readonly staticRefs : number[];
}[];
readonly operations : {
readonly add : number;
readonly mul : number;
readonly inv : number;
};
}
export interface ComponentAnalysisResult {
readonly transition : ProcedureAnalysisResult;
readonly evaluation : ProcedureAnalysisResult;
}
// AIR MODULE
// --------------------------------------------------------------------------------------------
export interface AirModule {
/** A finite field object used for all arithmetic operations of the computation. */
readonly field: FiniteField;
/** Number of state registers in the execution trace. */
readonly traceRegisterCount: number;
/** Number of static registers in the execution trace. */
readonly staticRegisterCount: number;
/** An array of input descriptors with metadata for inputs required by the computation. */
readonly inputDescriptors: InputDescriptor[];
/** Number of secret input registers defined for the computation */
readonly secretInputCount: number;
/** An array of constraint descriptors with metadata for the defined transition constraints */
readonly constraints: ConstraintDescriptor[];
/** The highest degree of transition constraints defined for the computation. */
readonly maxConstraintDegree: number;
/** An integer value specifying how much the execution trace is to be "stretched." */
readonly extensionFactor: number;
/**
* Instantiates a Prover object for a specific instance of the computation.
* @param inputs Values for initializing input registers. Must be provided only if the
* computation contains input registers. In such a case, the shape of input objects must
* be in line with the shapes specified by the computation's input descriptors.
* @param seed Must be provided only if the seed vector is used in the main export expression.
*/
initProvingContext(inputs?: any[], seed?: bigint[]): ProvingContext;
/**
* Instantiates a Verifier object for a specific instance of the computation.
* @param inputShapes Shapes of input registers consumed by the computation. Must be provided
* only if the computation contains input registers.
* @param publicInputs Values consumed by input registers. Must be provided only if the
* computation contains public input registers.
*/
initVerificationContext(inputShapes?: InputShape[], publicInputs?: any[]): VerificationContext;
}
export interface InputDescriptor {
/** An integer value indicating the position of the register in the input dependency tree. */
readonly rank: number;
/** A boolean value indicating wither the inputs for the register are public or secret. */
readonly secret: boolean;
/** A boolean value indicating whether the register can accept only binary values (ones and zeros). */
readonly binary: boolean;
/**
* A signed integer value specifying the number of steps by which an input value is to be
* shifted in the execution trace.
*/
readonly offset: number;
/**
* An integer value specifying an index of the parent input register. If the register has
* no parents, this property will be undefined.
*/
readonly parent?: number;
/**
* An integer value specifying the number of steps by which a register trace is to be
* expanded for each input value. For non-leaf registers, this property will be undefined.
*/
readonly steps?: number;
}
/**
* Describes shape of an input register. Each value in the array corresponds to the number of
* input values provided at that level of the input hierarchy.
*/
export type InputShape = number[];
export interface ConstraintDescriptor {
readonly degree : number;
readonly traceRefs : number[];
readonly staticRefs : number[];
}
// CONTEXTS
// --------------------------------------------------------------------------------------------
export interface AirContext {
/** Reference to the finite field object of the AirModule which describes the computation. */
readonly field: FiniteField;
/** Primitive root of unity of the evaluation domain for the instance of the computation. */
readonly rootOfUnity: bigint;
/** Length of the execution trace for the instance of the computation. */
readonly traceLength: number;
/** Extension factor of the execution trace. */
readonly extensionFactor: number;
/** An array of constraint descriptors with metadata for the defined transition constraints */
readonly constraints: ConstraintDescriptor[];
/** Shapes of all input registers for the instance of the computation. */
readonly inputShapes: InputShape[];
}
export interface VerificationContext extends AirContext {
/**
* Evaluates transition constraints at the specified point.
* @param x Point in the evaluation domain at which to evaluate constraints
* @param rValues Values of trace registers at the current step
* @param nValues Values of trace registers at the next step
* @param sValues Values of secret registers at the current step
*/
evaluateConstraintsAt(x: bigint, rValues: bigint[], nValues: bigint[], sValues: bigint[]): bigint[];
}
export interface ProvingContext extends AirContext {
/** Domain of the execution trace */
readonly executionDomain: Vector;
/** Domain of the low-degree extended execution trace */
readonly evaluationDomain: Vector;
/** Domain of the low-degree extended composition polynomial */
readonly compositionDomain: Vector;
/** Values of secret registers evaluated over evaluation domain */
readonly secretRegisterTraces: Vector[];
/**
* Generates execution trace for the computation. The trace is returned as a Matrix object where
* rows correspond to the dynamic register traces, and columns correspond to computation steps.
*/
generateExecutionTrace(): Matrix;
/**
* Generates traces of static registers for the computation. The trace is returned as a Matrix
* object where rows correspond to the dynamic register traces, and columns correspond to
* computation steps.
*/
generateStaticTrace(): Matrix;
/**
* Evaluates transition constraints for a computation. The evaluations are returned as a matrix
* object where each row represents a transition constraint evaluated over the composition domain.
* @param tracePolys A matrix where each row represents a polynomial interpolated from a
* corresponding register of the execution trace
*/
evaluateTransitionConstraints(tracePolys: Matrix): Matrix;
}
// PRNG
// --------------------------------------------------------------------------------------------
export type PrngFunction = (seed: Buffer, count: number, field: FiniteField) => bigint[];
export const prng: {
sha256: PrngFunction;
};
// ERRORS
// --------------------------------------------------------------------------------------------
export class AssemblyError {
readonly errors: any[];
constructor(errors: any[]);
}
// INTERNAL
// --------------------------------------------------------------------------------------------
export interface TraceInitializer {
/**
* @param k Array with values of static registers at last step
* @param p0 Vector with trace seed values
* @returns Array with values of execution trace at step 0
*/
(k: bigint[], p0: Vector): bigint[];
}
export interface TransitionFunction {
/**
* @param r Array with values of trace registers at the current step
* @param k Array with values of static registers at the current step
* @returns Array with values of trace registers for the next step
*/
(r: bigint[], k: bigint[]): bigint[];
}
export interface ConstraintEvaluator {
/**
* @param r Array with values of trace registers at the current step
* @param n Array with values of trace registers at the next step
* @param k Array with values of static registers at the current step
* @returns Array with values of constraint evaluated at the current step
*/
(r: bigint[], n: bigint[], k: bigint[]): bigint[];
}
export interface RegisterEvaluatorSpecs {
readonly values : bigint[];
readonly cyclic : boolean;
readonly secret? : boolean;
readonly invert? : boolean;
readonly rotate? : number;
}
export interface MaskRegisterDescriptor {
readonly source : number;
readonly inverted : boolean;
}
}