Ejemplo n.º 1
0
        private void EnsurePublicKey()
        {
            if (_publicKey == null)
            {
                if (_privateKey != null)
                {
                    EnsureInitialised();

                    // Standard values are big endian encoding (are using the Herumi deserialize / serialize)

                    var blsSecretKey = default(Bls384Interop.BlsSecretKey);
                    int bytesRead;
                    unsafe
                    {
                        fixed(byte *ptr = _privateKey)
                        {
                            bytesRead = Bls384Interop.SecretKeyDeserialize(ref blsSecretKey, ptr, _privateKey.Length);
                        }
                    }

                    if (bytesRead != _privateKey.Length)
                    {
                        throw new Exception($"Error deserializing BLS private key, length: {bytesRead}");
                    }

                    var blsPublicKey = default(Bls384Interop.BlsPublicKey);
                    Bls384Interop.GetPublicKey(ref blsPublicKey, ref blsSecretKey);

                    var buffer = new Span <byte>(new byte[PublicKeyLength]);
                    int bytesWritten;
                    unsafe
                    {
                        fixed(byte *ptr = buffer)
                        {
                            bytesWritten = Bls384Interop.PublicKeySerialize(ptr, buffer.Length, ref blsPublicKey);
                        }
                    }

                    if (bytesWritten != buffer.Length)
                    {
                        throw new Exception($"Error serializing BLS public key, length: {bytesWritten}");
                    }

                    _publicKey = buffer.ToArray();
                }
            }
        }