Fixes Pairingmodule: Improve PyLongObject handling, optimize, and address Windows/MinGW issues #329
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This Pull Request introduces several improvements and fixes to the pairing module.
Problem Background:
Upon further testing, I discovered that my previous attempt in #328 did not fully resolve the underlying issues. I've created a simple test script,
test.py, that exposes several lingering problems:Key Contributions in this PR:
Refined
PyLongObjectSize Handling & Macro Naming:PyLongObj_SizeandPy_SET_SIZEmacros, crucial for correctly getting/setting the signed length of thePyLongObject'sob_digitarray, had incorrect logic, affecting Python 3.12+ environments. This can be demonstrated byb = group.init(ZR, -1)andm = group.order()respectively.PyLongObj_SizeandPy_SET_SIZEhas been refined to better interpret thelv_tagfield and extract the signed length. All related macros have been consistently renamed (e.g.,PyLongObj_SizeltoPyLong_SIZE) for better readability.lv_tagfield, which these macros interact with, appears to be an internal CPython design. Making reliance on its specific bit-level interpretation might be unstable (Python 3.14+ may introduce new C APIs per PEP 757).Fixed incorrect
PyLong_SHIFTwhen compiled with MinGW for Windows 64-bit System:PyLong_SHIFTvalue was erroneously set to 15 bits, leading to incorrectlongObjToMPZbehavior with very large integers. This can be demonstrated byc = group.init(ZR, m + 123456789)ord = group.init(ZR, -m - 123456789).MS_WIN64macro in the relevant header files, which setsSIZEOF_VOID_Pto 8 in<your_python>/include/pyconfig.h, which further ensuresPYLONG_BITS_IN_DIGITis set to 30 in<your_python>/include/pyport.h, leading to the correctPyLong_SHIFTvalue.Enhanced
Element_mulfor Larger Integer Support:Element_mulfunction, the variablezwas limited to asigned long inttype. This restricted its ability to process larger integer inputs, potentially causing overflows for certain operations. This can be demonstrated byprint("Z*ZR:\t", int(r1) * r2)orprint("ZR*Z:\t", r1 * int(r2)).zhas been changed fromsigned long inttompz_t. This allows the function to handle arbitrary-precision integers, expanding its capability to process much larger inputs without limitation.Optimized
longObjToMPZPerformance:longObjToMPZhas been fine-tuned to eliminate redundantmpzrelated operations.