Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python3 pypbc meet error ”Fatal Python error: Segmentation fault“ #12

Open
JessKXWL opened this issue Dec 9, 2021 · 0 comments
Open

Comments

@JessKXWL
Copy link

JessKXWL commented Dec 9, 2021

import faulthandler
faulthandler.enable()

from pypbc import *
# from utils import hash_nosafe
import datetime
import hashlib

Hash1 = hashlib.sha256

#这里的内容就可以换成pbc中param文件夹下的几种曲线参数了,但是“”“是要保留的哦。
stored_params = """type a
q 8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791
h 12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776
r 730750818665451621361119245571504901405976559617
exp2 159
exp1 107
sign1 1
sign0 1
"""

# 密钥生成算法,输入安全参数qbits和rbits,返回[params, g, pk, sk]
# def KeyGen(qbits=512, rbits=160):
def KeyGen():
    params = Parameters(param_string=stored_params)   #参数初始化
    pairing = Pairing(params)  # 根据参数实例化双线性对
    # 返回公共参数,PEKS是对称双线性对,G1=G2,二者的生成元是一样的,G1同样可以替换为G2
    g = Element.random(pairing, G1)  # g是G1的一个生成元
    sk = Element.random(pairing, Zr) # 私钥是一个素数域Zp内的随机数
    pk = Element(pairing, G1, value=g ** sk)   # 公钥是[g, h = g^α] α=sk
    return [params, g, sk, pk]

params = Parameters(param_string=stored_params)
pairing = Pairing(params)

class Pkeet(object):
    def __init__(self, g):
        self.params = params
        self.pairing =  pairing
        self.g = Element(self.pairing, G1, g)
        # self.sk = Element(self.pairing, Zr, value=int(sk, 16))
        # self.pk = Element(self.pairing, G1, value=pk)
        # 随机数r的长度
        self.r_len = 42
        # 随机数m的长度
        self.m_len = 130

    def enc(self, pk, data):
        # m = str(data).encode('utf-8').hex()
        # m = str(data)
        element_m = Element(self.pairing, G1, str(data))

        r = Element.random(self.pairing, Zr)  # 定义一个Zp内的随机数r
        U = Element(self.pairing, G1, self.g ** r)
        V = Element(self.pairing, G1, element_m ** r)
        W = Element(self.pairing, G1, pk ** r)

        # hash_W是hex字符串
        # hash_W = hash_nosafe(self.m_len+self.r_len, str(U), str(V), str(W))
        hash_W = Hash1((str(U)+str(V)+str(W)).encode('utf-8')).hexdigest()
        element_hash_W = Element.from_hash(self.pairing, G1, hash_W)
        hex_m = str(data).encode('utf-8').hex()
        W_m = hex(int(str(element_hash_W), 16) ^ int(hex_m, 16))

        W_r = hex(int(str(element_hash_W), 16) ^ int(str(r), 16))

        W = (W_m, W_r)

        return (U, V, W)

    def dec(self, sk, data):
        U, V, W = data
        W_m, W_r = W
        # sk = Element(self.pairing, Zr, value=int(sk, 16))
        U_x = Element(self.pairing, G1, U ** sk)
        hash_W = Hash1((str(U) + str(V) + str(U_x)).encode('utf-8')).hexdigest()
        element_hash_W = Element.from_hash(self.pairing, G1, hash_W)
        dec_m = hex(int(str(element_hash_W), 16)^int(W_m, 16))
        dec_r = hex(int(str(element_hash_W), 16)^int(W_r, 16))
        # print(dec_m)
        dec_m_1 = bytes.fromhex(dec_m[2:])
        dec_m_2 = dec_m_1.decode('utf-8')
        element_m = Element(self.pairing, G1, dec_m_2)
        element_r = Element(self.pairing, Zr, int(dec_r, 16))

        U1 = Element(self.pairing, G1, self.g ** element_r)
        V1 = element_m ** element_r

        # TODO V = m^r,这个程序里面不等于, 不知道为啥
        # if U == U1 and V == V1:
        if U == U1:
            msg = dec_m_2
            return msg
        return False

    def equal_test(self, C1, C2):
        U1, V1, W1 = C1
        U2, V2, W2 = C2
        rst1 = self.pairing.apply(U1, V2)
        rst2 = self.pairing.apply(U2, V1)
        if rst1 == rst2:
            return True
        return False

def test_equal():
    now = datetime.datetime.now()
    [params, g, sk, pk] = KeyGen()
    end = datetime.datetime.now()
    print("KeyGen运行时间", end - now)


    pkeet1 = Pkeet(g)

    now = datetime.datetime.now()
    result1 = pkeet1.enc(data="lable: 16", pk=pk)
    end = datetime.datetime.now()
    print("加密运行时间", end - now)

    now = datetime.datetime.now()
    pkeet1.dec(sk, result1)
    end = datetime.datetime.now()
    print("解密运行时间", end - now)

    # print(pkeet1.dec(sk, result1))

    [params, g, sk, pk] = KeyGen()
    pkeet2 = Pkeet(g)
    result2 = pkeet1.enc(data="lable: 16", pk=pk)


    # print(pkeet1.dec(sk, result2))
    now = datetime.datetime.now()
    rst = pkeet2.equal_test(result1, result2)
    end = datetime.datetime.now()
    print("等值测试运行时间", end - now)

    print(rst)

if __name__ == '__main__':
    test_equal()

When I tried the equivalence test scheme(https://link.springer.com/chapter/10.1007/978-3-642-11925-5_9),i meet the error "Fatal Python error: Segmentation fault", and i don't know why.

image

environment:Ubuntu+windows pycharm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant