Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetStr on G2 issue #194

Closed
xfaber opened this issue Feb 17, 2024 · 5 comments
Closed

SetStr on G2 issue #194

xfaber opened this issue Feb 17, 2024 · 5 comments

Comments

@xfaber
Copy link

xfaber commented Feb 17, 2024

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
Copy link
Owner

herumi commented Feb 18, 2024

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
Copy link
Owner

herumi commented Feb 18, 2024

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

@herumi
Copy link
Owner

herumi commented Feb 18, 2024

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
Copy link
Author

xfaber commented Feb 19, 2024

The PKhexstring is a compressed PK form (bigendian)

@herumi
Copy link
Owner

herumi commented Feb 19, 2024

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;
        }

@herumi herumi closed this as completed Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants