commit dd8c162b5f486132919e749a9ccea62af44ad9a4
parent e550a5cba68e8db5e658a41336548ce9ec4672b6
Author: nolash <dev@holbrook.no>
Date: Wed, 15 Sep 2021 20:19:49 +0200
Add missing wasm relevant files
Diffstat:
6 files changed, 193 insertions(+), 0 deletions(-)
diff --git a/scripts/wasm/bmt.js b/scripts/wasm/bmt.js
@@ -0,0 +1,34 @@
+const fs = require('fs');
+
+const memory = new WebAssembly.Memory({initial: 3});
+const table = new WebAssembly.Table({initial: 10, element: 'anyfunc'});
+const importsObj = {
+ env: {
+ memory: memory,
+ __linear_memory: memory,
+ __indirect_function_table: table,
+ dlog: function(prefix, value) {
+ console.debug(prefix, value);
+ },
+ }
+}
+
+async function init() {
+ const code = fs.readFileSync('./lib/swarm.wasm');
+ const m = new WebAssembly.Module(code);
+ const i = new WebAssembly.Instance(m, importsObj);
+
+ let r;
+ let data = new Uint8Array(memory.buffer, 65535*2+1024, 3);
+ data.set([0x66, 0x6f, 0x6f], 0);
+ r = i.exports.bmt_hash_heap(65535*2+1024, 3, BigInt(3));
+
+ let outBuf = new Uint8Array(memory.buffer, r, 32);
+ let inBuf = new Uint8Array(memory.buffer, r + 32, 3);
+
+ i.exports.bmt_hash_free(r);
+
+ process.stdout.write(Buffer.from(outBuf).toString("hex") + "\n");
+}
+
+init();
diff --git a/scripts/wasm/keccak.js b/scripts/wasm/keccak.js
@@ -0,0 +1,35 @@
+const fs = require('fs');
+
+const memory = new WebAssembly.Memory({initial: 2});
+const table = new WebAssembly.Table({initial: 3, element: 'anyfunc'});
+const importsObj = {
+ env: {
+ memory: memory,
+ __linear_memory: memory,
+ __indirect_function_table: table,
+ },
+}
+
+async function init() {
+ const code_keccak = fs.readFileSync('./lib/keccak.wasm');
+ const m_keccak = new WebAssembly.Module(code_keccak);
+ const i_keccak = new WebAssembly.Instance(m_keccak, importsObj);
+
+ let r;
+ const p = i_keccak.exports.keccak_hash_heap_init(32, 3);
+ console.debug('r ' + p);
+
+ let outBuf = new Uint8Array(memory.buffer, p, 256);
+ let inBuf = new Uint8Array(memory.buffer, p + 256, 3);
+ inBuf.set([0x66, 0x6f, 0x6f], 0);
+ r = i_keccak.exports.keccak_hash(p, 32, p+256, 3, 200-64, 1);
+ if (r < 0) {
+ console.error('error ' + r);
+ } else {
+ console.log('in ' + Buffer.from(new Uint8Array(memory.buffer, p+256, 3)).toString('hex'));
+ console.log('out ' + Buffer.from(new Uint8Array(memory.buffer, p, 32)).toString('hex'));
+ }
+ i_keccak.exports.keccak_hash_heap_free(p);
+}
+
+init();
diff --git a/scripts/wasm/lib b/scripts/wasm/lib
@@ -0,0 +1 @@
+../../build/wasm
+\ No newline at end of file
diff --git a/src/wasm.c b/src/wasm.c
@@ -0,0 +1,41 @@
+//#include <string.h>
+//#include <stdlib.h>
+
+#include "keccak-tiny.h"
+#include "bmt.h"
+
+extern unsigned char __heap_base;
+extern int bmt_init_ptr(bmt_t *bctx, const char *input, const size_t input_length, const char *data_length);
+extern void *malloc(size_t v);
+extern void *memcpy(void *dst, void *src, size_t c);
+extern void dlog(int p, int v);
+
+//size_t keccak_hash_heap_init(size_t outLen, size_t inLen) {
+// //return (char*)__heap_base + (1024 * 64);
+// return ()(malloc(outLen + inLen));
+//}
+
+#define HEAP_PAGE_LENGTH 1024
+unsigned char *bmt_hash_heap_init(const size_t input_length) {
+ unsigned char *z = (unsigned char*)malloc(input_length);
+
+ dlog(95, z);
+ return z;
+}
+
+
+//int bmt_hash_heap(size_t out_offset, int in_offset, const size_t input_length, int data_length_offset) {
+int bmt_hash_heap(unsigned char *base, int out_offset, int in_offset, const size_t input_length, int data_length_offset) {
+ int r;
+
+ bmt_t bctx;
+
+ r = bmt_init_ptr(&bctx, (const char*)(base + in_offset), input_length, (const char*)(base + data_length_offset));
+ if (r != 0) {
+ return r;
+ }
+ r = bmt_sum(&bctx);
+ memcpy((char*)(__heap_base + out_offset), bctx.buf, _SWARM_WORD_SIZE);
+
+ return r;
+}
diff --git a/src/wasm/common.c b/src/wasm/common.c
@@ -0,0 +1,53 @@
+#include <stddef.h>
+
+#define MALLOC_PAGE_SIZE 1024
+
+extern unsigned char __heap_base;
+extern void dlog(int p, int v);
+
+void *malloc(size_t c) {
+ int i;
+ int n;
+ int offset;
+ unsigned char *offset_p;
+
+ unsigned char *ptr;
+ unsigned char *base = (unsigned char*)(&__heap_base);
+
+ offset = *((int*)(base));
+
+ n = (c / MALLOC_PAGE_SIZE) + 1;
+ n *= MALLOC_PAGE_SIZE;
+
+ ptr = base + offset;
+
+ dlog(77, &c);
+ offset += n;
+ offset_p = &offset;
+ for (i = 0; i < 4; i++) {
+ *(base+i) = *(offset_p+i);
+ }
+
+ offset = *((int*)(base));
+
+ return (void*)ptr;
+}
+
+void *memset(void *p, int v, size_t c) {
+ int i;
+
+ for (i = 0; i < c; i++) {
+ *((char*)(p+i)) = *((char*)(v+i));
+ }
+ return p;
+}
+
+void *memcpy(void *dst, void *src, size_t c) {
+ int i ;
+
+ dlog(12, c);
+ for (i = 0; i < c; i++) {
+ *((char*)(dst+i)) = *((char*)(src+i));
+ }
+ return dst;
+}
diff --git a/src/wasm/wasm.c b/src/wasm/wasm.c
@@ -0,0 +1,28 @@
+#include <string.h>
+#include <stdlib.h>
+
+#include "bmt.h"
+
+extern dlog(int p, int v);
+
+size_t keccak_hash_heap_init(size_t outLen, size_t inLen) {
+ return (size_t)malloc(outLen + inLen);
+}
+
+void keccak_hash_heap_free(void *mem) {
+ free(mem);
+}
+
+void *bmt_hash_heap(const char *input, const size_t input_length, const bmt_spansize_t data_length) {
+ char *out;
+
+ out = malloc(_SWARM_WORD_SIZE + input_length);
+
+ bmt_hash(out, input, input_length, data_length);
+
+ return out;
+}
+
+void bmt_hash_free(void * out) {
+ free(out);
+}