websnark/test/int.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

86 lines
2.4 KiB
JavaScript

const assert = require("assert");
const { describe, it, beforeEach } = require("node:test")
const bigInt = require("big-integer");
const buildProtoboard = require("../src/protoboard.js");
const buildInt = require("../src/build_int.js");
const buildTest = require("../src/build_test.js");
const helpers = require("./helpers/helpers.js");
describe("Basic tests for Int", () => {
let pbInt;
beforeEach(async () => {
pbInt = await buildProtoboard((module) => {
buildInt(module, 4);
buildTest(module, "int_mul");
buildTest(module, "int_mulOld");
}, 32);
});
it("It should do a basic multiplication", async () => {
let c;
const pA = pbInt.alloc();
const pB = pbInt.alloc();
const pC = pbInt.alloc(64);
const values = helpers.genValues(8, false);
for (let i=0; i<values.length; i++) {
for (let j=0; j<values.length; j++) {
pbInt.set(pA, values[i]);
pbInt.set(pB, values[j]);
// console.log(values[i].toString(16));
// console.log(values[j].toString(16));
pbInt.int_mul(pA, pB, pC);
c = pbInt.get(pC, 1, 64);
// console.log("Result: " + c.toString(16));
// console.log("Refere: " + values[i].times(values[j]).toString(16));
assert(c.equals(values[i].times(values[j])));
}
}
});
it("It should profile int", { timeout: 10000000 }, async () => {
const pA = pbInt.alloc();
const pB = pbInt.alloc();
const pC = pbInt.alloc(64);
let start, end, time;
const A = bigInt.one.shiftLeft(256).minus(1);
const B = bigInt.one.shiftLeft(256).minus(1);
pbInt.set(pA, A);
pbInt.set(pB, B);
start = new Date().getTime();
pbInt.test_int_mul(pA, pB, pC, 50000000);
end = new Date().getTime();
time = end - start;
const c1 = pbInt.get(pC, 1, 64);
assert(c1.equals(A.times(B)));
console.log("Mul Time (ms): " + time);
start = new Date().getTime();
pbInt.test_int_mulOld(pA, pB, pC, 50000000);
end = new Date().getTime();
time = end - start;
const c2 = pbInt.get(pC, 1, 64);
assert(c2.equals(A.times(B)));
console.log("Mul Old Time (ms): " + time);
});
});