diff --git a/include/wrapping_dap_hash.h b/include/wrapping_dap_hash.h new file mode 100644 index 0000000000000000000000000000000000000000..1d80fab57ca7fa50b72383dc9d76995b98dce569 --- /dev/null +++ b/include/wrapping_dap_hash.h @@ -0,0 +1,136 @@ +#ifndef _WRAPPING_DAP_HASH_ +#define _WRAPPING_DAP_HASH_ + +#include <Python.h> +#include "dap_hash.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct PyDapHashType{ + PyObject_HEAD + dap_hash_type_t hash_type; +}PyDapHashTypeObject; + +PyObject *DAP_HASH_TYPE_KECCAK_PY(); +PyObject *DAP_HASH_TYPE_SLOW_0_PY(); + +static PyMethodDef DapHashTypeMethods[] = { + {"DAP_HASH_TYPE_KECCAK", (PyCFunction)DAP_HASH_TYPE_KECCAK_PY, METH_NOARGS | METH_STATIC, ""}, + {"DAP_HASH_TYPE_SLOW_0", (PyCFunction)DAP_HASH_TYPE_SLOW_0_PY, METH_NOARGS | METH_STATIC, ""}, + {NULL, NULL, 0, NULL} +}; + +static PyTypeObject DapChainHashTypeObject_DapChainHashTypeObjectType = { + PyVarObject_HEAD_INIT(NULL, 0) + "CellFrame.ChainHashType", /* tp_name */ + sizeof(PyDapHashTypeObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Chain hash type object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DapHashTypeMethods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + +/*=================*/ + +/* Chain hash fast */ +typedef struct PyDapChainHashFast{ + PyObject_HEAD + dap_chain_hash_fast_t *hash_fast; +}PyDapChainHashFastObject; + +PyObject *dap_chain_str_to_hash_fast_py(PyObject *self, PyObject *args); +PyObject *dap_hash_fast_py(PyObject *self, PyObject *args); +PyObject *dap_hash_fast_compare_py(PyObject *self, PyObject *args); +PyObject *dap_hash_fast_is_blank_py(PyObject *self, PyObject *args); +PyObject *dap_chain_hash_fast_to_str_py(PyObject *self, PyObject *args); + +static PyMethodDef DapHashFastMethods[] = { + {"strToHashFast", (PyCFunction)dap_chain_str_to_hash_fast_py, METH_VARARGS | METH_STATIC, ""}, + {"hashFast", (PyCFunction)dap_hash_fast_py, METH_VARARGS, ""}, + {"compare", (PyCFunction)dap_hash_fast_compare_py, METH_VARARGS | METH_STATIC, ""}, + {"isBlank", (PyCFunction)dap_hash_fast_is_blank_py, METH_VARARGS, ""}, + {"toStr", (PyCFunction)dap_chain_hash_fast_to_str_py, METH_VARARGS, ""}, + {NULL, NULL, 0, NULL} +}; + +static PyTypeObject DapChainHashFastObject_DapChainHashFastObjectType = { + PyVarObject_HEAD_INIT(NULL, 0) + "CellFrame.ChainFast", /* tp_name */ + sizeof(PyDapChainHashFastObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Chain hash fast object", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DapHashFastMethods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew, /* tp_new */ +}; + +#ifdef __cplusplus +} +#endif + +#endif // _WRAPPING_DAP_HASH_ diff --git a/src/wrapping_dap_hash.c b/src/wrapping_dap_hash.c new file mode 100644 index 0000000000000000000000000000000000000000..06d1227ea7c9b27a60d475801d82002e47a58efb --- /dev/null +++ b/src/wrapping_dap_hash.c @@ -0,0 +1,63 @@ +#include "wrapping_dap_hash.h" + +PyObject *DAP_HASH_TYPE_KECCAK_PY(){ + PyObject *obj = _PyObject_New(&DapChainHashTypeObject_DapChainHashTypeObjectType); + ((PyDapHashTypeObject*)obj)->hash_type = DAP_HASH_TYPE_KECCAK; + return Py_BuildValue("O", obj); +} +PyObject *DAP_HASH_TYPE_SLOW_0_PY(){ + PyObject *obj = _PyObject_New(&DapChainHashTypeObject_DapChainHashTypeObjectType); + ((PyDapHashTypeObject*)obj)->hash_type = DAP_HASH_TYPE_SLOW_0; + return Py_BuildValue("O", obj); +} + +PyObject *dap_chain_str_to_hash_fast_py(PyObject *self, PyObject *args){ + const char *hash_str; + PyObject *obj_hash_fast; + if (!PyArg_ParseTuple(args, "s|O", &hash_str, &obj_hash_fast)) + return NULL; + int res = dap_chain_str_to_hash_fast(hash_str, ((PyDapChainHashFastObject*)obj_hash_fast)->hash_fast); + return Py_BuildValue("nO", res, obj_hash_fast); +} + +PyObject *dap_hash_fast_py(PyObject *self, PyObject *args){ + PyObject *obj_bytes; + size_t data_in_size; + if (!PyArg_ParseTuple(args, "O|n", &obj_bytes, &data_in_size)) + return NULL; + const void *bytes = (void*)PyBytes_AsString(obj_bytes); + bool res = dap_hash_fast(bytes, data_in_size, ((PyDapChainHashFastObject*)self)->hash_fast); + if (res) + return Py_BuildValue("O", Py_True); + else + return Py_BuildValue("O", Py_False); +} + +PyObject *dap_hash_fast_compare_py(PyObject *self, PyObject *args){ + PyObject *hash1; + PyObject *hash2; + if (!PyArg_ParseTuple(args, "O|O", &hash1, &hash2)) + return NULL; + bool res = dap_hash_fast_compare(((PyDapChainHashFastObject*)hash1)->hash_fast, ((PyDapChainHashFastObject*)hash2)->hash_fast); + if (res) + return Py_BuildValue("O", Py_True); + else + return Py_BuildValue("O", Py_False); +} + +PyObject *dap_hash_fast_is_blank_py(PyObject *self, PyObject *args){ + bool res = dap_hash_fast_is_blank(((PyDapChainHashFastObject*)self)->hash_fast); + if (res) + return Py_BuildValue("O", Py_True); + else + return Py_BuildValue("O", Py_False); +} + +PyObject *dap_chain_hash_fast_to_str_py(PyObject *self, PyObject *args){ + char *str; + size_t str_max; + if (!PyArg_ParseTuple(args, "s|n", &str, &str_max)) + return NULL; + int res = dap_chain_hash_fast_to_str(((PyDapChainHashFastObject*)self)->hash_fast, str, str_max); + return Py_BuildValue("sn", &str, &str_max); +}