Example #1
0
        public static unsafe bool Ecdh(byte[] agreement, byte[] publicKey, byte[] privateKey)
        {
            int outputLength = agreement.Length;

            secp256k1_ecdh_hash_function hashFunctionPtr = (void *output, void *x, void *y, IntPtr d) =>
            {
                var outputSpan = new Span <byte>(output, outputLength);
                var xSpan      = new Span <byte>(x, 32);
                if (xSpan.Length < 32)
                {
                    return(0);
                }

                xSpan.CopyTo(outputSpan);
                return(1);
            };

            GCHandle gch = GCHandle.Alloc(hashFunctionPtr);

            try
            {
                IntPtr fp = Marshal.GetFunctionPointerForDelegate(hashFunctionPtr);
                {
                    return(Platform == OsPlatform.Windows
                        ? Win64Lib.secp256k1_ecdh(Context, agreement, publicKey, privateKey, fp, IntPtr.Zero)
                        : Platform == OsPlatform.Linux
                            ? PosixLib.secp256k1_ecdh(Context, agreement, publicKey, privateKey, fp, IntPtr.Zero)
                            : MacLib.secp256k1_ecdh(Context, agreement, publicKey, privateKey, fp, IntPtr.Zero));
                }
            }
            finally
            {
                gch.Free();
            }
        }