/
snarky.d.ts
793 lines (679 loc) · 20.2 KB
/
snarky.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
export {
Field,
Bool,
Group,
Scalar,
EndoScalar,
AsFieldElements,
Circuit,
CircuitMain,
Poseidon,
VerificationKey,
Keypair,
Proof,
Ledger,
isReady,
shutdown,
Pickles,
JSONValue,
};
export * as Types from './snarky/gen/parties';
export { jsLayout } from './snarky/gen/js-layout';
/**
* An element of a finite field.
*/
declare function Field(x: Field | number | string | boolean | bigint): Field;
declare class Field {
/**
* Coerces anything field-like to a [[`Field`]].
*/
constructor(x: Field | number | string | boolean | bigint);
/**
* Negates this [[`Field`]]. This is equivalent to multiplying the [[`Field`]]
* by -1.
*
* ```typescript
* const negOne = Field.one.neg();
* negOne.assertEquals(-1);
* ```
*/
neg(): Field;
/**
* Inverts this [[`Field`]] element.
*
* ```typescript
* const invX = x.inv();
* invX.assertEquals(Field.one.div(x));
* ```
*
* @return A field element that is equivalent to one divided by this element.
*/
inv(): Field;
/**
* Adds this [[`Field`]] element to another coercible to a field.
*/
add(y: Field | number | string | boolean): Field;
/**
* Subtracts another [[`Field`]]-like element from this one.
*/
sub(y: Field | number | string | boolean): Field;
/**
* Multiplies this [[`Field`]] element with another coercible to a field.
*/
mul(y: Field | number | string | boolean): Field;
/**
* Divides this [[`Field`]] element through another coercible to a field.
*/
div(y: Field | number | string | boolean): Field;
/**
* Squares this [[`Field`]] element.
*
* ```typescript
* const x2 = x.square();
* x2.assertEquals(x.mul(x));
* ```
*/
square(): Field;
/**
* Square roots this [[`Field`]] element.
*
* ```typescript
* x.square().sqrt().assertEquals(x);
* ```
*/
sqrt(): Field;
/**
* Serialize the [[`Field`]] to a string, e.g. for printing.
* This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.
*/
toString(): string;
/**
* Serialize the [[`Field`]] to a bigint.
* This operation does NOT affect the circuit and can't be used to prove anything about the bigint representation of the Field.
*/
toBigInt(): bigint;
/**
* Serialize the [[`Field`]] to a JSON string.
* This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.
*/
toJSON(): JSONValue;
// TODO: Rename to size()
sizeInFields(): number;
// TODO: Rename to toFields()
toFields(): Field[];
// TODO: Make these long form version
/**
* Check if this [[`Field`]] is lower than another Field-like value.
* Returns a [[`Bool`]].
*
* ```ts
* Field(2).lt(3); // Bool(true)
* ```
*/
lt(y: Field | number | string | boolean): Bool;
/**
* Check if this [[`Field`]] is lower than or equal to another Field-like value.
* Returns a [[`Bool`]].
*
* ```ts
* Field(2).lte(3); // Bool(true)
* ```
*/
lte(y: Field | number | string | boolean): Bool;
/**
* Check if this [[`Field`]] is greater than another Field-like value.
* Returns a [[`Bool`]].
*
* ```ts
* Field(2).gt(1); // Bool(true)
* ```
*/
gt(y: Field | number | string | boolean): Bool;
/**
* Check if this [[`Field`]] is greater than or equal to another Field-like value.
* Returns a [[`Bool`]].
*
* ```ts
* Field(2).gte(1); // Bool(true)
* ```
*/
gte(y: Field | number | string | boolean): Bool;
// TODO: Make these long form version
/**
* Assert that this [[`Field`]] is lower than another Field-like value.
*
* ```ts
* Field.one.assertLt(2);
* ```
*
* This function can only be called inside a checked computation, like a
* SmartContract method, and causes it to fail if the assertion fails.
*/
assertLt(y: Field | number | string | boolean): void;
/**
* Assert that this [[`Field`]] is lower than or equal to another Field-like value.
*
* ```ts
* Field.one.assertLte(2);
* ```
*
* This function can only be called inside a checked computation, like a
* SmartContract method, and causes it to fail if the assertion fails.
*/
assertLte(y: Field | number | string | boolean): void;
/**
* Assert that this [[`Field`]] is greater than another Field-like value.
*
* ```ts
* Field.one.assertGt(0);
* ```
*
* This function can only be called inside a checked computation, like a
* SmartContract method, and causes it to fail if the assertion fails.
*/
assertGt(y: Field | number | string | boolean): void;
/**
* Assert that this [[`Field`]] is greater than or equal to another Field-like value.
*
* ```ts
* Field.one.assertGte(0);
* ```
*
* This function can only be called inside a checked computation, like a
* SmartContract method, and causes it to fail if the assertion fails.
*/
assertGte(y: Field | number | string | boolean): void;
/**
* Assert that this [[`Field`]] equals another Field-like value.
* Throws an error if the assertion fails.
*
* ```ts
* Field.one.assertEquals(1);
* ```
*/
assertEquals(y: Field | number | string | boolean): void;
/**
* Assert that this [[`Field`]] is either 0 or 1.
*
* ```ts
* Field.zero.assertBoolean();
* ```
*
* This function can only be called inside a checked computation, like a
* SmartContract method, and throws an error if the assertion fails.
*/
assertBoolean(): void;
isZero(): Bool;
/**
* Little endian binary representation of the field element.
*/
toBits(): Bool[];
/**
* Little endian binary representation of the field element.
* Fails if the field element cannot fit in `length` bits.
*/
toBits(length: number): Bool[];
/**
* Check if this [[`Field`]] equals another [[`Field`]]-like value.
* Returns a [[`Bool`]].
*
* ```ts
* Field(2).equals(2); // Bool(true)
* ```
*/
equals(y: Field | number | string | boolean): Bool;
// TODO: Izzy to document
seal(): Field;
// TODO: Izzy to document
rangeCheckHelper(numBits: number): Field;
isConstant(): boolean;
toConstant(): Field;
// value(this: Field | number | string | boolean): Field;
/* Self members */
/**
* The number 1 as a [[`Field`]].
*/
static one: Field;
/**
* The number 0 as a [[`Field`]].
*/
static zero: Field;
/**
* The number -1 as a [[`Field`]].
*/
static minusOne: Field;
/**
* The field order as a `bigint`.
*/
static ORDER: bigint;
/**
* A random field element.
*/
static random(): Field;
/*
static neg(x: Field | number | string | boolean): Field;
static inv(x: Field | number | string | boolean): Field;
static add(
x: Field | number | string | boolean,
y: Field | number | string | boolean
): Field;
static sub(
x: Field | number | string | boolean,
y: Field | number | string | boolean
): Field;
static mul(
x: Field | number | string | boolean,
y: Field | number | string | boolean
): Field;
static div(
x: Field | number | string | boolean,
y: Field | number | string | boolean
): Field;
static square(x: Field | number | string | boolean): Field;
static sqrt(x: Field | number | string | boolean): Field;
static toString(x: Field | number | string | boolean): string;
*/
// TODO: Ask izzy/matthew why we need this non-static version?
ofFields(fields: Field[]): Field;
// TODO: Rename to fromFields(fields: Field[])
// TODO: (bkase) Refactor AsFieldElements to not need these redundant static things
static ofFields(fields: Field[]): Field;
// TODO: Rename to size()
static sizeInFields(): number;
// TODO: Rename to toFields
static toFields(x: Field): Field[];
/*
static assertEqual(
x: Field | number | string | boolean,
y: Field | number | string | boolean
): Field;
static assertBoolean(x: Field | number | string | boolean): void;
static isZero(x: Field | number | string | boolean): Bool;
*/
/**
* Converts a bit array into a field element (little endian)
* Fails if the field element cannot fit given too many bits.
*
* TODO: Rename to fromBits
*/
static ofBits(x: (Bool | boolean)[]): Field;
/*
static toBits(x: Field | number | string | boolean): Bool[];
*/
/*
static equal(
x: Field | number | string | boolean,
y: Field | number | string | boolean
): Bool;
*/
// static toJSON(x: Field): JSONValue;
static fromJSON(x: JSONValue): Field | null;
static fromString(x: string): Field;
static fromNumber(x: number): Field;
static fromBigInt(x: bigint): Field;
}
/**
* A boolean value. You can use it like this:
*
* ```
* const x = new Bool(true);
* ```
*
* You can also combine multiple booleans via [[`not`]], [[`and`]], [[`or`]].
*
* Use [[assertEquals]] to enforce the value of a Bool.
*/
declare function Bool(x: Bool | boolean): Bool;
declare class Bool {
constructor(x: Bool | boolean);
/**
* Converts a [[`Bool`]] to a [[`Field`]]. `false` becomes 0 and `true` becomes 1.
*/
toField(): Field;
/**
* @returns a new [[`Bool`]] that is the negation of this [[`Bool`]].
*/
not(): Bool;
/**
* @param y A [[`Bool`]] to AND with this [[`Bool`]].
* @returns a new [[`Bool`]] that is set to true only if
* this [[`Bool`]] and `y` are also true.
*/
and(y: Bool | boolean): Bool;
/**
* @param y a [[`Bool`]] to OR with this [[`Bool`]].
* @returns a new [[`Bool`]] that is set to true if either
* this [[`Bool`]] or `y` is true.
*/
or(y: Bool | boolean): Bool;
/**
* Proves that this [[`Bool`]] is equal to `y`.
* @param y a [[`Bool`]].
*/
assertEquals(y: Bool | boolean): void;
/**
* Proves that this [[`Bool`]] is `true`.
*/
assertTrue(): void;
/**
* Proves that this [[`Bool`]] is `false`.
*/
assertFalse(): void;
/**
* Returns true if this [[`Bool`]] is equal to `y`.
* @param y a [[`Bool`]].
*/
equals(y: Bool | boolean): Bool;
sizeInFields(): number;
toFields(): Field[];
/**
* Serialize the [[`Bool`]] to a string, e.g. for printing.
* This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.
*/
toString(): string;
/**
* Serialize the [[`Bool`]] to a JSON string.
* This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field.
*/
toJSON(): JSONValue;
/**
* This converts the [[`Bool`]] to a javascript [[boolean]].
* This can only be called on non-witness values.
*/
toBoolean(): boolean;
/* static members */
/**
* The constant [[`Bool`]] that is `true`.
*/
static true: Bool;
/**
* The constant [[`Bool`]] that is `false`.
*/
static false: Bool;
static toField(x: Bool | boolean): Field;
static Unsafe: {
/**
* Converts a [[`Field`]] into a [[`Bool`]]. This is a **dangerous** operation
* as it assumes that the field element is either 1 or 0
* (which might not be true).
* @param x a [[`Field`]]
*/
ofField(x: Field | number | string | boolean): Bool;
};
static not(x: Bool | boolean): Bool;
static and(x: Bool | boolean, y: Bool | boolean): Bool;
static or(x: Bool | boolean, y: Bool | boolean): Bool;
static assertEqual(x: Bool | boolean, y: Bool | boolean): void;
static equal(x: Bool | boolean, y: Bool | boolean): Bool;
static count(x: Bool | boolean[]): Field;
static sizeInFields(): number;
static toFields(x: Bool | boolean): Field[];
static ofFields(fields: Field[]): Bool;
static toJSON(x: Bool): JSONValue;
static fromJSON(x: JSONValue): Bool | null;
}
declare interface AsFieldElements<T> {
toFields(x: T): Field[];
ofFields(x: Field[]): T;
sizeInFields(): number;
check?: (x: T) => void;
}
declare interface CircuitMain<W, P> {
snarkyWitnessTyp: AsFieldElements<W>;
snarkyPublicTyp: AsFieldElements<P>;
snarkyMain: (w: W, p: P) => void;
}
declare class Circuit {
static addConstraint(
this: Circuit,
kind: 'multiply',
x: Field,
y: Field,
z: Field
): void;
static addConstraint(
this: Circuit,
kind: 'add',
x: Field,
y: Field,
z: Field
): void;
static addConstraint(
this: Circuit,
kind: 'equal',
x: Field,
y: Field,
z: Field
): void;
static addConstraint(
this: Circuit,
kind: 'boolean',
x: Field,
y: Field,
z: Field
): void;
static newVariable(f: () => Field | number | string | boolean): Field;
// this convoluted generic typing is needed to give type inference enough flexibility
static witness<T, S extends AsFieldElements<T> = AsFieldElements<T>>(
ctor: S,
f: () => T
): T;
static asProver(f: () => void): void;
static runAndCheck<T>(f: () => T): T;
static array<T>(
ctor: AsFieldElements<T>,
length: number
): AsFieldElements<T[]>;
static assertEqual<T>(ctor: { toFields(x: T): Field[] }, x: T, y: T): void;
static assertEqual<T>(x: T, y: T): void;
static equal<T>(ctor: { toFields(x: T): Field[] }, x: T, y: T): Bool;
static equal<T>(x: T, y: T): Bool;
static if<T>(b: Bool | boolean, ctor: AsFieldElements<T>, x: T, y: T): T;
static if<T>(b: Bool | boolean, x: T, y: T): T;
static generateKeypair(): Keypair;
static prove(privateInput: any[], publicInput: any[], kp: Keypair): Proof;
static verify(publicInput: any[], vk: VerificationKey, pi: Proof): boolean;
static toFields<A>(a: A): Field[];
static inProver(): boolean;
static inCheckedComputation(): boolean;
}
declare class Scalar {
toFields(this: Scalar): Field[];
/**
* Negate a scalar field element.
* Can only be called outside of circuit execution
* */
neg(): Scalar;
/**
* Add scalar field elements.
* Can only be called outside of circuit execution
* */
add(y: Scalar): Scalar;
/**
* Subtract scalar field elements.
* Can only be called outside of circuit execution
* */
sub(y: Scalar): Scalar;
/**
* Multiply scalar field elements.
* Can only be called outside of circuit execution
* */
mul(y: Scalar): Scalar;
/**
* Divide scalar field elements.
* Can only be called outside of circuit execution
* */
div(y: Scalar): Scalar;
toJSON(): JSONValue;
static toFields(x: Scalar): Field[];
static ofFields(fields: Field[]): Scalar;
static sizeInFields(): number;
static ofBits(bits: Bool[]): Scalar;
static random(): Scalar;
static toJSON(x: Scalar): JSONValue;
static fromJSON(x: JSONValue): Scalar | null;
}
declare class EndoScalar {
static toFields(x: Scalar): Field[];
static ofFields(fields: Field[]): Scalar;
static sizeInFields(): number;
}
declare class Group {
x: Field;
y: Field;
add(y: Group): Group;
sub(y: Group): Group;
neg(): Group;
scale(y: Scalar): Group;
// TODO: Add this function when OCaml bindings are implemented : endoScale(y: EndoScalar): Group;
assertEquals(y: Group): void;
equals(y: Group): Bool;
toJSON(): JSONValue;
constructor(args: {
x: Field | number | string | boolean;
y: Field | number | string | boolean;
});
constructor(
x: Field | number | string | boolean,
y: Field | number | string | boolean
);
static generator: Group;
static add(x: Group, y: Group): Group;
static sub(x: Group, y: Group): Group;
static neg(x: Group): Group;
static scale(x: Group, y: Scalar): Group;
// TODO: Add this function when OCaml bindings are implemented : static endoScale(x: Group, y: EndoScalar): Group;
static assertEqual(x: Group, y: Group): void;
static equal(x: Group, y: Group): Bool;
static toFields(x: Group): Field[];
static ofFields(fields: Field[]): Group;
static sizeInFields(): number;
static toJSON(x: Group): JSONValue;
static fromJSON({
x,
y,
}: {
x: string | number;
y: string | number;
}): Group | null;
}
declare class Sponge {
constructor();
absorb(x: Field): void;
squeeze(): Field;
}
declare const Poseidon: {
hash: (xs: Field[]) => Field;
Sponge: typeof Sponge;
};
/**
* Part of the circuit [[ Keypair ]]. A verification key can be used to verify a [[ Proof ]] when you provide the correct public input.
*/
declare class VerificationKey {
verify(publicInput: any[], proof: Proof): boolean;
}
/**
* Contains a proving key and [[ VerificationKey ]] which can be used to verify proofs.
*/
declare class Keypair {
verificationKey(): VerificationKey;
}
/**
* Proofs can be verified using a [[ VerificationKey ]] and the public input.
*/
declare class Proof {
verify(verificationKey: VerificationKey, publicInput: any[]): boolean;
}
interface UInt32_ {
value: Field;
}
interface UInt64_ {
value: Field;
}
interface Account {
publicKey: { g: Group };
balance: UInt64_;
nonce: UInt32_;
zkapp: { appState: Field[] };
}
// TODO would be nice to document these, at least the parts that end up being used in the public API
declare class Ledger {
static create(
genesisAccounts: Array<{ publicKey: { g: Group }; balance: string }>
): Ledger;
addAccount(publicKey: { g: Group }, balance: string): void;
applyJsonTransaction(txJson: string, accountCreationFee: string): Account[];
getAccount(publicKey: { g: Group }): Account | undefined;
static hashTransaction(partyHash: Field): Field;
static hashTransactionChecked(partyHash: Field): Field;
static transactionCommitments(txJson: string): {
commitment: Field;
fullCommitment: Field;
};
static transactionStatement(txJson: string, partyIndex: number): Statement;
static signFieldElement(
messageHash: Field,
privateKey: { s: Scalar }
): string;
static signFeePayer(txJson: string, privateKey: { s: Scalar }): string;
static signOtherParty(
txJson: string,
privateKey: { s: Scalar },
i: number
): string;
static verifyPartyProof(
statement: Statement,
proof: string,
verificationKey: string
): Promise<boolean>;
static publicKeyToString(publicKey: { g: Group }): string;
static publicKeyOfString(publicKeyBase58: string): Group;
static privateKeyToString(privateKey: { s: Scalar }): string;
static privateKeyOfString(privateKeyBase58: string): Scalar;
static fieldToBase58(field: Field): string;
static fieldOfBase58(fieldBase58: string): Field;
static memoToBase58(memoString: string): string;
static fieldsOfJson(json: string): Field[];
static hashPartyFromFields(fields: Field[]): Field;
}
/**
* This function *must* be called at the end of a nodejs program, otherwise the
* worker threads will continue running and the program will never terminate.
* From web applications, this function is a no-op.
*/
declare const shutdown: () => Promise<undefined>;
/**
* A Promise that resolves when SnarkyJS is ready to be used
*/
declare let isReady: Promise<undefined>;
type Statement = { transaction: Field; atParty: Field };
declare const Pickles: {
/**
* This is the core API of the `Pickles` library, exposed from OCaml to JS. It takes a list of circuits --
* each in the form of a function which takes a public input `{ transaction: Field; atParty: Field }` as argument --,
* and joins them into one single circuit which can not only provide proofs for any of the sub-circuits, but also
* adds the necessary circuit logic to recursively merge in earlier proofs.
*
* After forming that big circuit in the finite field represented by `Field`, it gets wrapped in a
* recursive circuit in the field represented by `Scalar`. Any SmartContract proof will go through both of these circuits,
* so that the final proof ends up back in `Field`.
*
* The function returns the building blocks needed for SmartContract proving:
* * `provers` - a list of prover functions, on for each input `rule`
* * `verify` - a function which can verify proofs from any of the provers
* * `getVerificationKeyArtifact` - a function which returns the verification key used in `verify`, in base58 format, usable to deploy a zkapp
*/
compile: (rules: [0, string, (statement: Statement) => void][]) => {
provers: ((statement: Statement) => Promise<unknown>)[];
verify: (statement: Statement, proof: unknown) => Promise<boolean>;
getVerificationKeyArtifact: () => { data: string; hash: string };
};
proofToString: (proof: unknown) => string;
};
type JSONValue =
| number
| string
| boolean
| null
| Array<JSONValue>
| { [key: string]: JSONValue };