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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
85 changes: 41 additions & 44 deletions src/lib/proof-system/zkprogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,41 +183,40 @@ let SideloadedTag = {
},
};

function ZkProgram<
Config extends {
publicInput?: ProvableType;
publicOutput?: ProvableType;
methods: {
[I in string]: {
privateInputs: Tuple<PrivateInput>;
auxiliaryOutput?: ProvableType;
};
type ConfigBaseType = {
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'>;
}
>(
};
};

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]
>;
};

function ZkProgram<Config extends ConfigBaseType>(
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 @@ -247,10 +246,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 @@ -264,10 +263,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 @@ -581,15 +585,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>>;

class SelfProof<PublicInput, PublicOutput> extends Proof<
PublicInput,
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