Skip to content

Commit

Permalink
Add verifierNameSuffix parameter into the createVerifier function (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrom131 authored Nov 20, 2024
1 parent dfbfdc6 commit 7c78dc0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ The `implementer` is the instance of a certain proving system. Currently `groth1

---

- **`async createVerifier("sol" | "vy")`**
- **`async createVerifier("sol" | "vy", verifierNameSuffix?: string)`**

Creates a Solidity | Vyper verifier contract on `verifierDirPath` path, which was specified in the config.
Creates a Solidity | Vyper verifier contract with the optional `verifierNameSuffix` on `verifierDirPath` path, which was specified in the config.

```typescript
await circuit.createVerifier("sol");
await circuit.createVerifier("sol", "_suffix_");
```

- **`async calculateWitness(inputs) -> bigint[]`**
Expand Down Expand Up @@ -101,12 +102,12 @@ const calldata = await circuit.generateCalldata(proof);

Returns the name of the circuit from the config.

- **`getVerifierName() -> string`**
- **`getVerifierName(verifierNameSuffix?: string) -> string`**

Returns the name of the verifier in the following form:

```typescript
<Circuit name><Proving system>Verifier
<Circuit name><Suffix><Proving system>Verifier
```

- **`getProvingSystemType() -> "groth16" | "plonk"`**
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/zkit",
"version": "0.3.1",
"version": "0.3.2",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
21 changes: 14 additions & 7 deletions src/core/CircuitZKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ export class CircuitZKit<Type extends ProvingSystemType> {
) {}

/**
* Creates a verifier contract for the specified contract language.
* Creates a verifier contract for the specified contract language with optional name suffix.
* For more details regarding the structure of the contract verifier name, see {@link getVerifierName} description.
*
* @param {VerifierLanguageType} languageExtension - The verifier contract language extension.
* @param {string} verifierNameSuffix - The optional verifier name suffix.
*/
public async createVerifier(languageExtension: VerifierLanguageType): Promise<void> {
public async createVerifier(languageExtension: VerifierLanguageType, verifierNameSuffix?: string): Promise<void> {
const vKeyFilePath: string = this.mustGetArtifactsFilePath("vkey");
const verifierFilePath = path.join(
this._config.verifierDirPath,
`${this._implementer.getVerifierName(this._config.circuitName)}.${languageExtension}`,
`${this.getVerifierName(verifierNameSuffix)}.${languageExtension}`,
);

this._implementer.createVerifier(this._config.circuitName, vKeyFilePath, verifierFilePath, languageExtension);
this._implementer.createVerifier(vKeyFilePath, verifierFilePath, languageExtension);
}

/**
Expand Down Expand Up @@ -104,12 +108,15 @@ export class CircuitZKit<Type extends ProvingSystemType> {
}

/**
* Returns the verifier name. The verifier name is the name of the circuit file without the extension, suffixed with "Verifier".
* Returns the verifier name. The verifier name has the next structure:
* `<template name><suffix><proving system>Verifier.<extension>`.
*
* @param {string} verifierNameSuffix - The optional verifier name suffix.
*
* @returns {string} The verifier name.
*/
public getVerifierName(): string {
return this._implementer.getVerifierName(this._config.circuitName);
public getVerifierName(verifierNameSuffix?: string): string {
return this._implementer.getVerifierName(this._config.circuitName, verifierNameSuffix);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/core/protocols/AbstractImplementer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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,
Expand All @@ -25,7 +24,7 @@ export abstract class AbstractProtocolImplementer<T extends ProvingSystemType> i
}

const templateParams = JSON.parse(fs.readFileSync(vKeyFilePath, "utf-8"));
templateParams["verifier_id"] = this.getVerifierName(circuitName);
templateParams["verifier_id"] = path.parse(verifierFilePath).name;

const verifierCode = ejs.render(verifierTemplate, templateParams);

Expand All @@ -51,10 +50,11 @@ export abstract class AbstractProtocolImplementer<T extends ProvingSystemType> i
);
}

public getVerifierName(circuitName: string): string {
public getVerifierName(circuitName: string, verifierNameSuffix?: string): string {
const protocolType: ProvingSystemType = this.getProvingSystemType();
const nameSuffix: string = verifierNameSuffix ?? "";

return `${circuitName}${protocolType.charAt(0).toUpperCase() + protocolType.slice(1)}Verifier`;
return `${circuitName}${nameSuffix}${protocolType.charAt(0).toUpperCase() + protocolType.slice(1)}Verifier`;
}

public getZKeyFileName(circuitName: string): string {
Expand Down
3 changes: 1 addition & 2 deletions src/types/protocols/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from "./plonk";

export interface IProtocolImplementer<T extends ProvingSystemType> {
createVerifier(
circuitName: string,
vKeyFilePath: string,
verifierFilePath: string,
languageExtension: VerifierLanguageType,
Expand All @@ -25,7 +24,7 @@ export interface IProtocolImplementer<T extends ProvingSystemType> {

getTemplate(fileExtension: VerifierLanguageType): string;

getVerifierName(circuitName: string): string;
getVerifierName(circuitName: string, verifierNameSuffix?: string): string;

getZKeyFileName(circuitName: string): string;

Expand Down
37 changes: 37 additions & 0 deletions test/CircuitZKit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,43 @@ describe("CircuitZKit", () => {
expect(fs.readFileSync(expectedVerifierFilePath, "utf-8")).to.be.eq(ejs.render(template, templateParams));
});

it("should correctly create verifier file with the name suffix", async () => {
const circuitName = "Multiplier";
const verifierDirPath = getVerifiersDirFullPath();
const artifactsDirFullPath = getArtifactsFullPath(`${circuitName}.circom`);
const protocolType: ProvingSystemType = "plonk";

const multiplierCircuit = getCircuitZKit<"plonk">(circuitName, "plonk", {
circuitName,
circuitArtifactsPath: artifactsDirFullPath,
verifierDirPath,
});

const nameSuffix: string = "_2_3_";

expect(multiplierCircuit.getVerifierName(nameSuffix)).to.be.eq(`${circuitName}${nameSuffix}PlonkVerifier`);

const expectedVerifierFilePath = path.join(
verifierDirPath,
`${multiplierCircuit.getVerifierName(nameSuffix)}.sol`,
);

expect(fs.existsSync(expectedVerifierFilePath)).to.be.false;

await multiplierCircuit.createVerifier("sol", nameSuffix);

expect(fs.existsSync(expectedVerifierFilePath)).to.be.true;

const expectedVKeyFilePath = path.join(artifactsDirFullPath, `${circuitName}.${protocolType}.vkey.json`);
expect(multiplierCircuit.getArtifactsFilePath("vkey")).to.be.eq(expectedVKeyFilePath);

const template = multiplierCircuit.getVerifierTemplate("sol");
const templateParams = JSON.parse(fs.readFileSync(expectedVKeyFilePath, "utf-8"));
templateParams["verifier_id"] = multiplierCircuit.getVerifierName(nameSuffix);

expect(fs.readFileSync(expectedVerifierFilePath, "utf-8")).to.be.eq(ejs.render(template, templateParams));
});

it("should correctly create verifier and verify 'groth16' proof", async function () {
const circuitName = "Multiplier";
const verifierDirPath = getVerifiersDirFullPath();
Expand Down

0 comments on commit 7c78dc0

Please sign in to comment.