Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix correctness issue in ZkProgram typing #1962

Merged
merged 6 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions src/lib/proof-system/zkprogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,35 @@ let SideloadedTag = {
},
};

type ConfigBaseType = {
publicInput?: ProvableType;
publicOutput?: ProvableType;
methods: {
[I in string]: {
privateInputs: Tuple<PrivateInput>;
auxiliaryOutput?: ProvableType;
};
};
};

type InferMethodSignatures<Config extends ConfigBaseType> = Config['methods'];
type InferPrivateInput<Config extends ConfigBaseType> = {
[I in keyof Config['methods']]: Config['methods'][I]['privateInputs'];
};
type InferAuxiliaryOutputs<Config extends ConfigBaseType> = {
[I in keyof InferMethodSignatures<Config>]: Get<
InferMethodSignatures<Config>[I],
'auxiliaryOutput'
>;
};
type InferMethodType<Config extends ConfigBaseType> = {
[I in keyof Config['methods']]: Method<
InferProvableOrUndefined<Get<Config, 'publicInput'>>,
InferProvableOrVoid<Get<Config, 'publicOutput'>>,
Config['methods'][I]
>;
};

/**
* Wraps config + provable code into a program capable of producing {@link Proof}s.
*
Expand Down Expand Up @@ -229,40 +258,13 @@ let SideloadedTag = {
* @returns an object that can be used to compile, prove, and verify the program.
*/
function ZkProgram<
Config extends {
publicInput?: ProvableType;
publicOutput?: ProvableType;
methods: {
[I in string]: {
privateInputs: Tuple<PrivateInput>;
auxiliaryOutput?: ProvableType;
};
};
},
Methods extends {
[I in keyof Config['methods']]: Method<
InferProvableOrUndefined<Get<Config, 'publicInput'>>,
InferProvableOrVoid<Get<Config, 'publicOutput'>>,
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<MethodSignatures[I], 'auxiliaryOutput'>;
} = {
[I in keyof MethodSignatures]: Get<MethodSignatures[I], 'auxiliaryOutput'>;
}
Config extends ConfigBaseType,
_ extends unknown = unknown // weird hack that makes methods infer correctly when their inputs are not annotated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this extra line fixed the type error in foreign-field-gadgets.unit-test.ts

I also noticed that you could remove the errors when explicitly annotating method inputs in that file. Which would be good practice, since if you don't do it the inputs will be typed as any inside the method.

however, I also found that adding any extra generic type argument makes the error disappear. no idea why!
since writing a zkprogram is already hard, because you tend to get confusing type errors on an incomplete ZkProgram definition, I think it's worth keeping this hack to remove one more confusing error.

>(
config: Config & {
name: string;
methods: {
[I in keyof Config['methods']]: Methods[I];
[I in keyof Config['methods']]: InferMethodType<Config>[I];
};
overrideWrapDomain?: 0 | 1 | 2;
}
Expand Down Expand Up @@ -292,10 +294,10 @@ function ZkProgram<

publicInputType: ProvableOrUndefined<Get<Config, 'publicInput'>>;
publicOutputType: ProvableOrVoid<Get<Config, 'publicOutput'>>;
privateInputTypes: PrivateInputs;
auxiliaryOutputTypes: AuxiliaryOutputs;
privateInputTypes: InferPrivateInput<Config>;
auxiliaryOutputTypes: InferAuxiliaryOutputs<Config>;
rawMethods: {
[I in keyof Config['methods']]: Methods[I]['method'];
[I in keyof Config['methods']]: InferMethodType<Config>[I]['method'];
};

Proof: typeof Proof<
Expand All @@ -309,10 +311,15 @@ function ZkProgram<
[I in keyof Config['methods']]: Prover<
InferProvableOrUndefined<Get<Config, 'publicInput'>>,
InferProvableOrVoid<Get<Config, 'publicOutput'>>,
PrivateInputs[I],
InferProvableOrUndefined<AuxiliaryOutputs[I]>
InferPrivateInput<Config>[I],
InferProvableOrUndefined<InferAuxiliaryOutputs<Config>[I]>
>;
} {
// derived types for convenience
type Methods = InferMethodSignatures<Config>;
type PrivateInputs = InferPrivateInput<Config>;
type AuxiliaryOutputs = InferAuxiliaryOutputs<Config>;

let doProving = true;

let methods = config.methods;
Expand Down Expand Up @@ -626,15 +633,8 @@ type ZkProgram<
auxiliaryOutput?: ProvableType;
};
};
},
Methods extends {
[I in keyof Config['methods']]: Method<
InferProvableOrUndefined<Get<Config, 'publicInput'>>,
InferProvableOrVoid<Get<Config, 'publicOutput'>>,
Config['methods'][I]
>;
}
> = ReturnType<typeof ZkProgram<Config, Methods>>;
> = ReturnType<typeof ZkProgram<Config>>;

/**
* A class representing the type of Proof produced by the {@link ZkProgram} in which it is used.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/testing/constraint-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ constraintSystem.fromZkProgram = function fromZkProgram<
methodName: K,
test: ConstraintSystemTest
) {
let program_: ZkProgram<any, any> = program as any;
let program_: ZkProgram<any> = program as any;
let from: any = [...program_.privateInputTypes[methodName]];
if (program_.publicInputType !== Undefined) {
from.unshift(program_.publicInputType);
Expand Down
Loading