TrueBitFoundation / truebit-toolchain

Utility for preparing Truebit tasks
MIT License
8 stars 3 forks source link

Wrapping wasm failed when I use libpbc. #4

Open imTyrant opened 5 years ago

imTyrant commented 5 years ago

I try to write a demo on Truebit with libgmp and libpbc, but it reported error when I use Truebit-toolchain to wrap the .wasm code.

Here is the demo I used:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pbc.h>
#include <pbc_test.h>

int main(int argc, char** argv) 
{
  pairing_t pairing;  
  char s[16384];
  FILE *fp = stdin;
  printf("try to read param file\n");
  fp = fopen("a.param", "r");
  if (!fp) pbc_die("error opening %s", argv[1]);
  printf("file read!\n");
  size_t count = fread(s, 1, 16384, fp);
  if (!count) pbc_die("input error");
  fclose(fp);
  printf("pairing init\n");
  if (pairing_init_set_buf(pairing, s, count)) pbc_die("pairing init failed");

  printf("formal work start here!\n");

  element_t g, h;
  element_t public_key, secret_key;
  element_t sig;
  element_t temp1, temp2;

  element_init_G2(g, pairing);
  element_init_G2(public_key, pairing);
  element_init_G1(h, pairing);
  element_init_G1(sig, pairing);
  element_init_GT(temp1, pairing);
  element_init_GT(temp2, pairing);
  element_init_Zr(secret_key, pairing);

  element_random(g);
  element_random(secret_key);
  element_pow_zn(public_key, g, secret_key);
  element_from_hash(h, "ABCDEF", 6);
  element_pow_zn(sig, h, secret_key);

  pairing_apply(temp1, sig, g, pairing);
  pairing_apply(temp2, h, public_key, pairing);
  if (!element_cmp(temp1, temp2))
  {
    printf("signature verifies\n");
  } 
  else 
  {
    printf("signature does not verify\n");
  }

  return 0;
}

and an extra parameter file a.param:

type a
q 8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791
h 12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776
r 730750818665451621361119245571504901405976559617
exp2 159
exp1 107
sign1 1
sign0 1

I compiled it in the docker with the command:

emcc pbctest.c -s WASM=1 - pbctest.js \
-I $EMSCRIPTEN/system/include \
-I $EMSCRIPTEN/system/include/pbc \
-lpbc -lgmp

The emrun result of it is correct. But, when I try to wrap the .wasm code by the following command:

node /emscripten-module-wrapper/prepare.js \
/workspace/pbc/pbctest.js \
--file /workspace/pbc/a.param \
--asmjs \
--out /workspace/pbc/dist

it reported:

/emscripten-module-wrapper/prepare.js:21
  return relativePathsArray.map(filePath => {
                            ^

TypeError: relativePathsArray.map is not a function
    at fixPaths (/emscripten-module-wrapper/prepare.js:21:29)
    at localizeArgv (/emscripten-module-wrapper/prepare.js:62:6)
    at Object.<anonymous> (/emscripten-module-wrapper/prepare.js:67:8)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)

The other codes only using standard c libraries work fine.

mrsmkl commented 5 years ago

The fix should be at emscripten-module-wrapper. Also added a new Dockerfile to https://github.com/TrueBitFoundation/wasm-ports/ Thanks for testing!