|
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 |
2 | 8 |
|
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 | +} |
6 | 46 |
|
7 | 47 | function bn128add (input) {
|
8 | 48 | return Buffer.from(ec_add(input.toString('hex')), 'hex')
|
|
0 commit comments