herumi / bls

288 stars 132 forks source link

Initial value of mclBnFr, mclBnG1 and mclBnG2 is random #99

Closed gogoex closed 1 year ago

gogoex commented 1 year ago

This issue is to solve the same underlying issue as #98.

When mclBnFr, mclBnG1 or mclBnG2 is instantiated, the initial value seems to be set to a random value. If a clear function is called, all the values under a type becomes zero.

For example, when I created three mclBnFr instances, I got:

>> mclBnFr
B0817FC7FC7F0000 7D90CB86F6550000 40827FC7FC7F0000 40827FC7FC7F0000 
B0817FC7FC7F0000 7D90CB86F6550000 40B7E488CF7F0000 40827FC7FC7F0000 
B0817FC7FC7F0000 7D90CB86F6550000 40B7E488CF7F0000 40827FC7FC7F0000 

There is a difference in the 3rd uint64_t values.

And if I call mclBnFr_clear before printint out the value, I see values set to all zeros.

>> mclBnFr
0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 

I observe the same symptom for mclBnG1 and mclBnG2 as well.

Since you have mentioned the values should have set to all zeros when an instance is created in #98, I assume this is not an expected behavior.

Here is a minimum example to reproduce this: https://github.com/gogoex/mcl-bls-sample

herumi commented 1 year ago

Since you have mentioned the values should have set to all zeros when an instance is created in https://github.com/herumi/bls/issues/98, I assume this is not an expected behavior.

I said that those values were zero for static variables. The behavior that the initial value of local variables is not zero is C/C++ specification.


#include <stdio.h>

struct A {
  int a[3];
  void put()
  {
    printf("%d %d %d\n", a[0], a[1], a[2]);
  }
};

int main()
{
  static A a1;
  a1.put(); // must be all zero
  A a2;
  a2.put(); // may be not zero
}```
gogoex commented 1 year ago

I see. I will check with static variable case then.

gogoex commented 1 year ago

I could confirm that the initial values are all set to zero when mclBnFr, mclBnG1 and mclBnG2 are instantiated as static variables. Thank you. I close this issue.

>>>>>> before blsInit, not calling clear function
>> mclBnFr
0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 
>> mclBnG1
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 
>> blsSignature (mclBnG2)
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 
0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000