Add some rudimentary Python access + example

This commit is contained in:
Ramana Kumar 2022-09-18 19:50:49 +01:00
parent cd404dbb0a
commit 560127f1f5
No known key found for this signature in database
GPG Key ID: ED471C788B900433
3 changed files with 58 additions and 3 deletions

4
.gitignore vendored
View File

@ -16,6 +16,6 @@ inc/blst_aux.h*
*.json
.clang-format
bindings/*/_*.so
bindings/python/*.py
bindings/python/*_wrap.c
bindings/python/ckzg.py
bindings/python/c_kzg_wrap.c
__pycache__

View File

@ -1,11 +1,50 @@
%module ckzg
%include "stdint.i"
%include "carrays.i"
%include "cpointer.i"
%{
#include <stdbool.h>
#include "c_kzg.h"
#include "bls12_381.h"
%}
%rename(alloc_poly) new_poly;
%rename(alloc_poly_l) new_poly_l;
%typemap(in, numinputs=0) OBJECT *out ($1_basetype tmp) { $1 = &tmp; }
%typemap(argout) OBJECT *out {
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);
}
%apply OBJECT *out { fr_t *out, poly *out, poly_l *out }
%typemap(in, numinputs=0) uint64_t out[4] (uint64_t tmp[4]) { $1 = tmp; }
%typemap(argout) uint64_t out[4] {
PyObject *obj = PyTuple_Pack(4,
PyLong_FromUnsignedLongLong($1[0]),
PyLong_FromUnsignedLongLong($1[1]),
PyLong_FromUnsignedLongLong($1[2]),
PyLong_FromUnsignedLongLong($1[3]));
$result = ($result == NULL) ? obj : SWIG_Python_AppendOutput($result, obj);
}
%typemap(in) const uint64_t[4] (uint64_t tmp[4]) {
int i;
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;
}
}
%array_class(fr_t, frArray)
%pointer_class(poly, polyp)
%pointer_class(poly_l, poly_lp)
%include "../src/c_kzg.h"
%array_class(fr_t, fr_array)
%include "../src/bls12_381.h"

View File

@ -0,0 +1,16 @@
import ckzg
c1 = ckzg.fr_from_uint64s((12,13,0,0))
c2 = ckzg.fr_from_uint64(2)
c3 = ckzg.fr_from_uint64s((1,0,0,0))
c4 = ckzg.fr_sub(c2, c3)
assert ckzg.fr_is_one(c4)
assert ckzg.fr_equal(c3, c4)
coeffs = [c1, c2, c3, c4]
cfa = ckzg.frArray(len(coeffs))
for i, c in enumerate(coeffs):
cfa[i] = c
ret, pptr = ckzg.new_poly_with_coeffs(cfa.cast(), len(coeffs))
p = ckzg.polyp_frompointer(pptr).value()
assert p.length == 4
pcoeffs = ckzg.frArray_frompointer(p.coeffs)
assert ckzg.fr_to_uint64s(pcoeffs[1]) == (2, 0, 0, 0)