在Charm-crypto库中对GT元素执行加减操作时出现SystemError
解决Charm-crypto中GT群元素减法触发的SystemError问题
问题重现
执行以下代码时触发SystemError: error return without exception set:
from charm.toolbox.pairinggroup import PairingGroup, ZR, G1, G2, GT, pair groupObj = PairingGroup('MNT224') gt = groupObj.random(GT) g2 = groupObj.random(GT) result = gt -g2
运行环境:Linux Ubuntu22.04、charm-crypt 0.5、Python3.10、libgmp5.1、openssl1.0
错误栈:
Traceback (most recent call last): File "/home/x/ex/scheme.py", line 284, in <module> main() File "/home/x/ex/scheme.py", line 252, in main gt - g2 SystemError: error return without exception set
问题原因
GT群是配对运算生成的乘法循环群,仅支持乘法、除法(乘法逆元)、指数运算,不支持加法、减法这类加法群操作。Charm-crypto的GT元素实现未重载减法运算符,直接调用会导致底层C扩展抛出错误但未正确封装为Python异常,从而触发该SystemError。
解决方案
乘法群中的"减法"等价于乘以被减数的逆元,可以用以下两种方式替代减法操作:
方式1:乘以逆元
result = gt * g2 ** -1
方式2:使用除法语法糖
result = gt / g2
补充:Charm-crypto各群运算支持
- ZR(整数群):支持加减乘除所有基础运算
- G1/G2(椭圆曲线群):支持加法、减法、数乘运算
- GT(配对结果群):仅支持乘法、除法、指数运算
内容的提问来源于stack exchange,提问作者user24053132
相关产品推荐
相关产品推荐

