herumi / mcl

a portable and fast pairing-based cryptography library
BSD 3-Clause "New" or "Revised" License
450 stars 152 forks source link

Difference of mcl-wasm between Node.js and wasm on Chrome #88

Closed kfkfkfkfkfkf closed 3 years ago

kfkfkfkfkfkf commented 3 years ago

I run hashMapToG2 method on Node.js and Chrome. Each results mutually different. I run mcl.init(0).then(()=>{}) at first.

return value of mcl.hashMapToG2("ABC") is as follows.

on Node.js Uint32Array [ 4231643088, 186483900, 1579834675, 2532212738, 2487224005, 4154472267, 472563795, 338080911, 0, 7265, 808595506, 942813488, 3022037666, 622963809, 2131975551, 3744725402, 1066469284, 79687653, 1134895680, 25972217, 0, 0, 0, 0, 1333155664, 1254971821, 4110240047, 4283821980, 2336250250, 3802218657, 2011476692, 600567000, 0, 0, 0, 0, 3840295081, 2756205684, 908125785, 750105577, 3706886338, 843457378, 732889473, 305838302, 0, 0, 0, 0, 2235888398, 3562826732, 1403545530, 1931697044, 635007739, 228647681, 645772411, 417137088, 0, 0, 0, 0, 216315475, 2604303592, 2251267847, 2566488181, 83558708, 1696980712, 2208970411, 599965009, 0, 0, 0, 0 ]

on Chrome Uint32Array(72) 0: 4231643088 1: 186483900 2: 1579834675 3: 2532212738 4: 2487224005 5: 4154472267 6: 472563795 7: 338080911 8: 0 9: 0 10: 0 11: 0 12: 3022037666 13: 622963809 14: 2131975551 15: 3744725402 16: 1066469284 17: 79687653 18: 1134895680 19: 25972217 20: 0 21: 0 22: 0 23: 0 24: 1333155664 25: 1254971821 26: 4110240047 27: 4283821980 28: 2336250250 29: 3802218657 30: 2011476692 31: 600567000 32: 0 33: 0 34: 0 35: 0 36: 3840295081 37: 2756205684 38: 908125785 39: 750105577 40: 3706886338 41: 843457378 42: 732889473 43: 305838302 44: 0 45: 0 46: 0 47: 0 48: 2235888398 49: 3562826732 50: 1403545530 51: 1931697044 52: 635007739 53: 228647681 54: 645772411 55: 417137088 56: 0 57: 0 58: 0 59: 0 60: 216315475 61: 2604303592 62: 2251267847 63: 2566488181 64: 83558708 65: 1696980712 66: 2208970411 67: 599965009 68: 0 69: 0 70: 0 71: 0

This difference affects later calculation result.

What is the factor of difference?

herumi commented 3 years ago

The difference is non-used memory. I thinks that serializeToHexStr() show the same value.

mcl.hashAndMapToG2('ABC').serializeToHexStr()
'f689ecb9ec3ebeca68356a7491e3a7b24ded053e1bebe68a635dc4e4061cc924e31c010f5de86553bf094de5cece85076506617fc0870be304bf045b609c9712'
kfkfkfkfkfkf commented 3 years ago

I try it,then both results were same. But, I want to use type of G2. On Chrome and Node.js, this comparison is true. mcl.hashAndMapToG2('ABC') != new mcl.G2().deserializeHexStr(mcl.hashAndMapToG2('ABC').serializeToHexStr())

What the architecture should i select for using G2 on both frontend and backend?

herumi commented 3 years ago

To compare the value of G2, you have to use G2.isEqual method.

>const a = mcl.hashAndMapToG2('ABC')
>a.isEqual(mcl.deserializeHexStrToG2(a.serializeToHexStr()))
true