// Copyright (C) 2024 Vaughn Nugent // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . using System; using System.Runtime.InteropServices; using VNLib.Utils.Memory; namespace NVault.Crypto.Secp256k1 { /// /// Represents a Secp256k1 secret key, the size is fixed, and should use /// the sizeof() operator to get the size /// [StructLayout(LayoutKind.Sequential, Size = 32)] public unsafe struct Secp256k1SecretKey { private fixed byte data[32]; /// /// Implict cast to a span of raw bytes /// /// The secret key to cast public static implicit operator Span(Secp256k1SecretKey key) => new(key.data, 32); /// /// Casts the secret key span to a via a structure copy /// /// The key data to copy /// public static explicit operator Secp256k1SecretKey(ReadOnlySpan key) => FromSpan(key); /// /// Creates a new from a span of bytes /// by copying the bytes into the struct /// /// The secret key data to copy /// An initilaized public static Secp256k1SecretKey FromSpan(ReadOnlySpan span) { Secp256k1SecretKey newKey = new(); MemoryUtil.CopyStruct(span, ref newKey); return newKey; } } }