Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support wasm target #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 44 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,48 @@
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)

let bufferView = new Uint8Array(buffer)
data.copy(bufferView, 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.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ extern "C" {
}

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