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

Added calculateWitness method #9

Merged
merged 7 commits into from
Jul 30, 2024
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
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.2.3",
"version": "0.2.4",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
28 changes: 25 additions & 3 deletions src/core/CircuitZKit.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import ejs from "ejs";
import fs from "fs";
import * as os from "os";
import path from "path";
import * as snarkjs from "snarkjs";

import {
ArtifactsFileType,
Calldata,
CircuitZKitConfig,
Inputs,
Signals,
ProofStruct,
VerifierTemplateType,
} from "../types/circuit-zkit";
Expand Down Expand Up @@ -54,16 +55,37 @@ export class CircuitZKit {
fs.writeFileSync(verifierFilePath, verifierCode, "utf-8");
}

/**
* Calculates a witness for the given inputs.
*
* @param {Signals} inputs - The inputs for the circuit.
* @returns {Promise<bigint[]>} The generated witness.
*/
public async calculateWitness(inputs: Signals): Promise<bigint[]> {
const tmpDir = path.join(os.tmpdir(), ".zkit");

if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, { recursive: true });
}

const wtnsFile = path.join(tmpDir, `${this.getCircuitName()}.wtns`);
const wasmFile = this.mustGetArtifactsFilePath("wasm");

await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);

return (await snarkjs.wtns.exportJson(wtnsFile)) as bigint[];
}

/**
* Generates a proof for the given inputs.
*
* @dev The `inputs` should be in the same order as the circuit expects them.
*
* @param {Inputs} inputs - The inputs for the circuit.
* @param {Signals} inputs - The inputs for the circuit.
* @returns {Promise<ProofStruct>} The generated proof.
* @todo Add support for other proving systems.
*/
public async generateProof(inputs: Inputs): Promise<ProofStruct> {
public async generateProof(inputs: Signals): Promise<ProofStruct> {
const zKeyFile = this.mustGetArtifactsFilePath("zkey");
const wasmFile = this.mustGetArtifactsFilePath("wasm");

Expand Down
7 changes: 3 additions & 4 deletions src/types/circuit-zkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ export type ProofStruct = {
publicSignals: PublicSignals;
};

export type NumberLike = number | bigint | string;
export type NumberLike = number | bigint | `${number}`;
export type ArrayLike = NumberLike[] | ArrayLike[];
export type InputLike = NumberLike | ArrayLike;

export type Inputs = Record<string, InputLike>;
export type Signal = NumberLike | ArrayLike;
export type Signals = Record<string, Signal>;

export type ArtifactsFileType = "r1cs" | "zkey" | "vkey" | "sym" | "json" | "wasm";
export type VerifierTemplateType = "groth16";
Expand Down
29 changes: 28 additions & 1 deletion test/CircuitZKit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ejs from "ejs";
import path from "path";
import fs from "fs";
import * as os from "os";
import path from "path";

import { expect } from "chai";
import { useFixtureProject } from "./helpers";
Expand Down Expand Up @@ -177,6 +178,32 @@ describe("CircuitZKit", () => {
});
});

describe("calculateWitness", () => {
useFixtureProject("simple-circuits");

it("should correctly create witness", async () => {
const circuitName = "Multiplier";
const circuitArtifactsPath = getArtifactsFullPath(`${circuitName}.circom`);

const multiplierCircuit: CircuitZKit = new CircuitZKit({
circuitName,
circuitArtifactsPath,
verifierDirPath: getVerifiersDirFullPath(),
});

const b = 10,
a = 20;

const tmpDir = path.join(os.tmpdir(), ".zkit");

fs.rmSync(tmpDir, { force: true, recursive: true });

expect(await multiplierCircuit.calculateWitness({ a, b })).to.deep.eq([1n, 200n, 20n, 10n]);
expect(await multiplierCircuit.calculateWitness({ a, b })).to.deep.eq([1n, 200n, 20n, 10n]);
expect(await multiplierCircuit.calculateWitness({ a, b: 30 })).to.deep.eq([1n, 600n, 20n, 30n]);
});
});

describe("generateProof/verifyProof", () => {
useFixtureProject("simple-circuits");

Expand Down
Loading