Created
May 17, 2019 09:41
-
-
Save s1na/92eae826c696de5e4d60708f8743e59f to your computer and use it in GitHub Desktop.
keccak256 wasm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const path = require('path') | |
const memset = (mem, data, offset) => { | |
const asBytes = new Uint8Array(mem.buffer, offset, data.length) | |
asBytes.set(data) | |
} | |
const memget = (mem, offset, length) => { | |
return Buffer.from(new Uint8Array(mem.buffer, offset, length)) | |
} | |
const wasmFile = fs.readFileSync(path.join(__dirname, 'keccak256_rhash.wasm')) | |
const mod = new WebAssembly.Module(wasmFile) | |
const instance = new WebAssembly.Instance(mod) | |
const mem = instance.exports.memory | |
const heapBase = instance.exports.__data_end | |
module.exports = (a) => { | |
data = a | |
// Zero out enough memory for input + output | |
memset(mem, Buffer.alloc(a.length + 32), heapBase) | |
// Write input to memory | |
memset(mem, a, heapBase) | |
try { | |
instance.exports._main(heapBase + a.length, heapBase, a.length) | |
} catch (e) { | |
// console.log(e) | |
} | |
return memget(mem, heapBase + a.length, 32) | |
} |
cdetrio
commented
Jul 9, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment