accordproject / ergo

Programming Language for Smart Legal Contracts
https://accordproject.org/projects/ergo
Apache License 2.0
158 stars 55 forks source link

Wasm Interoperability #727

Open jeromesimeon opened 4 years ago

jeromesimeon commented 4 years ago

Is your feature request related to a problem? Please describe.

Would anyone like to call Wasm from Ergo?

Describe the solution you'd like

A way to call Wasm modules from Ergo contracts.

Additional context

WASM specification: https://webassembly.github.io/spec/core/

Reference implementation in OCaml: https://github.com/WebAssembly/spec

WASM/JS interface: https://webassembly.github.io/spec/js-api/index.html

Interesting bits about WASM/JS interop performances: https://hacks.mozilla.org/2018/10/calls-between-javascript-and-webassembly-are-finally-fast-🎉/

jeromesimeon commented 4 years ago

Things we might need to get this working:

aksharma53 commented 4 years ago

I would like to work on this project.

jolanglinais commented 4 years ago

Great @AnalystAbhishek! If you're interested in the GSoC project around WASM, be sure to look into the resources we have pinned in our slack channel.

aksharma53 commented 4 years ago

Yeah I'm already going through the post shared by Dan and Jerome on slack channel.

aksharma53 commented 4 years ago

I'have finished my first task of understanding the whole documentation of Accord Project and has a prior knowledge on WASM as well as Ocaml. I have also set up the enviroment to start work on it, should I start making purposal ?

jeromesimeon commented 4 years ago

Some notes from today's discussion on the WASM backend.

Plan

  1. ImpWasm = Imp over EJson+SumTypes (i.e., do not encode left/right as objects) @jeromesimeon

  2. Mini compiler: take imp_ejson AST (just constant + a few operators) and compile to WASM AST + (static) link runtime + run @pkel

  3. Grow the runtime supported (& decide which runtime operators can be eliminated by the compiler) @pkel

  4. Grow the subset of imp_ejson AST being compiled

    1. if-then-else
    2. statements: for loops
    3. blocks?? probably need to eliminate blocks because of scoping

Blocks & scoping @jeromesimeon

WASM doesn't support block-level scoping (not 'let' only 'var')

Imp supports block-level scoping. e.g.,:

{ let x = 1;
  let y = 3;
  { let x = 2;
    x + y } }

Maybe removed by renaming/blocks merging:

{ let x1 = 1 ;
  let y = 3;
  let x2 = 2;
  x2 + y }

Runtime operators

Examples of runtime operators

concat: [a:1,b:2] + [c:3]     ==> [a:1,b:2,c:3]
concat: [a:1,b:2] + [c:3,b:4] ==> [a:1,c:3,b:4]

merge: [a:1,b:2] + [c:3]     ==> some([a:1,b:2,c:3])
merge: [a:1,b:2] + [c:3,b:2] ==> some([a:1,b:2,c:3])
merge: [a:1,b:2] + [c:3,b:4] ==> none

nth: [1,2,3] + 0 ==> some(1)
nth: [1,2,3] + 3 ==> none
jeromesimeon commented 4 years ago

Todo list, based on latest development in Q*cert (wasm branch) and Ergo (release-1.0 branch) :

  1. [x] Get the release-1.0 branch of Ergo to build on the wasm branch of Q*cert (requires per-module extraction)
  2. [x] Add a -target wasm option to the Ergo compiler, hook it up to a corresponding ErgoImptoWasm translation backed up by your compiler
    • [x] Turn ImpEJson to Wasm translation into a functor so it can be used both in Q*cert and Ergo (@pkel)
  3. [x] Support for objects ("branded values")
    • [ ] Turn type hierarchy into a proper constant in the generated code, or a parameter of the compilation rather than eval (@jeromesimeon)
    • [x] Implement sub_brands_dec (or equivalent) in runtime (@pkel)
  4. [ ] Compile time elimination of runtime operators (@jeromesimeon)
    • [x] Brand
    • [ ] Unbrand (is it possible?)
    • [ ] aggregate operators (e.g., count, sum, avg)
    • [ ] which other?
  5. [ ] Add a -target imp_ejson option to the Ergo compiler? Along with a proper pretty printer consistent with the new imp_ejson syntax we saw in the demo
  6. [ ] Figure out runtime/testing, missing runtime operators for helloworld etc. (@pkel)
  7. [ ] [Not Wasm Specific] Still need to update release-1.0 of Ergo to use the latest development version of Concerto (alpha of Concerto 1.0)
  8. [ ] [Not Wasm Specific] Merge release-1.0 branch of Ergo into master
  9. [ ] Merge wasm branch of Qcert into master ; publish Q\cert ; publish Concerto ; publish Ergo
  10. [ ] Tada!