arnaucube
arnaucube/websnarkPublic Self-hosted repository
websnark/src/build_testf1.js
src/build_testf1.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");
21const utils = require("./utils.js");
22
23module.exports = function buildTestF1(module) {
24
25    const q = bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
26
27    const pR2 = module.modules.f1m.pR2;
28    const n8 = module.modules.f1m.n64*8;
29
30    const pR3 = module.alloc(utils.bigInt2BytesLE(bigInt.one.shiftLeft(256).square().mod(q).shiftRight(128), n8));
31
32
33    function buildTestF1() {
34        const f = module.addFunction("testF1");
35        f.addParam("n", "i32");
36        f.addLocal("i", "i32");
37
38        const c = f.getCodeBuilder();
39
40        const pAux1 = module.alloc(n8);
41
42        f.addCode(c.setLocal("i", c.getLocal("n")));
43        f.addCode(c.block(c.loop(
44//            c.call("f1m_add", c.i32_const(pR2), c.i32_const(pR2), c.i32_const(pAux1)),
45            c.call("f1m_mul", c.i32_const(pR2), c.i32_const(pR2), c.i32_const(pAux1)),
46//            c.call("int_div", c.i32_const(pR2), c.i32_const(pR3), c.i32_const(pAux1), c.i32_const(0)),
47            c.setLocal("i", c.i32_sub(c.getLocal("i"), c.i32_const(1))),
48            c.br_if(1, c.i32_eqz ( c.getLocal("i") )),
49            c.br(0)
50        )));
51    }
52
53    buildTestF1();
54    module.exportFunction("testF1");
55};