arnaucube
arnaucube/websnarkPublic Self-hosted repository
websnark/src/utils.js
src/utils.jsRaw
 1/*
 2    Copyright 2019 0KIMS association.
 3
 4    This file is part of websnark (Web Assembly zkSnark Prover).
 5
 6    websnark is a free software: you can redistribute it and/or modify it
 7    under the terms of the GNU General Public License as published by
 8    the Free Software Foundation, either version 3 of the License, or
 9    (at your option) any later version.
10
11    websnark is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14    License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with websnark. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20const bigInt = require("big-integer");
21
22exports.bigInt2BytesLE = function bigInt2BytesLE(_a, len) {
23    const b = Array(len);
24    let v = bigInt(_a);
25    for (let i=0; i<len; i++) {
26        b[i] = v.and(0xFF).toJSNumber();
27        v = v.shiftRight(8);
28    }
29    return b;
30};
31
32exports.bigInt2U32LE = function bigInt2BytesLE(_a, len) {
33    const b = Array(len);
34    let v = bigInt(_a);
35    for (let i=0; i<len; i++) {
36        b[i] = v.and(0xFFFFFFFF).toJSNumber();
37        v = v.shiftRight(32);
38    }
39    return b;
40};