herumi / mcl

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

SetStr on G2 issue #194

Closed xfaber closed 4 months ago

xfaber commented 4 months ago

I want to set a PK to this value (with curve BLS12-381) "0xa0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c315425 41771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940 766d9a5b9db91e3b697e5d70a975181e007f87fca5e" after the SetStr, the value is zero. Is there any problem with the length of the value?

var PKhexstring = "0xa0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c315425 41771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940 766d9a5b9db91e3b697e5d70a975181e007f87fca5e"; var PK = new G2(); PK.SetStr(PKhexstring, 16); var check = PK.IsValid(); //=TRUE var check0 = PK.IsZero(); //=TRUE

How I can set the value PKhexstring? Thanks for your help. F

herumi commented 4 months ago

If the value of G2 is the form (x, y), set "1 " where and are in Fp2, then they are two elements of Fp. see https://github.com/herumi/mcl/blob/master/api.md#string-conversion The string length you want to set is 192, so it is a concatenation of 4 elements of Fp. So you should split the string into 4 elements x1, x2, x3 and x4, and set "1 ". But I don't know the original string format so that it may be "1 " or so.

herumi commented 4 months ago

If the string format is big-endian, you must reverse each Fp element. e.g., 0x123456 => 0x563412 .

herumi commented 4 months ago

If you get the string from the other library, and if the library supports ETH specification, then it is better to use Deserialize() for the other library output of serialize() after calling ETHmode() once.

xfaber commented 4 months ago

The PKhexstring is a compressed PK form (bigendian)

herumi commented 4 months ago

Then, could you use PK.Deserialize to the data by the following FromHexStr, for example?

        public static byte[] FromHexStr(string s)
        {
            if (s.Length % 2 == 1) {
                throw new ArgumentException("s.Length is odd." + s.Length);
            }
            int n = s.Length / 2;
            var buf = new byte[n];
            for (int i = 0; i < n; i++) {
                buf[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
            }
            return buf;
        }