herumi / mcl

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

Fr.setHashOf() function behaviour #120

Closed Henzi96 closed 3 years ago

Henzi96 commented 3 years ago

Hello i would like to ask you how this function works ? I am using this function in my QR generator. Let's say, that i have input "herumiMCL" My question is, which steps are included in a process of hashing ? Which hashing function is used ? SHA-256 ? I assume the following procedure:

  1. SHA-256("herumiMCL")
  2. Converting output to BigInteger
  3. Modular reduction of output ( somethink like: output.mod(q) )

Thank you for answer !

herumi commented 3 years ago

The behavior of setHashOf is not what you expected. Please exec the following function for what you want.

template<class F>
void setHash(F& x, const void *msg, size_t msgSize)
{
    uint8_t md[32];
    mcl::fp::sha256(md, sizeof(md), msg, msgSize);
    x.setBigEndianMod(md, sizeof(md));
}
Henzi96 commented 3 years ago

Hello, I tried to use the setHashOf() method of the mcl library. Unfortunately, it didn't work for me. The Android system reports an error :

A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 17126

Would it be possible to give me the exact procedure to implement the function in Java ? I am in a hurry because of the deadline. I would like to know how I could implement the identical function using classic Java functions (mod) and objects (BigInteger).

Suppose I have an input "ABC". So how to proceed? Convert this string to an array of bytes first, hash it with SHA-256 and modularly reduce it by the order of the group q ?

herumi commented 3 years ago

I'll check the error later. If you use Java, then hash a string with SHA-256 of Java API and pass the 32-byte data to setLittleEndianMod. If you treat it as a big endian, swap the 32 bytes.

Henzi96 commented 3 years ago

Thank you for your prompt reply. I did some research and came up with an interesting thing. I sent the word "Male" to the SHA-256 hash function within the Java API. The function returned this string to me: 03F8C1273E3DA99BB315FE7B71C8E45159C826E6D3F42EECD4612078248F73A1

If I convert the given string to LittleEndian, I get this string:

A1738F24782061D4EC2EF4D3E626C85951E4C8717BFE15B39BA93D3E27C1F803

One of my colleagues uses the MCL library's setHashOf() function in his C application. If he sends the word "Male" to the method, he gets this output:

21738F24782061D4EC2EF4D3E626C85951E4C8717BFE15B39BA93D3E27C1F803

The hashes are more or less identical, with only a small difference (letter/number at the beginning). What accounts for this difference ?

herumi commented 3 years ago

The hashes are more or less identical, with only a small difference (letter/number at the beginning). What accounts for this difference ?

As I wrote at https://github.com/herumi/mcl/issues/120#issuecomment-830773908 , setHashOf is different from setHash defined in it.