herumi / mcl

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

mcl-wasm: Clarification or possible error in deserializeHexStrToFp #60

Closed mhewett-ks closed 5 years ago

mhewett-ks commented 5 years ago

Is Fp supposed to be a number in the range [0..p] where p defines the modular field of the elliptic curve? In BLS12-381, p is 381 bits long so Fp should also be 381 bits long. However, deserializeHexStrToFp limits the number to 256 bits.

Unable to deserialize 'eca3a7bd15e08e0f6771805cbc4b8905654e9d356d5398a2aa4cbc71a55017c'
test.js:23975 Error: fromHexStr:length must be even 63
    at Object.exports.fromHexStr (test.js:22820)
    at exports.Fp.deserializeHexStr (test.js:22950)
    at Object.exports.deserializeHexStrToFp (test.js:23113)

What I actually want to generate is a random number in [0..p). Does mcl-wasm provide a function to do that?

herumi commented 5 years ago

However, deserializeHexStrToFp limits the number to 256 bits.

The elliptic curve is defined over Fp and the order of the curve is r. For BLS12-381, |p| = 381 and |r| = 255. see https://github.com/herumi/mcl#curve-parameter .

What I actually want to generate is a random number in [0..p)

Please use setByCSPRNG().

let a = new mcl.Fr()
a.setByCSPRNG()
a.getStr()
"3030761079396743944577226857910186964859260808529341867926635497195550768241"