UNPKG

wasmcurves

Version:

elliptic curves implementations in wasm

81 lines (64 loc) 2.34 kB
/* Copyright 2019 0KIMS association. This file is part of wasmsnark (Web Assembly zkSnark Prover). wasmsnark is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. wasmsnark is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with wasmsnark. If not, see <https://www.gnu.org/licenses/>. */ module.exports = function buildApplyKey(module, fnName, gPrefix, frPrefix, sizeGIn, sizeGOut, sizeF, opGtimesF) { const f = module.addFunction(fnName); f.addParam("pIn", "i32"); f.addParam("n", "i32"); f.addParam("pFirst", "i32"); f.addParam("pInc", "i32"); f.addParam("pOut", "i32"); f.addLocal("pOldFree", "i32"); f.addLocal("i", "i32"); f.addLocal("pFrom", "i32"); f.addLocal("pTo", "i32"); const c = f.getCodeBuilder(); const t = c.i32_const(module.alloc(sizeF)); f.addCode( c.setLocal("pFrom", c.getLocal("pIn")), c.setLocal("pTo", c.getLocal("pOut")), ); // t = first f.addCode( c.call( frPrefix + "_copy", c.getLocal("pFirst"), t ) ); f.addCode( c.setLocal("i", c.i32_const(0)), c.block(c.loop( c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("n") )), c.call( opGtimesF, c.getLocal("pFrom"), t, c.getLocal("pTo") ), c.setLocal("pFrom", c.i32_add(c.getLocal("pFrom"), c.i32_const(sizeGIn))), c.setLocal("pTo", c.i32_add(c.getLocal("pTo"), c.i32_const(sizeGOut))), // t = t* inc c.call( frPrefix + "_mul", t, c.getLocal("pInc"), t ), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0) )) ); module.exportFunction(fnName); };