Skip to content
Merged
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
29 changes: 16 additions & 13 deletions charm/core/math/pairing/pairingmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@

#include "pairingmodule.h"

// PEP 757 – C API to import-export Python integers
#if PY_MINOR_VERSION <= 11
#define PythonLongVal(l, i) l->ob_digit[i]
#else
#define PythonLongVal(l, i) l->long_value.ob_digit[i]
#define PyLongObj_Val(l, i) (l)->ob_digit[i]
#define PyLongObj_Size(l) Py_SIZE(l)
#else
#define PyLongObj_Val(l, i) (l)->long_value.ob_digit[i]
// #define _PyLong_SIZE_SHIFTS __builtin_popcount(_PyLong_NON_SIZE_BITS)
#define _PyLong_SIZE_SHIFTS 2
#define PyLongObj_Size(l) ((l)->long_value.lv_tag >> _PyLong_SIZE_SHIFTS)
#endif

// PEP 674 – Disallow using macros as l-values
#if PY_MINOR_VERSION <= 10
#define PYTHON_SET_SIZE(l, i) Py_SIZE(l) = i
#else
#define PYTHON_SET_SIZE(l, i) Py_SET_SIZE(l, i);
#define Py_SET_SIZE(l, i) do { Py_SIZE(l) = (i); } while (0)
#endif

int exp_rule(GroupType lhs, GroupType rhs)
Expand Down Expand Up @@ -133,26 +137,25 @@ PyObject *mpzToLongObj (mpz_t m)
mpz_init_set (temp, m);
for (i = 0; i < size; i++)
{
PythonLongVal(l, i) = (digit) (mpz_get_ui (temp) & PyLong_MASK);
PyLongObj_Val(l, i) = (digit) (mpz_get_ui (temp) & PyLong_MASK);
mpz_fdiv_q_2exp (temp, temp, PyLong_SHIFT);
}
i = size;
while ((i > 0) && (PythonLongVal(l, i - 1) == 0))
while ((i > 0) && (PyLongObj_Val(l, i - 1) == 0))
i--;
if(isNeg) {
// Py_SET_SIZE(l,-i);
PYTHON_SET_SIZE(l, -i);
Py_SET_SIZE(l, -i);
}
else {
PYTHON_SET_SIZE(l, i);
Py_SET_SIZE(l, i);
}
mpz_clear (temp);
return (PyObject *) l;
}

void longObjToMPZ (mpz_t m, PyLongObject * p)
{
int size, i, tmp = Py_SIZE(p);
int size, i, tmp = PyLongObj_Size(p);
int isNeg = FALSE;
mpz_t temp, temp2;
mpz_init (temp);
Expand All @@ -166,7 +169,7 @@ void longObjToMPZ (mpz_t m, PyLongObject * p)
mpz_set_ui (m, 0);
for (i = 0; i < size; i++)
{
mpz_set_ui (temp, PythonLongVal(p, i));
mpz_set_ui (temp, PyLongObj_Val(p, i));
mpz_mul_2exp (temp2, temp, PyLong_SHIFT * i);
mpz_add (m, m, temp2);
}
Expand Down