Skip to content

Commit 64bbfad

Browse files
authored
Merge pull request #1974 from o1-labs/2025-01-touch-up-doc-comments
Add doc comments to ZkProgram et al
2 parents 54d9a52 + 7acd940 commit 64bbfad

File tree

4 files changed

+107
-10
lines changed

4 files changed

+107
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1919

2020
### Added
2121
- `setFee` and `setFeePerSnarkCost` for `Transaction` and `PendingTransaction` https://github.com/o1-labs/o1js/pull/1968
22+
- Doc comments for various ZkProgram methods https://github.com/o1-labs/o1js/pull/1974
2223

2324
### Changed
2425
- Sort order for actions now includes the transaction sequence number and the exact account id sequence https://github.com/o1-labs/o1js/pull/1917

src/lib/proof-system/proof.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
initializeBindings,
44
withThreadPool,
55
} from '../../snarky.js';
6-
import { Pickles } from '../../snarky.js';
6+
import { Pickles, Base64ProofString } from '../../snarky.js';
77
import { Field, Bool } from '../provable/wrapped.js';
88
import type {
99
FlexibleProvable,
@@ -121,7 +121,10 @@ class ProofBase<Input = any, Output = any> {
121121
return (this.constructor as typeof ProofBase).publicFields(this);
122122
}
123123

124-
static _proofFromBase64(proofString: string, maxProofsVerified: 0 | 1 | 2) {
124+
static _proofFromBase64(
125+
proofString: Base64ProofString,
126+
maxProofsVerified: 0 | 1 | 2
127+
) {
125128
assertBindingsInitialized();
126129
return Pickles.proofOfBase64(proofString, maxProofsVerified)[1];
127130
}

src/lib/proof-system/zkprogram.ts

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { EmptyUndefined, EmptyVoid } from '../../bindings/lib/generic.js';
2-
import { Snarky, initializeBindings, withThreadPool } from '../../snarky.js';
2+
import {
3+
Base64ProofString,
4+
Base64VerificationKeyString,
5+
Snarky,
6+
initializeBindings,
7+
withThreadPool,
8+
} from '../../snarky.js';
39
import { Pickles, Gate } from '../../snarky.js';
410
import { Field } from '../provable/wrapped.js';
511
import {
@@ -118,9 +124,16 @@ function createProgramState() {
118124
};
119125
}
120126

127+
/**
128+
* Initializes Pickles bindings, serializes the input proof and verification key for use in OCaml, then calls into the Pickles verify function and returns the result.
129+
*
130+
* @param proof Either a `Proof` instance or a serialized JSON proof
131+
* @param verificationKey Either a base64 serialized verification key or a `VerificationKey` instance which will be base64 serialized for use in the bindings.
132+
* @returns A promise that resolves to a boolean indicating whether the proof is valid.
133+
*/
121134
async function verify(
122135
proof: ProofBase<any, any> | JsonProof,
123-
verificationKey: string | VerificationKey
136+
verificationKey: Base64VerificationKeyString | VerificationKey
124137
) {
125138
await initializeBindings();
126139
let picklesProof: Pickles.Proof;
@@ -155,11 +168,16 @@ async function verify(
155168
);
156169
}
157170

171+
/**
172+
* Serializable representation of a Pickles proof, useful for caching compiled proofs.
173+
*/
158174
type JsonProof = {
175+
/** Array of string, where each string is a `Field` in the publicInput of this proof */
159176
publicInput: string[];
177+
/** Array of string, where each string is a `Field` in the publicOutput of this proof */
160178
publicOutput: string[];
161179
maxProofsVerified: 0 | 1 | 2;
162-
proof: string;
180+
proof: Base64ProofString;
163181
};
164182
type CompiledTag = unknown;
165183

@@ -183,6 +201,33 @@ let SideloadedTag = {
183201
},
184202
};
185203

204+
/**
205+
* Wraps config + provable code into a program capable of producing {@link Proof}s.
206+
*
207+
* @example
208+
* ```ts
209+
* const ExampleProgram = ZkProgram({
210+
* name: 'ExampleProgram',
211+
* publicOutput: Int64,
212+
* methods: {
213+
* // Prove that I know 2 numbers less than 100 each, whose product is greater than 1000
214+
* provableMultiply: {
215+
* privateInputs: [Int64, Int64],
216+
* method: async (n1: Int64, n2: Int64) => {
217+
* n1.assertLessThan(100);
218+
* n2.assertLessThan(100);
219+
* const publicOutput = n1.mul(n2);
220+
* publicOutput.assertGreaterThan(1000);
221+
* return { publicOutput: n1.mul(n2) }
222+
* }
223+
* }
224+
* }
225+
* });
226+
* ```
227+
*
228+
* @param config The configuration of the program, describing the type of the public input and public output, as well as defining the methods which can be executed provably.
229+
* @returns an object that can be used to compile, prove, and verify the program.
230+
*/
186231
function ZkProgram<
187232
Config extends {
188233
publicInput?: ProvableType;
@@ -591,6 +636,33 @@ type ZkProgram<
591636
}
592637
> = ReturnType<typeof ZkProgram<Config, Methods>>;
593638

639+
/**
640+
* A class representing the type of Proof produced by the {@link ZkProgram} in which it is used.
641+
*
642+
* @example
643+
* ```ts
644+
* const ExampleProgram = ZkProgram({
645+
* name: 'ExampleProgram',
646+
* publicOutput: Field,
647+
* methods: {
648+
* baseCase: {
649+
* privateInputs: [],
650+
* method: async () => {
651+
* return { publicOutput: Field(0) }
652+
* }
653+
* },
654+
* add: {
655+
* privateInputs: [SelfProof, Field],
656+
* // `previous` is the type of proof produced by ExampleProgram
657+
* method: async (previous: SelfProof<undefined, Field>, f: Field) => {
658+
* previous.verify();
659+
* return { publicOutput: previous.publicOutput.add(f) }
660+
* }
661+
* }
662+
* }
663+
* });
664+
* ```
665+
*/
594666
class SelfProof<PublicInput, PublicOutput> extends Proof<
595667
PublicInput,
596668
PublicOutput

src/snarky.d.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,22 @@ export {
4848
MlPublicKeyVar,
4949
MlFeatureFlags,
5050
areBindingsInitialized,
51+
Base64ProofString,
52+
Base64VerificationKeyString,
5153
};
5254

5355
declare let areBindingsInitialized: boolean;
5456

57+
/**
58+
* A string representation of a Pickles proof in base64 encoding, used for communication between OCaml and TypeScript and for JSON serialization.
59+
*/
60+
type Base64ProofString = string;
61+
62+
/**
63+
* A string representation of a constraint system's verification key in base64 encoding, used for communication between OCaml and TypeScript and for JSON serialization.
64+
*/
65+
type Base64VerificationKeyString = string;
66+
5567
type WasmModule = typeof wasm;
5668

5769
type MlGroup = MlPair<FieldVar, FieldVar>;
@@ -700,13 +712,15 @@ declare const Pickles: {
700712
/**
701713
* @returns (base64 vk, hash)
702714
*/
703-
getVerificationKey: () => Promise<[_: 0, data: string, hash: FieldConst]>;
715+
getVerificationKey: () => Promise<
716+
[_: 0, data: Base64VerificationKeyString, hash: FieldConst]
717+
>;
704718
};
705719

706720
verify(
707721
statement: Pickles.Statement<FieldConst>,
708722
proof: Pickles.Proof,
709-
verificationKey: string
723+
verificationKey: Base64VerificationKeyString
710724
): Promise<boolean>;
711725

712726
loadSrsFp(): WasmFpSrs;
@@ -720,12 +734,16 @@ declare const Pickles: {
720734
/**
721735
* @returns (base64 vk, hash)
722736
*/
723-
dummyVerificationKey: () => [_: 0, data: string, hash: FieldConst];
737+
dummyVerificationKey: () => [
738+
_: 0,
739+
data: Base64VerificationKeyString,
740+
hash: FieldConst
741+
];
724742

725743
encodeVerificationKey: (vk: MlWrapVerificationKey) => string;
726744
decodeVerificationKey: (vk: string) => MlWrapVerificationKey;
727745

728-
proofToBase64: (proof: [0 | 1 | 2, Pickles.Proof]) => string;
746+
proofToBase64: (proof: [0 | 1 | 2, Pickles.Proof]) => Base64ProofString;
729747
proofOfBase64: <N extends 0 | 1 | 2>(
730748
base64: string,
731749
maxProofsVerified: N
@@ -745,7 +763,10 @@ declare const Pickles: {
745763
// Instantiate the verification key inside the circuit (required).
746764
inCircuit: (tag: unknown, verificationKey: unknown) => undefined;
747765
// Instantiate the verification key in prover-only logic (also required).
748-
inProver: (tag: unknown, verificationKey: string) => undefined;
766+
inProver: (
767+
tag: unknown,
768+
verificationKey: Base64VerificationKeyString
769+
) => undefined;
749770
// Create an in-circuit representation of a verification key
750771
vkToCircuit: (
751772
verificationKey: () => string

0 commit comments

Comments
 (0)