Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions bindings/blst.swg
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ static int PyLong_GetSign(PyObject *obj, int *sign)

#ifdef __cplusplus
%typemap(in) (const POINT* points[], size_t npoints)
(std::unique_ptr<$*1_ltype[]> points, size_t _global_npoints) %{
(std::unique_ptr<$*1_ltype[]> points, size_t _global_npoints,
Py_buffer view = {}) %{
if (PyList_Check($input)) {
_global_npoints = PyList_Size($input);

Expand Down Expand Up @@ -253,17 +254,19 @@ static int PyLong_GetSign(PyObject *obj, int *sign)
$1 = points.get();
$2 = _global_npoints = nbytes / sizeof(*points[0]);
} else if (PyMemoryView_Check($input)) { // output from to_affine()
Py_buffer *buf = PyMemoryView_GET_BUFFER($input);
// rejects released and non-contiguous views, holds the buffer
if (PyObject_GetBuffer($input, &view, PyBUF_SIMPLE) < 0)
SWIG_fail;

if (!PyBytes_Check(buf->obj))
if (!PyBytes_Check(PyMemoryView_GET_BUFFER($input)->obj))
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting 'bytes' in "
"'memoryview' for points[]");
points = std::unique_ptr<$*1_ltype[]>(new $*1_ltype[2]);
points[0] = ($*1_ltype)buf->buf;
points[0] = ($*1_ltype)view.buf;
points[1] = nullptr;
$1 = points.get();
$2 = _global_npoints = buf->len / sizeof(*points[0]);
$2 = _global_npoints = view.len / sizeof(*points[0]);
} else {
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', expecting "
"'list', 'bytes' or 'memoryview' "
Expand All @@ -273,6 +276,8 @@ static int PyLong_GetSign(PyObject *obj, int *sign)
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', expecting "
"non-empty set of points[]");
%}
%typemap(freearg) (const POINT* points[], size_t npoints)
%{ PyBuffer_Release(&view$argnum); %}
%apply (const POINT* points[], size_t npoints) {
(const blst::P1_Affine* const points[], size_t npoints),
(const blst::P2_Affine* const points[], size_t npoints),
Expand Down Expand Up @@ -320,7 +325,7 @@ static int PyLong_GetSign(PyObject *obj, int *sign)
%nodefault blst::P2_Affines;

%typemap(in) (const byte* const scalars[], size_t nbits)
(std::unique_ptr<byte[]> bytes, byte *scalars[2]) %{
(std::unique_ptr<byte[]> bytes, byte *scalars[2], Py_buffer view = {}) %{
if (PyList_Check($input)) {
if ((size_t)PyList_Size($input) != _global_npoints)
SWIG_exception_fail(SWIG_IndexError, "in method '$symname', 'list' "
Expand Down Expand Up @@ -376,16 +381,19 @@ static int PyLong_GetSign(PyObject *obj, int *sign)
$1 = scalars;
$2 = 8 * (PyByteArray_Size($input) / _global_npoints);
} else if (PyMemoryView_Check($input)) {
Py_buffer *buf = PyMemoryView_GET_BUFFER($input);
// rejects released and non-contiguous views, holds the buffer
if (PyObject_GetBuffer($input, &view, PyBUF_SIMPLE) < 0)
SWIG_fail;

if (!PyBytes_Check(buf->obj) && !PyByteArray_Check(buf->obj))
PyObject *obj = PyMemoryView_GET_BUFFER($input)->obj;
if (!PyBytes_Check(obj) && !PyByteArray_Check(obj))
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting 'bytes' in "
"'memoryview' for points[]");
scalars[0] = ($*1_ltype)buf->buf;
scalars[0] = ($*1_ltype)view.buf;
scalars[1] = nullptr;
$1 = scalars;
$2 = 8 * (buf->len / _global_npoints);
$2 = 8 * (view.len / _global_npoints);
} else {
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', expecting "
"'list', 'bytes', 'bytearray' "
Expand All @@ -395,6 +403,8 @@ static int PyLong_GetSign(PyObject *obj, int *sign)
SWIG_exception_fail(SWIG_ValueError, "in method '$symname', zero-length "
"scalars, likely an error");
%}
%typemap(freearg) (const byte* const scalars[], size_t nbits)
%{ PyBuffer_Release(&view$argnum); %}

%typemap(out) BLST_ERROR %{
if ($1 != BLST_SUCCESS) {
Expand Down
16 changes: 16 additions & 0 deletions bindings/python/run.me
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ a = blst.P2_Affines.mult_pippenger(blst.P2_Affines.as_memory(points), scalars)
if not a.is_equal(blst.G2().mult(total)):
raise AssertionError("disaster")

# released or non-contiguous memoryview-s are to be rejected
mem = memoryview(bytes(blst.P2_Affines.as_memory(points)))
for args in [(mem[::-1], scalars),
(blst.P2_Affines.as_memory(points), memoryview(bytes(8*42))[::-1])]:
try:
blst.P2_Affines.mult_pippenger(*args)
raise AssertionError("disaster")
except BufferError:
pass
mem.release()
try:
blst.P2_Affines.mult_pippenger(mem, scalars)
raise AssertionError("disaster")
except ValueError:
pass

########################################################################
# rudimentary blind signature PoC

Expand Down
Loading