websnark/tools/buildwitness.js
Micah Zoltu f3a23e3a39 Removes most dependencies and switches to @noble/hashes.
* Browserify is incredibly high dependency count, so it was removed (esbuild will be added in future commit).
* NodeJS now comes with built-in support for testing, so mocha and chai are no longer required, removing most dependencies from this project.
* eslint isn't necessary for an unmaintained project, and can be used on a per-developer basis.
* package is only used to get version, which is useless and an unnecessary security risk.
* yargs brings in 16 dependencies and can be replaced with minimist which is just 1.
* normalized transitive dependencies of big-integer to all the same version.

These changes get us down to 5 dependencies total, with only one being relevant at runtime.

Also removes usage of `assert` and `Buffer`, which are NodeJS things.

Fixes a minor bug in `stringifybigint` that was incorrectly checking if something was a native `bigint`.
2024-12-02 15:33:24 +08:00

74 lines
1.7 KiB
JavaScript

const {unstringifyBigInts} = require("./stringifybigint.js");
const fs = require("fs");
const assert = require("assert");
const process = require("node:process")
var argv = require('minimist')(process.argv.slice(2), { boolean: ['h', 'help'] });
if (argv.help || argv.h) {
console.log(`node buildpkey.js -i "witness.json" -o "witness.bin"
-i --input default: witness.json
-o --output default: witness.bin
-h --help
Copyright (C) 2018 0kims association
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it under certain conditions;
see the COPYING file in the official repo directory at https://github.com/iden3/circom`)
process.exit()
}
const inputName = argv.input || argv.i || "witness.json";
const outputName = argv.output || argv.o || "witness.bin";
const witness = unstringifyBigInts(JSON.parse(fs.readFileSync(inputName, "utf8")));
function writeUint32(h, val) {
h.dataView.setUint32(h.offset, val, true);
h.offset += 4;
}
function writeBigInt(h, bi) {
for (let i=0; i<8; i++) {
const v = bi.shiftRight(i*32).and(0xFFFFFFFF).toJSNumber();
writeUint32(h, v);
}
}
function calculateBuffLen(witness) {
let size = 0;
// beta2, delta2
size += witness.length * 32;
return size;
}
const buffLen = calculateBuffLen(witness);
const buff = new ArrayBuffer(buffLen);
const h = {
dataView: new DataView(buff),
offset: 0
};
// writeUint32(h, witness.length);
for (let i=0; i<witness.length; i++) {
writeBigInt(h, witness[i]);
}
assert.equal(h.offset, buffLen);
var wstream = fs.createWriteStream(outputName);
wstream.write(Buffer.from(buff));
wstream.end();