arnaucube
arnaucube/circom_testerPublic Self-hosted repository
circom_tester/wasm/utils.js
wasm/utils.jsRaw
 1const fnv = require("fnv-plus");
 2
 3module.exports.fnvHash = fnvHash;
 4module.exports.toArray32 = toArray32;
 5module.exports.fromArray32 = fromArray32;
 6module.exports.flatArray = flatArray;
 7
 8function toArray32(s,size) {
 9    const res = []; //new Uint32Array(size); //has no unshift
10    let rem = BigInt(s);
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
26function 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
35function 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
51function fnvHash(str) {
52    return fnv.hash(str, 64).hex();
53}