ZKCross SDK

The ZKCross SDK is divided into two parts: the client API and the host function API.

The client API is implemented in an authentic web application using e.g. TypeScript with React. It also has the capability to connect to a wallet, deploy WASM images and send prove and verify request.

The host function API is used by the WASM internal code logic which can be written in C/C++/Rust or any other high-level language and compiled into WebAssembly binary code. WASM host API is exported WASM host function provided by the WASM runtime, they are exposed as a WASM module so that the host function can be called.

export default async function () {
  if (instance != null) {
    return instance.exports;
  } else {
    module = await makeWasm({
      'global': {}, 
      'env': {
        'memory': new Memory({ initial: 10, limit: 100 }), 
        'table': new Table({ initial: 0, element: 'anyfunc' }), 
        'abort': () => {console.error("abort in wasm!"); throw new Error("Unsupported wasm api: abort");},
        'require': (b) => {if(!b){console.error("require failed"); throw new Error("Require failed");}},
        'wasm_input': () => {
          console.error("wasm_input should not been called in non-zkwasm mode");          throw new Error("Unsupported wasm api: wasm_input");
        }
        'kv_set':() => ...
        'kv_get':() => ...
      }
    });  
    console.log("module loaded", module);

WASM host functions provide a way for WASM modules to interact with the host environment, in the case of ZKCross which is the Web3 blockchain.

Let's see an host function API example, e.g when a WASM module want to store a value.

First, it need include the ZKC-SDK headfile.

#include "ZKC-SDK.h"

Then can just call Host function API kv_set() directly.

kv_set(key, value);

In ZKC-SDK, the kv_set() function was implemented to connect ZKC backend data layer.

The ZKCross API Table List

Client API

  1. web3subscribe
// tbd
  1. WASM Image operation and query
// Some code
  1. Task submit and ZK proving
// Some code
  1. Contract Proxy and Verification
// Some code

Host function API

ECC : bls functions

void blspair_g1(uint64_t x);
void blspair_g2(uint64_t x);
uint64_t blspair_pop(void);
 
void blssum_g1(uint64_t x);
uint64_t blssum_pop(void);
 
void blspair(uint64_t* g1, uint64_t* g2, uint64_t* g3);
void blssum(uint32_t size, uint64_t* g1, uint64_t *gr);

ECC: bn254 functions

void bn254pair_g1(uint64_t x);
void bn254pair_g2(uint64_t x);
uint64_t bn254pair_pop(void);
 
void bn254msm_g1(uint64_t x);
uint64_t bn254msm_pop(void);
 
void bn254pair(uint64_t* g1, uint64_t* g2, uint64_t* g3);
void bn254msm(uint32_t size, uint64_t* g1, uint64_t *gr);

Hash function

void sha256(uint8_t* data, uint32_t size, uint64_t* r);

Key value functions

void kv_setroot(uint64_t *root);
void kv_getroot(uint64_t *data);
void kv_get(const uint32_t key, uint64_t *data);
void kv_set(const uint32_t key, uint64_t *data)