circom/src/utils.js
src/utils.jsRaw
1const fnv = require("fnv-plus");
2const bigInt = require("big-integer");
3
4module.exports.ident =ident;
5
6module.exports.extractSizes =extractSizes;
7module.exports.flatArray = flatArray;
8module.exports.csArr = csArr;
9module.exports.accSizes = accSizes;
10module.exports.fnvHash = fnvHash;
11module.exports.stringifyBigInts = stringifyBigInts;
12module.exports.unstringifyBigInts = unstringifyBigInts;
13module.exports.sameSizes = sameSizes;
14module.exports.isDefined = isDefined;
15module.exports.accSizes2Str = accSizes2Str;
16
17function ident(text) {
18 if (typeof text === "string") {
19 let lines = text.split("\n");
20 for (let i=0; i<lines.length; i++) {
21 if (lines[i]) lines[i] = " "+lines[i];
22 }
23 return lines.join("\n");
24 } else if (Array.isArray(text)) {
25 for (let i=0; i<text.length; i++ ) {
26 text[i] = ident(text[i]);
27 }
28 return text;
29 }
30}
31
32function extractSizes (o) {
33 if (! Array.isArray(o)) return [];
34 return [o.length, ...extractSizes(o[0])];
35}
36
37function flatArray(a) {
38 var res = [];
39 fillArray(res, a);
40 return res;
41
42 function fillArray(res, a) {
43 if (Array.isArray(a)) {
44 for (let i=0; i<a.length; i++) {
45 fillArray(res, a[i]);
46 }
47 } else {
48 res.push(bigInt(a));
49 }
50 }
51}
52
53// Input [1,2,3]
54// Returns " ,1 ,2, 3"
55function csArr(_arr) {
56 let S = "";
57 const arr = _arr || [];
58 for (let i=0; i<arr.length; i++) {
59 S = " ,"+arr[i];
60 }
61 return S;
62}
63
64function accSizes(_sizes) {
65 const sizes = _sizes || [];
66 const accSizes = [1, 0];
67 for (let i=sizes.length-1; i>=0; i--) {
68 accSizes.unshift(accSizes[0]*sizes[i]);
69 }
70 return accSizes;
71}
72
73function fnvHash(str) {
74 return fnv.hash(str, 64).hex();
75}
76
77
78
79function stringifyBigInts(o) {
80 if ((typeof(o) == "bigint") || o.isZero !== undefined) {
81 return o.toString(10);
82 } else if (Array.isArray(o)) {
83 return o.map(stringifyBigInts);
84 } else if (typeof o == "object") {
85 const res = {};
86 for (let k in o) {
87 res[k] = stringifyBigInts(o[k]);
88 }
89 return res;
90 } else {
91 return o;
92 }
93}
94
95
96
97function unstringifyBigInts(o) {
98 if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) {
99 return bigInt(o);
100 } else if (Array.isArray(o)) {
101 return o.map(unstringifyBigInts);
102 } else if (typeof o == "object") {
103 const res = {};
104 for (let k in o) {
105 res[k] = unstringifyBigInts(o[k]);
106 }
107 return res;
108 } else {
109 return bigInt(o);
110 }
111}
112
113function sameSizes(s1, s2) {
114 if (!Array.isArray(s1)) return false;
115 if (!Array.isArray(s2)) return false;
116 if (s1.length != s2.length) return false;
117 for (let i=0; i<s1.length; i++) {
118 if (s1[i] != s2[i]) return false;
119 }
120 return true;
121}
122
123function isDefined(v) {
124 return ((typeof(v) != "undefined")&&(v != null));
125}
126
127function accSizes2Str(sizes) {
128 if (sizes.length == 2) return "";
129 return `[${sizes[0]/sizes[1]}]`+accSizes2Str(sizes.slice(1));
130}
131
132
133
134