Skip to content

Commit a5ab77f

Browse files
committed
Support compiling to wasm
1 parent 76832b8 commit a5ab77f

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

index.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1-
const bn128 = require('./lib/index.asm.js')
1+
if (global.WebAssembly !== undefined) {
2+
const wasmModule = require('fs').readFileSync('./lib/rustbn.wasm')
3+
const bn128 = WebAssembly.instantiate(wasmModule, { env: {} }).then(function (bn128) {
4+
const ec_add = bn128.instance.exports.ec_add
5+
const ec_mul = bn128.instance.exports.ec_mul
6+
const ec_pairing = bn128.instance.exports.ec_pairing
7+
let memory = bn128.instance.exports.memory
28

3-
const ec_add = bn128.cwrap('ec_add', 'string', ['string'])
4-
const ec_mul = bn128.cwrap('ec_mul', 'string', ['string'])
5-
const ec_pairing = bn128.cwrap('ec_pairing', 'string', ['string'])
9+
function findFreeMemoryPtr () {
10+
// FIXME: implement
11+
return 42
12+
}
13+
14+
function writeHexString (buffer, startOffset, data) {
15+
// FIXME: implement
16+
console.log('writing to', startOffset)
17+
18+
let bufferView = new Uint8Array(buffer)
19+
data.copy(bufferView, startOffset)
20+
}
21+
22+
function extractHexString (buffer, startOffset) {
23+
console.log('reading from', startOffset)
24+
25+
const bufferView = new Uint8Array(buffer)
26+
27+
let endOffset = startOffset
28+
for (; bufferView[endOffset] !== 0; endOffset++) { }
29+
30+
return Buffer.from(bufferView.slice(startOffset, endOffset), 'hex')
31+
}
32+
33+
let ptr = findFreeMemoryPtr()
34+
writeHexString(memory.buffer, ptr, Buffer.from('00112200', 'hex'))
35+
let retPtr = ec_add(ptr)
36+
let ret = extractHexString(memory.buffer, retPtr)
37+
38+
console.log('returned', ret.toString('hex'))
39+
})
40+
} else {
41+
const bn128 = require('./lib/index.asm.js')
42+
const ec_add = bn128.cwrap('ec_add', 'string', ['string'])
43+
const ec_mul = bn128.cwrap('ec_mul', 'string', ['string'])
44+
const ec_pairing = bn128.cwrap('ec_pairing', 'string', ['string'])
45+
}
646

747
function bn128add (input) {
848
return Buffer.from(ec_add(input.toString('hex')), 'hex')

lib/rustbn.wasm

2.16 MB
Binary file not shown.

makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ all:
1313
sed -ibak 's/run()$$/Module\["arguments"\]=\[\];run();module\.exports=Module;/' lib/index.asm.js
1414

1515
wasm:
16-
cargo build --target=wasm32-unknown-emscripten --release
17-
mkdir -p exp
18-
find target/wasm32-unknown-emscripten/release/deps -type f -name "*.wasm" | xargs -I {} cp {} exp/rustbn.wasm
19-
find target/wasm32-unknown-emscripten/release/deps -type f ! -name "*.asm.js" -name "*.js" | xargs -I {} cp {} exp/index.wasm.js
16+
cargo build --target=wasm32-unknown-unknown --release
17+
cp target/wasm32-unknown-unknown/release/rustbn-js.wasm lib/rustbn.wasm

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ extern {
7272
}
7373

7474
fn main() {
75+
#[cfg(not(target_arch = "wasm32"))]
7576
unsafe {
7677
emscripten_exit_with_live_runtime();
7778
}

0 commit comments

Comments
 (0)