arnaucube
arnaucube/websnarkPublic Self-hosted repository
websnark/src/build_f1.js
src/build_f1.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
22const buildF1m =require("./build_f1m.js");
23
24module.exports = function buildF1(module, _q, _prefix, _f1mPrefix, _intPrefix) {
25
26    const q = bigInt(_q);
27    const n64 = Math.floor((q.minus(1).bitLength() - 1)/64) +1;
28    const n8 = n64*8;
29
30    const prefix = _prefix || "f1";
31    if (module.modules[prefix]) return prefix;  // already builded
32    module.modules[prefix] = {
33        n64: n64
34    };
35
36    const intPrefix = _intPrefix || "int";
37    const f1mPrefix = buildF1m(module, q, _f1mPrefix, intPrefix);
38
39
40    const pR2 =     module.modules[f1mPrefix].pR2;
41    const pq =     module.modules[f1mPrefix].pq;
42
43    function buildMul() {
44        const pAux1 = module.alloc(n8);
45
46        const f = module.addFunction(prefix+ "_mul");
47        f.addParam("x", "i32");
48        f.addParam("y", "i32");
49        f.addParam("r", "i32");
50
51        const c = f.getCodeBuilder();
52        f.addCode(c.call(f1mPrefix + "_mul", c.getLocal("x"), c.getLocal("y"), c.i32_const(pAux1)));
53        f.addCode(c.call(f1mPrefix + "_mul", c.i32_const(pAux1), c.i32_const(pR2), c.getLocal("r")));
54    }
55
56    function buildInverse() {
57
58        const f = module.addFunction(prefix+ "_inverse");
59        f.addParam("x", "i32");
60        f.addParam("r", "i32");
61
62        const c = f.getCodeBuilder();
63        f.addCode(c.call(intPrefix + "_inverseMod", c.getLocal("x"), c.i32_const(pq), c.getLocal("r")));
64    }
65
66    buildMul();
67    buildInverse();
68    module.exportFunction(f1mPrefix + "_add", prefix + "_add");
69    module.exportFunction(f1mPrefix + "_sub", prefix + "_sub");
70    module.exportFunction(f1mPrefix + "_neg", prefix + "_neg");
71    module.exportFunction(prefix + "_mul");
72    module.exportFunction(prefix + "_inverse");
73    module.exportFunction(f1mPrefix + "_copy", prefix+"_copy");
74    module.exportFunction(f1mPrefix + "_zero", prefix+"_zero");
75    module.exportFunction(f1mPrefix + "_one", prefix+"_one");
76    module.exportFunction(f1mPrefix + "_isZero", prefix+"_isZero");
77    module.exportFunction(f1mPrefix + "_eq", prefix+"_eq");
78
79    return prefix;
80};