JHUISI / charm

Charm: A Framework for Rapidly Prototyping Cryptosystems
http://charm-crypto.io
GNU Lesser General Public License v3.0
556 stars 167 forks source link

hashPair has some bits always equal 1 #195

Closed mike1729 closed 5 years ago

mike1729 commented 5 years ago

Hi all!

Here is a snippet for reproducing the problem:

from charm.toolbox.pairinggroup import PairingGroup, ZR from charm.core.math.pairing import hashPair from base64 import decodebytes

group = PairingGroup('MNT224') el = group.random(ZR)

[decodebytes(hashPair(el))[i] % 2 for i in range(0,48,3)]

The last line yields only ones. As I understand, hashPair is a sha2 of the element (btw in the code a comment says that it is sha1), so it should be random, while first bit is always 1, second is 1 with probability around 0.6, and the third seems to be random, and for next bits the story repeats.

Could you provide some info if this is a feature and I don't understand sth or there is some problem with hashPair.

jakinyele commented 5 years ago

Note that decodebytes expects bytes. hashPair returns a hex string that is encoded as a byte object. Therefore, there is no issue. You mean to call bytes.fromhex(hashPair(el) instead of base64.decodebytes.

Try this:

from charm.toolbox.pairinggroup import PairingGroup, ZR from charm.core.math.pairing import hashPair

group = PairingGroup('BN254') el = group.random(ZR)

res = hashPair(el) res = res.decode('utf8') print([bytes.fromhex(res)[i] % 2 for i in range(0,32,2)])