Skip to content

Commit

Permalink
final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dovgopoly committed May 9, 2024
1 parent 1dd1728 commit 4d8cb0c
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 50 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
To install dependencies:

```bash
bun install
npm install
```

To run:
To run tests:

```bash
bun run index.ts
npm run test
```

This project was created using `bun init` in bun v1.1.4. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
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.0.1",
"version": "0.1.0",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
27 changes: 6 additions & 21 deletions src/CircomZKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import path from "path";
import { CircuitZKit } from "./CircuitZKit";
import { ManagerZKit } from "./ManagerZKit";
import { CircuitInfo } from "./types";
import { readDirRecursively } from "./utils";
import { resolveInclude } from "ejs";

export class CircomZKit {
constructor(private readonly _manager: ManagerZKit) {}
Expand Down Expand Up @@ -66,28 +68,11 @@ export class CircomZKit {

let circuits = [] as string[];

const getAllCircuits = (dir: string) => {
if (!fs.existsSync(dir)) {
return;
readDirRecursively(circuitsDir, (dir: string, file: string) => {
if (path.extname(file) == ".circom") {
circuits.push(path.relative(circuitsDir, file));
}

/// @dev After NodeJS v20 `recursive` flag can be passed
const entries = fs.readdirSync(dir, { withFileTypes: true });

for (const entry of entries) {
const entryPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
getAllCircuits(entryPath);
}

if (entry.isFile() && path.extname(entry.name) == ".circom") {
circuits.push(path.relative(this._manager.getCircuitsDir(), entryPath));
}
}
};

getAllCircuits(circuitsDir);
});

return circuits;
}
Expand Down
13 changes: 9 additions & 4 deletions src/CircuitZKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as snarkjs from "snarkjs";
import { defaultCompileOptions } from "./defaults";
import { ManagerZKit } from "./ManagerZKit";
import { Calldata, CompileOptions, DirType, FileType, Inputs, ProofStruct } from "./types";
import { readDirRecursively } from "./utils";

const { CircomRunner, bindings } = require("@distributedlab/circom2");

Expand Down Expand Up @@ -228,11 +229,15 @@ export class CircuitZKit {
fs.rmSync(outDir, { recursive: true, force: true });
fs.mkdirSync(outDir, { recursive: true });

fs.readdirSync(tempDir).forEach((entry) => {
const sourcePath = path.join(tempDir, entry);
const destPath = path.join(outDir, entry);
readDirRecursively(tempDir, (dir: string, file: string) => {
const correspondingOutDir = path.join(outDir, path.relative(tempDir, dir));
const correspondingOutFile = path.join(outDir, path.relative(tempDir, file));

fs.renameSync(sourcePath, destPath);
if (!fs.existsSync(correspondingOutDir)) {
fs.mkdirSync(correspondingOutDir);
}

fs.copyFileSync(file, correspondingOutFile);
});
}

Expand Down
25 changes: 13 additions & 12 deletions src/ManagerZKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ export class ManagerZKit {

const isGlobalPtau = !config.ptauFile;

const tempDir = path.join(os.homedir(), ".zkit");

const ptauPath = isGlobalPtau ? path.join(tempDir, ".ptau") : path.join(projectRoot, config.ptauFile!);

if (!isGlobalPtau && path.extname(ptauPath) != ".ptau") {
if (!isGlobalPtau && path.extname(config.ptauFile!) != ".ptau") {
throw new Error('Ptau file must have ".ptau" extension.');
}

const tempDir = path.join(os.tmpdir(), ".zkit");
const ptauPath = isGlobalPtau
? path.join(os.homedir(), ".zkit", ".ptau")
: path.join(projectRoot, config.ptauFile!);

this._config = {
circuits: path.join(projectRoot, config.circuits ?? "circuits"),
artifacts: path.join(projectRoot, config.artifacts ?? "zkit-artifacts"),
verifiers: path.join(projectRoot, config.verifiers ?? "contracts/verifiers"),
circuitsDir: path.join(projectRoot, config.circuits ?? "circuits"),
artifactsDir: path.join(projectRoot, config.artifacts ?? "zkit-artifacts"),
verifiersDir: path.join(projectRoot, config.verifiers ?? "contracts/verifiers"),
tempDir,
ptau: {
isGlobal: isGlobalPtau,
Expand All @@ -49,15 +50,15 @@ export class ManagerZKit {
}

public getArtifactsDir(): string {
return this._config.artifacts;
return this._config.artifactsDir;
}

public getCircuitsDir(): string {
return this._config.circuits;
return this._config.circuitsDir;
}

public getVerifiersDir(): string {
return this._config.verifiers;
return this._config.verifiersDir;
}

public getPtauPath(): string {
Expand All @@ -69,7 +70,7 @@ export class ManagerZKit {
}

public getTempDir(): string {
return path.join(os.homedir(), ".zkit", uuid());
return path.join(this._config.tempDir, uuid());
}

public getTemplate(templateType: TemplateType): string {
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ export * from "./CircomZKit";
export * from "./CircuitZKit";
export * from "./ManagerZKit";

export * from "./types";
export {
NumericString,
PublicSignals,
Groth16Proof,
Calldata,
ProofStruct,
Inputs,
ManagerZKitConfig,
CompileOptions,
CircuitInfo,
} from "./types";

export * from "./defaults";
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export type ManagerZKitConfig = {
};

export type ManagerZKitPrivateConfig = {
circuits: string;
artifacts: string;
verifiers: string;
circuitsDir: string;
artifactsDir: string;
verifiersDir: string;
tempDir: string;
ptau: {
isGlobal: boolean;
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from "fs";
import path from "path";

/// @dev After Node.js v20 `recursive` flag can be passed to `fs.readdir`
export function readDirRecursively(dir: string, callback: (dir: string, file: string) => void): void {
if (!fs.existsSync(dir)) {
return;
}

const entries = fs.readdirSync(dir, { withFileTypes: true });

for (const entry of entries) {
const entryPath = path.join(dir, entry.name);

if (entry.isDirectory()) {
readDirRecursively(entryPath, callback);
}

if (entry.isFile()) {
callback(dir, entryPath);
}
}
}
2 changes: 1 addition & 1 deletion test/CircomZKit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("happy flow", function () {
const manager = new ManagerZKit({
circuits: "test/circuits",
artifacts: "test/zkit-artifacts",
verifiers: "test/verifiers",
verifiers: "test/verifiers"
});

const circom = new CircomZKit(manager);
Expand Down

0 comments on commit 4d8cb0c

Please sign in to comment.