charm icon indicating copy to clipboard operation
charm copied to clipboard

OverflowError: Python int too large to convert to C long

Open ShijiMariam opened this issue 8 years ago • 3 comments

I am trying to implement an identity based encryption scheme. I encountered a new issue when multiplying two pairing elements. The value will be computed and can be printed. But after that Overflow Error is showing.

There is no way I can figure out how to fix it. I have been stuck on this for a few days, please help thanks in advance!

ShijiMariam avatar Apr 27 '16 19:04 ShijiMariam

Hi, can you post a code snippet so I can reproduce the issue? I'll try to fix it as soon as I can. Thanks

jakinyele avatar Apr 27 '16 19:04 jakinyele

from charm.core.math.integer import integer,bitsize from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,pair from charm.toolbox.hash_module import Hash,int2Bytes,integer from charm.toolbox.IBEnc import IBEnc

debug = False class IBE_SmartGrid(IBEnc): def init(self, groupObj): print("\ninit") IBEnc.init(self) global group,h group = groupObj h = Hash('sha1', group)

def setup(self):
    print("\nsetup")
    s, g = group.random(ZR), group.random(G1)
    u = g ** s
    params = { 'G1':G1, 'G2':G2, 'g':group.serialize(g), 'u':group.serialize(u) }
    mk = { 'params':params, 's':group.serialize(s) }
    if(debug):
        print("Public parameters... and Secret parameters...")
        group.debug(mk)
    return mk

def extract(self, s, ID):
    #print("\nextract")
    k_ID = group.hash(ID, G1) ** group.deserialize(s)
    if(debug):
        #print("Key for id => '%s'" % ID)
        group.debug(k_ID)
    return (group.serialize(k_ID))

def encrypt(self, params, ID, M):
    #print("\nencrypt")
    sig = group.random(G2)
    enc_M = self.encodeToZn(M)
    r = h.hashToZr(sig, enc_M)
    if bitsize(enc_M) / 8 <= group.messageSize():
        C1 = group.deserialize(params['g']) ** r
        temp = pair(group.deserialize(params['u']), group.hash(ID, G1)) ** r
        C2 = sig * h.hashToZn(temp)
        C3 = enc_M ^ h.hashToZn(sig)
        C = { 'C1':group.serialize(C1), 'C2':group.serialize(C2), 'C3':int2Bytes(C3)}
    else:
        print("Message cannot be encoded.")
        return None
    if(debug):
        #print('\nEncrypt...')
        #print('C => %s' % C)
        group.debug(C)
    return C

def decrypt(self, params, k_ID, C):
    #print("\ndecrypt")
    sig = group.deserialize(C['C2']) / h.hashToZn(pair(group.deserialize(C['C1']), group.deserialize(k_ID)))
    dec_M = integer(C['C3']) ^ h.hashToZn(sig)
    M = self.decodeFromZn(dec_M)
    if(debug):
        #print('\nDecrypt....')
        print('M => %s' % M)
        group.debug(M)
    return M

this will work as long as nothing is printed. But I need to use this scheme in another program. So after decryption the program will terminate automatically displaying the overflow error.

ShijiMariam avatar Apr 27 '16 19:04 ShijiMariam

The issue can be resolved by replacing temp = pair(group.deserialize(params['u']), group.hash(ID, G1)) ** r C2 = sig * h.hashToZn(temp)

with

    C2 = sig * pair(group.deserialize(params['u']), group.hash(ID, G1)) ** r

I think the hash function is causing the problem. When I do

T = group.random(G2) k['k_ID'] ** -1 * group.hash(group.serialize(T), G1)

Again the same error arises.

Is there any hash function that maps from G1 to G2?

ShijiMariam avatar May 05 '16 12:05 ShijiMariam