arnaucube
arnaucube/websnarkPublic Self-hosted repository
websnark/src/build_mem.js
src/build_mem.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 buildMem(module, prefix, prefixField) {
 21
 22    function buildCopy() {
 23        const f = module.addFunction(prefix+"copy");
 24        f.addParam("s", "i32");
 25        f.addParam("d", "i32");
 26        f.addParam("n", "i32");
 27        f.addLocal("bytesAfter", "i32");
 28        f.addLocal("sp", "i32");
 29
 30        const c = f.getCodeBuilder();
 31
 32        f.addCode(
 33            c.setLocal(
 34                "n64",
 35                c.i32_shl(
 36                    c.getLocal("n"),
 37                    c.i32_const(3)
 38                )
 39            ),
 40            c.setLocal(
 41                "bytesAfter",
 42                c.i32_and(
 43                    c.getLocal("n"),
 44                    c.i32_const(0x7)
 45                )
 46            ),
 47            c.setLocal("sp", c.getLocal("s")),
 48            c.setLocal("dp", c.getLocal("d")),
 49            c.setLocal("lastsp",
 50                c.add(
 51                    c.getLocal("s"),
 52                    c.i32_and(
 53                        c.getLocal("n"),
 54                        c.i32_const(0xFFFFFFF8)
 55                    )
 56                )
 57            ),
 58
 59
 60            c.block(c.loop(
 61                c.br_if(
 62                    1,
 63                    c.i32_eq(
 64                        c.getLocal("sp"),
 65                        c.getLocal("lastsp")
 66                    )
 67                ),
 68                c.i64_store(
 69                    c.getLocal("dp"),
 70                    c.i64_load(
 71                        c.getLocal("sp")
 72                    )
 73                ),
 74                c.setLocal("sp", c.i32_add(c.getLocal("sp"), c.i32_const(8))),
 75                c.setLocal("dp", c.i32_add(c.getLocal("dp"), c.i32_const(8))),
 76                c.br(0)
 77            )),
 78
 79            c.if(
 80                c.getLocal("bytesAfter"),
 81                [
 82                    ...c.if(
 83                        c.i32_and(c.getLocal("bytesAfter"), c.i32_const(0x4)),
 84                        [
 85                            ...c.i32_store(
 86                                c.getLocal("dp"),
 87                                c.i32_load(
 88                                    c.getLocal("sp")
 89                                )
 90                            ),
 91                            ...c.setLocal("sp", c.i32_add(c.getLocal("sp"), c.i32_const(4))),
 92                            ...c.setLocal("dp", c.i32_add(c.getLocal("dp"), c.i32_const(4))),
 93                        ]
 94                    ),
 95                    ...c.if(
 96                        c.i32_and(c.getLocal("bytesAfter"), c.i32_const(0x2)),
 97                        [
 98                            ...c.i32_store16(
 99                                c.getLocal("dp"),
100                                c.i32_load16(
101                                    c.getLocal("sp")
102                                )
103                            ),
104                            ...c.setLocal("sp", c.i32_add(c.getLocal("sp"), c.i32_const(2))),
105                            ...c.setLocal("dp", c.i32_add(c.getLocal("dp"), c.i32_const(2))),
106                        ]
107                    ),
108                    ...c.if(
109                        c.i32_and(c.getLocal("bytesAfter"), c.i32_const(0x1)),
110                        c.i32_store8(
111                            c.getLocal("dp"),
112                            c.i32_load8(
113                                c.getLocal("sp")
114                            )
115                        )
116                    )
117                ]
118            )
119        );
120
121    }
122
123    buildCopy();
124    module.exportFunction(prefix+"_copy");
125
126    return prefix;
127};