From 2628661be5ff1aa9911587354ff56382bf2ae2fe Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Thu, 29 Sep 2022 19:36:17 +0100 Subject: [PATCH] Start on SWIG for minimal interface --- min-bindings/c_kzg_4844.swg | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 min-bindings/c_kzg_4844.swg diff --git a/min-bindings/c_kzg_4844.swg b/min-bindings/c_kzg_4844.swg new file mode 100644 index 0000000..0397d6e --- /dev/null +++ b/min-bindings/c_kzg_4844.swg @@ -0,0 +1,63 @@ +%module ckzg +%{ +#include "c_kzg_4844.h" +%} +%include "stdint.i" +%include "carrays.i" +%include "cpointer.i" + +%typemap(in, numinputs=0) OBJECT *OUTPUT($1_basetype tmp) { $1 = &tmp; } +%typemap(argout) OBJECT *OUTPUT { +#if defined(SWIGPYTHON) + PyObject *obj = SWIG_NewPointerObj(memcpy(malloc(sizeof($1_basetype)),$1,sizeof($1_basetype)), + $descriptor, SWIG_POINTER_NEW); + $result = ($result == NULL) ? obj : SWIG_Python_AppendOutput($result, obj); +#endif +} +%apply OBJECT *OUTPUT { + KZGSettings *out, BLSFieldElement *out, PolynomialEvalForm *out, + KZGCommitment *out, KZGProof *out +} + +%typemap(in, numinputs=0) bool *OUTPUT(bool tmp) { $1 = &tmp; } +%typemap(argout) bool *OUTPUT { +#if defined(SWIGPYTHON) + PyObject *obj = *$1 ? Py_True : Py_False; + $result = ($result == NULL) ? obj : SWIG_Python_AppendOutput($result, obj); +#endif +} +%apply bool *OUTPUT { bool *out } + +%typemap(in, numinputs=0) uint64_t out[4] (uint64_t tmp[4]) { $1 = tmp; } +%typemap(argout) uint64_t out[4] { +#if defined(SWIGPYTHON) + PyObject *obj = PyTuple_Pack(4, + PyLong_FromUnsignedLong($1[0]), + PyLong_FromUnsignedLong($1[1]), + PyLong_FromUnsignedLong($1[2]), + PyLong_FromUnsignedLong($1[3])); + $result = ($result == NULL) ? obj : SWIG_Python_AppendOutput($result, obj); +#endif +} + +%typemap(in) const uint64_t[4] (uint64_t tmp[4]) { +#if defined(SWIGPYTHON) + if (PyTuple_Check($input)) { + if (!PyArg_ParseTuple($input, "kkkk", tmp, tmp+1, tmp+2, tmp+3)) { + PyErr_SetString(PyExc_TypeError, "tuple must have 4 elements"); + SWIG_fail; + } + $1 = &tmp[0]; + } else { + PyErr_SetString(PyExc_TypeError, "expected a tuple."); + SWIG_fail; + } +#endif +} + +%array_class(scalar_t, scalars) +%array_class(BLSFieldElement, BLSFieldElements) +%pointer_class(PolynomialEvalForm, PolynomialEvalFormPtr) + +%include "../min-src/c_kzg_4844.h" +%include "../inc/blst.h"