From 4de57a717ad6129ad2e92406b19e664bb366270f Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Fri, 20 Dec 2024 14:41:00 +0100 Subject: [PATCH 1/3] 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); From 464f170342bd134d19441063305e66cc40bc82c4 Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Fri, 20 Dec 2024 16:45:34 +0100 Subject: [PATCH 2/3] Refactored inline types to also use defined inferring types --- src/lib/proof-system/zkprogram.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index 18f646594a..a930645672 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -199,7 +199,7 @@ type InferMethodSignatures = Config['methods']; type InferPrivateInput = { [I in keyof Config['methods']]: Config['methods'][I]['privateInputs']; }; -type InferAuxilaryOutputs = { +type InferAuxiliaryOutputs = { [I in keyof InferMethodSignatures]: Get< InferMethodSignatures[I], 'auxiliaryOutput' @@ -248,7 +248,7 @@ function ZkProgram( publicInputType: ProvableOrUndefined>; publicOutputType: ProvableOrVoid>; privateInputTypes: InferPrivateInput; - auxiliaryOutputTypes: InferAuxilaryOutputs; + auxiliaryOutputTypes: InferAuxiliaryOutputs; rawMethods: { [I in keyof Config['methods']]: InferMethodType[I]['method']; }; @@ -265,7 +265,7 @@ function ZkProgram( InferProvableOrUndefined>, InferProvableOrVoid>, InferPrivateInput[I], - InferProvableOrUndefined[I]> + InferProvableOrUndefined[I]> >; } { type Methods = { @@ -276,13 +276,8 @@ function ZkProgram( >; }; // 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; - }; + type PrivateInputs = InferPrivateInput + type AuxiliaryOutputs = InferAuxiliaryOutputs; let doProving = true; From 5f16af4a88ff1026f15075e2f06df216aaf27ece Mon Sep 17 00:00:00 2001 From: Raphael Panic Date: Fri, 20 Dec 2024 16:49:28 +0100 Subject: [PATCH 3/3] Refactored inline types pt. 2 --- src/lib/proof-system/zkprogram.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/lib/proof-system/zkprogram.ts b/src/lib/proof-system/zkprogram.ts index a930645672..616bcec903 100644 --- a/src/lib/proof-system/zkprogram.ts +++ b/src/lib/proof-system/zkprogram.ts @@ -268,14 +268,8 @@ function ZkProgram( InferProvableOrUndefined[I]> >; } { - type Methods = { - [I in keyof Config['methods']]: Method< - InferProvableOrUndefined>, - InferProvableOrVoid>, - Config['methods'][I] - >; - }; // derived types for convenience + type Methods = InferMethodSignatures; type PrivateInputs = InferPrivateInput type AuxiliaryOutputs = InferAuxiliaryOutputs;