From 4de57a717ad6129ad2e92406b19e664bb366270f Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Fri, 20 Dec 2024 14:41:00 +0100 Subject: [PATCH] Fixed correctness issue in ZkProgram typing --- src/lib/proof-system/zkprogram.ts | 96 +++++++++++++++------------- src/lib/testing/constraint-system.ts | 2 +- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index b6e39ee960..18f646594a 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -184,41 +184,40 @@ let SideloadedTag = { }, }; -function ZkProgram< - Config extends { - publicInput?: ProvableType; - publicOutput?: ProvableType; - methods: { - [I in string]: { - privateInputs: Tuple; - auxiliaryOutput?: ProvableType; - }; +type ConfigBaseType = { + publicInput?: ProvableType; + publicOutput?: ProvableType; + methods: { + [I in string]: { + privateInputs: Tuple; + auxiliaryOutput?: ProvableType; }; - }, - Methods extends { - [I in keyof Config['methods']]: Method< - InferProvableOrUndefined>, - InferProvableOrVoid>, - Config['methods'][I] - >; - }, - // derived types for convenience - MethodSignatures extends Config['methods'] = Config['methods'], - PrivateInputs extends { - [I in keyof Config['methods']]: Config['methods'][I]['privateInputs']; - } = { - [I in keyof Config['methods']]: Config['methods'][I]['privateInputs']; - }, - AuxiliaryOutputs extends { - [I in keyof MethodSignatures]: Get; - } = { - [I in keyof MethodSignatures]: Get; - } ->( + }; +}; + +type InferMethodSignatures = Config['methods']; +type InferPrivateInput = { + [I in keyof Config['methods']]: Config['methods'][I]['privateInputs']; +}; +type InferAuxilaryOutputs = { + [I in keyof InferMethodSignatures]: Get< + InferMethodSignatures[I], + 'auxiliaryOutput' + >; +}; +type InferMethodType = { + [I in keyof Config['methods']]: Method< + InferProvableOrUndefined>, + InferProvableOrVoid>, + Config['methods'][I] + >; +}; + +function ZkProgram( config: Config & { name: string; methods: { - [I in keyof Config['methods']]: Methods[I]; + [I in keyof Config['methods']]: InferMethodType[I]; }; overrideWrapDomain?: 0 | 1 | 2; } @@ -248,10 +247,10 @@ function ZkProgram< publicInputType: ProvableOrUndefined>; publicOutputType: ProvableOrVoid>; - privateInputTypes: PrivateInputs; - auxiliaryOutputTypes: AuxiliaryOutputs; + privateInputTypes: InferPrivateInput; + auxiliaryOutputTypes: InferAuxilaryOutputs; rawMethods: { - [I in keyof Config['methods']]: Methods[I]['method']; + [I in keyof Config['methods']]: InferMethodType[I]['method']; }; Proof: typeof Proof< @@ -265,10 +264,26 @@ function ZkProgram< [I in keyof Config['methods']]: Prover< InferProvableOrUndefined>, InferProvableOrVoid>, - PrivateInputs[I], - InferProvableOrUndefined + InferPrivateInput[I], + InferProvableOrUndefined[I]> >; } { + type Methods = { + [I in keyof Config['methods']]: Method< + InferProvableOrUndefined>, + InferProvableOrVoid>, + Config['methods'][I] + >; + }; + // derived types for convenience + type MethodSignatures = Config['methods']; + type PrivateInputs = { + [I in keyof Config['methods']]: Config['methods'][I]['privateInputs']; + }; + type AuxiliaryOutputs = { + [I in keyof MethodSignatures]: Get; + }; + let doProving = true; let methods = config.methods; @@ -582,15 +597,8 @@ type ZkProgram< auxiliaryOutput?: ProvableType; }; }; - }, - Methods extends { - [I in keyof Config['methods']]: Method< - InferProvableOrUndefined>, - InferProvableOrVoid>, - Config['methods'][I] - >; } -> = ReturnType>; +> = ReturnType>; class SelfProof extends Proof< PublicInput, diff --git a/src/lib/testing/constraint-system.ts b/src/lib/testing/constraint-system.ts index 13a7d952c5..996275b520 100644 --- a/src/lib/testing/constraint-system.ts +++ b/src/lib/testing/constraint-system.ts @@ -114,7 +114,7 @@ constraintSystem.fromZkProgram = function fromZkProgram< methodName: K, test: ConstraintSystemTest ) { - let program_: ZkProgram = program as any; + let program_: ZkProgram = program as any; let from: any = [...program_.privateInputTypes[methodName]]; if (program_.publicInputType !== Undefined) { from.unshift(program_.publicInputType);