Skip to content

Commit

Permalink
Support compiling to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Jan 9, 2019
1 parent 76832b8 commit ad5097b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
45 changes: 41 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
const bn128 = require('./lib/index.asm.js')
if (global.WebAssembly !== undefined) {
const wasmModule = require('fs').readFileSync('./lib/rustbn.wasm')
const bn128 = WebAssembly.instantiate(wasmModule, { env: {} }).then(function (bn128) {
const ec_add = bn128.instance.exports.ec_add
const ec_mul = bn128.instance.exports.ec_mul
const ec_pairing = bn128.instance.exports.ec_pairing
let memory = bn128.instance.exports.memory

const ec_add = bn128.cwrap('ec_add', 'string', ['string'])
const ec_mul = bn128.cwrap('ec_mul', 'string', ['string'])
const ec_pairing = bn128.cwrap('ec_pairing', 'string', ['string'])
function findFreeMemoryPtr () {
// FIXME: implement
return 42
}

function writeHexString (buffer, startOffset, data) {
// FIXME: implement
console.log('writing to', startOffset)
}

function extractHexString (buffer, startOffset) {
console.log('reading from', startOffset)

const bufferView = new Uint8Array(buffer)

let endOffset = startOffset
for (; bufferView[endOffset] !== 0; endOffset++) { }

return Buffer.from(bufferView.slice(startOffset, endOffset), 'hex')
}

let ptr = findFreeMemoryPtr()
writeHexString(memory.buffer, ptr, Buffer.from('00112200', 'hex'))
let retPtr = ec_add(ptr)
let ret = extractHexString(memory.buffer, retPtr)

console.log('returned', ret.toString('hex'))
})
} else {
const bn128 = require('./lib/index.asm.js')
const ec_add = bn128.cwrap('ec_add', 'string', ['string'])
const ec_mul = bn128.cwrap('ec_mul', 'string', ['string'])
const ec_pairing = bn128.cwrap('ec_pairing', 'string', ['string'])
}

function bn128add (input) {
return Buffer.from(ec_add(input.toString('hex')), 'hex')
Expand Down
Binary file added lib/rustbn.wasm
Binary file not shown.
6 changes: 2 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ all:
sed -ibak 's/run()$$/Module\["arguments"\]=\[\];run();module\.exports=Module;/' lib/index.asm.js

wasm:
cargo build --target=wasm32-unknown-emscripten --release
mkdir -p exp
find target/wasm32-unknown-emscripten/release/deps -type f -name "*.wasm" | xargs -I {} cp {} exp/rustbn.wasm
find target/wasm32-unknown-emscripten/release/deps -type f ! -name "*.asm.js" -name "*.js" | xargs -I {} cp {} exp/index.wasm.js
cargo build --target=wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/rustbn-js.wasm lib/rustbn.wasm
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ extern {
}

fn main() {
#[cfg(not(target_arch = "wasm32"))]
unsafe {
emscripten_exit_with_live_runtime();
}
Expand Down

0 comments on commit ad5097b

Please sign in to comment.