arnaucube
arnaucube/websnarkPublic Self-hosted repository
websnark/src/build_pol.js
src/build_pol.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
 20module.exports = function buildPol(module, prefix, prefixField) {
 21
 22    const n64 = module.modules[prefixField].n64;
 23    const n8 = n64*8;
 24
 25
 26    function buildZero() {
 27        const f = module.addFunction(prefix+"_zero");
 28        f.addParam("px", "i32");
 29        f.addParam("n", "i32");
 30        f.addLocal("lastp", "i32");
 31        f.addLocal("p", "i32");
 32
 33        const c = f.getCodeBuilder();
 34
 35        f.addCode(
 36            c.setLocal("p", c.getLocal("px")),
 37            c.setLocal(
 38                "lastp",
 39                c.i32_add(
 40                    c.getLocal("px"),
 41                    c.i32_mul(
 42                        c.getLocal("n"),
 43                        c.i32_const(n8)
 44                    )
 45                )
 46            ),
 47            c.block(c.loop(
 48                c.br_if(
 49                    1,
 50                    c.i32_eq(
 51                        c.getLocal("p"),
 52                        c.getLocal("lastp")
 53                    )
 54                ),
 55                c.call(prefixField + "_zero", c.getLocal("p")),
 56                c.setLocal("p", c.i32_add(c.getLocal("p"), c.i32_const(n8))),
 57                c.br(0)
 58            ))
 59        );
 60    }
 61
 62    function buildConstructLC() {
 63        const f = module.addFunction(prefix+"_constructLC");
 64        f.addParam("ppolynomials", "i32");
 65        f.addParam("psignals", "i32");
 66        f.addParam("nSignals", "i32");
 67        f.addParam("pres", "i32");
 68        f.addLocal("i", "i32");
 69        f.addLocal("j", "i32");
 70        f.addLocal("pp", "i32");
 71        f.addLocal("ps", "i32");
 72        f.addLocal("pd", "i32");
 73        f.addLocal("ncoefs", "i32");
 74
 75        const c = f.getCodeBuilder();
 76
 77        const aux = c.i32_const(module.alloc(n8));
 78
 79        f.addCode(
 80            c.setLocal("i", c.i32_const(0)),
 81            c.setLocal("pp", c.getLocal("ppolynomials")),
 82            c.setLocal("ps", c.getLocal("psignals")),
 83            c.block(c.loop(
 84                c.br_if(
 85                    1,
 86                    c.i32_eq(
 87                        c.getLocal("i"),
 88                        c.getLocal("nSignals")
 89                    )
 90                ),
 91
 92                c.setLocal("ncoefs", c.i32_load(c.getLocal("pp"))),
 93                c.setLocal("pp", c.i32_add(c.getLocal("pp"), c.i32_const(4))),
 94
 95                c.setLocal("j", c.i32_const(0)),
 96                c.block(c.loop(
 97                    c.br_if(
 98                        1,
 99                        c.i32_eq(
100                            c.getLocal("j"),
101                            c.getLocal("ncoefs")
102                        )
103                    ),
104
105                    c.setLocal(
106                        "pd",
107                        c.i32_add(
108                            c.getLocal("pres"),
109                            c.i32_mul(
110                                c.i32_load(c.getLocal("pp")),
111                                c.i32_const(n8)
112                            )
113                        )
114                    ),
115
116                    c.setLocal("pp", c.i32_add(c.getLocal("pp"), c.i32_const(4))),
117
118
119                    c.call(
120                        prefixField + "_mul",
121                        c.getLocal("ps"),
122                        c.getLocal("pp"),
123                        aux
124                    ),
125
126                    c.call(
127                        prefixField + "_add",
128                        aux,
129                        c.getLocal("pd"),
130                        c.getLocal("pd")
131                    ),
132
133                    c.setLocal("pp", c.i32_add(c.getLocal("pp"), c.i32_const(n8))),
134                    c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))),
135                    c.br(0)
136                )),
137
138                c.setLocal("ps", c.i32_add(c.getLocal("ps"), c.i32_const(n8))),
139                c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
140                c.br(0)
141            ))
142        );
143
144    }
145
146    buildZero();
147    buildConstructLC();
148
149
150    module.exportFunction(prefix + "_zero");
151    module.exportFunction(prefix + "_constructLC");
152
153    return prefix;
154
155
156
157
158};