From 744d3b241cd482d668f5c980e9c9160d9db17681 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Thu, 2 Jul 2020 05:33:31 +0200 Subject: [PATCH] deps and verbos --- TUTORIAL.md | 2 +- cli.js | 11 ++++++++++- package.json | 2 +- ports/c/builder.js | 2 +- ports/wasm/build_runtime.js | 14 ++++++-------- src/compiler.js | 10 ++++++---- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/TUTORIAL.md b/TUTORIAL.md index 647f16b..b4a3999 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -8,7 +8,7 @@ This tutorial will guide you in creating your first zero-knowledge SNARK circuit If you don't have it installed yet, you need to install `Node.js`. -The last stable version of `Node.js` (or 8.12.0) works just fine, but if you install the latest current version `Node.js` (10.12.0) you will see a significant increase in performance. This is because last versions of node includes Big Integer Libraries nativelly. The `snarkjs` library makes use of this feature if available, and this improves the performance x10 (!). +You should install at least version 10 of node. It's important to note here that the latests versions of javascript, includes big integer support and web assembly compilers that make the code run fast. ### 1.2 Install **circom** and **snarkjs** diff --git a/cli.js b/cli.js index ab71c75..5ed2d58 100755 --- a/cli.js +++ b/cli.js @@ -82,7 +82,6 @@ const options = {}; options.reduceConstraints = !argv.fast; options.verbose = argv.verbose || false; options.sanityCheck = argv.sanitycheck; -options.prime = argv.prime ? Scalar.fromString(argv.prime) : Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); if (argv.csource) { options.cSourceWriteStream = fs.createWriteStream(cSourceName); @@ -102,6 +101,16 @@ if (argv.sym) { if (argv.newThreadTemplates) { options.newThreadTemplates = new RegExp(argv.newThreadTemplates); } +if (!argv.prime) { + options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); +} else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) { + options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16); +} else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) { + options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); +} else { + options.prime = Scalar.fromString(argv.prime); +} + compiler(fullFileName, options).then( () => { let cSourceDone = false; diff --git a/package.json b/package.json index 030c98b..3cdedcf 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "dependencies": { "chai": "^4.2.0", "circom_runtime": "0.0.6", - "fastfile": "0.0.1", + "fastfile": "0.0.2", "ffiasm": "0.0.2", "ffjavascript": "0.1.2", "ffwasm": "0.0.7", diff --git a/ports/c/builder.js b/ports/c/builder.js index e8460a9..7b53714 100644 --- a/ports/c/builder.js +++ b/ports/c/builder.js @@ -504,7 +504,7 @@ class BuilderC { function addShortMontgomeryNegative(a) { - const b = self.F.neg(a); + const b = -Scalar.toNumber(self.F.neg(a)); return `${b.toString()}, 0x40000000, { ${getLongString(toMontgomery(a))} }`; } diff --git a/ports/wasm/build_runtime.js b/ports/wasm/build_runtime.js index 2c74cce..bd3bf52 100644 --- a/ports/wasm/build_runtime.js +++ b/ports/wasm/build_runtime.js @@ -728,12 +728,9 @@ module.exports = function buildRuntime(module, builder) { c.setLocal( "pSrc", - c.i32_add( - c.i32_const(builder.pSignals), - c.i32_mul( - c.getLocal("i"), - c.i32_const(builder.sizeFr) - ) + c.call( + "getPWitness", + c.getLocal("i"), ) ), @@ -745,7 +742,7 @@ module.exports = function buildRuntime(module, builder) { c.setLocal( "pDst", c.i32_add( - c.i32_const(builder.pSignals), + c.i32_const(builder.pOutput), c.i32_mul( c.getLocal("i"), c.i32_const(builder.sizeFr-8) @@ -770,7 +767,7 @@ module.exports = function buildRuntime(module, builder) { c.br(0) )), - c.i32_const(builder.pSignals) + c.i32_const(builder.pOutput) ); } @@ -804,6 +801,7 @@ module.exports = function buildRuntime(module, builder) { buildWasmFf(module, "Fr", builder.header.P); builder.pSignals=module.alloc(builder.header.NSignals*builder.sizeFr); + builder.pOutput=module.alloc(builder.header.NVars*(builder.sizeFr-8)); builder.pInputSignalsToTrigger=module.alloc(builder.header.NComponents*4); builder.pSignalsAssigned=module.alloc(builder.header.NSignals*4); diff --git a/src/compiler.js b/src/compiler.js index a8e3534..275e653 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -32,14 +32,14 @@ const buildSyms = require("./buildsyms"); module.exports = compile; async function compile(srcFile, options) { - options.p = options.p || Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); + options.prime = options.prime || Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); if (!options) { options = {}; } if (typeof options.reduceConstraints === "undefined") { options.reduceConstraints = true; } - const ctx = new Ctx(options.p); + const ctx = new Ctx(options.prime); ctx.verbose= options.verbose || false; ctx.mainComponent = options.mainComponent || "main"; ctx.newThreadTemplates = options.newThreadTemplates; @@ -82,7 +82,8 @@ async function compile(srcFile, options) { } if (options.cSourceWriteStream) { - ctx.builder = new BuilderC(options.p); + if (ctx.verbose) console.log("Generating c..."); + ctx.builder = new BuilderC(options.prime); build(ctx); const rdStream = ctx.builder.build(); rdStream.pipe(options.cSourceWriteStream); @@ -91,7 +92,8 @@ async function compile(srcFile, options) { } if ((options.wasmWriteStream)||(options.watWriteStream)) { - ctx.builder = new BuilderWasm(options.p); + if (ctx.verbose) console.log("Generating wasm..."); + ctx.builder = new BuilderWasm(options.prime); build(ctx); if (options.wasmWriteStream) { const rdStream = ctx.builder.build("wasm");