circom/ports/c/buildasm/buildzqfieldtester.js
ports/c/buildasm/buildzqfieldtester.jsRaw
1const chai = require("chai");
2const assert = chai.assert;
3
4const fs = require("fs");
5var tmp = require("tmp-promise");
6const path = require("path");
7const util = require("util");
8const exec = util.promisify(require("child_process").exec);
9
10const BuildZqField = require("./buildzqfield");
11
12module.exports = testField;
13
14async function testField(prime, test) {
15 tmp.setGracefulCleanup();
16
17 const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
18
19 const source = await BuildZqField(prime, "Fr");
20
21 // console.log(dir.path);
22
23 await fs.promises.writeFile(path.join(dir.path, "fr.asm"), source.asm, "utf8");
24 await fs.promises.writeFile(path.join(dir.path, "fr.h"), source.h, "utf8");
25 await fs.promises.writeFile(path.join(dir.path, "fr.c"), source.c, "utf8");
26
27 await exec(`cp ${path.join(__dirname, "tester.cpp")} ${dir.path}`);
28
29 await exec("nasm -fmacho64 --prefix _ " +
30 ` ${path.join(dir.path, "fr.asm")}`
31 );
32
33 await exec("g++" +
34 ` ${path.join(dir.path, "tester.cpp")}` +
35 ` ${path.join(dir.path, "fr.o")}` +
36 ` ${path.join(dir.path, "fr.c")}` +
37 ` -o ${path.join(dir.path, "tester")}` +
38 " -lgmp -g"
39 );
40
41 const inLines = [];
42 for (let i=0; i<test.length; i++) {
43 for (let j=0; j<test[i][0].length; j++) {
44 inLines.push(test[i][0][j]);
45 }
46 }
47 inLines.push("");
48
49 await fs.promises.writeFile(path.join(dir.path, "in.tst"), inLines.join("\n"), "utf8");
50
51 await exec(`${path.join(dir.path, "tester")}` +
52 ` <${path.join(dir.path, "in.tst")}` +
53 ` >${path.join(dir.path, "out.tst")}`);
54
55 const res = await fs.promises.readFile(path.join(dir.path, "out.tst"), "utf8");
56 const resLines = res.split("\n");
57
58 for (let i=0; i<test.length; i++) {
59 const expected = test[i][1].toString();
60 const calculated = resLines[i];
61
62 if (calculated != expected) {
63 console.log("FAILED");
64 for (let j=0; j<test[i][0].length; j++) {
65 console.log(test[i][0][j]);
66 }
67 console.log("Should Return: " + expected);
68 console.log("But Returns: " + calculated);
69 }
70
71 assert.equal(calculated, expected);
72 }
73
74}
75