github.com/aergoio/aergo@v1.3.1/contract/crypto_module.c (about)

     1  #include "_cgo_export.h"
     2  #include "util.h"
     3  
     4  static int crypto_sha256(lua_State *L)
     5  {
     6      size_t len;
     7      char *arg;
     8      struct LuaCryptoSha256_return ret;
     9  
    10      luaL_checktype(L, 1, LUA_TSTRING);
    11      arg = (char *)lua_tolstring(L, 1, &len);
    12  
    13      ret = LuaCryptoSha256(L, arg, len);
    14      if (ret.r1 < 0) {
    15          strPushAndRelease(L, ret.r1);
    16          lua_error(L);
    17      }
    18      strPushAndRelease(L, ret.r0);
    19  	return 1;
    20  }
    21  
    22  static int crypto_ecverify(lua_State *L)
    23  {
    24      char *msg, *sig, *addr;
    25      struct LuaECVerify_return ret;
    26  
    27      luaL_checktype(L, 1, LUA_TSTRING);
    28      luaL_checktype(L, 2, LUA_TSTRING);
    29      luaL_checktype(L, 3, LUA_TSTRING);
    30      msg = (char *)lua_tostring(L, 1);
    31      sig = (char *)lua_tostring(L, 2);
    32      addr = (char *)lua_tostring(L, 3);
    33  
    34      ret = LuaECVerify(L, msg, sig, addr);
    35      if (ret.r1 != NULL) {
    36          strPushAndRelease(L, ret.r1);
    37          lua_error(L);
    38      }
    39  
    40      lua_pushboolean(L, ret.r0);
    41  
    42  	return 1;
    43  }
    44  
    45  static int crypto_verifyProof(lua_State *L)
    46  {
    47      int argc = lua_gettop(L);
    48      char *k, *v, *h;
    49      struct proof *proof;
    50      size_t kLen, vLen, hLen, nProof;
    51      int i, b;
    52      const int proofIndex = 4;
    53      if (argc < proofIndex) {
    54          lua_pushboolean(L, 0);
    55          return 1;
    56      }
    57      nProof = argc - (proofIndex - 1);
    58      k = (char *)lua_tolstring(L, 1, &kLen);
    59      v = (char *)lua_tolstring(L, 2, &vLen);
    60      h = (char *)lua_tolstring(L, 3, &hLen);
    61      proof = (struct proof *)malloc(sizeof(struct proof) * nProof);
    62      for (i = proofIndex; i <= argc; ++i) {
    63          proof[i-proofIndex].data = (char *)lua_tolstring(L, i, &proof[i-proofIndex].len);
    64      }
    65      b = LuaCryptoVerifyProof(k, kLen, v, vLen, h, hLen, proof, nProof);
    66      free(proof);
    67      lua_pushboolean(L, b);
    68      return 1;
    69  }
    70  
    71  static int crypto_keccak256(lua_State *L)
    72  {
    73      size_t len;
    74      char *arg;
    75      struct LuaCryptoKeccak256_return ret;
    76  
    77      luaL_checktype(L, 1, LUA_TSTRING);
    78      arg = (char *)lua_tolstring(L, 1, &len);
    79  
    80      ret = LuaCryptoKeccak256(arg, len);
    81      lua_pushlstring(L, ret.r0, ret.r1);
    82      free(ret.r0);
    83  	return 1;
    84  }
    85  
    86  static const luaL_Reg crypto_lib[] = {
    87  	{"sha256", crypto_sha256},
    88  	{"ecverify", crypto_ecverify},
    89  	{"verifyProof", crypto_verifyProof},
    90  	{"keccak256", crypto_keccak256},
    91  	{NULL, NULL}
    92  };
    93  
    94  int luaopen_crypto(lua_State *L)
    95  {
    96  	luaL_register(L, "crypto", crypto_lib);
    97  	lua_pop(L, 1);
    98  	return 1;
    99  }