Skip to content

Commit

Permalink
v0.0.8: add auto backend; add Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
lispc committed May 18, 2021
1 parent e7726fc commit 9e4d04e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:16-buster
WORKDIR /data
RUN apt update && apt install -y libgmp-dev nlohmann-json3-dev nasm g++
RUN rm -rf /var/lib/apt/lists/*
COPY package.json package-lock.json /data
COPY dist /data/dist
RUN npm install .
ENTRYPOINT ["node", "./dist/cli.js"]
CMD ["--version"]
6 changes: 3 additions & 3 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Command } from 'commander';
async function main() {
try {
const program = new Command();
program.version('0.0.7');
program.version('0.0.8');

program
.command('compile <circuit_dir>')
.description('compile a circom circuit dir')
.option('-f, --force_recompile', 'ignore compiled files', false)
.option('-s, --sanity_check', 'check constraints when generate witness', false)
.option('-v, --verbose', 'print verbose log', false)
.option('-b, --backend <string>', 'native or wasm', 'native')
.option('-b, --backend <string>', 'native|wasm|auto', 'auto')
.action(async (circuit_dir, options) => {
await compileCircuitDir(circuit_dir, {
alwaysRecompile: options.force_recompile,
Expand All @@ -30,7 +30,7 @@ async function main() {
.option('-f, --force_recompile', 'ignore compiled files', false)
.option('-s, --sanity_check', 'check constraints when generate witness', false)
.option('-v, --verbose', 'print verbose log', false)
.option('-b, --backend <string>', 'native or wasm', 'native')
.option('-b, --backend <string>', 'native|wasm|auto', 'auto')
.option('-w, --witness_type <string>', 'bin or text', 'text')
.description('test a circom circuit with given inputs/outputs')
.action(async (circuit_dir, options) => {
Expand Down
6 changes: 3 additions & 3 deletions dist/cli.js

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

2 changes: 1 addition & 1 deletion dist/cli.js.map

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

1 change: 1 addition & 0 deletions dist/src/witness_generator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare class WitnessGenerator {
verbose: boolean;
sanityCheck: boolean;
});
chooseBackend(): Promise<"native" | "wasm">;
compile(circuitDirName: any): Promise<{
r1csFilepath: string;
symFilepath: string;
Expand Down
35 changes: 19 additions & 16 deletions dist/src/witness_generator.js

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

2 changes: 1 addition & 1 deletion dist/src/witness_generator.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snarkit",
"version": "0.0.7",
"version": "0.0.8",
"description": "A command line tool to test circom circuit with given inputs/outputs",
"main": "./dist/index.js",
"bin": {
Expand All @@ -9,7 +9,8 @@
"build": "npx tsc",
"scripts": {
"fmt": "npx prettier --write \"**/*.{js,ts}\"",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"docker": "docker buildx build --platform linux/amd64 --load -t fluidex/snarkit ."
},
"repository": {
"type": "git",
Expand Down
36 changes: 20 additions & 16 deletions src/witness_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,28 @@ class WitnessGenerator {
}
}

async chooseBackend() {
const needFeatures = ['bmi2', 'adx'];
let cpuFeatures = (await si.cpu()).flags.split(/\s/);
if (process.platform === 'darwin') {
const stdout = shelljs.exec('sysctl machdep.cpu.leaf7_features', { silent: true });
const features = stdout.trim().toLowerCase().split(/\s/).slice(1);
cpuFeatures.push(...features);
}
for (const f of needFeatures) {
if (!cpuFeatures.includes(f)) {
console.log(`cpu missing needed feature ${f} for native backend, fallback to wasm`);
console.log(`cpus earlier than Intel Boradwell / AMD Ryzen are not supported for native backend`);
return 'wasm';
}
}
return 'native';
}

async compile(circuitDirName) {
this.circuitDirName = path.resolve(circuitDirName);
if (this.backend === 'native') {
const needFeatures = ['bmi2', 'adx'];
let cpuFeatures = (await si.cpu()).flags.split(/\s/);
if (process.platform === 'darwin') {
const stdout = shelljs.exec('sysctl machdep.cpu.leaf7_features', { silent: true });
const features = stdout.trim().toLowerCase().split(/\s/).slice(1);
cpuFeatures.push(...features);
}
for (const f of needFeatures) {
if (!cpuFeatures.includes(f)) {
console.log(`cpu missing needed feature ${f} for native backend, fallback to wasm`);
console.log(`cpus earlier than Intel Boradwell / AMD Ryzen are not supported for native backend`);
this.backend = 'wasm';
break;
}
}
if (this.backend === 'auto') {
this.backend = await this.chooseBackend();
}

const { r1csFilepath, symFilepath, binaryFilePath } = await compileCircuitDir(this.circuitDirName, {
Expand Down

0 comments on commit 9e4d04e

Please sign in to comment.