@proto-kit/common 0.1.1-develop.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +201 -0
- package/dist/compiling/AtomicCompileHelper.d.ts +13 -0
- package/dist/compiling/AtomicCompileHelper.d.ts.map +1 -0
- package/dist/compiling/AtomicCompileHelper.js +40 -0
- package/dist/compiling/AtomicCompileHelper.js.map +1 -0
- package/dist/compiling/CompilableModule.d.ts +6 -0
- package/dist/compiling/CompilableModule.d.ts.map +1 -0
- package/dist/compiling/CompilableModule.js +2 -0
- package/dist/compiling/CompilableModule.js.map +1 -0
- package/dist/compiling/CompileRegistry.d.ts +26 -0
- package/dist/compiling/CompileRegistry.d.ts.map +1 -0
- package/dist/compiling/CompileRegistry.js +68 -0
- package/dist/compiling/CompileRegistry.js.map +1 -0
- package/dist/compiling/services/ChildVerificationKeyService.d.ts +10 -0
- package/dist/compiling/services/ChildVerificationKeyService.d.ts.map +1 -0
- package/dist/compiling/services/ChildVerificationKeyService.js +27 -0
- package/dist/compiling/services/ChildVerificationKeyService.js.map +1 -0
- package/dist/config/ChildContainerCreatable.d.ts +5 -0
- package/dist/config/ChildContainerCreatable.d.ts.map +1 -0
- package/dist/config/ChildContainerCreatable.js +2 -0
- package/dist/config/ChildContainerCreatable.js.map +1 -0
- package/dist/config/ChildContainerProvider.d.ts +5 -0
- package/dist/config/ChildContainerProvider.d.ts.map +1 -0
- package/dist/config/ChildContainerProvider.js +2 -0
- package/dist/config/ChildContainerProvider.js.map +1 -0
- package/dist/config/ConfigurableModule.d.ts +25 -0
- package/dist/config/ConfigurableModule.d.ts.map +1 -0
- package/dist/config/ConfigurableModule.js +24 -0
- package/dist/config/ConfigurableModule.js.map +1 -0
- package/dist/config/ModuleContainer.d.ts +162 -0
- package/dist/config/ModuleContainer.d.ts.map +1 -0
- package/dist/config/ModuleContainer.js +289 -0
- package/dist/config/ModuleContainer.js.map +1 -0
- package/dist/config/injectAlias.d.ts +18 -0
- package/dist/config/injectAlias.d.ts.map +1 -0
- package/dist/config/injectAlias.js +47 -0
- package/dist/config/injectAlias.js.map +1 -0
- package/dist/dependencyFactory/DependencyFactory.d.ts +29 -0
- package/dist/dependencyFactory/DependencyFactory.d.ts.map +1 -0
- package/dist/dependencyFactory/DependencyFactory.js +2 -0
- package/dist/dependencyFactory/DependencyFactory.js.map +1 -0
- package/dist/dependencyFactory/injectOptional.d.ts +16 -0
- package/dist/dependencyFactory/injectOptional.d.ts.map +1 -0
- package/dist/dependencyFactory/injectOptional.js +40 -0
- package/dist/dependencyFactory/injectOptional.js.map +1 -0
- package/dist/dummyVerificationKey.d.ts +3 -0
- package/dist/dummyVerificationKey.d.ts.map +1 -0
- package/dist/dummyVerificationKey.js +8 -0
- package/dist/dummyVerificationKey.js.map +1 -0
- package/dist/events/EventEmitter.d.ts +19 -0
- package/dist/events/EventEmitter.d.ts.map +1 -0
- package/dist/events/EventEmitter.js +35 -0
- package/dist/events/EventEmitter.js.map +1 -0
- package/dist/events/EventEmitterProxy.d.ts +18 -0
- package/dist/events/EventEmitterProxy.d.ts.map +1 -0
- package/dist/events/EventEmitterProxy.js +35 -0
- package/dist/events/EventEmitterProxy.js.map +1 -0
- package/dist/events/EventEmittingComponent.d.ts +9 -0
- package/dist/events/EventEmittingComponent.d.ts.map +1 -0
- package/dist/events/EventEmittingComponent.js +2 -0
- package/dist/events/EventEmittingComponent.js.map +1 -0
- package/dist/events/ReplayingSingleUseEventEmitter.d.ts +17 -0
- package/dist/events/ReplayingSingleUseEventEmitter.d.ts.map +1 -0
- package/dist/events/ReplayingSingleUseEventEmitter.js +34 -0
- package/dist/events/ReplayingSingleUseEventEmitter.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +37 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +114 -0
- package/dist/log.js.map +1 -0
- package/dist/trees/InMemoryMerkleTreeStorage.d.ts +11 -0
- package/dist/trees/InMemoryMerkleTreeStorage.d.ts.map +1 -0
- package/dist/trees/InMemoryMerkleTreeStorage.js +13 -0
- package/dist/trees/InMemoryMerkleTreeStorage.js.map +1 -0
- package/dist/trees/MerkleTreeStore.d.ts +5 -0
- package/dist/trees/MerkleTreeStore.d.ts.map +1 -0
- package/dist/trees/MerkleTreeStore.js +2 -0
- package/dist/trees/MerkleTreeStore.js.map +1 -0
- package/dist/trees/MockAsyncMerkleStore.d.ts +9 -0
- package/dist/trees/MockAsyncMerkleStore.d.ts.map +1 -0
- package/dist/trees/MockAsyncMerkleStore.js +20 -0
- package/dist/trees/MockAsyncMerkleStore.js.map +1 -0
- package/dist/trees/RollupMerkleTree.d.ts +147 -0
- package/dist/trees/RollupMerkleTree.d.ts.map +1 -0
- package/dist/trees/RollupMerkleTree.js +218 -0
- package/dist/trees/RollupMerkleTree.js.map +1 -0
- package/dist/trees/VirtualMerkleTreeStore.d.ts +13 -0
- package/dist/trees/VirtualMerkleTreeStore.d.ts.map +1 -0
- package/dist/trees/VirtualMerkleTreeStore.js +18 -0
- package/dist/trees/VirtualMerkleTreeStore.js.map +1 -0
- package/dist/types.d.ts +27 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +48 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +113 -0
- package/dist/utils.js.map +1 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts +54 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.d.ts.map +1 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.js +97 -0
- package/dist/zkProgrammable/ProvableMethodExecutionContext.js.map +1 -0
- package/dist/zkProgrammable/ZkProgrammable.d.ts +40 -0
- package/dist/zkProgrammable/ZkProgrammable.d.ts.map +1 -0
- package/dist/zkProgrammable/ZkProgrammable.js +79 -0
- package/dist/zkProgrammable/ZkProgrammable.js.map +1 -0
- package/dist/zkProgrammable/provableMethod.d.ts +19 -0
- package/dist/zkProgrammable/provableMethod.d.ts.map +1 -0
- package/dist/zkProgrammable/provableMethod.js +71 -0
- package/dist/zkProgrammable/provableMethod.js.map +1 -0
- package/jest.config.cjs +12 -0
- package/package.json +34 -0
- package/src/compiling/AtomicCompileHelper.ts +62 -0
- package/src/compiling/CompilableModule.ts +6 -0
- package/src/compiling/CompileRegistry.ts +78 -0
- package/src/compiling/services/ChildVerificationKeyService.ts +26 -0
- package/src/config/ChildContainerCreatable.ts +5 -0
- package/src/config/ChildContainerProvider.ts +5 -0
- package/src/config/ConfigurableModule.ts +57 -0
- package/src/config/ModuleContainer.ts +487 -0
- package/src/config/injectAlias.ts +70 -0
- package/src/dependencyFactory/DependencyFactory.ts +57 -0
- package/src/dependencyFactory/injectOptional.ts +41 -0
- package/src/dummyVerificationKey.ts +10 -0
- package/src/events/EventEmitter.ts +61 -0
- package/src/events/EventEmitterProxy.ts +83 -0
- package/src/events/EventEmittingComponent.ts +11 -0
- package/src/events/ReplayingSingleUseEventEmitter.ts +42 -0
- package/src/index.ts +25 -0
- package/src/log.ts +143 -0
- package/src/trees/InMemoryMerkleTreeStorage.ts +17 -0
- package/src/trees/MerkleTreeStore.ts +5 -0
- package/src/trees/MockAsyncMerkleStore.ts +30 -0
- package/src/trees/RollupMerkleTree.ts +356 -0
- package/src/trees/VirtualMerkleTreeStore.ts +20 -0
- package/src/types.ts +58 -0
- package/src/utils.ts +200 -0
- package/src/zkProgrammable/ProvableMethodExecutionContext.ts +122 -0
- package/src/zkProgrammable/ZkProgrammable.ts +151 -0
- package/src/zkProgrammable/provableMethod.ts +124 -0
- package/test/config/ContainerEvents.test.ts +67 -0
- package/test/config/ModuleContainer.test.ts +215 -0
- package/test/config/injectAlias.test.ts +28 -0
- package/test/trees/MerkleTree.test.ts +106 -0
- package/test/tsconfig.json +7 -0
- package/test/zkProgrammable/ZkProgrammable.test.ts +304 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Proof } from "o1js";
|
|
2
|
+
import type { ArgumentTypes } from "./provableMethod";
|
|
3
|
+
export declare class ProvableMethodExecutionResult {
|
|
4
|
+
moduleName?: string;
|
|
5
|
+
methodName?: string;
|
|
6
|
+
args?: ArgumentTypes;
|
|
7
|
+
prover?: () => Promise<Proof<unknown, unknown>>;
|
|
8
|
+
prove<ProofType extends Proof<unknown, unknown>>(): Promise<ProofType>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Execution context used to wrap runtime module methods,
|
|
12
|
+
* allowing them to post relevant information (such as execution status)
|
|
13
|
+
* into the context without any unnecessary 'prop drilling'.
|
|
14
|
+
*/
|
|
15
|
+
export declare class ProvableMethodExecutionContext {
|
|
16
|
+
id: string;
|
|
17
|
+
methods: string[];
|
|
18
|
+
result: ProvableMethodExecutionResult;
|
|
19
|
+
/**
|
|
20
|
+
* Adds a method prover to the current execution context,
|
|
21
|
+
* which can be collected and ran asynchronously at a later point in time.
|
|
22
|
+
*
|
|
23
|
+
* @param prove - Prover function to be ran later,
|
|
24
|
+
* when the method execution needs to be proven
|
|
25
|
+
*/
|
|
26
|
+
setProver(prover: () => Promise<Proof<unknown, unknown>>): void;
|
|
27
|
+
/**
|
|
28
|
+
* Adds a method to the method execution stack, reseting the execution context
|
|
29
|
+
* in a case a new top-level (non nested) method call is made.
|
|
30
|
+
*
|
|
31
|
+
* @param methodName - Name of the method being captured in the context
|
|
32
|
+
*/
|
|
33
|
+
beforeMethod(moduleName: string, methodName: string, args: ArgumentTypes): void;
|
|
34
|
+
/**
|
|
35
|
+
* Removes the latest method from the execution context stack,
|
|
36
|
+
* keeping track of the amount of 'unfinished' methods. Allowing
|
|
37
|
+
* for the context to distinguish between top-level and nested method calls.
|
|
38
|
+
*/
|
|
39
|
+
afterMethod(): void;
|
|
40
|
+
get isTopLevel(): boolean;
|
|
41
|
+
get isFinished(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* @returns - Current execution context state
|
|
44
|
+
*/
|
|
45
|
+
current(): {
|
|
46
|
+
isFinished: boolean;
|
|
47
|
+
result: ProvableMethodExecutionResult;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Manually clears/resets the execution context
|
|
51
|
+
*/
|
|
52
|
+
clear(): void;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=ProvableMethodExecutionContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProvableMethodExecutionContext.d.ts","sourceRoot":"","sources":["../../src/zkProgrammable/ProvableMethodExecutionContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAIlC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAWtD,qBAAa,6BAA6B;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB,IAAI,CAAC,EAAE,aAAa,CAAC;IAErB,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAE1C,KAAK,CAChB,SAAS,SAAS,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,KACtC,OAAO,CAAC,SAAS,CAAC;CAaxB;AAED;;;;GAIG;AACH,qBACa,8BAA8B;IAClC,EAAE,SAAc;IAEhB,OAAO,EAAE,MAAM,EAAE,CAAM;IAEvB,MAAM,EAAE,6BAA6B,CACN;IAGtC;;;;;;OAMG;IACI,SAAS,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAI/D;;;;;OAKG;IACI,YAAY,CACjB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,aAAa;IAYrB;;;;OAIG;IACI,WAAW;IAIlB,IAAW,UAAU,YAEpB;IAED,IAAW,UAAU,YAEpB;IAED;;OAEG;IACI,OAAO;;;;IAOd;;OAEG;IACI,KAAK;CAGb"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { singleton } from "tsyringe";
|
|
8
|
+
import uniqueId from "lodash/uniqueId";
|
|
9
|
+
const errors = {
|
|
10
|
+
moduleOrMethodNameNotSet: () => new Error("Module or method name not set"),
|
|
11
|
+
proverNotSet: (moduleName, methodName) => new Error(`Prover not set for '${moduleName}.${methodName}', did you forget to decorate your method?`),
|
|
12
|
+
};
|
|
13
|
+
export class ProvableMethodExecutionResult {
|
|
14
|
+
async prove() {
|
|
15
|
+
if (!this.prover) {
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
17
|
+
if (!this.moduleName || !this.methodName) {
|
|
18
|
+
throw errors.moduleOrMethodNameNotSet();
|
|
19
|
+
}
|
|
20
|
+
throw errors.proverNotSet(this.moduleName, this.methodName);
|
|
21
|
+
}
|
|
22
|
+
// turn the prover result into the desired proof type
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
24
|
+
return (await this.prover());
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Execution context used to wrap runtime module methods,
|
|
29
|
+
* allowing them to post relevant information (such as execution status)
|
|
30
|
+
* into the context without any unnecessary 'prop drilling'.
|
|
31
|
+
*/
|
|
32
|
+
export let ProvableMethodExecutionContext = class ProvableMethodExecutionContext {
|
|
33
|
+
constructor() {
|
|
34
|
+
this.id = uniqueId();
|
|
35
|
+
this.methods = [];
|
|
36
|
+
this.result = new ProvableMethodExecutionResult();
|
|
37
|
+
}
|
|
38
|
+
// TODO See if we should make this class generic, bc I think we can persist the type
|
|
39
|
+
/**
|
|
40
|
+
* Adds a method prover to the current execution context,
|
|
41
|
+
* which can be collected and ran asynchronously at a later point in time.
|
|
42
|
+
*
|
|
43
|
+
* @param prove - Prover function to be ran later,
|
|
44
|
+
* when the method execution needs to be proven
|
|
45
|
+
*/
|
|
46
|
+
setProver(prover) {
|
|
47
|
+
this.result.prover = prover;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adds a method to the method execution stack, reseting the execution context
|
|
51
|
+
* in a case a new top-level (non nested) method call is made.
|
|
52
|
+
*
|
|
53
|
+
* @param methodName - Name of the method being captured in the context
|
|
54
|
+
*/
|
|
55
|
+
beforeMethod(moduleName, methodName, args) {
|
|
56
|
+
if (this.isFinished) {
|
|
57
|
+
this.clear();
|
|
58
|
+
this.result.moduleName = moduleName;
|
|
59
|
+
this.result.methodName = methodName;
|
|
60
|
+
this.result.args = args;
|
|
61
|
+
}
|
|
62
|
+
this.methods.push(methodName);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Removes the latest method from the execution context stack,
|
|
66
|
+
* keeping track of the amount of 'unfinished' methods. Allowing
|
|
67
|
+
* for the context to distinguish between top-level and nested method calls.
|
|
68
|
+
*/
|
|
69
|
+
afterMethod() {
|
|
70
|
+
this.methods.pop();
|
|
71
|
+
}
|
|
72
|
+
get isTopLevel() {
|
|
73
|
+
return this.methods.length === 1;
|
|
74
|
+
}
|
|
75
|
+
get isFinished() {
|
|
76
|
+
return this.methods.length === 0;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @returns - Current execution context state
|
|
80
|
+
*/
|
|
81
|
+
current() {
|
|
82
|
+
return {
|
|
83
|
+
isFinished: this.isFinished,
|
|
84
|
+
result: this.result,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Manually clears/resets the execution context
|
|
89
|
+
*/
|
|
90
|
+
clear() {
|
|
91
|
+
this.result = new ProvableMethodExecutionResult();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
ProvableMethodExecutionContext = __decorate([
|
|
95
|
+
singleton()
|
|
96
|
+
], ProvableMethodExecutionContext);
|
|
97
|
+
//# sourceMappingURL=ProvableMethodExecutionContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProvableMethodExecutionContext.js","sourceRoot":"","sources":["../../src/zkProgrammable/ProvableMethodExecutionContext.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,QAAQ,MAAM,iBAAiB,CAAC;AAIvC,MAAM,MAAM,GAAG;IACb,wBAAwB,EAAE,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,+BAA+B,CAAC;IAE1E,YAAY,EAAE,CAAC,UAAkB,EAAE,UAAkB,EAAE,EAAE,CACvD,IAAI,KAAK,CACP,uBAAuB,UAAU,IAAI,UAAU,4CAA4C,CAC5F;CACJ,CAAC;AAEF,MAAM,OAAO,6BAA6B;IASjC,KAAK,CAAC,KAAK;QAGhB,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAChB,yEAAyE;YACzE,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE;gBACxC,MAAM,MAAM,CAAC,wBAAwB,EAAE,CAAC;aACzC;YACD,MAAM,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;SAC7D;QAED,qDAAqD;QACrD,yEAAyE;QACzE,OAAO,CAAC,MAAM,IAAI,CAAC,MA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,8BAA8B;IAD1C,SAAS,EAAE;GACC,8BAA8B,CA0E1C"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ZkProgram, FlexibleProvablePure, Proof, Field, Provable } from "o1js";
|
|
2
|
+
import type { CompileRegistry } from "../compiling/CompileRegistry";
|
|
3
|
+
export interface CompileArtifact {
|
|
4
|
+
verificationKey: {
|
|
5
|
+
data: string;
|
|
6
|
+
hash: Field;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export interface AreProofsEnabled {
|
|
10
|
+
areProofsEnabled: boolean;
|
|
11
|
+
setProofsEnabled: (areProofsEnabled: boolean) => void;
|
|
12
|
+
}
|
|
13
|
+
export interface Verify<PublicInput, PublicOutput> {
|
|
14
|
+
(proof: Proof<PublicInput, PublicOutput>): Promise<boolean>;
|
|
15
|
+
}
|
|
16
|
+
export interface Compile {
|
|
17
|
+
(): Promise<CompileArtifact>;
|
|
18
|
+
}
|
|
19
|
+
export interface PlainZkProgram<PublicInput = undefined, PublicOutput = void> {
|
|
20
|
+
name: string;
|
|
21
|
+
compile: Compile;
|
|
22
|
+
verify: Verify<PublicInput, PublicOutput>;
|
|
23
|
+
Proof: ReturnType<typeof ZkProgram.Proof<FlexibleProvablePure<PublicInput>, FlexibleProvablePure<PublicOutput>>>;
|
|
24
|
+
methods: Record<string, ((...args: any) => Promise<Proof<PublicInput, PublicOutput>>) | ((publicInput: PublicInput, ...args: any) => Promise<Proof<PublicInput, PublicOutput>>)>;
|
|
25
|
+
analyzeMethods: () => Promise<Record<string, Awaited<ReturnType<typeof Provable.constraintSystem>>>>;
|
|
26
|
+
}
|
|
27
|
+
export declare function verifyToMockable<PublicInput, PublicOutput>(verify: Verify<PublicInput, PublicOutput>, { areProofsEnabled }: AreProofsEnabled): (proof: Proof<PublicInput, PublicOutput>) => Promise<boolean>;
|
|
28
|
+
export declare const MOCK_VERIFICATION_KEY: import("o1js/dist/node/lib/proof-system/zkprogram").VerificationKey;
|
|
29
|
+
export declare function compileToMockable(compile: Compile, { areProofsEnabled }: AreProofsEnabled): () => Promise<CompileArtifact>;
|
|
30
|
+
export declare abstract class ZkProgrammable<PublicInput = undefined, PublicOutput = void> {
|
|
31
|
+
abstract get areProofsEnabled(): AreProofsEnabled | undefined;
|
|
32
|
+
abstract zkProgramFactory(): PlainZkProgram<PublicInput, PublicOutput>[];
|
|
33
|
+
private zkProgramSingleton?;
|
|
34
|
+
get zkProgram(): PlainZkProgram<PublicInput, PublicOutput>[];
|
|
35
|
+
compile(registry: CompileRegistry): Promise<Record<string, CompileArtifact>>;
|
|
36
|
+
}
|
|
37
|
+
export interface WithZkProgrammable<PublicInput = undefined, PublicOutput = void> {
|
|
38
|
+
zkProgrammable: ZkProgrammable<PublicInput, PublicOutput>;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=ZkProgrammable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ZkProgrammable.d.ts","sourceRoot":"","sources":["../../src/zkProgrammable/ZkProgrammable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAM/E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AASpE,MAAM,WAAW,eAAe;IAC9B,eAAe,EAAE;QACf,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,KAAK,CAAC;KACb,CAAC;CACH;AAED,MAAM,WAAW,gBAAgB;IAC/B,gBAAgB,EAAE,OAAO,CAAC;IAC1B,gBAAgB,EAAE,CAAC,gBAAgB,EAAE,OAAO,KAAK,IAAI,CAAC;CACvD;AAED,MAAM,WAAW,MAAM,CAAC,WAAW,EAAE,YAAY;IAC/C,CAAC,KAAK,EAAE,KAAK,CAAC,WAAW,EAAE,YAAY,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC7D;AAED,MAAM,WAAW,OA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G,IAAI;IAEnB,aAAoB,gBAAgB,IAAI,gBAAgB,GAAG,SAAS,CAAC;aAErD,gBAAgB,IAAI,cAAc,CAChD,WAAW,EACX,YAAY,CACb,EAAE;IAEH,OAAO,CAAC,kBAAkB,CAAC,CAA8C;IAEzE,IACW,SAAS,IAAI,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,EAAE,CAelE;IAEY,OAAO,CAAC,QAAQ,EAAE,eAAe;CAc/C;AAED,MAAM,WAAW,kBAAkB,CACjC,WAAW,GAAG,SAAS,EACvB,YAAY,GAAG,IAAI;IAEnB,cAAc,EAAE,cAAc,CAAC,WAAW,EAAE,YAAY,CAAC,CAAC;CAC3D"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Memoize } from "typescript-memoize";
|
|
11
|
+
import { log } from "../log";
|
|
12
|
+
import { dummyVerificationKey } from "../dummyVerificationKey";
|
|
13
|
+
import { reduceSequential } from "../utils";
|
|
14
|
+
import { MOCK_PROOF } from "./provableMethod";
|
|
15
|
+
const errors = {
|
|
16
|
+
areProofsEnabledNotSet: (name) => new Error(`AreProofsEnabled was not injected for: ${name}`),
|
|
17
|
+
};
|
|
18
|
+
export function verifyToMockable(verify, { areProofsEnabled }) {
|
|
19
|
+
return async (proof) => {
|
|
20
|
+
if (areProofsEnabled) {
|
|
21
|
+
let verified = false;
|
|
22
|
+
try {
|
|
23
|
+
verified = await verify(proof);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
// silently fail verification
|
|
27
|
+
log.error(error);
|
|
28
|
+
verified = false;
|
|
29
|
+
}
|
|
30
|
+
return verified;
|
|
31
|
+
}
|
|
32
|
+
return proof.proof === MOCK_PROOF;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export const MOCK_VERIFICATION_KEY = dummyVerificationKey();
|
|
36
|
+
export function compileToMockable(compile, { areProofsEnabled }) {
|
|
37
|
+
return async () => {
|
|
38
|
+
if (areProofsEnabled) {
|
|
39
|
+
return await compile();
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
verificationKey: MOCK_VERIFICATION_KEY,
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export class ZkProgrammable {
|
|
47
|
+
get zkProgram() {
|
|
48
|
+
if (this.zkProgramSingleton === undefined) {
|
|
49
|
+
this.zkProgramSingleton = this.zkProgramFactory();
|
|
50
|
+
}
|
|
51
|
+
return this.zkProgramSingleton.map((bucket) => {
|
|
52
|
+
if (!this.areProofsEnabled) {
|
|
53
|
+
throw errors.areProofsEnabledNotSet(this.constructor.name);
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
...bucket,
|
|
57
|
+
verify: verifyToMockable(bucket.verify, this.areProofsEnabled),
|
|
58
|
+
compile: compileToMockable(bucket.compile, this.areProofsEnabled),
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
async compile(registry) {
|
|
63
|
+
return await reduceSequential(this.zkProgram, async (acc, program) => {
|
|
64
|
+
const result = await registry.compile(program);
|
|
65
|
+
return {
|
|
66
|
+
...acc,
|
|
67
|
+
[program.name]: result,
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
71
|
+
{});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
__decorate([
|
|
75
|
+
Memoize(),
|
|
76
|
+
__metadata("design:type", Array),
|
|
77
|
+
__metadata("design:paramtypes", [])
|
|
78
|
+
], ZkProgrammable.prototype, "zkProgram", null);
|
|
79
|
+
//# sourceMappingURL=ZkProgrammable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ZkProgrammable.js","sourceRoot":"","sources":["../../src/zkProgrammable/ZkProgrammable.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAE7C,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAC7B,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAG5C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,MAAM,MAAM,GAAG;IACb,sBAAsB,EAAE,CAAC,IAAY,EAAE,EAAE,CACvC,IAAI,KAAK,CAAC,0CAA0C,IAAI,EAAE,CAAC;CAC9D,CAAC;AA6CF,MAAM,UAAU,gBAAgB,CAC9B,MAAyC,EACzC,EAAE,gBAAgB,EAAoB;IAEtC,OAAO,KAAK,EAAE,KAAuC,EAAE,EAAE;QACvD,IAAI,gBAAgB,EAAE;YACpB,IAAI,QAAQ,GAAG,KAAK,CAAC;YAErB,IAAI;gBACF,QAAQ,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,CAAC;aAChC;YAAC,OAAO,KAAc,EAAE;gBACvB,6BAA6B;gBAC7B,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACjB,QAAQ,GAAG,KAAK,CAAC;aAClB;YAED,OAAO,QAAQ,CAAC;SACjB;QAED,OAAO,KAAK,CAAC,KAA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CAAC,IAAI,CAAC,EAAE,MAAM;aACvB,CAAC;QACJ,CAAC;QACD,yEAAyE;QACzE,EAAqC,CACtC,CAAC;IACJ,CAAC;CACF;AAhCC;IAAC,OAAO,EAAE;;;+CAgBT"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Proof, DynamicProof } from "o1js";
|
|
2
|
+
import { ProvableMethodExecutionContext } from "./ProvableMethodExecutionContext";
|
|
3
|
+
import type { WithZkProgrammable, ZkProgrammable } from "./ZkProgrammable";
|
|
4
|
+
export type O1JSPrimitive = object | string | boolean | number;
|
|
5
|
+
export type ArgumentTypes = (O1JSPrimitive | Proof<unknown, unknown> | DynamicProof<unknown, unknown>)[];
|
|
6
|
+
export type DecoratedMethod = (...args: ArgumentTypes) => Promise<unknown>;
|
|
7
|
+
export declare const MOCK_PROOF = "mock-proof";
|
|
8
|
+
export declare function toProver(methodName: string, simulatedMethod: DecoratedMethod, isFirstParameterPublicInput: boolean, ...args: ArgumentTypes): (this: ZkProgrammable<any, any>) => Promise<Proof<any, any>>;
|
|
9
|
+
/**
|
|
10
|
+
* Decorates a provable method on a 'prover class', depending on
|
|
11
|
+
* if proofs are enabled or not, either runs the respective zkProgram prover,
|
|
12
|
+
* or simulates the method execution and issues a mock proof.
|
|
13
|
+
*
|
|
14
|
+
* @param isFirstParameterPublicInput
|
|
15
|
+
* @param executionContext
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
export declare function provableMethod(isFirstParameterPublicInput?: boolean, executionContext?: ProvableMethodExecutionContext): <Target extends ZkProgrammable<any, any> | WithZkProgrammable<any, any>>(target: Target, methodName: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => Promise<any> | any>) => TypedPropertyDescriptor<(...args: any[]) => Promise<any> | any>;
|
|
19
|
+
//# sourceMappingURL=provableMethod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provableMethod.d.ts","sourceRoot":"","sources":["../../src/zkProgrammable/provableMethod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAG3C,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAG3E,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AAC/D,MAAM,MAAM,aAAa,GAAG,CACxB,aAAa,GACb,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,GACvB,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,CACjC,EAAE,CAAC;AAEJ,MAAM,MAAM,eAAe,GAAG,CAAC,GAAG,IAAI,EAAE,aAAa,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE3E,eAAO,MAAM,UAAU,eAAe,CAAC;AAGvC,wBAAgB,QAAQ,CACtB,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,eAAe,EAChC,2BAA2B,EAAE,OAAO,EACpC,GAAG,IAAI,EAAE,aAAa,UAEa,eAAe,GAAG,EAAE,GAAG,CAAC,8BAiC5D;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,2BAA2B,UAAO,EAClC,gBAAgB,GAAE,8BAEjB,wGAMa,MAAM,gDAC4B,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,GAAG,GAAG,wCAA5B,GAAG,EAAE,KAAK,QAAQ,GAAG,CAAC,GAAG,GAAG,EA2C7E"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { container } from "tsyringe";
|
|
2
|
+
import { ProvableMethodExecutionContext } from "./ProvableMethodExecutionContext";
|
|
3
|
+
export const MOCK_PROOF = "mock-proof";
|
|
4
|
+
// (await Proof.dummy(Field(0), Field(0), 2)).proof as string;
|
|
5
|
+
export function toProver(methodName, simulatedMethod, isFirstParameterPublicInput, ...args) {
|
|
6
|
+
return async function prover() {
|
|
7
|
+
const { areProofsEnabled } = this.areProofsEnabled;
|
|
8
|
+
const zkProgram = this.zkProgram.find((prog) => Object.keys(prog.methods).includes(methodName));
|
|
9
|
+
if (zkProgram === undefined) {
|
|
10
|
+
throw new Error("Correct ZkProgram not found");
|
|
11
|
+
}
|
|
12
|
+
if (areProofsEnabled) {
|
|
13
|
+
const programProvableMethod = zkProgram.methods[methodName];
|
|
14
|
+
return await Reflect.apply(programProvableMethod, this, args);
|
|
15
|
+
}
|
|
16
|
+
// create a mock proof by simulating method> execution in JS
|
|
17
|
+
const publicOutput = await Reflect.apply(simulatedMethod, this, args);
|
|
18
|
+
return new zkProgram.Proof({
|
|
19
|
+
proof: MOCK_PROOF,
|
|
20
|
+
// TODO: provide undefined if public input is not used
|
|
21
|
+
publicInput: isFirstParameterPublicInput ? args[0] : undefined,
|
|
22
|
+
publicOutput,
|
|
23
|
+
/**
|
|
24
|
+
* We set this to the max possible number, to avoid having
|
|
25
|
+
* to manually count in-circuit proof verifications
|
|
26
|
+
*/
|
|
27
|
+
maxProofsVerified: 2,
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Decorates a provable method on a 'prover class', depending on
|
|
33
|
+
* if proofs are enabled or not, either runs the respective zkProgram prover,
|
|
34
|
+
* or simulates the method execution and issues a mock proof.
|
|
35
|
+
*
|
|
36
|
+
* @param isFirstParameterPublicInput
|
|
37
|
+
* @param executionContext
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
export function provableMethod(isFirstParameterPublicInput = true, executionContext = container.resolve(ProvableMethodExecutionContext)) {
|
|
41
|
+
return (target, methodName, descriptor) => {
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
43
|
+
const simulatedMethod = descriptor.value;
|
|
44
|
+
descriptor.value = async function value(...args) {
|
|
45
|
+
const prover = toProver(methodName, simulatedMethod, isFirstParameterPublicInput, ...args);
|
|
46
|
+
executionContext.beforeMethod(this.constructor.name, methodName, args);
|
|
47
|
+
/**
|
|
48
|
+
* Check if the method is called at the top level,
|
|
49
|
+
* if yes then create a prover.
|
|
50
|
+
*/
|
|
51
|
+
if (executionContext.isTopLevel) {
|
|
52
|
+
executionContext.setProver(prover.bind(this));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Regardless of if the method is called from the top level
|
|
56
|
+
* or not, execute its simulated (Javascript) version and
|
|
57
|
+
* return the result.
|
|
58
|
+
*/
|
|
59
|
+
let result;
|
|
60
|
+
try {
|
|
61
|
+
result = Reflect.apply(simulatedMethod, this, args);
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
executionContext.afterMethod();
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
68
|
+
return descriptor;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=provableMethod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provableMethod.js","sourceRoot":"","sources":["../../src/zkProgrammable/provableMethod.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAalF,MAAM,CAAC,MAAM,UAAU,GAAG,YAAY,CAAC;AACvC,8DAA8D;AAE9D,MAAM,UAAU,QAAQ,CACtB,UAAkB,EAClB,eAAgC,EAChC,2BAAoC,EACpC,GAAG,IAAmB;IAEtB,OAAO,KAAK,UAAU,MAAM;QAC1B,MAAM,EAAE,gBAAgB,EAAE,GAAG,IAAI,CAAC,gBAAiB,CAAC;QAEpD,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAC7C,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,CAC/C,CAAC;QAEF,IAAI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B,gBAAgB,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;aAC/C;YAED;;;;eAIG;YACH,IAAI,MAAe,CAAC;YACpB,IAAI;gBACF,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,eAAe,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;aACrD;oBAAS;gBACR,gBAAgB,CAAC,WAAW,EAAE,CAAC;aAChC;YAED,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;QAEF,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC"}
|
package/jest.config.cjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@proto-kit/common",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"version": "0.1.1-develop.0+19469b2",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -p tsconfig.json",
|
|
9
|
+
"dev": "tsc -p tsconfig.json --watch",
|
|
10
|
+
"lint": "eslint ./src ./test",
|
|
11
|
+
"test:file": "node --experimental-vm-modules --experimental-wasm-modules ../../node_modules/jest/bin/jest.js",
|
|
12
|
+
"test": "npm run test:file -- ./test/**",
|
|
13
|
+
"test:watch": "npm run test:file -- ./test/** --watch"
|
|
14
|
+
},
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"lodash": "^4.17.21",
|
|
21
|
+
"loglevel": "^1.8.1",
|
|
22
|
+
"reflect-metadata": "^0.1.13",
|
|
23
|
+
"typescript-memoize": "^1.1.1"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"o1js": "^1.6.0",
|
|
27
|
+
"tsyringe": "^4.7.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@jest/globals": "^29.5.0",
|
|
31
|
+
"@types/lodash": "^4.14.194"
|
|
32
|
+
},
|
|
33
|
+
"gitHead": "19469b285cd6953e6c621875d789959b521842c3"
|
|
34
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AreProofsEnabled,
|
|
3
|
+
CompileArtifact,
|
|
4
|
+
MOCK_VERIFICATION_KEY,
|
|
5
|
+
} from "../zkProgrammable/ZkProgrammable";
|
|
6
|
+
import { isSubtypeOfName } from "../utils";
|
|
7
|
+
import { TypedClass } from "../types";
|
|
8
|
+
import { log } from "../log";
|
|
9
|
+
|
|
10
|
+
export type ArtifactRecord = Record<string, CompileArtifact>;
|
|
11
|
+
|
|
12
|
+
export type CompileTarget = {
|
|
13
|
+
name: string;
|
|
14
|
+
compile: () => Promise<CompileArtifact>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export class AtomicCompileHelper {
|
|
18
|
+
public constructor(private readonly areProofsEnabled: AreProofsEnabled) {}
|
|
19
|
+
|
|
20
|
+
private compilationPromises: {
|
|
21
|
+
[key: string]: Promise<CompileArtifact>;
|
|
22
|
+
} = {};
|
|
23
|
+
|
|
24
|
+
public async compileContract(
|
|
25
|
+
contract: CompileTarget,
|
|
26
|
+
overrideProofsEnabled?: boolean
|
|
27
|
+
): Promise<CompileArtifact> {
|
|
28
|
+
let newPromise = false;
|
|
29
|
+
const { name } = contract;
|
|
30
|
+
if (this.compilationPromises[name] === undefined) {
|
|
31
|
+
const proofsEnabled =
|
|
32
|
+
overrideProofsEnabled ?? this.areProofsEnabled.areProofsEnabled;
|
|
33
|
+
|
|
34
|
+
// We only care about proofs enabled here if it's a contract, because
|
|
35
|
+
// in all other cases, ZkProgrammable already handles this switch, and we
|
|
36
|
+
// want to preserve the artifact layout (which might be more than one
|
|
37
|
+
// entry for ZkProgrammables)
|
|
38
|
+
if (
|
|
39
|
+
proofsEnabled ||
|
|
40
|
+
!isSubtypeOfName(
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
42
|
+
contract as unknown as TypedClass<any>,
|
|
43
|
+
"SmartContract"
|
|
44
|
+
)
|
|
45
|
+
) {
|
|
46
|
+
log.time(`Compiling ${name}`);
|
|
47
|
+
this.compilationPromises[name] = contract.compile();
|
|
48
|
+
newPromise = true;
|
|
49
|
+
} else {
|
|
50
|
+
log.trace(`Compiling ${name} - mock`);
|
|
51
|
+
this.compilationPromises[name] = Promise.resolve({
|
|
52
|
+
verificationKey: MOCK_VERIFICATION_KEY,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const result = await this.compilationPromises[name];
|
|
57
|
+
if (newPromise) {
|
|
58
|
+
log.timeEnd.info(`Compiling ${name}`);
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { inject, injectable, singleton } from "tsyringe";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AreProofsEnabled,
|
|
5
|
+
CompileArtifact,
|
|
6
|
+
} from "../zkProgrammable/ZkProgrammable";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
ArtifactRecord,
|
|
10
|
+
AtomicCompileHelper,
|
|
11
|
+
CompileTarget,
|
|
12
|
+
} from "./AtomicCompileHelper";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The CompileRegistry compiles "compilable modules"
|
|
16
|
+
* (i.e. zkprograms, contracts or contractmodules)
|
|
17
|
+
* while making sure they don't get compiled twice in the same process in parallel.
|
|
18
|
+
*/
|
|
19
|
+
@injectable()
|
|
20
|
+
@singleton()
|
|
21
|
+
export class CompileRegistry {
|
|
22
|
+
public constructor(
|
|
23
|
+
@inject("AreProofsEnabled")
|
|
24
|
+
private readonly areProofsEnabled: AreProofsEnabled
|
|
25
|
+
) {
|
|
26
|
+
this.compiler = new AtomicCompileHelper(this.areProofsEnabled);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private compiler: AtomicCompileHelper;
|
|
30
|
+
|
|
31
|
+
private artifacts: ArtifactRecord = {};
|
|
32
|
+
|
|
33
|
+
private inForceProverBlock = false;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* This function forces compilation even if the artifact itself is in the registry.
|
|
37
|
+
* Basically the statement is: The artifact along is not enough, we need to
|
|
38
|
+
* actually have the prover compiled.
|
|
39
|
+
* This is true for non-sideloaded circuit dependencies.
|
|
40
|
+
*/
|
|
41
|
+
public async forceProverExists(
|
|
42
|
+
f: (registry: CompileRegistry) => Promise<void>
|
|
43
|
+
) {
|
|
44
|
+
this.inForceProverBlock = true;
|
|
45
|
+
await f(this);
|
|
46
|
+
this.inForceProverBlock = false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public async compile(target: CompileTarget) {
|
|
50
|
+
if (this.artifacts[target.name] === undefined || this.inForceProverBlock) {
|
|
51
|
+
const artifact = await this.compiler.compileContract(target);
|
|
52
|
+
this.artifacts[target.name] = artifact;
|
|
53
|
+
return artifact;
|
|
54
|
+
}
|
|
55
|
+
return this.artifacts[target.name];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public getArtifact(name: string): CompileArtifact | undefined {
|
|
59
|
+
if (this.artifacts[name] === undefined) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Artifact for ${name} not available, did you compile it via the CompileRegistry?`
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return this.artifacts[name];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public addArtifactsRaw(artifacts: ArtifactRecord) {
|
|
69
|
+
this.artifacts = {
|
|
70
|
+
...this.artifacts,
|
|
71
|
+
...artifacts,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public getAllArtifacts() {
|
|
76
|
+
return this.artifacts;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { injectable, Lifecycle, scoped } from "tsyringe";
|
|
2
|
+
|
|
3
|
+
import { CompileRegistry } from "../CompileRegistry";
|
|
4
|
+
|
|
5
|
+
@injectable()
|
|
6
|
+
@scoped(Lifecycle.ContainerScoped)
|
|
7
|
+
export class ChildVerificationKeyService {
|
|
8
|
+
private compileRegistry?: CompileRegistry;
|
|
9
|
+
|
|
10
|
+
public setCompileRegistry(registry: CompileRegistry) {
|
|
11
|
+
this.compileRegistry = registry;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public getVerificationKey(name: string) {
|
|
15
|
+
if (this.compileRegistry === undefined) {
|
|
16
|
+
throw new Error("CompileRegistry hasn't been set yet");
|
|
17
|
+
}
|
|
18
|
+
const artifact = this.compileRegistry.getArtifact(name);
|
|
19
|
+
if (artifact === undefined) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Verification Key for child program ${name} not found in registry`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return artifact.verificationKey;
|
|
25
|
+
}
|
|
26
|
+
}
|