@ohos-ports/circom_tester 0.0.0-stage → 0.0.24-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/main.yml +29 -0
- package/.github/workflows/npm-publish.yml +20 -0
- package/README.md +35 -2
- package/c/tester.js +51 -0
- package/common/tester.js +430 -0
- package/common/uint_compat.h +6 -0
- package/index.js +2 -0
- package/package.json +39 -4
- package/wasm/tester.js +31 -0
- package/wasm/utils.js +59 -0
- package/wasm/witness_calculator.js +279 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
test:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
strategy:
|
|
10
|
+
matrix:
|
|
11
|
+
node: [ 22 ]
|
|
12
|
+
name: Node ${{ matrix.node }}
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
|
|
16
|
+
- name: Setup node
|
|
17
|
+
uses: actions/setup-node@v3
|
|
18
|
+
with:
|
|
19
|
+
node-version: ${{ matrix.node }}
|
|
20
|
+
|
|
21
|
+
- name: Setup other deps
|
|
22
|
+
run: sudo apt-get update && sudo apt-get install -y wget nlohmann-json3-dev libgmp-dev nasm g++ build-essential
|
|
23
|
+
|
|
24
|
+
- name: Setup Circom
|
|
25
|
+
run: wget https://github.com/iden3/circom/releases/latest/download/circom-linux-amd64 && sudo mv ./circom-linux-amd64 /usr/bin/circom && sudo chmod +x /usr/bin/circom
|
|
26
|
+
|
|
27
|
+
- run: npm ci
|
|
28
|
+
|
|
29
|
+
- run: npm run test
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Publish package to NPM
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish-npm:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
- uses: actions/setup-node@v4
|
|
13
|
+
with:
|
|
14
|
+
node-version: '20.x'
|
|
15
|
+
registry-url: https://registry.npmjs.org/
|
|
16
|
+
cache: 'npm'
|
|
17
|
+
- run: npm ci
|
|
18
|
+
- run: npm publish
|
|
19
|
+
env:
|
|
20
|
+
NODE_AUTH_TOKEN: ${{secrets.IDEN3_CIRCOM_NPM_PUBLISH_TOKEN}}
|
package/README.md
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
|
-
#
|
|
1
|
+
# circom tester
|
|
2
|
+
## Setup
|
|
3
|
+
1. Create new node js project.
|
|
4
|
+
2. Install `circom_tester` and `chai` packges.
|
|
5
|
+
3. Install `mocha` packge to run the tests.
|
|
2
6
|
|
|
3
|
-
|
|
7
|
+
|
|
8
|
+
## Creating & Running a test file
|
|
9
|
+
|
|
10
|
+
Create a js file contain the following code or use the src provided in the following section.
|
|
11
|
+
<br>Execue `mocha -p -r ts-node/register 'multiplier2.js'` to run the test file.
|
|
12
|
+
|
|
13
|
+
multiplier2.js ([src](test/multiplier2.js))
|
|
14
|
+
``` multiplier2.js
|
|
15
|
+
const chai = require("chai");
|
|
16
|
+
const path = require("path");
|
|
17
|
+
const wasm_tester = require("./../index").wasm;
|
|
18
|
+
const c_tester = require("./../index").c;
|
|
19
|
+
|
|
20
|
+
const F1Field = require("ffjavascript").F1Field;
|
|
21
|
+
const Scalar = require("ffjavascript").Scalar;
|
|
22
|
+
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
|
23
|
+
const Fr = new F1Field(exports.p);
|
|
24
|
+
|
|
25
|
+
const assert = chai.assert;
|
|
26
|
+
|
|
27
|
+
describe("Simple test", function () {
|
|
28
|
+
this.timeout(100000);
|
|
29
|
+
|
|
30
|
+
it("Checking the compilation of a simple circuit generating wasm", async function () {
|
|
31
|
+
const circuit = await wasm_tester(path.join(__dirname, "Multiplier2circom"));
|
|
32
|
+
const w = await circuit.calculateWitness({a: 2, b: 4});
|
|
33
|
+
await circuit.checkConstraints(w);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
```
|
package/c/tester.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const { BaseTester, parseOptionsAndCompile} = require("../common/tester");
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
const util = require("util");
|
|
7
|
+
const exec = util.promisify(require("child_process").exec);
|
|
8
|
+
|
|
9
|
+
const readWtns = require("snarkjs").wtns.exportJson;
|
|
10
|
+
|
|
11
|
+
module.exports = c_tester;
|
|
12
|
+
|
|
13
|
+
BigInt.prototype.toJSON = function () {
|
|
14
|
+
return this.toString()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function c_tester(circomInput, _options) {
|
|
18
|
+
let options = Object.assign({}, _options);
|
|
19
|
+
options.c = true;
|
|
20
|
+
options = await parseOptionsAndCompile(circomInput, options);
|
|
21
|
+
return new CTester(options.output, options.baseName);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class CTester extends BaseTester {
|
|
25
|
+
constructor(dir, baseName) {
|
|
26
|
+
super(dir, baseName);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async calculateWitness(input) {
|
|
30
|
+
const inputjson = JSON.stringify(input);
|
|
31
|
+
const inputFile = path.join(this.dir, this.baseName + "_cpp/" + this.baseName + ".json");
|
|
32
|
+
const wtnsFile = path.join(this.dir, this.baseName + "_cpp/" + this.baseName + ".wtns");
|
|
33
|
+
const runc = path.join(this.dir, this.baseName + "_cpp/" + this.baseName);
|
|
34
|
+
fs.writeFile(inputFile, inputjson, function (err) {
|
|
35
|
+
if (err) throw err;
|
|
36
|
+
});
|
|
37
|
+
await exec("cd " + path.join(this.dir, this.baseName + "_cpp/"));
|
|
38
|
+
let proc = await exec(runc + " " + inputFile + " " + wtnsFile);
|
|
39
|
+
if (proc.stdout !== "") {
|
|
40
|
+
console.log(proc.stdout);
|
|
41
|
+
}
|
|
42
|
+
if (proc.stderr !== "") {
|
|
43
|
+
console.error(proc.stderr);
|
|
44
|
+
}
|
|
45
|
+
return await readBinWitnessFile(wtnsFile);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function readBinWitnessFile(fileName) {
|
|
50
|
+
return readWtns(fileName);
|
|
51
|
+
}
|
package/common/tester.js
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { F1Field } = require("ffjavascript");
|
|
4
|
+
const { readR1cs } = require("r1csfile");
|
|
5
|
+
const util = require("util");
|
|
6
|
+
const tmp = require("tmp-promise");
|
|
7
|
+
const { createHash } = require('crypto');
|
|
8
|
+
const exec = util.promisify(require("child_process").exec);
|
|
9
|
+
|
|
10
|
+
async function parseOptionsAndCompile(circomInput, options) {
|
|
11
|
+
const compilerVersion = await compiler_above_version("2.0.0");
|
|
12
|
+
if (!compilerVersion) {
|
|
13
|
+
throw new Error("Wrong compiler version. Must be at least 2.0.0");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let baseName = path.basename(circomInput, ".circom");
|
|
17
|
+
|
|
18
|
+
options.sym = true;
|
|
19
|
+
options.baseName = baseName;
|
|
20
|
+
options.json = options.json || false; // constraints in json format
|
|
21
|
+
options.r1cs = true;
|
|
22
|
+
options.compile = (typeof options.recompile === 'undefined') ? true : options.recompile; // by default compile
|
|
23
|
+
|
|
24
|
+
// if output is not set, create a temporary directory
|
|
25
|
+
if (typeof options.output === 'undefined') {
|
|
26
|
+
tmp.setGracefulCleanup();
|
|
27
|
+
const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true});
|
|
28
|
+
options.output = dir.path;
|
|
29
|
+
} else {
|
|
30
|
+
try {
|
|
31
|
+
await fs.promises.access(options.output);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
if (!options.compile) {
|
|
34
|
+
throw new Error("Cannot set recompile to false if the output path does not exist");
|
|
35
|
+
}
|
|
36
|
+
await fs.promises.mkdir(options.output, {recursive: true});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// read contents of circomInput
|
|
41
|
+
const circomCode = await fs.promises.readFile(circomInput, "utf8");
|
|
42
|
+
|
|
43
|
+
// check if circomCode has main component instantiated
|
|
44
|
+
const mainRegex = /^\s*component\s+main[\s={\/]/m;
|
|
45
|
+
const mainDefined = mainRegex.test(circomCode);
|
|
46
|
+
|
|
47
|
+
// if main is not defined, get all template names
|
|
48
|
+
const templateNames = [];
|
|
49
|
+
const pragmaLines = [];
|
|
50
|
+
if (!mainDefined) {
|
|
51
|
+
// get all pragma lines to put in temporary circom file
|
|
52
|
+
const pragmaRegex = /^\s*pragma\s+.*$/gm;
|
|
53
|
+
let pragmaMatches;
|
|
54
|
+
while ((pragmaMatches = pragmaRegex.exec(circomCode)) !== null) {
|
|
55
|
+
pragmaLines.push(pragmaMatches[0]);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// get all template names
|
|
59
|
+
const templateRegex = /^\s*template\s+([a-zA-Z0-9_$]+)\s*\(/gm;
|
|
60
|
+
let templateMatches;
|
|
61
|
+
while ((templateMatches = templateRegex.exec(circomCode)) !== null) {
|
|
62
|
+
templateNames.push(templateMatches[1]);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// check for conflicting options
|
|
67
|
+
if (mainDefined) {
|
|
68
|
+
if (options.templateName || options.templateParams || options.templatePublicSignals) {
|
|
69
|
+
throw new Error("Cannot set template name, params and public signals if main is already defined");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// when main is not defined, create temporary circom file using specified template as a main component
|
|
74
|
+
if (!mainDefined) {
|
|
75
|
+
// if templateName is not provided, try to autodetect it
|
|
76
|
+
if (!options.templateName) {
|
|
77
|
+
// if file has only one template, use it as main component
|
|
78
|
+
if (templateNames.length === 1) {
|
|
79
|
+
options.templateName = templateNames[0];
|
|
80
|
+
} else {
|
|
81
|
+
throw new Error("No main component defined and template name to generate one is not provided");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// define main component with templateName
|
|
86
|
+
let params = "";
|
|
87
|
+
if (options.templateParams) {
|
|
88
|
+
params = options.templateParams.map((p) => p.toString()).join(", ");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// define public signals if any
|
|
92
|
+
let publicSignals = "";
|
|
93
|
+
if (options.templatePublicSignals) {
|
|
94
|
+
publicSignals = "{public [" + options.templatePublicSignals.map((p) => p.toString()).join(", ") + "]}";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// define main component
|
|
98
|
+
const mainComponent = `component main ${publicSignals} = ${options.templateName}(${params});`;
|
|
99
|
+
|
|
100
|
+
// include original circom file
|
|
101
|
+
const includes = "include \"" + baseName + ".circom\";";
|
|
102
|
+
|
|
103
|
+
const tmpCircomCodeLines = [...pragmaLines, includes, mainComponent];
|
|
104
|
+
const tmpCircomCode = tmpCircomCodeLines.join("\n");
|
|
105
|
+
|
|
106
|
+
// calculate the hash of the main component using sha256
|
|
107
|
+
const h = createHash("sha256");
|
|
108
|
+
const hash = h.update(Buffer.from(tmpCircomCode, "utf8")).digest('hex');
|
|
109
|
+
// and use it as a suffix for the temporary directory name inside the output path
|
|
110
|
+
let suffix = hash.substring(0, 8);
|
|
111
|
+
const tmpCircomPath = path.join(options.output, baseName + "_" + options.templateName + "_" + suffix);
|
|
112
|
+
|
|
113
|
+
// create the directory if it doesn't exist
|
|
114
|
+
await fs.promises.mkdir(tmpCircomPath, {recursive: true});
|
|
115
|
+
|
|
116
|
+
// add the original circom file directory to the include path
|
|
117
|
+
const includePath = path.dirname(path.resolve(circomInput));
|
|
118
|
+
if (options.include) {
|
|
119
|
+
if (Array.isArray(options.include)) {
|
|
120
|
+
options.include.push(includePath);
|
|
121
|
+
} else {
|
|
122
|
+
options.include = [options.include, includePath];
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
options.include = [includePath];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// override baseName, output path and circomInput
|
|
129
|
+
baseName = "circuit";
|
|
130
|
+
options.baseName = baseName;
|
|
131
|
+
options.output = tmpCircomPath;
|
|
132
|
+
circomInput = path.join(tmpCircomPath, baseName + ".circom");
|
|
133
|
+
|
|
134
|
+
// write the generated circom code to the temporary file circuit.circom
|
|
135
|
+
await fs.promises.writeFile(circomInput, tmpCircomCode, "utf8");
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// compile flag is set by default, unless overwritten by recompile=false option
|
|
140
|
+
if (options.compile) {
|
|
141
|
+
await compile(baseName, circomInput, options);
|
|
142
|
+
} else {
|
|
143
|
+
const jsPath = path.join(options.output, baseName + "_js");
|
|
144
|
+
try {
|
|
145
|
+
await fs.promises.access(jsPath);
|
|
146
|
+
} catch (err) {
|
|
147
|
+
throw new Error("Cannot set recompile to false if the " + jsPath + " folder does not exist")
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return options;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function compile(baseName, fileName, options) {
|
|
155
|
+
// OpenHarmony (aarch64) platform adaptation:
|
|
156
|
+
// circom's default asm codegen emits x86_64-only NASM (fr.asm), which cannot
|
|
157
|
+
// be assembled on aarch64. The documented --no_asm mode makes circom emit a
|
|
158
|
+
// pure C++ fr.cpp instead, which is the correct artifact mapping on this
|
|
159
|
+
// platform. Only applied when the user has not explicitly set no_asm.
|
|
160
|
+
if (process.platform === "openharmony" && options.c && options.no_asm === undefined) {
|
|
161
|
+
options.no_asm = true;
|
|
162
|
+
}
|
|
163
|
+
let flags = "";
|
|
164
|
+
if (options.include) {
|
|
165
|
+
if (Array.isArray(options.include)) {
|
|
166
|
+
for (let i = 0; i < options.include.length; i++) {
|
|
167
|
+
flags += "-l " + options.include[i] + " ";
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
flags += "-l " + options.include + " ";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (options.c) flags += "--c ";
|
|
174
|
+
if (options.wasm) flags += "--wasm ";
|
|
175
|
+
if (options.sym) flags += "--sym ";
|
|
176
|
+
if (options.r1cs) flags += "--r1cs ";
|
|
177
|
+
if (options.json) flags += "--json ";
|
|
178
|
+
if (options.output) flags += "--output " + options.output + " ";
|
|
179
|
+
if (options.prime) flags += "--prime " + options.prime + " ";
|
|
180
|
+
if (options.O === 0) flags += "--O0 ";
|
|
181
|
+
if (options.O === 1) flags += "--O1 ";
|
|
182
|
+
if (options.O === 2) flags += "--O2 ";
|
|
183
|
+
if (options.O2round) flags += "--O2round " + options.O2round + " ";
|
|
184
|
+
if (options.verbose) flags += "--verbose ";
|
|
185
|
+
if (options.inspect) flags += "--inspect ";
|
|
186
|
+
if (options.simplification_substitution) flags += "--simplification_substitution ";
|
|
187
|
+
if (options.no_asm) flags += "--no_asm ";
|
|
188
|
+
if (options.no_init) flags += "--no_init ";
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
let b = await exec("circom " + flags + fileName);
|
|
192
|
+
if (options.verbose) {
|
|
193
|
+
console.log(b.stdout);
|
|
194
|
+
}
|
|
195
|
+
if (b.stderr) {
|
|
196
|
+
console.error(b.stderr);
|
|
197
|
+
}
|
|
198
|
+
} catch (e) {
|
|
199
|
+
throw new Error("circom compiler error: " + e);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (options.c) {
|
|
203
|
+
const c_folder = path.join(options.output, baseName + "_cpp/")
|
|
204
|
+
let makeCmd = "make -C " + c_folder;
|
|
205
|
+
let makeExecOpts;
|
|
206
|
+
if (process.platform === "openharmony") {
|
|
207
|
+
// OpenHarmony (aarch64) platform adaptation:
|
|
208
|
+
// 1) circom-generated fr.hpp relies on the glibc-only legacy `uint`
|
|
209
|
+
// typedef, which does not exist on OpenHarmony's musl libc. Ship a
|
|
210
|
+
// small compat header next to the generated sources.
|
|
211
|
+
const shimDest = path.join(c_folder, "uint_compat.h");
|
|
212
|
+
await fs.promises.copyFile(path.join(__dirname, "uint_compat.h"), shimDest);
|
|
213
|
+
// 2) The generated Makefile assigns CFLAGS itself, so env-level
|
|
214
|
+
// CFLAGS/CXXFLAGS would be ignored; append a force-include of the
|
|
215
|
+
// shim to the generated Makefile instead.
|
|
216
|
+
const makefilePath = path.join(c_folder, "Makefile");
|
|
217
|
+
const makefileExtra =
|
|
218
|
+
"\n# circom_tester openharmony port: force-include musl uint compat shim\n" +
|
|
219
|
+
"circom_tester_ohos_uint_shim = -include uint_compat.h\n" +
|
|
220
|
+
"CFLAGS += $(circom_tester_ohos_uint_shim)\n";
|
|
221
|
+
await fs.promises.appendFile(makefilePath, makefileExtra, "utf8");
|
|
222
|
+
// 3) Build env: pass through the user/system environment (CPATH for
|
|
223
|
+
// headers such as nlohmann/json.hpp, LIBRARY_PATH for libs such as
|
|
224
|
+
// libgmp), but drop LD_LIBRARY_PATH which conflicts with the
|
|
225
|
+
// toolchain linker's own library resolution. Runtime loaders still
|
|
226
|
+
// see the caller's environment.
|
|
227
|
+
const buildEnv = Object.assign({}, process.env);
|
|
228
|
+
delete buildEnv.LD_LIBRARY_PATH;
|
|
229
|
+
makeExecOpts = { env: buildEnv };
|
|
230
|
+
}
|
|
231
|
+
let b = await exec(makeCmd, makeExecOpts);
|
|
232
|
+
if (b.stderr) {
|
|
233
|
+
console.error(b.stderr);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class BaseTester {
|
|
240
|
+
constructor(dir, baseName) {
|
|
241
|
+
this.dir = dir;
|
|
242
|
+
this.baseName = baseName;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async release() {
|
|
246
|
+
await this.dir.cleanup();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async loadSymbols() {
|
|
250
|
+
if (this.symbols) return;
|
|
251
|
+
this.symbols = {};
|
|
252
|
+
const symsStr = await fs.promises.readFile(
|
|
253
|
+
path.join(this.dir, this.baseName + ".sym"),
|
|
254
|
+
"utf8"
|
|
255
|
+
);
|
|
256
|
+
const lines = symsStr.split("\n");
|
|
257
|
+
for (let i = 0; i < lines.length; i++) {
|
|
258
|
+
const arr = lines[i].split(",");
|
|
259
|
+
if (arr.length !== 4) continue;
|
|
260
|
+
this.symbols[arr[3]] = {
|
|
261
|
+
labelIdx: Number(arr[0]),
|
|
262
|
+
varIdx: Number(arr[1]),
|
|
263
|
+
componentIdx: Number(arr[2]),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async loadConstraints() {
|
|
269
|
+
const self = this;
|
|
270
|
+
if (this.constraints) return;
|
|
271
|
+
const r1cs = await readR1cs(path.join(this.dir, this.baseName + ".r1cs"), {
|
|
272
|
+
loadConstraints: true,
|
|
273
|
+
loadMap: false,
|
|
274
|
+
getFieldFromPrime: (p, singlethread) => new F1Field(p)
|
|
275
|
+
});
|
|
276
|
+
self.F = r1cs.F;
|
|
277
|
+
self.nVars = r1cs.nVars;
|
|
278
|
+
self.constraints = r1cs.constraints;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async assertOut(actualOut, expectedOut) {
|
|
282
|
+
const self = this;
|
|
283
|
+
if (!self.symbols) await self.loadSymbols();
|
|
284
|
+
|
|
285
|
+
checkObject("main", expectedOut);
|
|
286
|
+
|
|
287
|
+
function checkObject(prefix, eOut) {
|
|
288
|
+
|
|
289
|
+
if (Array.isArray(eOut)) {
|
|
290
|
+
for (let i = 0; i < eOut.length; i++) {
|
|
291
|
+
checkObject(prefix + "[" + i + "]", eOut[i]);
|
|
292
|
+
}
|
|
293
|
+
} else if (typeof eOut === "object" && eOut.constructor.name === "Object") {
|
|
294
|
+
for (let k in eOut) {
|
|
295
|
+
checkObject(prefix + "." + k, eOut[k]);
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
if (typeof self.symbols[prefix] === "undefined") {
|
|
299
|
+
throw new Error("Output variable not defined: " + prefix);
|
|
300
|
+
}
|
|
301
|
+
const ba = actualOut[self.symbols[prefix].varIdx].toString();
|
|
302
|
+
const be = eOut.toString();
|
|
303
|
+
if (ba !== be) {
|
|
304
|
+
throw new Error(`Assertion failed for ${prefix}: expected ${be}, got ${ba}`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async getDecoratedOutput(witness) {
|
|
311
|
+
const self = this;
|
|
312
|
+
const lines = [];
|
|
313
|
+
if (!self.symbols) await self.loadSymbols();
|
|
314
|
+
for (let n in self.symbols) {
|
|
315
|
+
let v;
|
|
316
|
+
if (witness[self.symbols[n].varIdx] !== undefined) {
|
|
317
|
+
v = witness[self.symbols[n].varIdx].toString();
|
|
318
|
+
} else {
|
|
319
|
+
v = "undefined";
|
|
320
|
+
}
|
|
321
|
+
lines.push(`${n} --> ${v}`);
|
|
322
|
+
}
|
|
323
|
+
return lines.join("\n");
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async getOutput(witness, output, templateName = "main") {
|
|
327
|
+
const self = this;
|
|
328
|
+
if (!self.symbols) await self.loadSymbols();
|
|
329
|
+
|
|
330
|
+
let prefix = "main";
|
|
331
|
+
if (templateName !== "main") {
|
|
332
|
+
const regex = new RegExp(`^.*${templateName}[^.]*\.[^.]+$`);
|
|
333
|
+
Object.keys(self.symbols).find((k) => {
|
|
334
|
+
if (regex.test(k)) {
|
|
335
|
+
prefix = k.replace(/\.[^.]+$/, "");
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return get_by_prefix(prefix, output);
|
|
342
|
+
|
|
343
|
+
function get_by_prefix(prefix, out) {
|
|
344
|
+
if (typeof out === "object" && out.constructor.name === "Object") {
|
|
345
|
+
return Object.fromEntries(
|
|
346
|
+
Object.entries(out).map(([k, v]) => [
|
|
347
|
+
k,
|
|
348
|
+
get_by_prefix(`${prefix}.${k}`, v),
|
|
349
|
+
])
|
|
350
|
+
);
|
|
351
|
+
} else if (Array.isArray(out)) {
|
|
352
|
+
if (out.length === 1) {
|
|
353
|
+
return get_by_prefix(prefix, out[0]);
|
|
354
|
+
} else if (out.length === 0 || out.length > 2) {
|
|
355
|
+
throw new Error(`Invalid output format: ${prefix} ${out}`);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return Array.from({ length: out[0] }, (_, i) =>
|
|
359
|
+
get_by_prefix(`${prefix}[${i}]`, out[1])
|
|
360
|
+
);
|
|
361
|
+
} else {
|
|
362
|
+
if (out === 1) {
|
|
363
|
+
const name = `${prefix}`;
|
|
364
|
+
if (typeof self.symbols[name] == "undefined") {
|
|
365
|
+
throw new Error(`Output variable not defined: ${name}`);
|
|
366
|
+
}
|
|
367
|
+
return witness[self.symbols[name].varIdx];
|
|
368
|
+
} else {
|
|
369
|
+
return Array.from({ length: out }, (_, i) => {
|
|
370
|
+
const name = `${prefix}[${i}]`;
|
|
371
|
+
if (typeof self.symbols[name] == "undefined") {
|
|
372
|
+
throw new Error(`Output variable not defined: ${name}`);
|
|
373
|
+
}
|
|
374
|
+
return witness[self.symbols[name].varIdx];
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async checkConstraints(witness) {
|
|
382
|
+
const self = this;
|
|
383
|
+
if (!self.constraints) await self.loadConstraints();
|
|
384
|
+
const F = self.F;
|
|
385
|
+
for (let i = 0; i < self.constraints.length; i++) {
|
|
386
|
+
const constraint = self.constraints[i];
|
|
387
|
+
const a = evalLC(constraint[0]);
|
|
388
|
+
const b = evalLC(constraint[1]);
|
|
389
|
+
const c = evalLC(constraint[2]);
|
|
390
|
+
if (!F.isZero(F.sub(F.mul(a, b), c))) {
|
|
391
|
+
throw new Error("Constraint doesn't match");
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function evalLC(lc) {
|
|
396
|
+
const F = self.F;
|
|
397
|
+
let v = F.zero;
|
|
398
|
+
for (let w in lc) {
|
|
399
|
+
v = F.add(
|
|
400
|
+
v,
|
|
401
|
+
F.mul(lc[w], F.e(witness[w]))
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
return v;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function version_to_list(v) {
|
|
411
|
+
return v.split(".").map((x) => parseInt(x, 10));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function check_versions(v1, v2) {
|
|
415
|
+
//check if v1 is newer than or equal to v2
|
|
416
|
+
for (let i = 0; i < v2.length; i++) {
|
|
417
|
+
if (v1[i] > v2[i]) return true;
|
|
418
|
+
if (v1[i] < v2[i]) return false;
|
|
419
|
+
}
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
async function compiler_above_version(v) {
|
|
424
|
+
const output = (await exec("circom --version")).stdout;
|
|
425
|
+
const compiler_version = version_to_list(output.slice(output.search(/\d/), -1));
|
|
426
|
+
const vlist = version_to_list(v);
|
|
427
|
+
return check_versions(compiler_version, vlist);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
module.exports = { BaseTester, version_to_list, check_versions, compile, compiler_above_version, parseOptionsAndCompile };
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohos-ports/circom_tester",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.24-beta.1",
|
|
4
|
+
"description": "Tools for testing circom circuits.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "./node_modules/.bin/mocha -p 'test/*/*.js'"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
12
|
+
"directory": "ports/circom_tester/0.0.24"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"circom",
|
|
16
|
+
"test",
|
|
17
|
+
"snarkjs",
|
|
18
|
+
"iden3"
|
|
19
|
+
],
|
|
20
|
+
"author": "Albert Rubio",
|
|
21
|
+
"license": "GPL-3.0",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/iden3/circom_tester#readme",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"chai": "^4.3.6",
|
|
28
|
+
"ffjavascript": "^0.3.1",
|
|
29
|
+
"fnv-plus": "^1.3.1",
|
|
30
|
+
"r1csfile": "^0.0.48",
|
|
31
|
+
"tmp-promise": "^3.0.3",
|
|
32
|
+
"util": "^0.12.5",
|
|
33
|
+
"snarkjs": "^0.7.5"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/chai": "^4.3.6",
|
|
37
|
+
"@types/mocha": "^10.0.1",
|
|
38
|
+
"mocha": "^10.1.0",
|
|
39
|
+
"circomlib": "^2.0.5"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/wasm/tester.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { BaseTester, parseOptionsAndCompile} = require("../common/tester");
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
module.exports = wasm_tester;
|
|
7
|
+
|
|
8
|
+
async function wasm_tester(circomInput, _options) {
|
|
9
|
+
let options = Object.assign({}, _options);
|
|
10
|
+
options.wasm = true;
|
|
11
|
+
options = await parseOptionsAndCompile(circomInput, options);
|
|
12
|
+
|
|
13
|
+
const baseName = options.baseName;
|
|
14
|
+
|
|
15
|
+
const WitnessCalculator = require("./witness_calculator");
|
|
16
|
+
const wasm = await fs.promises.readFile(path.join(options.output, baseName + "_js/" + baseName + ".wasm"));
|
|
17
|
+
const wc = await WitnessCalculator(wasm);
|
|
18
|
+
|
|
19
|
+
return new WasmTester(options.output, baseName, wc);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class WasmTester extends BaseTester {
|
|
23
|
+
constructor(dir, baseName, witnessCalculator) {
|
|
24
|
+
super(dir, baseName);
|
|
25
|
+
this.witnessCalculator = witnessCalculator;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async calculateWitness(input, sanityCheck) {
|
|
29
|
+
return await this.witnessCalculator.calculateWitness(input, sanityCheck);
|
|
30
|
+
}
|
|
31
|
+
}
|
package/wasm/utils.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const fnv = require("fnv-plus");
|
|
2
|
+
|
|
3
|
+
module.exports.fnvHash = fnvHash;
|
|
4
|
+
module.exports.toArray32 = toArray32;
|
|
5
|
+
module.exports.fromArray32 = fromArray32;
|
|
6
|
+
module.exports.flatArray = flatArray;
|
|
7
|
+
module.exports.normalize = normalize;
|
|
8
|
+
|
|
9
|
+
function toArray32(rem, size) {
|
|
10
|
+
const res = []; //new Uint32Array(size); //has no unshift
|
|
11
|
+
const radix = BigInt(0x100000000);
|
|
12
|
+
while (rem) {
|
|
13
|
+
res.unshift(Number(rem % radix));
|
|
14
|
+
rem = rem / radix;
|
|
15
|
+
}
|
|
16
|
+
if (size) {
|
|
17
|
+
var i = size - res.length;
|
|
18
|
+
while (i > 0) {
|
|
19
|
+
res.unshift(0);
|
|
20
|
+
i--;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return res;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fromArray32(arr) { //returns a BigInt
|
|
27
|
+
var res = BigInt(0);
|
|
28
|
+
const radix = BigInt(0x100000000);
|
|
29
|
+
for (let i = 0; i < arr.length; i++) {
|
|
30
|
+
res = res * radix + BigInt(arr[i]);
|
|
31
|
+
}
|
|
32
|
+
return res;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function flatArray(a) {
|
|
36
|
+
var res = [];
|
|
37
|
+
fillArray(res, a);
|
|
38
|
+
return res;
|
|
39
|
+
|
|
40
|
+
function fillArray(res, a) {
|
|
41
|
+
if (Array.isArray(a)) {
|
|
42
|
+
for (let i = 0; i < a.length; i++) {
|
|
43
|
+
fillArray(res, a[i]);
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
res.push(a);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalize(n, prime) {
|
|
52
|
+
let res = BigInt(n) % prime
|
|
53
|
+
if (res < 0) res += prime
|
|
54
|
+
return res
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function fnvHash(str) {
|
|
58
|
+
return fnv.hash(str, 64).hex();
|
|
59
|
+
}
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
const utils = require("./utils");
|
|
2
|
+
|
|
3
|
+
module.exports = async function builder(code, options) {
|
|
4
|
+
|
|
5
|
+
options = options || {};
|
|
6
|
+
|
|
7
|
+
let wasmModule;
|
|
8
|
+
try {
|
|
9
|
+
wasmModule = await WebAssembly.compile(code);
|
|
10
|
+
} catch (err) {
|
|
11
|
+
console.log(err);
|
|
12
|
+
console.log("\nTry to run circom --c in order to generate c++ code instead\n");
|
|
13
|
+
throw new Error(err);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let wc;
|
|
17
|
+
|
|
18
|
+
let errStr = "";
|
|
19
|
+
let msgStr = "";
|
|
20
|
+
|
|
21
|
+
const instance = await WebAssembly.instantiate(wasmModule, {
|
|
22
|
+
runtime: {
|
|
23
|
+
exceptionHandler: function (code) {
|
|
24
|
+
let err;
|
|
25
|
+
if (code === 1) {
|
|
26
|
+
err = "Signal not found.\n";
|
|
27
|
+
} else if (code === 2) {
|
|
28
|
+
err = "Too many signals set.\n";
|
|
29
|
+
} else if (code === 3) {
|
|
30
|
+
err = "Signal already set.\n";
|
|
31
|
+
} else if (code === 4) {
|
|
32
|
+
err = "Assert Failed.\n";
|
|
33
|
+
} else if (code === 5) {
|
|
34
|
+
err = "Not enough memory.\n";
|
|
35
|
+
} else if (code === 6) {
|
|
36
|
+
err = "Input signal array access exceeds the size.\n";
|
|
37
|
+
} else {
|
|
38
|
+
err = "Unknown error.\n";
|
|
39
|
+
}
|
|
40
|
+
throw new Error(err + errStr);
|
|
41
|
+
},
|
|
42
|
+
printErrorMessage: function () {
|
|
43
|
+
errStr += getMessage() + "\n";
|
|
44
|
+
// console.error(getMessage());
|
|
45
|
+
},
|
|
46
|
+
writeBufferMessage: function () {
|
|
47
|
+
const msg = getMessage();
|
|
48
|
+
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
|
|
49
|
+
if (msg === "\n") {
|
|
50
|
+
console.log(msgStr);
|
|
51
|
+
msgStr = "";
|
|
52
|
+
} else {
|
|
53
|
+
// If we've buffered other content, put a space in between the items
|
|
54
|
+
if (msgStr !== "") {
|
|
55
|
+
msgStr += " "
|
|
56
|
+
}
|
|
57
|
+
// Then append the message to the message we are creating
|
|
58
|
+
msgStr += msg;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
showSharedRWMemory: function () {
|
|
62
|
+
printSharedRWMemory();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const sanityCheck =
|
|
69
|
+
options
|
|
70
|
+
// options &&
|
|
71
|
+
// (
|
|
72
|
+
// options.sanityCheck ||
|
|
73
|
+
// options.logGetSignal ||
|
|
74
|
+
// options.logSetSignal ||
|
|
75
|
+
// options.logStartComponent ||
|
|
76
|
+
// options.logFinishComponent
|
|
77
|
+
// );
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
wc = new WitnessCalculator(instance, sanityCheck);
|
|
81
|
+
return wc;
|
|
82
|
+
|
|
83
|
+
function getMessage() {
|
|
84
|
+
let message = "";
|
|
85
|
+
let c = instance.exports.getMessageChar();
|
|
86
|
+
while (c != 0) {
|
|
87
|
+
message += String.fromCharCode(c);
|
|
88
|
+
c = instance.exports.getMessageChar();
|
|
89
|
+
}
|
|
90
|
+
return message;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function printSharedRWMemory() {
|
|
94
|
+
const shared_rw_memory_size = instance.exports.getFieldNumLen32();
|
|
95
|
+
const arr = new Uint32Array(shared_rw_memory_size);
|
|
96
|
+
for (let j = 0; j < shared_rw_memory_size; j++) {
|
|
97
|
+
arr[shared_rw_memory_size - 1 - j] = instance.exports.readSharedRWMemory(j);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If we've buffered other content, put a space in between the items
|
|
101
|
+
if (msgStr !== "") {
|
|
102
|
+
msgStr += " "
|
|
103
|
+
}
|
|
104
|
+
// Then append the value to the message we are creating
|
|
105
|
+
msgStr += (utils.fromArray32(arr).toString());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
class WitnessCalculator {
|
|
111
|
+
constructor(instance, sanityCheck) {
|
|
112
|
+
this.instance = instance;
|
|
113
|
+
|
|
114
|
+
this.version = this.instance.exports.getVersion();
|
|
115
|
+
this.n32 = this.instance.exports.getFieldNumLen32();
|
|
116
|
+
|
|
117
|
+
this.instance.exports.getRawPrime();
|
|
118
|
+
const arr = new Uint32Array(this.n32);
|
|
119
|
+
for (let i = 0; i < this.n32; i++) {
|
|
120
|
+
arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
|
|
121
|
+
}
|
|
122
|
+
this.prime = utils.fromArray32(arr);
|
|
123
|
+
|
|
124
|
+
this.witnessSize = this.instance.exports.getWitnessSize();
|
|
125
|
+
|
|
126
|
+
this.sanityCheck = sanityCheck;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
circom_version() {
|
|
130
|
+
return this.instance.exports.getVersion();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async _doCalculateWitness(input, sanityCheck) {
|
|
134
|
+
//input is assumed to be a map from signals to arrays of bigints
|
|
135
|
+
this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
|
|
136
|
+
const keys = Object.keys(input);
|
|
137
|
+
let input_counter = 0;
|
|
138
|
+
keys.forEach((k) => {
|
|
139
|
+
const h = utils.fnvHash(k);
|
|
140
|
+
const hMSB = parseInt(h.slice(0, 8), 16);
|
|
141
|
+
const hLSB = parseInt(h.slice(8, 16), 16);
|
|
142
|
+
const fArr = utils.flatArray(input[k]);
|
|
143
|
+
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
|
|
144
|
+
if (signalSize < 0) {
|
|
145
|
+
throw new Error(`Signal ${k} not found\n`);
|
|
146
|
+
}
|
|
147
|
+
if (fArr.length < signalSize) {
|
|
148
|
+
throw new Error(`Not enough values for input signal ${k}\n`);
|
|
149
|
+
}
|
|
150
|
+
if (fArr.length > signalSize) {
|
|
151
|
+
throw new Error(`Too many values for input signal ${k}\n`);
|
|
152
|
+
}
|
|
153
|
+
for (let i = 0; i < fArr.length; i++) {
|
|
154
|
+
const arrFr = utils.toArray32(utils.normalize(fArr[i], this.prime), this.n32)
|
|
155
|
+
for (let j = 0; j < this.n32; j++) {
|
|
156
|
+
this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j]);
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
this.instance.exports.setInputSignal(hMSB, hLSB, i);
|
|
160
|
+
input_counter++;
|
|
161
|
+
} catch (err) {
|
|
162
|
+
// console.log(`After adding signal ${i} of ${k}`)
|
|
163
|
+
throw new Error(err);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
});
|
|
168
|
+
if (input_counter < this.instance.exports.getInputSize()) {
|
|
169
|
+
throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async calculateWitness(input, sanityCheck) {
|
|
174
|
+
|
|
175
|
+
const w = [];
|
|
176
|
+
|
|
177
|
+
await this._doCalculateWitness(input, sanityCheck);
|
|
178
|
+
|
|
179
|
+
for (let i = 0; i < this.witnessSize; i++) {
|
|
180
|
+
this.instance.exports.getWitness(i);
|
|
181
|
+
const arr = new Uint32Array(this.n32);
|
|
182
|
+
for (let j = 0; j < this.n32; j++) {
|
|
183
|
+
arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
|
|
184
|
+
}
|
|
185
|
+
w.push(utils.fromArray32(arr));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return w;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
async calculateBinWitness(input, sanityCheck) {
|
|
193
|
+
|
|
194
|
+
const buff32 = new Uint32Array(this.witnessSize * this.n32);
|
|
195
|
+
const buff = new Uint8Array(buff32.buffer);
|
|
196
|
+
await this._doCalculateWitness(input, sanityCheck);
|
|
197
|
+
|
|
198
|
+
for (let i = 0; i < this.witnessSize; i++) {
|
|
199
|
+
this.instance.exports.getWitness(i);
|
|
200
|
+
const pos = i * this.n32;
|
|
201
|
+
for (let j = 0; j < this.n32; j++) {
|
|
202
|
+
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return buff;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
async calculateWTNSBin(input, sanityCheck) {
|
|
211
|
+
|
|
212
|
+
const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11);
|
|
213
|
+
const buff = new Uint8Array(buff32.buffer);
|
|
214
|
+
await this._doCalculateWitness(input, sanityCheck);
|
|
215
|
+
|
|
216
|
+
//"wtns"
|
|
217
|
+
buff[0] = "w".charCodeAt(0)
|
|
218
|
+
buff[1] = "t".charCodeAt(0)
|
|
219
|
+
buff[2] = "n".charCodeAt(0)
|
|
220
|
+
buff[3] = "s".charCodeAt(0)
|
|
221
|
+
|
|
222
|
+
//version 2
|
|
223
|
+
buff32[1] = 2;
|
|
224
|
+
|
|
225
|
+
//number of sections: 2
|
|
226
|
+
buff32[2] = 2;
|
|
227
|
+
|
|
228
|
+
//id section 1
|
|
229
|
+
buff32[3] = 1;
|
|
230
|
+
|
|
231
|
+
const n8 = this.n32 * 4;
|
|
232
|
+
//id section 1 length in 64bytes
|
|
233
|
+
const idSection1length = 8 + n8;
|
|
234
|
+
const idSection1lengthHex = idSection1length.toString(16);
|
|
235
|
+
buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
|
|
236
|
+
buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
|
|
237
|
+
|
|
238
|
+
//this.n32
|
|
239
|
+
buff32[6] = n8;
|
|
240
|
+
|
|
241
|
+
//prime number
|
|
242
|
+
this.instance.exports.getRawPrime();
|
|
243
|
+
|
|
244
|
+
let pos = 7;
|
|
245
|
+
for (let j = 0; j < this.n32; j++) {
|
|
246
|
+
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
|
247
|
+
}
|
|
248
|
+
pos += this.n32;
|
|
249
|
+
|
|
250
|
+
// witness size
|
|
251
|
+
buff32[pos] = this.witnessSize;
|
|
252
|
+
pos++;
|
|
253
|
+
|
|
254
|
+
//id section 2
|
|
255
|
+
buff32[pos] = 2;
|
|
256
|
+
pos++;
|
|
257
|
+
|
|
258
|
+
// section 2 length
|
|
259
|
+
const idSection2length = n8 * this.witnessSize;
|
|
260
|
+
const idSection2lengthHex = idSection2length.toString(16);
|
|
261
|
+
buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
|
|
262
|
+
buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
|
|
263
|
+
|
|
264
|
+
pos += 2;
|
|
265
|
+
for (let i = 0; i < this.witnessSize; i++) {
|
|
266
|
+
this.instance.exports.getWitness(i);
|
|
267
|
+
for (let j = 0; j < this.n32; j++) {
|
|
268
|
+
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
|
269
|
+
}
|
|
270
|
+
pos += this.n32;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return buff;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|