-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add plonk support * Update package version * added vyper plonk verifier template * fixed P_TOTAL_SIZE in vyper plonk verifier template * template fix * modified vyper plonk verifier template --------- Co-authored-by: mllwchrry <[email protected]>
- Loading branch information
Showing
28 changed files
with
1,971 additions
and
183 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import fs from "fs"; | ||
import ejs from "ejs"; | ||
import path from "path"; | ||
|
||
import { Signals } from "../../types/proof-utils"; | ||
import { | ||
IProtocolImplementer, | ||
ProvingSystemType, | ||
ProofStructByProtocol, | ||
CalldataByProtocol, | ||
} from "../../types/protocols"; | ||
import { VerifierLanguageType } from "../../types/circuit-zkit"; | ||
|
||
export abstract class AbstractProtocolImplementer<T extends ProvingSystemType> implements IProtocolImplementer<T> { | ||
public async createVerifier( | ||
circuitName: string, | ||
vKeyFilePath: string, | ||
verifierFilePath: string, | ||
languageExtension: VerifierLanguageType, | ||
): Promise<void> { | ||
const verifierTemplate: string = this.getTemplate(languageExtension); | ||
|
||
if (!fs.existsSync(path.dirname(verifierFilePath))) { | ||
fs.mkdirSync(path.dirname(verifierFilePath), { recursive: true }); | ||
} | ||
|
||
const templateParams = JSON.parse(fs.readFileSync(vKeyFilePath, "utf-8")); | ||
templateParams["verifier_id"] = this.getVerifierName(circuitName); | ||
|
||
const verifierCode = ejs.render(verifierTemplate, templateParams); | ||
|
||
fs.writeFileSync(verifierFilePath, verifierCode, "utf-8"); | ||
} | ||
|
||
public abstract generateProof( | ||
inputs: Signals, | ||
zKeyFilePath: string, | ||
wasmFilePath: string, | ||
): Promise<ProofStructByProtocol<T>>; | ||
|
||
public abstract verifyProof(proof: ProofStructByProtocol<T>, vKeyFilePath: string): Promise<boolean>; | ||
|
||
public abstract generateCalldata(proof: ProofStructByProtocol<T>): Promise<CalldataByProtocol<T>>; | ||
|
||
public abstract getProvingSystemType(): ProvingSystemType; | ||
|
||
public getTemplate(languageExtension: VerifierLanguageType): string { | ||
return fs.readFileSync( | ||
path.join(__dirname, "..", "templates", `verifier_${this.getProvingSystemType()}.${languageExtension}.ejs`), | ||
"utf8", | ||
); | ||
} | ||
|
||
public getVerifierName(circuitName: string): string { | ||
const protocolType: ProvingSystemType = this.getProvingSystemType(); | ||
|
||
return `${circuitName}${protocolType.charAt(0).toUpperCase() + protocolType.slice(1)}Verifier`; | ||
} | ||
|
||
public getZKeyFileName(circuitName: string): string { | ||
return `${circuitName}.${this.getProvingSystemType()}.zkey`; | ||
} | ||
|
||
public getVKeyFileName(circuitName: string): string { | ||
return `${circuitName}.${this.getProvingSystemType()}.vkey.json`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import fs from "fs"; | ||
import * as snarkjs from "snarkjs"; | ||
|
||
import { AbstractProtocolImplementer } from "./AbstractImplementer"; | ||
|
||
import { Signals } from "../../types/proof-utils"; | ||
import { Groth16ProofStruct, ProvingSystemType, Groth16Calldata } from "../../types/protocols"; | ||
|
||
export class Groth16Implementer extends AbstractProtocolImplementer<"groth16"> { | ||
public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<Groth16ProofStruct> { | ||
return (await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath)) as Groth16ProofStruct; | ||
} | ||
|
||
public async verifyProof(proof: Groth16ProofStruct, vKeyFilePath: string): Promise<boolean> { | ||
const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString()); | ||
|
||
return await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof); | ||
} | ||
|
||
public async generateCalldata(proof: Groth16ProofStruct): Promise<Groth16Calldata> { | ||
const calldata = await snarkjs.groth16.exportSolidityCallData(proof.proof, proof.publicSignals); | ||
|
||
return JSON.parse(`[${calldata}]`) as Groth16Calldata; | ||
} | ||
|
||
public getProvingSystemType(): ProvingSystemType { | ||
return "groth16"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import fs from "fs"; | ||
import * as snarkjs from "snarkjs"; | ||
|
||
import { AbstractProtocolImplementer } from "./AbstractImplementer"; | ||
|
||
import { Signals } from "../../types/proof-utils"; | ||
import { PlonkCalldata, PlonkProofStruct, ProvingSystemType } from "../../types/protocols"; | ||
|
||
export class PlonkImplementer extends AbstractProtocolImplementer<"plonk"> { | ||
public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<PlonkProofStruct> { | ||
return (await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath)) as PlonkProofStruct; | ||
} | ||
|
||
public async verifyProof(proof: PlonkProofStruct, vKeyFilePath: string): Promise<boolean> { | ||
const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString()); | ||
|
||
return await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof); | ||
} | ||
|
||
public async generateCalldata(proof: PlonkProofStruct): Promise<PlonkCalldata> { | ||
const calldata = await snarkjs.plonk.exportSolidityCallData(proof.proof, proof.publicSignals); | ||
const proofArrEndIndex: number = calldata.indexOf("]") + 1; | ||
|
||
return JSON.parse( | ||
`[${calldata.slice(0, proofArrEndIndex)},${calldata.slice(proofArrEndIndex, calldata.length)}]`, | ||
) as PlonkCalldata; | ||
} | ||
|
||
public getProvingSystemType(): ProvingSystemType { | ||
return "plonk"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { AbstractProtocolImplementer } from "./AbstractImplementer"; | ||
export { Groth16Implementer } from "./Groth16Implementer"; | ||
export { PlonkImplementer } from "./PlonkImplementer"; |
Oops, something went wrong.