Start on SWIG for minimal interface

This commit is contained in:
Ramana Kumar 2022-09-29 19:36:17 +01:00
parent 24212039b2
commit 2628661be5
No known key found for this signature in database
GPG Key ID: ED471C788B900433
1 changed files with 63 additions and 0 deletions

View File

@ -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"