mirror of
https://github.com/status-im/c-kzg-4844.git
synced 2025-01-11 02:35:53 +00:00
Add some rudimentary Python access + example
This commit is contained in:
parent
cd404dbb0a
commit
560127f1f5
4
.gitignore
vendored
4
.gitignore
vendored
@ -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__
|
||||
|
@ -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"
|
||||
|
16
bindings/python/example.py
Normal file
16
bindings/python/example.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user